]> git.saurik.com Git - wxWidgets.git/blob - src/msw/app.cpp
Fixed various wxMSW compile problems that came down the telephone line...
[wxWidgets.git] / src / msw / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: app.cpp
3 // Purpose: wxApp
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "app.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #define IN_WX_MAIN_CPP
18 #include "wx/wxprec.h"
19
20 #if defined(__BORLANDC__)
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/frame.h"
26 #include "wx/app.h"
27 #include "wx/utils.h"
28 #include "wx/gdicmn.h"
29 #include "wx/pen.h"
30 #include "wx/brush.h"
31 #include "wx/cursor.h"
32 #include "wx/icon.h"
33 #include "wx/palette.h"
34 #include "wx/dc.h"
35 #include "wx/dialog.h"
36 #include "wx/msgdlg.h"
37 #endif
38
39 #include "wx/msw/private.h"
40 #include "wx/postscrp.h"
41 #include "wx/log.h"
42 #include "wx/module.h"
43
44 #if USE_WX_RESOURCES
45 #include "wx/resource.h"
46 #endif
47
48
49 #include <string.h>
50
51 #if defined(__WIN95__) && !defined(__GNUWIN32__)
52 #include <commctrl.h>
53 #endif
54
55 // use debug CRT functions for memory leak detections in VC++
56 /* THIS CRAPS OUT in VC++ 5.
57 #if defined(__WXDEBUG__) && defined(_MSC_VER)
58 #include <crtdbg.h>
59 #endif
60 */
61
62 extern char *wxBuffer;
63 extern char *wxOsVersion;
64 extern wxList *wxWinHandleList;
65 extern wxList wxPendingDelete;
66 extern void wxSetKeyboardHook(bool doIt);
67 extern wxCursor *g_globalCursor;
68
69 HINSTANCE wxhInstance = 0;
70 static MSG s_currentMsg;
71 wxApp *wxTheApp = NULL;
72
73 char wxFrameClassName[] = "wxFrameClass";
74 char wxMDIFrameClassName[] = "wxMDIFrameClass";
75 char wxMDIChildFrameClassName[] = "wxMDIChildFrameClass";
76 char wxPanelClassName[] = "wxPanelClass";
77 char wxCanvasClassName[] = "wxCanvasClass";
78
79 HICON wxSTD_FRAME_ICON = NULL;
80 HICON wxSTD_MDICHILDFRAME_ICON = NULL;
81 HICON wxSTD_MDIPARENTFRAME_ICON = NULL;
82
83 HICON wxDEFAULT_FRAME_ICON = NULL;
84 HICON wxDEFAULT_MDICHILDFRAME_ICON = NULL;
85 HICON wxDEFAULT_MDIPARENTFRAME_ICON = NULL;
86
87 HBRUSH wxDisableButtonBrush = 0;
88
89 LRESULT APIENTRY wxWndProc(HWND, UINT, WPARAM, LPARAM);
90
91 #if !USE_SHARED_LIBRARY
92 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
93 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
94 EVT_IDLE(wxApp::OnIdle)
95 END_EVENT_TABLE()
96 #endif
97
98 long wxApp::sm_lastMessageTime = 0;
99
100 #ifdef __WIN95__
101 static HINSTANCE gs_hRichEdit = NULL;
102 #endif
103
104 bool wxApp::Initialize(WXHINSTANCE instance)
105 {
106 HINSTANCE hInstance = (HINSTANCE) instance;
107
108 CommonInit();
109
110 #if defined(__WIN95__)
111 InitCommonControls();
112 gs_hRichEdit = LoadLibrary("RICHED32.DLL");
113
114 if (gs_hRichEdit == NULL)
115 {
116 wxMessageBox("Could not initialise Rich Edit DLL");
117 }
118 #endif
119
120 #if defined(WX_DRAG_DROP)
121 // we need to initialize OLE library
122 if ( FAILED(::OleInitialize(NULL)) )
123 wxFatalError(_("Cannot initialize OLE"));
124 #endif
125
126 #if CTL3D
127 if (!Ctl3dRegister(hInstance))
128 wxFatalError("Cannot register CTL3D");
129
130 Ctl3dAutoSubclass(hInstance);
131 #endif
132
133 wxSTD_FRAME_ICON = LoadIcon(hInstance, "wxSTD_FRAME");
134 wxSTD_MDIPARENTFRAME_ICON = LoadIcon(hInstance, "wxSTD_MDIPARENTFRAME");
135 wxSTD_MDICHILDFRAME_ICON = LoadIcon(hInstance, "wxSTD_MDICHILDFRAME");
136
137 wxDEFAULT_FRAME_ICON = LoadIcon(hInstance, "wxDEFAULT_FRAME");
138 wxDEFAULT_MDIPARENTFRAME_ICON = LoadIcon(hInstance, "wxDEFAULT_MDIPARENTFRAME");
139 wxDEFAULT_MDICHILDFRAME_ICON = LoadIcon(hInstance, "wxDEFAULT_MDICHILDFRAME");
140
141 RegisterWindowClasses();
142
143 // Create the brush for disabling bitmap buttons
144
145 LOGBRUSH lb ;
146 lb.lbStyle = BS_PATTERN;
147 lb.lbHatch = (int)LoadBitmap( hInstance, "wxDISABLE_BUTTON_BITMAP" ) ;
148 wxDisableButtonBrush = ::CreateBrushIndirect( & lb ) ;
149 ::DeleteObject( (HGDIOBJ)lb.lbHatch ) ;
150
151 #if USE_PENWINDOWS
152 wxRegisterPenWin();
153 #endif
154
155 wxWinHandleList = new wxList(wxKEY_INTEGER);
156
157 // This is to foil optimizations in Visual C++ that
158 // throw out dummy.obj.
159 #if (_MSC_VER >= 800) && !defined(WXMAKINGDLL)
160 extern char wxDummyChar;
161 if (wxDummyChar) wxDummyChar++;
162 #endif
163 wxSetKeyboardHook(TRUE);
164
165 wxModule::RegisterModules();
166 if (!wxModule::InitializeModules())
167 return FALSE;
168 return TRUE;
169 }
170
171 bool wxApp::RegisterWindowClasses()
172 {
173 ///////////////////////////////////////////////////////////////////////
174 // Register the frame window class.
175 WNDCLASS wndclass; // Structure used to register Windows class.
176
177 wndclass.style = CS_HREDRAW | CS_VREDRAW;
178 wndclass.lpfnWndProc = (WNDPROC)wxWndProc;
179 wndclass.cbClsExtra = 0;
180 wndclass.cbWndExtra = sizeof( DWORD ); // was 4
181 wndclass.hInstance = wxhInstance;
182 wndclass.hIcon = NULL; // wxSTD_FRAME_ICON;
183 wndclass.hCursor = LoadCursor( NULL, IDC_ARROW );
184 wndclass.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1) ;
185 // wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
186 wndclass.lpszMenuName = NULL;
187 #ifdef _MULTIPLE_INSTANCES
188 sprintf( wxFrameClassName,"wxFrameClass%d", hInstance );
189 #endif
190 wndclass.lpszClassName = wxFrameClassName;
191
192 if (!RegisterClass( &wndclass ))
193 {
194 // wxFatalError("Can't register Frame Window class");
195 }
196
197 ///////////////////////////////////////////////////////////////////////
198 // Register the MDI frame window class.
199 WNDCLASS wndclass1; // Structure used to register Windows class.
200
201 wndclass1.style = CS_HREDRAW | CS_VREDRAW;
202 wndclass1.lpfnWndProc = (WNDPROC)wxWndProc;
203 wndclass1.cbClsExtra = 0;
204 wndclass1.cbWndExtra = sizeof( DWORD ); // was 4
205 wndclass1.hInstance = wxhInstance;
206 wndclass1.hIcon = NULL; // wxSTD_MDIPARENTFRAME_ICON;
207 wndclass1.hCursor = LoadCursor( NULL, IDC_ARROW );
208 // wndclass1.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1) ;
209 wndclass1.hbrBackground = NULL;
210 wndclass1.lpszMenuName = NULL;
211
212 wndclass1.lpszClassName = wxMDIFrameClassName;
213 if (!RegisterClass( &wndclass1 ))
214 {
215 // wxFatalError("Can't register MDI Frame window class");
216 // return FALSE;
217 }
218
219 ///////////////////////////////////////////////////////////////////////
220 // Register the MDI child frame window class.
221 WNDCLASS wndclass4; // Structure used to register Windows class.
222
223 wndclass4.style = CS_HREDRAW | CS_VREDRAW;
224 wndclass4.lpfnWndProc = (WNDPROC)wxWndProc;
225 wndclass4.cbClsExtra = 0;
226 wndclass4.cbWndExtra = sizeof( DWORD ); // was 4
227 wndclass4.hInstance = wxhInstance;
228 wndclass4.hIcon = NULL; // wxSTD_MDICHILDFRAME_ICON;
229 wndclass4.hCursor = LoadCursor( NULL, IDC_ARROW );
230 // TODO: perhaps this should be NULL so that Windows doesn't
231 // paint the background itself (would OnEraseBackground duplicate
232 // this?)
233 wndclass4.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ;
234 // wndclass4.hbrBackground = NULL;
235 wndclass4.lpszMenuName = NULL;
236 wndclass4.lpszClassName = wxMDIChildFrameClassName;
237
238 if (!RegisterClass( &wndclass4 ))
239 {
240 // wxFatalError("Can't register MDI child frame window class");
241 // return FALSE;
242 }
243
244 ///////////////////////////////////////////////////////////////////////
245 // Register the panel window class.
246 WNDCLASS wndclass2; // Structure used to register Windows class.
247 memset(&wndclass2, 0, sizeof(WNDCLASS)); // start with NULL defaults
248 // Use CS_OWNDC to avoid messing about restoring the context
249 // for every graphic operation.
250 wndclass2.style = CS_HREDRAW | CS_VREDRAW;
251 wndclass2.lpfnWndProc = (WNDPROC)wxWndProc;
252 wndclass2.cbClsExtra = 0;
253 wndclass2.cbWndExtra = sizeof( DWORD ); // was 4
254 wndclass2.hInstance = wxhInstance;
255 wndclass2.hIcon = NULL;
256 wndclass2.hCursor = NULL;
257 // wndclass2.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1) ;
258 wndclass2.hbrBackground = (HBRUSH) GetStockObject( LTGRAY_BRUSH );
259 wndclass2.lpszMenuName = NULL;
260 wndclass2.lpszClassName = wxPanelClassName;
261 if (!RegisterClass( &wndclass2 ))
262 {
263 // wxFatalError("Can't register Panel Window class");
264 // return FALSE;
265 }
266
267 ///////////////////////////////////////////////////////////////////////
268 // Register the canvas and textsubwindow class name
269 WNDCLASS wndclass3; // Structure used to register Windows class.
270 memset(&wndclass3, 0, sizeof(WNDCLASS)); // start with NULL defaults
271 // Use CS_OWNDC to avoid messing about restoring the context
272 // for every graphic operation.
273 // wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS ;
274 // wxWin 2.0
275 wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
276 wndclass3.lpfnWndProc = (WNDPROC)wxWndProc;
277 wndclass3.cbClsExtra = 0;
278 wndclass3.cbWndExtra = sizeof( DWORD ); // was 4
279 wndclass3.hInstance = wxhInstance;
280 wndclass3.hIcon = NULL;
281 wndclass3.hCursor = NULL;
282 // wndclass3.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ;
283 wndclass3.hbrBackground = NULL;
284 wndclass3.lpszMenuName = NULL;
285 wndclass3.lpszClassName = wxCanvasClassName;
286 if (!RegisterClass( &wndclass3))
287 {
288 // wxFatalError("Can't register Canvas class");
289 // return FALSE;
290 }
291
292 return TRUE;
293 }
294
295 // Cleans up any wxWindows internal structures left lying around
296 void wxApp::CleanUp()
297 {
298 wxModule::CleanUpModules();
299
300 CommonCleanUp();
301
302 wxSetKeyboardHook(FALSE);
303
304 #ifdef __WIN95__
305 if (gs_hRichEdit != NULL)
306 FreeLibrary(gs_hRichEdit);
307 #endif
308
309 #if USE_PENWINDOWS
310 wxCleanUpPenWin();
311 #endif
312
313 if (wxSTD_FRAME_ICON)
314 DestroyIcon(wxSTD_FRAME_ICON);
315 if (wxSTD_MDICHILDFRAME_ICON)
316 DestroyIcon(wxSTD_MDICHILDFRAME_ICON);
317 if (wxSTD_MDIPARENTFRAME_ICON)
318 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON);
319
320 if (wxDEFAULT_FRAME_ICON)
321 DestroyIcon(wxDEFAULT_FRAME_ICON);
322 if (wxDEFAULT_MDICHILDFRAME_ICON)
323 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON);
324 if (wxDEFAULT_MDIPARENTFRAME_ICON)
325 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON);
326
327 if ( wxDisableButtonBrush )
328 ::DeleteObject( wxDisableButtonBrush ) ;
329
330 #if defined(WX_DRAG_DROP)
331 ::OleUninitialize();
332 #endif
333
334 #if CTL3D
335 Ctl3dUnregister(wxhInstance);
336 #endif
337
338 if (wxWinHandleList)
339 delete wxWinHandleList ;
340
341 // do it as the very last thing because everything else can log messages
342 wxLog::DontCreateOnDemand();
343 delete wxLog::SetActiveTarget(NULL);
344 }
345
346 void wxApp::CommonInit()
347 {
348 #ifdef __WXMSW__
349 wxBuffer = new char[1500];
350 #else
351 wxBuffer = new char[BUFSIZ + 512];
352 #endif
353
354 wxClassInfo::InitializeClasses();
355
356 #if USE_RESOURCES
357 wxGetResource("wxWindows", "OsVersion", &wxOsVersion);
358 #endif
359
360 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
361 wxTheColourDatabase->Initialize();
362
363 wxInitializeStockLists();
364 wxInitializeStockObjects();
365
366 #if USE_WX_RESOURCES
367 wxInitializeResourceSystem();
368 #endif
369
370 // For PostScript printing
371 #if USE_POSTSCRIPT
372 wxInitializePrintSetupData();
373 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
374 wxThePrintPaperDatabase->CreateDatabase();
375 #endif
376
377 wxBitmap::InitStandardHandlers();
378
379 g_globalCursor = new wxCursor;
380 }
381
382 void wxApp::CommonCleanUp()
383 {
384 #if USE_WX_RESOURCES
385 wxCleanUpResourceSystem();
386
387 // wxDefaultResourceTable->ClearTable();
388 #endif
389 // Indicate that the cursor can be freed,
390 // so that cursor won't be deleted by deleting
391 // the bitmap list before g_globalCursor goes out
392 // of scope (double deletion of the cursor).
393 wxSetCursor(wxNullCursor);
394 delete g_globalCursor;
395
396 wxDeleteStockObjects() ;
397
398 // Destroy all GDI lists, etc.
399 delete wxTheBrushList;
400 wxTheBrushList = NULL;
401
402 delete wxThePenList;
403 wxThePenList = NULL;
404
405 delete wxTheFontList;
406 wxTheFontList = NULL;
407
408 delete wxTheBitmapList;
409 wxTheBitmapList = NULL;
410
411 delete wxTheColourDatabase;
412 wxTheColourDatabase = NULL;
413
414 #if USE_POSTSCRIPT
415 wxInitializePrintSetupData(FALSE);
416 delete wxThePrintPaperDatabase;
417 wxThePrintPaperDatabase = NULL;
418 #endif
419
420 wxBitmap::CleanUpHandlers();
421
422 delete[] wxBuffer;
423 wxBuffer = NULL;
424 }
425
426 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
427
428 // Main wxWindows entry point
429
430 int wxEntry(WXHINSTANCE hInstance, WXHINSTANCE WXUNUSED(hPrevInstance), char *m_lpCmdLine,
431 int nCmdShow, bool enterLoop)
432 {
433 wxhInstance = (HINSTANCE) hInstance;
434
435 /* No, sorry, whatever this is, forget it. Doesn't work with VC++ 5.
436
437 #if defined(__WXDEBUG__) && defined(_MSC_VER)
438 // do check for memory leaks on program exit
439 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
440 // deallocated memory which may be used to simulate low-memory condition)
441 _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
442 #endif // debug build under MS VC++
443 */
444
445 #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
446
447 #if !defined(_WINDLL)
448 streambuf* sBuf = new wxDebugStreamBuf;
449 #else
450 streambuf* sBuf = NULL;
451 #endif
452 ostream* oStr = new ostream(sBuf) ;
453 wxDebugContext::SetStream(oStr, sBuf);
454
455 #endif // USE_MEMORY_TRACING
456
457 if (!wxApp::Initialize((WXHINSTANCE) wxhInstance))
458 return 0;
459
460 // The app may have declared a global application object, but we recommend
461 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
462 // for delayed, dynamic app object construction.
463 if (!wxTheApp)
464 {
465 if (!wxApp::GetInitializerFunction())
466 {
467 MessageBox(NULL, "No initializer - use IMPLEMENT_APP macro.", "wxWindows Error", MB_APPLMODAL | MB_ICONSTOP | MB_OK);
468 return 0;
469 }
470
471 wxTheApp = (* wxApp::GetInitializerFunction()) ();
472 }
473
474 if (!wxTheApp) {
475 MessageBox(NULL, "You have to define an instance of wxApp!", "wxWindows Error", MB_APPLMODAL | MB_ICONSTOP | MB_OK);
476 return 0;
477 }
478
479 // Split command line into tokens, as in usual main(argc, argv)
480 char **command = new char*[50];
481
482 int count = 0;
483 char *buf = new char[strlen(m_lpCmdLine) + 1];
484
485 // Hangs around until end of app. in case
486 // user carries pointers to the tokens
487
488 /* Model independent strcpy */
489 int i;
490 for (i = 0; (buf[i] = m_lpCmdLine[i]) != 0; i++)
491 {
492 /* loop */;
493 }
494
495 // Get application name
496 char name[200];
497 ::GetModuleFileName(wxhInstance, name, 199);
498
499 // Is it only 16-bit Borland that already copies the program name
500 // to the first argv index?
501 #if !defined(__GNUWIN32__)
502 // #if ! (defined(__BORLANDC__) && !defined(__WIN32__))
503 command[count++] = copystring(name);
504 // #endif
505 #endif
506
507 strcpy(name, wxFileNameFromPath(name));
508 wxStripExtension(name);
509 wxTheApp->SetAppName(name);
510
511 /* Break up string */
512 // Treat strings enclosed in double-quotes as single arguments
513 char* str = buf;
514 while (*str)
515 {
516 while (*str && *str <= ' ') str++; // skip whitespace
517 if (*str == '"')
518 {
519 str++;
520 command[count++] = str;
521 while (*str && *str != '"') str++;
522 }
523 else if (*str)
524 {
525 command[count++] = str;
526 while (*str && *str > ' ') str++;
527 }
528 if (*str) *str++ = '\0';
529 }
530 command[count] = NULL; /* argv[] is NULL terminated list! */
531
532 wxTheApp->argc = count;
533 wxTheApp->argv = command;
534 wxTheApp->m_nCmdShow = nCmdShow;
535
536 // GUI-specific initialisation. In fact on Windows we don't have any,
537 // but this call is provided for compatibility across platforms.
538 wxTheApp->OnInitGui() ;
539
540 if (!wxTheApp->OnInit())
541 {
542 wxTheApp->DeletePendingObjects();
543 wxTheApp->OnExit();
544 wxApp::CleanUp();
545
546 delete wxTheApp;
547 wxTheApp = NULL;
548
549 delete [] buf ;
550
551 // TODO: This should really be cleaned up in ~wxApp
552 delete [] command[0] ;
553 delete [] command ;
554 return 0;
555 }
556
557 if (!enterLoop)
558 return 0;
559
560 int retValue = 1;
561
562 /* New behaviour - leave it to the app to show the top window
563 if (wxTheApp->GetTopWindow()) {
564 // show the toplevel frame, only if we are not iconized (from MS-Windows)
565 if(wxTheApp->GetShowFrameOnInit() && (nCmdShow!=SW_HIDE)) wxTheApp->GetTopWindow()->Show(TRUE);
566 }
567 */
568
569 retValue = wxTheApp->OnRun();
570
571 if (wxTheApp->GetTopWindow())
572 {
573 // Forcibly delete the window.
574 if (wxTheApp->GetTopWindow()->IsKindOf(CLASSINFO(wxFrame)) ||
575 wxTheApp->GetTopWindow()->IsKindOf(CLASSINFO(wxDialog)))
576 {
577 wxTheApp->GetTopWindow()->Close(TRUE);
578 wxTheApp->DeletePendingObjects();
579 }
580 else
581 {
582 delete wxTheApp->GetTopWindow();
583 wxTheApp->SetTopWindow(NULL);
584 }
585 }
586
587 wxTheApp->OnExit();
588 wxApp::CleanUp();
589
590 delete wxTheApp;
591 wxTheApp = NULL;
592
593 delete [] buf ;
594 delete [] command[0] ;
595 delete [] command ;
596
597 #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
598 // At this point we want to check if there are any memory
599 // blocks that aren't part of the wxDebugContext itself,
600 // as a special case. Then when dumping we need to ignore
601 // wxDebugContext, too.
602 if (wxDebugContext::CountObjectsLeft() > 0)
603 {
604 wxTrace("There were memory leaks.\n");
605 wxDebugContext::Dump();
606 wxDebugContext::PrintStatistics();
607 }
608 wxDebugContext::SetStream(NULL, NULL);
609 #endif
610
611 return retValue;
612 }
613
614 #else /* _WINDLL */
615
616 int wxEntry(WXHINSTANCE hInstance)
617 {
618 wxhInstance = (HINSTANCE) hInstance;
619 wxApp::Initialize((WXHINSTANCE) wxhInstance);
620
621 // The app may have declared a global application object, but we recommend
622 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
623 // for delayed, dynamic app object construction.
624
625 if (!wxTheApp)
626 {
627 if (!wxApp::GetInitializerFunction())
628 {
629 MessageBox(NULL, "No initializer - use IMPLEMENT_APP macro.", "wxWindows Error", MB_APPLMODAL | MB_ICONSTOP | MB_OK);
630 return 0;
631 }
632
633 wxTheApp = (* wxApp::GetInitializerFunction()) ();
634 }
635
636 if (!wxTheApp) {
637 MessageBox(NULL, "You have to define an instance of wxApp!", "wxWindows Error", MB_APPLMODAL | MB_ICONSTOP | MB_OK);
638 return 0;
639 }
640
641 wxTheApp->argc = 0;
642 wxTheApp->argv = NULL;
643
644 wxTheApp->OnInitGui();
645
646 wxTheApp->OnInit();
647
648 if (wxTheApp->GetTopWindow() && wxTheApp->GetTopWindow()->GetHWND()) {
649 wxTheApp->GetTopWindow()->Show(TRUE);
650 }
651
652 return 1;
653 }
654 #endif // _WINDLL
655
656 // Static member initialization
657 wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL;
658
659 wxApp::wxApp()
660 {
661 m_topWindow = NULL;
662 wxTheApp = this;
663 // work_proc = NULL ;
664 m_className = "";
665 // m_resourceCollection = TRUE;
666 // m_pendingCleanup = FALSE;
667 m_wantDebugOutput = TRUE ;
668 m_appName = "";
669 argc = 0;
670 argv = NULL;
671 #ifdef __WXMSW__
672 m_printMode = wxPRINT_WINDOWS;
673 #else
674 m_printMode = wxPRINT_POSTSCRIPT;
675 #endif
676 m_exitOnFrameDelete = TRUE;
677 m_auto3D = TRUE;
678 }
679
680 bool wxApp::Initialized()
681 {
682 #ifndef _WINDLL
683 if (GetTopWindow())
684 return TRUE;
685 else
686 return FALSE;
687 #endif
688 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
689 return TRUE;
690 #endif
691 }
692
693 /*
694 * Get and process a message, returning FALSE if WM_QUIT
695 * received.
696 *
697 */
698 bool wxApp::DoMessage()
699 {
700 if (!::GetMessage(&s_currentMsg, (HWND) NULL, 0, 0))
701 {
702 return FALSE;
703 }
704
705 // Process the message
706 if (!ProcessMessage((WXMSG *)&s_currentMsg))
707 {
708 ::TranslateMessage(&s_currentMsg);
709 wxApp::sm_lastMessageTime = s_currentMsg.time; /* MATTHEW: timeStamp impl. */
710 ::DispatchMessage(&s_currentMsg);
711 }
712 return TRUE;
713 }
714
715 /*
716 * Keep trying to process messages until WM_QUIT
717 * received.
718 *
719 * If there are messages to be processed, they will all be
720 * processed and OnIdle will not be called.
721 * When there are no more messages, OnIdle is called.
722 * If OnIdle requests more time,
723 * it will be repeatedly called so long as there are no pending messages.
724 * A 'feature' of this is that once OnIdle has decided that no more processing
725 * is required, then it won't get processing time until further messages
726 * are processed (it'll sit in DoMessage).
727 */
728
729 int wxApp::MainLoop()
730 {
731 m_keepGoing = TRUE;
732 while (m_keepGoing)
733 {
734 while (!::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) &&
735 ProcessIdle()) {}
736 if (!DoMessage())
737 m_keepGoing = FALSE;
738 }
739
740 return s_currentMsg.wParam;
741 }
742
743 // Returns TRUE if more time is needed.
744 bool wxApp::ProcessIdle()
745 {
746 wxIdleEvent event;
747 event.SetEventObject(this);
748 ProcessEvent(event);
749
750 return event.MoreRequested();
751 }
752
753 void wxApp::ExitMainLoop()
754 {
755 m_keepGoing = FALSE;
756 }
757
758 bool wxApp::Pending()
759 {
760 return (::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) != 0) ;
761 }
762
763 void wxApp::Dispatch()
764 {
765 if (!DoMessage())
766 m_keepGoing = FALSE;
767 }
768
769 /*
770 * Give all windows a chance to preprocess
771 * the message. Some may have accelerator tables, or have
772 * MDI complications.
773 */
774 bool wxApp::ProcessMessage(WXMSG *Msg)
775 {
776 MSG *msg = (MSG *)Msg;
777
778 HWND hWnd;
779
780 // Try translations first; find the youngest window with
781 // a translation table.
782 for (hWnd = msg->hwnd; hWnd != NULL; hWnd = ::GetParent(hWnd))
783 {
784 wxWindow *wnd = wxFindWinFromHandle((WXHWND) hWnd);
785 if (wnd)
786 {
787 if (wnd->MSWTranslateMessage(Msg))
788 return TRUE;
789 }
790 }
791
792 // Anyone for a non-translation message? Try youngest descendants first.
793 for (hWnd = msg->hwnd; hWnd != NULL; hWnd = ::GetParent(hWnd))
794 {
795 wxWindow *wnd = wxFindWinFromHandle((WXHWND) hWnd);
796 if (wnd)
797 {
798 if (wnd->MSWProcessMessage(Msg))
799 return TRUE;
800 }
801 }
802 return FALSE;
803 }
804
805 void wxApp::OnIdle(wxIdleEvent& event)
806 {
807 static bool inOnIdle = FALSE;
808
809 // Avoid recursion (via ProcessEvent default case)
810 if (inOnIdle)
811 return;
812
813 inOnIdle = TRUE;
814
815 // 'Garbage' collection of windows deleted with Close().
816 DeletePendingObjects();
817
818 // flush the logged messages if any
819 wxLog *pLog = wxLog::GetActiveTarget();
820 if ( pLog != NULL && pLog->HasPendingMessages() )
821 pLog->Flush();
822
823 // Send OnIdle events to all windows
824 bool needMore = SendIdleEvents();
825 // bool needMore = FALSE;
826
827 if (needMore)
828 event.RequestMore(TRUE);
829
830 inOnIdle = FALSE;
831 }
832
833 // Send idle event to all top-level windows
834 bool wxApp::SendIdleEvents()
835 {
836 bool needMore = FALSE;
837 wxNode* node = wxTopLevelWindows.First();
838 while (node)
839 {
840 wxWindow* win = (wxWindow*) node->Data();
841 if (SendIdleEvents(win))
842 needMore = TRUE;
843
844 node = node->Next();
845 }
846 return needMore;
847 }
848
849 // Send idle event to window and all subwindows
850 bool wxApp::SendIdleEvents(wxWindow* win)
851 {
852 bool needMore = FALSE;
853
854 wxIdleEvent event;
855 event.SetEventObject(win);
856 win->ProcessEvent(event);
857
858 if (event.MoreRequested())
859 needMore = TRUE;
860
861 wxNode* node = win->GetChildren()->First();
862 while (node)
863 {
864 wxWindow* win = (wxWindow*) node->Data();
865 if (SendIdleEvents(win))
866 needMore = TRUE;
867
868 node = node->Next();
869 }
870 return needMore ;
871 }
872
873 void wxApp::DeletePendingObjects()
874 {
875 wxNode *node = wxPendingDelete.First();
876 while (node)
877 {
878 wxObject *obj = (wxObject *)node->Data();
879
880 delete obj;
881
882 if (wxPendingDelete.Member(obj))
883 delete node;
884
885 // Deleting one object may have deleted other pending
886 // objects, so start from beginning of list again.
887 node = wxPendingDelete.First();
888 }
889 }
890
891 /*
892 // Free up font objects that are not being used at present.
893 bool wxApp::DoResourceCleanup()
894 {
895 // wxDebugMsg("ResourceCleanup\n");
896
897 if (wxTheFontList)
898 {
899 wxNode *node = wxTheFontList->First();
900 while (node)
901 {
902 wxGDIObject *obj = (wxGDIObject *)node->Data();
903 if ((obj->GetResourceHandle() != 0) && (obj->GetResourceUsage() == 0))
904 {
905 // wxDebugMsg("Freeing font %ld (GDI object %d)\n", (long)obj, (int)obj->GetResourceHandle());
906 obj->FreeResource();
907 }
908 node = node->Next();
909 }
910 }
911 if (wxThePenList)
912 {
913 wxNode *node = wxThePenList->First();
914 while (node)
915 {
916 wxGDIObject *obj = (wxGDIObject *)node->Data();
917 if ((obj->GetResourceHandle() != 0) && (obj->GetResourceUsage() == 0))
918 {
919 // wxDebugMsg("Freeing pen %ld (GDI object %d)\n", (long)obj, (int)obj->GetResourceHandle());
920 obj->FreeResource();
921 }
922 node = node->Next();
923 }
924 }
925 if (wxTheBrushList)
926 {
927 wxNode *node = wxTheBrushList->First();
928 while (node)
929 {
930 wxGDIObject *obj = (wxGDIObject *)node->Data();
931 if ((obj->GetResourceHandle() != 0) && (obj->GetResourceUsage() == 0))
932 {
933 // wxDebugMsg("Freeing brush %ld (GDI object %d)\n", (long)obj, (int)obj->GetResourceHandle());
934 obj->FreeResource();
935 }
936 node = node->Next();
937 }
938 }
939
940 SetPendingCleanup(FALSE);
941 return FALSE;
942 }
943 */
944
945 wxLog* wxApp::CreateLogTarget()
946 {
947 return new wxLogGui;
948 }
949
950 wxWindow* wxApp::GetTopWindow() const
951 {
952 if (m_topWindow)
953 return m_topWindow;
954 else if (wxTopLevelWindows.Number() > 0)
955 return (wxWindow*) wxTopLevelWindows.First()->Data();
956 else
957 return NULL;
958 }
959
960 int wxApp::GetComCtl32Version() const
961 {
962 // have we loaded COMCTL32 yet?
963 HMODULE theModule = ::GetModuleHandle("COMCTL32");
964 int version = 0;
965
966 // if so, then we can check for the version
967 if (theModule)
968 {
969 // InitCommonControlsEx is unique to 4.7 and later
970 FARPROC theProc = ::GetProcAddress(theModule, "InitCommonControlsEx");
971
972 if (! theProc)
973 { // not found, must be 4.00
974 version = 400;
975 }
976 else
977 {
978 // The following symbol are unique to 4.71
979 // DllInstall
980 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
981 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
982 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
983 // FlatSB_ShowScrollBar
984 // _DrawIndirectImageList _DuplicateImageList
985 // InitializeFlatSB
986 // UninitializeFlatSB
987 // we could check for any of these - I chose DllInstall
988 FARPROC theProc = ::GetProcAddress(theModule, "DllInstall");
989 if (! theProc)
990 {
991 // not found, must be 4.70
992 version = 470;
993 }
994 else
995 { // found, must be 4.71
996 version = 471;
997 }
998 }
999 }
1000 return version;
1001 }
1002
1003 void wxExit()
1004 {
1005 wxApp::CleanUp();
1006 FatalAppExit(0, "Fatal error: exiting");
1007 }
1008
1009 // Yield to incoming messages
1010 bool wxYield()
1011 {
1012 MSG msg;
1013 // We want to go back to the main message loop
1014 // if we see a WM_QUIT. (?)
1015 while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) && msg.message != WM_QUIT)
1016 {
1017 if (!wxTheApp->DoMessage())
1018 break;
1019 }
1020
1021 return TRUE;
1022 }
1023
1024 HINSTANCE wxGetInstance()
1025 {
1026 return wxhInstance;
1027 }
1028
1029 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1030 // if in a separate file. So include it here to ensure it's linked.
1031 #if (defined(_MSC_VER) && !defined(__WIN32__)) || defined(__GNUWIN32__)
1032 #include "main.cpp"
1033 #endif
1034
1035 #undef IN_WX_MAIN_CPP
1036