]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: app.cpp | |
3 | // Purpose: wxApp | |
4 | // Author: David Webster | |
5 | // Modified by: | |
6 | // Created: 10/13/99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) David Webster | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifndef WX_PRECOMP | |
16 | #include "wx/frame.h" | |
17 | #include "wx/app.h" | |
18 | #include "wx/utils.h" | |
19 | #include "wx/gdicmn.h" | |
20 | #include "wx/pen.h" | |
21 | #include "wx/brush.h" | |
22 | #include "wx/cursor.h" | |
23 | #include "wx/icon.h" | |
24 | #include "wx/palette.h" | |
25 | #include "wx/dc.h" | |
26 | #include "wx/dialog.h" | |
27 | #include "wx/msgdlg.h" | |
28 | #include "wx/intl.h" | |
29 | #include "wx/dynarray.h" | |
30 | # include "wx/wxchar.h" | |
31 | # include "wx/icon.h" | |
32 | #endif | |
33 | ||
34 | #include "wx/log.h" | |
35 | #include "wx/module.h" | |
36 | ||
37 | #include "wx/os2/private.h" | |
38 | ||
39 | #if wxUSE_THREADS | |
40 | #include "wx/thread.h" | |
41 | ||
42 | // define the array of QMSG strutures | |
43 | WX_DECLARE_OBJARRAY(QMSG, wxMsgArray); | |
44 | ||
45 | #include "wx/arrimpl.cpp" | |
46 | ||
47 | WX_DEFINE_OBJARRAY(wxMsgArray); | |
48 | #endif // wxUSE_THREADS | |
49 | ||
50 | #if wxUSE_WX_RESOURCES | |
51 | #include "wx/resource.h" | |
52 | #endif | |
53 | ||
54 | #include <string.h> | |
55 | #include <ctype.h> | |
56 | ||
57 | // --------------------------------------------------------------------------- | |
58 | // global variables | |
59 | // --------------------------------------------------------------------------- | |
60 | ||
61 | extern wxChar* wxBuffer; | |
62 | extern wxChar* wxOsVersion; | |
63 | extern wxList* wxWinHandleList; | |
64 | extern wxList WXDLLEXPORT wxPendingDelete; | |
65 | extern void wxSetKeyboardHook(bool doIt); | |
66 | extern wxCursor* g_globalCursor; | |
67 | ||
68 | HINSTANCE wxhInstance = 0; | |
69 | QMSG svCurrentMsg; | |
70 | wxApp* wxTheApp = NULL; | |
71 | ||
72 | // FIXME why not const? and not static? | |
73 | ||
74 | // NB: all "NoRedraw" classes must have the same names as the "normal" classes | |
75 | // with NR suffix - wxWindow::OS2Create() supposes this | |
76 | wxChar wxFrameClassName[] = wxT("wxFrameClass"); | |
77 | wxChar wxFrameClassNameNoRedraw[] = wxT("wxFrameClassNR"); | |
78 | wxChar wxMDIFrameClassName[] = wxT("wxMDIFrameClass"); | |
79 | wxChar wxMDIFrameClassNameNoRedraw[] = wxT("wxMDIFrameClassNR"); | |
80 | wxChar wxMDIChildFrameClassName[] = wxT("wxMDIChildFrameClass"); | |
81 | wxChar wxMDIChildFrameClassNameNoRedraw[] = wxT("wxMDIChildFrameClassNR"); | |
82 | wxChar wxPanelClassName[] = wxT("wxPanelClass"); | |
83 | wxChar wxCanvasClassName[] = wxT("wxCanvasClass"); | |
84 | ||
85 | HICON wxSTD_FRAME_ICON = (HICON) NULL; | |
86 | HICON wxSTD_MDICHILDFRAME_ICON = (HICON) NULL; | |
87 | HICON wxSTD_MDIPARENTFRAME_ICON = (HICON) NULL; | |
88 | ||
89 | HICON wxDEFAULT_FRAME_ICON = (HICON) NULL; | |
90 | HICON wxDEFAULT_MDICHILDFRAME_ICON = (HICON) NULL; | |
91 | HICON wxDEFAULT_MDIPARENTFRAME_ICON = (HICON) NULL; | |
92 | ||
93 | HBRUSH wxDisableButtonBrush = (HBRUSH) 0; | |
94 | ||
95 | MRESULT wxWndProc(HWND, UINT, MPARAM, MPARAM); | |
96 | ||
97 | // =========================================================================== | |
98 | // implementation | |
99 | // =========================================================================== | |
100 | ||
101 | // --------------------------------------------------------------------------- | |
102 | // wxApp | |
103 | // --------------------------------------------------------------------------- | |
104 | ||
105 | #if !USE_SHARED_LIBRARY | |
106 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) | |
107 | ||
108 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) | |
109 | EVT_IDLE(wxApp::OnIdle) | |
110 | EVT_END_SESSION(wxApp::OnEndSession) | |
111 | EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession) | |
112 | END_EVENT_TABLE() | |
113 | #endif | |
114 | ||
115 | //// Initialize | |
116 | bool wxApp::Initialize( | |
117 | HAB vHab | |
118 | ) | |
119 | { | |
120 | // Some people may wish to use this, but | |
121 | // probably it shouldn't be here by default. | |
122 | #ifdef __WXDEBUG__ | |
123 | // wxRedirectIOToConsole(); | |
124 | #endif | |
125 | ||
126 | wxBuffer = new wxChar[1500]; // FIXME; why? | |
127 | ||
128 | wxClassInfo::InitializeClasses(); | |
129 | ||
130 | #if wxUSE_RESOURCES | |
131 | wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion); | |
132 | #endif | |
133 | ||
134 | // I'm annoyed ... I don't know where to put this and I don't want to | |
135 | // create a module for that as it's part of the core. | |
136 | wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING); | |
137 | wxTheColourDatabase->Initialize(); | |
138 | ||
139 | wxInitializeStockLists(); | |
140 | wxInitializeStockObjects(); | |
141 | ||
142 | #if wxUSE_WX_RESOURCES | |
143 | wxInitializeResourceSystem(); | |
144 | #endif | |
145 | ||
146 | wxBitmap::InitStandardHandlers(); | |
147 | ||
148 | g_globalCursor = new wxCursor; | |
149 | ||
150 | #if 0 | |
151 | wxSTD_FRAME_ICON = ::WinLoadFileIcon(wxT("wxSTD_FRAME"), TRUE); | |
152 | wxSTD_MDIPARENTFRAME_ICON = ::WinLoadFileIcon(wxT("wxSTD_MDIPARENTFRAME"), TRUE); | |
153 | wxSTD_MDICHILDFRAME_ICON = ::WinLoadFileIcon(wxT("wxSTD_MDICHILDFRAME"), TRUE); | |
154 | ||
155 | wxDEFAULT_FRAME_ICON = ::WinLoadFileIcon(wxT("wxDEFAULT_FRAME"), TRUE); | |
156 | wxDEFAULT_MDIPARENTFRAME_ICON = ::WinLoadFileIcon(wxT("wxDEFAULT_MDIPARENTFRAME"), TRUE); | |
157 | wxDEFAULT_MDICHILDFRAME_ICON = ::WinLoadFileIcon(wxT("wxDEFAULT_MDICHILDFRAME"), TRUE); | |
158 | #endif | |
159 | RegisterWindowClasses(vHab); | |
160 | wxWinHandleList = new wxList(wxKEY_INTEGER); | |
161 | ||
162 | // This is to foil optimizations in Visual C++ that throw out dummy.obj. | |
163 | // PLEASE DO NOT ALTER THIS. | |
164 | #if !defined(WXMAKINGDLL) | |
165 | extern char wxDummyChar; | |
166 | if (wxDummyChar) wxDummyChar++; | |
167 | #endif | |
168 | ||
169 | wxSetKeyboardHook(TRUE); | |
170 | ||
171 | wxModule::RegisterModules(); | |
172 | if (!wxModule::InitializeModules()) | |
173 | return FALSE; | |
174 | return TRUE; | |
175 | } | |
176 | ||
177 | // --------------------------------------------------------------------------- | |
178 | // RegisterWindowClasses | |
179 | // --------------------------------------------------------------------------- | |
180 | ||
181 | // TODO we should only register classes really used by the app. For this it | |
182 | // would be enough to just delay the class registration until an attempt | |
183 | // to create a window of this class is made. | |
184 | bool wxApp::RegisterWindowClasses( | |
185 | HAB vHab | |
186 | ) | |
187 | { | |
188 | APIRET rc; | |
189 | ||
190 | if ((rc = ::WinRegisterClass( vHab | |
191 | ,wxFrameClassName | |
192 | ,(PFNWP)wxWndProc | |
193 | ,CS_SIZEREDRAW | CS_SYNCPAINT | CS_HITTEST | CS_CLIPCHILDREN | CS_FRAME | |
194 | ,0 | |
195 | )) != 0) | |
196 | { | |
197 | wxLogLastError("RegisterClass(frame)"); | |
198 | ||
199 | return FALSE; | |
200 | } | |
201 | ||
202 | if (!::WinRegisterClass( vHab | |
203 | ,wxFrameClassNameNoRedraw | |
204 | ,(PFNWP)wxWndProc | |
205 | ,CS_HITTEST | CS_CLIPCHILDREN | CS_FRAME | |
206 | ,0 | |
207 | )) | |
208 | { | |
209 | wxLogLastError("RegisterClass(no redraw frame)"); | |
210 | ||
211 | return FALSE; | |
212 | } | |
213 | ||
214 | if (!::WinRegisterClass( vHab | |
215 | ,wxMDIFrameClassName | |
216 | ,(PFNWP)wxWndProc | |
217 | ,CS_SIZEREDRAW | CS_SYNCPAINT | CS_HITTEST | CS_CLIPCHILDREN | CS_FRAME | |
218 | ,0 | |
219 | )) | |
220 | { | |
221 | wxLogLastError("RegisterClass(MDI parent)"); | |
222 | ||
223 | return FALSE; | |
224 | } | |
225 | ||
226 | if (!::WinRegisterClass( vHab | |
227 | ,wxMDIFrameClassNameNoRedraw | |
228 | ,(PFNWP)wxWndProc | |
229 | ,CS_HITTEST | CS_CLIPCHILDREN | CS_FRAME | |
230 | ,0 | |
231 | )) | |
232 | { | |
233 | wxLogLastError("RegisterClass(no redraw MDI parent)"); | |
234 | ||
235 | return FALSE; | |
236 | } | |
237 | ||
238 | if (!::WinRegisterClass( vHab | |
239 | ,wxMDIChildFrameClassName | |
240 | ,(PFNWP)wxWndProc | |
241 | ,CS_MOVENOTIFY | CS_SIZEREDRAW | CS_SYNCPAINT | CS_HITTEST | CS_CLIPSIBLINGS | CS_FRAME | |
242 | ,0 | |
243 | )) | |
244 | { | |
245 | wxLogLastError("RegisterClass(MDI child)"); | |
246 | ||
247 | return FALSE; | |
248 | } | |
249 | ||
250 | if (!::WinRegisterClass( vHab | |
251 | ,wxMDIChildFrameClassNameNoRedraw | |
252 | ,(PFNWP)wxWndProc | |
253 | ,CS_HITTEST | CS_CLIPSIBLINGS | CS_FRAME | |
254 | ,0 | |
255 | )) | |
256 | { | |
257 | wxLogLastError("RegisterClass(no redraw MDI child)"); | |
258 | ||
259 | return FALSE; | |
260 | } | |
261 | ||
262 | if (!::WinRegisterClass( vHab | |
263 | ,wxPanelClassName | |
264 | ,(PFNWP)wxWndProc | |
265 | ,CS_MOVENOTIFY | CS_SIZEREDRAW | CS_HITTEST | CS_CLIPSIBLINGS | CS_SAVEBITS | CS_SYNCPAINT | |
266 | ,0 | |
267 | )) | |
268 | { | |
269 | wxLogLastError("RegisterClass(Panel)"); | |
270 | ||
271 | return FALSE; | |
272 | } | |
273 | ||
274 | if (!::WinRegisterClass( vHab | |
275 | ,wxCanvasClassName | |
276 | ,(PFNWP)wxWndProc | |
277 | ,CS_MOVENOTIFY | CS_SIZEREDRAW | CS_HITTEST | CS_CLIPSIBLINGS | CS_SAVEBITS | CS_SYNCPAINT | |
278 | ,0 | |
279 | )) | |
280 | { | |
281 | wxLogLastError("RegisterClass(Canvas)"); | |
282 | ||
283 | return FALSE; | |
284 | } | |
285 | return TRUE; | |
286 | } | |
287 | ||
288 | //// Cleans up any wxWindows internal structures left lying around | |
289 | ||
290 | void wxApp::CleanUp() | |
291 | { | |
292 | //// COMMON CLEANUP | |
293 | ||
294 | #if wxUSE_LOG | |
295 | // flush the logged messages if any and install a 'safer' log target: the | |
296 | // default one (wxLogGui) can't be used after the resources are freed just | |
297 | // below and the user suppliedo ne might be even more unsafe (using any | |
298 | // wxWindows GUI function is unsafe starting from now) | |
299 | wxLog::DontCreateOnDemand(); | |
300 | ||
301 | // this will flush the old messages if any | |
302 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
303 | #endif // wxUSE_LOG | |
304 | ||
305 | // One last chance for pending objects to be cleaned up | |
306 | wxTheApp->DeletePendingObjects(); | |
307 | ||
308 | wxModule::CleanUpModules(); | |
309 | ||
310 | #if wxUSE_WX_RESOURCES | |
311 | wxCleanUpResourceSystem(); | |
312 | ||
313 | // wxDefaultResourceTable->ClearTable(); | |
314 | #endif | |
315 | ||
316 | // Indicate that the cursor can be freed, so that cursor won't be deleted | |
317 | // by deleting the bitmap list before g_globalCursor goes out of scope | |
318 | // (double deletion of the cursor). | |
319 | wxSetCursor(wxNullCursor); | |
320 | delete g_globalCursor; | |
321 | g_globalCursor = NULL; | |
322 | ||
323 | wxDeleteStockObjects(); | |
324 | ||
325 | // Destroy all GDI lists, etc. | |
326 | wxDeleteStockLists(); | |
327 | ||
328 | delete wxTheColourDatabase; | |
329 | wxTheColourDatabase = NULL; | |
330 | ||
331 | wxBitmap::CleanUpHandlers(); | |
332 | ||
333 | delete[] wxBuffer; | |
334 | wxBuffer = NULL; | |
335 | ||
336 | //// WINDOWS-SPECIFIC CLEANUP | |
337 | ||
338 | wxSetKeyboardHook(FALSE); | |
339 | ||
340 | if (wxSTD_FRAME_ICON) | |
341 | ::WinFreeFileIcon(wxSTD_FRAME_ICON); | |
342 | if (wxSTD_MDICHILDFRAME_ICON) | |
343 | ::WinFreeFileIcon(wxSTD_MDICHILDFRAME_ICON); | |
344 | if (wxSTD_MDIPARENTFRAME_ICON) | |
345 | ::WinFreeFileIcon(wxSTD_MDIPARENTFRAME_ICON); | |
346 | ||
347 | if (wxDEFAULT_FRAME_ICON) | |
348 | ::WinFreeFileIcon(wxDEFAULT_FRAME_ICON); | |
349 | if (wxDEFAULT_MDICHILDFRAME_ICON) | |
350 | ::WinFreeFileIcon(wxDEFAULT_MDICHILDFRAME_ICON); | |
351 | if (wxDEFAULT_MDIPARENTFRAME_ICON) | |
352 | ::WinFreeFileIcon(wxDEFAULT_MDIPARENTFRAME_ICON); | |
353 | ||
354 | if ( wxDisableButtonBrush ) | |
355 | { | |
356 | // TODO: ::DeleteObject( wxDisableButtonBrush ); | |
357 | } | |
358 | ||
359 | if (wxWinHandleList) | |
360 | delete wxWinHandleList; | |
361 | ||
362 | // GL: I'm annoyed ... I don't know where to put this and I don't want to | |
363 | // create a module for that as it's part of the core. | |
364 | #if wxUSE_THREADS | |
365 | delete wxPendingEvents; | |
366 | delete wxPendingEventsLocker; | |
367 | // If we don't do the following, we get an apparent memory leak. | |
368 | ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker(); | |
369 | #endif | |
370 | ||
371 | wxClassInfo::CleanUpClasses(); | |
372 | ||
373 | delete wxTheApp; | |
374 | wxTheApp = NULL; | |
375 | ||
376 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT | |
377 | // At this point we want to check if there are any memory | |
378 | // blocks that aren't part of the wxDebugContext itself, | |
379 | // as a special case. Then when dumping we need to ignore | |
380 | // wxDebugContext, too. | |
381 | if (wxDebugContext::CountObjectsLeft(TRUE) > 0) | |
382 | { | |
383 | wxLogDebug(wxT("There were memory leaks.")); | |
384 | wxDebugContext::Dump(); | |
385 | wxDebugContext::PrintStatistics(); | |
386 | } | |
387 | // wxDebugContext::SetStream(NULL, NULL); | |
388 | #endif | |
389 | ||
390 | #if wxUSE_LOG | |
391 | // do it as the very last thing because everything else can log messages | |
392 | delete wxLog::SetActiveTarget(NULL); | |
393 | #endif // wxUSE_LOG | |
394 | } | |
395 | ||
396 | int wxEntry( | |
397 | int argc | |
398 | , char* argv[] | |
399 | ) | |
400 | { | |
401 | HAB vHab; | |
402 | ||
403 | if (!wxApp::Initialize(vHab)) | |
404 | return 0; | |
405 | ||
406 | // | |
407 | // create the application object or ensure that one already exists | |
408 | // | |
409 | if (!wxTheApp) | |
410 | { | |
411 | wxCHECK_MSG( wxApp::GetInitializerFunction() | |
412 | ,-1 | |
413 | ,wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") | |
414 | ); | |
415 | ||
416 | wxAppInitializerFunction fnAppIni = wxApp::GetInitializerFunction(); | |
417 | wxObject* pTest_app = fnAppIni(); | |
418 | ||
419 | wxTheApp = (wxApp*)pTest_app; | |
420 | } | |
421 | wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") ); | |
422 | wxTheApp->argc = argc; | |
423 | ||
424 | #if wxUSE_UNICODE | |
425 | wxTheApp->argv = new wxChar*[argc+1]; | |
426 | ||
427 | int nArgc = 0; | |
428 | ||
429 | while (nArgc < argc) | |
430 | { | |
431 | wxTheApp->argv[nArgc] = wxStrdup(wxConvLibc.cMB2WX(argv[nArgc])); | |
432 | nArgc++; | |
433 | } | |
434 | wxTheApp->argv[nArgc] = (wxChar *)NULL; | |
435 | #else | |
436 | wxTheApp->argv = argv; | |
437 | #endif | |
438 | ||
439 | wxString sName(wxFileNameFromPath(argv[0])); | |
440 | ||
441 | wxStripExtension(sName); | |
442 | wxTheApp->SetAppName(sName); | |
443 | ||
444 | int nRetValue = 0; | |
445 | ||
446 | if (!wxTheApp->OnInitGui()) | |
447 | nRetValue = -1; | |
448 | ||
449 | if (nRetValue == 0) | |
450 | { | |
451 | if (wxTheApp->OnInit()) | |
452 | { | |
453 | nRetValue = -1; | |
454 | } | |
455 | } | |
456 | ||
457 | if (nRetValue == 0) | |
458 | { | |
459 | wxWindow* pTopWindow = wxTheApp->GetTopWindow(); | |
460 | ||
461 | if (pTopWindow) | |
462 | { | |
463 | // Forcibly delete the window. | |
464 | if (pTopWindow->IsKindOf(CLASSINFO(wxFrame)) || | |
465 | pTopWindow->IsKindOf(CLASSINFO(wxDialog)) ) | |
466 | { | |
467 | pTopWindow->Close(TRUE); | |
468 | wxTheApp->DeletePendingObjects(); | |
469 | } | |
470 | else | |
471 | { | |
472 | delete pTopWindow; | |
473 | wxTheApp->SetTopWindow(NULL); | |
474 | } | |
475 | } | |
476 | } | |
477 | wxTheApp->OnExit(); | |
478 | wxApp::CleanUp(); | |
479 | return(nRetValue); | |
480 | } | |
481 | ||
482 | bool wxApp::OnInitGui() | |
483 | { | |
484 | m_vHab = WinInitialize(0); | |
485 | m_hMq = WinCreateMsgQueue(m_vHab, 0); | |
486 | ||
487 | return TRUE; | |
488 | } | |
489 | ||
490 | // | |
491 | // Static member initialization | |
492 | // | |
493 | wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL; | |
494 | ||
495 | wxApp::wxApp() | |
496 | { | |
497 | m_topWindow = NULL; | |
498 | wxTheApp = this; | |
499 | m_wantDebugOutput = TRUE; | |
500 | ||
501 | argc = 0; | |
502 | argv = NULL; | |
503 | m_nPrintMode = wxPRINT_WINDOWS; | |
504 | m_exitOnFrameDelete = TRUE; | |
505 | m_bAuto3D = TRUE; | |
506 | } | |
507 | ||
508 | wxApp::~wxApp() | |
509 | { | |
510 | // Delete command-line args | |
511 | int i; | |
512 | for (i = 0; i < argc; i++) | |
513 | { | |
514 | delete[] argv[i]; | |
515 | } | |
516 | delete[] argv; | |
517 | } | |
518 | ||
519 | bool wxApp::Initialized() | |
520 | { | |
521 | if (GetTopWindow()) | |
522 | return TRUE; | |
523 | else | |
524 | return FALSE; | |
525 | } | |
526 | ||
527 | // | |
528 | // Get and process a message, returning FALSE if WM_QUIT | |
529 | // received (and also set the flag telling the app to exit the main loop) | |
530 | // | |
531 | bool wxApp::DoMessage() | |
532 | { | |
533 | BOOL bRc = ::WinGetMsg(m_vHab, &m_vMsg, HWND(NULL), 0, 0); | |
534 | ||
535 | if (bRc == 0) | |
536 | { | |
537 | // got WM_QUIT | |
538 | m_bKeepGoing = FALSE; | |
539 | return FALSE; | |
540 | } | |
541 | else if (bRc == -1) | |
542 | { | |
543 | // should never happen, but let's test for it nevertheless | |
544 | wxLogLastError("GetMessage"); | |
545 | } | |
546 | else | |
547 | { | |
548 | #if wxUSE_THREADS | |
549 | wxASSERT_MSG( wxThread::IsMain() | |
550 | ,wxT("only the main thread can process Windows messages") | |
551 | ); | |
552 | ||
553 | static bool sbHadGuiLock = TRUE; | |
554 | static wxMsgArray svSavedMessages; | |
555 | ||
556 | // | |
557 | // if a secondary thread owns is doing GUI calls, save all messages for | |
558 | // later processing - we can't process them right now because it will | |
559 | // lead to recursive library calls (and we're not reentrant) | |
560 | // | |
561 | if (!wxGuiOwnedByMainThread()) | |
562 | { | |
563 | sbHadGuiLock = FALSE; | |
564 | ||
565 | // leave out WM_COMMAND messages: too dangerous, sometimes | |
566 | // the message will be processed twice | |
567 | if ( !wxIsWaitingForThread() || | |
568 | svCurrentMsg.msg != WM_COMMAND ) | |
569 | { | |
570 | svSavedMessages.Add(svCurrentMsg); | |
571 | } | |
572 | return TRUE; | |
573 | } | |
574 | else | |
575 | { | |
576 | // | |
577 | // have we just regained the GUI lock? if so, post all of the saved | |
578 | // messages | |
579 | // | |
580 | // FIXME of course, it's not _exactly_ the same as processing the | |
581 | // messages normally - expect some things to break... | |
582 | // | |
583 | if (!sbHadGuiLock ) | |
584 | { | |
585 | sbHadGuiLock = TRUE; | |
586 | ||
587 | size_t nCount = svSavedMessages.Count(); | |
588 | ||
589 | for (size_t n = 0; n < nCount; n++) | |
590 | { | |
591 | QMSG vMsg = svSavedMessages[n]; | |
592 | ||
593 | if ( !ProcessMessage((WXMSG *)&vMsg) ) | |
594 | { | |
595 | ::WinDispatchMsg(m_vHab, &vMsg); | |
596 | } | |
597 | } | |
598 | svSavedMessages.Empty(); | |
599 | } | |
600 | } | |
601 | #endif // wxUSE_THREADS | |
602 | ||
603 | // Process the message | |
604 | if (!ProcessMessage((WXMSG *)&svCurrentMsg) ) | |
605 | { | |
606 | ::WinDispatchMsg(m_vHab, (PQMSG)&svCurrentMsg); | |
607 | } | |
608 | } | |
609 | return TRUE; | |
610 | } | |
611 | ||
612 | ////////////////////////////////////////////////////////////////////////////// | |
613 | // | |
614 | // Keep trying to process messages until WM_QUIT | |
615 | // received. | |
616 | // | |
617 | // If there are messages to be processed, they will all be | |
618 | // processed and OnIdle will not be called. | |
619 | // When there are no more messages, OnIdle is called. | |
620 | // If OnIdle requests more time, | |
621 | // it will be repeatedly called so long as there are no pending messages. | |
622 | // A 'feature' of this is that once OnIdle has decided that no more processing | |
623 | // is required, then it won't get processing time until further messages | |
624 | // are processed (it'll sit in DoMessage). | |
625 | // | |
626 | ////////////////////////////////////////////////////////////////////////////// | |
627 | int wxApp::MainLoop() | |
628 | { | |
629 | m_bKeepGoing = TRUE; | |
630 | ||
631 | while (m_bKeepGoing) | |
632 | { | |
633 | #if wxUSE_THREADS | |
634 | wxMutexGuiLeaveOrEnter(); | |
635 | #endif // wxUSE_THREADS | |
636 | while (!::WinPeekMsg(m_vHab, &svCurrentMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) && | |
637 | ProcessIdle() ) | |
638 | { | |
639 | } | |
640 | DoMessage(); | |
641 | } | |
642 | return (int)svCurrentMsg.mp1; | |
643 | } | |
644 | ||
645 | // | |
646 | // Returns TRUE if more time is needed. | |
647 | // | |
648 | bool wxApp::ProcessIdle() | |
649 | { | |
650 | wxIdleEvent vEvent; | |
651 | ||
652 | vEvent.SetEventObject(this); | |
653 | ProcessEvent(vEvent); | |
654 | return vEvent.MoreRequested(); | |
655 | } | |
656 | ||
657 | #if wxUSE_THREADS | |
658 | void wxApp::ProcessPendingEvents() | |
659 | { | |
660 | wxNode* pNode = wxPendingEvents->First(); | |
661 | wxCriticalSectionLocker vLocker(*wxPendingEventsLocker); | |
662 | ||
663 | while (pNode) | |
664 | { | |
665 | wxEvtHandler* pHandler = (wxEvtHandler *)pNode->Data(); | |
666 | pHandler->ProcessPendingEvents(); | |
667 | ||
668 | delete pNode; | |
669 | pNode = wxPendingEvents->First(); | |
670 | } | |
671 | } | |
672 | #endif | |
673 | ||
674 | void wxApp::ExitMainLoop() | |
675 | { | |
676 | m_bKeepGoing = FALSE; | |
677 | } | |
678 | ||
679 | bool wxApp::Pending() | |
680 | { | |
681 | return (::WinPeekMsg(m_vHab, (PQMSG)&svCurrentMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) != 0); | |
682 | } | |
683 | ||
684 | void wxApp::Dispatch() | |
685 | { | |
686 | DoMessage(); | |
687 | } | |
688 | ||
689 | ////////////////////////////////////////////////////////////////////////////// | |
690 | // | |
691 | // Give all windows a chance to preprocess | |
692 | // the message. Some may have accelerator tables, or have | |
693 | // MDI complications. | |
694 | // | |
695 | ////////////////////////////////////////////////////////////////////////////// | |
696 | bool wxApp::ProcessMessage( | |
697 | WXMSG* pWxmsg | |
698 | ) | |
699 | { | |
700 | QMSG* vMsg = (PQMSG)pWxmsg; | |
701 | HWND hWnd = vMsg->hwnd; | |
702 | wxWindow* pWndThis = wxFindWinFromHandle((WXHWND)hWnd); | |
703 | wxWindow* pWnd; | |
704 | ||
705 | // | |
706 | // for some composite controls (like a combobox), wndThis might be NULL | |
707 | // because the subcontrol is not a wxWindow, but only the control itself | |
708 | // is - try to catch this case | |
709 | // | |
710 | while (hWnd && !pWndThis) | |
711 | { | |
712 | hWnd = ::WinQueryWindow(hWnd, QW_PARENT); | |
713 | pWndThis = wxFindWinFromHandle((WXHWND)hWnd); | |
714 | } | |
715 | ||
716 | // Anyone for a non-translation message? Try youngest descendants first. | |
717 | for (pWnd = pWndThis; pWnd; pWnd = pWnd->GetParent()) | |
718 | { | |
719 | if (pWnd->OS2ProcessMessage(pWxmsg)) | |
720 | return TRUE; | |
721 | } | |
722 | return FALSE; | |
723 | } | |
724 | ||
725 | void wxApp::OnIdle( | |
726 | wxIdleEvent& rEvent | |
727 | ) | |
728 | { | |
729 | static bool sbInOnIdle = FALSE; | |
730 | ||
731 | // | |
732 | // Avoid recursion (via ProcessEvent default case) | |
733 | // | |
734 | if (sbInOnIdle ) | |
735 | return; | |
736 | ||
737 | sbInOnIdle = TRUE; | |
738 | ||
739 | // | |
740 | // 'Garbage' collection of windows deleted with Close(). | |
741 | // | |
742 | DeletePendingObjects(); | |
743 | ||
744 | #if wxUSE_LOG | |
745 | // flush the logged messages if any | |
746 | wxLog* pLog = wxLog::GetActiveTarget(); | |
747 | ||
748 | if (pLog != NULL && pLog->HasPendingMessages()) | |
749 | pLog->Flush(); | |
750 | #endif // wxUSE_LOG | |
751 | ||
752 | // Send OnIdle events to all windows | |
753 | if (SendIdleEvents()) | |
754 | { | |
755 | // | |
756 | // SendIdleEvents() returns TRUE if at least one window requested more | |
757 | // idle events | |
758 | // | |
759 | rEvent.RequestMore(TRUE); | |
760 | } | |
761 | ||
762 | // | |
763 | // If they are pending events, we must process them. | |
764 | // | |
765 | #if wxUSE_THREADS | |
766 | ProcessPendingEvents(); | |
767 | #endif | |
768 | sbInOnIdle = FALSE; | |
769 | } | |
770 | ||
771 | void wxWakeUpIdle() | |
772 | { | |
773 | // **** please implement me! **** | |
774 | // Wake up the idle handler processor, even if it is in another thread... | |
775 | } | |
776 | ||
777 | // Send idle event to all top-level windows | |
778 | bool wxApp::SendIdleEvents() | |
779 | { | |
780 | bool bNeedMore = FALSE; | |
781 | wxWindowList::Node* pNode = wxTopLevelWindows.GetFirst(); | |
782 | ||
783 | while (pNode) | |
784 | { | |
785 | wxWindow* pWin = pNode->GetData(); | |
786 | ||
787 | if (SendIdleEvents(pWin)) | |
788 | bNeedMore = TRUE; | |
789 | pNode = pNode->GetNext(); | |
790 | } | |
791 | return bNeedMore; | |
792 | } | |
793 | ||
794 | // | |
795 | // Send idle event to window and all subwindows | |
796 | // | |
797 | bool wxApp::SendIdleEvents( | |
798 | wxWindow* pWin | |
799 | ) | |
800 | { | |
801 | bool bNeedMore = FALSE; | |
802 | wxIdleEvent vEvent; | |
803 | ||
804 | vEvent.SetEventObject(pWin); | |
805 | pWin->GetEventHandler()->ProcessEvent(vEvent); | |
806 | ||
807 | if (vEvent.MoreRequested()) | |
808 | bNeedMore = TRUE; | |
809 | ||
810 | wxNode* pNode = pWin->GetChildren().First(); | |
811 | ||
812 | while (pNode) | |
813 | { | |
814 | wxWindow* pWin = (wxWindow*) pNode->Data(); | |
815 | ||
816 | if (SendIdleEvents(pWin)) | |
817 | bNeedMore = TRUE; | |
818 | pNode = pNode->Next(); | |
819 | } | |
820 | return bNeedMore; | |
821 | } | |
822 | ||
823 | void wxApp::DeletePendingObjects() | |
824 | { | |
825 | wxNode* pNode = wxPendingDelete.First(); | |
826 | ||
827 | while (pNode) | |
828 | { | |
829 | wxObject* pObj = (wxObject *)pNode->Data(); | |
830 | ||
831 | delete pObj; | |
832 | ||
833 | if (wxPendingDelete.Member(pObj)) | |
834 | delete pNode; | |
835 | ||
836 | // | |
837 | // Deleting one object may have deleted other pending | |
838 | // objects, so start from beginning of list again. | |
839 | // | |
840 | pNode = wxPendingDelete.First(); | |
841 | } | |
842 | } | |
843 | ||
844 | void wxApp::OnEndSession( | |
845 | wxCloseEvent& WXUNUSED(rEvent)) | |
846 | { | |
847 | if (GetTopWindow()) | |
848 | GetTopWindow()->Close(TRUE); | |
849 | } | |
850 | ||
851 | // | |
852 | // Default behaviour: close the application with prompts. The | |
853 | // user can veto the close, and therefore the end session. | |
854 | // | |
855 | void wxApp::OnQueryEndSession( | |
856 | wxCloseEvent& rEvent | |
857 | ) | |
858 | { | |
859 | if (GetTopWindow()) | |
860 | { | |
861 | if (!GetTopWindow()->Close(!rEvent.CanVeto())) | |
862 | rEvent.Veto(TRUE); | |
863 | } | |
864 | } | |
865 | ||
866 | void wxExit() | |
867 | { | |
868 | wxLogError(_("Fatal error: exiting")); | |
869 | ||
870 | wxApp::CleanUp(); | |
871 | } | |
872 | ||
873 | // Yield to incoming messages | |
874 | bool wxYield() | |
875 | { | |
876 | HAB vHab; | |
877 | QMSG vMsg; | |
878 | // We want to go back to the main message loop | |
879 | // if we see a WM_QUIT. (?) | |
880 | while (::WinPeekMsg(vHab, &vMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) && vMsg.msg != WM_QUIT) | |
881 | { | |
882 | if (!wxTheApp->DoMessage()) | |
883 | break; | |
884 | } | |
885 | // If they are pending events, we must process them. | |
886 | #if wxUSE_THREADS | |
887 | wxTheApp->ProcessPendingEvents(); | |
888 | #endif | |
889 | return TRUE; | |
890 | } | |
891 | ||
892 | wxIcon wxApp::GetStdIcon( | |
893 | int nWhich | |
894 | ) const | |
895 | { | |
896 | switch(nWhich) | |
897 | { | |
898 | case wxICON_INFORMATION: | |
899 | return wxIcon("wxICON_INFO"); | |
900 | ||
901 | case wxICON_QUESTION: | |
902 | return wxIcon("wxICON_QUESTION"); | |
903 | ||
904 | case wxICON_EXCLAMATION: | |
905 | return wxIcon("wxICON_WARNING"); | |
906 | ||
907 | default: | |
908 | wxFAIL_MSG(wxT("requested non existent standard icon")); | |
909 | // still fall through | |
910 | ||
911 | case wxICON_HAND: | |
912 | return wxIcon("wxICON_ERROR"); | |
913 | } | |
914 | return wxIcon("wxICON_ERROR"); | |
915 | } | |
916 | ||
917 | HINSTANCE wxGetInstance() | |
918 | { | |
919 | return wxhInstance; | |
920 | } | |
921 | ||
922 | void wxSetInstance(HINSTANCE hInst) | |
923 | { | |
924 | wxhInstance = hInst; | |
925 | } | |
926 |