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