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"
57 // ---------------------------------------------------------------------------
59 // ---------------------------------------------------------------------------
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
;
68 HINSTANCE wxhInstance
= 0;
70 wxApp
* wxTheApp
= NULL
;
73 // FIXME why not const? and not static?
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 MRESULT
wxWndProc( HWND
102 // ===========================================================================
104 // ===========================================================================
106 // ---------------------------------------------------------------------------
108 // ---------------------------------------------------------------------------
110 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
112 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
113 EVT_IDLE(wxApp::OnIdle
)
114 EVT_END_SESSION(wxApp::OnEndSession
)
115 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
119 bool wxApp::Initialize(
123 // Some people may wish to use this, but
124 // probably it shouldn't be here by default.
126 // wxRedirectIOToConsole();
129 wxBuffer
= new wxChar
[1500]; // FIXME; why?
131 wxClassInfo::InitializeClasses();
134 wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion
);
137 // I'm annoyed ... I don't know where to put this and I don't want to
138 // create a module for that as it's part of the core.
139 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
140 wxTheColourDatabase
->Initialize();
142 wxInitializeStockLists();
143 wxInitializeStockObjects();
145 #if wxUSE_WX_RESOURCES
146 wxInitializeResourceSystem();
149 wxBitmap::InitStandardHandlers();
151 g_globalCursor
= new wxCursor
;
154 wxSTD_FRAME_ICON
= ::WinLoadFileIcon(wxT("wxSTD_FRAME"), TRUE
);
155 wxSTD_MDIPARENTFRAME_ICON
= ::WinLoadFileIcon(wxT("wxSTD_MDIPARENTFRAME"), TRUE
);
156 wxSTD_MDICHILDFRAME_ICON
= ::WinLoadFileIcon(wxT("wxSTD_MDICHILDFRAME"), TRUE
);
158 wxDEFAULT_FRAME_ICON
= ::WinLoadFileIcon(wxT("wxDEFAULT_FRAME"), TRUE
);
159 wxDEFAULT_MDIPARENTFRAME_ICON
= ::WinLoadFileIcon(wxT("wxDEFAULT_MDIPARENTFRAME"), TRUE
);
160 wxDEFAULT_MDICHILDFRAME_ICON
= ::WinLoadFileIcon(wxT("wxDEFAULT_MDICHILDFRAME"), TRUE
);
162 RegisterWindowClasses(vHab
);
163 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
165 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
166 // PLEASE DO NOT ALTER THIS.
167 #if !defined(WXMAKINGDLL)
168 extern char wxDummyChar
;
169 if (wxDummyChar
) wxDummyChar
++;
172 wxSetKeyboardHook(TRUE
);
174 wxModule::RegisterModules();
175 if (!wxModule::InitializeModules())
180 // ---------------------------------------------------------------------------
181 // RegisterWindowClasses
182 // ---------------------------------------------------------------------------
184 // TODO we should only register classes really used by the app. For this it
185 // would be enough to just delay the class registration until an attempt
186 // to create a window of this class is made.
187 bool wxApp::RegisterWindowClasses(
193 if ((rc
= ::WinRegisterClass( vHab
196 ,CS_SIZEREDRAW
| CS_SYNCPAINT
| CS_HITTEST
| CS_CLIPCHILDREN
| CS_FRAME
200 wxLogLastError("RegisterClass(frame)");
205 if (!::WinRegisterClass( vHab
206 ,wxFrameClassNameNoRedraw
208 ,CS_HITTEST
| CS_CLIPCHILDREN
| CS_FRAME
212 wxLogLastError("RegisterClass(no redraw frame)");
217 if (!::WinRegisterClass( vHab
220 ,CS_SIZEREDRAW
| CS_SYNCPAINT
| CS_HITTEST
| CS_CLIPCHILDREN
| CS_FRAME
224 wxLogLastError("RegisterClass(MDI parent)");
229 if (!::WinRegisterClass( vHab
230 ,wxMDIFrameClassNameNoRedraw
232 ,CS_HITTEST
| CS_CLIPCHILDREN
| CS_FRAME
236 wxLogLastError("RegisterClass(no redraw MDI parent)");
241 if (!::WinRegisterClass( vHab
242 ,wxMDIChildFrameClassName
244 ,CS_MOVENOTIFY
| CS_SIZEREDRAW
| CS_SYNCPAINT
| CS_HITTEST
| CS_CLIPSIBLINGS
| CS_FRAME
248 wxLogLastError("RegisterClass(MDI child)");
253 if (!::WinRegisterClass( vHab
254 ,wxMDIChildFrameClassNameNoRedraw
256 ,CS_HITTEST
| CS_CLIPSIBLINGS
| CS_FRAME
260 wxLogLastError("RegisterClass(no redraw MDI child)");
265 if (!::WinRegisterClass( vHab
268 ,CS_MOVENOTIFY
| CS_SIZEREDRAW
| CS_HITTEST
| CS_CLIPSIBLINGS
| CS_SAVEBITS
| CS_SYNCPAINT
272 wxLogLastError("RegisterClass(Panel)");
277 if (!::WinRegisterClass( vHab
280 ,CS_MOVENOTIFY
| CS_SIZEREDRAW
| CS_HITTEST
| CS_CLIPSIBLINGS
| CS_SAVEBITS
| CS_SYNCPAINT
284 wxLogLastError("RegisterClass(Canvas)");
291 //// Cleans up any wxWindows internal structures left lying around
293 void wxApp::CleanUp()
298 // flush the logged messages if any and install a 'safer' log target: the
299 // default one (wxLogGui) can't be used after the resources are freed just
300 // below and the user suppliedo ne might be even more unsafe (using any
301 // wxWindows GUI function is unsafe starting from now)
302 wxLog::DontCreateOnDemand();
304 // this will flush the old messages if any
305 delete wxLog::SetActiveTarget(new wxLogStderr
);
308 // One last chance for pending objects to be cleaned up
309 wxTheApp
->DeletePendingObjects();
311 wxModule::CleanUpModules();
313 #if wxUSE_WX_RESOURCES
314 wxCleanUpResourceSystem();
316 // wxDefaultResourceTable->ClearTable();
319 // Indicate that the cursor can be freed, so that cursor won't be deleted
320 // by deleting the bitmap list before g_globalCursor goes out of scope
321 // (double deletion of the cursor).
322 wxSetCursor(wxNullCursor
);
323 delete g_globalCursor
;
324 g_globalCursor
= NULL
;
326 wxDeleteStockObjects();
328 // Destroy all GDI lists, etc.
329 wxDeleteStockLists();
331 delete wxTheColourDatabase
;
332 wxTheColourDatabase
= NULL
;
334 wxBitmap::CleanUpHandlers();
339 //// WINDOWS-SPECIFIC CLEANUP
341 wxSetKeyboardHook(FALSE
);
343 if (wxSTD_FRAME_ICON
)
344 ::WinFreeFileIcon(wxSTD_FRAME_ICON
);
345 if (wxSTD_MDICHILDFRAME_ICON
)
346 ::WinFreeFileIcon(wxSTD_MDICHILDFRAME_ICON
);
347 if (wxSTD_MDIPARENTFRAME_ICON
)
348 ::WinFreeFileIcon(wxSTD_MDIPARENTFRAME_ICON
);
350 if (wxDEFAULT_FRAME_ICON
)
351 ::WinFreeFileIcon(wxDEFAULT_FRAME_ICON
);
352 if (wxDEFAULT_MDICHILDFRAME_ICON
)
353 ::WinFreeFileIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
354 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
355 ::WinFreeFileIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
357 if ( wxDisableButtonBrush
)
359 // TODO: ::DeleteObject( wxDisableButtonBrush );
363 delete wxWinHandleList
;
365 // GL: I'm annoyed ... I don't know where to put this and I don't want to
366 // create a module for that as it's part of the core.
368 delete wxPendingEvents
;
369 delete wxPendingEventsLocker
;
370 // If we don't do the following, we get an apparent memory leak.
371 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
374 wxClassInfo::CleanUpClasses();
379 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
380 // At this point we want to check if there are any memory
381 // blocks that aren't part of the wxDebugContext itself,
382 // as a special case. Then when dumping we need to ignore
383 // wxDebugContext, too.
384 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
386 wxLogDebug(wxT("There were memory leaks."));
387 wxDebugContext::Dump();
388 wxDebugContext::PrintStatistics();
390 // wxDebugContext::SetStream(NULL, NULL);
394 // do it as the very last thing because everything else can log messages
395 delete wxLog::SetActiveTarget(NULL
);
406 if (!wxApp::Initialize(vHab
))
410 // create the application object or ensure that one already exists
414 wxCHECK_MSG( wxApp::GetInitializerFunction()
416 ,wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n")
419 wxAppInitializerFunction fnAppIni
= wxApp::GetInitializerFunction();
420 wxObject
* pTest_app
= fnAppIni();
422 wxTheApp
= (wxApp
*)pTest_app
;
424 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
425 wxTheApp
->argc
= argc
;
428 wxTheApp
->argv
= new wxChar
*[argc
+1];
434 wxTheApp
->argv
[nArgc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[nArgc
]));
437 wxTheApp
->argv
[nArgc
] = (wxChar
*)NULL
;
439 wxTheApp
->argv
= argv
;
442 wxString
sName(wxFileNameFromPath(argv
[0]));
444 wxStripExtension(sName
);
445 wxTheApp
->SetAppName(sName
);
449 if (!wxTheApp
->OnInitGui())
454 if (wxTheApp
->OnInit())
462 wxWindow
* pTopWindow
= wxTheApp
->GetTopWindow();
466 // Forcibly delete the window.
467 if (pTopWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
468 pTopWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
470 pTopWindow
->Close(TRUE
);
471 wxTheApp
->DeletePendingObjects();
476 wxTheApp
->SetTopWindow(NULL
);
485 bool wxApp::OnInitGui()
487 vHabmain
= WinInitialize(0);
488 m_hMq
= WinCreateMsgQueue(vHabmain
, 0);
494 // Static member initialization
496 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
502 m_wantDebugOutput
= TRUE
;
506 m_nPrintMode
= wxPRINT_WINDOWS
;
507 m_exitOnFrameDelete
= TRUE
;
513 // Delete command-line args
515 for (i
= 0; i
< argc
; i
++)
522 bool wxApp::Initialized()
531 // Get and process a message, returning FALSE if WM_QUIT
532 // received (and also set the flag telling the app to exit the main loop)
534 bool wxApp::DoMessage()
536 BOOL bRc
= ::WinGetMsg(vHabmain
, &m_vMsg
, HWND(NULL
), 0, 0);
541 m_bKeepGoing
= FALSE
;
546 // should never happen, but let's test for it nevertheless
547 wxLogLastError("GetMessage");
552 wxASSERT_MSG( wxThread::IsMain()
553 ,wxT("only the main thread can process Windows messages")
556 static bool sbHadGuiLock
= TRUE
;
557 static wxMsgArray svSavedMessages
;
560 // if a secondary thread owns is doing GUI calls, save all messages for
561 // later processing - we can't process them right now because it will
562 // lead to recursive library calls (and we're not reentrant)
564 if (!wxGuiOwnedByMainThread())
566 sbHadGuiLock
= FALSE
;
568 // leave out WM_COMMAND messages: too dangerous, sometimes
569 // the message will be processed twice
570 if ( !wxIsWaitingForThread() ||
571 svCurrentMsg
.msg
!= WM_COMMAND
)
573 svSavedMessages
.Add(svCurrentMsg
);
580 // have we just regained the GUI lock? if so, post all of the saved
583 // FIXME of course, it's not _exactly_ the same as processing the
584 // messages normally - expect some things to break...
590 size_t nCount
= svSavedMessages
.Count();
592 for (size_t n
= 0; n
< nCount
; n
++)
594 QMSG vMsg
= svSavedMessages
[n
];
596 if ( !ProcessMessage((WXMSG
*)&vMsg
) )
598 ::WinDispatchMsg(vHabmain
, &vMsg
);
601 svSavedMessages
.Empty();
604 #endif // wxUSE_THREADS
606 // Process the message
607 if (!ProcessMessage((WXMSG
*)&svCurrentMsg
) )
609 ::WinDispatchMsg(vHabmain
, (PQMSG
)&svCurrentMsg
);
615 //////////////////////////////////////////////////////////////////////////////
617 // Keep trying to process messages until WM_QUIT
620 // If there are messages to be processed, they will all be
621 // processed and OnIdle will not be called.
622 // When there are no more messages, OnIdle is called.
623 // If OnIdle requests more time,
624 // it will be repeatedly called so long as there are no pending messages.
625 // A 'feature' of this is that once OnIdle has decided that no more processing
626 // is required, then it won't get processing time until further messages
627 // are processed (it'll sit in DoMessage).
629 //////////////////////////////////////////////////////////////////////////////
630 int wxApp::MainLoop()
637 wxMutexGuiLeaveOrEnter();
638 #endif // wxUSE_THREADS
639 while (!::WinPeekMsg(vHabmain
, &svCurrentMsg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) &&
645 return (int)svCurrentMsg
.mp1
;
649 // Returns TRUE if more time is needed.
651 bool wxApp::ProcessIdle()
655 vEvent
.SetEventObject(this);
656 ProcessEvent(vEvent
);
657 return vEvent
.MoreRequested();
661 void wxApp::ProcessPendingEvents()
663 wxNode
* pNode
= wxPendingEvents
->First();
664 wxCriticalSectionLocker
vLocker(*wxPendingEventsLocker
);
668 wxEvtHandler
* pHandler
= (wxEvtHandler
*)pNode
->Data();
669 pHandler
->ProcessPendingEvents();
672 pNode
= wxPendingEvents
->First();
677 void wxApp::ExitMainLoop()
679 m_bKeepGoing
= FALSE
;
682 bool wxApp::Pending()
684 return (::WinPeekMsg(vHabmain
, (PQMSG
)&svCurrentMsg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) != 0);
687 void wxApp::Dispatch()
692 //////////////////////////////////////////////////////////////////////////////
694 // Give all windows a chance to preprocess
695 // the message. Some may have accelerator tables, or have
696 // MDI complications.
698 //////////////////////////////////////////////////////////////////////////////
699 bool wxApp::ProcessMessage(
703 QMSG
* vMsg
= (PQMSG
)pWxmsg
;
704 HWND hWnd
= vMsg
->hwnd
;
705 wxWindow
* pWndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
709 // for some composite controls (like a combobox), wndThis might be NULL
710 // because the subcontrol is not a wxWindow, but only the control itself
711 // is - try to catch this case
713 while (hWnd
&& !pWndThis
)
715 hWnd
= ::WinQueryWindow(hWnd
, QW_PARENT
);
716 pWndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
719 // Anyone for a non-translation message? Try youngest descendants first.
720 for (pWnd
= pWndThis
; pWnd
; pWnd
= pWnd
->GetParent())
722 if (pWnd
->OS2ProcessMessage(pWxmsg
))
732 static bool sbInOnIdle
= FALSE
;
735 // Avoid recursion (via ProcessEvent default case)
743 // 'Garbage' collection of windows deleted with Close().
745 DeletePendingObjects();
748 // flush the logged messages if any
749 wxLog
* pLog
= wxLog::GetActiveTarget();
751 if (pLog
!= NULL
&& pLog
->HasPendingMessages())
755 // Send OnIdle events to all windows
756 if (SendIdleEvents())
759 // SendIdleEvents() returns TRUE if at least one window requested more
762 rEvent
.RequestMore(TRUE
);
766 // If they are pending events, we must process them.
769 ProcessPendingEvents();
776 // **** please implement me! ****
777 // Wake up the idle handler processor, even if it is in another thread...
780 // Send idle event to all top-level windows
781 bool wxApp::SendIdleEvents()
783 bool bNeedMore
= FALSE
;
784 wxWindowList::Node
* pNode
= wxTopLevelWindows
.GetFirst();
788 wxWindow
* pWin
= pNode
->GetData();
790 if (SendIdleEvents(pWin
))
792 pNode
= pNode
->GetNext();
798 // Send idle event to window and all subwindows
800 bool wxApp::SendIdleEvents(
804 bool bNeedMore
= FALSE
;
807 vEvent
.SetEventObject(pWin
);
808 pWin
->GetEventHandler()->ProcessEvent(vEvent
);
810 if (vEvent
.MoreRequested())
813 wxNode
* pNode
= pWin
->GetChildren().First();
817 wxWindow
* pWin
= (wxWindow
*) pNode
->Data();
819 if (SendIdleEvents(pWin
))
821 pNode
= pNode
->Next();
826 void wxApp::DeletePendingObjects()
828 wxNode
* pNode
= wxPendingDelete
.First();
832 wxObject
* pObj
= (wxObject
*)pNode
->Data();
836 if (wxPendingDelete
.Member(pObj
))
840 // Deleting one object may have deleted other pending
841 // objects, so start from beginning of list again.
843 pNode
= wxPendingDelete
.First();
847 void wxApp::OnEndSession(
848 wxCloseEvent
& WXUNUSED(rEvent
))
851 GetTopWindow()->Close(TRUE
);
855 // Default behaviour: close the application with prompts. The
856 // user can veto the close, and therefore the end session.
858 void wxApp::OnQueryEndSession(
864 if (!GetTopWindow()->Close(!rEvent
.CanVeto()))
871 wxLogError(_("Fatal error: exiting"));
876 // Yield to incoming messages
881 // We want to go back to the main message loop
882 // if we see a WM_QUIT. (?)
883 while (::WinPeekMsg(vHab
, &vMsg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) && vMsg
.msg
!= WM_QUIT
)
885 if (!wxTheApp
->DoMessage())
888 // If they are pending events, we must process them.
890 wxTheApp
->ProcessPendingEvents();
895 wxIcon
wxApp::GetStdIcon(
901 case wxICON_INFORMATION
:
902 return wxIcon("wxICON_INFO");
904 case wxICON_QUESTION
:
905 return wxIcon("wxICON_QUESTION");
907 case wxICON_EXCLAMATION
:
908 return wxIcon("wxICON_WARNING");
911 wxFAIL_MSG(wxT("requested non existent standard icon"));
912 // still fall through
915 return wxIcon("wxICON_ERROR");
917 return wxIcon("wxICON_ERROR");
920 HINSTANCE
wxGetInstance()
925 void wxSetInstance(HINSTANCE hInst
)