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