/
ntwinio.c
4698 lines (4173 loc) · 111 KB
/
ntwinio.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
/*
* Uses the Win32 screen API.
*
* $Id: ntwinio.c,v 1.226 2024/01/17 01:09:01 tom Exp $
* Written by T.E.Dickey for vile (october 1997).
* -- improvements by Clark Morgan (see w32cbrd.c, w32pipe.c).
*/
#include "estruct.h" /* includes <windows.h> */
#include <windowsx.h>
#include <commdlg.h>
#include <shellapi.h>
#include <stdlib.h>
#include <math.h>
#include "edef.h"
#include "pscreen.h"
#include "patchlev.h"
#include "winvile.h"
#include "nefsms.h"
#include "nefunc.h"
#include "chgdfunc.h"
#include "makeargv.h"
#define ABS(x) (((x) < 0) ? -(x) : (x))
#define WM_SYSTIMER 0x118 // undocumented http://support.microsoft.com/?id=108938
#define MIN_ROWS MINWLNS
#define MIN_COLS 15
#define OOPS (4)
#define MAX_CURSOR_STYLE 2
#if OPT_TRACE
#define IGN_PROC(tag,name) \
case name: \
TRACE((tag #name " (ignored)\n")); \
break;
#define DEF_PROC(tag,name) \
case name: \
TRACE((tag #name " (%s)\n", which_window(hWnd))); \
return (DefWindowProc(hWnd, message, wParam, lParam))
#else
#define IGN_PROC(tag,name) case name: break;
#define DEF_PROC(tag,name) /*nothing */
#endif
#define MAIN_CLASS W32_STRING("VileMain")
#define TEXT_CLASS W32_STRING("VileText")
#define GRIP_CLASS W32_STRING("VileResize")
#define MY_APPLE W32_STRING("Vile Application")
#define MY_FONT SYSTEM_FIXED_FONT /* or ANSI_FIXED_FONT */
#define NROW 128 /* Max Screen size. */
#define NCOL 256 /* Edit if you want to. */
#define NOKYMAP (-1)
#define KYREDIR (-2) /* sent keystroke elsewhere. */
#define RSZ_WDW_HGHT 18 /* pixels */
#define RSZ_WDW_WDTH 190
#define SetCols(value) term.cols = cur_win->cols = value
#define SetRows(value) term.rows = cur_win->rows = value
#define RowToPixel(n) ((n) * nLineHeight)
#define ColToPixel(n) ((n) * nCharWidth)
#define PixelToRow(n) ((n) / nLineHeight)
#define PixelToCol(n) ((n) / nCharWidth)
#define RectToCols(rect) (PixelToCol(rect.right - rect.left - SbWidth))
#define RectToRows(rect) (PixelToRow(rect.bottom - rect.top))
#if OPT_SCROLLBARS
#define SbWidth nLineHeight // FIXME? GetSystemMetrics(SM_CXVSCROLL);
#else
#define SbWidth 0
#endif
static DWORD default_bcolor;
static DWORD default_fcolor;
static ENC_CHOICES my_encoding = enc_DEFAULT;
static HANDLE hAccTable; /* handle to accelerator table */
static HANDLE vile_hinstance;
static HCURSOR arrow_cursor;
static HCURSOR hglass_cursor;
static HCURSOR selection_cursor;
static HCURSOR wdwsize_cursor;
static HMENU vile_menu, popup_menu;
static LOGFONT vile_logfont;
static SETTIMER_RETVAL nIDTimer = 0;
static int ac_active = FALSE; /* AutoColor active? */
static int caret_disabled = TRUE;
static int caret_exists = 0;
static int caret_visible = 0;
static int desired_wdw_state;
static int dont_update_sb = FALSE;
static int enable_popup = TRUE;
static int font_resize_in_progress;
static int gui_resize_in_progress;
static int initialized = FALSE; /* winvile open for business */
static int mouse_captured = 0;
static int nCursorAdj = 0;
static int nCharWidth = 8;
static int nLineToFill = 0;
static int nLineHeight = 10;
static int vile_resizing = FALSE; /* rely on repaint_window if true */
static int vile_selecting = FALSE; /* true when using mouse to select */
static int icursor; /* T -> enable insertion cursor */
static int icursor_style; /* 1 -> cmdmode = block,
* insmode = vertical bar
* 2 -> cmdmode = vertical bar
* insmode = block
*/
#ifdef VILE_OLE
static OLEAUTO_OPTIONS oa_opts;
static int redirect_keys;
#endif
static int nfcolor = -1; /* normal foreground color */
static int nbcolor = -1; /* normal background color */
static int crow = -1; /* current row */
static int ccol = -1; /* current col */
/* ansi to ibm color translation table */
static const char *initpalettestr = "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15";
/* black, red, green, yellow, blue, magenta, cyan, white */
static int cur_pos = 0;
static VIDEO_ATTR cur_atr = 0;
typedef struct {
HWND w;
RECT r;
int shown;
} SBDATA;
static struct gui_info {
HWND main_hwnd; /* the top-level window */
HWND text_hwnd; /* the enclosed text area */
int closed;
int cols;
int rows;
RECT xy_limit;
int x_border;
int y_border;
int y_titles; /* height of caption/titles */
int nscrollbars;
int maxscrollbars;
SBDATA *scrollbars;
SBDATA size_box;
SBDATA size_grip;
} only_window, *cur_win = &only_window;
/* *INDENT-OFF* */
static struct my_font {
int used;
HFONT font;
VIDEO_ATTR attr;
} MyFonts[] = {
{ FALSE, 0, 0 },
{ FALSE, 0, VABOLD },
{ FALSE, 0, VAUL },
{ FALSE, 0, VAITAL },
};
/* *INDENT-ON* */
#if OPT_SCROLLBARS
static int check_scrollbar_allocs(void);
static void update_scrollbar_sizes(void);
#endif
#if OPT_TRACE
static char *
which_window(HWND hwnd)
{
if (hwnd == 0) {
return "NULL";
}
if (hwnd == cur_win->main_hwnd) {
return "main";
} else if (hwnd == cur_win->text_hwnd) {
return "text";
} else {
int n;
static char temp[20];
sprintf(temp, "h%p", hwnd);
for (n = 0; n < cur_win->nscrollbars; n++) {
if (hwnd == cur_win->scrollbars[n].w) {
sprintf(temp, "sb%d", n);
break;
}
}
return temp;
}
}
static char *
message2s(unsigned code)
{
static struct {
WORD code;
char *name;
} table[] = {
/* *INDENT-OFF* */
{ WM_ACTIVATE, "WM_ACTIVATE" },
{ WM_ACTIVATEAPP, "WM_ACTIVATEAPP" },
{ WM_CANCELMODE, "WM_CANCELMODE" },
{ WM_CAPTURECHANGED, "WM_CAPTURECHANGED" },
{ WM_CHAR, "WM_CHAR" },
{ WM_CLOSE, "WM_CLOSE" },
{ WM_CONTEXTMENU, "WM_CONTEXTMENU" },
{ WM_CREATE, "WM_CREATE" },
{ WM_CTLCOLORSCROLLBAR, "WM_CTLCOLORSCROLLBAR" },
{ WM_DROPFILES, "WM_DROPFILES" },
{ WM_ENABLE, "WM_ENABLE" },
{ WM_ENTERIDLE, "WM_ENTERIDLE" },
{ WM_ENTERMENULOOP, "WM_ENTERMENULOOP" },
{ WM_ENTERSIZEMOVE, "WM_ENTERSIZEMOVE" },
{ WM_ERASEBKGND, "WM_ERASEBKGND" },
{ WM_EXITMENULOOP, "WM_EXITMENULOOP" },
{ WM_EXITSIZEMOVE, "WM_EXITSIZEMOVE" },
{ WM_GETMINMAXINFO, "WM_GETMINMAXINFO" },
{ WM_GETTEXT, "WM_GETTEXT" },
{ WM_IME_NOTIFY, "WM_IME_NOTIFY" },
{ WM_IME_SETCONTEXT, "WM_IME_SETCONTEXT" },
{ WM_INITMENU, "WM_INITMENU" },
{ WM_INITMENUPOPUP, "WM_INITMENUPOPUP" },
{ WM_KEYDOWN, "WM_KEYDOWN" },
{ WM_KEYUP, "WM_KEYUP" },
{ WM_KILLFOCUS, "WM_KILLFOCUS" },
{ WM_MENUSELECT, "WM_MENUSELECT" },
{ WM_MOUSEACTIVATE, "WM_MOUSEACTIVATE" },
{ WM_MOUSEMOVE, "WM_MOUSEMOVE" },
{ WM_MOVE, "WM_MOVE" },
{ WM_MOVING, "WM_MOVING" },
{ WM_NCACTIVATE, "WM_NCACTIVATE" },
{ WM_NCCALCSIZE, "WM_NCCALCSIZE" },
{ WM_NCCREATE, "WM_NCCREATE" },
{ WM_NCHITTEST, "WM_NCHITTEST" },
{ WM_NCLBUTTONDOWN, "WM_NCLBUTTONDOWN" },
{ WM_NCMOUSEMOVE, "WM_NCMOUSEMOVE" },
{ WM_NCPAINT, "WM_NCPAINT" },
{ WM_PAINT, "WM_PAINT" },
{ WM_PARENTNOTIFY, "WM_PARENTNOTIFY" },
{ WM_QUERYNEWPALETTE, "WM_QUERYNEWPALETTE" },
{ WM_RBUTTONUP, "WM_RBUTTONUP" },
{ WM_SETCURSOR, "WM_SETCURSOR" },
{ WM_SETFOCUS, "WM_SETFOCUS" },
{ WM_SETTEXT, "WM_SETTEXT" },
{ WM_SHOWWINDOW, "WM_SHOWWINDOW" },
{ WM_SIZE, "WM_SIZE" },
{ WM_SIZING, "WM_SIZING" },
{ WM_STYLECHANGED, "WM_STYLECHANGED" },
{ WM_STYLECHANGING, "WM_STYLECHANGING" },
{ WM_SYSCOMMAND, "WM_SYSCOMMAND" },
{ WM_SYSKEYDOWN, "WM_SYSKEYDOWN" },
{ WM_SYSKEYUP, "WM_SYSKEYUP" },
{ WM_SYSTIMER, "WM_SYSTIMER" },
{ WM_TIMER, "WM_TIMER" },
{ WM_USER, "WM_USER" },
{ WM_WINDOWPOSCHANGED, "WM_WINDOWPOSCHANGED" },
{ WM_WINDOWPOSCHANGING, "WM_WINDOWPOSCHANGING" },
/* custom ntwinio WINDOWS messages */
{ WM_WVILE_CURSOR_ON, "WM_WVILE_CURSOR_ON" },
{ WM_WVILE_CURSOR_OFF, "WM_WVILE_CURSOR_OFF" },
/* *INDENT-ON* */
};
size_t n;
static char temp[20];
for (n = 0; n < TABLESIZE(table); n++) {
if (table[n].code == code) {
return table[n].name;
}
}
sprintf(temp, "%#x", code);
return temp;
}
static char *
syscommand2s(unsigned code)
{
static struct {
WORD code;
char *name;
} table[] = {
/* *INDENT-OFF* */
{ SC_SIZE, "SC_SIZE" },
{ SC_MOVE, "SC_MOVE" },
{ SC_MINIMIZE, "SC_MINIMIZE" },
{ SC_MAXIMIZE, "SC_MAXIMIZE" },
{ SC_NEXTWINDOW, "SC_NEXTWINDOW" },
{ SC_PREVWINDOW, "SC_PREVWINDOW" },
{ SC_CLOSE, "SC_CLOSE" },
{ SC_VSCROLL, "SC_VSCROLL" },
{ SC_HSCROLL, "SC_HSCROLL" },
{ SC_MOUSEMENU, "SC_MOUSEMENU" },
{ SC_KEYMENU, "SC_KEYMENU" },
{ SC_ARRANGE, "SC_ARRANGE" },
{ SC_RESTORE, "SC_RESTORE" },
{ SC_TASKLIST, "SC_TASKLIST" },
{ SC_SCREENSAVE, "SC_SCREENSAVE" },
{ SC_HOTKEY, "SC_HOTKEY" },
/* *INDENT-ON* */
};
size_t n;
static char temp[80];
unsigned remainder = code & 0xf;
for (n = 0; n < TABLESIZE(table); n++) {
if (table[n].code == (code - remainder)) {
sprintf(temp, "%s+%X", table[n].name, remainder);
return temp;
}
}
sprintf(temp, "%#x", code);
return temp;
}
static void
TraceWindowRect(HWND hwnd)
{
RECT wrect;
int rows;
int cols;
GetWindowRect(hwnd, &wrect);
cols = RectToCols(wrect);
rows = RectToRows(wrect);
TRACE(("... (%3ld,%3ld) (%3ld,%3ld), window is %dx%d cells (%dx%d pixels)%s\n",
wrect.top, wrect.left,
wrect.bottom, wrect.right,
rows,
cols,
RowToPixel(rows),
ColToPixel(cols),
IsZoomed(hwnd) ? " zoomed" : ""
));
}
static void
TraceClientRect(HWND hwnd)
{
RECT crect;
int rows;
int cols;
GetClientRect(hwnd, &crect);
cols = RectToCols(crect);
rows = RectToRows(crect);
TRACE(("... (%3ld,%3ld) (%3ld,%3ld), client is %dx%d cells (%dx%d pixels)\n",
crect.top, crect.left,
crect.bottom, crect.right,
rows,
cols,
RowToPixel(rows),
ColToPixel(cols)
));
}
#else
#define TraceWindowRect(hwnd) /* nothing */
#define TraceClientRect(hwnd) /* nothing */
#endif
static HBRUSH
Background(HDC hdc)
{
TRACE(("Background %#06lx\n", GetBkColor(hdc)));
return CreateSolidBrush(GetBkColor(hdc));
}
void
gui_resize(int cols, int rows)
{
RECT crect;
RECT wrect;
int main_wide;
int main_high;
int text_wide;
int text_high;
/*
* There's an undesirable feedback loop between gui_resize() and
* ResizeClient() when the user changes the font. Here's how it
* goes:
*
* - user changes font
* - use_font() is eventually called
* - use_font() calls gui_resize()
* - gui_resize calls MoveWindow(cur_win->main_hwnd, ...) which triggers
* a call to MainWndProc(), which calls ResizeClient()
* - ResizeClient() calls gui_resize()
*
* ResizeClient(), unfortunately, recomputes the editor's rows and
* cols and sometimes gets it wrong, especially when the number of
* rows exceeds a magic value (e.g., 37 rows on a 1024 x 768
* screen).
*
* To stop the feedback loop, check to see if an instance of
* gui_resize() is active, and if so, exit.
*
* Note: even though "gui_resize_in_progress" breaks the
* aforementioned feedback loop, bear in mind that it's still not
* possible to define a screen geometry that's greater than the
* dimensions of the current desktop. This restriction is enforced by
* Windows when the first ShowWindow() call is made from
* winvile_start().
*/
if (gui_resize_in_progress && font_resize_in_progress)
return;
gui_resize_in_progress = TRUE;
TRACE(("gui_resize(%d x %d)\n", rows, cols));
TraceWindowRect(cur_win->main_hwnd);
TraceClientRect(cur_win->main_hwnd);
GetClientRect(cur_win->main_hwnd, &crect);
GetWindowRect(cur_win->main_hwnd, &wrect);
text_wide = ColToPixel(cols);
text_high = RowToPixel(rows);
wrect.right += text_wide - crect.right;
wrect.bottom += text_high - crect.bottom;
main_wide = text_wide + (2 * cur_win->x_border) + SbWidth;
main_high = text_high + (2 * cur_win->y_border) + cur_win->y_titles;
TRACE(("... gui_resize -> (%ld,%ld) (%ld,%ld) main %dx%d, text %dx%d\n",
wrect.top,
wrect.left,
wrect.bottom,
wrect.right,
main_high,
main_wide,
text_high,
text_wide));
MoveWindow(cur_win->main_hwnd,
wrect.left,
wrect.top,
main_wide,
main_high,
TRUE);
MoveWindow(cur_win->text_hwnd,
0,
0,
text_wide,
text_high,
TRUE);
TraceWindowRect(cur_win->main_hwnd);
TraceClientRect(cur_win->main_hwnd);
#if OPT_SCROLLBARS
update_scrollbar_sizes();
#endif
TRACE(("... gui_resize finish\n"));
gui_resize_in_progress = FALSE;
}
static int
AdjustedHeight(int high)
{
int extra = cur_win->y_titles + (2 * cur_win->y_border);
int rows;
if (high > cur_win->xy_limit.bottom)
high = cur_win->xy_limit.bottom;
rows = PixelToRow(high - extra);
if (rows < MIN_ROWS)
rows = MIN_ROWS;
return RowToPixel(rows) + extra;
}
static int
AdjustedWidth(int wide)
{
int extra = SbWidth + (2 * cur_win->x_border);
int cols;
if (wide > cur_win->xy_limit.right)
wide = cur_win->xy_limit.right;
cols = PixelToCol(wide - extra);
if (cols < MIN_COLS)
cols = MIN_COLS;
return ColToPixel(cols) + extra;
}
static WINDOW_PROC_RETVAL
AdjustPosChanging(HWND hwnd, WINDOWPOS * pos)
{
(void) hwnd;
if (!(pos->flags & SWP_NOSIZE)) {
int wide = AdjustedWidth(pos->cx);
int high = AdjustedHeight(pos->cy);
TRACE(("...%s position %d,%d, resize from %d,%d to %d,%d\n",
IsZoomed(hwnd) ? " zoomed" : "",
pos->y, pos->x,
pos->cy, pos->cx, high, wide));
pos->cx = wide;
pos->cy = high;
}
return 0;
}
static HWND
sizing_window(void)
{
RECT crect;
int szw = RSZ_WDW_WDTH, szh = RSZ_WDW_HGHT, szx, szy;
GetClientRect(cur_win->main_hwnd, &crect);
szx = crect.right / 2 - szw / 2;
szy = crect.bottom / 2 - szh / 2;
if (szx < 0 || szy < 0)
szx = szy = CW_USEDEFAULT;
return (CreateWindow(W32_STRING("STATIC"),
W32_STRING(""),
WS_CHILD | WS_VISIBLE | SS_CENTER,
szx,
szy,
szw,
szh,
cur_win->main_hwnd,
(HMENU) 0,
vile_hinstance,
(LPVOID) 0));
}
/*
* Handle WM_SIZING, forcing the screen size to stay in multiples of a
* character cell.
*/
static WINDOW_PROC_RETVAL
AdjustResizing(HWND hwnd, WPARAM fwSide, RECT * rect)
{
int wide = rect->right - rect->left;
int high = rect->bottom - rect->top;
int adjX = wide - AdjustedWidth(wide);
int adjY = high - AdjustedHeight(high);
(void) hwnd;
TRACE(("AdjustResizing now (%ld,%ld) (%ld,%ld) (%ldx%ld pixels)\n",
rect->top, rect->left,
rect->bottom, rect->right,
(rect->bottom - rect->top),
(rect->right - rect->left)));
TraceWindowRect(hwnd);
TraceClientRect(hwnd);
if (fwSide == WMSZ_LEFT
|| fwSide == WMSZ_TOPLEFT
|| fwSide == WMSZ_BOTTOMLEFT)
rect->left += adjX;
else if (fwSide == WMSZ_RIGHT
|| fwSide == WMSZ_TOPRIGHT
|| fwSide == WMSZ_BOTTOMRIGHT)
rect->right -= adjX;
if (fwSide == WMSZ_TOP
|| fwSide == WMSZ_TOPLEFT
|| fwSide == WMSZ_TOPRIGHT)
rect->top += adjY;
else if (fwSide == WMSZ_BOTTOM
|| fwSide == WMSZ_BOTTOMLEFT
|| fwSide == WMSZ_BOTTOMRIGHT)
rect->bottom -= adjY;
TRACE(("... AdjustResizing (%ld,%ld) (%ld,%ld) adjY:%d, adjX:%d\n",
rect->top, rect->left,
rect->bottom, rect->right,
adjY, adjX));
return TRUE;
}
static void
ResizeClient(void)
{
int h, w;
RECT crect;
if (cur_win->closed) {
TRACE(("ResizeClient ignored (closed)\n"));
return;
}
/*
* See comments in gui_resize() for an explanation of
* gui_resize_in_progress.
*/
if (gui_resize_in_progress && font_resize_in_progress)
return;
TRACE(("ResizeClient begin, currently %dx%d\n", term.rows, term.cols));
TraceWindowRect(cur_win->main_hwnd);
TraceClientRect(cur_win->main_hwnd);
GetClientRect(cur_win->main_hwnd, &crect);
h = RectToRows(crect);
w = RectToCols(crect);
/*
* The WM_WINDOWPOSCHANGING message is supposed to allow modification
* to keep a window in bounds. But it doesn't work. This does (by
* forcing the calls on MoveWindow to have a "good" value).
*/
if (h < MIN_ROWS)
h = MIN_ROWS;
if (w < MIN_COLS)
w = MIN_COLS;
if ((h > 1 && h != term.rows) || (w > 1 && w != term.cols)) {
TRACE(("...ResizeClient %dx%d\n", h, w));
vile_resizing = TRUE;
newscreensize(h, w);
SetRows(h);
SetCols(w);
#if OPT_SCROLLBARS
if (check_scrollbar_allocs() == TRUE) /* no allocation failure */
update_scrollbar_sizes();
#endif
vile_resizing = FALSE;
TRACE(("...ResizeClient %dx%d\n", h, w));
} else {
TRACE(("ResizeClient ignored (h=%d, w=%d, vs %d x %d)\n",
h, w, term.rows, term.cols));
}
gui_resize(w, h);
TRACE(("...ResizeClient finish\n"));
}
static int RedValue[NCOLORS], GreenValue[NCOLORS], BlueValue[NCOLORS];
/*
* Ask for a palette setting string.
* Format is "<entry> <red> <green> <blue>"
*/
int
SetRGBPalette(int f, int n)
{
#define RGB_DATA(name) { name, 0 }
static struct {
char *name;
int value;
} table[3] = {
RGB_DATA("red"), RGB_DATA("green"), RGB_DATA("blue")
};
char *next;
char prompt[NLINE + 1];
char tstring[NLINE + 1]; /* string to add */
FSM_BLIST *fp = name_to_choices("fcolor");
int code;
int count = 0;
int status; /* status return code */
long value;
(void) f;
(void) n;
*tstring = EOS;
status = kbd_string("Color: ", tstring, sizeof(tstring), ' ',
KBD_NORMAL, fsm_complete);
if (status != TRUE)
return (status);
code = choice_to_code(fp, tstring, strlen(tstring));
if (code < 0) {
next = 0;
code = strtol(tstring, &next, 0);
if (code < 0 || code >= NCOLORS)
return FALSE;
}
/* ask for setting string */
while (count < 3) {
*tstring = EOS;
sprintf(prompt, "Palette value (%s): ", table[count].name);
status = mlreply(prompt, tstring, sizeof(tstring));
if (status != TRUE)
return (status);
next = 0;
value = strtol(tstring, &next, 0);
if (next == 0 || *next)
return FALSE;
table[count++].value = value & 255;
}
RedValue[code] = table[0].value;
GreenValue[code] = table[1].value;
BlueValue[code] = table[2].value;
set_winflags(TRUE, WFHARD | WFCOLR);
vile_refresh(FALSE, 1);
return (TRUE);
}
/*
* Set default color map palette
*/
int
ResetRGBPalette(int f, int n)
{
int code;
int red, green, blue;
int first = f ? n : 0;
int last = f ? n + 1 : NCOLORS;
for (code = first; code < last && code < NCOLORS; code++) {
red = green = blue = 0;
if (code & 1)
red = rgb_normal;
if (code & 2)
green = rgb_normal;
if (code & 4)
blue = rgb_normal;
if (code & 8) {
if (red)
red = rgb_bright;
if (green)
green = rgb_bright;
if (blue)
blue = rgb_bright;
if (code == 8) {
red = rgb_gray;
green = rgb_gray;
blue = rgb_gray;
}
}
RedValue[code] = red;
GreenValue[code] = green;
BlueValue[code] = blue;
}
set_winflags(TRUE, WFHARD | WFCOLR);
vile_refresh(FALSE, 1);
return TRUE;
}
static COLORREF
color_of(int code)
{
code = ctrans[code & (NCOLORS - 1)];
return PALETTERGB(RedValue[code], GreenValue[code], BlueValue[code]);
}
/*
* Translate the attribute into indices into the color table.
*/
static int
attr_to_colors(VIDEO_ATTR attr, int *fcolor, int *bcolor)
{
int ninvert = FALSE; /* normal colors inverted? */
*fcolor = nfcolor;
*bcolor = nbcolor;
attr &= (VASPCOL | VACOLOR | VABOLD | VAITAL | VASEL | VAREV);
if (attr) {
ninvert = ((attr & (VASEL | VAREV)) == VASEL
|| (attr & (VASEL | VAREV)) == VAREV);
if (attr & VASPCOL)
*fcolor = (VCOLORNUM(attr) & (NCOLORS - 1));
else if (attr & VACOLOR)
*fcolor = ((VCOLORNUM(attr)) & (NCOLORS - 1));
if (ninvert) {
int temp = *bcolor;
*bcolor = *fcolor;
*fcolor = temp;
}
TRACE2(("attr_to_colors(%04x) fg=%2d, bg=%2d %s\n",
attr, *fcolor, *bcolor, ninvert ? "INVERT" : ""));
}
return ninvert;
}
static int
fake_color(int current, int nominal)
{
int x = nominal;
if (current == ENUM_FCOLOR) {
int r = GetRValue(nominal) ^ 0x40;
int g = GetGValue(nominal) ^ 0x40;
int b = GetBValue(nominal) ^ 0x40;
x = PALETTERGB(r, g, b);
}
TRACE2(("fake_color(%d, %#x) = %#x\n", current, nominal, x));
nominal = x;
return nominal;
}
/*
* Provide an array of inter-character spacing, needed to force bold and italic fonts
* to align with the normal font.
*/
static INT *
intercharacter(int cols)
{
if (cols == 0 || nCharWidth == 0)
return 0;
else {
static INT *result;
static unsigned length;
if (++cols >= (int) length) {
length = 1 + ((cols * 3) / 2);
safe_typereallocn(INT, result, length);
}
if (result != 0) {
while (--cols >= 0)
result[cols] = nCharWidth;
}
return result;
}
}
static HFONT
GetMyFont(VIDEO_ATTR attr)
{
unsigned n;
if ((attr & (VAUL | VABOLD | VAITAL)) != 0) {
for (n = 1; n < TABLESIZE(MyFonts); ++n) {
if ((attr & MyFonts[n].attr) == MyFonts[n].attr)
break;
}
} else {
n = 0;
}
if (n >= TABLESIZE(MyFonts)) {
return GetMyFont(VAUL);
} else if (!MyFonts[n].used) {
LOGFONT logfont = vile_logfont;
logfont.lfItalic = (BYTE) ((attr & VAITAL) != 0);
logfont.lfUnderline = (BYTE) ((attr & VAUL) != 0);
if (attr & VABOLD)
logfont.lfWeight = FW_SEMIBOLD;
if ((MyFonts[n].font = CreateFontIndirect(&logfont)) != 0) {
MyFonts[n].used = TRUE;
} else if ((attr & (VABOLD | VAITAL)) == (VABOLD | VAITAL)) {
return GetMyFont(VABOLD);
} else if ((attr & VAUL) != 0) {
return GetMyFont(VAUL);
} else {
n = 0;
}
} else if (MyFonts[n].font == 0) {
n = 0; /* live with previous failure */
}
return MyFonts[n].font;
}
static void
SetMyFont(HFONT font, LOGFONT * lf)
{
unsigned n;
for (n = 0; n < TABLESIZE(MyFonts); ++n) {
if (MyFonts[n].used) {
DeleteObject(MyFonts[n].font);
MyFonts[n].used = FALSE;
}
}
MyFonts[0].font = font;
MyFonts[0].used = TRUE;
if (lf != 0 && lf != &vile_logfont)
vile_logfont = *lf;
}
static HDC
get_DC_with_Font(HFONT font)
{
HDC hDC = GetDC(cur_win->text_hwnd);
if (hDC != 0) {
if (SelectFont(hDC, font) == 0)
ReleaseDC(cur_win->text_hwnd, hDC);
}
return hDC;
}
static void
nt_set_colors(HDC hdc, VIDEO_ATTR attr)
{
int fcolor;
int bcolor;
int ninvert;
SelectFont(hdc, GetMyFont(attr));
#ifdef GVAL_VIDEO
attr ^= global_g_val(GVAL_VIDEO);
#endif
ninvert = attr_to_colors(attr, &fcolor, &bcolor);
if (fcolor < 0)
fcolor = ninvert
? fake_color(fcolor, default_fcolor)
: fake_color(fcolor, default_bcolor);
else
fcolor = color_of(fcolor);
if (bcolor < 0)
bcolor = ninvert
? fake_color(bcolor, default_bcolor)
: fake_color(bcolor, default_fcolor);
else
bcolor = color_of(bcolor);
SetTextColor(hdc, fcolor);
SetBkColor(hdc, bcolor);
}
static int
fhide_cursor(void)
{
TRACE(("fhide_cursor pos %#x,%#x (visible:%d, exists:%d)\n", ttrow,
ttcol, caret_visible, caret_exists));
if (!ac_active) {
if (caret_visible) {
HideCaret(cur_win->text_hwnd);
caret_visible = 0;
}
if (caret_exists) {
DestroyCaret();
caret_exists = 0;
}
}
return 0;
}
static void
fshow_cursor(void)
{
int x, y, width;
POINT z;
if (ac_active /* reject while repainting autocolor */
|| caret_disabled /* reject display during font-dialog */
|| ttrow > term.rows /* reject bogus position in init */
|| ttcol > term.cols)
return;
TRACE(("fshow_cursor pos %#x,%#x (visible:%d, exists:%d)\n", ttrow,
ttcol, caret_visible, caret_exists));
x = ColToPixel(ttcol) + 1 - nCursorAdj;
y = RowToPixel(ttrow) + 1;
if (caret_exists) {
GetCaretPos(&z);
if (x != z.x
|| y != z.y)
fhide_cursor();
}
if (!caret_exists) {
width = nCharWidth; /* assume block cursor */
#ifdef UNICODE
/*
* If the current cell that we are pointing to contains a zero, then
* that is an extension of a multicolumn character. Widen the cell to
* account for it.
*/
if (CELL_TEXT(ttrow, ttcol) == 0) {
width *= 2;
x -= (nCharWidth + nCursorAdj);
}
#endif
#if OPT_ICURSOR
if (icursor) {
int vertbar = FALSE;
if (insertmode) {
if (icursor_style == 1)
vertbar = TRUE;
} else { /* command mode */
if (icursor_style == 2)
vertbar = TRUE;
}
if (vertbar) {
if (width < 5)
width = 2;
else
width = 3;
}
}
#endif
TRACE(("...CreateCaret(%d,%d)\n", ttrow, ttcol));