-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwndproc.c
More file actions
1860 lines (1717 loc) · 79.9 KB
/
wndproc.c
File metadata and controls
1860 lines (1717 loc) · 79.9 KB
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
#include "prose_code.h"
/* Convert mouse position to text position */
bpos mouse_to_pos(int mx, int my) {
Document *doc = current_doc();
if (!doc) return 0;
int edit_y = DPI(TITLEBAR_H + MENUBAR_H + TABBAR_H);
int gw = gutter_width(doc);
int text_x = gw;
int lh = g_editor.line_height;
int cw_px = g_editor.char_width;
int px = mx - text_x;
/* Account for horizontal scroll in code mode */
if (doc->mode == MODE_CODE) px += doc->scroll_x;
if (px < 0) px = 0;
if (doc->mode == MODE_PROSE && doc->wc.count > 0) {
bpos vline = (my - edit_y + doc->scroll_y) / lh;
if (vline < 0) vline = 0;
if (vline >= doc->wc.count) vline = doc->wc.count - 1;
bpos vls = doc->wc.entries[vline].pos;
bpos vle = wc_visual_line_end(&doc->wc, &doc->gb, &doc->lc, vline);
bpos vline_len = vle - vls;
bpos col = pixel_x_to_col(&doc->gb, vls, vline_len, px, cw_px);
return vls + col;
}
bpos line = (my - edit_y + doc->scroll_y) / lh;
if (line < 0) line = 0;
if (line >= doc->lc.count) line = doc->lc.count - 1;
bpos ls = lc_line_start(&doc->lc, line);
bpos le = lc_line_end(&doc->lc, &doc->gb, line);
bpos ll = le - ls;
bpos col = pixel_x_to_col(&doc->gb, ls, ll, px, cw_px);
return ls + col;
}
/* Scrollbar geometry helper — returns thumb_y and thumb_h for the current scroll state */
int scrollbar_thumb_geometry(int *out_thumb_y, int *out_thumb_h, int *out_edit_y, int *out_edit_h) {
Document *doc = current_doc();
if (!doc) return 0;
int edit_y = DPI(TITLEBAR_H + MENUBAR_H + TABBAR_H);
int edit_h = g_editor.client_h - edit_y - DPI(STATUSBAR_H);
int vlines = (int)((doc->mode == MODE_PROSE && doc->wc.count > 0) ? doc->wc.count : doc->lc.count);
int total_h = vlines * g_editor.line_height;
if (out_edit_y) *out_edit_y = edit_y;
if (out_edit_h) *out_edit_h = edit_h;
if (total_h <= edit_h) return 0; /* no scrollbar needed */
int thumb_h = (edit_h * edit_h) / total_h;
if (thumb_h < 30) thumb_h = 30;
int thumb_y = edit_y + (doc->scroll_y * (edit_h - thumb_h)) / (total_h - edit_h);
/* Clamp thumb within the scrollbar track */
if (thumb_y < edit_y) thumb_y = edit_y;
if (thumb_y + thumb_h > edit_y + edit_h) thumb_y = edit_y + edit_h - thumb_h;
if (out_thumb_y) *out_thumb_y = thumb_y;
if (out_thumb_h) *out_thumb_h = thumb_h;
return 1;
}
/* Word boundary helpers */
bpos word_start(GapBuffer *gb, bpos pos) {
if (pos <= 0) return 0;
/* Skip non-word characters first (spaces, punctuation) */
while (pos > 0) {
wchar_t c = gb_char_at(gb, pos - 1);
if (iswalnum(c) || c == L'_') break;
pos--;
}
/* Then skip the word itself */
while (pos > 0) {
wchar_t c = gb_char_at(gb, pos - 1);
if (!iswalnum(c) && c != L'_') break;
pos--;
}
return pos;
}
bpos word_end(GapBuffer *gb, bpos pos) {
bpos len = gb_length(gb);
while (pos < len) {
wchar_t c = gb_char_at(gb, pos);
if (!iswalnum(c) && c != L'_') break;
pos++;
}
return pos;
}
/* Compute whether position 'up_to' is inside a block comment.
* Scans from document start, respecting strings and line comments. */
int compute_block_comment_state(GapBuffer *gb, bpos up_to) {
bpos len = gb_length(gb);
if (up_to > len) up_to = len;
if (up_to <= 0) return 0;
/* Prefer arena to avoid heap alloc/free on every backward scroll frame */
wchar_t *buf = (wchar_t *)arena_alloc(&g_frame_arena, up_to * sizeof(wchar_t));
int used_arena = 1;
if (!buf) {
buf = (wchar_t *)malloc(up_to * sizeof(wchar_t));
used_arena = 0;
}
if (!buf) return 0;
gb_copy_range(gb, 0, up_to, buf);
int in_bc = 0, in_str = 0, in_lc = 0;
for (bpos i = 0; i < up_to; i++) {
wchar_t c = buf[i];
wchar_t cn = (i + 1 < up_to) ? buf[i + 1] : 0;
if (c == L'\n' || c == L'\r') { in_lc = 0; in_str = 0; continue; }
if (in_lc) continue;
if (in_str) { if (c == L'\\') { i++; continue; } if (c == in_str) in_str = 0; continue; }
if (in_bc) { if (c == L'*' && cn == L'/') { in_bc = 0; i++; } continue; }
if (c == L'/' && cn == L'/') { in_lc = 1; i++; }
else if (c == L'/' && cn == L'*') { in_bc = 1; i++; }
else if (c == L'"' || c == L'\'') { in_str = c; }
}
if (!used_arena) free(buf);
return in_bc;
}
int advance_block_comment_state_for_line(GapBuffer *gb, bpos ls, bpos le, int in_bc) {
bpos len = le - ls;
if (len <= 0) return in_bc;
wchar_t *buf = (wchar_t *)arena_alloc(&g_frame_arena, len * sizeof(wchar_t));
int used_arena = 1;
if (!buf) {
buf = (wchar_t *)malloc(len * sizeof(wchar_t));
used_arena = 0;
}
if (!buf) return in_bc;
gb_copy_range(gb, ls, len, buf);
int in_str = 0;
int in_lc = 0;
for (bpos i = 0; i < len; i++) {
wchar_t c = buf[i];
wchar_t cn = (i + 1 < len) ? buf[i + 1] : 0;
if (in_lc) continue;
if (in_str) {
if (c == L'\\') { i++; continue; }
if (c == in_str) in_str = 0;
continue;
}
if (in_bc) {
if (c == L'*' && cn == L'/') { in_bc = 0; i++; }
continue;
}
if (c == L'/' && cn == L'/') { in_lc = 1; i++; }
else if (c == L'/' && cn == L'*') { in_bc = 1; i++; }
else if (c == L'"' || c == L'\'') { in_str = c; }
}
if (!used_arena) free(buf);
return in_bc;
}
bpos find_matching_bracket(GapBuffer *gb, bpos pos) {
bpos len = gb_length(gb);
if (pos < 0 || pos >= len) return -1;
wchar_t c = gb_char_at(gb, pos);
wchar_t match;
int dir;
switch (c) {
case L'(': match = L')'; dir = 1; break;
case L')': match = L'('; dir = -1; break;
case L'[': match = L']'; dir = 1; break;
case L']': match = L'['; dir = -1; break;
case L'{': match = L'}'; dir = 1; break;
case L'}': match = L'{'; dir = -1; break;
default: return -1;
}
/* Cache: tokenize each line at most once during the scan */
bpos cached_line = -1;
SynToken cached_tokens[2048];
int depth = 1;
bpos i = pos + dir;
while (i >= 0 && i < len && depth > 0) {
wchar_t ch = gb_char_at(gb, i);
if (ch == c || ch == match) {
/* Check if this position is inside a string or comment */
bpos line = lc_line_of(¤t_doc()->lc, i);
bpos ls = lc_line_start(¤t_doc()->lc, line);
bpos le = lc_line_end(¤t_doc()->lc, ¤t_doc()->gb, line);
bpos ll = le - ls;
if (ll > 0 && ll <= 2048) {
/* Tokenize this line (cached per-line) */
if (line != cached_line) {
wchar_t lbuf[2048];
gb_copy_range(gb, ls, ll, lbuf);
int bc_state = compute_block_comment_state(gb, ls);
tokenize_line_code(lbuf, (int)ll, cached_tokens, bc_state);
cached_line = line;
}
bpos idx = i - ls;
if (idx >= 0 && idx < 2048) {
SynToken t = cached_tokens[idx];
if (t == TOK_STRING || t == TOK_COMMENT) { i += dir; continue; }
}
}
if (ch == c) depth++;
else if (ch == match) depth--;
}
if (depth == 0) return i;
i += dir;
}
return -1;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE: {
/* Create fonts */
g_editor.font_size = FONT_SIZE_DEFAULT;
g_editor.menu_open = -1;
g_editor.menu_hover_item = -1;
g_editor.fab_hover = -1;
g_editor.spellcheck_enabled = 1; /* spell check on by default */
g_editor.font_main = CreateFontW(
-DPI(g_editor.font_size), 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, FIXED_PITCH | FF_MODERN, L"Consolas");
g_editor.font_bold = CreateFontW(
-DPI(g_editor.font_size), 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, FIXED_PITCH | FF_MODERN, L"Consolas");
g_editor.font_italic = CreateFontW(
-DPI(g_editor.font_size), 0, 0, 0, FW_NORMAL, TRUE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, FIXED_PITCH | FF_MODERN, L"Consolas");
g_editor.font_ui = CreateFontW(
-DPI(14), 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Segoe UI");
g_editor.font_ui_small = CreateFontW(
-DPI(12), 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Segoe UI");
g_editor.font_title = CreateFontW(
-DPI(15), 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, FIXED_PITCH | FF_MODERN, L"Consolas");
g_editor.font_stats_hero = CreateFontW(
-DPI(28), 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Segoe UI");
/* Measure character dimensions */
HDC hdc = GetDC(hwnd);
HFONT old = (HFONT)SelectObject(hdc, g_editor.font_main);
TEXTMETRICW tm;
GetTextMetricsW(hdc, &tm);
g_editor.char_width = tm.tmAveCharWidth;
g_editor.line_height = tm.tmHeight + tm.tmExternalLeading + 4;
SelectObject(hdc, old);
ReleaseDC(hwnd, hdc);
/* Create initial tab */
new_tab();
g_editor.show_minimap = 1;
/* Start cursor blink timer (~30fps for smooth fade — 1200ms sine period needs ~30fps) */
SetTimer(hwnd, TIMER_BLINK, 33, NULL);
/* Note: TIMER_SMOOTH is started on-demand when scroll target changes */
g_editor.cursor_visible = 1; g_editor.cursor_last_active = GetTickCount();
g_editor.session_start_time = GetTickCount();
/* Start autosave timer — fires every 30 seconds */
SetTimer(hwnd, TIMER_AUTOSAVE, 30000, NULL);
return 0;
}
case WM_SIZE: {
g_editor.client_w = LOWORD(lParam);
g_editor.client_h = HIWORD(lParam);
/* Recreate back buffer */
HDC hdc = GetDC(hwnd);
if (g_editor.hdc_back) {
/* Restore original bitmap before deleting ours */
SelectObject(g_editor.hdc_back, g_editor.bmp_back_old);
DeleteObject(g_editor.bmp_back);
DeleteDC(g_editor.hdc_back);
}
if (g_editor.client_w <= 0 || g_editor.client_h <= 0) {
g_editor.hdc_back = NULL;
g_editor.bmp_back = NULL;
ReleaseDC(hwnd, hdc);
return 0;
}
g_editor.hdc_back = CreateCompatibleDC(hdc);
g_editor.bmp_back = CreateCompatibleBitmap(hdc, g_editor.client_w, g_editor.client_h);
g_editor.bmp_back_old = (HBITMAP)SelectObject(g_editor.hdc_back, g_editor.bmp_back);
ReleaseDC(hwnd, hdc);
InvalidateRect(hwnd, NULL, FALSE);
return 0;
}
case WM_DPICHANGED: {
/* Update DPI scale and recreate all fonts for the new monitor DPI */
int new_dpi = HIWORD(wParam);
g_dpi_scale = (float)new_dpi / 96.0f;
/* Destroy all fonts */
DeleteObject(g_editor.font_main);
DeleteObject(g_editor.font_bold);
DeleteObject(g_editor.font_italic);
DeleteObject(g_editor.font_ui);
DeleteObject(g_editor.font_ui_small);
DeleteObject(g_editor.font_title);
DeleteObject(g_editor.font_stats_hero);
/* Recreate all fonts at new DPI */
g_editor.font_main = CreateFontW(
-DPI(g_editor.font_size), 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, FIXED_PITCH | FF_MODERN, L"Consolas");
g_editor.font_bold = CreateFontW(
-DPI(g_editor.font_size), 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, FIXED_PITCH | FF_MODERN, L"Consolas");
g_editor.font_italic = CreateFontW(
-DPI(g_editor.font_size), 0, 0, 0, FW_NORMAL, TRUE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, FIXED_PITCH | FF_MODERN, L"Consolas");
g_editor.font_ui = CreateFontW(
-DPI(14), 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Segoe UI");
g_editor.font_ui_small = CreateFontW(
-DPI(12), 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Segoe UI");
g_editor.font_title = CreateFontW(
-DPI(15), 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, FIXED_PITCH | FF_MODERN, L"Consolas");
g_editor.font_stats_hero = CreateFontW(
-DPI(28), 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Segoe UI");
/* Remeasure character dimensions */
HDC hdc_m = GetDC(hwnd);
HFONT old_f = (HFONT)SelectObject(hdc_m, g_editor.font_main);
TEXTMETRICW tm_dpi;
GetTextMetricsW(hdc_m, &tm_dpi);
g_editor.char_width = tm_dpi.tmAveCharWidth;
g_editor.line_height = tm_dpi.tmHeight + tm_dpi.tmExternalLeading + 4;
SelectObject(hdc_m, old_f);
ReleaseDC(hwnd, hdc_m);
/* Resize window to suggested rect from the system */
RECT *suggested = (RECT *)lParam;
SetWindowPos(hwnd, NULL,
suggested->left, suggested->top,
suggested->right - suggested->left,
suggested->bottom - suggested->top,
SWP_NOZORDER | SWP_NOACTIVATE);
return 0;
}
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
if (g_editor.hdc_back) {
render(g_editor.hdc_back);
/* BitBlt only the dirty region — avoids full-window blit for cursor blink,
* scrollbar hover, and other small invalidations. */
int bx = ps.rcPaint.left, by = ps.rcPaint.top;
int bw = ps.rcPaint.right - bx, bh = ps.rcPaint.bottom - by;
BitBlt(hdc, bx, by, bw, bh, g_editor.hdc_back, bx, by, SRCCOPY);
}
EndPaint(hwnd, &ps);
return 0;
}
case WM_ERASEBKGND:
return 1; /* prevent flicker */
case WM_SETCURSOR: {
if (LOWORD(lParam) == HTCLIENT) {
POINT pt;
GetCursorPos(&pt);
ScreenToClient(hwnd, &pt);
int sb_x = g_editor.client_w - DPI(SCROLLBAR_W);
int in_editor = (pt.y >= DPI(TITLEBAR_H + MENUBAR_H + TABBAR_H) && pt.y < g_editor.client_h - DPI(STATUSBAR_H));
int in_scrollbar = (pt.x >= sb_x && in_editor);
int in_chrome = (pt.y < DPI(TITLEBAR_H + MENUBAR_H + TABBAR_H) || pt.y >= g_editor.client_h - DPI(STATUSBAR_H));
/* Update scrollbar hover state */
int was_hover = g_editor.scrollbar_hover;
g_editor.scrollbar_hover = in_scrollbar;
if (was_hover != g_editor.scrollbar_hover)
InvalidateRect(hwnd, NULL, FALSE);
if (in_scrollbar || in_chrome) {
SetCursor(LoadCursor(NULL, IDC_ARROW));
} else {
SetCursor(LoadCursor(NULL, IDC_IBEAM));
}
return TRUE;
}
break;
}
case WM_TIMER: {
if (wParam == TIMER_BLINK) {
/* Refresh stats overlay at ~1Hz for running clock */
if (g_editor.show_stats_screen) {
static int stats_tick = 0;
if (++stats_tick >= 60) {
stats_tick = 0;
InvalidateRect(hwnd, NULL, FALSE);
}
}
/* Refresh search bar cursor blink (~every 530ms) */
if (g_editor.search.active) {
static int search_tick = 0;
if (++search_tick >= 33) { /* ~530ms at 16ms/tick */
search_tick = 0;
int bar_w = DPI(460);
int sx = g_editor.client_w - bar_w - DPI(24);
int sy = DPI(TITLEBAR_H + MENUBAR_H + TABBAR_H) + DPI(8);
RECT sr = { sx, sy, sx + bar_w, sy + DPI(80) };
InvalidateRect(hwnd, &sr, FALSE);
}
}
/* Invalidate only the cursor rect for smooth fade animation.
* Skip when stats overlay is showing — the tiny partial repaint
* slices through ClearType glyphs and leaves visible seams. */
Document *blink_doc = current_doc();
if (blink_doc && !g_editor.show_stats_screen) {
int edit_y = DPI(TITLEBAR_H + MENUBAR_H + TABBAR_H);
int lh = g_editor.line_height;
int cw = g_editor.char_width;
int gw = gutter_width(blink_doc);
int cline, ccol, cline_start;
if (blink_doc->mode == MODE_PROSE && blink_doc->wc.count > 0) {
cline = wc_visual_line_of(&blink_doc->wc, blink_doc->cursor);
ccol = wc_col_in_vline(&blink_doc->wc, blink_doc->cursor, cline);
cline_start = blink_doc->wc.entries[cline].pos;
} else {
cline = pos_to_line(blink_doc, blink_doc->cursor);
ccol = pos_to_col(blink_doc, blink_doc->cursor);
cline_start = lc_line_start(&blink_doc->lc, cline);
}
int cy = edit_y + cline * lh - blink_doc->scroll_y;
int cx = gw + col_to_pixel_x(&blink_doc->gb, cline_start, ccol, cw) - blink_doc->scroll_x;
RECT cr = { cx - 1, cy, cx + DPI(CURSOR_WIDTH) + 2, cy + lh };
InvalidateRect(hwnd, &cr, FALSE);
}
}
if (wParam == TIMER_AUTOSAVE) {
autosave_tick();
}
if (wParam == TIMER_DRAG_SCROLL) {
Document *doc = current_doc();
if (doc && g_editor.mouse_captured) {
POINT pt;
GetCursorPos(&pt);
ScreenToClient(hwnd, &pt);
int edit_top = DPI(TITLEBAR_H + MENUBAR_H + TABBAR_H);
int edit_bot = g_editor.client_h - DPI(STATUSBAR_H);
int lh = g_editor.line_height;
int scroll_amt = 0;
if (pt.y < edit_top) {
scroll_amt = -lh * (1 + (edit_top - pt.y) / (lh * 2));
} else if (pt.y > edit_bot) {
scroll_amt = lh * (1 + (pt.y - edit_bot) / (lh * 2));
}
if (scroll_amt != 0) {
doc->target_scroll_y += scroll_amt;
if (doc->target_scroll_y < 0) doc->target_scroll_y = 0;
int total_vl = (int)((doc->mode == MODE_PROSE && doc->wc.count > 0) ? doc->wc.count : doc->lc.count);
int max_scroll = total_vl * lh - (edit_bot - edit_top);
if (max_scroll > 0 && doc->target_scroll_y > max_scroll)
doc->target_scroll_y = max_scroll;
doc->scroll_y = doc->target_scroll_y;
/* Clamp mx to text area */
int clamp_mx = pt.x;
int max_text_x = g_editor.client_w - DPI(SCROLLBAR_W);
if (g_editor.show_minimap) max_text_x -= DPI(MINIMAP_W);
if (clamp_mx > max_text_x) clamp_mx = max_text_x;
bpos pos = mouse_to_pos(clamp_mx, pt.y);
if (doc->sel_anchor < 0) doc->sel_anchor = doc->cursor;
doc->cursor = pos;
InvalidateRect(hwnd, NULL, FALSE);
}
} else {
KillTimer(hwnd, TIMER_DRAG_SCROLL);
}
return 0;
}
if (wParam == TIMER_SMOOTH) {
Document *doc = current_doc();
int needs_invalidate = 0;
if (doc && doc->scroll_y != doc->target_scroll_y) {
int diff = doc->target_scroll_y - doc->scroll_y;
/* Exponential ease-out: move 1/4 of remaining distance, minimum 1px */
int step = diff / 4;
if (step == 0) step = (diff > 0) ? 1 : -1;
doc->scroll_y += step;
if (abs(doc->scroll_y - doc->target_scroll_y) <= 1)
doc->scroll_y = doc->target_scroll_y;
needs_invalidate = 1;
}
/* Horizontal smooth scroll (code mode) */
if (doc && doc->scroll_x != doc->target_scroll_x) {
int diff = doc->target_scroll_x - doc->scroll_x;
int step = diff / 4;
if (step == 0) step = (diff > 0) ? 1 : -1;
doc->scroll_x += step;
if (abs(doc->scroll_x - doc->target_scroll_x) <= 1)
doc->scroll_x = doc->target_scroll_x;
needs_invalidate = 1;
}
if (needs_invalidate) {
g_editor.scroll_only_repaint = 1;
invalidate_editor_region(hwnd);
} else {
/* Animation complete — kill the timer to stop burning CPU when idle */
KillTimer(hwnd, TIMER_SMOOTH);
}
}
return 0;
}
case WM_MOUSEWHEEL: {
Document *doc = current_doc();
if (!doc) break;
int delta = GET_WHEEL_DELTA_WPARAM(wParam);
/* Shift+Wheel = horizontal scroll in code mode */
if ((LOWORD(wParam) & MK_SHIFT) && doc->mode == MODE_CODE) {
doc->target_scroll_x -= (delta / 120) * g_editor.char_width * 8;
if (doc->target_scroll_x < 0) doc->target_scroll_x = 0;
} else {
doc->target_scroll_y -= (delta / 120) * g_editor.line_height * 3;
int vlines = (int)((doc->mode == MODE_PROSE && doc->wc.count > 0) ? doc->wc.count : doc->lc.count);
int max_scroll = vlines * g_editor.line_height - (g_editor.client_h - DPI(TITLEBAR_H + MENUBAR_H + TABBAR_H) - DPI(STATUSBAR_H));
if (doc->target_scroll_y < 0) doc->target_scroll_y = 0;
if (max_scroll > 0 && doc->target_scroll_y > max_scroll) doc->target_scroll_y = max_scroll;
}
g_editor.scroll_only_repaint = 1;
start_scroll_animation();
invalidate_editor_region(hwnd);
return 0;
}
case WM_MOUSEHWHEEL: {
/* Native horizontal scroll (tilt wheel / trackpad) */
Document *doc = current_doc();
if (!doc || doc->mode != MODE_CODE) break;
int delta = GET_WHEEL_DELTA_WPARAM(wParam);
doc->target_scroll_x += (delta / 120) * g_editor.char_width * 8;
if (doc->target_scroll_x < 0) doc->target_scroll_x = 0;
{ int max_x = 300 * g_editor.char_width; /* reasonable max */
if (doc->target_scroll_x > max_x) doc->target_scroll_x = max_x; }
g_editor.scroll_only_repaint = 1;
start_scroll_animation();
invalidate_editor_region(hwnd);
return 0;
}
case WM_LBUTTONDOWN: {
int mx = GET_X_LPARAM(lParam);
int my = GET_Y_LPARAM(lParam);
/* Click anywhere to dismiss stats overlay */
if (g_editor.show_stats_screen) {
g_editor.show_stats_screen = 0;
InvalidateRect(hwnd, NULL, FALSE);
return 0;
}
/* Title bar area */
if (my < DPI(TITLEBAR_H)) {
/* Check window control buttons (rightmost) */
int bw = DPI(46);
int x = g_editor.client_w - bw * 3;
if (mx >= x && mx < x + bw) {
ShowWindow(hwnd, SW_MINIMIZE);
return 0;
}
x += bw;
if (mx >= x && mx < x + bw) {
WINDOWPLACEMENT wp = {0}; wp.length = sizeof(wp);
GetWindowPlacement(hwnd, &wp);
ShowWindow(hwnd, wp.showCmd == SW_MAXIMIZE ? SW_RESTORE : SW_MAXIMIZE);
return 0;
}
x += bw;
if (mx >= x && mx < x + bw) {
PostMessage(hwnd, WM_CLOSE, 0, 0);
return 0;
}
/* Check FAB clicks (LEFT SIDE of titlebar) */
int fab_size = DPI(28);
int fab_pad = DPI(6);
int fab_start_x = DPI(8);
int fab_y = (DPI(TITLEBAR_H) - fab_size) / 2;
int fab_total_w = MENU_COUNT * (fab_size + fab_pad);
if (mx >= fab_start_x && mx < fab_start_x + fab_total_w &&
my >= fab_y && my < fab_y + fab_size) {
for (int i = 0; i < MENU_COUNT; i++) {
int fx = fab_start_x + i * (fab_size + fab_pad);
if (mx >= fx && mx < fx + fab_size) {
/* Toggle this menu open/closed */
if (g_editor.menu_open == i) {
g_editor.menu_open = -1;
} else {
g_editor.menu_open = i;
g_editor.menu_hover_item = -1;
}
InvalidateRect(hwnd, NULL, FALSE);
return 0;
}
}
}
/* Drag to move (anywhere else in titlebar) */
g_editor.titlebar_dragging = 1;
g_editor.drag_start.x = mx;
g_editor.drag_start.y = my;
SetCapture(hwnd);
return 0;
}
/* Menu dropdown click — MUST be checked before tab bar since
* the dropdown overlaps the tab bar's y-range */
if (g_editor.menu_open >= 0) {
const MenuDef *menu = &g_menus[g_editor.menu_open];
int item_h = DPI(26);
int sep_h = DPI(9);
int dropdown_w = DPI(260);
/* FAB-based dropdown position (LEFT SIDE) */
int fab_size = DPI(28);
int fab_pad = DPI(6);
int fab_start_x = DPI(8);
int dropdown_x = fab_start_x + g_editor.menu_open * (fab_size + fab_pad);
int dropdown_y = DPI(TITLEBAR_H);
/* Calculate total dropdown height */
int total_dd_h = DPI(4);
for (int i = 0; i < menu->item_count; i++)
total_dd_h += (menu->items[i].id == MENU_ID_SEP) ? sep_h : item_h;
total_dd_h += DPI(4);
if (mx >= dropdown_x && mx < dropdown_x + dropdown_w &&
my >= dropdown_y && my < dropdown_y + total_dd_h) {
int cy = dropdown_y + DPI(4);
for (int i = 0; i < menu->item_count; i++) {
int h = (menu->items[i].id == MENU_ID_SEP) ? sep_h : item_h;
if (menu->items[i].id != MENU_ID_SEP &&
my >= cy && my < cy + h) {
menu_execute(menu->items[i].id);
return 0;
}
cy += h;
}
/* Clicked inside dropdown but on nothing (e.g. separator) */
return 0;
}
/* Clicked outside dropdown — close it */
g_editor.menu_open = -1;
g_editor.menu_hover_item = -1;
InvalidateRect(hwnd, NULL, FALSE);
/* Fall through to handle the click on whatever was underneath */
}
/* Tab bar */
if (my >= DPI(TITLEBAR_H + MENUBAR_H) && my < DPI(TITLEBAR_H + MENUBAR_H + TABBAR_H)) {
int tx = DPI(8);
for (int i = 0; i < g_editor.tab_count; i++) {
wchar_t label[128];
swprintf(label, 128, L"%ls%ls", g_editor.tabs[i]->title, g_editor.tabs[i]->modified ? L" \x2022" : L"");
int tw = (int)wcslen(label) * DPI(8) + DPI(TAB_PAD) * 2;
if (tw < DPI(TAB_MIN_W)) tw = DPI(TAB_MIN_W);
if (tw > DPI(TAB_MAX_W)) tw = DPI(TAB_MAX_W);
if (mx >= tx && mx < tx + tw) {
/* Check close button */
if (mx >= tx + tw - DPI(24)) {
close_tab(i);
} else {
g_editor.active_tab = i;
}
InvalidateRect(hwnd, NULL, FALSE);
return 0;
}
tx += tw + DPI(4);
}
/* New tab button */
if (mx >= tx + DPI(4) && mx < tx + DPI(30)) {
new_tab();
InvalidateRect(hwnd, NULL, FALSE);
}
return 0;
}
/* Search bar close button */
if (g_editor.search.active) {
int bar_h = g_editor.search.replace_active ? DPI(72) : DPI(40);
int bar_w = DPI(460);
int sb_x = g_editor.client_w - bar_w - DPI(24);
int sb_y = DPI(TITLEBAR_H + MENUBAR_H + TABBAR_H) + DPI(8);
int btn_size = DPI(20);
int bx = sb_x + bar_w - DPI(12) - btn_size;
int by = sb_y + DPI(10);
if (mx >= bx && mx < bx + btn_size && my >= by && my < by + btn_size) {
toggle_search();
InvalidateRect(hwnd, NULL, FALSE);
return 0;
}
}
/* Editor area */
if (my >= DPI(TITLEBAR_H + MENUBAR_H + TABBAR_H) && my < g_editor.client_h - DPI(STATUSBAR_H)) {
int sb_x = g_editor.client_w - DPI(SCROLLBAR_W);
int mm_x = g_editor.client_w - DPI(SCROLLBAR_W) - (g_editor.show_minimap ? DPI(MINIMAP_W) : 0);
/* Check if click is on the minimap */
if (g_editor.show_minimap && mx >= mm_x && mx < sb_x) {
Document *doc = current_doc();
if (doc) {
int edit_y_mm = DPI(TITLEBAR_H + MENUBAR_H + TABBAR_H);
int edit_h_mm = g_editor.client_h - edit_y_mm - DPI(STATUSBAR_H);
int total_vl = (int)((doc->mode == MODE_PROSE && doc->wc.count > 0) ? doc->wc.count : doc->lc.count);
if (total_vl < 1) total_vl = 1;
float click_ratio = (float)(my - edit_y_mm) / (float)edit_h_mm;
int target_line = (int)(click_ratio * total_vl);
if (target_line < 0) target_line = 0;
if (target_line >= total_vl) target_line = total_vl - 1;
int visible_lines = edit_h_mm / g_editor.line_height;
doc->target_scroll_y = (target_line - visible_lines / 2) * g_editor.line_height;
int max_scroll = total_vl * g_editor.line_height - edit_h_mm;
if (doc->target_scroll_y < 0) doc->target_scroll_y = 0;
if (max_scroll > 0 && doc->target_scroll_y > max_scroll) doc->target_scroll_y = max_scroll;
}
start_scroll_animation();
InvalidateRect(hwnd, NULL, FALSE);
return 0;
}
/* Check if click is on the scrollbar */
if (mx >= sb_x) {
int thumb_y, thumb_h, edit_y_sb, edit_h_sb;
if (scrollbar_thumb_geometry(&thumb_y, &thumb_h, &edit_y_sb, &edit_h_sb)) {
Document *doc = current_doc();
if (doc) {
int sb_vlines = (int)((doc->mode == MODE_PROSE && doc->wc.count > 0) ? doc->wc.count : doc->lc.count);
int total_h = sb_vlines * g_editor.line_height;
if (my >= thumb_y && my < thumb_y + thumb_h) {
/* Clicked on thumb — start dragging */
g_editor.scrollbar_dragging = 1;
g_editor.scrollbar_drag_offset = my - thumb_y;
SetCapture(hwnd);
} else {
/* Clicked on track — page jump to that position */
int max_scroll = total_h - edit_h_sb;
if (max_scroll > 0) {
float ratio = (float)(my - edit_y_sb) / (float)edit_h_sb;
doc->scroll_y = (int)(ratio * total_h - edit_h_sb / 2);
if (doc->scroll_y < 0) doc->scroll_y = 0;
if (doc->scroll_y > max_scroll) doc->scroll_y = max_scroll;
doc->target_scroll_y = doc->scroll_y;
/* Now start dragging from this new position */
scrollbar_thumb_geometry(&thumb_y, &thumb_h, NULL, NULL);
g_editor.scrollbar_dragging = 1;
g_editor.scrollbar_drag_offset = thumb_h / 2;
SetCapture(hwnd);
}
}
}
}
InvalidateRect(hwnd, NULL, FALSE);
return 0;
}
SetCapture(hwnd);
g_editor.mouse_captured = 1;
bpos pos = mouse_to_pos(mx, my);
int shift = GetKeyState(VK_SHIFT) & 0x8000;
if (GetKeyState(VK_CONTROL) & 0x8000) {
/* Ctrl+click: word select (only if clicking on a word character) */
Document *doc = current_doc();
if (doc) {
bpos tlen = gb_length(&doc->gb);
wchar_t ch = (pos < tlen) ? gb_char_at(&doc->gb, pos) : 0;
if (iswalnum(ch) || ch == L'_') {
doc->sel_anchor = word_start(&doc->gb, pos);
doc->cursor = word_end(&doc->gb, pos);
} else {
/* On whitespace/punctuation: just place cursor, no selection */
doc->sel_anchor = -1;
doc->cursor = pos;
}
}
} else {
editor_move_cursor(pos, shift);
}
g_editor.cursor_visible = 1; g_editor.cursor_last_active = GetTickCount();
InvalidateRect(hwnd, NULL, FALSE);
return 0;
}
break;
}
case WM_LBUTTONDBLCLK: {
int mx = GET_X_LPARAM(lParam);
int my = GET_Y_LPARAM(lParam);
/* Double click in title bar = maximize/restore */
if (my < DPI(TITLEBAR_H)) {
WINDOWPLACEMENT wp = {0}; wp.length = sizeof(wp);
GetWindowPlacement(hwnd, &wp);
ShowWindow(hwnd, wp.showCmd == SW_MAXIMIZE ? SW_RESTORE : SW_MAXIMIZE);
return 0;
}
/* Double click in editor = select word */
if (my >= DPI(TITLEBAR_H + MENUBAR_H + TABBAR_H) && my < g_editor.client_h - DPI(STATUSBAR_H)) {
Document *doc = current_doc();
if (doc) {
bpos pos = mouse_to_pos(mx, my);
doc->sel_anchor = word_start(&doc->gb, pos);
doc->cursor = word_end(&doc->gb, pos);
InvalidateRect(hwnd, NULL, FALSE);
}
return 0;
}
break;
}
case WM_MOUSEMOVE: {
int mx = GET_X_LPARAM(lParam);
int my = GET_Y_LPARAM(lParam);
/* FAB hover tracking in titlebar (LEFT SIDE) */
int old_fab_hover = g_editor.fab_hover;
g_editor.fab_hover = -1;
if (my < DPI(TITLEBAR_H)) {
int fab_size = DPI(28);
int fab_pad = DPI(6);
int fab_start_x = DPI(8);
int fab_total_w = MENU_COUNT * (fab_size + fab_pad);
int fab_y = (DPI(TITLEBAR_H) - fab_size) / 2;
if (mx >= fab_start_x && mx < fab_start_x + fab_total_w &&
my >= fab_y && my < fab_y + fab_size) {
for (int i = 0; i < MENU_COUNT; i++) {
int fx = fab_start_x + i * (fab_size + fab_pad);
if (mx >= fx && mx < fx + fab_size) {
g_editor.fab_hover = i;
/* If a menu is open and we hover a different FAB, switch to it */
if (g_editor.menu_open >= 0 && g_editor.menu_open != i) {
g_editor.menu_open = i;
g_editor.menu_hover_item = -1;
}
break;
}
}
}
}
if (old_fab_hover != g_editor.fab_hover)
InvalidateRect(hwnd, NULL, FALSE);
/* Menu dropdown hover tracking */
if (g_editor.menu_open >= 0) {
const MenuDef *menu = &g_menus[g_editor.menu_open];
int item_h = DPI(26);
int sep_h = DPI(9);
int dropdown_w = DPI(260);
/* FAB-based dropdown position (LEFT SIDE) */
int fab_size = DPI(28);
int fab_pad = DPI(6);
int fab_start_x = DPI(8);
int dropdown_x = fab_start_x + g_editor.menu_open * (fab_size + fab_pad);
int dropdown_y = DPI(TITLEBAR_H);
int old_hover = g_editor.menu_hover_item;
g_editor.menu_hover_item = -1;
if (mx >= dropdown_x && mx < dropdown_x + dropdown_w) {
int cy = dropdown_y + DPI(4);
for (int i = 0; i < menu->item_count; i++) {
int h = (menu->items[i].id == MENU_ID_SEP) ? sep_h : item_h;
if (menu->items[i].id != MENU_ID_SEP &&
my >= cy && my < cy + h) {
g_editor.menu_hover_item = i;
break;
}
cy += h;
}
}
if (old_hover != g_editor.menu_hover_item)
InvalidateRect(hwnd, NULL, FALSE);
}
if (g_editor.scrollbar_dragging) {
Document *doc = current_doc();
if (doc) {
int edit_y = DPI(TITLEBAR_H + MENUBAR_H + TABBAR_H);
int edit_h = g_editor.client_h - edit_y - DPI(STATUSBAR_H);
int drag_vlines = (int)((doc->mode == MODE_PROSE && doc->wc.count > 0) ? doc->wc.count : doc->lc.count);
int total_h = drag_vlines * g_editor.line_height;
if (total_h > edit_h) {
int thumb_h = (edit_h * edit_h) / total_h;
if (thumb_h < 30) thumb_h = 30;
int max_scroll = total_h - edit_h;
int new_thumb_y = my - g_editor.scrollbar_drag_offset;
int min_thumb_y = edit_y;
int max_thumb_y = edit_y + edit_h - thumb_h;
if (new_thumb_y < min_thumb_y) new_thumb_y = min_thumb_y;
if (new_thumb_y > max_thumb_y) new_thumb_y = max_thumb_y;
float ratio = (max_thumb_y > min_thumb_y)
? (float)(new_thumb_y - min_thumb_y) / (float)(max_thumb_y - min_thumb_y)
: 0.0f;
doc->scroll_y = (int)(ratio * max_scroll);
doc->target_scroll_y = doc->scroll_y;
}
}
InvalidateRect(hwnd, NULL, FALSE);
g_editor.scroll_only_repaint = 1;
return 0;
}
if (g_editor.titlebar_dragging) {
POINT pt;
GetCursorPos(&pt);
SetWindowPos(hwnd, NULL,
pt.x - g_editor.drag_start.x,
pt.y - g_editor.drag_start.y,
0, 0, SWP_NOSIZE | SWP_NOZORDER);
return 0;
}
if (g_editor.mouse_captured && (wParam & MK_LBUTTON)) {
/* Clamp mx to the text editing area to avoid minimap coordinates */
int clamp_mx = mx;
int max_text_x = g_editor.client_w - DPI(SCROLLBAR_W);
if (g_editor.show_minimap) max_text_x -= DPI(MINIMAP_W);
if (clamp_mx > max_text_x) clamp_mx = max_text_x;
bpos pos = mouse_to_pos(clamp_mx, my);
Document *doc = current_doc();
if (doc) {
if (doc->sel_anchor < 0) doc->sel_anchor = doc->cursor;
doc->cursor = pos;
editor_ensure_cursor_visible();
InvalidateRect(hwnd, NULL, FALSE);
}
/* Start/stop auto-scroll timer when dragging above/below editor */
int edit_top = DPI(TITLEBAR_H + MENUBAR_H + TABBAR_H);
int edit_bot = g_editor.client_h - DPI(STATUSBAR_H);
if (my < edit_top || my > edit_bot) {
SetTimer(hwnd, TIMER_DRAG_SCROLL, 30, NULL);
} else {
KillTimer(hwnd, TIMER_DRAG_SCROLL);
}
}
/* Track titlebar button hover */
{
int old_hover = g_editor.titlebar_hover_btn;
g_editor.titlebar_hover_btn = 0;
if (my < DPI(TITLEBAR_H)) {
int bw = DPI(46);
int bx = g_editor.client_w - bw * 3;
if (mx >= bx && mx < bx + bw) g_editor.titlebar_hover_btn = 1;
else if (mx >= bx + bw && mx < bx + bw * 2) g_editor.titlebar_hover_btn = 2;