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