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