1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "app.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
27 #if defined(__BORLANDC__)
35 #include "wx/gdicmn.h"
38 #include "wx/cursor.h"
40 #include "wx/palette.h"
42 #include "wx/dialog.h"
43 #include "wx/msgdlg.h"
45 #include "wx/dynarray.h"
46 #include "wx/wxchar.h"
51 #include "wx/apptrait.h"
52 #include "wx/filename.h"
53 #include "wx/module.h"
54 #include "wx/dynlib.h"
56 #include "wx/msw/private.h"
57 #include "wx/msw/ole/oleutils.h"
60 #include "wx/tooltip.h"
61 #endif // wxUSE_TOOLTIPS
63 // OLE is used for drag-and-drop, clipboard, OLE Automation..., but some
64 // compilers don't support it (missing headers, libs, ...)
65 #if defined(__GNUWIN32_OLD__) || defined(__SYMANTEC__) || defined(__SALFORDC__)
69 #endif // broken compilers
71 #if defined(__POCKETPC__) || defined(__SMARTPHONE__)
82 #include "wx/msw/wrapcctl.h"
86 #include "wx/msw/wince/missing.h"
89 // For DLLVER_PLATFORM_WINDOWS
90 #if (!defined(__MINGW32__) || wxCHECK_W32API_VERSION( 2, 0 )) && \
91 !defined(__CYGWIN__) && !defined(__DIGITALMARS__) && !defined(__WXWINCE__) && \
92 (!defined(_MSC_VER) || (_MSC_VER > 1100))
96 // ---------------------------------------------------------------------------
98 // ---------------------------------------------------------------------------
100 extern wxList WXDLLEXPORT wxPendingDelete
;
102 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
103 extern void wxSetKeyboardHook(bool doIt
);
106 // NB: all "NoRedraw" classes must have the same names as the "normal" classes
107 // with NR suffix - wxWindow::MSWCreate() supposes this
109 WXDLLIMPEXP_CORE wxChar
*wxCanvasClassName
;
110 WXDLLIMPEXP_CORE wxChar
*wxCanvasClassNameNR
;
112 WXDLLIMPEXP_CORE
const wxChar
*wxCanvasClassName
= wxT("wxWindowClass");
113 WXDLLIMPEXP_CORE
const wxChar
*wxCanvasClassNameNR
= wxT("wxWindowClassNR");
115 WXDLLIMPEXP_CORE
const wxChar
*wxMDIFrameClassName
= wxT("wxMDIFrameClass");
116 WXDLLIMPEXP_CORE
const wxChar
*wxMDIFrameClassNameNoRedraw
= wxT("wxMDIFrameClassNR");
117 WXDLLIMPEXP_CORE
const wxChar
*wxMDIChildFrameClassName
= wxT("wxMDIChildFrameClass");
118 WXDLLIMPEXP_CORE
const wxChar
*wxMDIChildFrameClassNameNoRedraw
= wxT("wxMDIChildFrameClassNR");
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 LRESULT WXDLLEXPORT APIENTRY
wxWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
126 // ===========================================================================
127 // wxGUIAppTraits implementation
128 // ===========================================================================
130 // private class which we use to pass parameters from BeforeChildWaitLoop() to
131 // AfterChildWaitLoop()
132 struct ChildWaitLoopData
134 ChildWaitLoopData(wxWindowDisabler
*wd_
, wxWindow
*winActive_
)
137 winActive
= winActive_
;
140 wxWindowDisabler
*wd
;
144 void *wxGUIAppTraits::BeforeChildWaitLoop()
147 We use a dirty hack here to disable all application windows (which we
148 must do because otherwise the calls to wxYield() could lead to some very
149 unexpected reentrancies in the users code) but to avoid losing
150 focus/activation entirely when the child process terminates which would
151 happen if we simply disabled everything using wxWindowDisabler. Indeed,
152 remember that Windows will never activate a disabled window and when the
153 last childs window is closed and Windows looks for a window to activate
154 all our windows are still disabled. There is no way to enable them in
155 time because we don't know when the childs windows are going to be
156 closed, so the solution we use here is to keep one special tiny frame
157 enabled all the time. Then when the child terminates it will get
158 activated and when we close it below -- after reenabling all the other
159 windows! -- the previously active window becomes activated again and
164 // first disable all existing windows
165 wxWindowDisabler
*wd
= new wxWindowDisabler
;
167 // then create an "invisible" frame: it has minimal size, is positioned
168 // (hopefully) outside the screen and doesn't appear on the taskbar
169 wxWindow
*winActive
= new wxFrame
171 wxTheApp
->GetTopWindow(),
174 wxPoint(32600, 32600),
176 wxDEFAULT_FRAME_STYLE
| wxFRAME_NO_TASKBAR
180 return new ChildWaitLoopData(wd
, winActive
);
183 void wxGUIAppTraits::AlwaysYield()
188 void wxGUIAppTraits::AfterChildWaitLoop(void *dataOrig
)
192 ChildWaitLoopData
* const data
= (ChildWaitLoopData
*)dataOrig
;
196 // finally delete the dummy frame and, as wd has been already destroyed and
197 // the other windows reenabled, the activation is going to return to the
198 // window which had had it before
199 data
->winActive
->Destroy();
201 // also delete the temporary data object itself
205 bool wxGUIAppTraits::DoMessageFromThreadWait()
207 // we should return false only if the app should exit, i.e. only if
208 // Dispatch() determines that the main event loop should terminate
209 return !wxTheApp
|| wxTheApp
->Dispatch();
212 wxToolkitInfo
& wxGUIAppTraits::GetToolkitInfo()
214 static wxToolkitInfo info
;
215 wxToolkitInfo
& baseInfo
= wxAppTraits::GetToolkitInfo();
216 info
.versionMajor
= baseInfo
.versionMajor
;
217 info
.versionMinor
= baseInfo
.versionMinor
;
218 info
.os
= baseInfo
.os
;
219 info
.shortName
= _T("msw");
220 info
.name
= _T("wxMSW");
221 #ifdef __WXUNIVERSAL__
222 info
.shortName
<< _T("univ");
223 info
.name
<< _T("/wxUniversal");
228 // ===========================================================================
229 // wxApp implementation
230 // ===========================================================================
232 int wxApp::m_nCmdShow
= SW_SHOWNORMAL
;
234 // ---------------------------------------------------------------------------
236 // ---------------------------------------------------------------------------
238 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
240 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
241 EVT_IDLE(wxApp::OnIdle
)
242 EVT_END_SESSION(wxApp::OnEndSession
)
243 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
246 // class to ensure that wxAppBase::CleanUp() is called if our Initialize()
248 class wxCallBaseCleanup
251 wxCallBaseCleanup(wxApp
*app
) : m_app(app
) { }
252 ~wxCallBaseCleanup() { if ( m_app
) m_app
->wxAppBase::CleanUp(); }
254 void Dismiss() { m_app
= NULL
; }
261 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
263 if ( !wxAppBase::Initialize(argc
, argv
) )
266 // ensure that base cleanup is done if we return too early
267 wxCallBaseCleanup
callBaseCleanup(this);
270 wxString tmp
= GetAppName();
271 tmp
+= wxT("ClassName");
272 wxCanvasClassName
= wxStrdup( tmp
.c_str() );
274 wxCanvasClassNameNR
= wxStrdup( tmp
.c_str() );
275 HWND hWnd
= FindWindow( wxCanvasClassNameNR
, NULL
);
278 SetForegroundWindow( (HWND
)(((DWORD
)hWnd
)|0x01) );
283 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
284 InitCommonControls();
287 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
288 SHInitExtraControls();
293 RegisterWindowClasses();
299 wxWinHandleHash
= new wxWinHashTable(wxKEY_INTEGER
, 100);
301 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
302 wxSetKeyboardHook(true);
305 callBaseCleanup
.Dismiss();
310 // ---------------------------------------------------------------------------
311 // RegisterWindowClasses
312 // ---------------------------------------------------------------------------
314 // TODO we should only register classes really used by the app. For this it
315 // would be enough to just delay the class registration until an attempt
316 // to create a window of this class is made.
317 bool wxApp::RegisterWindowClasses()
320 wxZeroMemory(wndclass
);
322 // for each class we register one with CS_(V|H)REDRAW style and one
323 // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
324 static const long styleNormal
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
325 static const long styleNoRedraw
= CS_DBLCLKS
;
327 // the fields which are common to all classes
328 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
329 wndclass
.hInstance
= wxhInstance
;
330 wndclass
.hCursor
= ::LoadCursor((HINSTANCE
)NULL
, IDC_ARROW
);
332 // register the class for all normal windows
333 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_BTNFACE
+ 1);
334 wndclass
.lpszClassName
= wxCanvasClassName
;
335 wndclass
.style
= styleNormal
;
337 if ( !RegisterClass(&wndclass
) )
339 wxLogLastError(wxT("RegisterClass(frame)"));
343 wndclass
.lpszClassName
= wxCanvasClassNameNR
;
344 wndclass
.style
= styleNoRedraw
;
346 if ( !RegisterClass(&wndclass
) )
348 wxLogLastError(wxT("RegisterClass(no redraw frame)"));
351 // Register the MDI frame window class.
352 wndclass
.hbrBackground
= (HBRUSH
)NULL
; // paint MDI frame ourselves
353 wndclass
.lpszClassName
= wxMDIFrameClassName
;
354 wndclass
.style
= styleNormal
;
356 if ( !RegisterClass(&wndclass
) )
358 wxLogLastError(wxT("RegisterClass(MDI parent)"));
361 // "no redraw" MDI frame
362 wndclass
.lpszClassName
= wxMDIFrameClassNameNoRedraw
;
363 wndclass
.style
= styleNoRedraw
;
365 if ( !RegisterClass(&wndclass
) )
367 wxLogLastError(wxT("RegisterClass(no redraw MDI parent frame)"));
370 // Register the MDI child frame window class.
371 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
372 wndclass
.lpszClassName
= wxMDIChildFrameClassName
;
373 wndclass
.style
= styleNormal
;
375 if ( !RegisterClass(&wndclass
) )
377 wxLogLastError(wxT("RegisterClass(MDI child)"));
380 // "no redraw" MDI child frame
381 wndclass
.lpszClassName
= wxMDIChildFrameClassNameNoRedraw
;
382 wndclass
.style
= styleNoRedraw
;
384 if ( !RegisterClass(&wndclass
) )
386 wxLogLastError(wxT("RegisterClass(no redraw MDI child)"));
392 // ---------------------------------------------------------------------------
393 // UnregisterWindowClasses
394 // ---------------------------------------------------------------------------
396 bool wxApp::UnregisterWindowClasses()
400 #ifndef __WXMICROWIN__
401 // MDI frame window class.
402 if ( !::UnregisterClass(wxMDIFrameClassName
, wxhInstance
) )
404 wxLogLastError(wxT("UnregisterClass(MDI parent)"));
409 // "no redraw" MDI frame
410 if ( !::UnregisterClass(wxMDIFrameClassNameNoRedraw
, wxhInstance
) )
412 wxLogLastError(wxT("UnregisterClass(no redraw MDI parent frame)"));
417 // MDI child frame window class.
418 if ( !::UnregisterClass(wxMDIChildFrameClassName
, wxhInstance
) )
420 wxLogLastError(wxT("UnregisterClass(MDI child)"));
425 // "no redraw" MDI child frame
426 if ( !::UnregisterClass(wxMDIChildFrameClassNameNoRedraw
, wxhInstance
) )
428 wxLogLastError(wxT("UnregisterClass(no redraw MDI child)"));
434 if ( !::UnregisterClass(wxCanvasClassName
, wxhInstance
) )
436 wxLogLastError(wxT("UnregisterClass(canvas)"));
441 if ( !::UnregisterClass(wxCanvasClassNameNR
, wxhInstance
) )
443 wxLogLastError(wxT("UnregisterClass(no redraw canvas)"));
447 #endif // __WXMICROWIN__
452 void wxApp::CleanUp()
454 // all objects pending for deletion must be deleted first, otherwise we
455 // would crash when they use wxWinHandleHash (and UnregisterWindowClasses()
456 // call wouldn't succeed as long as any windows still exist), so call the
457 // base class method first and only then do our clean up
458 wxAppBase::CleanUp();
460 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
461 wxSetKeyboardHook(false);
470 // for an EXE the classes are unregistered when it terminates but DLL may
471 // be loaded several times (load/unload/load) into the same process in
472 // which case the registration will fail after the first time if we don't
473 // unregister the classes now
474 UnregisterWindowClasses();
476 delete wxWinHandleHash
;
477 wxWinHandleHash
= NULL
;
480 free( wxCanvasClassName
);
481 free( wxCanvasClassNameNR
);
485 // ----------------------------------------------------------------------------
487 // ----------------------------------------------------------------------------
491 m_printMode
= wxPRINT_WINDOWS
;
496 // our cmd line arguments are allocated inside wxEntry(HINSTANCE), they
497 // don't come from main(), so we have to free them
501 // m_argv elements were allocated by wxStrdup()
505 // but m_argv itself -- using new[]
509 // ----------------------------------------------------------------------------
510 // wxApp idle handling
511 // ----------------------------------------------------------------------------
513 void wxApp::OnIdle(wxIdleEvent
& event
)
515 wxAppBase::OnIdle(event
);
517 #if wxUSE_DC_CACHEING
518 // automated DC cache management: clear the cached DCs and bitmap
519 // if it's likely that the app has finished with them, that is, we
520 // get an idle event and we're not dragging anything.
521 if (!::GetKeyState(MK_LBUTTON
) && !::GetKeyState(MK_MBUTTON
) && !::GetKeyState(MK_RBUTTON
))
523 #endif // wxUSE_DC_CACHEING
526 void wxApp::WakeUpIdle()
528 // Send the top window a dummy message so idle handler processing will
529 // start up again. Doing it this way ensures that the idle handler
530 // wakes up in the right thread (see also wxWakeUpMainThread() which does
531 // the same for the main app thread only)
532 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
535 if ( !::PostMessage(GetHwndOf(topWindow
), WM_NULL
, 0, 0) )
537 // should never happen
538 wxLogLastError(wxT("PostMessage(WM_NULL)"));
543 // ----------------------------------------------------------------------------
544 // other wxApp event hanlders
545 // ----------------------------------------------------------------------------
547 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
550 GetTopWindow()->Close(true);
553 // Default behaviour: close the application with prompts. The
554 // user can veto the close, and therefore the end session.
555 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
559 if (!GetTopWindow()->Close(!event
.CanVeto()))
564 // ----------------------------------------------------------------------------
566 // ----------------------------------------------------------------------------
569 int wxApp::GetComCtl32Version()
571 #if defined(__WXMICROWIN__) || defined(__WXWINCE__)
576 // NB: this is MT-ok as in the worst case we'd compute s_verComCtl32 twice,
577 // but as its value should be the same both times it doesn't matter
578 static int s_verComCtl32
= -1;
580 if ( s_verComCtl32
== -1 )
582 // initally assume no comctl32.dll at all
585 // we're prepared to handle the errors
589 wxDynamicLibrary
dllComCtl32(_T("comctl32.dll"), wxDL_VERBATIM
);
591 // if so, then we can check for the version
592 if ( dllComCtl32
.IsLoaded() )
594 #ifndef DLLVER_PLATFORM_WINDOWS
595 typedef struct _DllVersionInfo
598 DWORD dwMajorVersion
; // Major version
599 DWORD dwMinorVersion
; // Minor version
600 DWORD dwBuildNumber
; // Build number
601 DWORD dwPlatformID
; // DLLVER_PLATFORM_*
603 typedef HRESULT (CALLBACK
* DLLGETVERSIONPROC
)(DLLVERSIONINFO
*);
605 // try to use DllGetVersion() if available in _headers_
606 wxDYNLIB_FUNCTION( DLLGETVERSIONPROC
, DllGetVersion
, dllComCtl32
);
607 if ( pfnDllGetVersion
)
610 dvi
.cbSize
= sizeof(dvi
);
612 HRESULT hr
= (*pfnDllGetVersion
)(&dvi
);
615 wxLogApiError(_T("DllGetVersion"), hr
);
619 // this is incompatible with _WIN32_IE values, but
620 // compatible with the other values returned by
621 // GetComCtl32Version()
622 s_verComCtl32
= 100*dvi
.dwMajorVersion
+
627 // if DllGetVersion() is unavailable either during compile or
628 // run-time, try to guess the version otherwise
629 if ( !s_verComCtl32
)
631 // InitCommonControlsEx is unique to 4.70 and later
632 void *pfn
= dllComCtl32
.GetSymbol(_T("InitCommonControlsEx"));
635 // not found, must be 4.00
640 // many symbols appeared in comctl32 4.71, could use any of
641 // them except may be DllInstall()
642 pfn
= dllComCtl32
.GetSymbol(_T("InitializeFlatSB"));
645 // not found, must be 4.70
650 // found, must be 4.71 or later
658 return s_verComCtl32
;
659 #endif // Microwin/!Microwin
662 // Yield to incoming messages
664 bool wxApp::Yield(bool onlyIfNeeded
)
667 static bool s_inYield
= false;
670 // disable log flushing from here because a call to wxYield() shouldn't
671 // normally result in message boxes popping up &c
679 wxFAIL_MSG( wxT("wxYield called recursively" ) );
687 // we don't want to process WM_QUIT from here - it should be processed in
688 // the main event loop in order to stop it
690 while ( PeekMessage(&msg
, (HWND
)0, 0, 0, PM_NOREMOVE
) &&
691 msg
.message
!= WM_QUIT
)
694 wxMutexGuiLeaveOrEnter();
695 #endif // wxUSE_THREADS
697 if ( !wxTheApp
->Dispatch() )
701 // if there are pending events, we must process them.
702 ProcessPendingEvents();
705 // let the logs be flashed again
716 // ----------------------------------------------------------------------------
717 // exception handling
718 // ----------------------------------------------------------------------------
720 bool wxApp::OnExceptionInMainLoop()
722 // ask the user about what to do: use the Win32 API function here as it
723 // could be dangerous to use any wxWidgets code in this state
728 _T("An unhandled exception occurred. Press \"Abort\" to \
729 terminate the program,\r\n\
730 \"Retry\" to exit the program normally and \"Ignore\" to try to continue."),
731 _T("Unhandled exception"),
732 MB_ABORTRETRYIGNORE
|
742 wxFAIL_MSG( _T("unexpected MessageBox() return code") );
753 #endif // wxUSE_EXCEPTIONS
755 // ----------------------------------------------------------------------------
756 // deprecated event loop functions
757 // ----------------------------------------------------------------------------
759 #if WXWIN_COMPATIBILITY_2_4
761 #include "wx/evtloop.h"
763 void wxApp::DoMessage(WXMSG
*pMsg
)
765 wxEventLoop
*evtLoop
= wxEventLoop::GetActive();
767 evtLoop
->ProcessMessage(pMsg
);
770 bool wxApp::DoMessage()
772 wxEventLoop
*evtLoop
= wxEventLoop::GetActive();
773 return evtLoop
? evtLoop
->Dispatch() : false;
776 bool wxApp::ProcessMessage(WXMSG
* pMsg
)
778 wxEventLoop
*evtLoop
= wxEventLoop::GetActive();
779 return evtLoop
&& evtLoop
->PreProcessMessage(pMsg
);
782 #endif // WXWIN_COMPATIBILITY_2_4