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