]> git.saurik.com Git - wxWidgets.git/blob - src/msw/app.cpp
fixed typo
[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 // do it as the very last thing because everything else can log messages
341 wxLog::DontCreateOnDemand();
342 delete wxLog::SetActiveTarget(NULL);
343 }
344
345 void wxApp::CommonInit()
346 {
347 #ifdef __WXMSW__
348 wxBuffer = new char[1500];
349 #else
350 wxBuffer = new char[BUFSIZ + 512];
351 #endif
352
353 wxClassInfo::InitializeClasses();
354
355 #if USE_RESOURCES
356 wxGetResource("wxWindows", "OsVersion", &wxOsVersion);
357 #endif
358
359 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
360 wxTheColourDatabase->Initialize();
361
362 wxInitializeStockLists();
363 wxInitializeStockObjects();
364
365 #if USE_WX_RESOURCES
366 wxInitializeResourceSystem();
367 #endif
368
369 // For PostScript printing
370 #if USE_POSTSCRIPT
371 wxInitializePrintSetupData();
372 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
373 wxThePrintPaperDatabase->CreateDatabase();
374 #endif
375
376 wxBitmap::InitStandardHandlers();
377
378 g_globalCursor = new wxCursor;
379 }
380
381 void wxApp::CommonCleanUp()
382 {
383 #if USE_WX_RESOURCES
384 wxCleanUpResourceSystem();
385
386 // wxDefaultResourceTable->ClearTable();
387 #endif
388
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 wxDeleteStockLists();
400
401 delete wxTheColourDatabase;
402 wxTheColourDatabase = NULL;
403
404 #if USE_POSTSCRIPT
405 wxInitializePrintSetupData(FALSE);
406 delete wxThePrintPaperDatabase;
407 wxThePrintPaperDatabase = NULL;
408 #endif
409
410 wxBitmap::CleanUpHandlers();
411
412 delete[] wxBuffer;
413 wxBuffer = NULL;
414 }
415
416 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
417
418 // Main wxWindows entry point
419
420 int wxEntry(WXHINSTANCE hInstance, WXHINSTANCE WXUNUSED(hPrevInstance), char *m_lpCmdLine,
421 int nCmdShow, bool enterLoop)
422 {
423 wxhInstance = (HINSTANCE) hInstance;
424
425 /* Doesn't work when using the makefiles, for some reason.
426 #if defined(__WXDEBUG__) && defined(_MSC_VER)
427 // do check for memory leaks on program exit
428 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
429 // deallocated memory which may be used to simulate low-memory condition)
430 _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
431 #endif // debug build under MS VC++
432 */
433
434 #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
435
436 #if !defined(_WINDLL)
437 streambuf* sBuf = new wxDebugStreamBuf;
438 #else
439 streambuf* sBuf = NULL;
440 #endif
441 ostream* oStr = new ostream(sBuf) ;
442 wxDebugContext::SetStream(oStr, sBuf);
443
444 #endif // USE_MEMORY_TRACING
445
446 if (!wxApp::Initialize((WXHINSTANCE) wxhInstance))
447 return 0;
448
449 // The app may have declared a global application object, but we recommend
450 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
451 // for delayed, dynamic app object construction.
452 if (!wxTheApp)
453 {
454 if (!wxApp::GetInitializerFunction())
455 {
456 MessageBox(NULL, "No initializer - use IMPLEMENT_APP macro.", "wxWindows Error", MB_APPLMODAL | MB_ICONSTOP | MB_OK);
457 return 0;
458 }
459
460 wxTheApp = (* wxApp::GetInitializerFunction()) ();
461 }
462
463 if (!wxTheApp) {
464 MessageBox(NULL, "You have to define an instance of wxApp!", "wxWindows Error", MB_APPLMODAL | MB_ICONSTOP | MB_OK);
465 return 0;
466 }
467
468 // Split command line into tokens, as in usual main(argc, argv)
469 char **command = new char*[50];
470
471 int count = 0;
472 char *buf = new char[strlen(m_lpCmdLine) + 1];
473
474 // Hangs around until end of app. in case
475 // user carries pointers to the tokens
476
477 /* Model independent strcpy */
478 int i;
479 for (i = 0; (buf[i] = m_lpCmdLine[i]) != 0; i++)
480 {
481 /* loop */;
482 }
483
484 // Get application name
485 char name[200];
486 ::GetModuleFileName(wxhInstance, name, 199);
487
488 // Is it only 16-bit Borland that already copies the program name
489 // to the first argv index?
490 #if !defined(__GNUWIN32__)
491 // #if ! (defined(__BORLANDC__) && !defined(__WIN32__))
492 command[count++] = copystring(name);
493 // #endif
494 #endif
495
496 strcpy(name, wxFileNameFromPath(name));
497 wxStripExtension(name);
498 wxTheApp->SetAppName(name);
499
500 /* Break up string */
501 // Treat strings enclosed in double-quotes as single arguments
502 char* str = buf;
503 while (*str)
504 {
505 while (*str && *str <= ' ') str++; // skip whitespace
506 if (*str == '"')
507 {
508 str++;
509 command[count++] = str;
510 while (*str && *str != '"') str++;
511 }
512 else if (*str)
513 {
514 command[count++] = str;
515 while (*str && *str > ' ') str++;
516 }
517 if (*str) *str++ = '\0';
518 }
519 command[count] = NULL; /* argv[] is NULL terminated list! */
520
521 wxTheApp->argc = count;
522 wxTheApp->argv = command;
523 wxTheApp->m_nCmdShow = nCmdShow;
524
525 // GUI-specific initialisation. In fact on Windows we don't have any,
526 // but this call is provided for compatibility across platforms.
527 wxTheApp->OnInitGui() ;
528
529 if (!wxTheApp->OnInit())
530 {
531 wxTheApp->DeletePendingObjects();
532 wxTheApp->OnExit();
533 wxApp::CleanUp();
534
535 delete wxTheApp;
536 wxTheApp = NULL;
537
538 delete [] buf ;
539
540 // TODO: This should really be cleaned up in ~wxApp
541 delete [] command[0] ;
542 delete [] command ;
543 return 0;
544 }
545
546 if (!enterLoop)
547 return 0;
548
549 int retValue = 1;
550
551 /* New behaviour - leave it to the app to show the top window
552 if (wxTheApp->GetTopWindow()) {
553 // show the toplevel frame, only if we are not iconized (from MS-Windows)
554 if(wxTheApp->GetShowFrameOnInit() && (nCmdShow!=SW_HIDE)) wxTheApp->GetTopWindow()->Show(TRUE);
555 }
556 */
557
558 retValue = wxTheApp->OnRun();
559
560 if (wxTheApp->GetTopWindow())
561 {
562 // Forcibly delete the window.
563 if (wxTheApp->GetTopWindow()->IsKindOf(CLASSINFO(wxFrame)) ||
564 wxTheApp->GetTopWindow()->IsKindOf(CLASSINFO(wxDialog)))
565 {
566 wxTheApp->GetTopWindow()->Close(TRUE);
567 wxTheApp->DeletePendingObjects();
568 }
569 else
570 {
571 delete wxTheApp->GetTopWindow();
572 wxTheApp->SetTopWindow(NULL);
573 }
574 }
575
576 wxTheApp->OnExit();
577 wxApp::CleanUp();
578
579 delete wxTheApp;
580 wxTheApp = NULL;
581
582 delete [] buf ;
583 delete [] command[0] ;
584 delete [] command ;
585
586 #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
587 // At this point we want to check if there are any memory
588 // blocks that aren't part of the wxDebugContext itself,
589 // as a special case. Then when dumping we need to ignore
590 // wxDebugContext, too.
591 if (wxDebugContext::CountObjectsLeft() > 0)
592 {
593 wxTrace("There were memory leaks.\n");
594 wxDebugContext::Dump();
595 wxDebugContext::PrintStatistics();
596 }
597 wxDebugContext::SetStream(NULL, NULL);
598 #endif
599
600 return retValue;
601 }
602
603 #else /* _WINDLL */
604
605 int wxEntry(WXHINSTANCE hInstance)
606 {
607 wxhInstance = (HINSTANCE) hInstance;
608 wxApp::Initialize((WXHINSTANCE) wxhInstance);
609
610 // The app may have declared a global application object, but we recommend
611 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
612 // for delayed, dynamic app object construction.
613
614 if (!wxTheApp)
615 {
616 if (!wxApp::GetInitializerFunction())
617 {
618 MessageBox(NULL, "No initializer - use IMPLEMENT_APP macro.", "wxWindows Error", MB_APPLMODAL | MB_ICONSTOP | MB_OK);
619 return 0;
620 }
621
622 wxTheApp = (* wxApp::GetInitializerFunction()) ();
623 }
624
625 if (!wxTheApp) {
626 MessageBox(NULL, "You have to define an instance of wxApp!", "wxWindows Error", MB_APPLMODAL | MB_ICONSTOP | MB_OK);
627 return 0;
628 }
629
630 wxTheApp->argc = 0;
631 wxTheApp->argv = NULL;
632
633 wxTheApp->OnInitGui();
634
635 wxTheApp->OnInit();
636
637 if (wxTheApp->GetTopWindow() && wxTheApp->GetTopWindow()->GetHWND()) {
638 wxTheApp->GetTopWindow()->Show(TRUE);
639 }
640
641 return 1;
642 }
643 #endif // _WINDLL
644
645 // Static member initialization
646 wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL;
647
648 wxApp::wxApp()
649 {
650 m_topWindow = NULL;
651 wxTheApp = this;
652 // work_proc = NULL ;
653 m_className = "";
654 // m_resourceCollection = TRUE;
655 // m_pendingCleanup = FALSE;
656 m_wantDebugOutput = TRUE ;
657 m_appName = "";
658 argc = 0;
659 argv = NULL;
660 #ifdef __WXMSW__
661 m_printMode = wxPRINT_WINDOWS;
662 #else
663 m_printMode = wxPRINT_POSTSCRIPT;
664 #endif
665 m_exitOnFrameDelete = TRUE;
666 m_auto3D = TRUE;
667 }
668
669 bool wxApp::Initialized()
670 {
671 #ifndef _WINDLL
672 if (GetTopWindow())
673 return TRUE;
674 else
675 return FALSE;
676 #endif
677 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
678 return TRUE;
679 #endif
680 }
681
682 /*
683 * Get and process a message, returning FALSE if WM_QUIT
684 * received.
685 *
686 */
687 bool wxApp::DoMessage()
688 {
689 if (!::GetMessage(&s_currentMsg, (HWND) NULL, 0, 0))
690 {
691 return FALSE;
692 }
693
694 // Process the message
695 if (!ProcessMessage((WXMSG *)&s_currentMsg))
696 {
697 ::TranslateMessage(&s_currentMsg);
698 wxApp::sm_lastMessageTime = s_currentMsg.time; /* MATTHEW: timeStamp impl. */
699 ::DispatchMessage(&s_currentMsg);
700 }
701 return TRUE;
702 }
703
704 /*
705 * Keep trying to process messages until WM_QUIT
706 * received.
707 *
708 * If there are messages to be processed, they will all be
709 * processed and OnIdle will not be called.
710 * When there are no more messages, OnIdle is called.
711 * If OnIdle requests more time,
712 * it will be repeatedly called so long as there are no pending messages.
713 * A 'feature' of this is that once OnIdle has decided that no more processing
714 * is required, then it won't get processing time until further messages
715 * are processed (it'll sit in DoMessage).
716 */
717
718 int wxApp::MainLoop()
719 {
720 m_keepGoing = TRUE;
721 while (m_keepGoing)
722 {
723 while (!::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) &&
724 ProcessIdle()) {}
725 if (!DoMessage())
726 m_keepGoing = FALSE;
727 }
728
729 return s_currentMsg.wParam;
730 }
731
732 // Returns TRUE if more time is needed.
733 bool wxApp::ProcessIdle()
734 {
735 wxIdleEvent event;
736 event.SetEventObject(this);
737 ProcessEvent(event);
738
739 return event.MoreRequested();
740 }
741
742 void wxApp::ExitMainLoop()
743 {
744 m_keepGoing = FALSE;
745 }
746
747 bool wxApp::Pending()
748 {
749 return (::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) != 0) ;
750 }
751
752 void wxApp::Dispatch()
753 {
754 if (!DoMessage())
755 m_keepGoing = FALSE;
756 }
757
758 /*
759 * Give all windows a chance to preprocess
760 * the message. Some may have accelerator tables, or have
761 * MDI complications.
762 */
763 bool wxApp::ProcessMessage(WXMSG *Msg)
764 {
765 MSG *msg = (MSG *)Msg;
766
767 HWND hWnd;
768
769 // Try translations first; find the youngest window with
770 // a translation table.
771 for (hWnd = msg->hwnd; hWnd != NULL; hWnd = ::GetParent(hWnd))
772 {
773 wxWindow *wnd = wxFindWinFromHandle((WXHWND) hWnd);
774 if (wnd)
775 {
776 if (wnd->MSWTranslateMessage(Msg))
777 return TRUE;
778 }
779 }
780
781 // Anyone for a non-translation message? Try youngest descendants first.
782 for (hWnd = msg->hwnd; hWnd != NULL; hWnd = ::GetParent(hWnd))
783 {
784 wxWindow *wnd = wxFindWinFromHandle((WXHWND) hWnd);
785 if (wnd)
786 {
787 if (wnd->MSWProcessMessage(Msg))
788 return TRUE;
789 }
790 }
791 return FALSE;
792 }
793
794 void wxApp::OnIdle(wxIdleEvent& event)
795 {
796 static bool inOnIdle = FALSE;
797
798 // Avoid recursion (via ProcessEvent default case)
799 if (inOnIdle)
800 return;
801
802 inOnIdle = TRUE;
803
804 // 'Garbage' collection of windows deleted with Close().
805 DeletePendingObjects();
806
807 // flush the logged messages if any
808 wxLog *pLog = wxLog::GetActiveTarget();
809 if ( pLog != NULL && pLog->HasPendingMessages() )
810 pLog->Flush();
811
812 // Send OnIdle events to all windows
813 bool needMore = SendIdleEvents();
814 // bool needMore = FALSE;
815
816 if (needMore)
817 event.RequestMore(TRUE);
818
819 inOnIdle = FALSE;
820 }
821
822 // Send idle event to all top-level windows
823 bool wxApp::SendIdleEvents()
824 {
825 bool needMore = FALSE;
826 wxNode* node = wxTopLevelWindows.First();
827 while (node)
828 {
829 wxWindow* win = (wxWindow*) node->Data();
830 if (SendIdleEvents(win))
831 needMore = TRUE;
832
833 node = node->Next();
834 }
835 return needMore;
836 }
837
838 // Send idle event to window and all subwindows
839 bool wxApp::SendIdleEvents(wxWindow* win)
840 {
841 bool needMore = FALSE;
842
843 wxIdleEvent event;
844 event.SetEventObject(win);
845 win->ProcessEvent(event);
846
847 if (event.MoreRequested())
848 needMore = TRUE;
849
850 wxNode* node = win->GetChildren()->First();
851 while (node)
852 {
853 wxWindow* win = (wxWindow*) node->Data();
854 if (SendIdleEvents(win))
855 needMore = TRUE;
856
857 node = node->Next();
858 }
859 return needMore ;
860 }
861
862 void wxApp::DeletePendingObjects()
863 {
864 wxNode *node = wxPendingDelete.First();
865 while (node)
866 {
867 wxObject *obj = (wxObject *)node->Data();
868
869 delete obj;
870
871 if (wxPendingDelete.Member(obj))
872 delete node;
873
874 // Deleting one object may have deleted other pending
875 // objects, so start from beginning of list again.
876 node = wxPendingDelete.First();
877 }
878 }
879
880 /*
881 // Free up font objects that are not being used at present.
882 bool wxApp::DoResourceCleanup()
883 {
884 // wxDebugMsg("ResourceCleanup\n");
885
886 if (wxTheFontList)
887 {
888 wxNode *node = wxTheFontList->First();
889 while (node)
890 {
891 wxGDIObject *obj = (wxGDIObject *)node->Data();
892 if ((obj->GetResourceHandle() != 0) && (obj->GetResourceUsage() == 0))
893 {
894 // wxDebugMsg("Freeing font %ld (GDI object %d)\n", (long)obj, (int)obj->GetResourceHandle());
895 obj->FreeResource();
896 }
897 node = node->Next();
898 }
899 }
900 if (wxThePenList)
901 {
902 wxNode *node = wxThePenList->First();
903 while (node)
904 {
905 wxGDIObject *obj = (wxGDIObject *)node->Data();
906 if ((obj->GetResourceHandle() != 0) && (obj->GetResourceUsage() == 0))
907 {
908 // wxDebugMsg("Freeing pen %ld (GDI object %d)\n", (long)obj, (int)obj->GetResourceHandle());
909 obj->FreeResource();
910 }
911 node = node->Next();
912 }
913 }
914 if (wxTheBrushList)
915 {
916 wxNode *node = wxTheBrushList->First();
917 while (node)
918 {
919 wxGDIObject *obj = (wxGDIObject *)node->Data();
920 if ((obj->GetResourceHandle() != 0) && (obj->GetResourceUsage() == 0))
921 {
922 // wxDebugMsg("Freeing brush %ld (GDI object %d)\n", (long)obj, (int)obj->GetResourceHandle());
923 obj->FreeResource();
924 }
925 node = node->Next();
926 }
927 }
928
929 SetPendingCleanup(FALSE);
930 return FALSE;
931 }
932 */
933
934 wxLog* wxApp::CreateLogTarget()
935 {
936 return new wxLogGui;
937 }
938
939 wxWindow* wxApp::GetTopWindow() const
940 {
941 if (m_topWindow)
942 return m_topWindow;
943 else if (wxTopLevelWindows.Number() > 0)
944 return (wxWindow*) wxTopLevelWindows.First()->Data();
945 else
946 return NULL;
947 }
948
949 int wxApp::GetComCtl32Version() const
950 {
951 // have we loaded COMCTL32 yet?
952 HMODULE theModule = ::GetModuleHandle("COMCTL32");
953 int version = 0;
954
955 // if so, then we can check for the version
956 if (theModule)
957 {
958 // InitCommonControlsEx is unique to 4.7 and later
959 FARPROC theProc = ::GetProcAddress(theModule, "InitCommonControlsEx");
960
961 if (! theProc)
962 { // not found, must be 4.00
963 version = 400;
964 }
965 else
966 {
967 // The following symbol are unique to 4.71
968 // DllInstall
969 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
970 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
971 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
972 // FlatSB_ShowScrollBar
973 // _DrawIndirectImageList _DuplicateImageList
974 // InitializeFlatSB
975 // UninitializeFlatSB
976 // we could check for any of these - I chose DllInstall
977 FARPROC theProc = ::GetProcAddress(theModule, "DllInstall");
978 if (! theProc)
979 {
980 // not found, must be 4.70
981 version = 470;
982 }
983 else
984 { // found, must be 4.71
985 version = 471;
986 }
987 }
988 }
989 return version;
990 }
991
992 void wxExit()
993 {
994 wxApp::CleanUp();
995 FatalAppExit(0, "Fatal error: exiting");
996 }
997
998 // Yield to incoming messages
999 bool wxYield()
1000 {
1001 MSG msg;
1002 // We want to go back to the main message loop
1003 // if we see a WM_QUIT. (?)
1004 while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) && msg.message != WM_QUIT)
1005 {
1006 if (!wxTheApp->DoMessage())
1007 break;
1008 }
1009
1010 return TRUE;
1011 }
1012
1013 HINSTANCE wxGetInstance()
1014 {
1015 return wxhInstance;
1016 }
1017
1018 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1019 // if in a separate file. So include it here to ensure it's linked.
1020 #if (defined(_MSC_VER) && !defined(__WIN32__)) || defined(__GNUWIN32__)
1021 #include "main.cpp"
1022 #endif
1023