]>
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" |
4e938f5b | 22 | #pragma implementation "appbase.h" |
2bda0e17 KB |
23 | #endif |
24 | ||
25 | // For compilers that support precompilation, includes "wx.h". | |
2bda0e17 KB |
26 | #include "wx/wxprec.h" |
27 | ||
28 | #if defined(__BORLANDC__) | |
e5c0b16a | 29 | #pragma hdrstop |
2bda0e17 KB |
30 | #endif |
31 | ||
32 | #ifndef WX_PRECOMP | |
e5c0b16a VZ |
33 | #include "wx/frame.h" |
34 | #include "wx/app.h" | |
35 | #include "wx/utils.h" | |
36 | #include "wx/gdicmn.h" | |
37 | #include "wx/pen.h" | |
38 | #include "wx/brush.h" | |
39 | #include "wx/cursor.h" | |
40 | #include "wx/icon.h" | |
41 | #include "wx/palette.h" | |
42 | #include "wx/dc.h" | |
43 | #include "wx/dialog.h" | |
44 | #include "wx/msgdlg.h" | |
45 | #include "wx/intl.h" | |
46 | #include "wx/dynarray.h" | |
ebea0891 KB |
47 | # include "wx/wxchar.h" |
48 | # include "wx/icon.h" | |
2bda0e17 KB |
49 | #endif |
50 | ||
2bda0e17 KB |
51 | #include "wx/log.h" |
52 | #include "wx/module.h" | |
4bf78aae | 53 | |
4286a5b5 RR |
54 | #include "wx/msw/private.h" |
55 | ||
4bf78aae | 56 | #if wxUSE_THREADS |
bee503b0 VZ |
57 | #include "wx/thread.h" |
58 | ||
59 | // define the array of MSG strutures | |
60 | WX_DECLARE_OBJARRAY(MSG, wxMsgArray); | |
61 | ||
62 | #include "wx/arrimpl.cpp" | |
63 | ||
64 | WX_DEFINE_OBJARRAY(wxMsgArray); | |
65 | #endif // wxUSE_THREADS | |
2bda0e17 | 66 | |
47d67540 | 67 | #if wxUSE_WX_RESOURCES |
e5c0b16a | 68 | #include "wx/resource.h" |
2bda0e17 KB |
69 | #endif |
70 | ||
e5c0b16a | 71 | // OLE is used for drag-and-drop, clipboard, OLE Automation... |
7dee726c | 72 | #ifndef wxUSE_NORLANDER_HEADERS |
e5c0b16a VZ |
73 | #if defined(__GNUWIN32__) || defined(__SC__) || defined(__SALFORDC__) |
74 | #undef wxUSE_OLE | |
75 | ||
76 | #define wxUSE_OLE 0 | |
77 | #endif // broken compilers | |
7dee726c | 78 | #endif |
e5c0b16a VZ |
79 | |
80 | #if wxUSE_OLE | |
6e0d9d43 | 81 | #include <ole2.h> |
d05237ea | 82 | #endif |
ce3ed50d | 83 | |
2bda0e17 | 84 | #include <string.h> |
a5e0e655 | 85 | #include <ctype.h> |
2bda0e17 | 86 | |
65fd5cb0 | 87 | #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS) |
e5c0b16a | 88 | #include <commctrl.h> |
2bda0e17 KB |
89 | #endif |
90 | ||
6e0d9d43 | 91 | #include "wx/msw/msvcrt.h" |
370382c7 | 92 | |
e5c0b16a VZ |
93 | // --------------------------------------------------------------------------- |
94 | // global variables | |
95 | // --------------------------------------------------------------------------- | |
96 | ||
837e5743 OK |
97 | extern wxChar *wxBuffer; |
98 | extern wxChar *wxOsVersion; | |
2bda0e17 | 99 | extern wxList *wxWinHandleList; |
cde9f08e | 100 | extern wxList WXDLLEXPORT wxPendingDelete; |
7214297d | 101 | #if wxUSE_THREADS |
4d3a259a GL |
102 | extern wxList *wxPendingEvents; |
103 | extern wxCriticalSection *wxPendingEventsLocker; | |
7214297d | 104 | #endif |
2bda0e17 KB |
105 | extern void wxSetKeyboardHook(bool doIt); |
106 | extern wxCursor *g_globalCursor; | |
107 | ||
c4e7c2aa | 108 | HINSTANCE wxhInstance = 0; |
42e69d6b | 109 | MSG s_currentMsg; |
2bda0e17 KB |
110 | wxApp *wxTheApp = NULL; |
111 | ||
e5c0b16a | 112 | // FIXME why not const? and not static? |
193fe989 VZ |
113 | |
114 | // NB: all "NoRedraw" classes must have the same names as the "normal" classes | |
115 | // with NR suffix - wxWindow::MSWCreate() supposes this | |
223d09f6 KB |
116 | wxChar wxFrameClassName[] = wxT("wxFrameClass"); |
117 | wxChar wxFrameClassNameNoRedraw[] = wxT("wxFrameClassNR"); | |
118 | wxChar wxMDIFrameClassName[] = wxT("wxMDIFrameClass"); | |
119 | wxChar wxMDIFrameClassNameNoRedraw[] = wxT("wxMDIFrameClassNR"); | |
120 | wxChar wxMDIChildFrameClassName[] = wxT("wxMDIChildFrameClass"); | |
121 | wxChar wxMDIChildFrameClassNameNoRedraw[] = wxT("wxMDIChildFrameClassNR"); | |
122 | wxChar wxPanelClassName[] = wxT("wxPanelClass"); | |
123 | wxChar wxCanvasClassName[] = wxT("wxCanvasClass"); | |
2bda0e17 | 124 | |
57c208c5 JS |
125 | HICON wxSTD_FRAME_ICON = (HICON) NULL; |
126 | HICON wxSTD_MDICHILDFRAME_ICON = (HICON) NULL; | |
127 | HICON wxSTD_MDIPARENTFRAME_ICON = (HICON) NULL; | |
2bda0e17 | 128 | |
57c208c5 JS |
129 | HICON wxDEFAULT_FRAME_ICON = (HICON) NULL; |
130 | HICON wxDEFAULT_MDICHILDFRAME_ICON = (HICON) NULL; | |
131 | HICON wxDEFAULT_MDIPARENTFRAME_ICON = (HICON) NULL; | |
2bda0e17 | 132 | |
57c208c5 | 133 | HBRUSH wxDisableButtonBrush = (HBRUSH) 0; |
2bda0e17 KB |
134 | |
135 | LRESULT APIENTRY wxWndProc(HWND, UINT, WPARAM, LPARAM); | |
136 | ||
57c208c5 | 137 | #if defined(__WIN95__) && !defined(__TWIN32__) |
e5c0b16a | 138 | #define wxUSE_RICHEDIT 1 |
57c208c5 | 139 | #else |
e5c0b16a | 140 | #define wxUSE_RICHEDIT 0 |
57c208c5 JS |
141 | #endif |
142 | ||
143 | #if wxUSE_RICHEDIT | |
e5c0b16a | 144 | static HINSTANCE gs_hRichEdit = (HINSTANCE) NULL; |
2bda0e17 KB |
145 | #endif |
146 | ||
e5c0b16a VZ |
147 | // =========================================================================== |
148 | // implementation | |
149 | // =========================================================================== | |
589f0e3e | 150 | |
e5c0b16a VZ |
151 | // --------------------------------------------------------------------------- |
152 | // wxApp | |
153 | // --------------------------------------------------------------------------- | |
154 | ||
155 | #if !USE_SHARED_LIBRARY | |
156 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) | |
157 | ||
158 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) | |
159 | EVT_IDLE(wxApp::OnIdle) | |
160 | EVT_END_SESSION(wxApp::OnEndSession) | |
161 | EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession) | |
162 | END_EVENT_TABLE() | |
163 | #endif | |
164 | ||
e5c0b16a | 165 | //// Initialize |
589f0e3e | 166 | bool wxApp::Initialize() |
2bda0e17 | 167 | { |
c030b70f JS |
168 | // Some people may wish to use this, but |
169 | // probably it shouldn't be here by default. | |
170 | #ifdef __WXDEBUG__ | |
e5c0b16a | 171 | // wxRedirectIOToConsole(); |
c030b70f JS |
172 | #endif |
173 | ||
837e5743 | 174 | wxBuffer = new wxChar[1500]; // FIXME |
589f0e3e | 175 | |
aa0b7e1e | 176 | wxClassInfo::InitializeClasses(); |
589f0e3e | 177 | |
aa0b7e1e | 178 | #if wxUSE_RESOURCES |
223d09f6 | 179 | wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion); |
aa0b7e1e | 180 | #endif |
589f0e3e | 181 | |
42e69d6b VZ |
182 | // I'm annoyed ... I don't know where to put this and I don't want to |
183 | // create a module for that as it's part of the core. | |
4d3a259a GL |
184 | #if wxUSE_THREADS |
185 | wxPendingEvents = new wxList(); | |
186 | wxPendingEventsLocker = new wxCriticalSection(); | |
187 | #endif | |
188 | ||
aa0b7e1e JS |
189 | wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING); |
190 | wxTheColourDatabase->Initialize(); | |
589f0e3e | 191 | |
aa0b7e1e JS |
192 | wxInitializeStockLists(); |
193 | wxInitializeStockObjects(); | |
589f0e3e | 194 | |
aa0b7e1e | 195 | #if wxUSE_WX_RESOURCES |
a5e0e655 | 196 | wxInitializeResourceSystem(); |
aa0b7e1e | 197 | #endif |
2bda0e17 | 198 | |
aa0b7e1e | 199 | wxBitmap::InitStandardHandlers(); |
2bda0e17 | 200 | |
aa0b7e1e | 201 | #if defined(__WIN95__) |
a5e0e655 | 202 | InitCommonControls(); |
57c208c5 JS |
203 | |
204 | #if wxUSE_RICHEDIT | |
223d09f6 | 205 | gs_hRichEdit = LoadLibrary(wxT("RICHED32.DLL")); |
2bda0e17 | 206 | |
57c208c5 | 207 | if (gs_hRichEdit == (HINSTANCE) NULL) |
a5e0e655 | 208 | { |
e5c0b16a | 209 | wxLogError(_("Could not initialise Rich Edit DLL")); |
a5e0e655 | 210 | } |
e5c0b16a | 211 | #endif // wxUSE_RICHEDIT |
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 | |
223d09f6 KB |
237 | wxSTD_FRAME_ICON = LoadIcon(wxhInstance, wxT("wxSTD_FRAME")); |
238 | wxSTD_MDIPARENTFRAME_ICON = LoadIcon(wxhInstance, wxT("wxSTD_MDIPARENTFRAME")); | |
239 | wxSTD_MDICHILDFRAME_ICON = LoadIcon(wxhInstance, wxT("wxSTD_MDICHILDFRAME")); | |
2bda0e17 | 240 | |
223d09f6 KB |
241 | wxDEFAULT_FRAME_ICON = LoadIcon(wxhInstance, wxT("wxDEFAULT_FRAME")); |
242 | wxDEFAULT_MDIPARENTFRAME_ICON = LoadIcon(wxhInstance, wxT("wxDEFAULT_MDIPARENTFRAME")); | |
243 | wxDEFAULT_MDICHILDFRAME_ICON = LoadIcon(wxhInstance, wxT("wxDEFAULT_MDICHILDFRAME")); | |
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 KB |
524 | |
525 | #ifdef __WIN95__ | |
57c208c5 JS |
526 | |
527 | #if wxUSE_RICHEDIT | |
e5c0b16a VZ |
528 | if (gs_hRichEdit != (HINSTANCE) NULL) |
529 | FreeLibrary(gs_hRichEdit); | |
2bda0e17 KB |
530 | #endif |
531 | ||
57c208c5 JS |
532 | #endif |
533 | ||
47d67540 | 534 | #if wxUSE_PENWINDOWS |
e5c0b16a | 535 | wxCleanUpPenWin(); |
2bda0e17 KB |
536 | #endif |
537 | ||
e5c0b16a VZ |
538 | if (wxSTD_FRAME_ICON) |
539 | DestroyIcon(wxSTD_FRAME_ICON); | |
540 | if (wxSTD_MDICHILDFRAME_ICON) | |
541 | DestroyIcon(wxSTD_MDICHILDFRAME_ICON); | |
542 | if (wxSTD_MDIPARENTFRAME_ICON) | |
543 | DestroyIcon(wxSTD_MDIPARENTFRAME_ICON); | |
544 | ||
545 | if (wxDEFAULT_FRAME_ICON) | |
546 | DestroyIcon(wxDEFAULT_FRAME_ICON); | |
547 | if (wxDEFAULT_MDICHILDFRAME_ICON) | |
548 | DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON); | |
549 | if (wxDEFAULT_MDIPARENTFRAME_ICON) | |
550 | DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON); | |
551 | ||
552 | if ( wxDisableButtonBrush ) | |
42e69d6b | 553 | ::DeleteObject( wxDisableButtonBrush ); |
e5c0b16a VZ |
554 | |
555 | #if wxUSE_OLE | |
556 | ::OleUninitialize(); | |
5de5db0e | 557 | #endif |
2bda0e17 | 558 | |
1f112209 | 559 | #if wxUSE_CTL3D |
e5c0b16a | 560 | Ctl3dUnregister(wxhInstance); |
2bda0e17 KB |
561 | #endif |
562 | ||
e5c0b16a | 563 | if (wxWinHandleList) |
42e69d6b | 564 | delete wxWinHandleList; |
d50b2a58 | 565 | |
4d3a259a GL |
566 | // GL: I'm annoyed ... I don't know where to put this and I don't want to |
567 | // create a module for that as it's part of the core. | |
568 | #if wxUSE_THREADS | |
569 | delete wxPendingEvents; | |
570 | delete wxPendingEventsLocker; | |
63863e09 JS |
571 | // If we don't do the following, we get an apparent memory leak. |
572 | ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker(); | |
4d3a259a GL |
573 | #endif |
574 | ||
e5c0b16a | 575 | wxClassInfo::CleanUpClasses(); |
0c32066b | 576 | |
e5c0b16a VZ |
577 | delete wxTheApp; |
578 | wxTheApp = NULL; | |
184b5d99 JS |
579 | |
580 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT | |
e5c0b16a VZ |
581 | // At this point we want to check if there are any memory |
582 | // blocks that aren't part of the wxDebugContext itself, | |
583 | // as a special case. Then when dumping we need to ignore | |
584 | // wxDebugContext, too. | |
d13c32e9 | 585 | if (wxDebugContext::CountObjectsLeft(TRUE) > 0) |
e5c0b16a | 586 | { |
223d09f6 | 587 | wxLogDebug(wxT("There were memory leaks.")); |
e5c0b16a VZ |
588 | wxDebugContext::Dump(); |
589 | wxDebugContext::PrintStatistics(); | |
590 | } | |
591 | // wxDebugContext::SetStream(NULL, NULL); | |
184b5d99 JS |
592 | #endif |
593 | ||
546db2a8 | 594 | #if wxUSE_LOG |
e5c0b16a VZ |
595 | // do it as the very last thing because everything else can log messages |
596 | delete wxLog::SetActiveTarget(NULL); | |
546db2a8 | 597 | #endif // wxUSE_LOG |
2bda0e17 KB |
598 | } |
599 | ||
2bda0e17 KB |
600 | #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL)) |
601 | ||
e5c0b16a VZ |
602 | // temporarily disable this warning which would be generated in release builds |
603 | // because of __try | |
3f4a0c5b | 604 | #ifdef __VISUALC__ |
f8a3e080 VZ |
605 | #pragma warning(disable: 4715) // not all control paths return a value |
606 | #endif // Visual C++ | |
607 | ||
589f0e3e | 608 | //// Main wxWindows entry point |
a5e0e655 VZ |
609 | int wxEntry(WXHINSTANCE hInstance, |
610 | WXHINSTANCE WXUNUSED(hPrevInstance), | |
611 | char *lpCmdLine, | |
612 | int nCmdShow, | |
613 | bool enterLoop) | |
2bda0e17 | 614 | { |
6e0d9d43 VZ |
615 | // do check for memory leaks on program exit |
616 | // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free | |
617 | // deallocated memory which may be used to simulate low-memory condition) | |
618 | wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); | |
619 | ||
e5c0b16a VZ |
620 | // take everything into a try-except block in release build |
621 | // FIXME other compilers must support Win32 SEH (structured exception | |
622 | // handling) too, just find the appropriate keyword in their docs! | |
623 | // Please note that it's _not_ the same as C++ exceptions! | |
3f4a0c5b | 624 | #if !defined(__WXDEBUG__) && defined(__VISUALC__) |
f8a3e080 VZ |
625 | #define CATCH_PROGRAM_EXCEPTIONS |
626 | ||
3f4a0c5b | 627 | __try { |
f8a3e080 VZ |
628 | #else |
629 | #undef CATCH_PROGRAM_EXCEPTIONS | |
a5e0e655 VZ |
630 | #endif |
631 | ||
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 | |
e5c0b16a VZ |
659 | int retValue = 0; |
660 | ||
661 | if ( wxTheApp->OnInit() ) | |
662 | { | |
663 | if ( enterLoop ) | |
664 | { | |
665 | retValue = wxTheApp->OnRun(); | |
666 | } | |
667 | else | |
668 | // We want to initialize, but not run or exit immediately. | |
669 | return 1; | |
670 | } | |
671 | //else: app initialization failed, so we skipped OnRun() | |
672 | ||
673 | wxWindow *topWindow = wxTheApp->GetTopWindow(); | |
674 | if ( topWindow ) | |
675 | { | |
676 | // Forcibly delete the window. | |
677 | if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) || | |
678 | topWindow->IsKindOf(CLASSINFO(wxDialog)) ) | |
679 | { | |
680 | topWindow->Close(TRUE); | |
681 | wxTheApp->DeletePendingObjects(); | |
682 | } | |
683 | else | |
684 | { | |
685 | delete topWindow; | |
686 | wxTheApp->SetTopWindow(NULL); | |
687 | } | |
688 | } | |
689 | ||
690 | wxTheApp->OnExit(); | |
691 | ||
692 | wxApp::CleanUp(); | |
693 | ||
694 | return retValue; | |
695 | ||
696 | #ifdef CATCH_PROGRAM_EXCEPTIONS | |
697 | } | |
698 | __except ( EXCEPTION_EXECUTE_HANDLER ) { | |
699 | /* | |
700 | if ( wxTheApp ) | |
701 | wxTheApp->OnFatalException(); | |
702 | */ | |
703 | ||
704 | // using wxLog would be unsafe here | |
705 | ::MessageBox(NULL, | |
706 | _("Unrecoverable program error detected: " | |
707 | " the application will terminate."), | |
708 | _("Fatal Error"), | |
709 | MB_APPLMODAL | MB_ICONSTOP | MB_OK); | |
710 | ||
711 | ::ExitProcess(3); // the same exit code as abort() | |
712 | ||
713 | // NOTREACHED | |
714 | } | |
f8a3e080 | 715 | #endif // CATCH_PROGRAM_EXCEPTIONS |
2bda0e17 KB |
716 | } |
717 | ||
f8a3e080 | 718 | // restore warning state |
3f4a0c5b | 719 | #ifdef __VISUALC__ |
f8a3e080 VZ |
720 | #pragma warning(default: 4715) // not all control paths return a value |
721 | #endif // Visual C++ | |
722 | ||
2bda0e17 KB |
723 | #else /* _WINDLL */ |
724 | ||
589f0e3e JS |
725 | //// Entry point for DLLs |
726 | ||
2bda0e17 KB |
727 | int wxEntry(WXHINSTANCE hInstance) |
728 | { | |
e5c0b16a VZ |
729 | wxhInstance = (HINSTANCE) hInstance; |
730 | wxApp::Initialize(); | |
2bda0e17 | 731 | |
e5c0b16a VZ |
732 | // The app may have declared a global application object, but we recommend |
733 | // the IMPLEMENT_APP macro is used instead, which sets an initializer function | |
734 | // for delayed, dynamic app object construction. | |
735 | if (!wxTheApp) | |
a5e0e655 | 736 | { |
e5c0b16a VZ |
737 | wxCHECK_MSG( wxApp::GetInitializerFunction(), 0, |
738 | "No initializer - use IMPLEMENT_APP macro." ); | |
2bda0e17 | 739 | |
e5c0b16a VZ |
740 | wxTheApp = (* wxApp::GetInitializerFunction()) (); |
741 | } | |
2bda0e17 | 742 | |
e5c0b16a | 743 | wxCHECK_MSG( wxTheApp, 0, "You have to define an instance of wxApp!" ); |
2bda0e17 | 744 | |
e5c0b16a VZ |
745 | wxTheApp->argc = 0; |
746 | wxTheApp->argv = NULL; | |
2bda0e17 | 747 | |
e5c0b16a | 748 | wxTheApp->OnInitGui(); |
2bda0e17 | 749 | |
e5c0b16a | 750 | wxTheApp->OnInit(); |
2bda0e17 | 751 | |
e5c0b16a VZ |
752 | wxWindow *topWindow = wxTheApp->GetTopWindow(); |
753 | if ( topWindow && topWindow->GetHWND()) | |
754 | { | |
755 | topWindow->Show(TRUE); | |
756 | } | |
2bda0e17 | 757 | |
e5c0b16a | 758 | return 1; |
2bda0e17 KB |
759 | } |
760 | #endif // _WINDLL | |
761 | ||
589f0e3e JS |
762 | //// Static member initialization |
763 | ||
c94ad3c3 | 764 | wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL; |
2bda0e17 | 765 | |
bb6290e3 | 766 | wxApp::wxApp() |
2bda0e17 | 767 | { |
e5c0b16a VZ |
768 | m_topWindow = NULL; |
769 | wxTheApp = this; | |
42e69d6b VZ |
770 | m_wantDebugOutput = TRUE; |
771 | ||
e5c0b16a VZ |
772 | argc = 0; |
773 | argv = NULL; | |
e5c0b16a | 774 | m_printMode = wxPRINT_WINDOWS; |
e5c0b16a VZ |
775 | m_exitOnFrameDelete = TRUE; |
776 | m_auto3D = TRUE; | |
2bda0e17 KB |
777 | } |
778 | ||
589f0e3e JS |
779 | wxApp::~wxApp() |
780 | { | |
e5c0b16a VZ |
781 | // Delete command-line args |
782 | int i; | |
783 | for (i = 0; i < argc; i++) | |
784 | { | |
785 | delete[] argv[i]; | |
786 | } | |
787 | delete[] argv; | |
589f0e3e JS |
788 | } |
789 | ||
bb6290e3 | 790 | bool wxApp::Initialized() |
2bda0e17 KB |
791 | { |
792 | #ifndef _WINDLL | |
e5c0b16a VZ |
793 | if (GetTopWindow()) |
794 | return TRUE; | |
795 | else | |
796 | return FALSE; | |
2bda0e17 KB |
797 | #endif |
798 | #ifdef _WINDLL // Assume initialized if DLL (no way of telling) | |
e5c0b16a | 799 | return TRUE; |
2bda0e17 KB |
800 | #endif |
801 | } | |
802 | ||
803 | /* | |
804 | * Get and process a message, returning FALSE if WM_QUIT | |
bee503b0 | 805 | * received (and also set the flag telling the app to exit the main loop) |
2bda0e17 KB |
806 | * |
807 | */ | |
bb6290e3 | 808 | bool wxApp::DoMessage() |
2bda0e17 | 809 | { |
bee503b0 VZ |
810 | BOOL rc = ::GetMessage(&s_currentMsg, (HWND) NULL, 0, 0); |
811 | if ( rc == 0 ) | |
812 | { | |
813 | // got WM_QUIT | |
814 | m_keepGoing = FALSE; | |
4a9968f9 | 815 | |
bee503b0 VZ |
816 | return FALSE; |
817 | } | |
818 | else if ( rc == -1 ) | |
819 | { | |
820 | // should never happen, but let's test for it nevertheless | |
821 | wxLogLastError("GetMessage"); | |
822 | } | |
823 | else | |
824 | { | |
825 | #if wxUSE_THREADS | |
826 | wxASSERT_MSG( wxThread::IsMain(), | |
223d09f6 | 827 | wxT("only the main thread can process Windows messages") ); |
d50b2a58 | 828 | |
bee503b0 VZ |
829 | static bool s_hadGuiLock = TRUE; |
830 | static wxMsgArray s_aSavedMessages; | |
831 | ||
832 | // if a secondary thread owns is doing GUI calls, save all messages for | |
833 | // later processing - we can't process them right now because it will | |
834 | // lead to recursive library calls (and we're not reentrant) | |
835 | if ( !wxGuiOwnedByMainThread() ) | |
836 | { | |
837 | s_hadGuiLock = FALSE; | |
838 | ||
4a9968f9 VZ |
839 | // leave out WM_COMMAND messages: too dangerous, sometimes |
840 | // the message will be processed twice | |
841 | if ( !wxIsWaitingForThread() || | |
e5c0b16a | 842 | s_currentMsg.message != WM_COMMAND ) |
4a9968f9 VZ |
843 | { |
844 | s_aSavedMessages.Add(s_currentMsg); | |
845 | } | |
bee503b0 VZ |
846 | |
847 | return TRUE; | |
848 | } | |
849 | else | |
850 | { | |
851 | // have we just regained the GUI lock? if so, post all of the saved | |
852 | // messages | |
853 | // | |
854 | // FIXME of course, it's not _exactly_ the same as processing the | |
855 | // messages normally - expect some things to break... | |
856 | if ( !s_hadGuiLock ) | |
857 | { | |
858 | s_hadGuiLock = TRUE; | |
859 | ||
860 | size_t count = s_aSavedMessages.Count(); | |
861 | for ( size_t n = 0; n < count; n++ ) | |
862 | { | |
863 | MSG& msg = s_aSavedMessages[n]; | |
864 | ||
865 | if ( !ProcessMessage((WXMSG *)&msg) ) | |
866 | { | |
867 | ::TranslateMessage(&msg); | |
868 | ::DispatchMessage(&msg); | |
869 | } | |
870 | } | |
871 | ||
872 | s_aSavedMessages.Empty(); | |
873 | } | |
874 | } | |
875 | #endif // wxUSE_THREADS | |
876 | ||
877 | // Process the message | |
878 | if ( !ProcessMessage((WXMSG *)&s_currentMsg) ) | |
879 | { | |
880 | ::TranslateMessage(&s_currentMsg); | |
bee503b0 VZ |
881 | ::DispatchMessage(&s_currentMsg); |
882 | } | |
883 | } | |
884 | ||
885 | return TRUE; | |
2bda0e17 KB |
886 | } |
887 | ||
888 | /* | |
889 | * Keep trying to process messages until WM_QUIT | |
890 | * received. | |
891 | * | |
892 | * If there are messages to be processed, they will all be | |
893 | * processed and OnIdle will not be called. | |
894 | * When there are no more messages, OnIdle is called. | |
895 | * If OnIdle requests more time, | |
896 | * it will be repeatedly called so long as there are no pending messages. | |
897 | * A 'feature' of this is that once OnIdle has decided that no more processing | |
898 | * is required, then it won't get processing time until further messages | |
899 | * are processed (it'll sit in DoMessage). | |
900 | */ | |
901 | ||
bb6290e3 | 902 | int wxApp::MainLoop() |
2bda0e17 | 903 | { |
e5c0b16a | 904 | m_keepGoing = TRUE; |
bee503b0 | 905 | |
e5c0b16a VZ |
906 | while ( m_keepGoing ) |
907 | { | |
908 | #if wxUSE_THREADS | |
bee503b0 | 909 | wxMutexGuiLeaveOrEnter(); |
e5c0b16a | 910 | #endif // wxUSE_THREADS |
bee503b0 | 911 | |
e5c0b16a VZ |
912 | while ( !::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) && |
913 | ProcessIdle() ) | |
914 | { | |
915 | } | |
bee503b0 | 916 | |
7214297d | 917 | |
e5c0b16a VZ |
918 | DoMessage(); |
919 | } | |
2bda0e17 | 920 | |
e5c0b16a | 921 | return s_currentMsg.wParam; |
2bda0e17 KB |
922 | } |
923 | ||
924 | // Returns TRUE if more time is needed. | |
bb6290e3 | 925 | bool wxApp::ProcessIdle() |
2bda0e17 KB |
926 | { |
927 | wxIdleEvent event; | |
928 | event.SetEventObject(this); | |
929 | ProcessEvent(event); | |
930 | ||
931 | return event.MoreRequested(); | |
932 | } | |
933 | ||
7214297d GL |
934 | #if wxUSE_THREADS |
935 | void wxApp::ProcessPendingEvents() | |
1d3dbc7b | 936 | { |
4d3a259a GL |
937 | wxNode *node = wxPendingEvents->First(); |
938 | wxCriticalSectionLocker locker(*wxPendingEventsLocker); | |
7214297d GL |
939 | |
940 | while (node) | |
941 | { | |
942 | wxEvtHandler *handler = (wxEvtHandler *)node->Data(); | |
943 | ||
944 | handler->ProcessPendingEvents(); | |
945 | ||
946 | delete node; | |
4d3a259a | 947 | node = wxPendingEvents->First(); |
7214297d GL |
948 | } |
949 | } | |
950 | #endif | |
951 | ||
952 | ||
bb6290e3 | 953 | void wxApp::ExitMainLoop() |
2bda0e17 | 954 | { |
e5c0b16a | 955 | m_keepGoing = FALSE; |
2bda0e17 KB |
956 | } |
957 | ||
bb6290e3 | 958 | bool wxApp::Pending() |
2bda0e17 | 959 | { |
42e69d6b | 960 | return (::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) != 0); |
2bda0e17 KB |
961 | } |
962 | ||
bb6290e3 | 963 | void wxApp::Dispatch() |
2bda0e17 | 964 | { |
bee503b0 | 965 | DoMessage(); |
2bda0e17 KB |
966 | } |
967 | ||
968 | /* | |
969 | * Give all windows a chance to preprocess | |
970 | * the message. Some may have accelerator tables, or have | |
971 | * MDI complications. | |
972 | */ | |
2a47d3c1 | 973 | |
d3f0a137 | 974 | bool wxApp::ProcessMessage(WXMSG *wxmsg) |
2bda0e17 | 975 | { |
d3f0a137 VZ |
976 | MSG *msg = (MSG *)wxmsg; |
977 | HWND hWnd = msg->hwnd; | |
978 | wxWindow *wndThis = wxFindWinFromHandle((WXHWND)hWnd), *wnd; | |
2bda0e17 | 979 | |
834362a2 VZ |
980 | // for some composite controls (like a combobox), wndThis might be NULL |
981 | // because the subcontrol is not a wxWindow, but only the control itself | |
982 | // is - try to catch this case | |
983 | while ( hWnd && !wndThis ) | |
984 | { | |
985 | hWnd = ::GetParent(hWnd); | |
986 | wndThis = wxFindWinFromHandle((WXHWND)hWnd); | |
987 | } | |
988 | ||
e5c0b16a VZ |
989 | // Try translations first; find the youngest window with |
990 | // a translation table. | |
d3f0a137 | 991 | for ( wnd = wndThis; wnd; wnd = wnd->GetParent() ) |
2bda0e17 | 992 | { |
d3f0a137 VZ |
993 | if ( wnd->MSWTranslateMessage(wxmsg) ) |
994 | return TRUE; | |
2bda0e17 | 995 | } |
2bda0e17 | 996 | |
e5c0b16a | 997 | // Anyone for a non-translation message? Try youngest descendants first. |
d3f0a137 | 998 | for ( wnd = wndThis; wnd; wnd = wnd->GetParent() ) |
57a7b7c1 | 999 | { |
d3f0a137 VZ |
1000 | if ( wnd->MSWProcessMessage(wxmsg) ) |
1001 | return TRUE; | |
57a7b7c1 | 1002 | } |
d3f0a137 | 1003 | |
e5c0b16a | 1004 | return FALSE; |
2bda0e17 KB |
1005 | } |
1006 | ||
1007 | void wxApp::OnIdle(wxIdleEvent& event) | |
1008 | { | |
3222fde2 | 1009 | static bool s_inOnIdle = FALSE; |
2bda0e17 | 1010 | |
3222fde2 VZ |
1011 | // Avoid recursion (via ProcessEvent default case) |
1012 | if ( s_inOnIdle ) | |
1013 | return; | |
2bda0e17 | 1014 | |
3222fde2 | 1015 | s_inOnIdle = TRUE; |
2bda0e17 | 1016 | |
3222fde2 VZ |
1017 | // 'Garbage' collection of windows deleted with Close(). |
1018 | DeletePendingObjects(); | |
2bda0e17 | 1019 | |
546db2a8 | 1020 | #if wxUSE_LOG |
3222fde2 VZ |
1021 | // flush the logged messages if any |
1022 | wxLog *pLog = wxLog::GetActiveTarget(); | |
1023 | if ( pLog != NULL && pLog->HasPendingMessages() ) | |
1024 | pLog->Flush(); | |
546db2a8 | 1025 | #endif // wxUSE_LOG |
c54f78a2 | 1026 | |
3222fde2 VZ |
1027 | // Send OnIdle events to all windows |
1028 | if ( SendIdleEvents() ) | |
1029 | { | |
1030 | // SendIdleEvents() returns TRUE if at least one window requested more | |
1031 | // idle events | |
1032 | event.RequestMore(TRUE); | |
1033 | } | |
2bda0e17 | 1034 | |
aadbdf11 GL |
1035 | // If they are pending events, we must process them. |
1036 | #if wxUSE_THREADS | |
1037 | ProcessPendingEvents(); | |
1038 | #endif | |
3222fde2 | 1039 | s_inOnIdle = FALSE; |
2bda0e17 KB |
1040 | } |
1041 | ||
1042 | // Send idle event to all top-level windows | |
bb6290e3 | 1043 | bool wxApp::SendIdleEvents() |
2bda0e17 KB |
1044 | { |
1045 | bool needMore = FALSE; | |
e146b8c8 | 1046 | |
f1d534df | 1047 | wxWindowList::Node* node = wxTopLevelWindows.GetFirst(); |
3222fde2 VZ |
1048 | while (node) |
1049 | { | |
e146b8c8 | 1050 | wxWindow* win = node->GetData(); |
3222fde2 | 1051 | if (SendIdleEvents(win)) |
2bda0e17 | 1052 | needMore = TRUE; |
e146b8c8 | 1053 | node = node->GetNext(); |
3222fde2 VZ |
1054 | } |
1055 | ||
2bda0e17 KB |
1056 | return needMore; |
1057 | } | |
1058 | ||
1059 | // Send idle event to window and all subwindows | |
1060 | bool wxApp::SendIdleEvents(wxWindow* win) | |
1061 | { | |
e5c0b16a | 1062 | bool needMore = FALSE; |
2bda0e17 | 1063 | |
e5c0b16a VZ |
1064 | wxIdleEvent event; |
1065 | event.SetEventObject(win); | |
1066 | win->GetEventHandler()->ProcessEvent(event); | |
2bda0e17 | 1067 | |
e5c0b16a VZ |
1068 | if (event.MoreRequested()) |
1069 | needMore = TRUE; | |
2bda0e17 | 1070 | |
e5c0b16a VZ |
1071 | wxNode* node = win->GetChildren().First(); |
1072 | while (node) | |
1073 | { | |
1074 | wxWindow* win = (wxWindow*) node->Data(); | |
1075 | if (SendIdleEvents(win)) | |
1076 | needMore = TRUE; | |
2bda0e17 | 1077 | |
e5c0b16a VZ |
1078 | node = node->Next(); |
1079 | } | |
42e69d6b | 1080 | return needMore; |
2bda0e17 KB |
1081 | } |
1082 | ||
bb6290e3 | 1083 | void wxApp::DeletePendingObjects() |
2bda0e17 | 1084 | { |
e5c0b16a VZ |
1085 | wxNode *node = wxPendingDelete.First(); |
1086 | while (node) | |
1087 | { | |
1088 | wxObject *obj = (wxObject *)node->Data(); | |
d50b2a58 | 1089 | |
e5c0b16a | 1090 | delete obj; |
2bda0e17 | 1091 | |
e5c0b16a VZ |
1092 | if (wxPendingDelete.Member(obj)) |
1093 | delete node; | |
2bda0e17 | 1094 | |
e5c0b16a VZ |
1095 | // Deleting one object may have deleted other pending |
1096 | // objects, so start from beginning of list again. | |
1097 | node = wxPendingDelete.First(); | |
1098 | } | |
2bda0e17 KB |
1099 | } |
1100 | ||
57c208c5 | 1101 | void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event)) |
387a3b02 JS |
1102 | { |
1103 | if (GetTopWindow()) | |
1104 | GetTopWindow()->Close(TRUE); | |
1105 | } | |
1106 | ||
1107 | // Default behaviour: close the application with prompts. The | |
1108 | // user can veto the close, and therefore the end session. | |
1109 | void wxApp::OnQueryEndSession(wxCloseEvent& event) | |
1110 | { | |
1111 | if (GetTopWindow()) | |
1112 | { | |
1113 | if (!GetTopWindow()->Close(!event.CanVeto())) | |
1114 | event.Veto(TRUE); | |
1115 | } | |
1116 | } | |
1117 | ||
bb6290e3 JS |
1118 | int wxApp::GetComCtl32Version() const |
1119 | { | |
1120 | // have we loaded COMCTL32 yet? | |
223d09f6 | 1121 | HMODULE theModule = ::GetModuleHandle(wxT("COMCTL32")); |
bb6290e3 | 1122 | int version = 0; |
d50b2a58 | 1123 | |
bb6290e3 JS |
1124 | // if so, then we can check for the version |
1125 | if (theModule) | |
1126 | { | |
1127 | // InitCommonControlsEx is unique to 4.7 and later | |
1128 | FARPROC theProc = ::GetProcAddress(theModule, "InitCommonControlsEx"); | |
d50b2a58 | 1129 | |
bb6290e3 JS |
1130 | if (! theProc) |
1131 | { // not found, must be 4.00 | |
e5c0b16a | 1132 | version = 400; |
bb6290e3 JS |
1133 | } |
1134 | else | |
1135 | { | |
e5c0b16a VZ |
1136 | // The following symbol are unique to 4.71 |
1137 | // DllInstall | |
1138 | // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos | |
1139 | // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo | |
1140 | // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange | |
1141 | // FlatSB_ShowScrollBar | |
1142 | // _DrawIndirectImageList _DuplicateImageList | |
1143 | // InitializeFlatSB | |
1144 | // UninitializeFlatSB | |
1145 | // we could check for any of these - I chose DllInstall | |
1146 | FARPROC theProc = ::GetProcAddress(theModule, "DllInstall"); | |
1147 | if (! theProc) | |
1148 | { | |
1149 | // not found, must be 4.70 | |
1150 | version = 470; | |
1151 | } | |
1152 | else | |
1153 | { // found, must be 4.71 | |
1154 | version = 471; | |
1155 | } | |
bb6290e3 JS |
1156 | } |
1157 | } | |
1158 | return version; | |
1159 | } | |
1160 | ||
1161 | void wxExit() | |
2bda0e17 | 1162 | { |
e5c0b16a VZ |
1163 | wxLogError(_("Fatal error: exiting")); |
1164 | ||
1165 | wxApp::CleanUp(); | |
2bda0e17 KB |
1166 | } |
1167 | ||
1168 | // Yield to incoming messages | |
bb6290e3 | 1169 | bool wxYield() |
2bda0e17 | 1170 | { |
e5c0b16a VZ |
1171 | MSG msg; |
1172 | // We want to go back to the main message loop | |
1173 | // if we see a WM_QUIT. (?) | |
c455ab93 RR |
1174 | #ifdef __WXWINE__ |
1175 | while (PeekMessage(&msg, (HWND)NULL, 0, 0, PM_NOREMOVE) && msg.message != WM_QUIT) | |
1176 | #else | |
e5c0b16a | 1177 | while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) && msg.message != WM_QUIT) |
c455ab93 | 1178 | #endif |
e5c0b16a VZ |
1179 | { |
1180 | if ( !wxTheApp->DoMessage() ) | |
1181 | break; | |
1182 | } | |
aadbdf11 GL |
1183 | // If they are pending events, we must process them. |
1184 | #if wxUSE_THREADS | |
6a2f3103 | 1185 | wxTheApp->ProcessPendingEvents(); |
aadbdf11 | 1186 | #endif |
e5c0b16a VZ |
1187 | |
1188 | return TRUE; | |
2bda0e17 | 1189 | } |
094637f6 | 1190 | |
ebea0891 KB |
1191 | wxIcon |
1192 | wxApp::GetStdIcon(int which) const | |
1193 | { | |
094637f6 VZ |
1194 | switch(which) |
1195 | { | |
1196 | case wxICON_INFORMATION: | |
1197 | return wxIcon("wxICON_INFO"); | |
1198 | ||
1199 | case wxICON_QUESTION: | |
1200 | return wxIcon("wxICON_QUESTION"); | |
1201 | ||
1202 | case wxICON_EXCLAMATION: | |
1203 | return wxIcon("wxICON_WARNING"); | |
1204 | ||
1205 | default: | |
223d09f6 | 1206 | wxFAIL_MSG(wxT("requested non existent standard icon")); |
094637f6 VZ |
1207 | // still fall through |
1208 | ||
1209 | case wxICON_HAND: | |
1210 | return wxIcon("wxICON_ERROR"); | |
1211 | } | |
ebea0891 KB |
1212 | } |
1213 | ||
2bda0e17 KB |
1214 | |
1215 | HINSTANCE wxGetInstance() | |
1216 | { | |
e5c0b16a | 1217 | return wxhInstance; |
2bda0e17 KB |
1218 | } |
1219 | ||
b97b1af8 RD |
1220 | void wxSetInstance(HINSTANCE hInst) |
1221 | { | |
1222 | wxhInstance = hInst; | |
1223 | } | |
1224 | ||
2bda0e17 KB |
1225 | // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly |
1226 | // if in a separate file. So include it here to ensure it's linked. | |
3f4a0c5b | 1227 | #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__)) |
e5c0b16a | 1228 | #include "main.cpp" |
2bda0e17 | 1229 | #endif |