File: vapp.cpp

package info (click to toggle)
libv1.22 1.22-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,380 kB
  • ctags: 9,765
  • sloc: cpp: 58,097; ansic: 6,814; makefile: 1,597; java: 515; sh: 24
file content (1245 lines) | stat: -rw-r--r-- 38,160 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//                                                     VV         VV       //
//  VV       VV     V - A Portable C++ GUI Framework    VV       VV        //
//   VV     VV           designed and written by         VV     VV         //
//    VV   VV                                             VV   VV          //
//     VV VV              Bruce E. Wampler, Ph.D.          VV VV           //
//      VVV               e-mail: bruce@objectcentral.com   VVV            //
//       V                                                   V             //
//                                                                         //
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//                                                                         //
// vapp.cxx - The vApp control object (For Windows)                        //
//                                                                         //
// Copyright (C) 1995, 1996, 1997, 1998  Bruce E. Wampler                  //
//                                                                         //
// This file is part of the V C++ GUI Framework.                           //
//                                                                         //
// This library is free software; you can redistribute it and/or           //
// modify it under the terms of the GNU Library General Public             //
// License as published by the Free Software Foundation; either            //
// version 2 of the License, or (at your option) any later version.        //
//                                                                         //
// This library is distributed in the hope that it will be useful,         //
// but WITHOUT ANY WARRANTY; without even the implied warranty of          //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       //
// Library General Public License for more details.                        //
//                                                                         //
// You should have received a copy of the GNU Library General Public       //
// License along with this library (see COPYING.LIB); if not, write to the //
// Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //
//                                                                         //
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
#include <v/vwin32.h>		// for Win 32 stuff
#include <v/vapp.h>		// my header file
#include <v/vwindow.h>		// Win header
#include <v/vfont.h>		// for font stuff
#include <v/vcmdwin.h>
#include <v/vcmdpane.h>
#include <v/vthislst.h>

#include <stdlib.h>

#ifndef VWIN16
#include <commctrl.h>		// for tooltips
#endif

    // Globals available to the world

    vApp* theGlobalApp = NULL;	// to be filled in upon instantiation
    int (*pAppMain)(int, char**);
    DebugMask DebugState;

#ifdef __MINGW32__
    extern int vFORCE_TO_INCLUDE_MODULE;	// bug in mingw32
#endif
#ifdef __CYGWIN32__
    extern int vFORCE_TO_INCLUDE_MODULE;	// bug in cygwin?
#endif

    int CMain(HANDLE hInstance, HANDLE hPrevInstance,
		      LPSTR lpszCmdLine, int nCmdShow);

    static char szMdiFrameClass [] = "MdiFrameClass";	// To give it a name
    static char szMdiWindowClass [] = "MdiWindowClass";	// To give it a name
    static int _destroyed;		// for destructor


    static char copyright[] =
      "****> Copyright (C) 1995-1998 Bruce E. Wampler; under terms of the\
 GNU Library General Public License, version 2 <****";

//========================>>> vApp::vApp <<<=======================
  vApp::vApp(VCONST char* appName, int simSDI, int frameWidth, 
	int frameHeight) : vBaseItem(appName)	// constructor
  {
    // First, set the global pointer to the main App. This happens
    // when the user declares the instance of the app, either from
    // a vApp object direct, or an object from a class derived
    // from the vApp class.

    theGlobalApp = this;		// this is our object

    // now the data members

    _curThis = 0;
    _running = 0;		// we are running
    _WindowList = 0;		// no windows registered
    _CmdPaneList = 0;		// no command panes registered
    _clipText = 0;
    _inExit = 0;
    _simSDI = simSDI;

    _frameWidth = (frameWidth > 0) ? frameWidth + 20 : 0;
    _frameHeight = (frameHeight > 0) ? frameHeight + 20 : 0;

    _DefaultHeight = frameHeight;	// default sizes for canvas window
    _DefaultWidth = frameWidth;
    _destroyed = 0;		// Not destroyed yet

    _Frame = 0;	                // Not inited yet.
    _Client = 0;		// Not inited yet.

    // Set which debug items to show

    DebugState.System = 0;			// System debug messages
    DebugState.CmdEvents = 0;			// Show command events (buttons, etc.)
    DebugState.MouseEvents = 0;		  	// Show mouse events
    DebugState.WindowEvents = 0;		// Window events (resize, etc.)
    DebugState.Build = 0;			// Define/Build window
    DebugState.BadVals= 0;			// Error values
    DebugState.Misc = 0;			// Misc stuff
    DebugState.Text = 0;			// Text events
    DebugState.Constructor = 0;			// Show constructors
    DebugState.Destructor = 0;			// Show destructors
    DebugState.User = 0;			// Debug user events
    DebugState.UserApp1 = 0;			// Level 1 User App
    DebugState.UserApp2 = 0;			// Level 2 User App
    DebugState.UserApp3 = 0;

  }

//========================>>> vApp::initialize <<<=======================
  void vApp::initialize(int& argc, char** argv,
	HANDLE hInstance, HANDLE hPrevInstance, int nCmdShow)
  {
    // Main interface to the parent windowing system

    for (int argn = 1 ; argn < argc ; ++argn)	// look for vDebug switch
      {
	if (strcmp(argv[argn],"-vDebug") == 0)
	  {
	    // Turn them all off
	    DebugState.System = 0;			// System debug messages
	    DebugState.CmdEvents = 0;			// Show command events (buttons, etc.)
	    DebugState.MouseEvents = 0;			// Show mouse events
	    DebugState.WindowEvents = 0;		// Window events (resize, etc.)
	    DebugState.Build = 0;			// Define/Build window
	    DebugState.BadVals= 0;			// Error values
	    DebugState.Misc = 0;			// Misc stuff
	    DebugState.Text = 0;			// Text events
	    DebugState.Constructor = 0;			// Show constructors
	    DebugState.Destructor = 0;			// Show destructors
	    DebugState.User = 0;			// Debug user events
	    DebugState.UserApp1 = 0;			// Level 1 User App
	    DebugState.UserApp2 = 0;			// Level 2 User App
	    DebugState.UserApp3 = 0;

	    for (char* cp = argv[argn+1] ; *cp ; ++cp)
	      {
		switch (*cp)
		  {
		    case 'S':
			DebugState.System = 1;		// System debug messages
			break;
		    case 'c':
			DebugState.CmdEvents = 1;	// Show command events (buttons, etc.)
			break;
		    case 'm':
			DebugState.MouseEvents = 1;	// Show mouse events
			break;
		    case 'w':
			DebugState.WindowEvents = 1;	// Window events (resize, etc.)
			break;
		    case 'b':
			DebugState.Build = 1;		// Define/Build window
			break;
		    case 'v':
			DebugState.BadVals= 1;		// Error values
			break;
		    case 'o':
			DebugState.Misc = 1;		// (Other) Misc stuff
			break;
		    case 't':
			DebugState.Text = 1;		// Text events
			break;
		    case 'C':
			DebugState.Constructor = 1;	// Show constructors
			break;
		    case 'D':
			DebugState.Destructor = 1;	// Show destructors
			break;
		    case 'U':
			DebugState.User = 1;		// Debug user events
			break;
		    case '1':
			DebugState.UserApp1 = 1;	// Level 1 User App
			break;
		    case '2':
			DebugState.UserApp2 = 1;	// Level 2 User App
			break;
		    case '3':
			DebugState.UserApp3 = 1;
			break;
		  }
	      }
	    
	    // Now fixup the argument list and break loop
	    int ia;
	    for (ia = argn ; ia < argc ; ++ia)
		argv[ia] = argv[ia+2];
	    argv[ia] = 0;
	    argc -= 2;	// eat the -vDebug args
	    break;
	  }

      }

    // This is pretty much the standard Windows startup code
    // required by all Windows applications.

    WNDCLASS wndclass;

    _vHandle = hInstance;

    if (!hPrevInstance)
      {
	// Register the MDI frame window class

	_appicon = ::LoadIcon((HINSTANCE)hInstance,"vAppIcon");
	if (_appicon == NULL)
	    _appicon = ::LoadIcon(NULL,IDI_APPLICATION);
	_winicon = ::LoadIcon((HINSTANCE)hInstance,"vWindowIcon");
	if (_winicon == NULL)
	    _winicon = ::LoadIcon(NULL,IDI_APPLICATION);

	wndclass.style         = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc   = (WNDPROC) PMdiFrameProc;
	wndclass.cbClsExtra    = 0;
	wndclass.cbWndExtra    = 0;
	wndclass.hInstance     = (HINSTANCE) hInstance;
	wndclass.hIcon         = _appicon;
	wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE + 1);
	wndclass.lpszMenuName  = NULL;
	wndclass.lpszClassName = szMdiFrameClass;

	RegisterClass(&wndclass);

	// Register the MDI window class

	wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
	wndclass.lpfnWndProc   = (WNDPROC) PMdiWindowProc;
	wndclass.cbClsExtra    = 0;
	wndclass.cbWndExtra    = sizeof(void*);
	wndclass.hInstance     = (HINSTANCE) hInstance;
	wndclass.hIcon         = _winicon;
	wndclass.hCursor       = NULL; //LoadCursor (NULL, IDC_ARROW);
	wndclass.hbrBackground = static_cast<HBRUSH>(::GetStockObject(WHITE_BRUSH));
	wndclass.lpszMenuName  = NULL;
	wndclass.lpszClassName = szMdiWindowClass;

	RegisterClass(&wndclass);

      }

    hFrameMenu = ::CreateMenu();		// A File menu
    hFrameSubMenu = ::CreateMenu();	// Its submenu

    // Define a very simple default MDI menu
    ::AppendMenu(hFrameSubMenu, MF_STRING, M_WindowsReserved1, "&New");
    ::AppendMenu(hFrameSubMenu, MF_STRING, M_WindowsReserved2, "&Open");
    ::AppendMenu(hFrameSubMenu, MF_SEPARATOR,0,0);
    ::AppendMenu(hFrameSubMenu, MF_STRING, M_WindowsReserved3, "E&xit");
    ::AppendMenu(hFrameMenu, MF_POPUP, (UINT)hFrameSubMenu, "&File");

    hAccel = 0;				// No accelerators
    _workTimer = 0;

    // Create the frame window

    _Frame = ::CreateWindow(szMdiFrameClass, _name,
	WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_MAXIMIZE |
	WS_VISIBLE | WS_MAXIMIZEBOX | WS_MINIMIZEBOX,
	CW_USEDEFAULT, CW_USEDEFAULT,
       //	_DefaultWidth,_DefaultHeight,
	(_frameWidth == 0) ? CW_USEDEFAULT : _frameWidth,
	(_frameHeight == 0) ? CW_USEDEFAULT : _frameHeight,
	NULL, hFrameMenu, (HINSTANCE)hInstance, NULL);


    RECT frameRect;
    ::GetWindowRect(_Frame, &frameRect);
    //::GetClientRect(_Frame, &frameRect);
    if (_DefaultHeight <= 0)
      {
        _DefaultHeight = frameRect.bottom - frameRect.top - 200;
        if (_DefaultHeight < 50)
            _DefaultHeight = 50;
      }
    if (_DefaultWidth <= 0)
      {
        _DefaultWidth = frameRect.right - frameRect.left - 50;
        if (_DefaultWidth < 50)
            _DefaultWidth = 50;
      }

    _Client = ::GetWindow(_Frame, GW_CHILD);

    // Create some pens and brushes used for drawing custom controls
    // and dialogs.

    _WhitePen = ::CreatePen(PS_SOLID,1, RGB(255,255,255));
    _GrayPen = ::CreatePen(PS_SOLID,1, RGB(128,128,128));
    _ShadowPen = ::CreatePen(PS_INSIDEFRAME, 1,::GetSysColor(COLOR_BTNSHADOW));
    if (::GetSysColor(COLOR_BTNHIGHLIGHT) == ::GetSysColor(COLOR_BTNFACE))
	_LightPen = ::CreatePen(PS_INSIDEFRAME, 1,RGB(255,255,255));
    else
	_LightPen = ::CreatePen(PS_INSIDEFRAME, 1,::GetSysColor(COLOR_BTNHIGHLIGHT));
    _BarBrush = ::CreateSolidBrush(::GetSysColor(COLOR_BTNFACE));

    ::ShowWindow(_Frame, nCmdShow);

    ::UpdateWindow(_Frame);

    _running = 1;
  }

//======================>>> vApp::~vApp <<<=======================
  vApp::~vApp()
  {
    // WARNING! This destructor never gets called automatically,
    // at least with Borland C++ 4.5.
    SysDebug(Destructor,"vApp::~vApp destructor\n")

   if (_clipText != 0)
       delete [] _clipText;

   _destroyed = 1;

  }

//======================>>> vApp::Exit <<<=======================
  void vApp::Exit(void)
  {
    // Close All registered windows and exit

    WindList *curWin;
    vWindow *tmp;
    
    SysDebug(Build,"vApp::Exit()\n")

    if (_workTimer)		// Stop events first
      {
        _workTimer->TimerStop();
        delete _workTimer;
        _workTimer = 0;
      }

    _inExit = 1;			// Kludge - CloseAppWin needs this

    for (curWin = _WindowList ; curWin !=0 ; curWin = _WindowList)
      {
        int retv;
	tmp = curWin->window;
	if (IsHelpWin(tmp))
	    retv = CloseHelpWin(tmp);
	else
	    retv = CloseAppWin(tmp);		// use local or derived close app
        if (!retv)
          {
            _inExit = 0;
            return;
          }
      }

    _inExit = 0;			// done now, so can exit
    AppExit(0);
  }

//======================>>> vApp::AppExit <<<=======================
  void vApp::AppExit(int exitVal)
  {

    if (!_inExit)		// Only ONE, please!
      {
	::PostQuitMessage(exitVal);
      }
  }

//========================>>> vApp::CheckEvents <<<=======================
  void vApp::CheckEvents()
  {
    extern int vChkWinDlgMsgs(MSG*);
    extern int vChkCmdPaneMsgs(MSG*);
    MSG msg;

    // Enter the modified message loop

    while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
      {

	if (!vChkWinDlgMsgs(&msg) &&		// Dialogs and cmd panes handle
	  !vChkCmdPaneMsgs(&msg) &&             // their own messages
	  !::TranslateMDISysAccel(_Client, &msg) &&
	  !::TranslateAccelerator(_Frame, (HACCEL)hAccel, &msg)) // probably don't need?
	  {
	    ::TranslateMessage(&msg);
	    ::DispatchMessage(&msg);
	  }
      }
  }

//==================>>> vApp::CloseLastCmdWindow <<<=======================
  void vApp::CloseLastCmdWindow(vWindow* win, int exitcode)
  {
     // This method, new in 1.21, allows an empty MDI frame for
     // Windows. Default behavior is to exit on last close. To
     // overide, the derived CloseLastCmdWindow should simply return.

    // delete win; 			// this causes bug 1.21a - 1/6/99
    AppExit(0);
  }

//==================>>> vApp::ClipboardSetText <<<=======================
  int vApp::ClipboardSetText(VCONST char* text) VCONST
  {
    // set the system clipboard to the value in text

    int retval = 1;
    int lines = 0;		// how many lines in text

    for (VCONST char *tp = text ; *tp ; ++tp)
        if (*tp == '\n')
            ++lines;		// count up lines

    char* temp = new char[strlen(text)+lines+4]; // space for txt
    char* to = temp;
    VCONST char *from = text;
    while (*from)
      {
        if (*from != '\r')	// all except CRs
          {
            if (*from == '\n')	// force all cr/lf or lf to cr/lf
                *to++ = '\r';
            *to++ = *from;
          }
        ++from;
      }
    *to = 0;			// terminate
    int len = strlen(temp);	// how long string really is

    // Now, copy to system clipboard
    HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, len + 1);
    if (hMem != 0)
      {
        char* strMem = (char*)::GlobalLock(hMem);
        strcpy(strMem, temp);	// copy our string
        if (::OpenClipboard(0))
          {
            if (::EmptyClipboard())
              {
                ::SetClipboardData(CF_TEXT, hMem);
              }
            ::CloseClipboard();
          }
        else
            retval = 0;
        ::GlobalUnlock(hMem);		// Unlock memory, sys frees it
      }
    else
        retval = 0;

    delete [] temp;
    return retval;
  }

//====================>>> vApp::ClipboardGetText <<<=====================
  char* vApp::ClipboardGetText() VCONST
  {
    // return a pointer to the clipboard text - we will allocate
    // space for it as needed

    if (!::IsClipboardFormatAvailable(CF_TEXT))
        return 0;
    if (!::OpenClipboard(0))
        return 0;
    if (_clipText != 0)			// had some previous data
        delete [] _clipText;
    _clipText = 0;			// make 0 for default return

    HGLOBAL hMem = ::GetClipboardData(CF_TEXT);
    if (hMem != NULL)			// Got some text data
      {
        char* str = (char*) hMem;
        _clipText = new char[strlen(str)+1];
        strcpy(_clipText, str);
      }
   ::CloseClipboard();
   return _clipText;
  }

//====================>>> vApp::ClipboardCheckText <<<===================
  int vApp::ClipboardCheckText() VCONST
  {
    // check if text available on system clipboard
    return ::IsClipboardFormatAvailable(CF_TEXT);
  }

//====================>>> vApp::ClipboardClear <<<========================
  void vApp::ClipboardClear() VCONST
  {
    // Clear out the clipboard
    ::EmptyClipboard();
  }

//========================>>> vApp::CloseAppWin <<<=======================
  int vApp::CloseAppWin(vWindow* win)
  {

    SysDebug(Build,"vApp::CloseAppWin()\n");

    win->CloseWin();			// let the window close itself
  
    unregisterWindow(win);		// take it off the list
    delete win;				// free the window
    return 1;
  }

//========================>>> vApp::CloseHelpWin <<<=======================
  int vApp::CloseHelpWin(vWindow* win)
  {

    SysDebug(Build,"vApp::CloseHelpWin()\n");

    win->CloseWin();			// let the window close itself
    unregisterWindow(win);		// take it off the list
    delete win;				// free the window
    return 1;
  }

//========================>>> vApp::IsHelpWin <<<=======================
  int vApp::IsHelpWin(vWindow *Win)
  {
    WindList *curWin;

    for (curWin = _WindowList ; curWin !=0 ; curWin = curWin->nextWinList)
      {
	if (curWin->window == Win)
	  {
	    return (curWin->info == 0);	// Help if 0
	  }
      }
    return 0;
  }

//===========================>>> vApp::AppCommand <<<=========================
  void vApp::AppCommand(vWindow* win, ItemVal id, ItemVal retval, CmdType ctype)
  {
    // Do nothing by default.

    SysDebug1(CmdEvents,"vApp::AppCmd(id: %d)\n",id);
    if (id == M_Exit)
	Exit();
  }

//========================>>> vApp::GetDefaultFont <<<=======================
  vFont vApp::GetDefaultFont()
  {
    vFont sysF(vfDefaultSystem);	// construct a system font instance
    return sysF; 		      	// return it
  }

//========================>>> vApp::getAppWinInfo <<<=======================
  vAppWinInfo* vApp::getAppWinInfo(vWindow *Win)
  {
    // Search list to find associated vAppWinInfo.

    WindList *curWin;

    for (curWin = _WindowList ; curWin !=0 ; curWin = curWin->nextWinList)
      {
	if (curWin->window == Win)
	  {
	    return curWin->info;	// return assocated info ptr
	  }
      }
    return 0;
  }

//===========================>>> vApp::KeyIn  <<<===========================
  void vApp::KeyIn(vWindow* win, vKey key, unsigned int shift)
  {
    // Do nothing by default.
    SysDebug(Misc,"vApp::KeyIn\n");
  }

//========================>>> vApp::NewAppWin <<<=======================
  vWindow* vApp::NewAppWin(vWindow* win, VCONST char* name, int w, int h,
	vAppWinInfo* winInfo)
  {
    // The derived vApp needs to call this.

    vWindow* thisWin = win;
    vAppWinInfo* awinfo = winInfo;

    SysDebug1(Build,"vApp::NewAppWin(%s)\n",name);

    if (!thisWin)		// Not created
	thisWin = new vCmdWindow(name, w, h);

    if (!winInfo)
	awinfo = new vAppWinInfo(name);

    registerWindow(thisWin, awinfo);	// register this window
    return thisWin;
  }

//========================>>> vApp::NewHelpWin <<<=======================
  vWindow* vApp::NewHelpWin(vWindow* win, VCONST char* name, int w, int h)
  {
    vWindow* thisWin = win;

    SysDebug1(Build,"vApp::NewHelpWin(%s)\n",name);

    if (!thisWin)		// Not created
	return 0;
    registerWindow(thisWin, 0);	// register this window
    return thisWin;
  }

//========================>>> vApp::registerWindow <<<=======================
  void vApp::registerWindow(vWindow *Win, vAppWinInfo *awinfo)
  {
    WindList* newList = new WindList;	// new cell to add to list

    SysDebug1(Misc,"vApp::registerWindow - %s\n",Win->name())

    newList->window = Win;			// remember the window
    newList->info = awinfo;			// and its info class

    newList->nextWinList = _WindowList;		// link in at front
    _WindowList = newList;
  }

//========================>>> vApp::unregisterWindow <<<=======================
  void vApp::unregisterWindow(vWindow *Win)
  {
    // Scan window list to unregister this window and free some space 

    WindList *curWin, *tmp, *last, *next;

    last = 0;
   
    for (curWin = _WindowList ; curWin !=0 ; curWin = next)
      {
	next = curWin->nextWinList;
	if (curWin->window == Win)
	  {
	    SysDebug1(Misc,"vApp::unregisterWindow - %s\n",Win->name())

	    tmp = curWin;

	    if (curWin == _WindowList)
		_WindowList = curWin->nextWinList;
	    else
		last->nextWinList = curWin->nextWinList;

	    delete curWin->info;	// free the info space
	    delete tmp;			// free the list space
	  }
	last = curWin;
      }
  }

//========================>>> vApp::registerCmdPane <<<======================
  void vApp::registerCmdPane(vCommandPane* cmdPane)
  {
    CmdPaneList* newList = new CmdPaneList;	// new cell to add to list

    SysDebug(Misc,"vApp::registerCmdPane\n")

    newList->commandPane = cmdPane;		// remember the cmd pane

    newList->nextCPList = _CmdPaneList;		// link in at front
    _CmdPaneList = newList;
  }

//========================>>> vApp::unregisterCmdPane <<<=======================
  void vApp::unregisterCmdPane(vCommandPane* cmdPane)
  {
    // Scan pane list to unregister this window and free some space
    CmdPaneList *curCP, *tmp, *last, *next;

    last = 0;

    for (curCP = _CmdPaneList ; curCP !=0 ; curCP = next)
      {
	next = curCP->nextCPList;
	if (curCP->commandPane == cmdPane)
	  {

	    SysDebug(Misc,"vApp::unregisterCmdPane\n")

	    tmp = curCP;

	    if (curCP == _CmdPaneList)
		_CmdPaneList = curCP->nextCPList;
	    else
		last->nextCPList = curCP->nextCPList;

	    delete tmp;			// free the list space
	  }
	last = curCP;
      }
  }

//========================>>> vApp::selectCmdPanes <<<=======================
  void vApp::selectCmdPanes(vWindow* parent)
  {
    // This is needed by the MDI interface conventions.

    // This will turn off _isShown for panes that are children of other windows
    // and turn onn all cmd panes that are in our window
    CmdPaneList *curCP;

    // First, turn off all command panes in other windows

    for (curCP = _CmdPaneList ; curCP !=0 ; curCP = curCP->nextCPList)
      {
	if ((curCP->commandPane)->_parentWin != parent)
	  {
	    (curCP->commandPane)->_isShown = 0;
	    if (::IsWindow((curCP->commandPane)->_wDialog) &&
	       ::IsWindowVisible((curCP->commandPane)->_wDialog))
		   ::ShowWindow((curCP->commandPane)->_wDialog, SW_HIDE);
	  }
      }

    // Now, turn ours on
    for (curCP = _CmdPaneList ; curCP !=0 ; curCP = curCP->nextCPList)
      {
	if ((curCP->commandPane)->_parentWin == parent)
	  {
	    (curCP->commandPane)->_isShown = 1;
	    if (::IsWindow((curCP->commandPane)->_wDialog) &&
	       !::IsWindowVisible((curCP->commandPane)->_wDialog))
		   ::ShowWindow((curCP->commandPane)->_wDialog, SW_SHOW);
	  }
      }
  }

//========================>>> vApp::SendWindowCommandAll <<<=======================
  void vApp::SendWindowCommandAll(ItemVal id, int val, CmdType ctype)
  {
    // send a command to all windows
    for (WindList* curWin = _WindowList ; curWin !=0 ; curWin = curWin->nextWinList)
      {
	(curWin->window)->WindowCommand(id, val, ctype);
      }
  }

//========================>>> vApp::UpdateAllViews <<<=======================
  void vApp::UpdateAllViews(vWindow* sender, int hint, void* pHint)
  {
    // Easy way to do MVC - call UpdateView in all windows
    // This function is called by the user whenever a change is made to
    // the model e.g the document. This causes vWindow::UpdateView to be
    // called for every open window. The parameters are used to both filter and
    // hint the windows on which actions to take in vWindow::UpdateView:
    //
    //     sender: If this is not zero, this window will not invoke UpdateView,
    //             because typically the change of model was a result of
    //             an interaction with this window.
    //     hint:   This should be an enum defined in your derived app class.
    //             Hints about which kind of change is made, so that only
    //             appropriate action is taken on appropriate windows.
    //     pHint:  This is normally a pointer to the object representing the
    //             document

    for (WindList* curWin = _WindowList ; curWin !=0 ; curWin = curWin->nextWinList)
      {
	if (curWin->window != sender)
	    (curWin->window)->UpdateView(sender, hint, pHint);
      }
  }

//========================>>> vApp::SetValueAll <<<=======================
  void vApp::SetValueAll(ItemVal id, int val, ItemSetType setType)
  {
    // Set a Value in all windows
    for (WindList* curWin = _WindowList ; curWin !=0 ; curWin = curWin->nextWinList)
      {
	(curWin->window)->SetValue(id, val, setType);
      }
  }

//======================>>> vApp::SetAppTitle <<<==========================
  void vApp::SetAppTitle(VCONST char* title)
  {
    // set the title in the title bar -- this is a no-op on some platforms

    ::SetWindowText(_Frame, title);

  }

//========================>>> vApp::SetStringAll <<<=======================
  void vApp::SetStringAll(ItemVal id, VCONST char* str)
  {
    // Set a string in all windows
    for (WindList* curWin = _WindowList ; curWin !=0 ; curWin = curWin->nextWinList)
      {
	(curWin->window)->SetString(id, str);
      }
  }

//========================>>> vApp::ShowList <<<=======================
  int vApp::ShowList(void)
  {
    // This is a utility routine to show current information
#ifdef HAS_PRINTF
    fprintf(stderr,"Registered windows:\n");
    for (WindList* curWin = _WindowList ; curWin !=0 ; curWin = curWin->nextWinList)
      {
	fprintf(stderr,"    %s\n",(curWin->window)->name());
      }
#endif
    return 1;
  }

//====================>>> _appWorkTimer::TimerTick <<<====================
  void _appWorkTimer::TimerTick()
  {

   theApp->DispatchWork();
  }

//========================>>> vApp::EnableWorkSlice <<<====================
  int vApp::EnableWorkSlice(long slice)
  {

    if (slice > 0)
      {
	if (_workTimer == 0)		// First time to start timer
	  {
	    _workTimer = new _appWorkTimer;
	  }
	return _workTimer->TimerSet(slice);
      }
    else
      {
	if (_workTimer)
          {
	    _workTimer->TimerStop();
            delete _workTimer;
            _workTimer = 0;
          }
      }
    return 1;
  }

//========================>>> vApp::DispatchWork <<<=======================
  void vApp::DispatchWork(void)
  {

    WorkSlice();		// Work Slice for App
    // Call WorkSlice for all windows
    for (WindList* curWin = _WindowList ; curWin !=0 ;
	     curWin = curWin->nextWinList)
      {
	(curWin->window)->WorkSlice();
      }
  }

//========================>>> vApp::doEventLoop <<<=======================
  int vApp::doEventLoop(void)
  {
    // This is where we grab and handle events from the
    // parent windowing system
    extern int vChkWinDlgMsgs(MSG*);
    extern int vChkCmdPaneMsgs(MSG*);
    MSG msg;

    // Enter the modified message loop

    while (::GetMessage(&msg, NULL, 0, 0))
      {

	if (!vChkWinDlgMsgs(&msg) &&		// Dialogs and cmd panes handle
	  !vChkCmdPaneMsgs(&msg) &&             // their own messages
	  !::TranslateMDISysAccel(_Client, &msg) &&
	  !::TranslateAccelerator(_Frame, (HACCEL)hAccel, &msg)) // probably don't need?
	  {
	    ::TranslateMessage(&msg);
	    ::DispatchMessage(&msg);
	  }
      }

    // The "Honor System" that Windows requires of returning every tiny
    // resource you use really sucks. It took hours to figure out this
    // way of freeing resources. Turns out that the destructor for the static
    // vApp object never gets called once PostQuitMessage is called. We do come
    // through here, however, so here is where we can return these
    // resources.  A reasonable operating system recovers system
    // resources used by a process when it terminates. Not Windows...

    if (_workTimer)
      {
	_workTimer->TimerStop();
	delete _workTimer;
      }

    ::DestroyIcon(_appicon);
    ::DestroyIcon(_winicon);

    ::DeleteObject(_WhitePen);
    ::DeleteObject(_GrayPen);
    ::DeleteObject(_LightPen);
    ::DeleteObject(_ShadowPen);
    ::DeleteObject(_BarBrush);

    ::DestroyMenu(hFrameSubMenu);
    ::DestroyMenu(hFrameMenu);

    ::DestroyWindow(_Client);
    ::DestroyWindow(_Frame);

    return msg.wParam;
  }

//======================>>> PMdiFrameProc <<<================================
  long FAR PASCAL _export PMdiFrameProc(HWND hwnd, UINT message,
				UINT wParam, LPARAM lParam)
  {
    return theApp->MdiFrameProc(hwnd, message, wParam, lParam);
  }

//======================>>> vApp::OnCreate <<<============================
  int vApp::OnCreate(HWND hwnd, CREATESTRUCT FAR* lpCreateStruct)
  {

    CLIENTCREATESTRUCT clientcreate;

    clientcreate.hWindowMenu  = hFrameSubMenu;
    clientcreate.idFirstChild = M_FIRSTCHILD;

    if (_simSDI)
      {
        _Client = ::CreateWindow("MDICLIENT", NULL,
	  WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE | WS_CLIPSIBLINGS |
	  WS_HSCROLL | WS_VSCROLL | MDIS_ALLCHILDSTYLES,
	  0, 0, 0, 0, hwnd, NULL, (HINSTANCE)_vHandle,
	  (LPSTR) &clientcreate);
      }
    else
      {
	_Client = ::CreateWindow("MDICLIENT", NULL,
	  WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE | WS_CLIPSIBLINGS |
	  WS_HSCROLL | WS_VSCROLL,
	  0, 0, 0, 0, hwnd, NULL, (HINSTANCE)_vHandle,
	  (LPSTR) &clientcreate);
      }

    return _Client != NULL;
  }

// Following are routines to handle Windows messages

//======================>>> vApp::OnClose <<<============================
  void vApp::OnClose(HWND hwnd)
  {
    Exit();
  }

//======================>>> vApp::OnQueryEndSession <<<============================
  int vApp::OnQueryEndSession(HWND hwnd)
  {
    return 1;
  }

//======================>>> vApp::OnDestroy <<<============================
  void vApp::OnDestroy(HWND hwnd)
  {

  }

//======================>>> vApp::OnSysCommand <<<========================
  void vApp::OnSysCommand(HWND hwnd, UINT cmd, int x, int y)
  {
    // Set focus to frame window.  This causes any comboboxes
    // in dialogs to be closed.
    ::SetFocus(_Frame);
    FORWARD_WM_SYSCOMMAND(hwnd, cmd, x, y, Frame_DefProc);
  }

//======================>>> vApp::OnSize <<<========================
  void vApp::OnSize(HWND hwnd, UINT state, int cx, int cy)
  {
   // Force MDI Child window to be resized.
   FORWARD_vWM_ResizeMDIClient(hwnd, SendMessage);
  }

//======================>>> vApp::OnCommand <<<========================
  void vApp::OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
  {
    switch (id)
      {
        case M_WindowsReserved1:
	    AppCommand((vWindow*)0,(ItemVal) M_New, (ItemVal)0, (CmdType)0);
	    return;
        case M_WindowsReserved2:
	    AppCommand((vWindow*)0,(ItemVal) M_Open, (ItemVal)0, (CmdType)0);
	    return;
        case M_WindowsReserved3:
	    AppCommand((vWindow*)0,(ItemVal) M_Exit, (ItemVal)0, (CmdType)0);
	    return;

	// Messages for arranging child MDI windows
	case M_Tile:
	    (void)FORWARD_WM_MDITILE(_Client, WM_MDITILE, SendMessage);
	    return;

	case M_Cascade:
	    (void)FORWARD_WM_MDICASCADE(_Client,0,SendMessage);
	    return;

	case M_Arrange:
	    (void)FORWARD_WM_MDIICONARRANGE(_Client, SendMessage);
	    return;

	default:            // Pass to active child

	    HWND hwndChild = (HWND)::SendMessage(_Client,
		WM_MDIGETACTIVE, 0, 0L);

	    if (::IsWindow(hwndChild) && id < M_FIRSTCHILD)
	      {
	        FORWARD_WM_COMMAND(hwndChild, id, hwndCtl, codeNotify,
		    SendMessage);
		//return;
	      }
      }
    FORWARD_WM_COMMAND(hwnd, id, hwndCtl, codeNotify, Frame_DefProc);
  }

//======================>>> vApp::Frame_DefProc <<<========================
  LRESULT vApp::Frame_DefProc (HWND hwnd, UINT uMsg,
	WPARAM wParam, LPARAM lParam)
  {
    return ::DefFrameProc(hwnd, _Client, uMsg, wParam, lParam);
  }

//======================>>> vApp::OnResizeMDIClient <<<============================
  void vApp::OnResizeMDIClient(HWND hwnd)
  {
    // This is called whenever the Frame is resized, or when the state of
    // a status bar or command pane changes.
    RECT rc;

    vCommandPane* cp;
    CmdPaneList *curCPL;

    if (!::IsWindow(_Client))
	return;

   if (!_Frame)
      return;

    ::GetClientRect(_Frame, &rc);		 // the rect of the Frame

    int topY = 0;			// Need to track space used by
    int botY = rc.bottom;               // cmd bars and status bars

    // Scan the cmdpane list to reposition all cmd bars and status bars

    for (curCPL = _CmdPaneList ; curCPL != 0 ; curCPL = curCPL->nextCPList)
      {
	cp = curCPL->commandPane;
	if (cp->_isShown && cp->_isDisplayed &&
		   ::IsWindow(cp->HwndDialog()) && ::IsWindowVisible(cp->HwndDialog()))
	  {
	    if (cp->paneType() != P_Status)	// Cmd bar
	      {
		::MoveWindow(cp->HwndDialog(), 0,topY,rc.right,
		   cp->_windowH + topY,1);
		topY += cp->_windowH;
		cp->_windowY = topY;
	      }                                	// Status bar
	    else
	      {
		botY -= cp->_windowH;
		::MoveWindow(cp->HwndDialog(), 0, botY,rc.right,
		   cp->_windowH + botY,1);
		cp->_windowY = botY;
	      }
	  }
      }

    ::MoveWindow(_Client, 0, topY, rc.right, botY - topY , 1);
  }

//======================>>> vApp::MdiFrameProc <<<============================
  long vApp::MdiFrameProc(HWND hwnd, UINT uMsg, UINT wParam, LPARAM lParam)
  {
    switch (uMsg)
     {
      HANDLE_MSG(hwnd, WM_CREATE,           OnCreate);
      HANDLE_MSG(hwnd, WM_CLOSE,            OnClose);
      HANDLE_MSG(hwnd, WM_QUERYENDSESSION,  OnQueryEndSession);
      HANDLE_MSG(hwnd, WM_DESTROY,          OnDestroy);
      HANDLE_MSG(hwnd, WM_SYSCOMMAND,       OnSysCommand);
      HANDLE_MSG(hwnd, WM_SIZE,             OnSize);
      HANDLE_MSG(hwnd, WM_COMMAND,          OnCommand);
      HANDLE_MSG(hwnd, vWM_ResizeMDIClient, OnResizeMDIClient);
    }
   return(::DefFrameProc(hwnd, _Client, uMsg, wParam, lParam));
  }

//======================>>> PMdiWindowProc <<<================================
  long FAR PASCAL _export PMdiWindowProc(HWND hwnd, UINT message,
				WPARAM wParam, LPARAM lParam)
  {
    // Kludgy stuff to get things rolling. Part of hhis code is needed to
    // get the first mouse click to activate the window, but not
    // send a mousedown message.  Windows can be UGLY...

    static vWindow* createThis = 0;
    vWindow* thisWin = (vWindow*)::GetWindowLong(hwnd, 0);

    if (!thisWin && message == WM_CREATE)
      {
	// We need to intercept the message here to recover the vWindow
	// this of the window. It is sent in the lParam of the CREATESTRUCT.
	CREATESTRUCT* cs = (CREATESTRUCT*)lParam;	// fetch the ptr
	// now convert to MDICREATESTRUCT
	MDICREATESTRUCT* mdics = (MDICREATESTRUCT*)cs->lpCreateParams;

	createThis = thisWin = (vWindow*)mdics->lParam;	// remember the this!
      }
    else if (createThis != 0 && message == WM_MDIACTIVATE)
      {
	thisWin = createThis;
	createThis = 0;
      }

    if (!thisWin)
      {

	return ::DefMDIChildProc(hwnd, message, wParam, lParam);
      }
    else
	return thisWin->MdiWindowProc(hwnd, message, wParam, lParam);
   }


//#########################################################################

// The user function AppMain is accessed through a pointer. This function
// stores the user's AppMain in a pointr to function that is used inside
// the V library. This was added to enable V Win32 DLL. It works also
// with a static library.
//
V_EXPORT void vRegisterAppMain( int (*p)(int, char**) )
{
	pAppMain = p;
}

// Actually we should return vApp*, but VC++ complains that it
// can't find vGetApp when linking the application.
//
DWORD vGetApp()
{
	return (DWORD) theGlobalApp;
}

//======================>>> CMain <<<======================================
  int CMain(HANDLE hInstance, HANDLE hPrevInstance,
		      LPSTR lpszCmdLine, int nCmdShow)
  {
    // CMain is a C++ style wrapper -- makes the "friend" declaration nicer
    int argc = 0;
    char** argv = new char*[50];	// up to 50 args
    char name[200];

#ifdef __MINGW32__
     vFORCE_TO_INCLUDE_MODULE = 0;
#endif
#ifdef __CYGWIN32__
    vFORCE_TO_INCLUDE_MODULE = 0;	// bug in cygwin?
#endif

    int retcode;

    // Split command line into tokens, as in usual main(argc, argv)

    int cmdLen = strlen((char *)lpszCmdLine);
    char *buf = new char[cmdLen + 1];

    strcpy(buf, (char*) lpszCmdLine);		// make copy

    // Get application name

#ifndef VWIN16
    InitCommonControls();		// for tooltips
#endif

    ::GetModuleFileName((HINSTANCE)hInstance, name, 199);

    argv[argc++] = name;

    // Now split argument string

    {
    char *token;
    const char *IFS = " \t\r\n";
    if ((token = strtok(buf, IFS)) != NULL)
      {
	do
	  {
	    if (*token != '\0' && strchr(IFS, *token) == NULL)
	      argv[argc++] = token;
	  } while ((token = strtok(NULL, IFS)) != NULL);
      }
    }

    argv[argc] = NULL;  // argv[] is NULL terminated list!

    theApp->initialize(argc,argv,
	hInstance, hPrevInstance, nCmdShow);	// Create top level widget

    if ((retcode = pAppMain(argc,argv)) != 0)	// call the app main program
	return (retcode);

    return theApp->doEventLoop();		// And enter the event loop
  }

//#########################################################################
// Utilities
//=========================>>> vSysWarning <<<============================
  void vSysWarning(VCONST char* msg)
  {
    ::MessageBox(0, msg, "V", MB_ICONEXCLAMATION | MB_OK);
  }

//=========================>>> vSys <<<============================
  void vSysError(VCONST char* msg)
  {
    ::MessageBox(0, msg,"V", MB_ICONEXCLAMATION | MB_OK);
    theApp->AppExit(99);
  }

//#########################################################################