]>
Commit | Line | Data |
---|---|---|
0e320a79 DW |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: app.cpp | |
3 | // Purpose: wxApp | |
d88de032 | 4 | // Author: David Webster |
0e320a79 | 5 | // Modified by: |
d88de032 | 6 | // Created: 10/13/99 |
0e320a79 | 7 | // RCS-ID: $Id$ |
d88de032 DW |
8 | // Copyright: (c) David Webster |
9 | // Licence: wxWindows licence | |
0e320a79 DW |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
d88de032 DW |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifndef WX_PRECOMP | |
16 | #include "wx/frame.h" | |
17 | #include "wx/app.h" | |
18 | #include "wx/utils.h" | |
19 | #include "wx/gdicmn.h" | |
20 | #include "wx/pen.h" | |
21 | #include "wx/brush.h" | |
22 | #include "wx/cursor.h" | |
23 | #include "wx/icon.h" | |
24 | #include "wx/palette.h" | |
25 | #include "wx/dc.h" | |
26 | #include "wx/dialog.h" | |
27 | #include "wx/msgdlg.h" | |
28 | #include "wx/intl.h" | |
29 | #include "wx/dynarray.h" | |
30 | # include "wx/wxchar.h" | |
31 | # include "wx/icon.h" | |
0e320a79 DW |
32 | #endif |
33 | ||
0e320a79 DW |
34 | #include "wx/log.h" |
35 | #include "wx/module.h" | |
d88de032 DW |
36 | |
37 | #include "wx/os2/private.h" | |
38 | ||
39 | #if wxUSE_THREADS | |
40 | #include "wx/thread.h" | |
41 | ||
9ed0fac8 DW |
42 | // define the array of QMSG strutures |
43 | WX_DECLARE_OBJARRAY(QMSG, wxMsgArray); | |
d88de032 DW |
44 | |
45 | #include "wx/arrimpl.cpp" | |
46 | ||
9ed0fac8 | 47 | WX_DEFINE_OBJARRAY(wxMsgArray); |
d88de032 | 48 | #endif // wxUSE_THREADS |
0e320a79 DW |
49 | |
50 | #if wxUSE_WX_RESOURCES | |
d88de032 | 51 | #include "wx/resource.h" |
0e320a79 DW |
52 | #endif |
53 | ||
8df85a61 DW |
54 | #if wxUSE_TOOLTIPS |
55 | #include "wx/tooltip.h" | |
56 | #endif // wxUSE_TOOLTIPS | |
57 | ||
0e320a79 | 58 | #include <string.h> |
d88de032 DW |
59 | #include <ctype.h> |
60 | ||
61 | // --------------------------------------------------------------------------- | |
62 | // global variables | |
63 | // --------------------------------------------------------------------------- | |
64 | ||
9ed0fac8 DW |
65 | extern wxChar* wxBuffer; |
66 | extern wxChar* wxOsVersion; | |
67 | extern wxList* wxWinHandleList; | |
68 | extern wxList WXDLLEXPORT wxPendingDelete; | |
9ed0fac8 | 69 | extern wxCursor* g_globalCursor; |
0e320a79 | 70 | |
8df85a61 | 71 | HAB vHabmain = NULLHANDLE; |
9ed0fac8 DW |
72 | QMSG svCurrentMsg; |
73 | wxApp* wxTheApp = NULL; | |
d88de032 DW |
74 | |
75 | // NB: all "NoRedraw" classes must have the same names as the "normal" classes | |
13a4ea8d | 76 | // with NR suffix - wxWindow::OS2Create() supposes this |
9ed0fac8 DW |
77 | wxChar wxFrameClassName[] = wxT("wxFrameClass"); |
78 | wxChar wxFrameClassNameNoRedraw[] = wxT("wxFrameClassNR"); | |
79 | wxChar wxMDIFrameClassName[] = wxT("wxMDIFrameClass"); | |
80 | wxChar wxMDIFrameClassNameNoRedraw[] = wxT("wxMDIFrameClassNR"); | |
81 | wxChar wxMDIChildFrameClassName[] = wxT("wxMDIChildFrameClass"); | |
d88de032 | 82 | wxChar wxMDIChildFrameClassNameNoRedraw[] = wxT("wxMDIChildFrameClassNR"); |
9ed0fac8 DW |
83 | wxChar wxPanelClassName[] = wxT("wxPanelClass"); |
84 | wxChar wxCanvasClassName[] = wxT("wxCanvasClass"); | |
d88de032 | 85 | |
9ed0fac8 DW |
86 | HICON wxSTD_FRAME_ICON = (HICON) NULL; |
87 | HICON wxSTD_MDICHILDFRAME_ICON = (HICON) NULL; | |
d88de032 DW |
88 | HICON wxSTD_MDIPARENTFRAME_ICON = (HICON) NULL; |
89 | ||
9ed0fac8 DW |
90 | HICON wxDEFAULT_FRAME_ICON = (HICON) NULL; |
91 | HICON wxDEFAULT_MDICHILDFRAME_ICON = (HICON) NULL; | |
92 | HICON wxDEFAULT_MDIPARENTFRAME_ICON = (HICON) NULL; | |
d88de032 DW |
93 | |
94 | HBRUSH wxDisableButtonBrush = (HBRUSH) 0; | |
95 | ||
f23208ca DW |
96 | MRESULT EXPENTRY wxWndProc( HWND |
97 | ,ULONG | |
98 | ,MPARAM | |
99 | ,MPARAM | |
100 | ); | |
d88de032 DW |
101 | |
102 | // =========================================================================== | |
103 | // implementation | |
104 | // =========================================================================== | |
105 | ||
106 | // --------------------------------------------------------------------------- | |
107 | // wxApp | |
108 | // --------------------------------------------------------------------------- | |
109 | ||
d88de032 | 110 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) |
0e320a79 | 111 | |
d88de032 DW |
112 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) |
113 | EVT_IDLE(wxApp::OnIdle) | |
114 | EVT_END_SESSION(wxApp::OnEndSession) | |
115 | EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession) | |
116 | END_EVENT_TABLE() | |
0e320a79 | 117 | |
8df85a61 DW |
118 | // |
119 | // Initialize | |
120 | // | |
9ed0fac8 DW |
121 | bool wxApp::Initialize( |
122 | HAB vHab | |
123 | ) | |
0e320a79 | 124 | { |
914589c2 DW |
125 | // |
126 | // OS2 has to have an anchorblock | |
127 | // | |
128 | vHab = WinInitialize(0); | |
129 | ||
130 | if (!vHab) | |
131 | return FALSE; | |
132 | else | |
133 | vHabmain = vHab; | |
134 | ||
d88de032 DW |
135 | // Some people may wish to use this, but |
136 | // probably it shouldn't be here by default. | |
137 | #ifdef __WXDEBUG__ | |
138 | // wxRedirectIOToConsole(); | |
0e320a79 DW |
139 | #endif |
140 | ||
13a4ea8d | 141 | wxBuffer = new wxChar[1500]; // FIXME; why? |
0e320a79 | 142 | |
d88de032 | 143 | wxClassInfo::InitializeClasses(); |
0e320a79 | 144 | |
d88de032 DW |
145 | #if wxUSE_RESOURCES |
146 | wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion); | |
147 | #endif | |
148 | ||
8df85a61 DW |
149 | #if wxUSE_THREADS |
150 | wxPendingEventsLocker = new wxCriticalSection; | |
151 | #endif | |
152 | ||
d88de032 DW |
153 | wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING); |
154 | wxTheColourDatabase->Initialize(); | |
155 | ||
156 | wxInitializeStockLists(); | |
157 | wxInitializeStockObjects(); | |
0e320a79 DW |
158 | |
159 | #if wxUSE_WX_RESOURCES | |
d88de032 | 160 | wxInitializeResourceSystem(); |
0e320a79 DW |
161 | #endif |
162 | ||
d88de032 DW |
163 | wxBitmap::InitStandardHandlers(); |
164 | ||
9ed0fac8 | 165 | RegisterWindowClasses(vHab); |
d88de032 DW |
166 | wxWinHandleList = new wxList(wxKEY_INTEGER); |
167 | ||
168 | // This is to foil optimizations in Visual C++ that throw out dummy.obj. | |
169 | // PLEASE DO NOT ALTER THIS. | |
1b3d5e55 | 170 | #if !defined(WXMAKINGDLL) && defined(__VISAGECPP__) |
d88de032 DW |
171 | extern char wxDummyChar; |
172 | if (wxDummyChar) wxDummyChar++; | |
173 | #endif | |
174 | ||
61243a51 | 175 | // wxSetKeyboardHook(TRUE); |
d88de032 DW |
176 | |
177 | wxModule::RegisterModules(); | |
178 | if (!wxModule::InitializeModules()) | |
179 | return FALSE; | |
180 | return TRUE; | |
8df85a61 | 181 | } // end of wxApp::Initialize |
d88de032 DW |
182 | |
183 | // --------------------------------------------------------------------------- | |
184 | // RegisterWindowClasses | |
185 | // --------------------------------------------------------------------------- | |
186 | ||
9ed0fac8 DW |
187 | bool wxApp::RegisterWindowClasses( |
188 | HAB vHab | |
189 | ) | |
d88de032 | 190 | { |
3b9e3455 | 191 | APIRET rc; |
914589c2 DW |
192 | ERRORID vError = 0L; |
193 | wxString sError; | |
3b9e3455 | 194 | |
f23208ca DW |
195 | if (!::WinRegisterClass( vHab |
196 | ,wxFrameClassName | |
197 | ,(PFNWP)wxWndProc | |
198 | ,CS_SIZEREDRAW | CS_SYNCPAINT | |
a0606634 | 199 | ,sizeof(ULONG) |
f23208ca | 200 | )) |
d88de032 | 201 | { |
914589c2 DW |
202 | vError = ::WinGetLastError(vHab); |
203 | sError = wxPMErrorToStr(vError); | |
204 | wxLogLastError(sError); | |
d88de032 DW |
205 | return FALSE; |
206 | } | |
207 | ||
f23208ca DW |
208 | if (!::WinRegisterClass( vHab |
209 | ,wxFrameClassNameNoRedraw | |
210 | ,(PFNWP)wxWndProc | |
211 | ,0 | |
212 | ,0 | |
213 | )) | |
d88de032 | 214 | { |
914589c2 DW |
215 | vError = ::WinGetLastError(vHab); |
216 | sError = wxPMErrorToStr(vError); | |
217 | wxLogLastError(sError); | |
d88de032 DW |
218 | return FALSE; |
219 | } | |
220 | ||
f23208ca DW |
221 | if (!::WinRegisterClass( vHab |
222 | ,wxMDIFrameClassName | |
223 | ,(PFNWP)wxWndProc | |
224 | ,CS_SIZEREDRAW | CS_SYNCPAINT | |
225 | ,0 | |
226 | )) | |
d88de032 | 227 | { |
914589c2 DW |
228 | vError = ::WinGetLastError(vHab); |
229 | sError = wxPMErrorToStr(vError); | |
230 | wxLogLastError(sError); | |
d88de032 DW |
231 | return FALSE; |
232 | } | |
0e320a79 | 233 | |
f23208ca DW |
234 | if (!::WinRegisterClass( vHab |
235 | ,wxMDIFrameClassNameNoRedraw | |
236 | ,(PFNWP)wxWndProc | |
237 | ,0 | |
238 | ,0 | |
239 | )) | |
d88de032 | 240 | { |
914589c2 DW |
241 | vError = ::WinGetLastError(vHab); |
242 | sError = wxPMErrorToStr(vError); | |
243 | wxLogLastError(sError); | |
d88de032 DW |
244 | return FALSE; |
245 | } | |
246 | ||
f23208ca DW |
247 | if (!::WinRegisterClass( vHab |
248 | ,wxMDIChildFrameClassName | |
249 | ,(PFNWP)wxWndProc | |
250 | ,CS_MOVENOTIFY | CS_SIZEREDRAW | CS_SYNCPAINT | CS_HITTEST | |
251 | ,0 | |
252 | )) | |
d88de032 | 253 | { |
914589c2 DW |
254 | vError = ::WinGetLastError(vHab); |
255 | sError = wxPMErrorToStr(vError); | |
256 | wxLogLastError(sError); | |
d88de032 DW |
257 | return FALSE; |
258 | } | |
259 | ||
f23208ca DW |
260 | if (!::WinRegisterClass( vHab |
261 | ,wxMDIChildFrameClassNameNoRedraw | |
262 | ,(PFNWP)wxWndProc | |
263 | ,CS_HITTEST | |
264 | ,0 | |
265 | )) | |
d88de032 | 266 | { |
914589c2 DW |
267 | vError = ::WinGetLastError(vHab); |
268 | sError = wxPMErrorToStr(vError); | |
269 | wxLogLastError(sError); | |
d88de032 DW |
270 | return FALSE; |
271 | } | |
272 | ||
f23208ca DW |
273 | if (!::WinRegisterClass( vHab |
274 | ,wxPanelClassName | |
275 | ,(PFNWP)wxWndProc | |
276 | ,CS_MOVENOTIFY | CS_SIZEREDRAW | CS_HITTEST | CS_SAVEBITS | CS_SYNCPAINT | |
277 | ,0 | |
278 | )) | |
d88de032 | 279 | { |
914589c2 DW |
280 | vError = ::WinGetLastError(vHab); |
281 | sError = wxPMErrorToStr(vError); | |
282 | wxLogLastError(sError); | |
d88de032 DW |
283 | return FALSE; |
284 | } | |
285 | ||
f23208ca DW |
286 | if (!::WinRegisterClass( vHab |
287 | ,wxCanvasClassName | |
288 | ,(PFNWP)wxWndProc | |
289 | ,CS_MOVENOTIFY | CS_SIZEREDRAW | CS_HITTEST | CS_SAVEBITS | CS_SYNCPAINT | |
290 | ,0 | |
291 | )) | |
d88de032 | 292 | { |
914589c2 DW |
293 | vError = ::WinGetLastError(vHab); |
294 | sError = wxPMErrorToStr(vError); | |
295 | wxLogLastError(sError); | |
d88de032 DW |
296 | return FALSE; |
297 | } | |
d88de032 | 298 | return TRUE; |
8df85a61 | 299 | } // end of wxApp::RegisterWindowClasses |
d88de032 | 300 | |
8df85a61 DW |
301 | // |
302 | // Cleans up any wxWindows internal structures left lying around | |
303 | // | |
0e320a79 DW |
304 | void wxApp::CleanUp() |
305 | { | |
8df85a61 DW |
306 | // |
307 | // COMMON CLEANUP | |
308 | // | |
d88de032 DW |
309 | |
310 | #if wxUSE_LOG | |
8df85a61 DW |
311 | |
312 | // | |
313 | // Flush the logged messages if any and install a 'safer' log target: the | |
d88de032 DW |
314 | // default one (wxLogGui) can't be used after the resources are freed just |
315 | // below and the user suppliedo ne might be even more unsafe (using any | |
316 | // wxWindows GUI function is unsafe starting from now) | |
8df85a61 | 317 | // |
d88de032 DW |
318 | wxLog::DontCreateOnDemand(); |
319 | ||
8df85a61 DW |
320 | // |
321 | // This will flush the old messages if any | |
322 | // | |
d88de032 DW |
323 | delete wxLog::SetActiveTarget(new wxLogStderr); |
324 | #endif // wxUSE_LOG | |
325 | ||
8df85a61 | 326 | // |
d88de032 | 327 | // One last chance for pending objects to be cleaned up |
8df85a61 | 328 | // |
d88de032 DW |
329 | wxTheApp->DeletePendingObjects(); |
330 | ||
331 | wxModule::CleanUpModules(); | |
0e320a79 DW |
332 | |
333 | #if wxUSE_WX_RESOURCES | |
d88de032 | 334 | wxCleanUpResourceSystem(); |
d88de032 | 335 | #endif |
0e320a79 | 336 | |
d88de032 | 337 | wxDeleteStockObjects(); |
0e320a79 | 338 | |
8df85a61 | 339 | // |
d88de032 | 340 | // Destroy all GDI lists, etc. |
8df85a61 | 341 | // |
d88de032 | 342 | wxDeleteStockLists(); |
0e320a79 | 343 | |
d88de032 DW |
344 | delete wxTheColourDatabase; |
345 | wxTheColourDatabase = NULL; | |
0e320a79 | 346 | |
d88de032 | 347 | wxBitmap::CleanUpHandlers(); |
0e320a79 | 348 | |
d88de032 DW |
349 | delete[] wxBuffer; |
350 | wxBuffer = NULL; | |
0e320a79 | 351 | |
8df85a61 DW |
352 | // |
353 | // PM-SPECIFIC CLEANUP | |
354 | // | |
0e320a79 | 355 | |
61243a51 | 356 | // wxSetKeyboardHook(FALSE); |
9ed0fac8 | 357 | |
d88de032 | 358 | if (wxSTD_FRAME_ICON) |
9ed0fac8 | 359 | ::WinFreeFileIcon(wxSTD_FRAME_ICON); |
d88de032 | 360 | if (wxSTD_MDICHILDFRAME_ICON) |
9ed0fac8 | 361 | ::WinFreeFileIcon(wxSTD_MDICHILDFRAME_ICON); |
d88de032 | 362 | if (wxSTD_MDIPARENTFRAME_ICON) |
9ed0fac8 | 363 | ::WinFreeFileIcon(wxSTD_MDIPARENTFRAME_ICON); |
d88de032 DW |
364 | |
365 | if (wxDEFAULT_FRAME_ICON) | |
9ed0fac8 | 366 | ::WinFreeFileIcon(wxDEFAULT_FRAME_ICON); |
d88de032 | 367 | if (wxDEFAULT_MDICHILDFRAME_ICON) |
9ed0fac8 | 368 | ::WinFreeFileIcon(wxDEFAULT_MDICHILDFRAME_ICON); |
d88de032 | 369 | if (wxDEFAULT_MDIPARENTFRAME_ICON) |
9ed0fac8 DW |
370 | ::WinFreeFileIcon(wxDEFAULT_MDIPARENTFRAME_ICON); |
371 | ||
d88de032 DW |
372 | if ( wxDisableButtonBrush ) |
373 | { | |
374 | // TODO: ::DeleteObject( wxDisableButtonBrush ); | |
375 | } | |
376 | ||
377 | if (wxWinHandleList) | |
378 | delete wxWinHandleList; | |
379 | ||
d88de032 | 380 | delete wxPendingEvents; |
8df85a61 | 381 | #if wxUSE_THREADS |
d88de032 DW |
382 | delete wxPendingEventsLocker; |
383 | // If we don't do the following, we get an apparent memory leak. | |
384 | ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker(); | |
385 | #endif | |
0e320a79 | 386 | |
d88de032 | 387 | wxClassInfo::CleanUpClasses(); |
0e320a79 | 388 | |
d88de032 DW |
389 | delete wxTheApp; |
390 | wxTheApp = NULL; | |
77cd51c3 | 391 | |
0e320a79 | 392 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
d88de032 DW |
393 | // At this point we want to check if there are any memory |
394 | // blocks that aren't part of the wxDebugContext itself, | |
395 | // as a special case. Then when dumping we need to ignore | |
396 | // wxDebugContext, too. | |
397 | if (wxDebugContext::CountObjectsLeft(TRUE) > 0) | |
398 | { | |
399 | wxLogDebug(wxT("There were memory leaks.")); | |
400 | wxDebugContext::Dump(); | |
401 | wxDebugContext::PrintStatistics(); | |
402 | } | |
403 | // wxDebugContext::SetStream(NULL, NULL); | |
0e320a79 | 404 | #endif |
77cd51c3 | 405 | |
d88de032 DW |
406 | #if wxUSE_LOG |
407 | // do it as the very last thing because everything else can log messages | |
408 | delete wxLog::SetActiveTarget(NULL); | |
409 | #endif // wxUSE_LOG | |
8df85a61 | 410 | } // end of wxApp::CleanUp |
0e320a79 | 411 | |
9ed0fac8 DW |
412 | int wxEntry( |
413 | int argc | |
414 | , char* argv[] | |
415 | ) | |
d88de032 | 416 | { |
9dea36ef | 417 | HAB vHab = 0; |
d88de032 | 418 | |
9ed0fac8 DW |
419 | if (!wxApp::Initialize(vHab)) |
420 | return 0; | |
d88de032 | 421 | |
9ed0fac8 DW |
422 | // |
423 | // create the application object or ensure that one already exists | |
424 | // | |
425 | if (!wxTheApp) | |
426 | { | |
426d5745 DW |
427 | // The app may have declared a global application object, but we recommend |
428 | // the IMPLEMENT_APP macro is used instead, which sets an initializer | |
429 | // function for delayed, dynamic app object construction. | |
430 | wxCHECK_MSG( wxApp::GetInitializerFunction(), 0, | |
431 | wxT("No initializer - use IMPLEMENT_APP macro.") ); | |
432 | wxTheApp = (*wxApp::GetInitializerFunction()) (); | |
9ed0fac8 | 433 | } |
426d5745 | 434 | wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") ); |
9ed0fac8 | 435 | wxTheApp->argc = argc; |
d88de032 | 436 | |
9ed0fac8 DW |
437 | #if wxUSE_UNICODE |
438 | wxTheApp->argv = new wxChar*[argc+1]; | |
439 | ||
440 | int nArgc = 0; | |
441 | ||
442 | while (nArgc < argc) | |
443 | { | |
444 | wxTheApp->argv[nArgc] = wxStrdup(wxConvLibc.cMB2WX(argv[nArgc])); | |
445 | nArgc++; | |
446 | } | |
447 | wxTheApp->argv[nArgc] = (wxChar *)NULL; | |
448 | #else | |
449 | wxTheApp->argv = argv; | |
450 | #endif | |
d88de032 | 451 | |
9ed0fac8 | 452 | wxString sName(wxFileNameFromPath(argv[0])); |
d88de032 | 453 | |
9ed0fac8 DW |
454 | wxStripExtension(sName); |
455 | wxTheApp->SetAppName(sName); | |
d88de032 | 456 | |
9ed0fac8 | 457 | int nRetValue = 0; |
d88de032 | 458 | |
9ed0fac8 DW |
459 | if (!wxTheApp->OnInitGui()) |
460 | nRetValue = -1; | |
d88de032 | 461 | |
9ed0fac8 DW |
462 | if (nRetValue == 0) |
463 | { | |
464 | if (wxTheApp->OnInit()) | |
d88de032 | 465 | { |
7bed7a50 DW |
466 | nRetValue = wxTheApp->OnRun(); |
467 | // nRetValue = -1; | |
d88de032 | 468 | } |
9ed0fac8 DW |
469 | } |
470 | ||
7bed7a50 | 471 | wxWindow* pTopWindow = wxTheApp->GetTopWindow(); |
d88de032 | 472 | |
7bed7a50 DW |
473 | if (pTopWindow) |
474 | { | |
475 | // Forcibly delete the window. | |
476 | if (pTopWindow->IsKindOf(CLASSINFO(wxFrame)) || | |
477 | pTopWindow->IsKindOf(CLASSINFO(wxDialog)) ) | |
478 | { | |
479 | pTopWindow->Close(TRUE); | |
480 | wxTheApp->DeletePendingObjects(); | |
481 | } | |
482 | else | |
d88de032 | 483 | { |
7bed7a50 DW |
484 | delete pTopWindow; |
485 | wxTheApp->SetTopWindow(NULL); | |
d88de032 | 486 | } |
9ed0fac8 DW |
487 | } |
488 | wxTheApp->OnExit(); | |
489 | wxApp::CleanUp(); | |
490 | return(nRetValue); | |
8df85a61 | 491 | } // end of wxEntry |
77cd51c3 | 492 | |
9ed0fac8 | 493 | bool wxApp::OnInitGui() |
d88de032 | 494 | { |
914589c2 DW |
495 | ERRORID vError; |
496 | wxString sError; | |
77cd51c3 | 497 | |
f23208ca | 498 | m_hMq = ::WinCreateMsgQueue(vHabmain, 0); |
914589c2 DW |
499 | if (!m_hMq) |
500 | { | |
501 | vError = ::WinGetLastError(vHabmain); | |
502 | sError = wxPMErrorToStr(vError); | |
503 | wxLogDebug(sError); | |
504 | return FALSE; | |
505 | } | |
9ed0fac8 | 506 | return TRUE; |
8df85a61 | 507 | } // end of wxApp::OnInitGui |
0e320a79 | 508 | |
9ed0fac8 DW |
509 | // |
510 | // Static member initialization | |
511 | // | |
d88de032 | 512 | wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL; |
0e320a79 DW |
513 | |
514 | wxApp::wxApp() | |
515 | { | |
d88de032 DW |
516 | m_topWindow = NULL; |
517 | wxTheApp = this; | |
518 | m_wantDebugOutput = TRUE; | |
519 | ||
520 | argc = 0; | |
521 | argv = NULL; | |
9ed0fac8 | 522 | m_nPrintMode = wxPRINT_WINDOWS; |
d88de032 | 523 | m_exitOnFrameDelete = TRUE; |
9ed0fac8 | 524 | m_bAuto3D = TRUE; |
8df85a61 | 525 | } // end of wxApp::wxApp |
d88de032 DW |
526 | |
527 | wxApp::~wxApp() | |
528 | { | |
8df85a61 | 529 | // |
d88de032 | 530 | // Delete command-line args |
8df85a61 DW |
531 | // |
532 | int i; | |
533 | ||
d88de032 DW |
534 | for (i = 0; i < argc; i++) |
535 | { | |
536 | delete[] argv[i]; | |
537 | } | |
538 | delete[] argv; | |
8df85a61 | 539 | } // end of wxApp::~wxApp |
0e320a79 DW |
540 | |
541 | bool wxApp::Initialized() | |
542 | { | |
d88de032 DW |
543 | if (GetTopWindow()) |
544 | return TRUE; | |
545 | else | |
546 | return FALSE; | |
8df85a61 | 547 | } // end of wxApp::Initialized |
0e320a79 | 548 | |
9ed0fac8 DW |
549 | // |
550 | // Get and process a message, returning FALSE if WM_QUIT | |
551 | // received (and also set the flag telling the app to exit the main loop) | |
552 | // | |
d88de032 DW |
553 | bool wxApp::DoMessage() |
554 | { | |
43543d98 | 555 | BOOL bRc = ::WinGetMsg(vHabmain, &m_vMsg, HWND(NULL), 0, 0); |
9ed0fac8 DW |
556 | |
557 | if (bRc == 0) | |
d88de032 DW |
558 | { |
559 | // got WM_QUIT | |
9ed0fac8 | 560 | m_bKeepGoing = FALSE; |
d88de032 DW |
561 | return FALSE; |
562 | } | |
9ed0fac8 | 563 | else if (bRc == -1) |
d88de032 DW |
564 | { |
565 | // should never happen, but let's test for it nevertheless | |
566 | wxLogLastError("GetMessage"); | |
567 | } | |
568 | else | |
569 | { | |
570 | #if wxUSE_THREADS | |
9ed0fac8 DW |
571 | wxASSERT_MSG( wxThread::IsMain() |
572 | ,wxT("only the main thread can process Windows messages") | |
573 | ); | |
d88de032 | 574 | |
9ed0fac8 DW |
575 | static bool sbHadGuiLock = TRUE; |
576 | static wxMsgArray svSavedMessages; | |
d88de032 | 577 | |
9ed0fac8 | 578 | // |
8df85a61 | 579 | // If a secondary thread owns is doing GUI calls, save all messages for |
d88de032 DW |
580 | // later processing - we can't process them right now because it will |
581 | // lead to recursive library calls (and we're not reentrant) | |
9ed0fac8 DW |
582 | // |
583 | if (!wxGuiOwnedByMainThread()) | |
d88de032 | 584 | { |
9ed0fac8 | 585 | sbHadGuiLock = FALSE; |
d88de032 | 586 | |
8df85a61 DW |
587 | // |
588 | // Leave out WM_COMMAND messages: too dangerous, sometimes | |
d88de032 | 589 | // the message will be processed twice |
8df85a61 | 590 | // |
d88de032 | 591 | if ( !wxIsWaitingForThread() || |
9ed0fac8 | 592 | svCurrentMsg.msg != WM_COMMAND ) |
d88de032 | 593 | { |
9ed0fac8 | 594 | svSavedMessages.Add(svCurrentMsg); |
d88de032 | 595 | } |
d88de032 DW |
596 | return TRUE; |
597 | } | |
598 | else | |
599 | { | |
9ed0fac8 | 600 | // |
8df85a61 | 601 | // Have we just regained the GUI lock? if so, post all of the saved |
d88de032 DW |
602 | // messages |
603 | // | |
9ed0fac8 | 604 | if (!sbHadGuiLock ) |
d88de032 | 605 | { |
9ed0fac8 | 606 | sbHadGuiLock = TRUE; |
d88de032 | 607 | |
9ed0fac8 DW |
608 | size_t nCount = svSavedMessages.Count(); |
609 | ||
610 | for (size_t n = 0; n < nCount; n++) | |
d88de032 | 611 | { |
9ed0fac8 | 612 | QMSG vMsg = svSavedMessages[n]; |
d88de032 | 613 | |
9ed0fac8 | 614 | if ( !ProcessMessage((WXMSG *)&vMsg) ) |
d88de032 | 615 | { |
43543d98 | 616 | ::WinDispatchMsg(vHabmain, &vMsg); |
d88de032 DW |
617 | } |
618 | } | |
9ed0fac8 | 619 | svSavedMessages.Empty(); |
d88de032 DW |
620 | } |
621 | } | |
d88de032 DW |
622 | #endif // wxUSE_THREADS |
623 | ||
624 | // Process the message | |
9ed0fac8 | 625 | if (!ProcessMessage((WXMSG *)&svCurrentMsg) ) |
d88de032 | 626 | { |
43543d98 | 627 | ::WinDispatchMsg(vHabmain, (PQMSG)&svCurrentMsg); |
d88de032 | 628 | } |
d88de032 | 629 | } |
d88de032 | 630 | return TRUE; |
8df85a61 | 631 | } // end of wxApp::DoMessage |
d88de032 | 632 | |
9ed0fac8 DW |
633 | ////////////////////////////////////////////////////////////////////////////// |
634 | // | |
635 | // Keep trying to process messages until WM_QUIT | |
636 | // received. | |
637 | // | |
638 | // If there are messages to be processed, they will all be | |
639 | // processed and OnIdle will not be called. | |
640 | // When there are no more messages, OnIdle is called. | |
641 | // If OnIdle requests more time, | |
642 | // it will be repeatedly called so long as there are no pending messages. | |
643 | // A 'feature' of this is that once OnIdle has decided that no more processing | |
644 | // is required, then it won't get processing time until further messages | |
645 | // are processed (it'll sit in DoMessage). | |
646 | // | |
647 | ////////////////////////////////////////////////////////////////////////////// | |
0e320a79 DW |
648 | int wxApp::MainLoop() |
649 | { | |
9ed0fac8 | 650 | m_bKeepGoing = TRUE; |
d88de032 | 651 | |
9ed0fac8 | 652 | while (m_bKeepGoing) |
d88de032 DW |
653 | { |
654 | #if wxUSE_THREADS | |
655 | wxMutexGuiLeaveOrEnter(); | |
656 | #endif // wxUSE_THREADS | |
8df85a61 | 657 | while (!Pending() && ProcessIdle()) |
d88de032 DW |
658 | { |
659 | } | |
d88de032 DW |
660 | DoMessage(); |
661 | } | |
9ed0fac8 | 662 | return (int)svCurrentMsg.mp1; |
8df85a61 | 663 | } // end of wxApp::MainLoop |
0e320a79 | 664 | |
9ed0fac8 | 665 | // |
0e320a79 | 666 | // Returns TRUE if more time is needed. |
9ed0fac8 | 667 | // |
0e320a79 DW |
668 | bool wxApp::ProcessIdle() |
669 | { | |
9ed0fac8 | 670 | wxIdleEvent vEvent; |
0e320a79 | 671 | |
9ed0fac8 DW |
672 | vEvent.SetEventObject(this); |
673 | ProcessEvent(vEvent); | |
674 | return vEvent.MoreRequested(); | |
8df85a61 | 675 | } // end of wxApp::ProcessIdle |
d88de032 | 676 | |
0e320a79 DW |
677 | void wxApp::ExitMainLoop() |
678 | { | |
9ed0fac8 | 679 | m_bKeepGoing = FALSE; |
0e320a79 DW |
680 | } |
681 | ||
0e320a79 DW |
682 | bool wxApp::Pending() |
683 | { | |
43543d98 | 684 | return (::WinPeekMsg(vHabmain, (PQMSG)&svCurrentMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) != 0); |
0e320a79 DW |
685 | } |
686 | ||
0e320a79 DW |
687 | void wxApp::Dispatch() |
688 | { | |
d88de032 | 689 | DoMessage(); |
0e320a79 DW |
690 | } |
691 | ||
9ed0fac8 DW |
692 | ////////////////////////////////////////////////////////////////////////////// |
693 | // | |
694 | // Give all windows a chance to preprocess | |
695 | // the message. Some may have accelerator tables, or have | |
696 | // MDI complications. | |
697 | // | |
698 | ////////////////////////////////////////////////////////////////////////////// | |
699 | bool wxApp::ProcessMessage( | |
700 | WXMSG* pWxmsg | |
701 | ) | |
0e320a79 | 702 | { |
9ed0fac8 DW |
703 | QMSG* vMsg = (PQMSG)pWxmsg; |
704 | HWND hWnd = vMsg->hwnd; | |
705 | wxWindow* pWndThis = wxFindWinFromHandle((WXHWND)hWnd); | |
706 | wxWindow* pWnd; | |
d88de032 | 707 | |
8df85a61 DW |
708 | #if wxUSE_TOOLTIPS |
709 | // | |
710 | // We must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to | |
711 | // popup the tooltip bubbles | |
712 | // | |
713 | if (pWndThis && (vMsg->msg == WM_MOUSEMOVE)) | |
714 | { | |
715 | wxToolTip* pToolTip = pWndThis->GetToolTip(); | |
716 | if (pToolTip) | |
717 | { | |
718 | pToolTip->RelayEvent(pWxmsg); | |
719 | } | |
720 | } | |
721 | #endif // wxUSE_TOOLTIPS | |
722 | ||
9ed0fac8 | 723 | // |
8df85a61 | 724 | // For some composite controls (like a combobox), wndThis might be NULL |
d88de032 DW |
725 | // because the subcontrol is not a wxWindow, but only the control itself |
726 | // is - try to catch this case | |
9ed0fac8 DW |
727 | // |
728 | while (hWnd && !pWndThis) | |
d88de032 | 729 | { |
9ed0fac8 DW |
730 | hWnd = ::WinQueryWindow(hWnd, QW_PARENT); |
731 | pWndThis = wxFindWinFromHandle((WXHWND)hWnd); | |
d88de032 | 732 | } |
0e320a79 | 733 | |
8df85a61 | 734 | // |
d88de032 | 735 | // Anyone for a non-translation message? Try youngest descendants first. |
8df85a61 | 736 | // |
9ed0fac8 | 737 | for (pWnd = pWndThis; pWnd; pWnd = pWnd->GetParent()) |
d88de032 | 738 | { |
9ed0fac8 | 739 | if (pWnd->OS2ProcessMessage(pWxmsg)) |
d88de032 DW |
740 | return TRUE; |
741 | } | |
d88de032 | 742 | return FALSE; |
8df85a61 | 743 | } // end of wxApp::ProcessMessage |
0e320a79 | 744 | |
9ed0fac8 DW |
745 | void wxApp::OnIdle( |
746 | wxIdleEvent& rEvent | |
747 | ) | |
d88de032 | 748 | { |
9ed0fac8 | 749 | static bool sbInOnIdle = FALSE; |
d88de032 | 750 | |
9ed0fac8 | 751 | // |
d88de032 | 752 | // Avoid recursion (via ProcessEvent default case) |
9ed0fac8 DW |
753 | // |
754 | if (sbInOnIdle ) | |
d88de032 | 755 | return; |
0e320a79 | 756 | |
9ed0fac8 | 757 | sbInOnIdle = TRUE; |
0e320a79 | 758 | |
8df85a61 DW |
759 | // |
760 | // If there are pending events, we must process them: pending events | |
761 | // are either events to the threads other than main or events posted | |
762 | // with wxPostEvent() functions | |
763 | // | |
764 | ProcessPendingEvents(); | |
765 | ||
9ed0fac8 | 766 | // |
d88de032 | 767 | // 'Garbage' collection of windows deleted with Close(). |
9ed0fac8 | 768 | // |
d88de032 | 769 | DeletePendingObjects(); |
0e320a79 | 770 | |
d88de032 | 771 | #if wxUSE_LOG |
8df85a61 DW |
772 | // |
773 | // Flush the logged messages if any | |
774 | // | |
775 | wxLog::FlushActive(); | |
d88de032 | 776 | #endif // wxUSE_LOG |
0e320a79 | 777 | |
8df85a61 | 778 | // |
d88de032 | 779 | // Send OnIdle events to all windows |
8df85a61 | 780 | // |
9ed0fac8 | 781 | if (SendIdleEvents()) |
d88de032 | 782 | { |
9ed0fac8 | 783 | // |
d88de032 DW |
784 | // SendIdleEvents() returns TRUE if at least one window requested more |
785 | // idle events | |
9ed0fac8 DW |
786 | // |
787 | rEvent.RequestMore(TRUE); | |
d88de032 | 788 | } |
9ed0fac8 | 789 | sbInOnIdle = FALSE; |
8df85a61 | 790 | } // end of wxApp::OnIdle |
9779893b | 791 | |
0e320a79 DW |
792 | // Send idle event to all top-level windows |
793 | bool wxApp::SendIdleEvents() | |
794 | { | |
9ed0fac8 DW |
795 | bool bNeedMore = FALSE; |
796 | wxWindowList::Node* pNode = wxTopLevelWindows.GetFirst(); | |
d88de032 | 797 | |
9ed0fac8 | 798 | while (pNode) |
d88de032 | 799 | { |
9ed0fac8 | 800 | wxWindow* pWin = pNode->GetData(); |
0e320a79 | 801 | |
9ed0fac8 DW |
802 | if (SendIdleEvents(pWin)) |
803 | bNeedMore = TRUE; | |
804 | pNode = pNode->GetNext(); | |
805 | } | |
806 | return bNeedMore; | |
8df85a61 | 807 | } // end of wxApp::SendIdleEvents |
0e320a79 | 808 | |
9ed0fac8 | 809 | // |
0e320a79 | 810 | // Send idle event to window and all subwindows |
9ed0fac8 DW |
811 | // |
812 | bool wxApp::SendIdleEvents( | |
813 | wxWindow* pWin | |
814 | ) | |
0e320a79 | 815 | { |
9ed0fac8 DW |
816 | bool bNeedMore = FALSE; |
817 | wxIdleEvent vEvent; | |
818 | ||
819 | vEvent.SetEventObject(pWin); | |
820 | pWin->GetEventHandler()->ProcessEvent(vEvent); | |
0e320a79 | 821 | |
9ed0fac8 DW |
822 | if (vEvent.MoreRequested()) |
823 | bNeedMore = TRUE; | |
0e320a79 | 824 | |
9ed0fac8 | 825 | wxNode* pNode = pWin->GetChildren().First(); |
0e320a79 | 826 | |
9ed0fac8 | 827 | while (pNode) |
d88de032 | 828 | { |
9ed0fac8 | 829 | wxWindow* pWin = (wxWindow*) pNode->Data(); |
0e320a79 | 830 | |
9ed0fac8 DW |
831 | if (SendIdleEvents(pWin)) |
832 | bNeedMore = TRUE; | |
833 | pNode = pNode->Next(); | |
d88de032 | 834 | } |
9ed0fac8 | 835 | return bNeedMore; |
8df85a61 | 836 | } // end of wxApp::SendIdleEvents |
0e320a79 DW |
837 | |
838 | void wxApp::DeletePendingObjects() | |
839 | { | |
9ed0fac8 DW |
840 | wxNode* pNode = wxPendingDelete.First(); |
841 | ||
842 | while (pNode) | |
d88de032 | 843 | { |
9ed0fac8 | 844 | wxObject* pObj = (wxObject *)pNode->Data(); |
77cd51c3 | 845 | |
9ed0fac8 | 846 | delete pObj; |
0e320a79 | 847 | |
9ed0fac8 DW |
848 | if (wxPendingDelete.Member(pObj)) |
849 | delete pNode; | |
0e320a79 | 850 | |
9ed0fac8 | 851 | // |
d88de032 DW |
852 | // Deleting one object may have deleted other pending |
853 | // objects, so start from beginning of list again. | |
9ed0fac8 DW |
854 | // |
855 | pNode = wxPendingDelete.First(); | |
d88de032 | 856 | } |
8df85a61 | 857 | } // end of wxApp::DeletePendingObjects |
0e320a79 | 858 | |
9ed0fac8 DW |
859 | void wxApp::OnEndSession( |
860 | wxCloseEvent& WXUNUSED(rEvent)) | |
0e320a79 | 861 | { |
d88de032 DW |
862 | if (GetTopWindow()) |
863 | GetTopWindow()->Close(TRUE); | |
8df85a61 | 864 | } // end of wxApp::OnEndSession |
0e320a79 | 865 | |
9ed0fac8 | 866 | // |
d88de032 DW |
867 | // Default behaviour: close the application with prompts. The |
868 | // user can veto the close, and therefore the end session. | |
9ed0fac8 DW |
869 | // |
870 | void wxApp::OnQueryEndSession( | |
871 | wxCloseEvent& rEvent | |
872 | ) | |
0e320a79 | 873 | { |
d88de032 DW |
874 | if (GetTopWindow()) |
875 | { | |
9ed0fac8 DW |
876 | if (!GetTopWindow()->Close(!rEvent.CanVeto())) |
877 | rEvent.Veto(TRUE); | |
d88de032 | 878 | } |
8df85a61 | 879 | } // end of wxApp::OnQueryEndSession |
0e320a79 DW |
880 | |
881 | void wxExit() | |
882 | { | |
d88de032 DW |
883 | wxLogError(_("Fatal error: exiting")); |
884 | ||
885 | wxApp::CleanUp(); | |
ee453a16 | 886 | } // end of wxExit |
0e320a79 | 887 | |
8df85a61 | 888 | // |
d88de032 | 889 | // Yield to incoming messages |
8df85a61 | 890 | // |
0e320a79 DW |
891 | bool wxYield() |
892 | { | |
9dea36ef | 893 | HAB vHab = 0; |
dde11e60 | 894 | QMSG vMsg; |
ee453a16 | 895 | |
8df85a61 DW |
896 | // |
897 | // Disable log flushing from here because a call to wxYield() shouldn't | |
898 | // normally result in message boxes popping up &c | |
899 | // | |
900 | wxLog::Suspend(); | |
901 | ||
902 | // | |
d88de032 DW |
903 | // We want to go back to the main message loop |
904 | // if we see a WM_QUIT. (?) | |
8df85a61 | 905 | // |
dde11e60 | 906 | while (::WinPeekMsg(vHab, &vMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) && vMsg.msg != WM_QUIT) |
d88de032 | 907 | { |
8df85a61 DW |
908 | #if wxUSE_THREADS |
909 | wxMutexGuiLeaveOrEnter(); | |
910 | #endif // wxUSE_THREADS | |
dde11e60 | 911 | if (!wxTheApp->DoMessage()) |
d88de032 DW |
912 | break; |
913 | } | |
8df85a61 | 914 | // |
d88de032 | 915 | // If they are pending events, we must process them. |
8df85a61 DW |
916 | // |
917 | if (wxTheApp) | |
918 | wxTheApp->ProcessPendingEvents(); | |
919 | ||
920 | // | |
921 | // Let the logs be flashed again | |
922 | // | |
923 | wxLog::Resume(); | |
d88de032 | 924 | return TRUE; |
8df85a61 | 925 | } // end of wxYield |
d88de032 | 926 | |
9ed0fac8 DW |
927 | wxIcon wxApp::GetStdIcon( |
928 | int nWhich | |
929 | ) const | |
d88de032 | 930 | { |
9ed0fac8 | 931 | switch(nWhich) |
d88de032 DW |
932 | { |
933 | case wxICON_INFORMATION: | |
934 | return wxIcon("wxICON_INFO"); | |
935 | ||
936 | case wxICON_QUESTION: | |
937 | return wxIcon("wxICON_QUESTION"); | |
938 | ||
939 | case wxICON_EXCLAMATION: | |
940 | return wxIcon("wxICON_WARNING"); | |
941 | ||
942 | default: | |
943 | wxFAIL_MSG(wxT("requested non existent standard icon")); | |
944 | // still fall through | |
945 | ||
946 | case wxICON_HAND: | |
947 | return wxIcon("wxICON_ERROR"); | |
948 | } | |
d88de032 | 949 | return wxIcon("wxICON_ERROR"); |
8df85a61 DW |
950 | } // end of wxApp::GetStdIcon |
951 | ||
952 | //----------------------------------------------------------------------------- | |
953 | // wxWakeUpIdle | |
954 | //----------------------------------------------------------------------------- | |
955 | ||
956 | void wxWakeUpIdle() | |
957 | { | |
958 | // | |
959 | // Send the top window a dummy message so idle handler processing will | |
960 | // start up again. Doing it this way ensures that the idle handler | |
961 | // wakes up in the right thread (see also wxWakeUpMainThread() which does | |
962 | // the same for the main app thread only) | |
963 | // | |
964 | wxWindow* pTopWindow = wxTheApp->GetTopWindow(); | |
965 | ||
966 | if (pTopWindow) | |
967 | { | |
968 | if ( !::WinPostMsg(GetHwndOf(pTopWindow), WM_NULL, (MPARAM)0, (MPARAM)0)) | |
969 | { | |
970 | // | |
971 | // Should never happen | |
972 | // | |
973 | wxLogLastError("PostMessage(WM_NULL)"); | |
974 | } | |
975 | } | |
976 | } // end of wxWakeUpIdle | |
d88de032 | 977 | |
d88de032 DW |
978 | HINSTANCE wxGetInstance() |
979 | { | |
980 | return wxhInstance; | |
981 | } | |
982 | ||
983 | void wxSetInstance(HINSTANCE hInst) | |
984 | { | |
985 | wxhInstance = hInst; | |
0e320a79 DW |
986 | } |
987 |