/
sqWin32Window.c
3323 lines (2923 loc) · 102 KB
/
sqWin32Window.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
/****************************************************************************
* PROJECT: Squeak port for Win32 (NT / Win95)
* FILE: sqWin32Window.c
* CONTENT: Window management
*
* AUTHOR: Andreas Raab (ar)
* ADDRESS: University of Magdeburg, Germany
* EMAIL: raab@isg.cs.uni-magdeburg.de
*
* NOTES:
* 1) Currently supported Squeak color depths include 1,4,8,16,32 bits
* 2) To speed up drawing a slight update delay has been added (Toggle with F2)
* 3) The modifier keys are mapped as follows:
*
* Mac | Win32
* --------------------
* Shift -> Shift
* Ctrl -> Ctrl
* Command -> Left ALT
* Option -> Right ALT
*
*****************************************************************************/
#include <windows.h>
#include <shellapi.h>
#include <commdlg.h>
#include <excpt.h>
#if defined(__MINGW32_VERSION) && (__MINGW32_MAJOR_VERSION < 3)
/** Kludge to get multimonitor API's to compile in the mingw/directx7 mix. **/
/** Not needed in cygwin **/
# define COMPILE_MULTIMON_STUBS
# undef SM_CMONITORS
# define HMONITOR_DECLARED
# include "multimon.h"
#endif /* defined(__MINGW32_VERSION) && (__MINGW32_MAJOR_VERSION < 3) */
#include "sq.h"
#include "sqWin32Prefs.h"
#include "sqSCCSVersion.h"
/****************************************************************************/
/* General Squeak declarations and definitions */
/****************************************************************************/
/* Import from DropPlugin/sqWin32Drop.c */
void SetupDragAndDrop(void);
int dropLaunchFile(char *fileName);
/* Import from VM */
void setInterruptPending(sqInt);
sqInt forceInterruptCheck(void);
sqInt getInterruptKeycode(void);
void setFullScreenFlag(sqInt);
sqInt getSavedWindowSize(void);
extern sqInt deferDisplayUpdates;
/*** Variables -- image and path names ***/
#define IMAGE_NAME_SIZE MAX_PATH
char imageName[MAX_PATH+1]; /* full path and name to image */
TCHAR imagePath[MAX_PATH+1]; /* full path to image */
TCHAR vmPath[MAX_PATH+1]; /* full path to interpreter's directory */
TCHAR vmName[MAX_PATH+1]; /* name of the interpreter's executable */
char windowTitle[MAX_PATH]; /* what should we display in the title? */
TCHAR squeakIniName[MAX_PATH+1]; /* full path and name to ini file */
TCHAR windowClassName[MAX_PATH+1]; /* Window class name */
const TCHAR U_ON[] = TEXT("1");
const TCHAR U_OFF[] = TEXT("0");
const TCHAR U_GLOBAL[] = TEXT("Global");
const TCHAR U_SLASH[] = TEXT("/");
const TCHAR U_BACKSLASH[] = TEXT("\\");
/*** Variables -- Event Recording ***/
int inputSemaphoreIndex = 0;/* if non-zero the event semaphore index */
POINT mousePosition; /* position at last PointerMotion event */
int mouseWord; /* Input word for Squeak */
int buttonState = 0; /* mouse button and modifier state when mouse
button went down or 0 if not pressed */
DWORD winButtonState = 0;
#define KEYBUF_SIZE 64
int keyBuf[KEYBUF_SIZE]; /* circular buffer */
int keyBufGet = 0; /* index of next item of keyBuf to read */
int keyBufPut = 0; /* index of next item of keyBuf to write */
int keyBufOverflows = 0; /* number of characters dropped */
/*** Win32-related Variables (declared in sqWin32.h) ***/
HWND stWindow = NULL; /* the squeak window */
HWND browserWindow = NULL; /* The browser window */
HINSTANCE hInstance; /* the instance of squeak running */
HCURSOR currentCursor=0; /* current cursor displayed by squeak */
HPALETTE palette; /* the palette (might be unused) */
LOGPALETTE *logPal; /* the logical palette definition */
BITMAPINFO *bmi1; /* 1 bit depth bitmap info */
BITMAPINFO *bmi4; /* 4 bit depth bitmap info */
BITMAPINFO *bmi8; /* 8 bit depth bitmap info */
BITMAPINFO *bmi16; /* 16 bit depth bitmap info */
BITMAPINFO *bmi32; /* 32 bit depth bitmap info */
BOOL fWindows95; /* Are we running on Win95 or NT? */
BOOL fHasFocus = 0; /* if Squeak has the input focus */
/* Preference values */
BOOL fDeferredUpdate = 1; /* I prefer the deferred update*/
BOOL fShowConsole = 0; /* do we show the console window?*/
BOOL fDynamicConsole = 1; /* Should we show the console if any errors occur? */
BOOL fShowAllocations = 0; /* Show allocation activity */
BOOL fReduceCPUUsage = 1; /* Should we reduce CPU usage? */
BOOL fReduceCPUInBackground = 0; /* Should we reduce CPU usage when not active? */
BOOL fUseDirectSound = 1; /* Do we use DirectSound?! */
BOOL fRunSingleApp = 0; /* Do we allow only one instance of this VM? */
#ifdef CROQUET
BOOL fUseOpenGL = 1; /* Do we use OpenGL vs. D3D?! */
#else /* Squeak */
BOOL fUseOpenGL = 0; /* Do we use OpenGL vs. D3D?! */
#endif
BOOL fPriorityBoost = 1;
BOOL f1ButtonMouse = 0; /* Should we use a 1 button mouse mapping? */
BOOL f3ButtonMouse = 0; /* Should we use a real 3 button mouse mapping? */
/* Startup options */
BOOL fHeadlessImage = 0; /* Do we run headless? */
BOOL fRunService = 0; /* Do we run as NT service? */
DWORD dwMemorySize = 0; /* How much memory do we use? */
BOOL fBrowserMode = 0; /* Are we running in a web browser? */
/* Misc preferences */
BOOL fEnableAltF4Quit = 1; /* can we quit using Alt-F4? */
BOOL fEnableF2Menu = 1; /* can we get prefs menu via F2? */
BOOL fEnablePrefsMenu = 1; /* can we get a prefs menu at all? */
HANDLE vmWakeUpEvent = 0; /* wake up interpret() from sleep */
/* variables for cached display */
RECT updateRect; /* the rectangle to update */
HRGN updateRgn; /* the region to update (more accurate) */
BOOL updateRightNow; /* update flag */
HWND consoleWindow; /* console */
int wasFullScreen = 0; /* are in fullscreen mode? */
int shouldBeFullScreen = 0; /* or should we be in fullscreen mode? */
/* variables for DirectX support */
RECT stWindowRect; /* Client rectangle in screen coordinates */
#ifndef NO_PRINTER
/* printer settings */
PRINTDLG printValues;
static int printerSetup = FALSE;
#endif
#ifndef NO_WHEEL_MOUSE
UINT g_WM_MOUSEWHEEL = 0; /* RvL: 1999-04-19 The message we receive from wheel mices */
#endif
/* misc forward declarations */
int recordMouseEvent(MSG *msg, UINT nrClicks);
int recordKeyboardEvent(MSG *msg);
int recordWindowEvent(int action, RECT *r);
#if NewspeakVM
int ioDrainEventQueue(void);
#endif
extern sqInt byteSwapped(sqInt);
extern int convertToSqueakTime(SYSTEMTIME);
int recordMouseDown(WPARAM, LPARAM);
int recordModifierButtons();
int recordKeystroke(UINT,WPARAM,LPARAM);
int recordVirtualKey(UINT,WPARAM,LPARAM);
void recordMouse(void);
void SetSystemTrayIcon(BOOL on);
void HideSplashScreen(void);
sqInputEvent *sqNextEventPut(void);
int sqLaunchDrop(void);
#ifdef PharoVM
/**
* HACK: Hook for SDL2.
*/
static void (*ioCheckForEventsHooks)(void);
EXPORT(void) setIoProcessEventsHandler(void * handler) {
ioCheckForEventsHooks = (void (*)())handler;
}
#endif
extern int sqAskSecurityYesNoQuestion(const char *question)
{
return MessageBoxA(stWindow, question, "Squeak Security Alert", MB_YESNO | MB_ICONSTOP) == IDYES;
}
extern const char *sqGetCurrentImagePath(void)
{
return imagePath;
}
/****************************************************************************/
/* Synchronization functions */
/****************************************************************************/
/* NOTE: Why do we need this? When running multi-threaded code such as in
the networking code and in midi primitives
we will signal the interpreter several semaphores.
(Predates the internal synchronization of signalSemaphoreWithIndex ()) */
int synchronizedSignalSemaphoreWithIndex(int semaIndex)
{
int result;
/* Do our job - this is now synchronized in signalSemaphoreWithIndex */
result = signalSemaphoreWithIndex(semaIndex);
/* wake up interpret() if sleeping */
SetEvent(vmWakeUpEvent);
return result;
}
/****************************************************************************/
/* Message Processing */
/****************************************************************************/
/* The last dispatched event. It is used for the event processing mechanism. */
MSG *lastMessage = NULL;
/* The entry to the message hooks called from the window procedure.
If another module requires to process messages by itself, it should
put its message procedure in this place. */
messageHook firstMessageHook = 0;
/* The entry to a pre-message hook. Can be used to intercept any messages
to the squeak main window. Useful for modules that wish to be notified
about certain messages before they are processed. */
messageHook preMessageHook = 0;
/* main window procedure(s) */
LRESULT CALLBACK MainWndProcA(HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam) {
return DefWindowProc(hwnd, message, wParam, lParam);
}
LRESULT CALLBACK MainWndProcW(HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
PAINTSTRUCT ps;
static UINT lastClickTime = 0;
static UINT nrClicks = 0;
UINT timeNow = 0;
UINT timeDelta = 0;
/* Intercept any messages if wanted */
if(preMessageHook)
if((*preMessageHook)(hwnd, message,wParam, lParam))
return 1;
if(message == SQ_LAUNCH_DROP)
return sqLaunchDrop();
#ifndef NO_WHEEL_MOUSE
/* RvL 1999-04-19 00:23
MOUSE WHEELING START */
if( WM_MOUSEWHEEL == message || g_WM_MOUSEWHEEL == message ) {
/* Record mouse wheel msgs as CTRL-Up/Down.
* N.B. On iOS & X11 we also handle horizonal mouse wheel events.
* Should the same happen here?
*/
short zDelta = (short) HIWORD(wParam);
if(inputSemaphoreIndex) {
sqKeyboardEvent *evt = (sqKeyboardEvent*) sqNextEventPut();
evt->type = EventTypeKeyboard;
evt->timeStamp = lastMessage->time;
evt->charCode = (zDelta > 0) ? 30 : 31;
evt->pressCode = EventKeyChar;
/* N.B. on iOS & X11 all meta bits are set to distinguish mouse wheel
* events from real arrow key events. Should the same happen here?
*/
evt->modifiers = CtrlKeyBit;
/* It would be good if this were set in the SqueakVM also, no? */
#ifdef PharoVM
evt->utf32Code = evt->charCode;
#else
evt->utf32Code = 0;
#endif
evt->reserved1 = 0;
} else {
buttonState = 64;
if (zDelta < 0) {
recordVirtualKey(message,VK_DOWN,lParam);
} else {
recordVirtualKey(message,VK_UP,lParam);
}
}
return 1;
}
/* MOUSE WHEELING END */
#endif
switch(message) {
case WM_SYSCOMMAND:
case WM_COMMAND: {
int cmd = wParam & 0xFFF0;
if(cmd >= ID_PREF_FIRST && cmd <= ID_PREF_LAST) {
HandlePrefsMenu(cmd);
break;
}
#if !defined(_WIN32_WCE)
if(cmd == SC_MINIMIZE) {
if(fHeadlessImage) ShowWindow(stWindow, SW_HIDE);
else return DefWindowProcW(hwnd, message, wParam, lParam);
break;
}
#endif /* defined(_WIN32_WCE) */
if(cmd == SC_CLOSE) {
#if NewspeakVM
/* Newspeak doesn't easnt to quit if the main window is closed. Only
* when the last native window is closed.
*/
if(fEnableAltF4Quit)
ShowWindow(stWindow, SW_HIDE);
#else
if(prefsEnableAltF4Quit() || GetKeyState(VK_SHIFT) < 0) {
TCHAR msg[1001], label[1001];
GetPrivateProfileString(U_GLOBAL, TEXT("QuitDialogMessage"),
TEXT("Quit ") TEXT(VM_NAME) TEXT(" without saving?"),
msg, 1000, squeakIniName);
GetPrivateProfileString(U_GLOBAL, TEXT("QuitDialogLabel"),
TEXT(VM_NAME),
label, 1000, squeakIniName);
if(MessageBox(stWindow, msg, label, MB_YESNO) != IDYES)
return 0;
DestroyWindow(stWindow);
ioExit();
/*NOTREACHED*/
}
recordWindowEvent(WindowEventClose, NULL);
#endif /* NewspeakVM */
break;
}
return DefWindowProcW(hwnd,message,wParam,lParam);
break;
}
/* mousing */
case WM_MOUSEMOVE:
/* we have to be careful to not clear nrClicks if the mouse
happens to move in-between rapid clicks */
timeNow = GetMessageTime(); /* Win32 - gets time of last GetMessage() */
timeDelta = timeNow - lastClickTime;
if (timeDelta > GetDoubleClickTime())
nrClicks = 0;
if(inputSemaphoreIndex) {
recordMouseEvent(lastMessage, nrClicks);
break;
}
/* state based stuff */
mousePosition.x = LOWORD(lParam);
mousePosition.y = HIWORD(lParam);
break;
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
{ /* update the button mask for this event */
switch (message) {
case WM_LBUTTONDOWN: winButtonState |= MK_LBUTTON; break;
case WM_RBUTTONDOWN: winButtonState |= MK_RBUTTON; break;
case WM_MBUTTONDOWN: winButtonState |= MK_MBUTTON; break;
}
}
if(GetFocus() != stWindow) SetFocus(stWindow);
SetCapture(stWindow); /* capture mouse input */
/* count mouse clicks */
timeNow = GetMessageTime(); /* Win32 - gets time of last GetMessage() */
timeDelta = timeNow - lastClickTime;
nrClicks = (timeDelta <= GetDoubleClickTime()) ? (nrClicks + 1) : 1;
lastClickTime = timeNow;
if(inputSemaphoreIndex) {
recordMouseEvent(lastMessage, nrClicks);
break;
}
/* state based stuff */
mousePosition.x = LOWORD(lParam);
mousePosition.y = HIWORD(lParam);
/* check for console focus */
recordMouseDown(wParam, lParam);
recordModifierButtons();
/* capture the mouse as long as the button is pressed so we can scroll outside */
SetCapture(stWindow);
break;
case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_MBUTTONUP:
{ /* avoid posting mouse ups for which we haven't seen the
corresponding mouse down events (native dialogs etc) */
DWORD upMask = 0;
switch (message) {
case WM_LBUTTONUP: upMask = MK_LBUTTON; break;
case WM_RBUTTONUP: upMask = MK_RBUTTON; break;
case WM_MBUTTONUP: upMask = MK_MBUTTON; break;
}
if((winButtonState & upMask) == 0) break;
winButtonState &= ~upMask; /* clear current mask */
}
if(GetFocus() != stWindow) SetFocus(stWindow);
ReleaseCapture(); /* release mouse capture */
if(inputSemaphoreIndex) {
recordMouseEvent(lastMessage, nrClicks);
break;
}
/* state based stuff */
mousePosition.x = LOWORD(lParam);
mousePosition.y = HIWORD(lParam);
/* check for console focus */
if(GetFocus() != stWindow) SetFocus(stWindow);
recordMouseDown(wParam,lParam);
recordModifierButtons();
/* release capture */
ReleaseCapture();
break;
/* virtual key codes */
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
if(GetFocus() == consoleWindow)
return DefWindowProcW(hwnd, message, wParam, lParam);
if(inputSemaphoreIndex) {
recordKeyboardEvent(lastMessage);
if(wParam == VK_F2 && prefsEnableF2Menu()) {
TrackPrefsMenu();
}
if(wParam == VK_F4) {
/* We must let F4 through here if we want Alt-F4 to work */
return DefWindowProcW(hwnd, message, wParam, lParam);
}
break;
}
/* state based stuff */
recordModifierButtons();
if(!recordVirtualKey(message,wParam,lParam))
return DefWindowProcW(hwnd,message,wParam,lParam);
break;
case WM_KEYUP:
case WM_SYSKEYUP:
if(GetFocus() == consoleWindow)
return DefWindowProcW(hwnd, message, wParam, lParam);
if(inputSemaphoreIndex) {
recordKeyboardEvent(lastMessage);
break;
}
/* state based stuff */
recordModifierButtons();
break;
/* character codes */
case WM_CHAR:
case WM_SYSCHAR:
if(GetFocus() == consoleWindow)
return DefWindowProcW(hwnd, message, wParam, lParam);
if(inputSemaphoreIndex) {
recordKeyboardEvent(lastMessage);
break;
}
/* state based stuff */
recordModifierButtons();
recordKeystroke(message,wParam,lParam);
break;
case WM_DEADCHAR:
case WM_SYSDEADCHAR:
break;
/* expose events */
case WM_PAINT:
/* retrieve update region _before_ calling BeginPaint() !!! */
GetUpdateRgn(hwnd,updateRgn,FALSE);
BeginPaint(hwnd,&ps);
/* store the rectangle required for image bit reversal */
updateRect = ps.rcPaint;
EndPaint(hwnd,&ps);
/* Avoid repaint unless absolutely necessary (null update rects are quite common) */
if (!IsRectEmpty(&updateRect)) {
/* force redraw the next time ioShowDisplay() is called */
updateRightNow = TRUE;
fullDisplayUpdate(); /* this makes VM call ioShowDisplay */
}
break;
case WM_SIZE:
if(hwnd == stWindow) {
/* Adjust the console window */
GetClientRect(stWindow,&stWindowRect);
MoveWindow(consoleWindow, 0,
stWindowRect.bottom-100, stWindowRect.right, 100, 1);
/* Record the global stWindowRect for DirectX */
MapWindowPoints(stWindow, NULL, (LPPOINT)&stWindowRect, 2);
}
else return DefWindowProcW(hwnd,message,wParam,lParam);
case WM_MOVE:
if(hwnd == stWindow) {
/* Record the global stWindowRect for DirectX */
GetClientRect(stWindow,&stWindowRect);
MapWindowPoints(stWindow, NULL, (LPPOINT) &stWindowRect, 2);
}
else return DefWindowProcW(hwnd,message,wParam,lParam);
/* Erasing the background leads to flashing so avoid it */
case WM_ERASEBKGND:
return TRUE;
case WM_ACTIVATE:
if(wParam == WA_INACTIVE) {
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
} else if(fPriorityBoost) {
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
}
break;
#if !defined(_WIN32_WCE)
/* Don't change the cursor or system tray on WinCE */
/* cursor redraw */
case WM_SETCURSOR:
/* keep currentCursor */
if((LOWORD(lParam) == HTCLIENT) && currentCursor) {
SetCursor(currentCursor);
break;
}
else return DefWindowProcW(hwnd,message,wParam,lParam);
case WM_USER+42:
/* system tray notification */
if(wParam != (usqIntptr_t)hInstance) return 0;
/* if right button, show system menu */
/* if(lParam == WM_RBUTTONUP)
TrackPrefsMenu(GetSystemMenu(stWindow,0)); */
/* if double clicked, show main window */
if(lParam == WM_LBUTTONDBLCLK) {
if(!IsWindowVisible(stWindow))
ShowWindow(stWindow, SW_SHOW);
BringWindowToTop(stWindow);
}
return 0;
#endif /* !defined(_WIN32_WCE) */
/* Focus handling */
case WM_SETFOCUS:
fHasFocus = 1;
return DefWindowProcW(hwnd,message,wParam,lParam);
case WM_KILLFOCUS:
fHasFocus = 0;
return DefWindowProcW(hwnd,message,wParam,lParam);
default:
/* Unprocessed messages may be processed outside the current
module. If firstMessageHook is non-NULL and returns a non
zero value, the message has been successfully processed */
if(firstMessageHook)
if((*firstMessageHook)(hwnd, message, wParam, lParam))
return 1;
return DefWindowProcW(hwnd,message,wParam,lParam);
}
return 1;
}
/****************************************************************************/
/* Printer Setup */
/****************************************************************************/
// 18 June 2008 - jdm renamed from SetDefaultPrinter() which conflicts with Windows func
// No one seemed to be calling this func, anyway...
void SetTheDefaultPrinter()
{
#ifndef NO_PRINTER
if(!printerSetup) SetupPrinter();
printValues.Flags = PD_PRINTSETUP;
PrintDlg(&printValues);
#endif /* NO_PRINTER */
}
void SetupPrinter()
{
#ifndef NO_PRINTER
ZeroMemory(&printValues, sizeof(printValues));
printValues.lStructSize = sizeof(PRINTDLG);
printValues.hInstance = hInstance;
printValues.nMinPage = 1;
printValues.nMaxPage = 1;
printValues.nFromPage = 1;
printValues.nToPage = 1;
printValues.hwndOwner = stWindow;
/* fetch default DEVMODE/DEVNAMES */
printValues.Flags = PD_RETURNDEFAULT;
PrintDlg(&printValues);
printerSetup = 1;
#endif
}
/****************************************************************************/
/* Color and Bitmap setup */
/****************************************************************************/
#ifndef NO_STANDARD_COLORS
static void SetColorEntry(int index, int red, int green, int blue)
{
logPal->palPalEntry[index].peRed = red / 256;
logPal->palPalEntry[index].peGreen = green / 256;
logPal->palPalEntry[index].peBlue = blue / 256;
logPal->palPalEntry[index].peFlags = 0;
}
/* Generic color maps and bitmap info headers 1,4,8,16,32 bits per pixel */
void SetupPixmaps(void)
{ int i;
logPal = malloc(sizeof(LOGPALETTE) + 255 * sizeof(PALETTEENTRY));
if (!logPal) {
printLastError(TEXT("malloc pallette"));
return;
}
logPal->palVersion = 0x300;
logPal->palNumEntries = 256;
/* 1-bit colors (monochrome) */
SetColorEntry(0, 65535, 65535, 65535); /* white or transparent */
SetColorEntry(1, 0, 0, 0); /* black */
/* additional colors for 2-bit color */
SetColorEntry(2, 65535, 65535, 65535); /* opaque white */
SetColorEntry(3, 32768, 32768, 32768); /* 1/2 gray */
/* additional colors for 4-bit color */
SetColorEntry( 4, 65535, 0, 0); /* red */
SetColorEntry( 5, 0, 65535, 0); /* green */
SetColorEntry( 6, 0, 0, 65535); /* blue */
SetColorEntry( 7, 0, 65535, 65535); /* cyan */
SetColorEntry( 8, 65535, 65535, 0); /* yellow */
SetColorEntry( 9, 65535, 0, 65535); /* magenta */
SetColorEntry(10, 8192, 8192, 8192); /* 1/8 gray */
SetColorEntry(11, 16384, 16384, 16384); /* 2/8 gray */
SetColorEntry(12, 24576, 24576, 24576); /* 3/8 gray */
SetColorEntry(13, 40959, 40959, 40959); /* 5/8 gray */
SetColorEntry(14, 49151, 49151, 49151); /* 6/8 gray */
SetColorEntry(15, 57343, 57343, 57343); /* 7/8 gray */
/* additional colors for 8-bit color */
/* 24 more shades of gray (does not repeat 1/8th increments) */
SetColorEntry(16, 2048, 2048, 2048); /* 1/32 gray */
SetColorEntry(17, 4096, 4096, 4096); /* 2/32 gray */
SetColorEntry(18, 6144, 6144, 6144); /* 3/32 gray */
SetColorEntry(19, 10240, 10240, 10240); /* 5/32 gray */
SetColorEntry(20, 12288, 12288, 12288); /* 6/32 gray */
SetColorEntry(21, 14336, 14336, 14336); /* 7/32 gray */
SetColorEntry(22, 18432, 18432, 18432); /* 9/32 gray */
SetColorEntry(23, 20480, 20480, 20480); /* 10/32 gray */
SetColorEntry(24, 22528, 22528, 22528); /* 11/32 gray */
SetColorEntry(25, 26624, 26624, 26624); /* 13/32 gray */
SetColorEntry(26, 28672, 28672, 28672); /* 14/32 gray */
SetColorEntry(27, 30720, 30720, 30720); /* 15/32 gray */
SetColorEntry(28, 34815, 34815, 34815); /* 17/32 gray */
SetColorEntry(29, 36863, 36863, 36863); /* 18/32 gray */
SetColorEntry(30, 38911, 38911, 38911); /* 19/32 gray */
SetColorEntry(31, 43007, 43007, 43007); /* 21/32 gray */
SetColorEntry(32, 45055, 45055, 45055); /* 22/32 gray */
SetColorEntry(33, 47103, 47103, 47103); /* 23/32 gray */
SetColorEntry(34, 51199, 51199, 51199); /* 25/32 gray */
SetColorEntry(35, 53247, 53247, 53247); /* 26/32 gray */
SetColorEntry(36, 55295, 55295, 55295); /* 27/32 gray */
SetColorEntry(37, 59391, 59391, 59391); /* 29/32 gray */
SetColorEntry(38, 61439, 61439, 61439); /* 30/32 gray */
SetColorEntry(39, 63487, 63487, 63487); /* 31/32 gray */
/* The remainder of color table defines a color cube with six steps
for each primary color. Note that the corners of this cube repeat
previous colors, but simplifies the mapping between RGB colors and
color map indices. This color cube spans indices 40 through 255.
*/
{
int r, g, b;
for (r= 0; r < 6; r++)
for (g= 0; g < 6; g++)
for (b= 0; b < 6; b++)
{
int i= 40 + ((36 * r) + (6 * b) + g);
if (i > 255) error("index out of range in color table compuation");
SetColorEntry(i, (r * 65535) / 5, (g * 65535) / 5, (b * 65535) / 5);
}
}
palette = CreatePalette(logPal);
/* generate BITMAPINFOHEADERs */
/* 1 bit color depth */
bmi1 = (BITMAPINFO*) calloc(1,sizeof(BITMAPINFO) + 2 * sizeof(RGBQUAD));
bmi1->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi1->bmiHeader.biPlanes = 1;
bmi1->bmiHeader.biBitCount = 1;
bmi1->bmiHeader.biCompression = BI_RGB;
for(i=0;i<2;i++)
{
bmi1->bmiColors[i].rgbRed = logPal->palPalEntry[i].peRed;
bmi1->bmiColors[i].rgbGreen = logPal->palPalEntry[i].peGreen;
bmi1->bmiColors[i].rgbBlue = logPal->palPalEntry[i].peBlue;
}
/* 4 bit color depth */
bmi4 = (BITMAPINFO*) calloc(1,sizeof(BITMAPINFO) + 16 * sizeof(RGBQUAD));
bmi4->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi4->bmiHeader.biPlanes = 1;
bmi4->bmiHeader.biBitCount = 4;
bmi4->bmiHeader.biCompression = BI_RGB;
for(i=0;i<16;i++)
{
bmi4->bmiColors[i].rgbRed = logPal->palPalEntry[i].peRed;
bmi4->bmiColors[i].rgbGreen = logPal->palPalEntry[i].peGreen;
bmi4->bmiColors[i].rgbBlue = logPal->palPalEntry[i].peBlue;
}
/* 8 bit color depth */
bmi8 = (BITMAPINFO*) calloc(1,sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD));
bmi8->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi8->bmiHeader.biPlanes = 1;
bmi8->bmiHeader.biBitCount = 8;
bmi8->bmiHeader.biCompression = BI_RGB;
for(i=0;i<256;i++)
{
bmi8->bmiColors[i].rgbRed = logPal->palPalEntry[i].peRed;
bmi8->bmiColors[i].rgbGreen = logPal->palPalEntry[i].peGreen;
bmi8->bmiColors[i].rgbBlue = logPal->palPalEntry[i].peBlue;
}
/* 16 bit color depth */
bmi16 = (BITMAPINFO*) calloc(1,sizeof(BITMAPINFO) + 4 * sizeof(DWORD));
bmi16->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi16->bmiHeader.biPlanes = 1;
bmi16->bmiHeader.biBitCount = 16;
bmi16->bmiHeader.biCompression = BI_BITFIELDS;
((DWORD*) bmi16->bmiColors)[0] = (31 << 10); /* red mask */
((DWORD*) bmi16->bmiColors)[1] = (31 << 5); /* green mask */
((DWORD*) bmi16->bmiColors)[2] = (31 << 0); /* blue mask */
/* 32 bit color depth */
bmi32 = (BITMAPINFO*) calloc(1,sizeof(BITMAPINFO) + 4 * sizeof(DWORD));
bmi32->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi32->bmiHeader.biPlanes = 1;
bmi32->bmiHeader.biBitCount = 32;
bmi32->bmiHeader.biCompression = BI_BITFIELDS;
((DWORD*) bmi32->bmiColors)[0] = 0x00FF0000; /* red mask */
((DWORD*) bmi32->bmiColors)[1] = 0x0000FF00; /* green mask */
((DWORD*) bmi32->bmiColors)[2] = 0x000000FF; /* blue mask */
}
#endif /* NO_STANDARD_COLORS */
/****************************************************************************/
/* Window Setup */
/****************************************************************************/
/* SetWindowTitle(): Set the main window title */
void SetWindowTitle() {
char titleString[MAX_PATH+20];
WCHAR wideTitle[MAX_PATH+20];
if(!IsWindow(stWindow)) return;
if(*windowTitle) sprintf(titleString, "%s", windowTitle);
else sprintf(titleString,"%s! (%s)", VM_NAME, imageName);
MultiByteToWideChar(CP_UTF8, 0, titleString, -1, wideTitle, MAX_PATH+20);
SetWindowTextW(stWindow, wideTitle);
}
char *ioGetWindowLabel(void) {
return windowTitle;
}
sqInt ioSetWindowLabelOfSize(void* lblIndex, sqInt sz) {
if(sz > MAX_PATH) sz = MAX_PATH;
memcpy(windowTitle, (void*)lblIndex, sz);
windowTitle[sz] = 0;
SetWindowTitle();
return 1;
}
sqInt ioGetWindowWidth(void) {
RECT r;
if(!IsWindow(stWindow)) return -1;
r.left = r.right = r.top = r.bottom = 0;
GetWindowRect(stWindow, &r);
return r.right - r.left;
}
sqInt ioGetWindowHeight(void) {
RECT r;
if(!IsWindow(stWindow)) return -1;
r.left = r.right = r.top = r.bottom = 0;
GetWindowRect(stWindow, &r);
return r.bottom - r.top;
}
sqInt ioSetWindowWidthHeight(sqInt w, sqInt h) {
RECT workArea, workArea2, old, shifted;
HMONITOR hMonitor;
MONITORINFO mi;
int left, top, width, height, maxWidth, maxHeight;
if(!IsWindow(stWindow)) return 0;
width = w;
height = h;
/* minimum size is 64 x 64 */
width = ( width > 64) ? width : 64;
height = (height > 64) ? height : 64;
GetWindowRect(stWindow, &old);
GetWindowRect(stWindow, &shifted);
/* Work area of screen containing current window */
hMonitor = MonitorFromWindow (stWindow, MONITOR_DEFAULTTONEAREST);
mi.cbSize = (sizeof(mi));
GetMonitorInfo(hMonitor, &mi);
workArea = mi.rcWork;
/* Work area of screen containing target rectangle. */
shifted.bottom = (shifted.top + h);
shifted.right = (shifted.left + w);
hMonitor = MonitorFromRect (&shifted, MONITOR_DEFAULTTONEAREST);
mi.cbSize = (sizeof(mi));
GetMonitorInfo(hMonitor, &mi);
workArea2 = mi.rcWork;
/* If the second choice holds the target, use it, else use the more reliable window work area */
if ((workArea2.top <= shifted.top) && (workArea2.bottom >= shifted.bottom)
&& (workArea2.left <= shifted.left) && (workArea2.right >= shifted.right)) {
workArea = workArea2;
}
/* maximum size is working area */
maxWidth = workArea.right - workArea.left;
maxHeight = workArea.bottom - workArea.top;
width = (width <= maxWidth) ? width : maxWidth;
height = (height <= maxHeight) ? height : maxHeight;
/* We may have to center the window to fit on screen,
although if there is room, we retain the window's previous position. */
if (fBrowserMode) {
left = 0;
top = 0;
} else if ((old.left >= workArea.left) && (old.top >= workArea.top) &&
(old.left + width < workArea.right) && (old.top + height < workArea.bottom)) {
left = old.left;
top = old.top;
} else {
left = (workArea.left) + ((maxWidth-width) / 2);
top = (workArea.top) + ((maxHeight-height) / 2);
}
if ((old.left != left) || (old.top != top) ||
(old.left - old.right != width) || (old.bottom - old.top != height)) {
SetWindowPos(stWindow, NULL, left, top, width, height, SWP_NOZORDER);
}
return 1;
}
void* ioGetWindowHandle(void)
{
return stWindow;
}
sqInt ioIsWindowObscured(void) {
HWND hwnd;
RECT baseRect, hwndRect;
if(!IsWindow(stWindow)) return 1; /* not even a window */
if(IsIconic(stWindow)) return 1; /* minimized */
/* Check whether the window extends beyond the screen */
GetClientRect(stWindow, &baseRect);
MapWindowPoints(stWindow, NULL, (LPPOINT)(&baseRect), 2);
hwnd = GetDesktopWindow();
GetWindowRect(hwnd, &hwndRect);
if(baseRect.left < hwndRect.left ||
baseRect.right > hwndRect.right ||
baseRect.top < hwndRect.top ||
baseRect.bottom > hwndRect.bottom) return 1; /* too big */
/* Check whether any windows in front of this window overlap */
hwnd = stWindow;
while ((hwnd = GetNextWindow(hwnd, GW_HWNDPREV))) {
if(!IsWindowVisible(hwnd)) continue; /* skip invisible windows */
GetWindowRect(hwnd, &hwndRect);
if(!(hwndRect.left >= baseRect.right ||
baseRect.left >= hwndRect.right ||
hwndRect.top >= baseRect.bottom ||
baseRect.top >= hwndRect.bottom)) return 1; /* obscured */
}
return false; /* not obscured */
}
void SetupWindows()
{ WNDCLASS wc;
/* create our update region */
updateRgn = CreateRectRgn(0,0,1,1);
/* No windows at all when running as NT service */
if(fRunService && !fWindows95) return;
wc.style = CS_OWNDC; /* don't waste resources ;-) */
wc.lpfnWndProc = (WNDPROC)MainWndProcA;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(2));
wc.hCursor = NULL;
wc.hbrBackground = GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = windowClassName;
RegisterClass(&wc);
if(!browserWindow)
stWindow = CreateWindowEx(WS_EX_APPWINDOW /* | WS_EX_OVERLAPPEDWINDOW */,
windowClassName,
TEXT(VM_NAME) TEXT("!"),
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
0,
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
else {
/* Setup a browser window. */
fBrowserMode = 1;
stWindow = CreateWindowEx(0,
windowClassName,
TEXT(VM_NAME) TEXT("!"),
WS_CHILD | WS_CLIPCHILDREN,
0,
0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
browserWindow,
NULL,
hInstance,
NULL);
}
/* Force Unicode WM_CHAR */
SetWindowLongPtrW(stWindow,GWLP_WNDPROC,(LONG_PTR)MainWndProcW);
#ifndef NO_WHEEL_MOUSE
g_WM_MOUSEWHEEL = RegisterWindowMessage( TEXT("MSWHEEL_ROLLMSG") ); /* RvL 1999-04-19 00:23 */
#endif
#if defined(_WIN32_WCE)
/* WinCE does not support RegisterClassEx(), so we must set
the small icon after creating the window. */
SendMessage(stWindow,WM_SETICON, FALSE,
(LPARAM)LoadImage(hInstance, MAKEINTRESOURCE(1),
IMAGE_ICON, 16, 16, 0));
consoleWindow = NULL; /* We do not use console under WinCE */
#else /* defined(_WIN32_WCE) */
consoleWindow = CreateWindowEx(0,
TEXT("EDIT"),
TEXT(""),
WS_CHILD | WS_BORDER |
WS_HSCROLL | WS_VSCROLL | ES_MULTILINE,
0,
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
stWindow,
NULL,
hInstance,
NULL);
#endif /* defined(_WIN32_WCE) */
/* Modify the system menu for any VM options */
CreatePrefsMenu();
SetWindowTitle();
SetForegroundWindow(stWindow);
#ifndef NO_DROP
/* drag and drop needs to be set up on per-window basis */
SetupDragAndDrop();
#endif
#ifndef NO_DIRECTINPUT
/* direct input needs to be set up on per-window basis */