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