/
kguipc.cpp
1436 lines (1236 loc) · 35.5 KB
/
kguipc.cpp
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
// kguiapp.cpp : Defines the entry point for the application.
//
#include <sys/types.h>
#include <sys/stat.h>
#include <afxdlgs.h>
#include <winspool.h>
/* these are needed to copy/paste to the clipboard */
#include <afxole.h>
#include <afxadv.h>
#include <winuser.h>
#include <winsock.h>
#include <direct.h>
#include <errno.h>
#include <io.h>
#include <sys/utime.h>
#include <shlwapi.h>
/* used for get desktop name function */
#include <shlobj.h>
#define DEBUGPRINT 0
#define MAX_LOADSTRING 100
#define FRAMES_PER_SECOND 60
#ifndef GET_X_LPARAM
#define GET_X_LPARAM(lParam) ((int)(short)LOWORD(lParam))
#endif
#ifndef GET_Y_LPARAM
#define GET_Y_LPARAM(lParam) ((int)(short)HIWORD(lParam))
#endif
#define WM_MOUSEWHEEL 0x020A
/* these are in the users program */
#ifdef COMMANDLINE
int AppMain(int argc, char *argv[]);
#else
void AppInit(void);
void AppClose(void);
#endif
class kGUIPrintJobWin : public kGUIPrintJob
{
public:
kGUIPrintJobWin() {m_error=false;}
~kGUIPrintJobWin() {}
bool Start(kGUIPrinter *p,const char *jobname,int numpages,int numcopies,double ppi,double lm,double rm,double tm,double bm,kGUIDrawSurface *surface,bool landscape);
void StartPage(void);
void PrintSurface(kGUIDrawSurface *surface);
void DrawRect(int lx,int rx,int ty,int by,kGUIColor color);
void DrawTextList(int pointsize,const char *fontname,int num, Array<PRINTFONTCHAR_DEF> *list);
void DrawImage(int lx,int rx,int ty,int by);
void EndPage(void);
bool End(void);
private:
HDC m_printerHDC;
CDC m_pdc;
DOCINFO m_pdi;
};
/* windows version of system */
class kGUISystemWin : public kGUISystem
{
public:
void Init(HINSTANCE hInstance,int nCmdShow);
void Draw(HWND hWnd,int ulx,int urx,int uty,int uby);
void FileShow(const char *filename);
void ShellExec(const char *program,const char *parms,const char *dir);
void Copy(kGUIString *s);
void Paste(kGUIString *s);
void Sleep(int ms);
bool MakeDirectory(const char *name);
bool IsDir(const char *fn);
long FileTime(const char *fn);
void HideWindow(void);
void ShowWindow(void);
void ReDraw(void);
void ChangeMouse(void);
void ShowMouse(bool show);
void GetWindowPos(int *x,int *y,int *w,int *h);
void SetWindowPos(int x,int y);
void SetWindowSize(int w,int h);
void Minimize(void);
void AdjustMouse(int dx,int dy);
bool NeedRotatedSurfaceForPrinting(void) {return true;}
unsigned int GetNumPrinters(void) {return m_numprinters;}
void GetPrinterInfo(const char *name,int *pw,int *ph,int *ppih,int *ppiv);
int GetDefPrinterNum(void) {return m_defprinternum;}
kGUIPrinter *GetPrinterObj(int pid) {return m_printers.GetEntryPtr(pid);}
int LocatePrinter(const char *name,bool ordefault);
class kGUIPrintJob *AllocPrintJob(void) {kGUIPrintJob *pj;pj=new kGUIPrintJobWin(); return pj;}
LRESULT Event(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
bool CreateHDC(const char *printerName,int numcopies,HDC *hdc);
private:
void GetPrinters(void);
int m_mousex;
int m_mousey;
int m_mousewheeldelta;
int m_waskilled;
bool m_showwindow:1;
bool m_mouseleftbutton:1;
bool m_mouserightbutton:1;
int m_lasttickcount;
HWND m_hWnd;
WSADATA m_SockData;
HINSTANCE m_hInst; // current instance
TCHAR m_szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR m_szWindowClass[MAX_LOADSTRING]; // the main window class name
RECT m_WinRect;
RECT m_UpdateWinRect;
/* printer info */
unsigned int m_numprinters;
unsigned int m_defprinternum;
ClassArray<kGUIPrinter>m_printers;
};
kGUISystemWin *g_sys;
#ifdef COMMANDLINE
int main(int argc, char *argv[])
{
int rc;
g_sys=new kGUISystemWin();
kGUI::Init(g_sys,0,0,0,0,100);
rc=AppMain(argc,argv);
kGUI::Close();
delete g_sys;
return(rc);
}
#else
//since this must be a static function callback, we just pass it along to the class event handler
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
return(g_sys->Event(hWnd,message,wParam,lParam));
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
g_sys=new kGUISystemWin();
g_sys->Init(hInstance,nCmdShow);
delete g_sys;
return 0;
}
void kGUISystemWin::Init(HINSTANCE hInstance,int nCmdShow)
{
int startwidth,startheight;
int fullwidth,fullheight,maximages;
MSG msg;
m_showwindow=false;
m_mousex=0;
m_mousey=0;
m_mousewheeldelta=0;
m_waskilled=false;
m_mouseleftbutton=false;
m_mouserightbutton=false;
GetPrinters();
// Initialize global strings
kGUI::Trace("------------------------------------------------------------------------------\n");
#if 0
strcpy(m_szTitle,APPNAME);
strcpy(m_szWindowClass,APPNAME);
#else
LoadString(hInstance, IDS_APP_TITLE, m_szTitle, MAX_LOADSTRING);
kGUI::Trace(" LoadString(hInstance, IDS_APP_TITLE, m_szTitle, MAX_LOADSTRING);\n");
LoadString(hInstance, IDC_APP_NAME, m_szWindowClass, MAX_LOADSTRING);
kGUI::Trace(" LoadString(hInstance, IDC_APP_NAME, m_szWindowClass, MAX_LOADSTRING);\n");
#endif
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
#if defined(IDI_LARGEICON)
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_LARGEICON);
#else
wcex.hIcon=0;
#endif
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = 0;
wcex.lpszClassName = m_szWindowClass;
#if defined(IDI_SMALLICON)
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALLICON);
#else
wcex.hIconSm=0;
#endif
kGUI::Trace(" RegisterClass(&wcex);\n");
RegisterClassEx(&wcex);
}
/* get the full screen size */
fullwidth=GetSystemMetrics(SM_CXSCREEN);
fullheight=GetSystemMetrics(SM_CYSCREEN);
/* this is the size of that app window, use fullscreen */
/* unless overridden by the user requested dimensions */
#ifdef DEFSCREENWIDTH
startwidth=DEFSCREENWIDTH;
#else
startwidth=fullwidth;
#endif
#ifdef DEFSCREENHEIGHT
startheight=DEFSCREENHEIGHT;
#else
startheight=fullheight-35;
#endif
#ifdef DEFMAXIMAGES
maximages=DEFMAXIMAGES;
#else
maximages=200;
#endif
m_lasttickcount=GetTickCount();
m_hInst = hInstance; // Store instance handle in our global variable
kGUI::Trace(" hWnd = CreateWindow(m_szWindowClass, m_szTitle, WS_POPUP | WS_VISIBLE, 0, 0, ScreenWidth, ScreenHeight, NULL, NULL, hInstance, NULL);\n");
m_hWnd = CreateWindow(m_szWindowClass, m_szTitle, WS_POPUP | WS_VISIBLE /*|WS_EX_LAYERED*/ ,
(fullwidth-startwidth)/2, (fullheight-startheight)/2, startwidth, startheight, NULL, NULL, hInstance, NULL);
if (!m_hWnd)
{
kGUI::Trace(" Last Function failed, program aborting!\n");
return;
}
// Allocate BMP data for screen
kGUI::Trace(" GetClientRect(hWnd,&m_WinRect);\n");
GetClientRect(m_hWnd,&m_WinRect);
/* initialize kGUI engine */
kGUI::Trace(" kGUI::Init(m_WinRect.right-m_WinRect.left,m_WinRect.bottom-m_WinRect.top);\n");
if(kGUI::Init(g_sys,m_WinRect.right-m_WinRect.left,m_WinRect.bottom-m_WinRect.top,fullwidth,fullheight,maximages)==false)
{
kGUI::Trace(" Last Function failed, program aborting!\n");
return;
}
// kGUI::Trace(" LoadAccelerators(hInstance, (LPCTSTR)IDC_APP_NAME);\n");
// m_hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_APP_NAME);
kGUI::Trace(" WSAStartup(0x0202,&m_SockData);\n");
if(WSAStartup(0x0202,&m_SockData))
{
kGUI::Trace(" error initing WSAStartup;\n");
return;
}
/* call the application init code */
kGUI::Trace(" AppInit();\n");
AppInit();
/* show the window if the application has not already done so */
if(m_showwindow==false)
ShowWindow();
kGUI::Trace(" UpdateWindow(m_hWnd);\n");
UpdateWindow(m_hWnd);
kGUI::Trace(" Draw(m_hWnd);\n");
Draw(m_hWnd,0,0,0,0);
kGUI::Trace(" SetTimer(m_hWnd, 0, 1000 / FRAMES_PER_SECOND, NULL);\n");
SetTimer(m_hWnd, 0, 1000 / FRAMES_PER_SECOND, NULL);
// Main message event loop:
kGUI::Trace(" Entering Main loop: GetMessage(&msg, NULL, 0, 0);\n");
while (GetMessage(&msg, NULL, 0, 0))
{
// if (!TranslateAccelerator(msg.hwnd, m_hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
kGUI::Trace(" Exited Main loop, program done.\n");
}
static int ConvertKey(int inkey,int defkey)
{
int key=defkey;
switch (inkey)
{
case VK_HOME:
key=GUIKEY_HOME;
break;
case VK_END:
key=GUIKEY_END;
break;
case VK_LEFT:
key=GUIKEY_LEFT;
break;
case VK_RIGHT:
key=GUIKEY_RIGHT;
break;
case VK_UP:
key=GUIKEY_UP;
break;
case VK_DOWN:
key=GUIKEY_DOWN;
break;
case VK_PRIOR:
key=GUIKEY_PGUP;
break;
case VK_NEXT:
key=GUIKEY_PGDOWN;
break;
case VK_DELETE:
key=GUIKEY_DELETE;
break;
case VK_INSERT:
key=GUIKEY_INSERT;
break;
case VK_F1:
key=GUIKEY_F1;
break;
case VK_F2:
key=GUIKEY_F2;
break;
case VK_F3:
key=GUIKEY_F3;
break;
case VK_F4:
key=GUIKEY_F4;
break;
case VK_F5:
key=GUIKEY_F5;
break;
case VK_F6:
key=GUIKEY_F6;
break;
case VK_F7:
key=GUIKEY_F7;
break;
case VK_F8:
key=GUIKEY_F8;
break;
case VK_F9:
key=GUIKEY_F9;
break;
case VK_F10:
key=GUIKEY_F10;
break;
case VK_F11:
key=GUIKEY_F11;
break;
case VK_F12:
key=GUIKEY_F12;
break;
case 187: //'ctrl +'
key=GUIKEY_CTRL_PLUS;
break;
case 189: //'ctrl -'
key=GUIKEY_CTRL_MINUS;
break;
}
return(key);
}
//
// FUNCTION: Event(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT kGUISystemWin::Event(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
int key;
int newtick,deltatick;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
case WM_PAINT:
// Get the Windows update region
// while(kGUI::GetInDraw()==true || kGUI::GetInThread()==true);
// kGUI::SetInDraw(true);
GetUpdateRect(hWnd, &m_UpdateWinRect, FALSE);
hdc = BeginPaint(hWnd, &ps);
Draw(hWnd,m_UpdateWinRect.left,m_UpdateWinRect.right,m_UpdateWinRect.top,m_UpdateWinRect.bottom);
EndPaint(hWnd, &ps);
// kGUI::SetInDraw(false);
break;
case WM_TIMER:
newtick=GetTickCount();
deltatick=newtick-m_lasttickcount;
if(deltatick<0)
deltatick=0;
m_lasttickcount=newtick;
#if DEBUGPRINT
kGUI::Trace("Delta tick=%d\n",deltatick);
#endif
kGUI::Tick(deltatick);
if(GetAsyncKeyState(VK_LSHIFT) || GetAsyncKeyState(VK_RSHIFT))
kGUI::SetKeyShift(true);
else
kGUI::SetKeyShift(false);
if(GetAsyncKeyState(VK_CONTROL))
kGUI::SetKeyControl(true);
else
kGUI::SetKeyControl(false);
if(kGUI::GetTempMouse()>=0)
{
int t=kGUI::GetTempMouse();
kGUI::SetTempMouse(t-1);
if(!t)
kGUI::SetMouseCursor(MOUSECURSOR_DEFAULT);
}
if(kGUI::GetMouseCursor()!=MOUSECURSOR_DEFAULT) /* if not default arrow */
kGUI::ChangeMouse();
kGUI::UpdateInput();
m_mousewheeldelta=0;
// while(kGUI::GetInDraw()==true || kGUI::GetInThread()==true);
// kGUI::SetInDraw(true);
Draw(hWnd,0,0,0,0);
// kGUI::SetInDraw(false);
/* is the application shutting down? */
if(kGUI::IsAppClosed()==true)
DestroyWindow(hWnd);
else
SetTimer(hWnd, 0, 1000 / FRAMES_PER_SECOND, NULL);
break;
case WM_DESTROY:
/* sometimes this event get's called more than once, so allow only once! */
if(m_waskilled==false)
{
m_waskilled=true;
AppClose();
kGUI::Close();
PostQuitMessage(0);
WSACleanup();
}
break;
case WM_MOUSEWHEEL:
//values are increments of 120
m_mousewheeldelta = GET_WHEEL_DELTA_WPARAM(wParam)/120;
kGUI::SetMouse(m_mousex,m_mousey,m_mousewheeldelta,m_mouseleftbutton,m_mouserightbutton);
break;
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
case WM_MOUSEMOVE:
if(kGUI::GetMouseCursor()!=MOUSECURSOR_DEFAULT) /* if not default arrow */
kGUI::ChangeMouse();
m_mousex=GET_X_LPARAM(lParam);
m_mousey=GET_Y_LPARAM(lParam);
m_mouseleftbutton=(wParam&MK_LBUTTON)!=0;
m_mouserightbutton=(wParam&MK_RBUTTON)!=0;
/* this tells windows to keep updating me even if the mouse is moved off of */
/* my window area */
if(m_mouseleftbutton || m_mouserightbutton)
SetCapture(hWnd);
else
ReleaseCapture();
kGUI::SetMouse(m_mousex,m_mousey,m_mousewheeldelta,m_mouseleftbutton,m_mouserightbutton);
kGUI::Tick(0);
kGUI::UpdateInput();
return DefWindowProc(hWnd, message, wParam, lParam);
break;
case WM_CHAR:
key=(int)wParam;
switch(key)
{
case 1: //'ctrl-a'
key=GUIKEY_SELECTALL;
break;
case 3: //'ctrl-c'
key=GUIKEY_COPY;
break;
case 6: //'ctrl-f'
key=GUIKEY_FIND;
break;
// case 8: //'ctrl-h'
// key=GUIKEY_REPLACE;
// break;
case 22: //'ctrl-v'
key=GUIKEY_PASTE;
break;
case 24: //'ctrl-x'
key=GUIKEY_CUT;
break;
case 26: //'ctrl-z'
key=GUIKEY_UNDO;
break;
case 187: //'ctrl +'
key=GUIKEY_CTRL_PLUS;
break;
case 189: //'ctrl -'
key=GUIKEY_CTRL_MINUS;
break;
case VK_BACK:
key=GUIKEY_BACKSPACE;
break;
case VK_RETURN:
key=GUIKEY_RETURN;
break;
case VK_TAB:
if(GetAsyncKeyState(VK_LSHIFT) || GetAsyncKeyState(VK_RSHIFT))
key=GUIKEY_SHIFTTAB;
else
key=GUIKEY_TAB;
break;
case VK_ESCAPE:
key=GUIKEY_ESC;
break;
case VK_SHIFT:
case VK_CONTROL:
key=0;
break;
}
if(key)
kGUI::KeyPressed(key);
break;
case WM_KEYDOWN:
key=ConvertKey((int)wParam,0);
if(key)
kGUI::KeyPressed(key);
key=ConvertKey((int)wParam,(int)wParam);
if(key)
kGUI::SetKeyState(key,true);
break;
case WM_KEYUP:
key=ConvertKey((int)wParam,(int)wParam);
if(key)
kGUI::SetKeyState(key,false);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
void kGUISystemWin::Draw(HWND hWnd,int ulx,int urx,int uty,int uby)
{
int sw,sh;
int dw,dh;
int nl;
kGUIColor *bmp_data;
kGUICorners c;
BITMAPINFO bmp;
/* returns pointer to bitmap and c contains 4 corners of updated area */
c.lx=ulx;
c.rx=urx;
c.ty=uty;
c.by=uby;
bmp_data=kGUI::Draw(&c);
if(!bmp_data)
return;
dw=c.rx-c.lx; /* width of updated area */
dh=c.by-c.ty; /* height of updated area */
if(!dw || !dh)
return; /* nothing on screen was updated */
/* hmm, had this assert trigger when the screen size was made bigger while the program was running */
/* perhaps change this to a clip on rx and by if they are too large? */
sw=kGUI::GetSurfaceWidth();
sh=kGUI::GetSurfaceHeight();
if(c.lx<0 || c.ty<0 || c.rx>sw || c.by>sh)
assert(false,"Error!");
bmp.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmp.bmiHeader.biWidth = sw;
bmp.bmiHeader.biHeight = -sh;
bmp.bmiHeader.biPlanes = 1;
bmp.bmiHeader.biBitCount = WINBPP;
bmp.bmiHeader.biCompression = BI_RGB;
bmp.bmiHeader.biSizeImage = 0;
bmp.bmiHeader.biXPelsPerMeter = 0;
bmp.bmiHeader.biYPelsPerMeter = 0;
bmp.bmiHeader.biClrUsed = 0;
bmp.bmiHeader.biClrImportant = 0;
// if(kGUI::GetTrace())
// kGUI::Trace("PCDraw(%d,%d,%d,%d\n",c.lx,c.lx+dw,c.ty,c.ty+dh);
HDC hDC = GetDC(hWnd);
nl=SetDIBitsToDevice(hDC,
c.lx, c.ty, // upper left of destination
dw, dh, // source rectangle size
c.lx, 0, // lower left of source
0, dh, // first scan line, number of scan lines in array
bmp_data+(c.ty*sw), &bmp, DIB_RGB_COLORS);
#if 0
if(nl!=dh)
{
/* not drawn!, not sure why */
DWORD dw = GetLastError();
if(dw) /* 0=ok, must have draw area off screen since moving around */
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
passert(false,"%s",lpMsgBuf);
LocalFree(lpMsgBuf);
}
}
#endif
ReleaseDC(hWnd, hDC);
}
#endif
/**************************************************************************/
void kGUISystemWin::FileShow(const char *filename)
{
ShellExecute(NULL,"open",filename,NULL,NULL,SW_SHOWNORMAL);
}
void kGUISystemWin::ShellExec(const char *program,const char *parms,const char *dir)
{
SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = program;
ShExecInfo.lpParameters = parms;
ShExecInfo.lpDirectory = dir;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
}
/* true=sucess, false=error */
bool kGUISystemWin::MakeDirectory(const char *name)
{
if(_mkdir(name)==0)
return(true);
return(false);
}
/* paste string from the system clipboard */
void kGUISystemWin::Paste(kGUIString *s)
{
COleDataObject obj;
if (obj.AttachClipboard())
{
#if 0
if (obj.IsDataAvailable(CF_UNICODETEXT))
{
HGLOBAL hmem = obj.GetGlobalData(CF_UNICODETEXT);
CMemFile sf((BYTE*) ::GlobalLock(hmem),(UINT) ::GlobalSize(hmem));
CString buffer;
LPSTR str = buffer.GetBufferSetLength((int)::GlobalSize(hmem));
sf.Read(str,(UINT) ::GlobalSize(hmem));
::GlobalUnlock(hmem);
s->SetEncoding(ENCODING_UTF8);
s->SetString(buffer);
}
else if (obj.IsDataAvailable(CF_TEXT))
#else
if (obj.IsDataAvailable(CF_TEXT))
#endif
{
HGLOBAL hmem = obj.GetGlobalData(CF_TEXT);
CMemFile sf((BYTE*) ::GlobalLock(hmem),(UINT) ::GlobalSize(hmem));
CString buffer;
LPSTR str = buffer.GetBufferSetLength((int)::GlobalSize(hmem));
sf.Read(str,(UINT) ::GlobalSize(hmem));
::GlobalUnlock(hmem);
// s->SetEncoding(ENCODING_UTF8);
s->SetEncoding(ENCODING_8BIT);
s->SetString(buffer);
}
}
}
/* copy string from system clipboard */
void kGUISystemWin::Copy(kGUIString *s)
{
HGLOBAL hglbCopy;
LPTSTR lptstrCopy;
kGUIString u;
u.SetString(s);
u.SetEncoding(ENCODING_UTF8);
if (!OpenClipboard(m_hWnd))
return;
EmptyClipboard();
hglbCopy = GlobalAlloc(GMEM_MOVEABLE,u.GetLen()+1);
if (hglbCopy == NULL)
{
CloseClipboard();
return;
}
// Lock the handle and copy the text to the buffer.
lptstrCopy = (LPTSTR)GlobalLock(hglbCopy);
memcpy(lptstrCopy, s->GetString(),u.GetLen()+1);
GlobalUnlock(hglbCopy);
// Place the handle on the clipboard.
SetClipboardData(CF_TEXT, hglbCopy);
CloseClipboard();
}
/* get filetime */
long kGUISystemWin::FileTime(const char *fn)
{
struct __stat64 buf;
int result;
result = _stat64( fn, &buf );
/* Check if statistics are valid: */
if( result != 0 )
return(0);
else
return((long)buf.st_mtime);
}
/* force redraw of whole screen, mainly used for 'busy' bar when program */
/* has not returned control to the system */
void kGUISystemWin::ReDraw(void)
{
if(m_showwindow==false)
ShowWindow();
RedrawWindow(m_hWnd,NULL,NULL,RDW_INTERNALPAINT|RDW_UPDATENOW);
}
/* change mouse pointer shape to current shape type */
void kGUISystemWin::ChangeMouse(void)
{
static LPSTR mousecursors[]={
IDC_ARROW, /* default */
IDC_WAIT, /* busy */
IDC_SIZEWE, /* adjust left/right */
IDC_SIZENS, /* adjust up/down */
IDC_SIZENWSE, /* adjust size */
IDC_HAND}; /* hand for links on html page */
//IDC_CROSS Cross-hair cursor for selection
//IDC_UPARROW Arrow that points straight up
//IDC_SIZEALL A four-pointed arrow. The cursor to use to resize a window.
//IDC_SIZENWSE Two-headed arrow with ends at upper left and lower right
//IDC_SIZENESW Two-headed arrow with ends at upper right and lower left
//IDC_SIZEWE Horizontal two-headed arrow
//IDC_SIZENS Vertical two-headed arrow
SetCursor(AfxGetApp()->LoadStandardCursor(mousecursors[kGUI::GetMouseCursor()]));
}
void kGUISystemWin::ShowMouse(bool show)
{
ShowCursor(show);
}
void kGUISystemWin::GetWindowPos(int *x,int *y,int *w,int *h)
{
WINDOWPLACEMENT wp;
wp.length = sizeof wp;
if ( GetWindowPlacement(m_hWnd,&wp) )
{
x[0]=wp.rcNormalPosition.left;
y[0]=wp.rcNormalPosition.top;
w[0]=wp.rcNormalPosition.right-wp.rcNormalPosition.left;
h[0]=wp.rcNormalPosition.bottom-wp.rcNormalPosition.top;
}
}
void kGUISystemWin::AdjustMouse(int dx,int dy)
{
m_mousex+=dx;
m_mousey+=dy;
}
void kGUISystemWin::SetWindowPos(int x,int y)
{
WINDOWPLACEMENT wp;
int w,h;
wp.length = sizeof wp;
if ( GetWindowPlacement(m_hWnd,&wp) )
{
w=wp.rcNormalPosition.right-wp.rcNormalPosition.left;
h=wp.rcNormalPosition.bottom-wp.rcNormalPosition.top;
/* clip */
if((x+w)<25)
x=25-w;
else if(x>(GetSystemMetrics(SM_CXSCREEN)-25))
x=GetSystemMetrics(SM_CXSCREEN)-25;
if(y<0)
y=0;
else if(y>(GetSystemMetrics(SM_CYSCREEN)-25))
y=GetSystemMetrics(SM_CYSCREEN)-25;
wp.rcNormalPosition.left=x;
wp.rcNormalPosition.top=y;
wp.rcNormalPosition.right=x+w;
wp.rcNormalPosition.bottom=y+h;
SetWindowPlacement(m_hWnd,&wp);
}
}
void kGUISystemWin::SetWindowSize(int w,int h)
{
WINDOWPLACEMENT wp;
wp.length = sizeof wp;
if ( GetWindowPlacement(m_hWnd,&wp) )
{
wp.rcNormalPosition.right=wp.rcNormalPosition.left+w;
wp.rcNormalPosition.bottom=wp.rcNormalPosition.top+h;
SetWindowPlacement(m_hWnd,&wp);
}
}
void kGUISystemWin::ShowWindow(void)
{
m_showwindow=true;
::ShowWindow(m_hWnd,SW_SHOW);
}
void kGUISystemWin::HideWindow(void)
{
m_showwindow=false;
::ShowWindow(m_hWnd,SW_HIDE);
}
void kGUISystemWin::Minimize(void)
{
::ShowWindow(m_hWnd,SW_MINIMIZE);
}
void kGUISystemWin::Sleep(int ms)
{
::Sleep(ms);
}
/* is this a directory? */
bool kGUISystemWin::IsDir(const char *fn)
{
bool isdir;
intptr_t dir_handle;
struct _finddata_t dir_info;
dir_handle = _findfirst(fn, &dir_info);
if(dir_handle<0)
return(false); /* path must be unmounted or something is wrong! */
if(dir_info.attrib&16)
isdir=true;
else
isdir=false;
_findclose(dir_handle);
return(isdir);
}
/**********************************************************************/
/* get number of printers, default printer number and printer names */
void kGUISystemWin::GetPrinters(void)
{
DWORD numprinters;
DWORD defprinter=0;
DWORD dwSizeNeeded=0;
DWORD dwItem;
LPPRINTER_INFO_2 printerinfo = NULL;
// Get buffer size
kGUI::Trace("EnumPrinters ( PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS , NULL, 2, NULL, 0, &dwSizeNeeded, &numprinters );\n");
EnumPrinters ( PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS , NULL, 2, NULL, 0, &dwSizeNeeded, &numprinters );
// allocate memory
//printerinfo = (LPPRINTER_INFO_2)HeapAlloc ( GetProcessHeap (), HEAP_ZERO_MEMORY, dwSizeNeeded );
kGUI::Trace("printerinfo = (LPPRINTER_INFO_2)new char[%d];\n",dwSizeNeeded);
printerinfo = (LPPRINTER_INFO_2)new char[dwSizeNeeded];
kGUI::Trace("EnumPrinters ( PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS,NULL,2,(LPBYTE)printerinfo,dwSizeNeeded,&dwSizeNeeded,&numprinters);\n");
if ( EnumPrinters ( PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, // what to enumerate
NULL, // printer name (NULL for all)
2, // level
(LPBYTE)printerinfo, // buffer
dwSizeNeeded, // size of buffer
&dwSizeNeeded, // returns size
&numprinters // return num. items
) == 0 )
{
kGUI::Trace("No printers found!;\n");
numprinters=0;
}
{
DWORD size=0; /* if no default printer is set then GetDefaultPrinter will leave this unitlzd */
// TCHAR* buffer=0;
// Get the size of the default printer name.
kGUI::Trace("GetDefaultPrinter(NULL, &size);\n");
GetDefaultPrinter(NULL, &size);
kGUI::Trace("done GetDefaultPrinter size=%d;\n",size);
if(size)
{
// Allocate a buffer large enough to hold the printer name.
kGUI::Trace("TCHAR* buffer = new TCHAR[%d];\n",size);
TCHAR* buffer = new TCHAR[size];
// Get the printer name.
kGUI::Trace("GetDefaultPrinter(buffer, &size);\n");
GetDefaultPrinter(buffer, &size);
for ( dwItem = 0; dwItem < numprinters; dwItem++ )
{
kGUI::Trace("printername[%d]='%s'\n",dwItem,printerinfo[dwItem].pPrinterName);
if(!strcmp(buffer,printerinfo[dwItem].pPrinterName))
defprinter=dwItem;
}
kGUI::Trace("delete buffer;\n");
delete buffer;
}
}
kGUI::Trace("copying printer names numprinters=%d,defaultprinter=%d;\n",numprinters,defprinter);
/* copy printer info */
m_numprinters=numprinters;