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
;
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
, UINT
, MPARAM
, MPARAM
);
97 // ===========================================================================
99 // ===========================================================================
101 // ---------------------------------------------------------------------------
103 // ---------------------------------------------------------------------------
105 #if !USE_SHARED_LIBRARY
106 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
108 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
109 EVT_IDLE(wxApp::OnIdle
)
110 EVT_END_SESSION(wxApp::OnEndSession
)
111 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
116 bool wxApp::Initialize(
120 // Some people may wish to use this, but
121 // probably it shouldn't be here by default.
123 // wxRedirectIOToConsole();
126 wxBuffer
= new wxChar
[1500]; // FIXME; why?
128 wxClassInfo::InitializeClasses();
131 wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion
);
134 // I'm annoyed ... I don't know where to put this and I don't want to
135 // create a module for that as it's part of the core.
136 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
137 wxTheColourDatabase
->Initialize();
139 wxInitializeStockLists();
140 wxInitializeStockObjects();
142 #if wxUSE_WX_RESOURCES
143 wxInitializeResourceSystem();
146 wxBitmap::InitStandardHandlers();
148 g_globalCursor
= new wxCursor
;
151 wxSTD_FRAME_ICON
= ::WinLoadFileIcon(wxT("wxSTD_FRAME"), TRUE
);
152 wxSTD_MDIPARENTFRAME_ICON
= ::WinLoadFileIcon(wxT("wxSTD_MDIPARENTFRAME"), TRUE
);
153 wxSTD_MDICHILDFRAME_ICON
= ::WinLoadFileIcon(wxT("wxSTD_MDICHILDFRAME"), TRUE
);
155 wxDEFAULT_FRAME_ICON
= ::WinLoadFileIcon(wxT("wxDEFAULT_FRAME"), TRUE
);
156 wxDEFAULT_MDIPARENTFRAME_ICON
= ::WinLoadFileIcon(wxT("wxDEFAULT_MDIPARENTFRAME"), TRUE
);
157 wxDEFAULT_MDICHILDFRAME_ICON
= ::WinLoadFileIcon(wxT("wxDEFAULT_MDICHILDFRAME"), TRUE
);
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)
165 extern char wxDummyChar
;
166 if (wxDummyChar
) wxDummyChar
++;
169 wxSetKeyboardHook(TRUE
);
171 wxModule::RegisterModules();
172 if (!wxModule::InitializeModules())
177 // ---------------------------------------------------------------------------
178 // RegisterWindowClasses
179 // ---------------------------------------------------------------------------
181 // TODO we should only register classes really used by the app. For this it
182 // would be enough to just delay the class registration until an attempt
183 // to create a window of this class is made.
184 bool wxApp::RegisterWindowClasses(
189 if (!::WinRegisterClass( vHab
192 ,CS_SIZEREDRAW
| CS_SYNCPAINT
| CS_HITTEST
| CS_CLIPCHILDREN
| CS_FRAME
196 wxLogLastError("RegisterClass(frame)");
201 if (!::WinRegisterClass( vHab
202 ,wxFrameClassNameNoRedraw
204 ,CS_HITTEST
| CS_CLIPCHILDREN
| CS_FRAME
208 wxLogLastError("RegisterClass(no redraw frame)");
213 if (!::WinRegisterClass( vHab
216 ,CS_SIZEREDRAW
| CS_SYNCPAINT
| CS_HITTEST
| CS_CLIPCHILDREN
| CS_FRAME
220 wxLogLastError("RegisterClass(MDI parent)");
225 if (!::WinRegisterClass( vHab
226 ,wxMDIFrameClassNameNoRedraw
228 ,CS_HITTEST
| CS_CLIPCHILDREN
| CS_FRAME
232 wxLogLastError("RegisterClass(no redraw MDI parent)");
237 if (!::WinRegisterClass( vHab
238 ,wxMDIChildFrameClassName
240 ,CS_MOVENOTIFY
| CS_SIZEREDRAW
| CS_SYNCPAINT
| CS_HITTEST
| CS_CLIPSIBLINGS
| CS_FRAME
244 wxLogLastError("RegisterClass(MDI child)");
249 if (!::WinRegisterClass( vHab
250 ,wxMDIChildFrameClassNameNoRedraw
252 ,CS_HITTEST
| CS_CLIPSIBLINGS
| CS_FRAME
256 wxLogLastError("RegisterClass(no redraw MDI child)");
261 if (!::WinRegisterClass( vHab
264 ,CS_MOVENOTIFY
| CS_SIZEREDRAW
| CS_HITTEST
| CS_CLIPSIBLINGS
| CS_SAVEBITS
| CS_SYNCPAINT
268 wxLogLastError("RegisterClass(Panel)");
273 if (!::WinRegisterClass( vHab
276 ,CS_MOVENOTIFY
| CS_SIZEREDRAW
| CS_HITTEST
| CS_CLIPSIBLINGS
| CS_SAVEBITS
| CS_SYNCPAINT
280 wxLogLastError("RegisterClass(Canvas)");
287 //// Cleans up any wxWindows internal structures left lying around
289 void wxApp::CleanUp()
294 // flush the logged messages if any and install a 'safer' log target: the
295 // default one (wxLogGui) can't be used after the resources are freed just
296 // below and the user suppliedo ne might be even more unsafe (using any
297 // wxWindows GUI function is unsafe starting from now)
298 wxLog::DontCreateOnDemand();
300 // this will flush the old messages if any
301 delete wxLog::SetActiveTarget(new wxLogStderr
);
304 // One last chance for pending objects to be cleaned up
305 wxTheApp
->DeletePendingObjects();
307 wxModule::CleanUpModules();
309 #if wxUSE_WX_RESOURCES
310 wxCleanUpResourceSystem();
312 // wxDefaultResourceTable->ClearTable();
315 // Indicate that the cursor can be freed, so that cursor won't be deleted
316 // by deleting the bitmap list before g_globalCursor goes out of scope
317 // (double deletion of the cursor).
318 wxSetCursor(wxNullCursor
);
319 delete g_globalCursor
;
320 g_globalCursor
= NULL
;
322 wxDeleteStockObjects();
324 // Destroy all GDI lists, etc.
325 wxDeleteStockLists();
327 delete wxTheColourDatabase
;
328 wxTheColourDatabase
= NULL
;
330 wxBitmap::CleanUpHandlers();
335 //// WINDOWS-SPECIFIC CLEANUP
337 wxSetKeyboardHook(FALSE
);
339 if (wxSTD_FRAME_ICON
)
340 ::WinFreeFileIcon(wxSTD_FRAME_ICON
);
341 if (wxSTD_MDICHILDFRAME_ICON
)
342 ::WinFreeFileIcon(wxSTD_MDICHILDFRAME_ICON
);
343 if (wxSTD_MDIPARENTFRAME_ICON
)
344 ::WinFreeFileIcon(wxSTD_MDIPARENTFRAME_ICON
);
346 if (wxDEFAULT_FRAME_ICON
)
347 ::WinFreeFileIcon(wxDEFAULT_FRAME_ICON
);
348 if (wxDEFAULT_MDICHILDFRAME_ICON
)
349 ::WinFreeFileIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
350 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
351 ::WinFreeFileIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
353 if ( wxDisableButtonBrush
)
355 // TODO: ::DeleteObject( wxDisableButtonBrush );
359 delete wxWinHandleList
;
361 // GL: I'm annoyed ... I don't know where to put this and I don't want to
362 // create a module for that as it's part of the core.
364 delete wxPendingEvents
;
365 delete wxPendingEventsLocker
;
366 // If we don't do the following, we get an apparent memory leak.
367 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
370 wxClassInfo::CleanUpClasses();
375 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
376 // At this point we want to check if there are any memory
377 // blocks that aren't part of the wxDebugContext itself,
378 // as a special case. Then when dumping we need to ignore
379 // wxDebugContext, too.
380 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
382 wxLogDebug(wxT("There were memory leaks."));
383 wxDebugContext::Dump();
384 wxDebugContext::PrintStatistics();
386 // wxDebugContext::SetStream(NULL, NULL);
390 // do it as the very last thing because everything else can log messages
391 delete wxLog::SetActiveTarget(NULL
);
402 if (!wxApp::Initialize(vHab
))
406 // create the application object or ensure that one already exists
410 wxCHECK_MSG( wxApp::GetInitializerFunction()
412 ,wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n")
415 wxAppInitializerFunction fnAppIni
= wxApp::GetInitializerFunction();
416 wxObject
* pTest_app
= fnAppIni();
418 wxTheApp
= (wxApp
*)pTest_app
;
420 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
421 wxTheApp
->argc
= argc
;
424 wxTheApp
->argv
= new wxChar
*[argc
+1];
430 wxTheApp
->argv
[nArgc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[nArgc
]));
433 wxTheApp
->argv
[nArgc
] = (wxChar
*)NULL
;
435 wxTheApp
->argv
= argv
;
438 wxString
sName(wxFileNameFromPath(argv
[0]));
440 wxStripExtension(sName
);
441 wxTheApp
->SetAppName(sName
);
445 if (!wxTheApp
->OnInitGui())
450 if (wxTheApp
->OnInit())
458 wxWindow
* pTopWindow
= wxTheApp
->GetTopWindow();
462 // Forcibly delete the window.
463 if (pTopWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
464 pTopWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
466 pTopWindow
->Close(TRUE
);
467 wxTheApp
->DeletePendingObjects();
472 wxTheApp
->SetTopWindow(NULL
);
481 bool wxApp::OnInitGui()
483 m_vHab
= WinInitialize(0);
484 m_hMq
= WinCreateMsgQueue(m_vHab
, 0);
490 // Static member initialization
492 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
498 m_wantDebugOutput
= TRUE
;
502 m_nPrintMode
= wxPRINT_WINDOWS
;
503 m_exitOnFrameDelete
= TRUE
;
509 // Delete command-line args
511 for (i
= 0; i
< argc
; i
++)
518 bool wxApp::Initialized()
527 // Get and process a message, returning FALSE if WM_QUIT
528 // received (and also set the flag telling the app to exit the main loop)
530 bool wxApp::DoMessage()
532 BOOL bRc
= ::WinGetMsg(m_vHab
, &m_vMsg
, HWND(NULL
), 0, 0);
537 m_bKeepGoing
= FALSE
;
542 // should never happen, but let's test for it nevertheless
543 wxLogLastError("GetMessage");
548 wxASSERT_MSG( wxThread::IsMain()
549 ,wxT("only the main thread can process Windows messages")
552 static bool sbHadGuiLock
= TRUE
;
553 static wxMsgArray svSavedMessages
;
556 // if a secondary thread owns is doing GUI calls, save all messages for
557 // later processing - we can't process them right now because it will
558 // lead to recursive library calls (and we're not reentrant)
560 if (!wxGuiOwnedByMainThread())
562 sbHadGuiLock
= FALSE
;
564 // leave out WM_COMMAND messages: too dangerous, sometimes
565 // the message will be processed twice
566 if ( !wxIsWaitingForThread() ||
567 svCurrentMsg
.msg
!= WM_COMMAND
)
569 svSavedMessages
.Add(svCurrentMsg
);
576 // have we just regained the GUI lock? if so, post all of the saved
579 // FIXME of course, it's not _exactly_ the same as processing the
580 // messages normally - expect some things to break...
586 size_t nCount
= svSavedMessages
.Count();
588 for (size_t n
= 0; n
< nCount
; n
++)
590 QMSG vMsg
= svSavedMessages
[n
];
592 if ( !ProcessMessage((WXMSG
*)&vMsg
) )
594 ::WinDispatchMsg(m_vHab
, &vMsg
);
597 svSavedMessages
.Empty();
600 #endif // wxUSE_THREADS
602 // Process the message
603 if (!ProcessMessage((WXMSG
*)&svCurrentMsg
) )
605 ::WinDispatchMsg(m_vHab
, (PQMSG
)&svCurrentMsg
);
611 //////////////////////////////////////////////////////////////////////////////
613 // Keep trying to process messages until WM_QUIT
616 // If there are messages to be processed, they will all be
617 // processed and OnIdle will not be called.
618 // When there are no more messages, OnIdle is called.
619 // If OnIdle requests more time,
620 // it will be repeatedly called so long as there are no pending messages.
621 // A 'feature' of this is that once OnIdle has decided that no more processing
622 // is required, then it won't get processing time until further messages
623 // are processed (it'll sit in DoMessage).
625 //////////////////////////////////////////////////////////////////////////////
626 int wxApp::MainLoop()
633 wxMutexGuiLeaveOrEnter();
634 #endif // wxUSE_THREADS
635 while (!::WinPeekMsg(m_vHab
, &svCurrentMsg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) &&
641 return (int)svCurrentMsg
.mp1
;
645 // Returns TRUE if more time is needed.
647 bool wxApp::ProcessIdle()
651 vEvent
.SetEventObject(this);
652 ProcessEvent(vEvent
);
653 return vEvent
.MoreRequested();
657 void wxApp::ProcessPendingEvents()
659 wxNode
* pNode
= wxPendingEvents
->First();
660 wxCriticalSectionLocker
vLocker(*wxPendingEventsLocker
);
664 wxEvtHandler
* pHandler
= (wxEvtHandler
*)pNode
->Data();
665 pHandler
->ProcessPendingEvents();
668 pNode
= wxPendingEvents
->First();
673 void wxApp::ExitMainLoop()
675 m_bKeepGoing
= FALSE
;
678 bool wxApp::Pending()
680 return (::WinPeekMsg(m_vHab
, (PQMSG
)&svCurrentMsg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) != 0);
683 void wxApp::Dispatch()
688 //////////////////////////////////////////////////////////////////////////////
690 // Give all windows a chance to preprocess
691 // the message. Some may have accelerator tables, or have
692 // MDI complications.
694 //////////////////////////////////////////////////////////////////////////////
695 bool wxApp::ProcessMessage(
699 QMSG
* vMsg
= (PQMSG
)pWxmsg
;
700 HWND hWnd
= vMsg
->hwnd
;
701 wxWindow
* pWndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
705 // for some composite controls (like a combobox), wndThis might be NULL
706 // because the subcontrol is not a wxWindow, but only the control itself
707 // is - try to catch this case
709 while (hWnd
&& !pWndThis
)
711 hWnd
= ::WinQueryWindow(hWnd
, QW_PARENT
);
712 pWndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
715 // Anyone for a non-translation message? Try youngest descendants first.
716 for (pWnd
= pWndThis
; pWnd
; pWnd
= pWnd
->GetParent())
718 if (pWnd
->OS2ProcessMessage(pWxmsg
))
728 static bool sbInOnIdle
= FALSE
;
731 // Avoid recursion (via ProcessEvent default case)
739 // 'Garbage' collection of windows deleted with Close().
741 DeletePendingObjects();
744 // flush the logged messages if any
745 wxLog
* pLog
= wxLog::GetActiveTarget();
747 if (pLog
!= NULL
&& pLog
->HasPendingMessages())
751 // Send OnIdle events to all windows
752 if (SendIdleEvents())
755 // SendIdleEvents() returns TRUE if at least one window requested more
758 rEvent
.RequestMore(TRUE
);
762 // If they are pending events, we must process them.
765 ProcessPendingEvents();
772 // **** please implement me! ****
773 // Wake up the idle handler processor, even if it is in another thread...
776 // Send idle event to all top-level windows
777 bool wxApp::SendIdleEvents()
779 bool bNeedMore
= FALSE
;
780 wxWindowList::Node
* pNode
= wxTopLevelWindows
.GetFirst();
784 wxWindow
* pWin
= pNode
->GetData();
786 if (SendIdleEvents(pWin
))
788 pNode
= pNode
->GetNext();
794 // Send idle event to window and all subwindows
796 bool wxApp::SendIdleEvents(
800 bool bNeedMore
= FALSE
;
803 vEvent
.SetEventObject(pWin
);
804 pWin
->GetEventHandler()->ProcessEvent(vEvent
);
806 if (vEvent
.MoreRequested())
809 wxNode
* pNode
= pWin
->GetChildren().First();
813 wxWindow
* pWin
= (wxWindow
*) pNode
->Data();
815 if (SendIdleEvents(pWin
))
817 pNode
= pNode
->Next();
822 void wxApp::DeletePendingObjects()
824 wxNode
* pNode
= wxPendingDelete
.First();
828 wxObject
* pObj
= (wxObject
*)pNode
->Data();
832 if (wxPendingDelete
.Member(pObj
))
836 // Deleting one object may have deleted other pending
837 // objects, so start from beginning of list again.
839 pNode
= wxPendingDelete
.First();
843 void wxApp::OnEndSession(
844 wxCloseEvent
& WXUNUSED(rEvent
))
847 GetTopWindow()->Close(TRUE
);
851 // Default behaviour: close the application with prompts. The
852 // user can veto the close, and therefore the end session.
854 void wxApp::OnQueryEndSession(
860 if (!GetTopWindow()->Close(!rEvent
.CanVeto()))
867 wxLogError(_("Fatal error: exiting"));
872 // Yield to incoming messages
877 // We want to go back to the main message loop
878 // if we see a WM_QUIT. (?)
879 while (::WinPeekMsg(vHab
, &vMsg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) && vMsg
.msg
!= WM_QUIT
)
881 if (!wxTheApp
->DoMessage())
884 // If they are pending events, we must process them.
886 wxTheApp
->ProcessPendingEvents();
891 wxIcon
wxApp::GetStdIcon(
897 case wxICON_INFORMATION
:
898 return wxIcon("wxICON_INFO");
900 case wxICON_QUESTION
:
901 return wxIcon("wxICON_QUESTION");
903 case wxICON_EXCLAMATION
:
904 return wxIcon("wxICON_WARNING");
907 wxFAIL_MSG(wxT("requested non existent standard icon"));
908 // still fall through
911 return wxIcon("wxICON_ERROR");
913 return wxIcon("wxICON_ERROR");
916 HINSTANCE
wxGetInstance()
921 void wxSetInstance(HINSTANCE hInst
)