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