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