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