forked from quozl/netrek-client-cow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnu_win32.c
3510 lines (2967 loc) · 101 KB
/
gnu_win32.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
/* mswindow.c
* Windowing code for Win32 API (aka Windows NT)
* Jonathan Shekter Aug 94
* Updates for COW version, Aug 1995
* Shawn Collenburg
* Lots of stuff, Aug 1996
* COW 3.0 support, based on gnu win32 development system
* Kurt Siegl May 1998
*/
#include "copyright2.h"
// #undef DEBUG
#define NO_BOOLEAN //So defs.h doesn't screw us over
#include "config.h"
#undef min
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include "Wlib.h"
#include "defs.h"
#include "struct.h"
#include "data.h"
#include "xclrs.h"
#include "teams.bitmap"
#include "mapcursor.bitmap"
#include "localcursor.bitmap"
#include <limits.h>
#include INC_STRINGS
#undef WHITE
#undef BLACK
#undef RED
#undef GREEN
#undef YELLOW
#undef CYAN
#define WHITE 0
#define BLACK 1
#define RED 2
#define GREEN 3
#define YELLOW 4
#define CYAN 5
#define GREY 6
#ifdef RACE_COLORS
#define C_ROM 7
#define C_KLI 8
#define C_FED 9
#define C_ORI 10
#define C_IND 11
#endif
#ifdef RACE_COLORS
#define COLORS 12
#else
#define COLORS 7
#endif
#define RaceDefaultOffset (C_ROM - RED)
#define WIN_EDGE 1 //Width or window border edge
#define MENU_PAD 4
#define MENU_BAR 4
#define WIN_GRAPH 1
#define WIN_TEXT 2
#define WIN_MENU 3
#define WIN_SCROLL 4
#define MoveTo(dc, x, y) MoveToEx(dc, x, y, NULL);
// Custom window messages, used for communicating between threads
#define WM_TERMINATE_WAIT WM_USER
#define WM_CROSS_THREAD_DESTROY (WM_USER+1)
#ifndef DEBUG
#define MAX_SCROLLWINDOW_LINES 100
#else
#define MAX_SCROLLWINDOW_LINES 300
#endif
//The max # lines a scrollwindow will have
#define EVENT_Q_SIZE 15
//The number of events our custom queue will hold
struct menuItem
{
char *string;
W_Color color;
short len;
};
struct stringList
{
char *string;
W_Color color;
struct stringList *next;
};
typedef struct tagWindow
{
HWND hwnd;
short type;
short border;
W_Color BorderColor;
short UsingCreatedCursor;
HCURSOR cursor;
short tiled;
struct Icon *TileIcon;
short NumItems;
struct menuItem *items;
struct stringList *strings;
short AddedStrings;
short TextHeight;
short TextWidth;
RECT ClipRect;
W_Callback HandleKeydown;
W_Callback HandleKeyup;
W_Callback HandleButton;
W_Callback HandleExpose;
}
Window;
static Window myroot;
#define FNHEADER\
register Window *win;\
if (!window)\
return(0);\
win = (Window *)window
#define FNHEADER_VOID\
register Window *win;\
if (!window)\
return;\
win = (Window *)window
/******************************* Function prototypes *********************/
void W_Cleanup(void );
void GetColors();
LRESULT PASCAL NetrekWndProc(HWND,UINT,WPARAM,LPARAM);
unsigned char *X11toCursor(unsigned char *bits, int width, int height);
void RedrawMenu(Window *win, HDC hdc);
void RedrawScrolling(Window *win, HDC hdc);
void ChangeMenuItem(Window *win, int n, char *str, int len, W_Color color);
void AddToScrolling(Window *win, W_Color color, char *str, int len);
void DrawBorder(Window *win, HDC hdc);
unsigned char *X11toDIB(unsigned char *bits, int width, int height);
unsigned char *X11toDIBAndMirror(unsigned char *bits, int width, int height,
int outwidth, int outheight);
int checkGeometry(char *name, int *x, int *y, int *width, int *height);
void checkParent(char *name, W_Window *parent);
int checkMapped(char *name);
char *GetExeDir();
inline void ResetSysColors(void);
inline void SetTrekSysColors(void);
/******************************* Globals ****************************/
HINSTANCE MyInstance;
char ExeDir[100];
char HomeDir[100];
char ClassName[] = "NetrekWindow";
char FontFileName[] = "\\netrek.fon";
extern int DefaultsLoaded;
HWND AnyWindow;
DWORD MainThreadID;
#ifdef SUPPORT_WIN32S
extern W_Window console;
#endif
int W_FastClear=0;
W_Font W_BigFont, W_RegularFont;
W_Font W_HighlightFont, W_UnderlineFont;
W_Color W_White=WHITE, W_Black=BLACK, W_Red=RED, W_Green=GREEN;
W_Color W_Yellow=YELLOW, W_Cyan=CYAN, W_Grey=GREY;
#ifdef RACE_COLORS
W_Color W_Ind = C_IND, W_Fed = C_FED, W_Rom = C_ROM, W_Kli = C_KLI, W_Ori = C_ORI;
#endif
int W_Textwidth=6, W_Textheight=10;
int forceMono = 0;
const int SysColorNames[] = { COLOR_BTNFACE, COLOR_BTNTEXT, COLOR_3DFACE,
COLOR_3DDKSHADOW, COLOR_3DHILIGHT };
DWORD TrekSysColors[] = { 0, 0xffffff, 0, 0xc0c0c0, 0x808080 };
DWORD SysColors[sizeof(TrekSysColors)];
HDC GlobalMemDC, GlobalMemDC2;
HBITMAP GlobalOldMemDCBitmap, GlobalOldMemDC2Bitmap;
HCURSOR TrekCursor, WarnCursor;
struct
{
COLORREF rgb;
HBRUSH brush;
HPEN pen;
HPEN dashedpen;
}
colortable[COLORS] = {
{ RGB(0xff, 0xff, 0xff), 0, 0, 0}, //White
{ RGB(0x00, 0x00, 0x00), 0, 0, 0}, //Black
{ RGB(0xff, 0x5f, 0x5f), 0, 0, 0}, //Red
{ RGB(0x5f, 0xff, 0x5f), 0, 0, 0}, //Green
{ RGB(0xff, 0xff, 0x5f), 0, 0, 0}, //Yellow
{ RGB(0x5f, 0xff, 0xff), 0, 0, 0}, //Cyan
{ RGB(0xa0, 0xa0, 0xa0), 0, 0, 0} //Light grey
#ifdef RACE_COLORS
,
{ RGB(0xff, 0x5f, 0x5f), 0, 0, 0}, //Rom
{ RGB(0x5f, 0xff, 0x5f), 0, 0, 0}, //Kli
{ RGB(0xff, 0xff, 0x5f), 0, 0, 0}, //Fed
{ RGB(0x5f, 0xff, 0xff), 0, 0, 0}, //Ori
{ RGB(0xa0, 0xa0, 0xa0), 0, 0, 0} //Ind
#endif
},
// Used when we're in fixed-16 color mode - color values altered so that the color
// mapper picks the proper color. These are "intense" versions of the standard colors
altcolortable[COLORS] = {
{ RGB(0xff, 0xff, 0xff), 0, 0, 0}, //White
{ RGB(0x00, 0x00, 0x00), 0, 0, 0}, //Black
{ RGB(0xff, 0x00, 0x00), 0, 0, 0}, //Red
{ RGB(0x00, 0xff, 0x00), 0, 0, 0}, //Green
{ RGB(0xff, 0xff, 0x00), 0, 0, 0}, //Yellow
{ RGB(0x00, 0xff, 0xff), 0, 0, 0}, //Cyan
{ RGB(0xa0, 0xa0, 0xa0), 0, 0, 0} //Light grey
#ifdef RACE_COLORS
,
{ RGB(0xff, 0x00, 0x00), 0, 0, 0}, //Rom
{ RGB(0x00, 0xff, 0x00), 0, 0, 0}, //Kli
{ RGB(0xff, 0xff, 0x00), 0, 0, 0}, //Fed
{ RGB(0x00, 0xff, 0xff), 0, 0, 0}, //Ori
{ RGB(0xa0, 0xa0, 0xa0), 0, 0, 0} //Ind
#endif
};
char *colornames[COLORS] = {
{"white"},
{"black"},
{"red"},
{"green"},
{"yellow"},
{"cyan"},
{"light grey"}
#ifdef RACE_COLORS
,
{"Rom"},
{"Kli"},
{"Fed"},
{"Ori"},
{"Ind"}
#endif
};
HPALETTE NetrekPalette = 0;
//A structure that the W_Icon type (really a bitmap, not an icon) points to
struct Icon
{
HWND hwnd; //The window this is going into
HBITMAP bm; //The Windows bitmap it has been stored in
int x,y; //The source position in this bitmap
int width, height; //guess...
RECT *ClipRectAddr; //Address of the window clip region
};
#ifndef SCALE_BITMAPS
#define BIGBITMAP_WIDTH 240
#define BIGBITMAP_HEIGHT 240
#else
static int BIGBITMAP_WIDTH = 240;
static int BIGBITMAP_HEIGHT = 240;
#endif
struct BitmapList
{
HBITMAP bm;
struct BitmapList *next; //Put these in a LL for later de-allocation
};
struct BitmapList *CurrentBitmap = NULL;
int bmlCount = 0;
//Table that we will build use for virtual key code conversion. We can't
//just let Windows do it as we want to proces strange things like
// ^@ or ^# which the standard windows TranslateMessage() / ToAscii()
//will not do. So, we process VK codes instead, which means we have
//to manually map the SHIFT key. We create this mapping by asking for
// the VK + shift state for each ascii char during init.
//Mapping tables for VK codes to ASCII. We can't just let windows do
//it as we want strange combinations like ^@, so we build these maps
//ourselves.
char VKMap[256];
char VKShiftMap[256];
char *GetExeDir()
{
return ExeDir;
}
void W_Initialize(char *display)
{
int i;
TEXTMETRIC tm;
HBITMAP junk;
LOGFONT lf;
char FileName[100];
WNDCLASS wc;
static int InitDone = 0;
char * c;
HDC hdc;
char *fontname;
char defaultFontname[100];
GetColors();
if (InitDone)
return;
InitDone = -1;
#ifdef DEBUG
printf("Initializing windowing system\n");
#endif
MyInstance = GetModuleHandle(NULL);
//Get the executable file directory
c = ExeDir + GetModuleFileName(MyInstance, ExeDir, sizeof(ExeDir)) -1;
while (*c != '\\') //remove filename
*(c--) = 0;
*c = 0; //remove last backslash
//Register our class
wc.style = CS_NOCLOSE | CS_HREDRAW | CS_VREDRAW; //Don't allow them to close windows
wc.lpfnWndProc = NetrekWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = sizeof(Window *); //Store Window * in extra space
wc.hInstance = MyInstance;
wc.hIcon = LoadIcon(MyInstance, "main");
wc.hCursor = NULL; //We're handling our own cursors
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = ClassName;
RegisterClass(&wc);
//Load our cursors - the ones we use from a resource file, anyway
TrekCursor = LoadCursor(MyInstance, "trek");
WarnCursor = LoadCursor(MyInstance, "warn");
/* Clear the font structure. */
memset(&lf, 0, sizeof(LOGFONT));
/*
* In order to try and match the old windows client try and load
* up the netrek font.
*/
strcpy(FileName, GetExeDir());
strcat(FileName, FontFileName);
if (AddFontResource(FileName) == 0)
{
strcpy (defaultFontname, "Netrek");
}
else
{
strcpy (defaultFontname, "\0");
}
/* Negative font height means match character units not cell units */
lf.lfHeight = -intDefault("fontsize", 10);
lf.lfCharSet = ANSI_CHARSET;
lf.lfOutPrecision = OUT_TT_PRECIS;
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf.lfQuality = DEFAULT_QUALITY;
lf.lfPitchAndFamily = FF_MODERN | FIXED_PITCH;
/*
* If the user has set a font use that
*/
fontname = getdefault("font");
if (fontname == NULL)
strcpy (lf.lfFaceName, defaultFontname);
else
strcpy (lf.lfFaceName, fontname);
lf.lfWeight = FW_REGULAR;
W_RegularFont = (W_Font) CreateFontIndirect(&lf);
if (W_RegularFont == NULL)
{
printf ("Unable to create regular font.\n");
}
/*
* If the user has set a font use that
*/
fontname = getdefault("boldfont");
if (fontname == NULL)
strcpy (lf.lfFaceName, defaultFontname);
else
strcpy (lf.lfFaceName, fontname);
lf.lfWeight = FW_BOLD;
W_HighlightFont = (W_Font) CreateFontIndirect(&lf);
if (W_HighlightFont == NULL)
{
printf ("Unable to create highlight font.\n");
}
/*
* If the user has set a font use that
*/
fontname = getdefault("italicfont");
if (fontname == NULL)
strcpy (lf.lfFaceName, defaultFontname);
else
strcpy (lf.lfFaceName, fontname);
lf.lfWeight = FW_REGULAR;
lf.lfUnderline = TRUE;
W_UnderlineFont = (W_Font) CreateFontIndirect(&lf);
if (W_UnderlineFont == NULL)
{
printf ("Unable to create Underline font.\n");
}
/*
* If the user has set a font use that
*/
fontname = getdefault("bigfont");
if (fontname == NULL)
strcpy (lf.lfFaceName, "Arial");
else
strcpy (lf.lfFaceName, fontname);
lf.lfUnderline = FALSE;
lf.lfWeight = FW_MEDIUM;
lf.lfHeight = 52; // 52
lf.lfWidth = 32;
W_BigFont = (W_Font) CreateFontIndirect(&lf);
if (W_BigFont == NULL)
{
printf ("Unable to create Big font.\n");
}
//Set up or memory DCs. We need to get the default bitmap and store it,
//for later use. The only way we can do this is to select another bitmap
//into it... So we'll create a junk one, then delete it.
GlobalMemDC = CreateCompatibleDC(NULL);
GlobalMemDC2 = CreateCompatibleDC(NULL);
junk = CreateBitmap(1,1,1,1,NULL);
GlobalOldMemDCBitmap = (HBITMAP) SelectObject(GlobalMemDC, junk);
SelectObject(GlobalMemDC, GlobalOldMemDCBitmap);
GlobalOldMemDC2Bitmap = (HBITMAP) SelectObject(GlobalMemDC2, junk);
SelectObject(GlobalMemDC2, GlobalOldMemDC2Bitmap);
DeleteObject(junk);
/*
* Determine font size.
*/
{
TEXTMETRIC ftm;
SelectObject(GlobalMemDC,W_HighlightFont);
if (GetTextMetrics(GlobalMemDC,&ftm))
{
W_Textwidth = ftm.tmAveCharWidth;
W_Textheight = ftm.tmHeight;
}
SelectObject(GlobalMemDC,W_RegularFont);
if (GetTextMetrics(GlobalMemDC,&ftm))
{
if (W_Textwidth < ftm.tmAveCharWidth)
W_Textwidth = ftm.tmAveCharWidth;
if (W_Textheight < ftm.tmHeight)
W_Textheight = ftm.tmHeight;
}
SelectObject(GlobalMemDC,W_UnderlineFont);
if (GetTextMetrics(GlobalMemDC,&ftm))
{
if (W_Textwidth < ftm.tmAveCharWidth)
W_Textwidth = ftm.tmAveCharWidth;
if (W_Textheight < ftm.tmHeight)
W_Textheight = ftm.tmHeight;
}
}
//Create Virtual Key mapping table
memset(VKMap, 0, sizeof(VKMap));
memset(VKShiftMap, 0, sizeof(VKShiftMap));
for (i=0; i<128; i++)
{
SHORT res = VkKeyScan((TCHAR) i);
/* Converted SHR's to TESTs, faster on x86 -SAC */
if (!(res & 0xff00)) //!highbyte == no shift,ctrl,alt
VKMap[res] = i;
else if (res & 0x100) //Bit 1 of high byte = shift key
VKShiftMap[res & 0xff] = i;
}
VKMap[VK_ESCAPE] = 27; // 27 is mapped as Ctrl-[ by Windows
VKMap[VK_TAB] = 'i'+96; // Make it look like '^i' so macroKey: TAB will work
VKShiftMap[VK_SPACE] =' '; // Map shift+space -> space
VKShiftMap[VK_RETURN] ='\r'; // Map shift+return -> return
VKShiftMap[VK_ESCAPE] =27; // Map shift+escape-> escape
MainThreadID = GetCurrentThreadId(); // Save main thread ID so we can tell
// which thread we're in later
// Get the current system colors
for (i=0; i<sizeof(SysColorNames); i++)
{
SysColors[i] = GetSysColor(SysColorNames[i]);
}
GetPixmaps(&GlobalMemDC, &myroot);
atexit(W_Cleanup);
}
//Does all the resource de-allocation needed
//The rest of the silly code doesn't play nice and call DestroyIcon(), etc,
//so we have to track resource usage ourself.
void W_Cleanup()
{
int i;
struct BitmapList *tmp, *p = CurrentBitmap;
char FileName[100];
// Reset the system colors to something recognizable
ResetSysColors();
//Get rid of the color pens, etc.
for (i=0; i<COLORS; i++)
{
DeleteObject(colortable[i].brush);
DeleteObject(colortable[i].pen);
DeleteObject(colortable[i].dashedpen);
}
//Delete our two loaded (and shared, and hence not deleted when the window is
//destroyed) cursors
DestroyCursor(TrekCursor);
DestroyCursor(WarnCursor);
//Delete our fonts
DeleteObject((HFONT)W_RegularFont);
DeleteObject((HFONT)W_HighlightFont);
DeleteObject((HFONT)W_UnderlineFont);
DeleteObject((HFONT)W_BigFont);
strcpy(FileName, GetExeDir());
strcat(FileName, FontFileName);
RemoveFontResource(FileName);
//Select the original bitmaps back and delete our memory DCs
SelectObject(GlobalMemDC, GlobalOldMemDCBitmap);
DeleteDC(GlobalMemDC);
SelectObject(GlobalMemDC2, GlobalOldMemDC2Bitmap);
DeleteDC(GlobalMemDC2);
//Destory our custom palette
if (NetrekPalette)
DeleteObject(NetrekPalette);
//Remove the bitmap structures we've created, by traversing the linked list
while (p)
{
tmp = p->next;
DeleteObject(p->bm);
free(p);
p = tmp;
}
}
/*
* Figure out what kind of display we have, for color mapping
* There are basically three possible situations:
* 1) A fixed 16 color display (use alternate colors so they map well)
* 2) A paletted 8-bit display (create a custom palette and use it)
* 3) A 16 (15?) or 24 bit true-color display (no problem...)
* We also let the use force a selection on the above.
*/
#define hexdig(a) ( ((a)>='a' && (a)<='f') ? ((a)-'a'+10) : \
( ((a)>='A' && (a)<='F') ? ((a)-'A'+10) : ((a)-'0') ) )
#define hexbyte(a) ( hexdig(*(a))*16 + hexdig(*((a)+1)) )
void GetColors()
{
int i,j;
char *def;
int dtype=0;
HDC hdc;
if (DefaultsLoaded)
{
dtype=intDefault("forceDisplay", 0);
if (dtype==0)
{
if (booleanDefault("forceMono", 0))
dtype = 1;
else
{
//Look at display hardware and make a choice
hdc = GetDC(HWND_DESKTOP);
i=GetDeviceCaps(hdc, BITSPIXEL) * GetDeviceCaps(hdc, PLANES);
#ifdef DEBUG
printf("Bits per pixel detected: %d\n", i);
#endif
if ( (i<=4) || ((i<15) && !(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)) )
dtype = 1;
else if ( (i<15) && (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) )
dtype=2;
else
dtype=3;
ReleaseDC(HWND_DESKTOP, hdc);
}
}
if (dtype == 1) // need extra bright clrs on 4 plane display
memcpy(colortable, altcolortable, sizeof(colortable));
//Modify colors from netrekrc file directives
for (i=0; i<COLORS; i++)
{
char buf[30];
sprintf(buf, "color.%s", colornames[i]); //Check netrekrc file
def = getdefault(buf);
#ifdef RACE_COLORS
if (!def && i>=COLORS-RaceDefaultOffset) // For race colors we use default color
{ // color for that race if not set, i.e.
sprintf(buf, "color.%s", colornames[i-RaceDefaultOffset]);
def = getdefault(buf); // if "color.rom" is not set, "color.red"
}
#endif
if (def)
if (def[0] == '#') //Explicit RGB color
{
colortable[i].rgb = RGB(hexbyte(def+1), hexbyte(def+3), hexbyte(def+5));
}
else
{
for (j=0; j<XCLRS; j++) //Else assume color name
if (!stricmp(def, xclrs[j].name))
break;
if (j!=XCLRS)
colortable[i].rgb = RGB(xclrs[j].r, xclrs[j].g, xclrs[j].b);
else
fprintf(stderr, "Color '%s' unknown\n", def);
}
}
switch (dtype)
{
case 1:
#ifdef DEBUG
printf("16 color display detected.\n");
#endif
break;
case 2:
//Paletted display. Create a custom palette, and set the RGB indices
//in the colortable to palette indices instead of explicit colors
{
char space[sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * (COLORS)];
LOGPALETTE *pal = (LOGPALETTE *) space;
pal->palVersion = 0x300; //Windows version >=3.0 palette
pal->palNumEntries = COLORS;
for (i=0; i<COLORS; i++)
{
pal->palPalEntry[i].peRed = GetRValue(colortable[i].rgb);
pal->palPalEntry[i].peGreen = GetGValue(colortable[i].rgb);
pal->palPalEntry[i].peBlue = GetBValue(colortable[i].rgb);
pal->palPalEntry[i].peFlags = 0;
colortable[i].rgb = PALETTEINDEX(i);
}
NetrekPalette = CreatePalette(pal);
#ifdef DEBUG
printf("Paletted color display detected.\n");
#endif
}
break;
//else, everything's okey dokey, we have a true-color display
}
//Create the various pens and brushes for each color
for (i=0; i<COLORS; i++)
{
DWORD dashbits[] = { 1, 2 };
colortable[i].brush = CreateSolidBrush(colortable[i].rgb);
colortable[i].pen = CreatePen(PS_SOLID | PS_INSIDEFRAME, 1, colortable[i].rgb);
colortable[i].dashedpen = CreatePen(PS_DOT, 1, colortable[i].rgb);
}
}
}
W_Window W_RenameWindow(W_Window window, char *str)
{
FNHEADER;
SetWindowText(win->hwnd, str);
return window;
}
//Internal function - used as the base for all window creation
Window *newWindow(char *name, int x, int y, int width, int height,
W_Window parent, int border, W_Color color, int type)
{
Window *window,*parentwin=0;
HDC hdc;
char title_buff[100];
char *s;
DWORD SpecialStyle = 0;
if ( !(window = (Window *)malloc(sizeof(Window)) ) )
{
printf("Not enough memory to create a new window.");
return 0;
}
memset(window, 0, sizeof(Window));
//Stuff ripped from x11window.c - provides nicer titles when the window
// is a wait window or the main window.
if (strcmp (name, "netrek") == 0)
{
if (title)
s = title;
else
{
s = title_buff;
sprintf (title_buff, "Netrek @ %s", serverName);
}
SpecialStyle = WS_OVERLAPPEDWINDOW; //Make main window sizeable
//SpecialStyle |=WS_BORDER;
}
else if (strcmp (name, "wait") == 0)
{
if (title)
s = title;
else
s = serverName;
width += GetSystemMetrics( SM_CXFRAME);
//Make the wait window show up in the task list
SpecialStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
parentwin = &myroot;
}
else if (strncmp (name, "Meta", 4) == 0)
{
s = name;
height += GetSystemMetrics( SM_CYFRAME ) + border;
//Make the Metaserver window show up in the task list
SpecialStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
parentwin = &myroot;
}
#ifdef SUPPORT_WIN32S
else if (strcmp (name, "Console") == 0)
{
SpecialStyle = WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
s = name;
}
#endif
else
s = name;
//Mask border - 15 pixels max - and make sure it's at least 1, and that it's not black
if (border > 15) border = 15;
if (!border) border=1;
if (color == BLACK) color = WHITE;
//Set the various attributes to default states
window->type = type;
window->border = border;
window->BorderColor=color;
window->cursor = LoadCursor(NULL, IDC_ARROW);
//Check the parent - parent to root (==desktop) if null
if (!parentwin)
{
if (!parent)
{
parentwin = (baseWin ? (Window *)baseWin : &myroot);
SpecialStyle |= baseWin ? WS_POPUP | WS_CAPTION : WS_POPUP;
}
else
{
parentwin = (Window *)parent;
SpecialStyle |= WS_CHILD;
}
}
//Actually create the window
//Hacked to allow negative create locations -SAC
window->hwnd = CreateWindow( ClassName,
s,
WS_CLIPSIBLINGS | WS_CLIPCHILDREN | SpecialStyle,
x + parentwin->border,
y + parentwin->border,
width + border*2,
height + border*2 +
( (SpecialStyle & WS_CAPTION) ? GetSystemMetrics( SM_CYCAPTION ) : 0),
parentwin->hwnd,
NULL,
MyInstance,
(void *)window); //Pass Window struct * as user param
if (!window->hwnd)
{
printf("CreateWindow() for %s failed...",name);
return(0);
}
hdc = GetDC(window->hwnd);
//Select the custom palette if we're in a paletted mode
if (NetrekPalette)
{
SelectPalette(hdc, NetrekPalette, FALSE);
RealizePalette(hdc);
}
ReleaseDC(window->hwnd, hdc);
if (!AnyWindow)
AnyWindow = window->hwnd;
return window;
}
W_Window W_MakeWindow(char *name, int x, int y, int width, int height,
W_Window parent, int border, W_Color color)
{
Window *newwin;
//Get the default position, etc.
checkGeometry(name, &x, &y, &width, &height);
//Get the default parent..
checkParent(name, &parent);
if ( !(newwin = newWindow(name, x, y, width, height, parent, border, color, WIN_GRAPH)))
return(0);
//Map (show) the window if the user spec'd it
if (checkMapped(name))
W_MapWindow((W_Window)newwin);
return (W_Window) newwin;
}
//Make a window that contains fixed-spaced text, where columns must be
//aligned, etc. The x and y passed are in character cells, not pixels.
W_Window W_MakeTextWindow(char *name, int x, int y, int width, int height,
W_Window parent, int border)
{
Window *newwin;
//Get the default position, etc.
checkGeometry(name, &x, &y, &width, &height);
//Get the default parent..
checkParent(name, &parent);
if (!(newwin = newWindow( name, x, y,
width * W_Textwidth + WIN_EDGE * 2,
MENU_PAD * 2 + height * W_Textheight,
parent, border, W_Black, WIN_TEXT)) )
return(0);
//Store the original textheight, width
newwin->TextHeight = height;
newwin->TextWidth = width;
//Map (show) the window if the user spec'd it
if (checkMapped(name))
W_MapWindow((W_Window)newwin);
return (W_Window) newwin;
}
// Make a WIN_SCROLL type window.
// We use a scrollbar so we can look through the text, something the X version
// didn't have. Nyah, nyah.
W_Window W_MakeScrollingWindow(char *name, int x, int y, int width, int height,
W_Window parent, int border)
{
HWND hsb;
RECT ra;
Window *newwin;
int i;
//Get the default position, etc.
checkGeometry(name, &x, &y, &width, &height);
//Get the default parent..
checkParent(name, &parent);
if (! (newwin = newWindow( name, x, y,
width * W_Textwidth + WIN_EDGE * 2 + GetSystemMetrics(SM_CXVSCROLL),
height * W_Textheight + MENU_PAD * 2,
parent, border, W_White, WIN_SCROLL)) )
return(0);
//Store the original textheight, width
newwin->TextHeight = height;
newwin->TextWidth = width;
//Give it a scroll bar, and set the range (to zero, initially)
SetWindowLong(newwin->hwnd, GWL_STYLE, GetWindowLong(newwin->hwnd, GWL_STYLE) | WS_VSCROLL );
SetScrollRange(newwin->hwnd, SB_VERT, 0, 0, FALSE);
//Map (show) the window if the user spec'd it
if (checkMapped(name))
W_MapWindow((W_Window)newwin);
return (W_Window) newwin;
}
//Make a menu window. This is similar to a text window in that it has a 1x1 mappiing
//between coordinates and character cells. Note that the height parameter
//specified here indicates the number of items
W_Window W_MakeMenu(char *name, int x, int y,int width,int height,W_Window parent,int border)
{
struct menuItem *items;
Window *newwin;
HWND ParentHWND;
int i;
//Get the default position, etc.
checkGeometry(name, &x, &y, &width, &height);
//Get the default parent..
checkParent(name, &parent);
if (! (newwin = newWindow( name, x, y,
width * W_Textwidth + WIN_EDGE * 2,
height * (W_Textheight + MENU_PAD * 2) + (height - 1) * MENU_BAR,
parent, border, W_Black, WIN_MENU)) )
return(0);
if ( !(items=(struct menuItem *) malloc(height*sizeof(struct menuItem))) )
fprintf(stderr,"Could not allocate storage for menu window");
else
for (i=0; i<height; i++)
{
items[i].string=NULL;
items[i].color=W_White;
}
newwin->items = items;
newwin->NumItems = height;
newwin->TextHeight = height;
//Map (show) the window if the user spec'd it
if (checkMapped(name))
W_MapWindow((W_Window)newwin);
return (W_Window) newwin;
}
void W_ChangeBorder(W_Window window, W_Color color)
{
HDC hdc;
FNHEADER_VOID;
win->BorderColor = color;
hdc = GetDC(win->hwnd); //Turn off boder clipping
if (NetrekPalette)
{
SelectPalette(hdc, NetrekPalette, FALSE);
RealizePalette(hdc);
}
DrawBorder(win, hdc);
ReleaseDC(win->hwnd, hdc);
}
//Displays the window.
void W_MapWindow(W_Window window)
{
FNHEADER_VOID;
ShowWindow(win->hwnd, SW_SHOWNORMAL);
BringWindowToTop(win->hwnd);
if (window==baseWin &&
!booleanDefault("netrek.w32caption",1))
PostMessage(win->hwnd, WM_SYSKEYDOWN, VK_RETURN, 0);
}
//Hides the window.