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