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