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