-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfmt_text_view.c
3252 lines (2929 loc) · 79.5 KB
/
fmt_text_view.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* CLCL
*
* fmt_text_view.c
*
* Copyright (C) 1996-2019 by Ohno Tomoaki. All rights reserved.
* https://www.nakka.com/
*/
/* Include Files */
#define _INC_OLE
#include <windows.h>
#undef _INC_OLE
#include <tchar.h>
#ifdef OP_XP_STYLE
#include <uxtheme.h>
#include <vssym32.h>
#endif // OP_XP_STYLE
#include "General.h"
#include "Message.h"
#include "memory.h"
#include "string.h"
#include "Ini.h"
#include "Font.h"
#include "fmt_text_view.h"
#include "resource.h"
/* Define */
#define RESERVE_BUF 1024
#define RESERVE_INPUT 256
#define RESERVE_LINE 256
#define RESERVE_UNDO 256
#define DRAW_LEN 256
#define LINE_MAX 32768
#define TAB_STOP 8
// ホイールメッセージ
#ifndef WM_MOUSEWHEEL
#define WM_MOUSEWHEEL 0x020A
#endif
// XPテーマ変更通知
#ifndef WM_THEMECHANGED
#define WM_THEMECHANGED 0x031A
#endif
#ifndef IS_HIGH_SURROGATE
#define IS_HIGH_SURROGATE(wch) (((wch) & 0xfc00) == 0xd800)
#define IS_LOW_SURROGATE(wch) (((wch) & 0xfc00) == 0xdc00)
#define IS_SURROGATE_PAIR(hs, ls) (IS_HIGH_SURROGATE(hs) && IS_LOW_SURROGATE(ls))
#endif
#ifdef UNICODE
#define IS_LEAD_TBYTE(tb) IS_HIGH_SURROGATE(tb)
#else
#define IS_LEAD_TBYTE(tb) IsDBCSLeadByte(tb)
#endif
#define SWAP(a, b) {a = b - a; b -= a; a += b;}
#define BUF_LEN (bf->buf_len + bf->input_len - bf->del_len - bf->ip_len)
/* Global Variables */
// オプション
extern OPTION_INFO option;
/* Local Function Prototypes */
static BOOL string_to_clipboard(const HWND hWnd, const TCHAR *st, const TCHAR *en);
static void notify_message(const HWND hWnd, const BUFFER *bf, const int code);
static BOOL get_edit_rect(const HWND hWnd, const BUFFER *bf, RECT *rect);
static void set_scrollbar(const HWND hWnd, BUFFER *bf);
static void ensure_visible(const HWND hWnd, BUFFER *bf);
static BOOL is_lead_byte(const BUFFER *bf, TCHAR *p);
static int get_char_extent(BUFFER *bf, TCHAR *str, int *ret_len);
static TCHAR *char_next(const BUFFER *bf, TCHAR *p);
static TCHAR *char_prev(const BUFFER *bf, TCHAR *p);
static TCHAR *index_to_char(const BUFFER *bf, const DWORD index);
static DWORD char_to_index(const BUFFER *bf, const TCHAR *p);
static int index_to_line(const BUFFER *bf, const DWORD index);
static BOOL line_alloc(BUFFER *bf, const int i, const int size);
static BOOL line_move(BUFFER *bf, const int i, const int len);
static DWORD line_get(const BUFFER *bf, const int lindex);
static void line_set(BUFFER *bf, const int lindex, const DWORD len);
static void line_flush(BUFFER *bf);
static void line_add_length(BUFFER *bf, const int index, const int len);
static int line_get_length(const BUFFER *bf, const DWORD index);
static DWORD line_get_next_index(BUFFER *bf, const DWORD index);
static BOOL line_set_info(BUFFER *bf);
static int line_get_count(BUFFER *bf, const int lindex);
static int line_set_count(BUFFER *bf, const int lindex, const int old_cnt, const int line_cnt);
static void line_refresh(const HWND hWnd, const BUFFER *bf, const DWORD p, const DWORD r);
static BOOL undo_alloc(BUFFER *bf);
static void undo_free(BUFFER *bf, const int index);
static BOOL undo_set(BUFFER *bf, const int type, const DWORD st, const DWORD len);
static BOOL undo_exec(const HWND hWnd, BUFFER *bf);
static BOOL redo_exec(const HWND hWnd, BUFFER *bf);
static BOOL string_init(BUFFER *bf);
static BOOL string_set(const HWND hWnd, BUFFER *bf, const TCHAR *str, const DWORD len);
static BOOL string_insert(const HWND hWnd, BUFFER *bf, TCHAR *str, const int len, const BOOL insert_mode);
static void string_delete(const HWND hWnd, BUFFER *bf, DWORD st, DWORD en);
static void string_delete_char(const HWND hWnd, BUFFER *bf, DWORD st);
static BOOL string_flush(BUFFER *bf, const BOOL undo_flag);
static BOOL draw_init(const HWND hWnd, BUFFER *bf);
static void draw_free(BUFFER *bf);
static void draw_rect(const HWND hWnd, const HDC mdc, const BUFFER *bf, const int left, const int top, const int right, const int bottom);
static int draw_string(const HWND hWnd, const HDC mdc, const BUFFER *bf, const RECT *drect, const int left, const int top, const TCHAR *str, const int len, const BOOL sel);
static void draw_line(const HWND hWnd, const HDC mdc, BUFFER *bf, const int i, const int left, const int right);
static void caret_set_size(const HWND hWnd, BUFFER *bf);
static int caret_char_to_caret(const HDC mdc, BUFFER *bf, const int i, const DWORD cp);
static DWORD caret_point_to_caret(BUFFER *bf, const int x, const int y);
static void caret_get_token(BUFFER *bf);
static void caret_move(const HWND hWnd, BUFFER *bf, const int key);
static LRESULT CALLBACK nedit_proc(const HWND hWnd, const UINT msg, const WPARAM wParam, const LPARAM lParam);
/*
* font_select - フォントの選択
*/
static BOOL font_select(const HWND hWnd)
{
CHOOSEFONT cf;
LOGFONT lf;
HDC hdc;
// フォント情報の作成
ZeroMemory(&lf, sizeof(LOGFONT));
hdc = GetDC(NULL);
lf.lfHeight = -(int)((option.fmt_txt_font_size * GetDeviceCaps(hdc, LOGPIXELSY)) / 72);
ReleaseDC(NULL, hdc);
lf.lfWeight = option.fmt_txt_font_weight;
lf.lfItalic = option.fmt_txt_font_italic;
lf.lfCharSet = option.fmt_txt_font_charset;
lstrcpy(lf.lfFaceName, option.fmt_txt_font_name);
// フォント選択ダイアログを表示
ZeroMemory(&cf, sizeof(CHOOSEFONT));
cf.lStructSize = sizeof(CHOOSEFONT);
cf.hwndOwner = hWnd;
cf.lpLogFont = &lf;
cf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
cf.nFontType = SCREEN_FONTTYPE;
if (ChooseFont(&cf) == FALSE) {
return FALSE;
}
// 設定取得
mem_free(&option.fmt_txt_font_name);
option.fmt_txt_font_name = alloc_copy(lf.lfFaceName);
option.fmt_txt_font_weight = lf.lfWeight;
option.fmt_txt_font_italic = lf.lfItalic;
option.fmt_txt_font_charset = lf.lfCharSet;
option.fmt_txt_font_size = cf.iPointSize / 10;
return TRUE;
}
/*
* string_to_clipboard - 文字列をクリップボードに設定
*/
static BOOL string_to_clipboard(const HWND hWnd, const TCHAR *st, const TCHAR *en)
{
HANDLE hMem;
TCHAR *buf;
if (OpenClipboard(hWnd) == FALSE) {
return FALSE;
}
if (EmptyClipboard() == FALSE) {
CloseClipboard();
return FALSE;
}
if ((hMem = GlobalAlloc(GHND, sizeof(TCHAR) * (en - st + 1))) == NULL) {
CloseClipboard();
return FALSE;
}
if ((buf = GlobalLock(hMem)) == NULL) {
GlobalFree(hMem);
CloseClipboard();
return FALSE;
}
CopyMemory(buf, st, sizeof(TCHAR) * (en - st));
*(buf + (en - st)) = TEXT('\0');
GlobalUnlock(hMem);
#ifdef UNICODE
SetClipboardData(CF_UNICODETEXT, hMem);
#else
SetClipboardData(CF_TEXT, hMem);
#endif
CloseClipboard();
return TRUE;
}
/*
* notify_message - 親ウィンドウに通知
*/
static void notify_message(const HWND hWnd, const BUFFER *bf, const int code)
{
SendMessage(GetParent(hWnd), WM_COMMAND, MAKEWPARAM(bf->id, code), (LPARAM)hWnd);
}
/*
* get_edit_rect - 描画領域の取得
*/
static BOOL get_edit_rect(const HWND hWnd, const BUFFER *bf, RECT *rect)
{
BOOL ret;
ret = GetClientRect(hWnd, rect);
rect->left += bf->left_margin;
rect->top += bf->top_margin;
rect->right -= bf->right_margin;
rect->bottom -= bf->bottom_margin;
return ret;
}
/*
* set_scrollbar - スクロールバーの設定
*/
static void set_scrollbar(const HWND hWnd, BUFFER *bf)
{
SCROLLINFO si;
RECT rect;
get_edit_rect(hWnd, bf, &rect);
// 横スクロールバー
if (bf->wordwrap == FALSE && (rect.right - rect.left) < bf->line_width) {
int width = bf->line_width / bf->char_width + 1;
EnableScrollBar(hWnd, SB_HORZ, ESB_ENABLE_BOTH);
bf->max_x = width - ((rect.right - rect.left) / bf->char_width);
bf->pos_x = (bf->pos_x < bf->max_x) ? bf->pos_x : bf->max_x;
ZeroMemory(&si, sizeof(SCROLLINFO));
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
si.nPage = (rect.right - rect.left) / bf->char_width;
si.nMax = width - 1;
si.nPos = bf->pos_x;
SetScrollInfo(hWnd, SB_HORZ, &si, TRUE);
} else {
EnableScrollBar(hWnd, SB_HORZ, ESB_DISABLE_BOTH);
bf->pos_x = bf->max_x = 0;
ZeroMemory(&si, sizeof(SCROLLINFO));
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
si.nMax = (bf->wordwrap == TRUE) ? 0 : 1;
SetScrollInfo(hWnd, SB_HORZ, &si, TRUE);
}
// 縦スクロールバー
if (((rect.bottom - rect.top) / bf->font_height) < bf->line_len) {
EnableScrollBar(hWnd, SB_VERT, ESB_ENABLE_BOTH);
bf->max_y = bf->line_len - ((rect.bottom - rect.top) / bf->font_height);
bf->pos_y = (bf->pos_y < bf->max_y) ? bf->pos_y : bf->max_y;
ZeroMemory(&si, sizeof(SCROLLINFO));
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
si.nPage = (rect.bottom - rect.top) / bf->font_height;
si.nMax = bf->line_len - 1;
si.nPos = bf->pos_y;
SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
} else {
EnableScrollBar(hWnd, SB_VERT, ESB_DISABLE_BOTH);
bf->pos_y = bf->max_y = 0;
ZeroMemory(&si, sizeof(SCROLLINFO));
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
si.nMax = 1;
SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
}
}
/*
* ensure_visible - キャレット位置を表示
*/
static void ensure_visible(const HWND hWnd, BUFFER *bf)
{
RECT rect;
int i;
int x, y;
get_edit_rect(hWnd, bf, &rect);
i = index_to_line(bf, bf->cp);
x = caret_char_to_caret(bf->mdc, bf, i, bf->cp) + (bf->pos_x * bf->char_width);
y = i;
// x
i = bf->pos_x;
if (x < (bf->pos_x + 5) * bf->char_width) {
bf->pos_x = x / bf->char_width - 5;
if (bf->pos_x < 0) {
bf->pos_x = 0;
}
}
if (x > ((bf->pos_x - 5) * bf->char_width) + (rect.right - rect.left)) {
bf->pos_x = x / bf->char_width - (rect.right - rect.left) / bf->char_width + 5;
if (bf->pos_x > bf->max_x) {
bf->pos_x = bf->max_x;
}
}
if (i != bf->pos_x) {
SetScrollPos(hWnd, SB_HORZ, bf->pos_x, TRUE);
ScrollWindowEx(hWnd, (i - bf->pos_x) * bf->char_width, 0, NULL, &rect, NULL, NULL, SW_INVALIDATE | SW_ERASE);
}
// y
i = bf->pos_y;
if (y < bf->pos_y) {
bf->pos_y = y;
}
if (y > bf->pos_y + (rect.bottom - rect.top) / bf->font_height - 1) {
bf->pos_y = y - (rect.bottom - rect.top) / bf->font_height + 1;
if (bf->pos_y > bf->max_y) {
bf->pos_y = bf->max_y;
}
}
if (i != bf->pos_y) {
SetScrollPos(hWnd, SB_VERT, bf->pos_y, TRUE);
ScrollWindowEx(hWnd, 0, (i - bf->pos_y) * bf->font_height, NULL, &rect, NULL, NULL, SW_INVALIDATE | SW_ERASE);
}
}
/*
* is_lead_byte - マルチバイトの先頭バイトかチェック
*/
static BOOL is_lead_byte(const BUFFER *bf, TCHAR *p)
{
if (IS_LEAD_TBYTE((TBYTE)*p) == TRUE && char_to_index(bf, char_next(bf, p)) < BUF_LEN) {
return TRUE;
}
return FALSE;
}
/*
* get_char_extent - 文字の描画幅の取得
*/
static int get_char_extent(BUFFER *bf, TCHAR *str, int *ret_len)
{
SIZE sz;
int ret = 0;
#ifdef UNICODE
*ret_len = (is_lead_byte(bf, str) == TRUE) ? 2 : 1;
if (*ret_len == 1 && HIBYTE(*str) == 0 && *(bf->cwidth + LOBYTE(*str)) != 0) {
ret = *(bf->cwidth + LOBYTE(*str));
} else {
GetTextExtentPoint32(bf->mdc, str, *ret_len, &sz);
ret = sz.cx;
if (*ret_len == 1 && HIBYTE(*str) == 0 && sz.cx < 256) {
*(bf->cwidth + LOBYTE(*str)) = (BYTE)sz.cx;
}
}
#else
*ret_len = (is_lead_byte(bf, str) == TRUE) ? 2 : 1;
if (*ret_len == 1 && *(bf->cwidth + (unsigned char)*str) != 0) {
ret = *(bf->cwidth + (unsigned char)*str);
} else {
GetTextExtentPoint32(bf->mdc, str, *ret_len, &sz);
ret = sz.cx;
if (*ret_len == 1 && sz.cx < 256) {
*(bf->cwidth + (unsigned char)*str) = (BYTE)sz.cx;
}
}
#endif
return ret;
}
/*
* char_next - 次の文字を取得
*/
static TCHAR *char_next(const BUFFER *bf, TCHAR *p)
{
if (bf->dp != NULL && p == bf->dp - 1) {
return (bf->dp + bf->del_len);
}
if (bf->ip != NULL) {
if (p == bf->ip - 1) {
return bf->input_buf;
} else if (p == bf->input_buf + bf->input_len - 1) {
return (bf->ip + bf->ip_len);
}
}
return (p + 1);
}
/*
* char_prev - 前の文字を取得
*/
static TCHAR *char_prev(const BUFFER *bf, TCHAR *p)
{
if (bf->dp != NULL && p == bf->dp + bf->del_len) {
return (bf->dp - 1);
}
if (bf->ip != NULL) {
if (p == (bf->ip + bf->ip_len)) {
return (bf->input_buf + bf->input_len - 1);
} else if (p == bf->input_buf) {
return (bf->ip - 1);
}
}
return (p - 1);
}
/*
* index_to_char - 文字インデックスから文字位置を取得
*/
static TCHAR *index_to_char(const BUFFER *bf, const DWORD index)
{
TCHAR *p;
if (bf->dp != NULL && index >= (DWORD)(bf->dp - bf->buf)) {
p = bf->buf + index + bf->del_len;
} else if (bf->ip != NULL && index >= (DWORD)(bf->ip - bf->buf)) {
if (index < (DWORD)(bf->ip - bf->buf + bf->input_len)) {
p = bf->input_buf + (index - (bf->ip - bf->buf));
} else {
p = bf->buf + index - bf->input_len + bf->ip_len;
}
} else {
p = bf->buf + index;
}
return p;
}
/*
* char_to_index - 文字位置から文字インデックスを取得
*/
static DWORD char_to_index(const BUFFER *bf, const TCHAR *p)
{
DWORD i;
if (bf->dp != NULL && p >= bf->dp) {
i = p - bf->buf - bf->del_len;
} else if (bf->ip != NULL && !(p >= bf->buf && p < bf->ip)) {
if (p >= bf->input_buf && p <= bf->input_buf + bf->input_len) {
i = (bf->ip - bf->buf) + (p - bf->input_buf);
} else {
i = (p - bf->buf) + bf->input_len - bf->ip_len;
}
} else {
i = p - bf->buf;
}
return i;
}
/*
* index_to_line - 文字インデックスから行位置を取得
*/
static int index_to_line(const BUFFER *bf, const DWORD index)
{
int low = 0;
int high = bf->line_len - 1;
int i;
while (low <= high) {
i = (low + high) / 2;
if (line_get(bf, i) > index) {
if (i > 0 && line_get(bf, i - 1) <= index) {
return (i - 1);
}
high = i - 1;
} else if (line_get(bf, i) < index) {
if (i < bf->line_len - 1 && line_get(bf, i + 1) > index) {
return i;
}
low = i + 1;
} else {
return i;
}
}
return (bf->line_len - 1);
}
/*
* line_alloc - 行情報の確保
*/
static BOOL line_alloc(BUFFER *bf, const int i, const int size)
{
int *ln;
bf->line_size += size;
if ((ln = mem_alloc(sizeof(int) * bf->line_size)) == NULL) {
return FALSE;
}
if (bf->line != NULL) {
CopyMemory(ln, bf->line, sizeof(int) * i);
mem_free(&bf->line);
}
bf->line = ln;
return TRUE;
}
/*
* line_move - 行情報の移動
*/
static BOOL line_move(BUFFER *bf, const int i, const int len)
{
int *ln;
if (bf->line_len + len + 1 > bf->line_size) {
// 追加
bf->line_size = bf->line_len + len + 1 + RESERVE_LINE;
if ((ln = mem_alloc(sizeof(int) * bf->line_size)) == NULL) {
return FALSE;
}
CopyMemory(ln, bf->line, sizeof(int) * i);
CopyMemory(ln + i + len, bf->line + i, sizeof(int) * (bf->line_len - i + 1));
mem_free(&bf->line);
bf->line = ln;
} else {
MoveMemory(bf->line + i + len, bf->line + i, sizeof(int) * (bf->line_len - i + 1));
}
bf->line_len += len;
if (bf->line_len + 1 < bf->line_size - RESERVE_LINE) {
// 解放
bf->line_size = bf->line_len + 1 + RESERVE_LINE;
if ((ln = mem_alloc(sizeof(int) * bf->line_size)) == NULL) {
return FALSE;
}
CopyMemory(ln, bf->line, sizeof(int) * (bf->line_len + 1));
mem_free(&bf->line);
bf->line = ln;
}
return TRUE;
}
/*
* line_get - 行頭インデックスの取得
*/
static DWORD line_get(const BUFFER *bf, const int lindex)
{
if (bf->line_add_len != 0 && bf->line_add_index <= lindex) {
return (*(bf->line + lindex) + bf->line_add_len);
}
return *(bf->line + lindex);
}
/*
* line_set - 行頭インデックスを設定
*/
static void line_set(BUFFER *bf, const int lindex, const DWORD len)
{
if (bf->line_add_len != 0 && bf->line_add_index <= lindex) {
*(bf->line + lindex) = len - bf->line_add_len;
} else {
*(bf->line + lindex) = len;
}
}
/*
* line_flush - 行情報の反映
*/
static void line_flush(BUFFER *bf)
{
int i;
for (i = bf->line_add_index; i <= bf->line_len; i++) {
*(bf->line + i) += bf->line_add_len;
}
bf->line_add_index = 0;
bf->line_add_len = 0;
}
/*
* line_add_length - 行情報に文字数を追加
*/
static void line_add_length(BUFFER *bf, const int index, const int len)
{
int i;
if (bf->line_add_index != 0 && bf->line_add_index < index) {
for (i = bf->line_add_index; i < index; i++) {
*(bf->line + i) += bf->line_add_len;
}
}
bf->line_add_index = index;
bf->line_add_len += len;
}
/*
* line_get_length - 行の長さを取得
*/
static int line_get_length(const BUFFER *bf, const DWORD index)
{
int i, j;
i = line_get(bf, index_to_line(bf, index) + 1) - 1;
j = line_get(bf, index_to_line(bf, index));
for (; i >= j &&
(*index_to_char(bf, i) == TEXT('\r') || *index_to_char(bf, i) == TEXT('\n')); i--);
return (i - j + 1);
}
/*
* line_get_next_index - 次の行頭の文字インデックスを取得
*/
static DWORD line_get_next_index(BUFFER *bf, const DWORD index)
{
TCHAR *p;
DWORD word = -1;
DWORD i;
int line_size;
int cnt = 0;
int offset = bf->left_margin;
int word_offset;
int width;
int tab;
int clen;
if (bf->wordwrap == TRUE && bf->width <= 0) {
return BUF_LEN;
}
line_size = bf->width - bf->right_margin;
for (i = index; i < BUF_LEN; i++) {
p = index_to_char(bf, i);
switch (*p) {
case TEXT('\r'):
case TEXT('\n'):
// new line
if (*p == TEXT('\r') && (i + 1) < BUF_LEN && *(char_next(bf, p)) == TEXT('\n')) {
i++;
}
if (bf->wordwrap == FALSE && bf->line_width < offset + bf->right_margin) {
bf->line_width = offset + bf->right_margin;
}
return i;
case TEXT('\t'):
// tab
tab = bf->tab_stop * bf->char_width - ((offset - bf->left_margin) % (bf->tab_stop * bf->char_width));
if (tab < bf->char_width) {
tab += bf->tab_stop * bf->char_width;
}
if ((bf->wordwrap == TRUE && offset + tab > line_size) ||
(bf->line_max > 0 && cnt + bf->tab_stop - (cnt % bf->tab_stop) > bf->line_max)) {
// 折り返し
if (word != -1 && word != index && clen == 1) {
i = word;
offset = word_offset;
}
if (bf->wordwrap == FALSE && bf->line_width < offset + bf->right_margin) {
bf->line_width = offset + bf->right_margin;
}
return (i + ((cnt == 0) ? 1 : 0));
} else {
cnt += bf->tab_stop - (cnt % bf->tab_stop);
offset += tab;
}
word = -1;
break;
default:
// char
width = get_char_extent(bf, p, &clen);
if ((bf->wordwrap == TRUE && offset + width > line_size) ||
(bf->line_max > 0 && cnt + 1 > bf->line_max)) {
// 折り返し
if (word != -1 && word != index && clen == 1) {
i = word;
offset = word_offset;
}
if (bf->wordwrap == FALSE && bf->line_width < offset + bf->right_margin) {
bf->line_width = offset + bf->right_margin;
}
return (i + ((cnt == 0) ? clen : 0));
} else {
cnt += clen;
offset += width;
}
if (clen == 2) {
i++;
word = -1;
} else if (*p != TEXT(' ')) {
if (word == (DWORD)-1) {
// 単語の開始位置
word = i;
word_offset = offset;
}
} else {
word = -1;
}
break;
}
}
if (bf->wordwrap == FALSE && bf->line_width < offset + bf->right_margin) {
// 1行の長さ (スクロールバー用)
bf->line_width = offset + bf->right_margin;
}
return i;
}
/*
* line_set_info - 行情報の設定
*/
static BOOL line_set_info(BUFFER *bf)
{
DWORD index;
int i = 1;
index = line_get_next_index(bf, 0);
while (index < BUF_LEN) {
switch (*index_to_char(bf, index)) {
case TEXT('\r'):
case TEXT('\n'):
// 行頭のインデックスを設定
line_set(bf, i, index + 1);
if (++i >= bf->line_size && line_alloc(bf, i, BUF_LEN / 30) == FALSE) {
return FALSE;
}
index++;
break;
default:
// 行頭のインデックスを設定 (折り返し)
line_set(bf, i, index);
if (++i >= bf->line_size && line_alloc(bf, i, BUF_LEN / 30) == FALSE) {
return FALSE;
}
break;
}
index = line_get_next_index(bf, index);
}
line_set(bf, i, index);
bf->line_len = i;
return TRUE;
}
/*
* line_get_count - 論理行数の取得
*/
static int line_get_count(BUFFER *bf, const int lindex)
{
TCHAR *p;
int i;
for (i = lindex + 1; i < bf->line_len; i++) {
p = index_to_char(bf, line_get(bf, i) - 1);
if (*p == TEXT('\r') || *p == TEXT('\n')) {
// 論理行の終端
break;
}
}
return (i - lindex);
}
/*
* line_set_count - 行情報の設定
*/
static int line_set_count(BUFFER *bf, const int lindex, const int old_cnt, const int line_cnt)
{
TCHAR *p;
DWORD index;
int i = lindex;
int rcnt = 0;
index = line_get(bf, lindex - 1);
while (1) {
index = line_get_next_index(bf, index);
if ((i - lindex) >= old_cnt) {
// 挿入行を移動
line_move(bf, i, 1);
}
p = index_to_char(bf, index);
if (*p == TEXT('\r') || *p == TEXT('\n')) {
// 行頭のインデックスを設定
index++;
line_set(bf, i++, index);
if (++rcnt >= line_cnt) {
break;
}
} else {
// 行頭のインデックスを設定 (折り返し)
line_set(bf, i++, index);
if (index >= BUF_LEN) {
break;
}
}
}
if ((i - lindex) < old_cnt) {
// 行情報の削除
line_move(bf, i + old_cnt - (i - lindex), -(old_cnt - (i - lindex)));
}
return (i - lindex);
}
/*
* line_refresh - 指定範囲文字列のある行を再描画対象にする
*/
static void line_refresh(const HWND hWnd, const BUFFER *bf, const DWORD p, const DWORD r)
{
RECT rect;
int i, j;
// 文字位置から行のインデックスを取得
i = index_to_line(bf, p);
if (p == r) {
j = i;
} else {
j = index_to_line(bf, r);
}
if (i > j) {
SWAP(i, j);
}
i -= bf->pos_y;
j -= bf->pos_y;
// 再描画
get_edit_rect(hWnd, bf, &rect);
if (i * bf->font_height > (rect.bottom - rect.top) || j * bf->font_height + bf->font_height < 0) {
return;
}
if (j * bf->font_height + bf->font_height < (rect.bottom - rect.top)) {
rect.bottom = j * bf->font_height + bf->font_height + bf->top_margin;
}
if (i * bf->font_height > 0) {
rect.top = i * bf->font_height + bf->top_margin;
}
InvalidateRect(hWnd, &rect, FALSE);
}
/*
* undo_alloc - UNDOの確保
*/
static BOOL undo_alloc(BUFFER *bf)
{
UNDO *ud;
bf->undo_size += RESERVE_UNDO;
if ((ud = mem_calloc(sizeof(UNDO) * bf->undo_size)) == NULL) {
return FALSE;
}
if (bf->undo != NULL) {
CopyMemory(ud, bf->undo, sizeof(UNDO) * bf->undo_len);
mem_free(&bf->undo);
}
bf->undo = ud;
return TRUE;
}
/*
* undo_free - UNDOの解放
*/
static void undo_free(BUFFER *bf, const int index)
{
int i;
for (i = index; i < bf->undo_size; i++) {
(bf->undo + i)->type = 0;
mem_free(&(bf->undo + i)->buf);
}
}
/*
* undo_set - UNDOのセット
*/
static BOOL undo_set(BUFFER *bf, const int type, const DWORD index, const DWORD len)
{
undo_free(bf, bf->undo_len);
if (bf->undo_len + 1 >= bf->undo_size && undo_alloc(bf) == FALSE) {
return FALSE;
}
(bf->undo + bf->undo_len)->type = type;
(bf->undo + bf->undo_len)->st = index;
(bf->undo + bf->undo_len)->len = len;
switch (type) {
case UNDO_TYPE_INPUT:
// 入力
break;
case UNDO_TYPE_DELETE:
// 削除
if (((bf->undo + bf->undo_len)->buf = mem_alloc(sizeof(TCHAR) * len)) == NULL) {
return FALSE;
}
CopyMemory((bf->undo + bf->undo_len)->buf, bf->buf + index, sizeof(TCHAR) * len);
break;
}
bf->undo_len++;
return TRUE;
}
/*
* undo_exec - UNDOの実行
*/
static BOOL undo_exec(const HWND hWnd, BUFFER *bf)
{
int i;
string_flush(bf, TRUE);
i = bf->undo_len - 1;
if (i < 0) {
return TRUE;
}
bf->sp = bf->cp = (bf->undo + i)->st;
switch ((bf->undo + i)->type) {
case UNDO_TYPE_INPUT:
if ((bf->undo + i)->buf == NULL) {
if (((bf->undo + i)->buf = mem_alloc(sizeof(TCHAR) * (bf->undo + i)->len)) == NULL) {
return FALSE;
}
CopyMemory((bf->undo + i)->buf, bf->buf + (bf->undo + i)->st, sizeof(TCHAR) * (bf->undo + i)->len);
}
string_delete(hWnd, bf, (bf->undo + i)->st, (bf->undo + i)->st + (bf->undo + i)->len);
break;
case UNDO_TYPE_DELETE:
string_insert(hWnd, bf, (bf->undo + i)->buf, (bf->undo + i)->len, TRUE);
break;
}
string_flush(bf, FALSE);
bf->undo_len--;
SendMessage(hWnd, EM_SETMODIFY, (bf->undo_len == bf->undo_pos) ? FALSE : TRUE, 0);
set_scrollbar(hWnd, bf);
ensure_visible(hWnd, bf);
InvalidateRect(hWnd, NULL, FALSE);
return TRUE;
}
/*
* redo_exec - REDOの実行
*/
static BOOL redo_exec(const HWND hWnd, BUFFER *bf)
{
int i;
string_flush(bf, TRUE);
i = bf->undo_len;
if ((bf->undo + i)->type == 0) {
return TRUE;
}
bf->sp = bf->cp = (bf->undo + i)->st;
switch ((bf->undo + i)->type) {
case UNDO_TYPE_INPUT:
string_insert(hWnd, bf, (bf->undo + i)->buf, (bf->undo + i)->len, TRUE);
break;
case UNDO_TYPE_DELETE:
string_delete(hWnd, bf, (bf->undo + i)->st, (bf->undo + i)->st + (bf->undo + i)->len);
break;
}
string_flush(bf, FALSE);
bf->undo_len++;
SendMessage(hWnd, EM_SETMODIFY, (bf->undo_len == bf->undo_pos) ? FALSE : TRUE, 0);
set_scrollbar(hWnd, bf);
ensure_visible(hWnd, bf);
InvalidateRect(hWnd, NULL, FALSE);
return TRUE;
}
/*
* string_init - 初期化
*/
static BOOL string_init(BUFFER *bf)
{
// free
mem_free(&bf->input_buf);
mem_free(&bf->line);
undo_free(bf, 0);
mem_free(&bf->undo);
// input init
bf->input_size = RESERVE_INPUT;
if ((bf->input_buf = mem_alloc(sizeof(TCHAR) * bf->input_size)) == NULL) {
return FALSE;
}
*bf->input_buf = TEXT('\0');
bf->input_len = 0;
bf->ip = NULL;
bf->ip_len = 0;