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 wxCursor
* g_globalCursor
;
67 HINSTANCE wxhInstance
= 0;
69 wxApp
* wxTheApp
= NULL
;
72 // FIXME why not const? and not static?
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");
85 HICON wxSTD_FRAME_ICON
= (HICON
) NULL
;
86 HICON wxSTD_MDICHILDFRAME_ICON
= (HICON
) NULL
;
87 HICON wxSTD_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
89 HICON wxDEFAULT_FRAME_ICON
= (HICON
) NULL
;
90 HICON wxDEFAULT_MDICHILDFRAME_ICON
= (HICON
) NULL
;
91 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
93 HBRUSH wxDisableButtonBrush
= (HBRUSH
) 0;
95 MRESULT
wxWndProc( HWND
101 // ===========================================================================
103 // ===========================================================================
105 // ---------------------------------------------------------------------------
107 // ---------------------------------------------------------------------------
109 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
111 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
112 EVT_IDLE(wxApp::OnIdle
)
113 EVT_END_SESSION(wxApp::OnEndSession
)
114 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
118 bool wxApp::Initialize(
122 // Some people may wish to use this, but
123 // probably it shouldn't be here by default.
125 // wxRedirectIOToConsole();
128 wxBuffer
= new wxChar
[1500]; // FIXME; why?
130 wxClassInfo::InitializeClasses();
133 wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion
);
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();
141 wxInitializeStockLists();
142 wxInitializeStockObjects();
144 #if wxUSE_WX_RESOURCES
145 wxInitializeResourceSystem();
148 wxBitmap::InitStandardHandlers();
150 g_globalCursor
= new wxCursor
;
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
);
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
);
161 RegisterWindowClasses(vHab
);
162 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
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
++;
171 // wxSetKeyboardHook(TRUE);
173 wxModule::RegisterModules();
174 if (!wxModule::InitializeModules())
179 // ---------------------------------------------------------------------------
180 // RegisterWindowClasses
181 // ---------------------------------------------------------------------------
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(
192 if ((rc
= ::WinRegisterClass( vHab
195 ,CS_SIZEREDRAW
| CS_SYNCPAINT
| CS_HITTEST
| CS_CLIPCHILDREN
| CS_FRAME
199 wxLogLastError("RegisterClass(frame)");
204 if (!::WinRegisterClass( vHab
205 ,wxFrameClassNameNoRedraw
207 ,CS_HITTEST
| CS_CLIPCHILDREN
| CS_FRAME
211 wxLogLastError("RegisterClass(no redraw frame)");
216 if (!::WinRegisterClass( vHab
219 ,CS_SIZEREDRAW
| CS_SYNCPAINT
| CS_HITTEST
| CS_CLIPCHILDREN
| CS_FRAME
223 wxLogLastError("RegisterClass(MDI parent)");
228 if (!::WinRegisterClass( vHab
229 ,wxMDIFrameClassNameNoRedraw
231 ,CS_HITTEST
| CS_CLIPCHILDREN
| CS_FRAME
235 wxLogLastError("RegisterClass(no redraw MDI parent)");
240 if (!::WinRegisterClass( vHab
241 ,wxMDIChildFrameClassName
243 ,CS_MOVENOTIFY
| CS_SIZEREDRAW
| CS_SYNCPAINT
| CS_HITTEST
| CS_CLIPSIBLINGS
| CS_FRAME
247 wxLogLastError("RegisterClass(MDI child)");
252 if (!::WinRegisterClass( vHab
253 ,wxMDIChildFrameClassNameNoRedraw
255 ,CS_HITTEST
| CS_CLIPSIBLINGS
| CS_FRAME
259 wxLogLastError("RegisterClass(no redraw MDI child)");
264 if (!::WinRegisterClass( vHab
267 ,CS_MOVENOTIFY
| CS_SIZEREDRAW
| CS_HITTEST
| CS_CLIPSIBLINGS
| CS_SAVEBITS
| CS_SYNCPAINT
271 wxLogLastError("RegisterClass(Panel)");
276 if (!::WinRegisterClass( vHab
279 ,CS_MOVENOTIFY
| CS_SIZEREDRAW
| CS_HITTEST
| CS_CLIPSIBLINGS
| CS_SAVEBITS
| CS_SYNCPAINT
283 wxLogLastError("RegisterClass(Canvas)");
290 //// Cleans up any wxWindows internal structures left lying around
292 void wxApp::CleanUp()
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();
303 // this will flush the old messages if any
304 delete wxLog::SetActiveTarget(new wxLogStderr
);
307 // One last chance for pending objects to be cleaned up
308 wxTheApp
->DeletePendingObjects();
310 wxModule::CleanUpModules();
312 #if wxUSE_WX_RESOURCES
313 wxCleanUpResourceSystem();
315 // wxDefaultResourceTable->ClearTable();
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
;
325 wxDeleteStockObjects();
327 // Destroy all GDI lists, etc.
328 wxDeleteStockLists();
330 delete wxTheColourDatabase
;
331 wxTheColourDatabase
= NULL
;
333 wxBitmap::CleanUpHandlers();
338 //// WINDOWS-SPECIFIC CLEANUP
340 // wxSetKeyboardHook(FALSE);
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
);
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
);
356 if ( wxDisableButtonBrush
)
358 // TODO: ::DeleteObject( wxDisableButtonBrush );
362 delete wxWinHandleList
;
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.
367 delete wxPendingEvents
;
368 delete wxPendingEventsLocker
;
369 // If we don't do the following, we get an apparent memory leak.
370 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
373 wxClassInfo::CleanUpClasses();
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)
385 wxLogDebug(wxT("There were memory leaks."));
386 wxDebugContext::Dump();
387 wxDebugContext::PrintStatistics();
389 // wxDebugContext::SetStream(NULL, NULL);
393 // do it as the very last thing because everything else can log messages
394 delete wxLog::SetActiveTarget(NULL
);
405 if (!wxApp::Initialize(vHab
))
409 // create the application object or ensure that one already exists
413 wxCHECK_MSG( wxApp::GetInitializerFunction()
415 ,wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n")
418 wxAppInitializerFunction fnAppIni
= wxApp::GetInitializerFunction();
419 wxObject
* pTest_app
= fnAppIni();
421 wxTheApp
= (wxApp
*)pTest_app
;
423 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
424 wxTheApp
->argc
= argc
;
427 wxTheApp
->argv
= new wxChar
*[argc
+1];
433 wxTheApp
->argv
[nArgc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[nArgc
]));
436 wxTheApp
->argv
[nArgc
] = (wxChar
*)NULL
;
438 wxTheApp
->argv
= argv
;
441 wxString
sName(wxFileNameFromPath(argv
[0]));
443 wxStripExtension(sName
);
444 wxTheApp
->SetAppName(sName
);
448 if (!wxTheApp
->OnInitGui())
453 if (wxTheApp
->OnInit())
461 wxWindow
* pTopWindow
= wxTheApp
->GetTopWindow();
465 // Forcibly delete the window.
466 if (pTopWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
467 pTopWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
469 pTopWindow
->Close(TRUE
);
470 wxTheApp
->DeletePendingObjects();
475 wxTheApp
->SetTopWindow(NULL
);
484 bool wxApp::OnInitGui()
486 vHabmain
= WinInitialize(0);
487 m_hMq
= WinCreateMsgQueue(vHabmain
, 0);
493 // Static member initialization
495 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
501 m_wantDebugOutput
= TRUE
;
505 m_nPrintMode
= wxPRINT_WINDOWS
;
506 m_exitOnFrameDelete
= TRUE
;
513 // Delete command-line args
515 for (i
= 0; i
< argc
; i
++)
523 bool wxApp::Initialized()
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)
535 bool wxApp::DoMessage()
537 BOOL bRc
= ::WinGetMsg(vHabmain
, &m_vMsg
, HWND(NULL
), 0, 0);
542 m_bKeepGoing
= FALSE
;
547 // should never happen, but let's test for it nevertheless
548 wxLogLastError("GetMessage");
553 wxASSERT_MSG( wxThread::IsMain()
554 ,wxT("only the main thread can process Windows messages")
557 static bool sbHadGuiLock
= TRUE
;
558 static wxMsgArray svSavedMessages
;
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)
565 if (!wxGuiOwnedByMainThread())
567 sbHadGuiLock
= FALSE
;
569 // leave out WM_COMMAND messages: too dangerous, sometimes
570 // the message will be processed twice
571 if ( !wxIsWaitingForThread() ||
572 svCurrentMsg
.msg
!= WM_COMMAND
)
574 svSavedMessages
.Add(svCurrentMsg
);
581 // have we just regained the GUI lock? if so, post all of the saved
584 // FIXME of course, it's not _exactly_ the same as processing the
585 // messages normally - expect some things to break...
591 size_t nCount
= svSavedMessages
.Count();
593 for (size_t n
= 0; n
< nCount
; n
++)
595 QMSG vMsg
= svSavedMessages
[n
];
597 if ( !ProcessMessage((WXMSG
*)&vMsg
) )
599 ::WinDispatchMsg(vHabmain
, &vMsg
);
602 svSavedMessages
.Empty();
605 #endif // wxUSE_THREADS
607 // Process the message
608 if (!ProcessMessage((WXMSG
*)&svCurrentMsg
) )
610 ::WinDispatchMsg(vHabmain
, (PQMSG
)&svCurrentMsg
);
616 //////////////////////////////////////////////////////////////////////////////
618 // Keep trying to process messages until WM_QUIT
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).
630 //////////////////////////////////////////////////////////////////////////////
631 int wxApp::MainLoop()
638 wxMutexGuiLeaveOrEnter();
639 #endif // wxUSE_THREADS
640 while (!::WinPeekMsg(vHabmain
, &svCurrentMsg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) &&
646 return (int)svCurrentMsg
.mp1
;
650 // Returns TRUE if more time is needed.
652 bool wxApp::ProcessIdle()
656 vEvent
.SetEventObject(this);
657 ProcessEvent(vEvent
);
658 return vEvent
.MoreRequested();
662 void wxApp::ProcessPendingEvents()
664 wxNode
* pNode
= wxPendingEvents
->First();
665 wxCriticalSectionLocker
vLocker(*wxPendingEventsLocker
);
669 wxEvtHandler
* pHandler
= (wxEvtHandler
*)pNode
->Data();
670 pHandler
->ProcessPendingEvents();
673 pNode
= wxPendingEvents
->First();
678 void wxApp::ExitMainLoop()
680 m_bKeepGoing
= FALSE
;
683 bool wxApp::Pending()
685 return (::WinPeekMsg(vHabmain
, (PQMSG
)&svCurrentMsg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) != 0);
688 void wxApp::Dispatch()
693 //////////////////////////////////////////////////////////////////////////////
695 // Give all windows a chance to preprocess
696 // the message. Some may have accelerator tables, or have
697 // MDI complications.
699 //////////////////////////////////////////////////////////////////////////////
700 bool wxApp::ProcessMessage(
704 QMSG
* vMsg
= (PQMSG
)pWxmsg
;
705 HWND hWnd
= vMsg
->hwnd
;
706 wxWindow
* pWndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
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
714 while (hWnd
&& !pWndThis
)
716 hWnd
= ::WinQueryWindow(hWnd
, QW_PARENT
);
717 pWndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
720 // Anyone for a non-translation message? Try youngest descendants first.
721 for (pWnd
= pWndThis
; pWnd
; pWnd
= pWnd
->GetParent())
723 if (pWnd
->OS2ProcessMessage(pWxmsg
))
733 static bool sbInOnIdle
= FALSE
;
736 // Avoid recursion (via ProcessEvent default case)
744 // 'Garbage' collection of windows deleted with Close().
746 DeletePendingObjects();
749 // flush the logged messages if any
750 wxLog
* pLog
= wxLog::GetActiveTarget();
752 if (pLog
!= NULL
&& pLog
->HasPendingMessages())
756 // Send OnIdle events to all windows
757 if (SendIdleEvents())
760 // SendIdleEvents() returns TRUE if at least one window requested more
763 rEvent
.RequestMore(TRUE
);
767 // If they are pending events, we must process them.
770 ProcessPendingEvents();
777 // **** please implement me! ****
778 // Wake up the idle handler processor, even if it is in another thread...
781 // Send idle event to all top-level windows
782 bool wxApp::SendIdleEvents()
784 bool bNeedMore
= FALSE
;
785 wxWindowList::Node
* pNode
= wxTopLevelWindows
.GetFirst();
789 wxWindow
* pWin
= pNode
->GetData();
791 if (SendIdleEvents(pWin
))
793 pNode
= pNode
->GetNext();
799 // Send idle event to window and all subwindows
801 bool wxApp::SendIdleEvents(
805 bool bNeedMore
= FALSE
;
808 vEvent
.SetEventObject(pWin
);
809 pWin
->GetEventHandler()->ProcessEvent(vEvent
);
811 if (vEvent
.MoreRequested())
814 wxNode
* pNode
= pWin
->GetChildren().First();
818 wxWindow
* pWin
= (wxWindow
*) pNode
->Data();
820 if (SendIdleEvents(pWin
))
822 pNode
= pNode
->Next();
827 void wxApp::DeletePendingObjects()
829 wxNode
* pNode
= wxPendingDelete
.First();
833 wxObject
* pObj
= (wxObject
*)pNode
->Data();
837 if (wxPendingDelete
.Member(pObj
))
841 // Deleting one object may have deleted other pending
842 // objects, so start from beginning of list again.
844 pNode
= wxPendingDelete
.First();
848 void wxApp::OnEndSession(
849 wxCloseEvent
& WXUNUSED(rEvent
))
852 GetTopWindow()->Close(TRUE
);
856 // Default behaviour: close the application with prompts. The
857 // user can veto the close, and therefore the end session.
859 void wxApp::OnQueryEndSession(
865 if (!GetTopWindow()->Close(!rEvent
.CanVeto()))
872 wxLogError(_("Fatal error: exiting"));
877 // Yield to incoming messages
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
)
886 if (!wxTheApp
->DoMessage())
889 // If they are pending events, we must process them.
891 wxTheApp
->ProcessPendingEvents();
896 wxIcon
wxApp::GetStdIcon(
902 case wxICON_INFORMATION
:
903 return wxIcon("wxICON_INFO");
905 case wxICON_QUESTION
:
906 return wxIcon("wxICON_QUESTION");
908 case wxICON_EXCLAMATION
:
909 return wxIcon("wxICON_WARNING");
912 wxFAIL_MSG(wxT("requested non existent standard icon"));
913 // still fall through
916 return wxIcon("wxICON_ERROR");
918 return wxIcon("wxICON_ERROR");
921 HINSTANCE
wxGetInstance()
926 void wxSetInstance(HINSTANCE hInst
)