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