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