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