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