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