]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: app.cpp | |
3 | // Purpose: wxApp | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
a5e0e655 | 9 | // Licence: wxWindows license |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
e5c0b16a VZ |
12 | // =========================================================================== |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
2bda0e17 | 20 | #ifdef __GNUG__ |
e5c0b16a | 21 | #pragma implementation "app.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
2bda0e17 KB |
25 | #include "wx/wxprec.h" |
26 | ||
27 | #if defined(__BORLANDC__) | |
e5c0b16a | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #ifndef WX_PRECOMP | |
e5c0b16a VZ |
32 | #include "wx/frame.h" |
33 | #include "wx/app.h" | |
34 | #include "wx/utils.h" | |
35 | #include "wx/gdicmn.h" | |
36 | #include "wx/pen.h" | |
37 | #include "wx/brush.h" | |
38 | #include "wx/cursor.h" | |
39 | #include "wx/icon.h" | |
40 | #include "wx/palette.h" | |
41 | #include "wx/dc.h" | |
42 | #include "wx/dialog.h" | |
43 | #include "wx/msgdlg.h" | |
44 | #include "wx/intl.h" | |
45 | #include "wx/dynarray.h" | |
72cdf4c9 VZ |
46 | #include "wx/wxchar.h" |
47 | #include "wx/icon.h" | |
2bda0e17 KB |
48 | #endif |
49 | ||
2bda0e17 KB |
50 | #include "wx/log.h" |
51 | #include "wx/module.h" | |
4bf78aae | 52 | |
4286a5b5 RR |
53 | #include "wx/msw/private.h" |
54 | ||
4bf78aae | 55 | #if wxUSE_THREADS |
bee503b0 VZ |
56 | #include "wx/thread.h" |
57 | ||
58 | // define the array of MSG strutures | |
59 | WX_DECLARE_OBJARRAY(MSG, wxMsgArray); | |
60 | ||
61 | #include "wx/arrimpl.cpp" | |
62 | ||
63 | WX_DEFINE_OBJARRAY(wxMsgArray); | |
64 | #endif // wxUSE_THREADS | |
2bda0e17 | 65 | |
47d67540 | 66 | #if wxUSE_WX_RESOURCES |
e5c0b16a | 67 | #include "wx/resource.h" |
2bda0e17 KB |
68 | #endif |
69 | ||
8614c467 VZ |
70 | #if wxUSE_TOOLTIPS |
71 | #include "wx/tooltip.h" | |
72 | #endif // wxUSE_TOOLTIPS | |
73 | ||
c42404a5 VZ |
74 | // OLE is used for drag-and-drop, clipboard, OLE Automation..., but some |
75 | // compilers don't support it (missing headers, libs, ...) | |
76 | #if defined(__GNUWIN32_OLD__) || defined(__SC__) || defined(__SALFORDC__) | |
e5c0b16a VZ |
77 | #undef wxUSE_OLE |
78 | ||
79 | #define wxUSE_OLE 0 | |
80 | #endif // broken compilers | |
81 | ||
82 | #if wxUSE_OLE | |
6e0d9d43 | 83 | #include <ole2.h> |
d05237ea | 84 | #endif |
ce3ed50d | 85 | |
2bda0e17 | 86 | #include <string.h> |
a5e0e655 | 87 | #include <ctype.h> |
2bda0e17 | 88 | |
04ef50df | 89 | #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__) || defined(__WXMICROWIN__)) && !defined(__CYGWIN10__)) |
e5c0b16a | 90 | #include <commctrl.h> |
2bda0e17 KB |
91 | #endif |
92 | ||
04ef50df | 93 | #ifndef __WXMICROWIN__ |
6e0d9d43 | 94 | #include "wx/msw/msvcrt.h" |
04ef50df | 95 | #endif |
370382c7 | 96 | |
bdc72a22 VZ |
97 | // ---------------------------------------------------------------------------- |
98 | // conditional compilation | |
99 | // ---------------------------------------------------------------------------- | |
100 | ||
101 | // The macro _WIN32_IE is defined by commctrl.h (unless it had already been | |
102 | // defined before) and shows us what common control features are available | |
103 | // during the compile time (it doesn't mean that they will be available during | |
104 | // the run-time, use GetComCtl32Version() to test for them!). The possible | |
105 | // values are: | |
106 | // | |
107 | // 0x0200 for comctl32.dll 4.00 shipped with Win95/NT 4.0 | |
108 | // 0x0300 4.70 IE 3.x | |
109 | // 0x0400 4.71 IE 4.0 | |
110 | // 0x0401 4.72 IE 4.01 and Win98 | |
111 | // 0x0500 5.00 IE 5.x and NT 5.0 (Win2000) | |
112 | ||
113 | #ifndef _WIN32_IE | |
114 | // minimal set of features by default | |
115 | #define _WIN32_IE 0x0200 | |
116 | #endif | |
117 | ||
036bc7d9 VZ |
118 | #if _WIN32_IE >= 0x0300 |
119 | #include <shlwapi.h> | |
120 | #endif | |
121 | ||
e5c0b16a VZ |
122 | // --------------------------------------------------------------------------- |
123 | // global variables | |
124 | // --------------------------------------------------------------------------- | |
125 | ||
837e5743 | 126 | extern wxChar *wxBuffer; |
2bda0e17 | 127 | extern wxList *wxWinHandleList; |
cde9f08e | 128 | extern wxList WXDLLEXPORT wxPendingDelete; |
04ef50df | 129 | #ifndef __WXMICROWIN__ |
2bda0e17 | 130 | extern void wxSetKeyboardHook(bool doIt); |
04ef50df | 131 | #endif |
2bda0e17 | 132 | |
42e69d6b | 133 | MSG s_currentMsg; |
2bda0e17 KB |
134 | wxApp *wxTheApp = NULL; |
135 | ||
193fe989 VZ |
136 | // NB: all "NoRedraw" classes must have the same names as the "normal" classes |
137 | // with NR suffix - wxWindow::MSWCreate() supposes this | |
2ffa221c VZ |
138 | const wxChar *wxFrameClassName = wxT("wxFrameClass"); |
139 | const wxChar *wxFrameClassNameNoRedraw = wxT("wxFrameClassNR"); | |
140 | const wxChar *wxMDIFrameClassName = wxT("wxMDIFrameClass"); | |
141 | const wxChar *wxMDIFrameClassNameNoRedraw = wxT("wxMDIFrameClassNR"); | |
142 | const wxChar *wxMDIChildFrameClassName = wxT("wxMDIChildFrameClass"); | |
143 | const wxChar *wxMDIChildFrameClassNameNoRedraw = wxT("wxMDIChildFrameClassNR"); | |
144 | const wxChar *wxPanelClassName = wxT("wxPanelClass"); | |
d3f5d09d | 145 | const wxChar *wxPanelClassNameNR = wxT("wxPanelClassNR"); |
2ffa221c | 146 | const wxChar *wxCanvasClassName = wxT("wxCanvasClass"); |
d3f5d09d | 147 | const wxChar *wxCanvasClassNameNR = wxT("wxCanvasClassNR"); |
2bda0e17 | 148 | |
57c208c5 JS |
149 | HICON wxSTD_FRAME_ICON = (HICON) NULL; |
150 | HICON wxSTD_MDICHILDFRAME_ICON = (HICON) NULL; | |
151 | HICON wxSTD_MDIPARENTFRAME_ICON = (HICON) NULL; | |
2bda0e17 | 152 | |
57c208c5 JS |
153 | HICON wxDEFAULT_FRAME_ICON = (HICON) NULL; |
154 | HICON wxDEFAULT_MDICHILDFRAME_ICON = (HICON) NULL; | |
155 | HICON wxDEFAULT_MDIPARENTFRAME_ICON = (HICON) NULL; | |
2bda0e17 | 156 | |
57c208c5 | 157 | HBRUSH wxDisableButtonBrush = (HBRUSH) 0; |
2bda0e17 | 158 | |
3135f4a7 | 159 | LRESULT WXDLLEXPORT APIENTRY wxWndProc(HWND, UINT, WPARAM, LPARAM); |
2bda0e17 | 160 | |
bd3277fe VZ |
161 | // FIXME wxUSE_ON_FATAL_EXCEPTION is only supported for VC++ now because it |
162 | // needs compiler support for Win32 SEH. Others (especially Borland) | |
163 | // probably have it too, but I'm not sure about how it works | |
f6bcfd97 BP |
164 | // JACS: get 'Cannot use __try in functions that require unwinding |
165 | // in Unicode mode, so disabling. | |
166 | #if !defined(__VISUALC__) || defined(__WIN16__) || defined(UNICODE) | |
bd3277fe VZ |
167 | #undef wxUSE_ON_FATAL_EXCEPTION |
168 | #define wxUSE_ON_FATAL_EXCEPTION 0 | |
169 | #endif // VC++ | |
170 | ||
3b415ba4 VZ |
171 | #if wxUSE_ON_FATAL_EXCEPTION |
172 | static bool gs_handleExceptions = FALSE; | |
173 | #endif | |
174 | ||
e5c0b16a VZ |
175 | // =========================================================================== |
176 | // implementation | |
177 | // =========================================================================== | |
589f0e3e | 178 | |
e5c0b16a VZ |
179 | // --------------------------------------------------------------------------- |
180 | // wxApp | |
181 | // --------------------------------------------------------------------------- | |
182 | ||
f6bcfd97 | 183 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) |
e5c0b16a | 184 | |
f6bcfd97 BP |
185 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) |
186 | EVT_IDLE(wxApp::OnIdle) | |
187 | EVT_END_SESSION(wxApp::OnEndSession) | |
188 | EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession) | |
189 | END_EVENT_TABLE() | |
e5c0b16a | 190 | |
e5c0b16a | 191 | //// Initialize |
589f0e3e | 192 | bool wxApp::Initialize() |
2bda0e17 | 193 | { |
f6bcfd97 BP |
194 | // the first thing to do is to check if we're trying to run an Unicode |
195 | // program under Win9x - if so, abort right now as it has no chance to | |
196 | // work | |
197 | #if wxUSE_UNICODE | |
198 | if ( wxGetOsVersion() != wxWINDOWS_NT ) | |
199 | { | |
200 | // note that we can use MessageBoxW() as it's implemented even under | |
201 | // Win9x - OTOH, we can't use wxGetTranslation() because the file APIs | |
202 | // used by wxLocale are not | |
203 | ::MessageBox | |
204 | ( | |
205 | NULL, | |
206 | _T("This program uses Unicode and requires Windows NT/2000.\nProgram aborted."), | |
207 | _T("wxWindows Fatal Error"), | |
208 | MB_ICONERROR | MB_OK | |
209 | ); | |
210 | ||
211 | return FALSE; | |
212 | } | |
213 | #endif // wxUSE_UNICODE | |
214 | ||
c030b70f JS |
215 | // Some people may wish to use this, but |
216 | // probably it shouldn't be here by default. | |
217 | #ifdef __WXDEBUG__ | |
e5c0b16a | 218 | // wxRedirectIOToConsole(); |
c030b70f JS |
219 | #endif |
220 | ||
837e5743 | 221 | wxBuffer = new wxChar[1500]; // FIXME |
589f0e3e | 222 | |
aa0b7e1e | 223 | wxClassInfo::InitializeClasses(); |
589f0e3e | 224 | |
4d3a259a | 225 | #if wxUSE_THREADS |
8e193f38 | 226 | wxPendingEventsLocker = new wxCriticalSection; |
4d3a259a GL |
227 | #endif |
228 | ||
aa0b7e1e JS |
229 | wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING); |
230 | wxTheColourDatabase->Initialize(); | |
589f0e3e | 231 | |
aa0b7e1e JS |
232 | wxInitializeStockLists(); |
233 | wxInitializeStockObjects(); | |
589f0e3e | 234 | |
aa0b7e1e | 235 | #if wxUSE_WX_RESOURCES |
a5e0e655 | 236 | wxInitializeResourceSystem(); |
aa0b7e1e | 237 | #endif |
2bda0e17 | 238 | |
aa0b7e1e | 239 | wxBitmap::InitStandardHandlers(); |
2bda0e17 | 240 | |
04ef50df | 241 | #if defined(__WIN95__) && !defined(__WXMICROWIN__) |
a5e0e655 | 242 | InitCommonControls(); |
e5c0b16a | 243 | #endif // __WIN95__ |
2bda0e17 | 244 | |
8cb172b4 | 245 | #if wxUSE_OLE || wxUSE_DRAG_AND_DROP |
c49245f8 VZ |
246 | |
247 | #ifdef __WIN16__ | |
e5c0b16a | 248 | // for OLE, enlarge message queue to be as large as possible |
aa0b7e1e | 249 | int iMsg = 96; |
c49245f8 VZ |
250 | while (!SetMessageQueue(iMsg) && (iMsg -= 8)) |
251 | ; | |
252 | #endif // Win16 | |
8cb172b4 | 253 | |
a5e0e655 VZ |
254 | // we need to initialize OLE library |
255 | if ( FAILED(::OleInitialize(NULL)) ) | |
e5c0b16a | 256 | wxLogError(_("Cannot initialize OLE")); |
1e6feb95 | 257 | |
c49245f8 | 258 | #endif // wxUSE_OLE |
2bda0e17 | 259 | |
1f112209 | 260 | #if wxUSE_CTL3D |
a5e0e655 | 261 | if (!Ctl3dRegister(wxhInstance)) |
223d09f6 | 262 | wxLogError(wxT("Cannot register CTL3D")); |
2bda0e17 | 263 | |
a5e0e655 | 264 | Ctl3dAutoSubclass(wxhInstance); |
1e6feb95 | 265 | #endif // wxUSE_CTL3D |
2bda0e17 | 266 | |
87a1e308 VZ |
267 | // VZ: these icons are not in wx.rc anyhow (but should they?)! |
268 | #if 0 | |
223d09f6 KB |
269 | wxSTD_FRAME_ICON = LoadIcon(wxhInstance, wxT("wxSTD_FRAME")); |
270 | wxSTD_MDIPARENTFRAME_ICON = LoadIcon(wxhInstance, wxT("wxSTD_MDIPARENTFRAME")); | |
271 | wxSTD_MDICHILDFRAME_ICON = LoadIcon(wxhInstance, wxT("wxSTD_MDICHILDFRAME")); | |
2bda0e17 | 272 | |
223d09f6 KB |
273 | wxDEFAULT_FRAME_ICON = LoadIcon(wxhInstance, wxT("wxDEFAULT_FRAME")); |
274 | wxDEFAULT_MDIPARENTFRAME_ICON = LoadIcon(wxhInstance, wxT("wxDEFAULT_MDIPARENTFRAME")); | |
275 | wxDEFAULT_MDICHILDFRAME_ICON = LoadIcon(wxhInstance, wxT("wxDEFAULT_MDICHILDFRAME")); | |
87a1e308 | 276 | #endif // 0 |
2bda0e17 | 277 | |
aa0b7e1e | 278 | RegisterWindowClasses(); |
2bda0e17 | 279 | |
04ef50df | 280 | #ifndef __WXMICROWIN__ |
aa0b7e1e | 281 | // Create the brush for disabling bitmap buttons |
2bda0e17 | 282 | |
42e69d6b | 283 | LOGBRUSH lb; |
aa0b7e1e | 284 | lb.lbStyle = BS_PATTERN; |
223d09f6 | 285 | lb.lbHatch = (int)LoadBitmap( wxhInstance, wxT("wxDISABLE_BUTTON_BITMAP") ); |
3a5ffa81 VZ |
286 | if ( lb.lbHatch ) |
287 | { | |
288 | wxDisableButtonBrush = ::CreateBrushIndirect( & lb ); | |
289 | ::DeleteObject( (HGDIOBJ)lb.lbHatch ); | |
290 | } | |
291 | //else: wxWindows resources are probably not linked in | |
04ef50df | 292 | #endif |
2bda0e17 | 293 | |
aa0b7e1e | 294 | #if wxUSE_PENWINDOWS |
a5e0e655 | 295 | wxRegisterPenWin(); |
aa0b7e1e | 296 | #endif |
2bda0e17 | 297 | |
aa0b7e1e | 298 | wxWinHandleList = new wxList(wxKEY_INTEGER); |
2bda0e17 | 299 | |
6e0d9d43 | 300 | // This is to foil optimizations in Visual C++ that throw out dummy.obj. |
8cbd2bde | 301 | // PLEASE DO NOT ALTER THIS. |
7fee680b | 302 | #if defined(__VISUALC__) && defined(__WIN16__) && !defined(WXMAKINGDLL) |
a5e0e655 VZ |
303 | extern char wxDummyChar; |
304 | if (wxDummyChar) wxDummyChar++; | |
aa0b7e1e | 305 | #endif |
a5e0e655 | 306 | |
04ef50df | 307 | #ifndef __WXMICROWIN__ |
aa0b7e1e | 308 | wxSetKeyboardHook(TRUE); |
04ef50df | 309 | #endif |
2bda0e17 | 310 | |
aa0b7e1e JS |
311 | wxModule::RegisterModules(); |
312 | if (!wxModule::InitializeModules()) | |
313 | return FALSE; | |
314 | return TRUE; | |
2bda0e17 KB |
315 | } |
316 | ||
42e69d6b VZ |
317 | // --------------------------------------------------------------------------- |
318 | // RegisterWindowClasses | |
319 | // --------------------------------------------------------------------------- | |
589f0e3e | 320 | |
b782f2e0 VZ |
321 | // TODO we should only register classes really used by the app. For this it |
322 | // would be enough to just delay the class registration until an attempt | |
323 | // to create a window of this class is made. | |
bb6290e3 | 324 | bool wxApp::RegisterWindowClasses() |
2bda0e17 | 325 | { |
42e69d6b | 326 | WNDCLASS wndclass; |
e5c0b16a | 327 | |
193fe989 VZ |
328 | // for each class we register one with CS_(V|H)REDRAW style and one |
329 | // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag | |
330 | static const long styleNormal = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; | |
331 | static const long styleNoRedraw = CS_DBLCLKS; | |
332 | ||
42e69d6b | 333 | // the fields which are common to all classes |
e5c0b16a VZ |
334 | wndclass.lpfnWndProc = (WNDPROC)wxWndProc; |
335 | wndclass.cbClsExtra = 0; | |
193fe989 | 336 | wndclass.cbWndExtra = sizeof( DWORD ); // VZ: what is this DWORD used for? |
e5c0b16a | 337 | wndclass.hInstance = wxhInstance; |
42e69d6b VZ |
338 | wndclass.hIcon = (HICON) NULL; |
339 | wndclass.hCursor = ::LoadCursor((HINSTANCE)NULL, IDC_ARROW); | |
e5c0b16a | 340 | wndclass.lpszMenuName = NULL; |
42e69d6b VZ |
341 | |
342 | // Register the frame window class. | |
343 | wndclass.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE + 1); | |
e5c0b16a | 344 | wndclass.lpszClassName = wxFrameClassName; |
b782f2e0 | 345 | wndclass.style = styleNormal; |
e5c0b16a | 346 | |
42e69d6b | 347 | if ( !RegisterClass(&wndclass) ) |
e5c0b16a | 348 | { |
f6bcfd97 | 349 | wxLogLastError(wxT("RegisterClass(frame)")); |
42e69d6b VZ |
350 | |
351 | return FALSE; | |
e5c0b16a VZ |
352 | } |
353 | ||
193fe989 VZ |
354 | // "no redraw" frame |
355 | wndclass.lpszClassName = wxFrameClassNameNoRedraw; | |
356 | wndclass.style = styleNoRedraw; | |
357 | ||
358 | if ( !RegisterClass(&wndclass) ) | |
359 | { | |
f6bcfd97 | 360 | wxLogLastError(wxT("RegisterClass(no redraw frame)")); |
193fe989 VZ |
361 | |
362 | return FALSE; | |
363 | } | |
364 | ||
e5c0b16a | 365 | // Register the MDI frame window class. |
42e69d6b | 366 | wndclass.hbrBackground = (HBRUSH)NULL; // paint MDI frame ourselves |
b782f2e0 VZ |
367 | wndclass.lpszClassName = wxMDIFrameClassName; |
368 | wndclass.style = styleNormal; | |
42e69d6b VZ |
369 | |
370 | if ( !RegisterClass(&wndclass) ) | |
e5c0b16a | 371 | { |
f6bcfd97 | 372 | wxLogLastError(wxT("RegisterClass(MDI parent)")); |
42e69d6b VZ |
373 | |
374 | return FALSE; | |
e5c0b16a VZ |
375 | } |
376 | ||
193fe989 | 377 | // "no redraw" MDI frame |
b782f2e0 | 378 | wndclass.lpszClassName = wxMDIFrameClassNameNoRedraw; |
193fe989 VZ |
379 | wndclass.style = styleNoRedraw; |
380 | ||
381 | if ( !RegisterClass(&wndclass) ) | |
382 | { | |
f6bcfd97 | 383 | wxLogLastError(wxT("RegisterClass(no redraw MDI parent frame)")); |
193fe989 VZ |
384 | |
385 | return FALSE; | |
386 | } | |
387 | ||
e5c0b16a | 388 | // Register the MDI child frame window class. |
42e69d6b VZ |
389 | wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); |
390 | wndclass.lpszClassName = wxMDIChildFrameClassName; | |
b782f2e0 | 391 | wndclass.style = styleNormal; |
42e69d6b VZ |
392 | |
393 | if ( !RegisterClass(&wndclass) ) | |
e5c0b16a | 394 | { |
f6bcfd97 | 395 | wxLogLastError(wxT("RegisterClass(MDI child)")); |
42e69d6b VZ |
396 | |
397 | return FALSE; | |
e5c0b16a VZ |
398 | } |
399 | ||
193fe989 VZ |
400 | // "no redraw" MDI child frame |
401 | wndclass.lpszClassName = wxMDIChildFrameClassNameNoRedraw; | |
402 | wndclass.style = styleNoRedraw; | |
403 | ||
404 | if ( !RegisterClass(&wndclass) ) | |
405 | { | |
f6bcfd97 | 406 | wxLogLastError(wxT("RegisterClass(no redraw MDI child)")); |
193fe989 VZ |
407 | |
408 | return FALSE; | |
409 | } | |
410 | ||
e5c0b16a | 411 | // Register the panel window class. |
42e69d6b VZ |
412 | wndclass.hbrBackground = (HBRUSH) GetStockObject( LTGRAY_BRUSH ); |
413 | wndclass.lpszClassName = wxPanelClassName; | |
b782f2e0 | 414 | wndclass.style = styleNormal; |
42e69d6b VZ |
415 | |
416 | if ( !RegisterClass(&wndclass) ) | |
e5c0b16a | 417 | { |
f6bcfd97 | 418 | wxLogLastError(wxT("RegisterClass(panel)")); |
42e69d6b VZ |
419 | |
420 | return FALSE; | |
e5c0b16a VZ |
421 | } |
422 | ||
d3f5d09d UM |
423 | // Register the no redraw panel window class. |
424 | wndclass.lpszClassName = wxPanelClassNameNR; | |
425 | wndclass.style = styleNoRedraw; | |
426 | ||
427 | if ( !RegisterClass(&wndclass) ) | |
428 | { | |
f6bcfd97 | 429 | wxLogLastError(wxT("RegisterClass(no redraw panel)")); |
d3f5d09d UM |
430 | |
431 | return FALSE; | |
432 | } | |
433 | ||
e5c0b16a | 434 | // Register the canvas and textsubwindow class name |
42e69d6b VZ |
435 | wndclass.hbrBackground = (HBRUSH)NULL; |
436 | wndclass.lpszClassName = wxCanvasClassName; | |
437 | ||
438 | if ( !RegisterClass(&wndclass) ) | |
e5c0b16a | 439 | { |
f6bcfd97 | 440 | wxLogLastError(wxT("RegisterClass(canvas)")); |
42e69d6b VZ |
441 | |
442 | return FALSE; | |
e5c0b16a VZ |
443 | } |
444 | ||
d3f5d09d UM |
445 | wndclass.lpszClassName = wxCanvasClassNameNR; |
446 | wndclass.style = styleNoRedraw; | |
447 | if ( !RegisterClass(&wndclass) ) | |
448 | { | |
f6bcfd97 | 449 | wxLogLastError(wxT("RegisterClass(no redraw canvas)")); |
d3f5d09d UM |
450 | |
451 | return FALSE; | |
452 | } | |
453 | ||
e5c0b16a | 454 | return TRUE; |
2bda0e17 KB |
455 | } |
456 | ||
42e69d6b VZ |
457 | // --------------------------------------------------------------------------- |
458 | // Convert Windows to argc, argv style | |
459 | // --------------------------------------------------------------------------- | |
589f0e3e JS |
460 | |
461 | void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine) | |
462 | { | |
da36f544 | 463 | wxStringList args; |
589f0e3e | 464 | |
da36f544 JS |
465 | wxString cmdLine(lpCmdLine); |
466 | int count = 0; | |
589f0e3e | 467 | |
da36f544 | 468 | // Get application name |
837e5743 | 469 | wxChar name[260]; // 260 is MAX_PATH value from windef.h |
da36f544 JS |
470 | ::GetModuleFileName(wxhInstance, name, WXSIZEOF(name)); |
471 | ||
da36f544 | 472 | args.Add(name); |
50ef256e | 473 | count++; |
589f0e3e | 474 | |
837e5743 | 475 | wxStrcpy(name, wxFileNameFromPath(name)); |
da36f544 JS |
476 | wxStripExtension(name); |
477 | wxTheApp->SetAppName(name); | |
589f0e3e | 478 | |
da36f544 JS |
479 | // Break up string |
480 | // Treat strings enclosed in double-quotes as single arguments | |
481 | int i = 0; | |
482 | int len = cmdLine.Length(); | |
483 | while (i < len) | |
a5e0e655 | 484 | { |
da36f544 | 485 | // Skip whitespace |
837e5743 | 486 | while ((i < len) && wxIsspace(cmdLine.GetChar(i))) |
da36f544 JS |
487 | i ++; |
488 | ||
489 | if (i < len) | |
490 | { | |
223d09f6 | 491 | if (cmdLine.GetChar(i) == wxT('"')) // We found the start of a string |
da36f544 JS |
492 | { |
493 | i ++; | |
494 | int first = i; | |
223d09f6 | 495 | while ((i < len) && (cmdLine.GetChar(i) != wxT('"'))) |
da36f544 JS |
496 | i ++; |
497 | ||
498 | wxString arg(cmdLine.Mid(first, (i - first))); | |
499 | ||
500 | args.Add(arg); | |
501 | count ++; | |
502 | ||
503 | if (i < len) | |
504 | i ++; // Skip past 2nd quote | |
505 | } | |
506 | else // Unquoted argument | |
507 | { | |
508 | int first = i; | |
837e5743 | 509 | while ((i < len) && !wxIsspace(cmdLine.GetChar(i))) |
da36f544 JS |
510 | i ++; |
511 | ||
512 | wxString arg(cmdLine.Mid(first, (i - first))); | |
513 | ||
514 | args.Add(arg); | |
515 | count ++; | |
516 | } | |
517 | } | |
a5e0e655 | 518 | } |
a5e0e655 | 519 | |
837e5743 | 520 | wxTheApp->argv = new wxChar*[count + 1]; |
da36f544 | 521 | for (i = 0; i < count; i++) |
a5e0e655 | 522 | { |
da36f544 | 523 | wxString arg(args[i]); |
837e5743 | 524 | wxTheApp->argv[i] = copystring((const wxChar*)arg); |
a5e0e655 | 525 | } |
da36f544 JS |
526 | wxTheApp->argv[count] = NULL; // argv[] is a NULL-terminated list |
527 | wxTheApp->argc = count; | |
589f0e3e JS |
528 | } |
529 | ||
530 | //// Cleans up any wxWindows internal structures left lying around | |
531 | ||
bb6290e3 | 532 | void wxApp::CleanUp() |
2bda0e17 | 533 | { |
e5c0b16a VZ |
534 | //// COMMON CLEANUP |
535 | ||
546db2a8 | 536 | #if wxUSE_LOG |
e5c0b16a VZ |
537 | // flush the logged messages if any and install a 'safer' log target: the |
538 | // default one (wxLogGui) can't be used after the resources are freed just | |
539 | // below and the user suppliedo ne might be even more unsafe (using any | |
540 | // wxWindows GUI function is unsafe starting from now) | |
541 | wxLog::DontCreateOnDemand(); | |
f5e5bd66 | 542 | |
e5c0b16a VZ |
543 | // this will flush the old messages if any |
544 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
546db2a8 | 545 | #endif // wxUSE_LOG |
f5e5bd66 | 546 | |
e5c0b16a VZ |
547 | // One last chance for pending objects to be cleaned up |
548 | wxTheApp->DeletePendingObjects(); | |
f5e5bd66 | 549 | |
e5c0b16a | 550 | wxModule::CleanUpModules(); |
2bda0e17 | 551 | |
47d67540 | 552 | #if wxUSE_WX_RESOURCES |
e5c0b16a | 553 | wxCleanUpResourceSystem(); |
589f0e3e | 554 | |
e5c0b16a | 555 | // wxDefaultResourceTable->ClearTable(); |
589f0e3e JS |
556 | #endif |
557 | ||
42e69d6b | 558 | wxDeleteStockObjects(); |
589f0e3e | 559 | |
e5c0b16a VZ |
560 | // Destroy all GDI lists, etc. |
561 | wxDeleteStockLists(); | |
589f0e3e | 562 | |
e5c0b16a VZ |
563 | delete wxTheColourDatabase; |
564 | wxTheColourDatabase = NULL; | |
589f0e3e | 565 | |
e5c0b16a | 566 | wxBitmap::CleanUpHandlers(); |
589f0e3e | 567 | |
e5c0b16a VZ |
568 | delete[] wxBuffer; |
569 | wxBuffer = NULL; | |
589f0e3e | 570 | |
e5c0b16a | 571 | //// WINDOWS-SPECIFIC CLEANUP |
2bda0e17 | 572 | |
04ef50df | 573 | #ifndef __WXMICROWIN__ |
e5c0b16a | 574 | wxSetKeyboardHook(FALSE); |
04ef50df | 575 | #endif |
2bda0e17 | 576 | |
47d67540 | 577 | #if wxUSE_PENWINDOWS |
e5c0b16a | 578 | wxCleanUpPenWin(); |
2bda0e17 KB |
579 | #endif |
580 | ||
e5c0b16a VZ |
581 | if (wxSTD_FRAME_ICON) |
582 | DestroyIcon(wxSTD_FRAME_ICON); | |
583 | if (wxSTD_MDICHILDFRAME_ICON) | |
584 | DestroyIcon(wxSTD_MDICHILDFRAME_ICON); | |
585 | if (wxSTD_MDIPARENTFRAME_ICON) | |
586 | DestroyIcon(wxSTD_MDIPARENTFRAME_ICON); | |
587 | ||
588 | if (wxDEFAULT_FRAME_ICON) | |
589 | DestroyIcon(wxDEFAULT_FRAME_ICON); | |
590 | if (wxDEFAULT_MDICHILDFRAME_ICON) | |
591 | DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON); | |
592 | if (wxDEFAULT_MDIPARENTFRAME_ICON) | |
593 | DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON); | |
594 | ||
595 | if ( wxDisableButtonBrush ) | |
42e69d6b | 596 | ::DeleteObject( wxDisableButtonBrush ); |
e5c0b16a VZ |
597 | |
598 | #if wxUSE_OLE | |
599 | ::OleUninitialize(); | |
5de5db0e | 600 | #endif |
2bda0e17 | 601 | |
1f112209 | 602 | #if wxUSE_CTL3D |
e5c0b16a | 603 | Ctl3dUnregister(wxhInstance); |
2bda0e17 KB |
604 | #endif |
605 | ||
e5c0b16a | 606 | if (wxWinHandleList) |
42e69d6b | 607 | delete wxWinHandleList; |
d50b2a58 | 608 | |
3135f4a7 | 609 | // GL: I'm annoyed ... I don't know where to put this and I don't want to |
4d3a259a | 610 | // create a module for that as it's part of the core. |
4d3a259a | 611 | delete wxPendingEvents; |
1e6feb95 | 612 | |
8e193f38 | 613 | #if wxUSE_THREADS |
4d3a259a | 614 | delete wxPendingEventsLocker; |
1e6feb95 | 615 | // If we don't do the following, we get an apparent memory leak |
f80eabe5 | 616 | #if wxUSE_VALIDATORS |
63863e09 | 617 | ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker(); |
1e6feb95 VZ |
618 | #endif // wxUSE_VALIDATORS |
619 | #endif // wxUSE_THREADS | |
4d3a259a | 620 | |
e5c0b16a | 621 | wxClassInfo::CleanUpClasses(); |
0c32066b | 622 | |
e5c0b16a VZ |
623 | delete wxTheApp; |
624 | wxTheApp = NULL; | |
184b5d99 JS |
625 | |
626 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT | |
e5c0b16a VZ |
627 | // At this point we want to check if there are any memory |
628 | // blocks that aren't part of the wxDebugContext itself, | |
629 | // as a special case. Then when dumping we need to ignore | |
630 | // wxDebugContext, too. | |
d13c32e9 | 631 | if (wxDebugContext::CountObjectsLeft(TRUE) > 0) |
e5c0b16a | 632 | { |
5fa399c9 | 633 | wxLogMessage(wxT("There were memory leaks.")); |
e5c0b16a VZ |
634 | wxDebugContext::Dump(); |
635 | wxDebugContext::PrintStatistics(); | |
636 | } | |
637 | // wxDebugContext::SetStream(NULL, NULL); | |
184b5d99 JS |
638 | #endif |
639 | ||
546db2a8 | 640 | #if wxUSE_LOG |
e5c0b16a VZ |
641 | // do it as the very last thing because everything else can log messages |
642 | delete wxLog::SetActiveTarget(NULL); | |
546db2a8 | 643 | #endif // wxUSE_LOG |
2bda0e17 KB |
644 | } |
645 | ||
7ece89c6 RD |
646 | //---------------------------------------------------------------------- |
647 | // Entry point helpers, used by wxPython | |
648 | //---------------------------------------------------------------------- | |
649 | ||
f6bcfd97 | 650 | int WXDLLEXPORT wxEntryStart( int WXUNUSED(argc), char** WXUNUSED(argv) ) |
7ece89c6 RD |
651 | { |
652 | return wxApp::Initialize(); | |
653 | } | |
654 | ||
f6bcfd97 | 655 | int WXDLLEXPORT wxEntryInitGui() |
7ece89c6 | 656 | { |
1e6feb95 | 657 | return wxTheApp->OnInitGui(); |
7ece89c6 RD |
658 | } |
659 | ||
660 | void WXDLLEXPORT wxEntryCleanup() | |
661 | { | |
662 | wxApp::CleanUp(); | |
663 | } | |
664 | ||
665 | ||
2bda0e17 KB |
666 | #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL)) |
667 | ||
e5c0b16a VZ |
668 | // temporarily disable this warning which would be generated in release builds |
669 | // because of __try | |
3f4a0c5b | 670 | #ifdef __VISUALC__ |
f8a3e080 VZ |
671 | #pragma warning(disable: 4715) // not all control paths return a value |
672 | #endif // Visual C++ | |
673 | ||
7ece89c6 RD |
674 | //---------------------------------------------------------------------- |
675 | // Main wxWindows entry point | |
676 | //---------------------------------------------------------------------- | |
a5e0e655 VZ |
677 | int wxEntry(WXHINSTANCE hInstance, |
678 | WXHINSTANCE WXUNUSED(hPrevInstance), | |
679 | char *lpCmdLine, | |
680 | int nCmdShow, | |
681 | bool enterLoop) | |
2bda0e17 | 682 | { |
6e0d9d43 VZ |
683 | // do check for memory leaks on program exit |
684 | // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free | |
685 | // deallocated memory which may be used to simulate low-memory condition) | |
04ef50df | 686 | #ifndef __WXMICROWIN__ |
6e0d9d43 | 687 | wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); |
04ef50df JS |
688 | #endif |
689 | ||
6418cb93 SC |
690 | #ifdef __MWERKS__ |
691 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT | |
692 | // This seems to be necessary since there are 'rogue' | |
693 | // objects present at this point (perhaps global objects?) | |
694 | // Setting a checkpoint will ignore them as far as the | |
695 | // memory checking facility is concerned. | |
696 | // Of course you may argue that memory allocated in globals should be | |
697 | // checked, but this is a reasonable compromise. | |
698 | wxDebugContext::SetCheckpoint(); | |
699 | #endif | |
700 | #endif | |
3b415ba4 VZ |
701 | |
702 | // take everything into a try-except block to be able to call | |
703 | // OnFatalException() if necessary | |
3b415ba4 | 704 | #if wxUSE_ON_FATAL_EXCEPTION |
3f4a0c5b | 705 | __try { |
a5e0e655 | 706 | #endif |
e5c0b16a | 707 | wxhInstance = (HINSTANCE) hInstance; |
2bda0e17 | 708 | |
7ece89c6 | 709 | if (!wxEntryStart(0,0)) |
e5c0b16a | 710 | return 0; |
2bda0e17 | 711 | |
e5c0b16a VZ |
712 | // create the application object or ensure that one already exists |
713 | if (!wxTheApp) | |
714 | { | |
715 | // The app may have declared a global application object, but we recommend | |
716 | // the IMPLEMENT_APP macro is used instead, which sets an initializer | |
717 | // function for delayed, dynamic app object construction. | |
718 | wxCHECK_MSG( wxApp::GetInitializerFunction(), 0, | |
223d09f6 | 719 | wxT("No initializer - use IMPLEMENT_APP macro.") ); |
f8a3e080 | 720 | |
8cb172b4 | 721 | wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) (); |
e5c0b16a VZ |
722 | } |
723 | ||
223d09f6 | 724 | wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") ); |
e5c0b16a VZ |
725 | |
726 | // save the WinMain() parameters | |
727 | wxTheApp->ConvertToStandardCommandArgs(lpCmdLine); | |
728 | wxTheApp->m_nCmdShow = nCmdShow; | |
a5e0e655 | 729 | |
36edded9 JS |
730 | // We really don't want timestamps by default, because it means |
731 | // we can't simply double-click on the error message and get to that | |
732 | // line in the source. So VC++ at least, let's have a sensible default. | |
733 | #ifdef __VISUALC__ | |
734 | wxLog::SetTimestamp(NULL); | |
735 | #endif | |
736 | ||
e5c0b16a VZ |
737 | int retValue = 0; |
738 | ||
6afafc42 VZ |
739 | // it is common to create a modal dialog in OnInit() (to ask/notify the |
740 | // user about something) but it wouldn't work if we don't change the | |
741 | // "exit on delete last frame" flag here as when this dialog is | |
742 | // deleted, the app would terminate (it was the last top level window | |
743 | // as the main frame wasn't created yet!), so disable this behaviour | |
744 | // temproarily | |
745 | bool exitOnLastFrameDelete = wxTheApp->GetExitOnFrameDelete(); | |
746 | wxTheApp->SetExitOnFrameDelete(FALSE); | |
747 | ||
748 | // init the app | |
1e6feb95 | 749 | retValue = wxEntryInitGui() && wxTheApp->OnInit() ? 0 : -1; |
6afafc42 VZ |
750 | |
751 | // restore the old flag value | |
752 | wxTheApp->SetExitOnFrameDelete(exitOnLastFrameDelete); | |
753 | ||
754 | if ( retValue == 0 ) | |
e5c0b16a VZ |
755 | { |
756 | if ( enterLoop ) | |
757 | { | |
6afafc42 | 758 | // run the main loop |
e5c0b16a VZ |
759 | retValue = wxTheApp->OnRun(); |
760 | } | |
761 | else | |
6afafc42 VZ |
762 | { |
763 | // we want to initialize, but not run or exit immediately. | |
e5c0b16a | 764 | return 1; |
6afafc42 | 765 | } |
e5c0b16a VZ |
766 | } |
767 | //else: app initialization failed, so we skipped OnRun() | |
768 | ||
769 | wxWindow *topWindow = wxTheApp->GetTopWindow(); | |
770 | if ( topWindow ) | |
771 | { | |
772 | // Forcibly delete the window. | |
773 | if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) || | |
774 | topWindow->IsKindOf(CLASSINFO(wxDialog)) ) | |
775 | { | |
776 | topWindow->Close(TRUE); | |
777 | wxTheApp->DeletePendingObjects(); | |
778 | } | |
779 | else | |
780 | { | |
781 | delete topWindow; | |
782 | wxTheApp->SetTopWindow(NULL); | |
783 | } | |
784 | } | |
785 | ||
786 | wxTheApp->OnExit(); | |
787 | ||
7ece89c6 | 788 | wxEntryCleanup(); |
e5c0b16a VZ |
789 | |
790 | return retValue; | |
791 | ||
3b415ba4 | 792 | #if wxUSE_ON_FATAL_EXCEPTION |
e5c0b16a | 793 | } |
3b415ba4 VZ |
794 | __except ( gs_handleExceptions ? EXCEPTION_EXECUTE_HANDLER |
795 | : EXCEPTION_CONTINUE_SEARCH ) { | |
796 | if ( wxTheApp ) | |
f6bcfd97 BP |
797 | { |
798 | // give the user a chance to do something special about this | |
e5c0b16a | 799 | wxTheApp->OnFatalException(); |
f6bcfd97 | 800 | } |
e5c0b16a VZ |
801 | |
802 | ::ExitProcess(3); // the same exit code as abort() | |
803 | ||
804 | // NOTREACHED | |
805 | } | |
3b415ba4 | 806 | #endif // wxUSE_ON_FATAL_EXCEPTION |
2bda0e17 KB |
807 | } |
808 | ||
f8a3e080 | 809 | // restore warning state |
3f4a0c5b | 810 | #ifdef __VISUALC__ |
f8a3e080 VZ |
811 | #pragma warning(default: 4715) // not all control paths return a value |
812 | #endif // Visual C++ | |
813 | ||
2bda0e17 KB |
814 | #else /* _WINDLL */ |
815 | ||
7ece89c6 RD |
816 | //---------------------------------------------------------------------- |
817 | // Entry point for wxWindows + the App in a DLL | |
818 | //---------------------------------------------------------------------- | |
589f0e3e | 819 | |
2bda0e17 KB |
820 | int wxEntry(WXHINSTANCE hInstance) |
821 | { | |
e5c0b16a | 822 | wxhInstance = (HINSTANCE) hInstance; |
7ece89c6 | 823 | wxEntryStart(0, 0); |
2bda0e17 | 824 | |
e5c0b16a VZ |
825 | // The app may have declared a global application object, but we recommend |
826 | // the IMPLEMENT_APP macro is used instead, which sets an initializer function | |
827 | // for delayed, dynamic app object construction. | |
828 | if (!wxTheApp) | |
a5e0e655 | 829 | { |
e5c0b16a VZ |
830 | wxCHECK_MSG( wxApp::GetInitializerFunction(), 0, |
831 | "No initializer - use IMPLEMENT_APP macro." ); | |
2bda0e17 | 832 | |
e5c0b16a VZ |
833 | wxTheApp = (* wxApp::GetInitializerFunction()) (); |
834 | } | |
2bda0e17 | 835 | |
e5c0b16a | 836 | wxCHECK_MSG( wxTheApp, 0, "You have to define an instance of wxApp!" ); |
2bda0e17 | 837 | |
e5c0b16a VZ |
838 | wxTheApp->argc = 0; |
839 | wxTheApp->argv = NULL; | |
2bda0e17 | 840 | |
7ece89c6 | 841 | wxEntryInitGui(); |
2bda0e17 | 842 | |
e5c0b16a | 843 | wxTheApp->OnInit(); |
2bda0e17 | 844 | |
e5c0b16a VZ |
845 | wxWindow *topWindow = wxTheApp->GetTopWindow(); |
846 | if ( topWindow && topWindow->GetHWND()) | |
847 | { | |
848 | topWindow->Show(TRUE); | |
849 | } | |
2bda0e17 | 850 | |
e5c0b16a | 851 | return 1; |
2bda0e17 KB |
852 | } |
853 | #endif // _WINDLL | |
854 | ||
589f0e3e JS |
855 | //// Static member initialization |
856 | ||
c94ad3c3 | 857 | wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL; |
2bda0e17 | 858 | |
bb6290e3 | 859 | wxApp::wxApp() |
2bda0e17 | 860 | { |
e5c0b16a VZ |
861 | argc = 0; |
862 | argv = NULL; | |
e5c0b16a | 863 | m_printMode = wxPRINT_WINDOWS; |
e5c0b16a | 864 | m_auto3D = TRUE; |
2bda0e17 KB |
865 | } |
866 | ||
589f0e3e JS |
867 | wxApp::~wxApp() |
868 | { | |
e5c0b16a VZ |
869 | // Delete command-line args |
870 | int i; | |
871 | for (i = 0; i < argc; i++) | |
872 | { | |
873 | delete[] argv[i]; | |
874 | } | |
875 | delete[] argv; | |
589f0e3e JS |
876 | } |
877 | ||
bb6290e3 | 878 | bool wxApp::Initialized() |
2bda0e17 KB |
879 | { |
880 | #ifndef _WINDLL | |
e5c0b16a VZ |
881 | if (GetTopWindow()) |
882 | return TRUE; | |
883 | else | |
884 | return FALSE; | |
3b415ba4 | 885 | #else // Assume initialized if DLL (no way of telling) |
e5c0b16a | 886 | return TRUE; |
2bda0e17 KB |
887 | #endif |
888 | } | |
889 | ||
890 | /* | |
891 | * Get and process a message, returning FALSE if WM_QUIT | |
bee503b0 | 892 | * received (and also set the flag telling the app to exit the main loop) |
2bda0e17 KB |
893 | * |
894 | */ | |
bb6290e3 | 895 | bool wxApp::DoMessage() |
2bda0e17 | 896 | { |
bee503b0 VZ |
897 | BOOL rc = ::GetMessage(&s_currentMsg, (HWND) NULL, 0, 0); |
898 | if ( rc == 0 ) | |
899 | { | |
900 | // got WM_QUIT | |
901 | m_keepGoing = FALSE; | |
4a9968f9 | 902 | |
bee503b0 VZ |
903 | return FALSE; |
904 | } | |
905 | else if ( rc == -1 ) | |
906 | { | |
907 | // should never happen, but let's test for it nevertheless | |
f6bcfd97 | 908 | wxLogLastError(wxT("GetMessage")); |
bee503b0 VZ |
909 | } |
910 | else | |
911 | { | |
912 | #if wxUSE_THREADS | |
913 | wxASSERT_MSG( wxThread::IsMain(), | |
223d09f6 | 914 | wxT("only the main thread can process Windows messages") ); |
d50b2a58 | 915 | |
bee503b0 VZ |
916 | static bool s_hadGuiLock = TRUE; |
917 | static wxMsgArray s_aSavedMessages; | |
918 | ||
919 | // if a secondary thread owns is doing GUI calls, save all messages for | |
920 | // later processing - we can't process them right now because it will | |
921 | // lead to recursive library calls (and we're not reentrant) | |
922 | if ( !wxGuiOwnedByMainThread() ) | |
923 | { | |
924 | s_hadGuiLock = FALSE; | |
925 | ||
4a9968f9 VZ |
926 | // leave out WM_COMMAND messages: too dangerous, sometimes |
927 | // the message will be processed twice | |
928 | if ( !wxIsWaitingForThread() || | |
e5c0b16a | 929 | s_currentMsg.message != WM_COMMAND ) |
4a9968f9 VZ |
930 | { |
931 | s_aSavedMessages.Add(s_currentMsg); | |
932 | } | |
bee503b0 VZ |
933 | |
934 | return TRUE; | |
935 | } | |
936 | else | |
937 | { | |
938 | // have we just regained the GUI lock? if so, post all of the saved | |
939 | // messages | |
940 | // | |
941 | // FIXME of course, it's not _exactly_ the same as processing the | |
942 | // messages normally - expect some things to break... | |
943 | if ( !s_hadGuiLock ) | |
944 | { | |
945 | s_hadGuiLock = TRUE; | |
946 | ||
947 | size_t count = s_aSavedMessages.Count(); | |
948 | for ( size_t n = 0; n < count; n++ ) | |
949 | { | |
950 | MSG& msg = s_aSavedMessages[n]; | |
951 | ||
952 | if ( !ProcessMessage((WXMSG *)&msg) ) | |
953 | { | |
954 | ::TranslateMessage(&msg); | |
955 | ::DispatchMessage(&msg); | |
956 | } | |
957 | } | |
958 | ||
959 | s_aSavedMessages.Empty(); | |
960 | } | |
961 | } | |
962 | #endif // wxUSE_THREADS | |
963 | ||
964 | // Process the message | |
ed45e263 | 965 | DoMessage((WXMSG *)&s_currentMsg); |
bee503b0 VZ |
966 | } |
967 | ||
968 | return TRUE; | |
2bda0e17 KB |
969 | } |
970 | ||
ed45e263 VZ |
971 | void wxApp::DoMessage(WXMSG *pMsg) |
972 | { | |
973 | if ( !ProcessMessage(pMsg) ) | |
974 | { | |
975 | ::TranslateMessage((MSG *)pMsg); | |
976 | ::DispatchMessage((MSG *)pMsg); | |
977 | } | |
978 | } | |
979 | ||
2bda0e17 KB |
980 | /* |
981 | * Keep trying to process messages until WM_QUIT | |
982 | * received. | |
983 | * | |
984 | * If there are messages to be processed, they will all be | |
985 | * processed and OnIdle will not be called. | |
986 | * When there are no more messages, OnIdle is called. | |
987 | * If OnIdle requests more time, | |
988 | * it will be repeatedly called so long as there are no pending messages. | |
989 | * A 'feature' of this is that once OnIdle has decided that no more processing | |
990 | * is required, then it won't get processing time until further messages | |
991 | * are processed (it'll sit in DoMessage). | |
992 | */ | |
993 | ||
bb6290e3 | 994 | int wxApp::MainLoop() |
2bda0e17 | 995 | { |
e5c0b16a | 996 | m_keepGoing = TRUE; |
bee503b0 | 997 | |
e5c0b16a VZ |
998 | while ( m_keepGoing ) |
999 | { | |
1000 | #if wxUSE_THREADS | |
bee503b0 | 1001 | wxMutexGuiLeaveOrEnter(); |
e5c0b16a | 1002 | #endif // wxUSE_THREADS |
bee503b0 | 1003 | |
b6c588e1 VZ |
1004 | while ( !Pending() && ProcessIdle() ) |
1005 | ; | |
7214297d | 1006 | |
b6c588e1 | 1007 | // a message came or no more idle processing to do |
e5c0b16a VZ |
1008 | DoMessage(); |
1009 | } | |
2bda0e17 | 1010 | |
e5c0b16a | 1011 | return s_currentMsg.wParam; |
2bda0e17 KB |
1012 | } |
1013 | ||
1014 | // Returns TRUE if more time is needed. | |
bb6290e3 | 1015 | bool wxApp::ProcessIdle() |
2bda0e17 KB |
1016 | { |
1017 | wxIdleEvent event; | |
1018 | event.SetEventObject(this); | |
1019 | ProcessEvent(event); | |
1020 | ||
1021 | return event.MoreRequested(); | |
1022 | } | |
1023 | ||
bb6290e3 | 1024 | void wxApp::ExitMainLoop() |
2bda0e17 | 1025 | { |
b6c588e1 | 1026 | // VZ: why not ::PostQuitMessage()? |
e5c0b16a | 1027 | m_keepGoing = FALSE; |
2bda0e17 KB |
1028 | } |
1029 | ||
bb6290e3 | 1030 | bool wxApp::Pending() |
2bda0e17 | 1031 | { |
b6c588e1 | 1032 | return ::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) != 0; |
2bda0e17 KB |
1033 | } |
1034 | ||
bb6290e3 | 1035 | void wxApp::Dispatch() |
2bda0e17 | 1036 | { |
bee503b0 | 1037 | DoMessage(); |
2bda0e17 KB |
1038 | } |
1039 | ||
1040 | /* | |
1041 | * Give all windows a chance to preprocess | |
1042 | * the message. Some may have accelerator tables, or have | |
1043 | * MDI complications. | |
1044 | */ | |
2a47d3c1 | 1045 | |
d3f0a137 | 1046 | bool wxApp::ProcessMessage(WXMSG *wxmsg) |
2bda0e17 | 1047 | { |
d3f0a137 VZ |
1048 | MSG *msg = (MSG *)wxmsg; |
1049 | HWND hWnd = msg->hwnd; | |
8614c467 | 1050 | wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hWnd); |
2bda0e17 | 1051 | |
8614c467 VZ |
1052 | #if wxUSE_TOOLTIPS |
1053 | // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to | |
1054 | // popup the tooltip bubbles | |
1055 | if ( wndThis && (msg->message == WM_MOUSEMOVE) ) | |
834362a2 | 1056 | { |
8614c467 VZ |
1057 | wxToolTip *tt = wndThis->GetToolTip(); |
1058 | if ( tt ) | |
1059 | { | |
1060 | tt->RelayEvent(wxmsg); | |
1061 | } | |
834362a2 | 1062 | } |
8614c467 | 1063 | #endif // wxUSE_TOOLTIPS |
834362a2 | 1064 | |
e5c0b16a VZ |
1065 | // Try translations first; find the youngest window with |
1066 | // a translation table. | |
8614c467 | 1067 | wxWindow *wnd; |
42bcb12b | 1068 | |
ee544c17 | 1069 | bool pastTopLevelWindow = FALSE; |
d3f0a137 | 1070 | for ( wnd = wndThis; wnd; wnd = wnd->GetParent() ) |
2bda0e17 | 1071 | { |
ee544c17 JS |
1072 | if ( !pastTopLevelWindow && wnd->MSWTranslateMessage(wxmsg)) |
1073 | return TRUE; | |
1074 | if ( wnd->MSWProcessMessage(wxmsg) ) | |
d3f0a137 | 1075 | return TRUE; |
3bdc265d VZ |
1076 | |
1077 | // stop at first top level window, i.e. don't try to process the key | |
1078 | // strokes originating in a dialog using the accelerators of the parent | |
1079 | // frame - this doesn't make much sense | |
1080 | if ( wnd->IsTopLevel() ) | |
ee544c17 | 1081 | pastTopLevelWindow = TRUE; |
57a7b7c1 | 1082 | } |
d3f0a137 | 1083 | |
e5c0b16a | 1084 | return FALSE; |
2bda0e17 KB |
1085 | } |
1086 | ||
1087 | void wxApp::OnIdle(wxIdleEvent& event) | |
1088 | { | |
3222fde2 | 1089 | static bool s_inOnIdle = FALSE; |
2bda0e17 | 1090 | |
3222fde2 VZ |
1091 | // Avoid recursion (via ProcessEvent default case) |
1092 | if ( s_inOnIdle ) | |
1093 | return; | |
2bda0e17 | 1094 | |
3222fde2 | 1095 | s_inOnIdle = TRUE; |
2bda0e17 | 1096 | |
52e52bea GRG |
1097 | // If there are pending events, we must process them: pending events |
1098 | // are either events to the threads other than main or events posted | |
1099 | // with wxPostEvent() functions | |
1100 | // GRG: I have moved this here so that all pending events are processed | |
1101 | // before starting to delete any objects. This behaves better (in | |
1102 | // particular, wrt wxPostEvent) and is coherent with wxGTK's current | |
1103 | // behaviour. Changed Feb/2000 before 2.1.14 | |
1104 | ProcessPendingEvents(); | |
1105 | ||
3222fde2 VZ |
1106 | // 'Garbage' collection of windows deleted with Close(). |
1107 | DeletePendingObjects(); | |
2bda0e17 | 1108 | |
546db2a8 | 1109 | #if wxUSE_LOG |
3222fde2 | 1110 | // flush the logged messages if any |
2ed3265e | 1111 | wxLog::FlushActive(); |
546db2a8 | 1112 | #endif // wxUSE_LOG |
c54f78a2 | 1113 | |
3222fde2 VZ |
1114 | // Send OnIdle events to all windows |
1115 | if ( SendIdleEvents() ) | |
1116 | { | |
1117 | // SendIdleEvents() returns TRUE if at least one window requested more | |
1118 | // idle events | |
1119 | event.RequestMore(TRUE); | |
1120 | } | |
2bda0e17 | 1121 | |
3222fde2 | 1122 | s_inOnIdle = FALSE; |
2bda0e17 KB |
1123 | } |
1124 | ||
1125 | // Send idle event to all top-level windows | |
bb6290e3 | 1126 | bool wxApp::SendIdleEvents() |
2bda0e17 KB |
1127 | { |
1128 | bool needMore = FALSE; | |
e146b8c8 | 1129 | |
f1d534df | 1130 | wxWindowList::Node* node = wxTopLevelWindows.GetFirst(); |
3222fde2 VZ |
1131 | while (node) |
1132 | { | |
e146b8c8 | 1133 | wxWindow* win = node->GetData(); |
3222fde2 | 1134 | if (SendIdleEvents(win)) |
2bda0e17 | 1135 | needMore = TRUE; |
e146b8c8 | 1136 | node = node->GetNext(); |
3222fde2 VZ |
1137 | } |
1138 | ||
2bda0e17 KB |
1139 | return needMore; |
1140 | } | |
1141 | ||
1142 | // Send idle event to window and all subwindows | |
1143 | bool wxApp::SendIdleEvents(wxWindow* win) | |
1144 | { | |
e5c0b16a | 1145 | bool needMore = FALSE; |
2bda0e17 | 1146 | |
e5c0b16a VZ |
1147 | wxIdleEvent event; |
1148 | event.SetEventObject(win); | |
1149 | win->GetEventHandler()->ProcessEvent(event); | |
2bda0e17 | 1150 | |
e5c0b16a VZ |
1151 | if (event.MoreRequested()) |
1152 | needMore = TRUE; | |
2bda0e17 | 1153 | |
e5c0b16a VZ |
1154 | wxNode* node = win->GetChildren().First(); |
1155 | while (node) | |
1156 | { | |
1157 | wxWindow* win = (wxWindow*) node->Data(); | |
1158 | if (SendIdleEvents(win)) | |
1159 | needMore = TRUE; | |
2bda0e17 | 1160 | |
e5c0b16a VZ |
1161 | node = node->Next(); |
1162 | } | |
42e69d6b | 1163 | return needMore; |
2bda0e17 KB |
1164 | } |
1165 | ||
bb6290e3 | 1166 | void wxApp::DeletePendingObjects() |
2bda0e17 | 1167 | { |
e5c0b16a VZ |
1168 | wxNode *node = wxPendingDelete.First(); |
1169 | while (node) | |
1170 | { | |
1171 | wxObject *obj = (wxObject *)node->Data(); | |
d50b2a58 | 1172 | |
e5c0b16a | 1173 | delete obj; |
2bda0e17 | 1174 | |
e5c0b16a VZ |
1175 | if (wxPendingDelete.Member(obj)) |
1176 | delete node; | |
2bda0e17 | 1177 | |
e5c0b16a VZ |
1178 | // Deleting one object may have deleted other pending |
1179 | // objects, so start from beginning of list again. | |
1180 | node = wxPendingDelete.First(); | |
1181 | } | |
2bda0e17 KB |
1182 | } |
1183 | ||
57c208c5 | 1184 | void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event)) |
387a3b02 JS |
1185 | { |
1186 | if (GetTopWindow()) | |
1187 | GetTopWindow()->Close(TRUE); | |
1188 | } | |
1189 | ||
1190 | // Default behaviour: close the application with prompts. The | |
1191 | // user can veto the close, and therefore the end session. | |
1192 | void wxApp::OnQueryEndSession(wxCloseEvent& event) | |
1193 | { | |
1194 | if (GetTopWindow()) | |
1195 | { | |
1196 | if (!GetTopWindow()->Close(!event.CanVeto())) | |
1197 | event.Veto(TRUE); | |
1198 | } | |
1199 | } | |
1200 | ||
6d167489 VZ |
1201 | /* static */ |
1202 | int wxApp::GetComCtl32Version() | |
1203 | { | |
04ef50df JS |
1204 | #ifdef __WXMICROWIN__ |
1205 | return 0; | |
1206 | #else | |
6d167489 | 1207 | // cache the result |
bdc72a22 VZ |
1208 | static int s_verComCtl32 = -1; |
1209 | ||
1210 | wxCRIT_SECT_DECLARE(csComCtl32); | |
1211 | wxCRIT_SECT_LOCKER(lock, csComCtl32); | |
6d167489 VZ |
1212 | |
1213 | if ( s_verComCtl32 == -1 ) | |
1214 | { | |
bdc72a22 | 1215 | // initally assume no comctl32.dll at all |
6d167489 VZ |
1216 | s_verComCtl32 = 0; |
1217 | ||
bdc72a22 VZ |
1218 | // do we have it? |
1219 | HMODULE hModuleComCtl32 = ::GetModuleHandle(wxT("COMCTL32")); | |
6d167489 VZ |
1220 | |
1221 | // if so, then we can check for the version | |
bdc72a22 | 1222 | if ( hModuleComCtl32 ) |
bb6290e3 | 1223 | { |
bdc72a22 VZ |
1224 | // try to use DllGetVersion() if available in _headers_ |
1225 | #ifdef DLLVER_PLATFORM_WINDOWS // defined in shlwapi.h | |
1226 | DLLGETVERSIONPROC pfnDllGetVersion = (DLLGETVERSIONPROC) | |
f6bcfd97 | 1227 | ::GetProcAddress(hModuleComCtl32, "DllGetVersion"); |
bdc72a22 | 1228 | if ( pfnDllGetVersion ) |
6d167489 | 1229 | { |
bdc72a22 VZ |
1230 | DLLVERSIONINFO dvi; |
1231 | dvi.cbSize = sizeof(dvi); | |
1232 | ||
1233 | HRESULT hr = (*pfnDllGetVersion)(&dvi); | |
1234 | if ( FAILED(hr) ) | |
1235 | { | |
1236 | wxLogApiError(_T("DllGetVersion"), hr); | |
1237 | } | |
1238 | else | |
1239 | { | |
1240 | // this is incompatible with _WIN32_IE values, but | |
1241 | // compatible with the other values returned by | |
1242 | // GetComCtl32Version() | |
1243 | s_verComCtl32 = 100*dvi.dwMajorVersion + | |
1244 | dvi.dwMinorVersion; | |
1245 | } | |
6d167489 | 1246 | } |
bdc72a22 VZ |
1247 | #endif |
1248 | // DllGetVersion() unavailable either during compile or | |
1249 | // run-time, try to guess the version otherwise | |
1250 | if ( !s_verComCtl32 ) | |
1251 | { | |
1252 | // InitCommonControlsEx is unique to 4.70 and later | |
1253 | FARPROC theProc = ::GetProcAddress | |
1254 | ( | |
1255 | hModuleComCtl32, | |
a8ee71c7 | 1256 | "InitCommonControlsEx" |
bdc72a22 VZ |
1257 | ); |
1258 | ||
1259 | if ( !theProc ) | |
1260 | { | |
1261 | // not found, must be 4.00 | |
1262 | s_verComCtl32 = 400; | |
1263 | } | |
1264 | else | |
1265 | { | |
1266 | // many symbols appeared in comctl32 4.71, could use | |
1267 | // any of them except may be DllInstall | |
1268 | theProc = ::GetProcAddress | |
1269 | ( | |
1270 | hModuleComCtl32, | |
a8ee71c7 | 1271 | "InitializeFlatSB" |
bdc72a22 VZ |
1272 | ); |
1273 | if ( !theProc ) | |
1274 | { | |
1275 | // not found, must be 4.70 | |
1276 | s_verComCtl32 = 470; | |
1277 | } | |
1278 | else | |
1279 | { | |
1280 | // found, must be 4.71 | |
1281 | s_verComCtl32 = 471; | |
1282 | } | |
1283 | } | |
6d167489 | 1284 | } |
bb6290e3 JS |
1285 | } |
1286 | } | |
6d167489 VZ |
1287 | |
1288 | return s_verComCtl32; | |
04ef50df | 1289 | #endif |
bb6290e3 JS |
1290 | } |
1291 | ||
1292 | void wxExit() | |
2bda0e17 | 1293 | { |
e5c0b16a VZ |
1294 | wxLogError(_("Fatal error: exiting")); |
1295 | ||
1296 | wxApp::CleanUp(); | |
3f8e5072 | 1297 | exit(0); |
2bda0e17 KB |
1298 | } |
1299 | ||
1300 | // Yield to incoming messages | |
cb2713bf JS |
1301 | |
1302 | static bool gs_inYield = FALSE; | |
1303 | ||
bb6290e3 | 1304 | bool wxYield() |
2bda0e17 | 1305 | { |
2ed3265e VZ |
1306 | // disable log flushing from here because a call to wxYield() shouldn't |
1307 | // normally result in message boxes popping up &c | |
1308 | wxLog::Suspend(); | |
1309 | ||
33ac7e6f | 1310 | #ifdef __WXDEBUG__ |
cb2713bf JS |
1311 | if (gs_inYield) |
1312 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
1313 | #endif | |
33ac7e6f | 1314 | |
cb2713bf JS |
1315 | gs_inYield = TRUE; |
1316 | ||
8e193f38 VZ |
1317 | // we don't want to process WM_QUIT from here - it should be processed in |
1318 | // the main event loop in order to stop it | |
e5c0b16a | 1319 | MSG msg; |
8e193f38 VZ |
1320 | while ( PeekMessage(&msg, (HWND)0, 0, 0, PM_NOREMOVE) && |
1321 | msg.message != WM_QUIT ) | |
e5c0b16a | 1322 | { |
5b615ed8 VZ |
1323 | #if wxUSE_THREADS |
1324 | wxMutexGuiLeaveOrEnter(); | |
1325 | #endif // wxUSE_THREADS | |
1326 | ||
e5c0b16a VZ |
1327 | if ( !wxTheApp->DoMessage() ) |
1328 | break; | |
1329 | } | |
8e193f38 | 1330 | |
aadbdf11 | 1331 | // If they are pending events, we must process them. |
3f8e5072 JS |
1332 | if (wxTheApp) |
1333 | wxTheApp->ProcessPendingEvents(); | |
e5c0b16a | 1334 | |
2ed3265e VZ |
1335 | // let the logs be flashed again |
1336 | wxLog::Resume(); | |
1337 | ||
cb2713bf JS |
1338 | gs_inYield = FALSE; |
1339 | ||
e5c0b16a | 1340 | return TRUE; |
2bda0e17 | 1341 | } |
094637f6 | 1342 | |
cb2713bf JS |
1343 | // Yield to incoming messages; but fail silently if recursion is detected. |
1344 | bool wxYieldIfNeeded() | |
1345 | { | |
1346 | if (gs_inYield) | |
1347 | return FALSE; | |
33ac7e6f | 1348 | |
cb2713bf JS |
1349 | return wxYield(); |
1350 | } | |
1351 | ||
3b415ba4 VZ |
1352 | bool wxHandleFatalExceptions(bool doit) |
1353 | { | |
1354 | #if wxUSE_ON_FATAL_EXCEPTION | |
1355 | // assume this can only be called from the main thread | |
1356 | gs_handleExceptions = doit; | |
1357 | ||
1358 | return TRUE; | |
1359 | #else | |
1360 | wxFAIL_MSG(_T("set wxUSE_ON_FATAL_EXCEPTION to 1 to sue this function")); | |
33ac7e6f KB |
1361 | |
1362 | (void)doit; | |
3b415ba4 VZ |
1363 | return FALSE; |
1364 | #endif | |
1365 | } | |
1366 | ||
9779893b RD |
1367 | //----------------------------------------------------------------------------- |
1368 | // wxWakeUpIdle | |
1369 | //----------------------------------------------------------------------------- | |
1370 | ||
1371 | void wxWakeUpIdle() | |
1372 | { | |
1373 | // Send the top window a dummy message so idle handler processing will | |
1374 | // start up again. Doing it this way ensures that the idle handler | |
b568d04f VZ |
1375 | // wakes up in the right thread (see also wxWakeUpMainThread() which does |
1376 | // the same for the main app thread only) | |
9779893b | 1377 | wxWindow *topWindow = wxTheApp->GetTopWindow(); |
b568d04f VZ |
1378 | if ( topWindow ) |
1379 | { | |
1380 | if ( !::PostMessage(GetHwndOf(topWindow), WM_NULL, 0, 0) ) | |
1381 | { | |
1382 | // should never happen | |
f6bcfd97 | 1383 | wxLogLastError(wxT("PostMessage(WM_NULL)")); |
b568d04f | 1384 | } |
9779893b RD |
1385 | } |
1386 | } | |
1387 | ||
1388 | //----------------------------------------------------------------------------- | |
1389 | ||
ebea0891 KB |
1390 | wxIcon |
1391 | wxApp::GetStdIcon(int which) const | |
1392 | { | |
094637f6 VZ |
1393 | switch(which) |
1394 | { | |
1395 | case wxICON_INFORMATION: | |
1396 | return wxIcon("wxICON_INFO"); | |
1397 | ||
1398 | case wxICON_QUESTION: | |
1399 | return wxIcon("wxICON_QUESTION"); | |
1400 | ||
1401 | case wxICON_EXCLAMATION: | |
1402 | return wxIcon("wxICON_WARNING"); | |
1403 | ||
1404 | default: | |
223d09f6 | 1405 | wxFAIL_MSG(wxT("requested non existent standard icon")); |
094637f6 VZ |
1406 | // still fall through |
1407 | ||
1408 | case wxICON_HAND: | |
1409 | return wxIcon("wxICON_ERROR"); | |
1410 | } | |
ebea0891 KB |
1411 | } |
1412 | ||
2bda0e17 KB |
1413 | // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly |
1414 | // if in a separate file. So include it here to ensure it's linked. | |
31e78e0c | 1415 | #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__) && !defined(WXMAKINGDLL)) |
e5c0b16a | 1416 | #include "main.cpp" |
2bda0e17 | 1417 | #endif |