1 /////////////////////////////////////////////////////////////////////////////
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #include "wx/gdicmn.h"
22 #include "wx/cursor.h"
24 #include "wx/palette.h"
26 #include "wx/dialog.h"
27 #include "wx/msgdlg.h"
29 #include "wx/dynarray.h"
30 # include "wx/wxchar.h"
35 #include "wx/module.h"
37 #include "wx/os2/private.h"
40 #include "wx/thread.h"
42 // define the array of QMSG strutures
43 WX_DECLARE_OBJARRAY(QMSG
, wxMsgArray
);
45 #include "wx/arrimpl.cpp"
47 WX_DEFINE_OBJARRAY(wxMsgArray
);
48 #endif // wxUSE_THREADS
50 #if wxUSE_WX_RESOURCES
51 #include "wx/resource.h"
55 #include "wx/tooltip.h"
56 #endif // wxUSE_TOOLTIPS
61 // ---------------------------------------------------------------------------
63 // ---------------------------------------------------------------------------
65 extern wxChar
* wxBuffer
;
66 extern wxChar
* wxOsVersion
;
67 extern wxList
* wxWinHandleList
;
68 extern wxList WXDLLEXPORT wxPendingDelete
;
69 extern wxCursor
* g_globalCursor
;
71 HAB vHabmain
= NULLHANDLE
;
73 wxApp
* wxTheApp
= NULL
;
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");
86 HICON wxSTD_FRAME_ICON
= (HICON
) NULL
;
87 HICON wxSTD_MDICHILDFRAME_ICON
= (HICON
) NULL
;
88 HICON wxSTD_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
90 HICON wxDEFAULT_FRAME_ICON
= (HICON
) NULL
;
91 HICON wxDEFAULT_MDICHILDFRAME_ICON
= (HICON
) NULL
;
92 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
94 HBRUSH wxDisableButtonBrush
= (HBRUSH
) 0;
96 // ===========================================================================
98 // ===========================================================================
100 // ---------------------------------------------------------------------------
102 // ---------------------------------------------------------------------------
104 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
106 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
107 EVT_IDLE(wxApp::OnIdle
)
108 EVT_END_SESSION(wxApp::OnEndSession
)
109 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
115 bool wxApp::Initialize(
120 // OS2 has to have an anchorblock
122 vHab
= WinInitialize(0);
129 // Some people may wish to use this, but
130 // probably it shouldn't be here by default.
132 // wxRedirectIOToConsole();
135 wxBuffer
= new wxChar
[1500]; // FIXME; why?
137 wxClassInfo::InitializeClasses();
140 wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion
);
144 wxPendingEventsLocker
= new wxCriticalSection
;
147 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
148 wxTheColourDatabase
->Initialize();
150 wxInitializeStockLists();
151 wxInitializeStockObjects();
153 #if wxUSE_WX_RESOURCES
154 wxInitializeResourceSystem();
157 wxBitmap::InitStandardHandlers();
159 RegisterWindowClasses(vHab
);
160 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
162 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
163 // PLEASE DO NOT ALTER THIS.
164 #if !defined(WXMAKINGDLL) && defined(__VISAGECPP__)
165 extern char wxDummyChar
;
166 if (wxDummyChar
) wxDummyChar
++;
169 // wxSetKeyboardHook(TRUE);
171 wxModule::RegisterModules();
172 if (!wxModule::InitializeModules())
175 } // end of wxApp::Initialize
177 // ---------------------------------------------------------------------------
178 // RegisterWindowClasses
179 // ---------------------------------------------------------------------------
181 bool wxApp::RegisterWindowClasses(
189 if (!::WinRegisterClass( vHab
192 ,CS_SIZEREDRAW
| CS_SYNCPAINT
196 vError
= ::WinGetLastError(vHab
);
197 sError
= wxPMErrorToStr(vError
);
198 wxLogLastError(sError
);
202 if (!::WinRegisterClass( vHab
203 ,wxFrameClassNameNoRedraw
209 vError
= ::WinGetLastError(vHab
);
210 sError
= wxPMErrorToStr(vError
);
211 wxLogLastError(sError
);
215 if (!::WinRegisterClass( vHab
218 ,CS_SIZEREDRAW
| CS_SYNCPAINT
222 vError
= ::WinGetLastError(vHab
);
223 sError
= wxPMErrorToStr(vError
);
224 wxLogLastError(sError
);
228 if (!::WinRegisterClass( vHab
229 ,wxMDIFrameClassNameNoRedraw
235 vError
= ::WinGetLastError(vHab
);
236 sError
= wxPMErrorToStr(vError
);
237 wxLogLastError(sError
);
241 if (!::WinRegisterClass( vHab
242 ,wxMDIChildFrameClassName
244 ,CS_MOVENOTIFY
| CS_SIZEREDRAW
| CS_SYNCPAINT
| CS_HITTEST
248 vError
= ::WinGetLastError(vHab
);
249 sError
= wxPMErrorToStr(vError
);
250 wxLogLastError(sError
);
254 if (!::WinRegisterClass( vHab
255 ,wxMDIChildFrameClassNameNoRedraw
261 vError
= ::WinGetLastError(vHab
);
262 sError
= wxPMErrorToStr(vError
);
263 wxLogLastError(sError
);
267 if (!::WinRegisterClass( vHab
270 ,CS_MOVENOTIFY
| CS_SIZEREDRAW
| CS_HITTEST
| CS_SAVEBITS
| CS_SYNCPAINT
274 vError
= ::WinGetLastError(vHab
);
275 sError
= wxPMErrorToStr(vError
);
276 wxLogLastError(sError
);
280 if (!::WinRegisterClass( vHab
283 ,CS_MOVENOTIFY
| CS_SIZEREDRAW
| CS_HITTEST
| CS_SAVEBITS
| CS_SYNCPAINT
287 vError
= ::WinGetLastError(vHab
);
288 sError
= wxPMErrorToStr(vError
);
289 wxLogLastError(sError
);
293 } // end of wxApp::RegisterWindowClasses
296 // Cleans up any wxWindows internal structures left lying around
298 void wxApp::CleanUp()
307 // Flush the logged messages if any and install a 'safer' log target: the
308 // default one (wxLogGui) can't be used after the resources are freed just
309 // below and the user suppliedo ne might be even more unsafe (using any
310 // wxWindows GUI function is unsafe starting from now)
312 wxLog::DontCreateOnDemand();
315 // This will flush the old messages if any
317 delete wxLog::SetActiveTarget(new wxLogStderr
);
321 // One last chance for pending objects to be cleaned up
323 wxTheApp
->DeletePendingObjects();
325 wxModule::CleanUpModules();
327 #if wxUSE_WX_RESOURCES
328 wxCleanUpResourceSystem();
331 wxDeleteStockObjects();
334 // Destroy all GDI lists, etc.
336 wxDeleteStockLists();
338 delete wxTheColourDatabase
;
339 wxTheColourDatabase
= NULL
;
341 wxBitmap::CleanUpHandlers();
347 // PM-SPECIFIC CLEANUP
350 // wxSetKeyboardHook(FALSE);
352 if (wxSTD_FRAME_ICON
)
353 ::WinFreeFileIcon(wxSTD_FRAME_ICON
);
354 if (wxSTD_MDICHILDFRAME_ICON
)
355 ::WinFreeFileIcon(wxSTD_MDICHILDFRAME_ICON
);
356 if (wxSTD_MDIPARENTFRAME_ICON
)
357 ::WinFreeFileIcon(wxSTD_MDIPARENTFRAME_ICON
);
359 if (wxDEFAULT_FRAME_ICON
)
360 ::WinFreeFileIcon(wxDEFAULT_FRAME_ICON
);
361 if (wxDEFAULT_MDICHILDFRAME_ICON
)
362 ::WinFreeFileIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
363 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
364 ::WinFreeFileIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
366 if ( wxDisableButtonBrush
)
368 // TODO: ::DeleteObject( wxDisableButtonBrush );
372 delete wxWinHandleList
;
374 delete wxPendingEvents
;
376 delete wxPendingEventsLocker
;
377 // If we don't do the following, we get an apparent memory leak.
378 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
381 wxClassInfo::CleanUpClasses();
383 // Delete Message queue
385 ::WinDestroyMsgQueue(wxTheApp
->m_hMq
);
390 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
391 // At this point we want to check if there are any memory
392 // blocks that aren't part of the wxDebugContext itself,
393 // as a special case. Then when dumping we need to ignore
394 // wxDebugContext, too.
395 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
397 wxLogDebug(wxT("There were memory leaks."));
398 wxDebugContext::Dump();
399 wxDebugContext::PrintStatistics();
401 // wxDebugContext::SetStream(NULL, NULL);
405 // do it as the very last thing because everything else can log messages
406 delete wxLog::SetActiveTarget(NULL
);
408 } // end of wxApp::CleanUp
417 if (!wxApp::Initialize(vHab
))
421 // create the application object or ensure that one already exists
425 // The app may have declared a global application object, but we recommend
426 // the IMPLEMENT_APP macro is used instead, which sets an initializer
427 // function for delayed, dynamic app object construction.
428 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
429 wxT("No initializer - use IMPLEMENT_APP macro.") );
430 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
432 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
433 wxTheApp
->argc
= argc
;
436 wxTheApp
->argv
= new wxChar
*[argc
+1];
442 wxTheApp
->argv
[nArgc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[nArgc
]));
445 wxTheApp
->argv
[nArgc
] = (wxChar
*)NULL
;
447 wxTheApp
->argv
= argv
;
450 wxString
sName(wxFileNameFromPath(argv
[0]));
452 wxStripExtension(sName
);
453 wxTheApp
->SetAppName(sName
);
457 if (!wxTheApp
->OnInitGui())
462 if (wxTheApp
->OnInit())
464 nRetValue
= wxTheApp
->OnRun();
469 wxWindow
* pTopWindow
= wxTheApp
->GetTopWindow();
473 // Forcibly delete the window.
474 if (pTopWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
475 pTopWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
477 pTopWindow
->Close(TRUE
);
478 wxTheApp
->DeletePendingObjects();
483 wxTheApp
->SetTopWindow(NULL
);
491 bool wxApp::OnInitGui()
496 m_hMq
= ::WinCreateMsgQueue(vHabmain
, 0);
499 vError
= ::WinGetLastError(vHabmain
);
500 sError
= wxPMErrorToStr(vError
);
505 } // end of wxApp::OnInitGui
508 // Static member initialization
510 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
516 m_wantDebugOutput
= TRUE
;
520 m_nPrintMode
= wxPRINT_WINDOWS
;
521 m_exitOnFrameDelete
= TRUE
;
524 } // end of wxApp::wxApp
529 // Delete command-line args
534 for (i
= 0; i
< argc
; i
++)
540 } // end of wxApp::~wxApp
542 bool wxApp::Initialized()
548 } // end of wxApp::Initialized
551 // Get and process a message, returning FALSE if WM_QUIT
552 // received (and also set the flag telling the app to exit the main loop)
554 bool wxApp::DoMessage()
556 BOOL bRc
= ::WinGetMsg(vHabmain
, &svCurrentMsg
, HWND(NULL
), 0, 0);
562 m_bKeepGoing
= FALSE
;
567 // should never happen, but let's test for it nevertheless
568 wxLogLastError("GetMessage");
573 wxASSERT_MSG( wxThread::IsMain()
574 ,wxT("only the main thread can process Windows messages")
577 static bool sbHadGuiLock
= TRUE
;
578 static wxMsgArray svSavedMessages
;
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)
585 if (!wxGuiOwnedByMainThread())
587 sbHadGuiLock
= FALSE
;
590 // Leave out WM_COMMAND messages: too dangerous, sometimes
591 // the message will be processed twice
593 if ( !wxIsWaitingForThread() ||
594 svCurrentMsg
.msg
!= WM_COMMAND
)
596 svSavedMessages
.Add(svCurrentMsg
);
603 // Have we just regained the GUI lock? if so, post all of the saved
610 size_t nCount
= svSavedMessages
.Count();
612 for (size_t n
= 0; n
< nCount
; n
++)
614 QMSG vMsg
= svSavedMessages
[n
];
616 if ( !ProcessMessage((WXMSG
*)&vMsg
) )
618 ::WinDispatchMsg(vHabmain
, &vMsg
);
621 svSavedMessages
.Empty();
624 #endif // wxUSE_THREADS
626 // Process the message
627 if (!ProcessMessage((WXMSG
*)&svCurrentMsg
))
629 ::WinDispatchMsg(vHabmain
, (PQMSG
)&svCurrentMsg
);
633 } // end of wxApp::DoMessage
635 //////////////////////////////////////////////////////////////////////////////
637 // Keep trying to process messages until WM_QUIT
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).
649 //////////////////////////////////////////////////////////////////////////////
650 int wxApp::MainLoop()
657 wxMutexGuiLeaveOrEnter();
658 #endif // wxUSE_THREADS
659 while (!Pending() && ProcessIdle())
665 return (int)svCurrentMsg
.mp1
;
666 } // end of wxApp::MainLoop
669 // Returns TRUE if more time is needed.
671 bool wxApp::ProcessIdle()
675 vEvent
.SetEventObject(this);
676 ProcessEvent(vEvent
);
677 return vEvent
.MoreRequested();
678 } // end of wxApp::ProcessIdle
680 void wxApp::ExitMainLoop()
682 m_bKeepGoing
= FALSE
;
685 bool wxApp::Pending()
687 return (::WinPeekMsg(vHabmain
, (PQMSG
)&svCurrentMsg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) != 0);
690 void wxApp::Dispatch()
695 //////////////////////////////////////////////////////////////////////////////
697 // Give all windows a chance to preprocess
698 // the message. Some may have accelerator tables, or have
699 // MDI complications.
701 //////////////////////////////////////////////////////////////////////////////
702 bool wxApp::ProcessMessage(
706 QMSG
* vMsg
= (PQMSG
)pWxmsg
;
707 HWND hWnd
= vMsg
->hwnd
;
708 wxWindow
* pWndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
713 // We must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
714 // popup the tooltip bubbles
716 if (pWndThis
&& (vMsg
->msg
== WM_MOUSEMOVE
))
718 wxToolTip
* pToolTip
= pWndThis
->GetToolTip();
721 pToolTip
->RelayEvent(pWxmsg
);
724 #endif // wxUSE_TOOLTIPS
727 // For some composite controls (like a combobox), wndThis might be NULL
728 // because the subcontrol is not a wxWindow, but only the control itself
729 // is - try to catch this case
731 while (hWnd
&& !pWndThis
)
733 hWnd
= ::WinQueryWindow(hWnd
, QW_PARENT
);
734 pWndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
738 // Anyone for a non-translation message? Try youngest descendants first.
740 for (pWnd
= pWndThis
; pWnd
; pWnd
= pWnd
->GetParent())
742 if (pWnd
->OS2ProcessMessage(pWxmsg
))
746 } // end of wxApp::ProcessMessage
752 static bool sbInOnIdle
= FALSE
;
755 // Avoid recursion (via ProcessEvent default case)
763 // If there are pending events, we must process them: pending events
764 // are either events to the threads other than main or events posted
765 // with wxPostEvent() functions
767 ProcessPendingEvents();
770 // 'Garbage' collection of windows deleted with Close().
772 DeletePendingObjects();
776 // Flush the logged messages if any
778 wxLog::FlushActive();
782 // Send OnIdle events to all windows
784 if (SendIdleEvents())
787 // SendIdleEvents() returns TRUE if at least one window requested more
790 rEvent
.RequestMore(TRUE
);
793 } // end of wxApp::OnIdle
795 // Send idle event to all top-level windows
796 bool wxApp::SendIdleEvents()
798 bool bNeedMore
= FALSE
;
799 wxWindowList::Node
* pNode
= wxTopLevelWindows
.GetFirst();
803 wxWindow
* pWin
= pNode
->GetData();
805 if (SendIdleEvents(pWin
))
807 pNode
= pNode
->GetNext();
810 } // end of wxApp::SendIdleEvents
813 // Send idle event to window and all subwindows
815 bool wxApp::SendIdleEvents(
819 bool bNeedMore
= FALSE
;
822 vEvent
.SetEventObject(pWin
);
823 pWin
->GetEventHandler()->ProcessEvent(vEvent
);
825 if (vEvent
.MoreRequested())
828 wxNode
* pNode
= pWin
->GetChildren().First();
832 wxWindow
* pWin
= (wxWindow
*) pNode
->Data();
834 if (SendIdleEvents(pWin
))
836 pNode
= pNode
->Next();
839 } // end of wxApp::SendIdleEvents
841 void wxApp::DeletePendingObjects()
843 wxNode
* pNode
= wxPendingDelete
.First();
847 wxObject
* pObj
= (wxObject
*)pNode
->Data();
851 if (wxPendingDelete
.Member(pObj
))
855 // Deleting one object may have deleted other pending
856 // objects, so start from beginning of list again.
858 pNode
= wxPendingDelete
.First();
860 } // end of wxApp::DeletePendingObjects
862 void wxApp::OnEndSession(
863 wxCloseEvent
& WXUNUSED(rEvent
))
866 GetTopWindow()->Close(TRUE
);
867 } // end of wxApp::OnEndSession
870 // Default behaviour: close the application with prompts. The
871 // user can veto the close, and therefore the end session.
873 void wxApp::OnQueryEndSession(
879 if (!GetTopWindow()->Close(!rEvent
.CanVeto()))
882 } // end of wxApp::OnQueryEndSession
886 wxLogError(_("Fatal error: exiting"));
892 // Yield to incoming messages
900 // Disable log flushing from here because a call to wxYield() shouldn't
901 // normally result in message boxes popping up &c
906 // We want to go back to the main message loop
907 // if we see a WM_QUIT. (?)
909 while (::WinPeekMsg(vHab
, &vMsg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) && vMsg
.msg
!= WM_QUIT
)
912 wxMutexGuiLeaveOrEnter();
913 #endif // wxUSE_THREADS
914 if (!wxTheApp
->DoMessage())
918 // If they are pending events, we must process them.
921 wxTheApp
->ProcessPendingEvents();
924 // Let the logs be flashed again
930 wxIcon
wxApp::GetStdIcon(
936 case wxICON_INFORMATION
:
937 return wxIcon("wxICON_INFO");
939 case wxICON_QUESTION
:
940 return wxIcon("wxICON_QUESTION");
942 case wxICON_EXCLAMATION
:
943 return wxIcon("wxICON_WARNING");
946 wxFAIL_MSG(wxT("requested non existent standard icon"));
947 // still fall through
950 return wxIcon("wxICON_ERROR");
952 return wxIcon("wxICON_ERROR");
953 } // end of wxApp::GetStdIcon
955 //-----------------------------------------------------------------------------
957 //-----------------------------------------------------------------------------
962 // Send the top window a dummy message so idle handler processing will
963 // start up again. Doing it this way ensures that the idle handler
964 // wakes up in the right thread (see also wxWakeUpMainThread() which does
965 // the same for the main app thread only)
967 wxWindow
* pTopWindow
= wxTheApp
->GetTopWindow();
971 if ( !::WinPostMsg(GetHwndOf(pTopWindow
), WM_NULL
, (MPARAM
)0, (MPARAM
)0))
974 // Should never happen
976 wxLogLastError("PostMessage(WM_NULL)");
979 } // end of wxWakeUpIdle