]>
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" | |
cb21bd1b SN |
30 | #include "wx/wxchar.h" |
31 | #include "wx/icon.h" | |
32 | #include "wx/timer.h" | |
0e320a79 DW |
33 | #endif |
34 | ||
0e320a79 DW |
35 | #include "wx/log.h" |
36 | #include "wx/module.h" | |
d88de032 DW |
37 | |
38 | #include "wx/os2/private.h" | |
39 | ||
29d83fc1 DW |
40 | #ifdef __EMX__ |
41 | ||
19193a2c KB |
42 | #include <sys/ioctl.h> |
43 | #include <sys/select.h> | |
29d83fc1 DW |
44 | |
45 | #else | |
46 | ||
65b851bb | 47 | #include <nerrno.h> |
19193a2c KB |
48 | #include <sys/ioctl.h> |
49 | #include <sys/select.h> | |
50 | #include <sys/time.h> | |
29d83fc1 | 51 | |
19193a2c | 52 | #endif // |
29d83fc1 | 53 | |
3958ae62 | 54 | #ifndef __EMX__ |
29d83fc1 | 55 | |
3958ae62 | 56 | #define select(a,b,c,d,e) bsdselect(a,b,c,d,e) |
19193a2c KB |
57 | extern "C" int _System bsdselect(int, |
58 | struct fd_set *, | |
59 | struct fd_set *, | |
60 | struct fd_set *, | |
61 | struct timeval *); | |
3958ae62 SN |
62 | #endif |
63 | ||
d88de032 DW |
64 | #if wxUSE_THREADS |
65 | #include "wx/thread.h" | |
66 | ||
9ed0fac8 DW |
67 | // define the array of QMSG strutures |
68 | WX_DECLARE_OBJARRAY(QMSG, wxMsgArray); | |
d88de032 DW |
69 | |
70 | #include "wx/arrimpl.cpp" | |
71 | ||
9ed0fac8 | 72 | WX_DEFINE_OBJARRAY(wxMsgArray); |
d88de032 | 73 | #endif // wxUSE_THREADS |
0e320a79 DW |
74 | |
75 | #if wxUSE_WX_RESOURCES | |
d88de032 | 76 | #include "wx/resource.h" |
0e320a79 DW |
77 | #endif |
78 | ||
8df85a61 DW |
79 | #if wxUSE_TOOLTIPS |
80 | #include "wx/tooltip.h" | |
81 | #endif // wxUSE_TOOLTIPS | |
82 | ||
0e320a79 | 83 | #include <string.h> |
d88de032 DW |
84 | #include <ctype.h> |
85 | ||
86 | // --------------------------------------------------------------------------- | |
87 | // global variables | |
88 | // --------------------------------------------------------------------------- | |
89 | ||
9ed0fac8 | 90 | extern wxChar* wxBuffer; |
9ed0fac8 DW |
91 | extern wxList* wxWinHandleList; |
92 | extern wxList WXDLLEXPORT wxPendingDelete; | |
9ed0fac8 | 93 | extern wxCursor* g_globalCursor; |
0e320a79 | 94 | |
8df85a61 | 95 | HAB vHabmain = NULLHANDLE; |
9ed0fac8 DW |
96 | QMSG svCurrentMsg; |
97 | wxApp* wxTheApp = NULL; | |
d88de032 | 98 | |
d88de032 | 99 | |
9ed0fac8 DW |
100 | HICON wxSTD_FRAME_ICON = (HICON) NULL; |
101 | HICON wxSTD_MDICHILDFRAME_ICON = (HICON) NULL; | |
d88de032 DW |
102 | HICON wxSTD_MDIPARENTFRAME_ICON = (HICON) NULL; |
103 | ||
9ed0fac8 DW |
104 | HICON wxDEFAULT_FRAME_ICON = (HICON) NULL; |
105 | HICON wxDEFAULT_MDICHILDFRAME_ICON = (HICON) NULL; | |
106 | HICON wxDEFAULT_MDIPARENTFRAME_ICON = (HICON) NULL; | |
d88de032 DW |
107 | |
108 | HBRUSH wxDisableButtonBrush = (HBRUSH) 0; | |
109 | ||
51c1d535 DW |
110 | MRESULT EXPENTRY wxWndProc( HWND hWnd,ULONG message,MPARAM mp1,MPARAM mp2); |
111 | MRESULT EXPENTRY wxFrameWndProc( HWND hWnd,ULONG message,MPARAM mp1,MPARAM mp2); | |
112 | ||
d88de032 DW |
113 | // =========================================================================== |
114 | // implementation | |
115 | // =========================================================================== | |
116 | ||
3958ae62 SN |
117 | // --------------------------------------------------------------------------- |
118 | // helper struct and functions for socket handling | |
119 | // --------------------------------------------------------------------------- | |
120 | ||
121 | struct GsocketCallbackInfo{ | |
122 | void (*proc)(void *); | |
123 | int type; | |
124 | int handle; | |
125 | void* gsock; | |
126 | }; | |
127 | ||
128 | // These defines and wrapper functions are used here and in gsockpm.c | |
129 | #define wxSockReadMask 0x01 | |
130 | #define wxSockWriteMask 0x02 | |
131 | ||
29d83fc1 | 132 | #ifdef __EMX__ |
3958ae62 SN |
133 | extern "C" |
134 | int wxAppAddSocketHandler(int handle, int mask, | |
29d83fc1 | 135 | void (*callback)(void*), void * gsock) |
3958ae62 SN |
136 | { |
137 | return wxTheApp->AddSocketHandler(handle, mask, callback, gsock); | |
138 | } | |
3958ae62 SN |
139 | extern "C" |
140 | void wxAppRemoveSocketHandler(int handle) | |
141 | { | |
142 | wxTheApp->RemoveSocketHandler(handle); | |
143 | } | |
29d83fc1 DW |
144 | #else |
145 | // Linkage mode problems using callbacks with extern C in a .cpp module | |
146 | int wxAppAddSocketHandler(int handle, int mask, | |
147 | void (*callback)(void*), void * gsock) | |
148 | { | |
149 | return wxTheApp->AddSocketHandler(handle, mask, callback, gsock); | |
150 | } | |
151 | void wxAppRemoveSocketHandler(int handle) | |
152 | { | |
153 | wxTheApp->RemoveSocketHandler(handle); | |
154 | } | |
155 | #endif | |
3958ae62 SN |
156 | |
157 | void wxApp::HandleSockets() | |
158 | { | |
29d83fc1 | 159 | bool pendingEvent = FALSE; |
3958ae62 SN |
160 | |
161 | // Check whether it's time for Gsocket operation | |
162 | if (m_maxSocketHandles > 0 && m_maxSocketNr > 0) | |
163 | { | |
164 | fd_set readfds = m_readfds; | |
165 | fd_set writefds = m_writefds; | |
166 | struct timeval timeout; | |
167 | int i; | |
168 | struct GsocketCallbackInfo | |
169 | *CallbackInfo = (struct GsocketCallbackInfo *)m_sockCallbackInfo; | |
170 | int r = 0; | |
171 | timeout.tv_sec = 0; | |
172 | timeout.tv_usec = 0; | |
173 | if ( select(m_maxSocketNr, &readfds, &writefds, 0, &timeout) > 0) | |
174 | { | |
175 | for (i = m_lastUsedHandle + 1; i != m_lastUsedHandle; i++) | |
176 | { | |
177 | if (i == m_maxSocketNr) | |
178 | i = 0; | |
179 | if (FD_ISSET(i, &readfds)) | |
180 | { | |
181 | int r; | |
182 | for (r = 0; r < m_maxSocketHandles; r++){ | |
183 | if(CallbackInfo[r].handle == i && | |
184 | CallbackInfo[r].type == wxSockReadMask) | |
185 | break; | |
186 | } | |
187 | if (r < m_maxSocketHandles) | |
188 | { | |
189 | CallbackInfo[r].proc(CallbackInfo[r].gsock); | |
29d83fc1 | 190 | pendingEvent = TRUE; |
3958ae62 SN |
191 | wxYield(); |
192 | } | |
193 | } | |
194 | if (FD_ISSET(i, &writefds)) | |
195 | { | |
196 | int r; | |
197 | for (r = 0; r < m_maxSocketHandles; r++) | |
198 | if(CallbackInfo[r].handle == i && | |
199 | CallbackInfo[r].type == wxSockWriteMask) | |
200 | break; | |
201 | if (r < m_maxSocketHandles) | |
202 | { | |
203 | CallbackInfo[r].proc(CallbackInfo[r].gsock); | |
29d83fc1 | 204 | pendingEvent = TRUE; |
3958ae62 SN |
205 | wxYield(); |
206 | } | |
207 | } | |
208 | } | |
209 | m_lastUsedHandle = i; | |
210 | } | |
211 | if (pendingEvent) | |
212 | wxYield(); | |
213 | } | |
214 | } | |
d88de032 DW |
215 | // --------------------------------------------------------------------------- |
216 | // wxApp | |
217 | // --------------------------------------------------------------------------- | |
218 | ||
d88de032 | 219 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) |
0e320a79 | 220 | |
d88de032 DW |
221 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) |
222 | EVT_IDLE(wxApp::OnIdle) | |
223 | EVT_END_SESSION(wxApp::OnEndSession) | |
224 | EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession) | |
225 | END_EVENT_TABLE() | |
0e320a79 | 226 | |
8df85a61 DW |
227 | // |
228 | // Initialize | |
229 | // | |
9ed0fac8 DW |
230 | bool wxApp::Initialize( |
231 | HAB vHab | |
232 | ) | |
0e320a79 | 233 | { |
7e99520b DW |
234 | #if defined(wxUSE_CONSOLEDEBUG) |
235 | #if wxUSE_CONSOLEDEBUG | |
236 | /***********************************************/ | |
237 | /* Code for using stdout debug */ | |
238 | /* To use it you mast link app as "Window" - EK*/ | |
239 | /***********************************************/ | |
240 | { | |
241 | PPIB pib; | |
242 | PTIB tib; | |
243 | ||
244 | printf("In console\n"); | |
245 | ||
246 | DosGetInfoBlocks(&tib, &pib); | |
247 | /* Try morphing into a PM application. */ | |
248 | // if(pib->pib_ultype == 2) /* VIO */ | |
249 | pib->pib_ultype = 3; | |
250 | } | |
251 | /**********************************************/ | |
252 | /**********************************************/ | |
253 | #endif //wxUSE_CONSOLEDEBUG | |
254 | #endif | |
255 | ||
13a4ea8d | 256 | wxBuffer = new wxChar[1500]; // FIXME; why? |
0e320a79 | 257 | |
d88de032 | 258 | wxClassInfo::InitializeClasses(); |
0e320a79 | 259 | |
8df85a61 DW |
260 | #if wxUSE_THREADS |
261 | wxPendingEventsLocker = new wxCriticalSection; | |
262 | #endif | |
263 | ||
d88de032 DW |
264 | wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING); |
265 | wxTheColourDatabase->Initialize(); | |
266 | ||
267 | wxInitializeStockLists(); | |
268 | wxInitializeStockObjects(); | |
0e320a79 DW |
269 | |
270 | #if wxUSE_WX_RESOURCES | |
d88de032 | 271 | wxInitializeResourceSystem(); |
0e320a79 DW |
272 | #endif |
273 | ||
d88de032 DW |
274 | wxBitmap::InitStandardHandlers(); |
275 | ||
54ffa107 DW |
276 | // |
277 | // OS2 has to have an anchorblock | |
278 | // | |
279 | vHab = WinInitialize(0); | |
280 | ||
281 | if (!vHab) | |
282 | return FALSE; | |
283 | else | |
284 | vHabmain = vHab; | |
285 | ||
286 | // Some people may wish to use this, but | |
287 | // probably it shouldn't be here by default. | |
288 | #ifdef __WXDEBUG__ | |
289 | // wxRedirectIOToConsole(); | |
290 | #endif | |
291 | ||
d88de032 DW |
292 | wxWinHandleList = new wxList(wxKEY_INTEGER); |
293 | ||
294 | // This is to foil optimizations in Visual C++ that throw out dummy.obj. | |
295 | // PLEASE DO NOT ALTER THIS. | |
1b3d5e55 | 296 | #if !defined(WXMAKINGDLL) && defined(__VISAGECPP__) |
d88de032 DW |
297 | extern char wxDummyChar; |
298 | if (wxDummyChar) wxDummyChar++; | |
299 | #endif | |
300 | ||
61243a51 | 301 | // wxSetKeyboardHook(TRUE); |
d88de032 DW |
302 | |
303 | wxModule::RegisterModules(); | |
304 | if (!wxModule::InitializeModules()) | |
305 | return FALSE; | |
54ffa107 | 306 | RegisterWindowClasses(vHab); |
d88de032 | 307 | return TRUE; |
8df85a61 | 308 | } // end of wxApp::Initialize |
d88de032 | 309 | |
7e99520b | 310 | const char* CANTREGISTERCLASS = " Can't register Class "; |
d88de032 DW |
311 | // --------------------------------------------------------------------------- |
312 | // RegisterWindowClasses | |
313 | // --------------------------------------------------------------------------- | |
314 | ||
9ed0fac8 DW |
315 | bool wxApp::RegisterWindowClasses( |
316 | HAB vHab | |
317 | ) | |
d88de032 | 318 | { |
914589c2 DW |
319 | ERRORID vError = 0L; |
320 | wxString sError; | |
3b9e3455 | 321 | |
f23208ca DW |
322 | if (!::WinRegisterClass( vHab |
323 | ,wxFrameClassName | |
51c1d535 | 324 | ,wxFrameWndProc |
f9efbe3a | 325 | ,CS_SIZEREDRAW | CS_SYNCPAINT |
a0606634 | 326 | ,sizeof(ULONG) |
f23208ca | 327 | )) |
d88de032 | 328 | { |
914589c2 DW |
329 | vError = ::WinGetLastError(vHab); |
330 | sError = wxPMErrorToStr(vError); | |
331 | wxLogLastError(sError); | |
d88de032 DW |
332 | return FALSE; |
333 | } | |
334 | ||
f23208ca DW |
335 | if (!::WinRegisterClass( vHab |
336 | ,wxFrameClassNameNoRedraw | |
51c1d535 | 337 | ,wxWndProc |
f23208ca | 338 | ,0 |
51c1d535 | 339 | ,sizeof(ULONG) |
f23208ca | 340 | )) |
d88de032 | 341 | { |
914589c2 DW |
342 | vError = ::WinGetLastError(vHab); |
343 | sError = wxPMErrorToStr(vError); | |
344 | wxLogLastError(sError); | |
d88de032 DW |
345 | return FALSE; |
346 | } | |
347 | ||
f23208ca DW |
348 | if (!::WinRegisterClass( vHab |
349 | ,wxMDIFrameClassName | |
51c1d535 | 350 | ,wxWndProc |
f6bcfd97 | 351 | ,CS_SIZEREDRAW | CS_MOVENOTIFY | CS_SYNCPAINT |
51c1d535 | 352 | ,sizeof(ULONG) |
f23208ca | 353 | )) |
d88de032 | 354 | { |
914589c2 DW |
355 | vError = ::WinGetLastError(vHab); |
356 | sError = wxPMErrorToStr(vError); | |
357 | wxLogLastError(sError); | |
d88de032 DW |
358 | return FALSE; |
359 | } | |
0e320a79 | 360 | |
f23208ca DW |
361 | if (!::WinRegisterClass( vHab |
362 | ,wxMDIFrameClassNameNoRedraw | |
51c1d535 | 363 | ,wxWndProc |
f23208ca | 364 | ,0 |
51c1d535 | 365 | ,sizeof(ULONG) |
f23208ca | 366 | )) |
d88de032 | 367 | { |
914589c2 DW |
368 | vError = ::WinGetLastError(vHab); |
369 | sError = wxPMErrorToStr(vError); | |
370 | wxLogLastError(sError); | |
d88de032 DW |
371 | return FALSE; |
372 | } | |
373 | ||
f23208ca DW |
374 | if (!::WinRegisterClass( vHab |
375 | ,wxMDIChildFrameClassName | |
51c1d535 | 376 | ,wxWndProc |
f23208ca | 377 | ,CS_MOVENOTIFY | CS_SIZEREDRAW | CS_SYNCPAINT | CS_HITTEST |
51c1d535 | 378 | ,sizeof(ULONG) |
f23208ca | 379 | )) |
d88de032 | 380 | { |
914589c2 DW |
381 | vError = ::WinGetLastError(vHab); |
382 | sError = wxPMErrorToStr(vError); | |
383 | wxLogLastError(sError); | |
d88de032 DW |
384 | return FALSE; |
385 | } | |
386 | ||
f23208ca DW |
387 | if (!::WinRegisterClass( vHab |
388 | ,wxMDIChildFrameClassNameNoRedraw | |
51c1d535 | 389 | ,wxWndProc |
f23208ca | 390 | ,CS_HITTEST |
51c1d535 | 391 | ,sizeof(ULONG) |
f23208ca | 392 | )) |
d88de032 | 393 | { |
914589c2 DW |
394 | vError = ::WinGetLastError(vHab); |
395 | sError = wxPMErrorToStr(vError); | |
396 | wxLogLastError(sError); | |
d88de032 DW |
397 | return FALSE; |
398 | } | |
399 | ||
f23208ca DW |
400 | if (!::WinRegisterClass( vHab |
401 | ,wxPanelClassName | |
51c1d535 | 402 | ,wxWndProc |
f23208ca | 403 | ,CS_MOVENOTIFY | CS_SIZEREDRAW | CS_HITTEST | CS_SAVEBITS | CS_SYNCPAINT |
51c1d535 | 404 | ,sizeof(ULONG) |
f23208ca | 405 | )) |
d88de032 | 406 | { |
914589c2 DW |
407 | vError = ::WinGetLastError(vHab); |
408 | sError = wxPMErrorToStr(vError); | |
409 | wxLogLastError(sError); | |
d88de032 DW |
410 | return FALSE; |
411 | } | |
412 | ||
f23208ca DW |
413 | if (!::WinRegisterClass( vHab |
414 | ,wxCanvasClassName | |
51c1d535 | 415 | ,wxWndProc |
54ffa107 | 416 | ,CS_SIZEREDRAW | CS_HITTEST | CS_SYNCPAINT |
51c1d535 | 417 | ,sizeof(ULONG) |
f23208ca | 418 | )) |
d88de032 | 419 | { |
914589c2 DW |
420 | vError = ::WinGetLastError(vHab); |
421 | sError = wxPMErrorToStr(vError); | |
422 | wxLogLastError(sError); | |
d88de032 DW |
423 | return FALSE; |
424 | } | |
0256cfeb DW |
425 | if (!::WinRegisterClass( vHab |
426 | ,wxCanvasClassNameNR | |
427 | ,wxWndProc | |
428 | ,CS_HITTEST | CS_SYNCPAINT | |
429 | ,sizeof(ULONG) | |
430 | )) | |
431 | { | |
432 | vError = ::WinGetLastError(vHab); | |
433 | sError = wxPMErrorToStr(vError); | |
434 | wxLogLastError(sError); | |
435 | return FALSE; | |
436 | } | |
d88de032 | 437 | return TRUE; |
8df85a61 | 438 | } // end of wxApp::RegisterWindowClasses |
d88de032 | 439 | |
8df85a61 DW |
440 | // |
441 | // Cleans up any wxWindows internal structures left lying around | |
442 | // | |
0e320a79 DW |
443 | void wxApp::CleanUp() |
444 | { | |
8df85a61 DW |
445 | // |
446 | // COMMON CLEANUP | |
447 | // | |
d88de032 DW |
448 | |
449 | #if wxUSE_LOG | |
8df85a61 DW |
450 | |
451 | // | |
452 | // Flush the logged messages if any and install a 'safer' log target: the | |
d88de032 DW |
453 | // default one (wxLogGui) can't be used after the resources are freed just |
454 | // below and the user suppliedo ne might be even more unsafe (using any | |
455 | // wxWindows GUI function is unsafe starting from now) | |
8df85a61 | 456 | // |
d88de032 DW |
457 | wxLog::DontCreateOnDemand(); |
458 | ||
8df85a61 DW |
459 | // |
460 | // This will flush the old messages if any | |
461 | // | |
d88de032 DW |
462 | delete wxLog::SetActiveTarget(new wxLogStderr); |
463 | #endif // wxUSE_LOG | |
464 | ||
8df85a61 | 465 | // |
d88de032 | 466 | // One last chance for pending objects to be cleaned up |
8df85a61 | 467 | // |
d88de032 DW |
468 | wxTheApp->DeletePendingObjects(); |
469 | ||
470 | wxModule::CleanUpModules(); | |
0e320a79 DW |
471 | |
472 | #if wxUSE_WX_RESOURCES | |
d88de032 | 473 | wxCleanUpResourceSystem(); |
d88de032 | 474 | #endif |
0e320a79 | 475 | |
d88de032 | 476 | wxDeleteStockObjects(); |
0e320a79 | 477 | |
8df85a61 | 478 | // |
d88de032 | 479 | // Destroy all GDI lists, etc. |
8df85a61 | 480 | // |
d88de032 | 481 | wxDeleteStockLists(); |
0e320a79 | 482 | |
d88de032 DW |
483 | delete wxTheColourDatabase; |
484 | wxTheColourDatabase = NULL; | |
0e320a79 | 485 | |
d88de032 | 486 | wxBitmap::CleanUpHandlers(); |
0e320a79 | 487 | |
d88de032 DW |
488 | delete[] wxBuffer; |
489 | wxBuffer = NULL; | |
0e320a79 | 490 | |
8df85a61 DW |
491 | // |
492 | // PM-SPECIFIC CLEANUP | |
493 | // | |
0e320a79 | 494 | |
61243a51 | 495 | // wxSetKeyboardHook(FALSE); |
9ed0fac8 | 496 | |
d88de032 | 497 | if (wxSTD_FRAME_ICON) |
9ed0fac8 | 498 | ::WinFreeFileIcon(wxSTD_FRAME_ICON); |
d88de032 | 499 | if (wxSTD_MDICHILDFRAME_ICON) |
9ed0fac8 | 500 | ::WinFreeFileIcon(wxSTD_MDICHILDFRAME_ICON); |
d88de032 | 501 | if (wxSTD_MDIPARENTFRAME_ICON) |
9ed0fac8 | 502 | ::WinFreeFileIcon(wxSTD_MDIPARENTFRAME_ICON); |
d88de032 DW |
503 | |
504 | if (wxDEFAULT_FRAME_ICON) | |
9ed0fac8 | 505 | ::WinFreeFileIcon(wxDEFAULT_FRAME_ICON); |
d88de032 | 506 | if (wxDEFAULT_MDICHILDFRAME_ICON) |
9ed0fac8 | 507 | ::WinFreeFileIcon(wxDEFAULT_MDICHILDFRAME_ICON); |
d88de032 | 508 | if (wxDEFAULT_MDIPARENTFRAME_ICON) |
9ed0fac8 DW |
509 | ::WinFreeFileIcon(wxDEFAULT_MDIPARENTFRAME_ICON); |
510 | ||
d88de032 DW |
511 | if ( wxDisableButtonBrush ) |
512 | { | |
513 | // TODO: ::DeleteObject( wxDisableButtonBrush ); | |
514 | } | |
515 | ||
516 | if (wxWinHandleList) | |
517 | delete wxWinHandleList; | |
518 | ||
d88de032 | 519 | delete wxPendingEvents; |
8df85a61 | 520 | #if wxUSE_THREADS |
d88de032 DW |
521 | delete wxPendingEventsLocker; |
522 | // If we don't do the following, we get an apparent memory leak. | |
523 | ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker(); | |
524 | #endif | |
0e320a79 | 525 | |
d88de032 | 526 | wxClassInfo::CleanUpClasses(); |
0e320a79 | 527 | |
468e327a SN |
528 | // Delete Message queue |
529 | if (wxTheApp->m_hMq) | |
530 | ::WinDestroyMsgQueue(wxTheApp->m_hMq); | |
531 | ||
d88de032 DW |
532 | delete wxTheApp; |
533 | wxTheApp = NULL; | |
77cd51c3 | 534 | |
0e320a79 | 535 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
d88de032 DW |
536 | // At this point we want to check if there are any memory |
537 | // blocks that aren't part of the wxDebugContext itself, | |
538 | // as a special case. Then when dumping we need to ignore | |
539 | // wxDebugContext, too. | |
540 | if (wxDebugContext::CountObjectsLeft(TRUE) > 0) | |
541 | { | |
542 | wxLogDebug(wxT("There were memory leaks.")); | |
543 | wxDebugContext::Dump(); | |
544 | wxDebugContext::PrintStatistics(); | |
545 | } | |
546 | // wxDebugContext::SetStream(NULL, NULL); | |
0e320a79 | 547 | #endif |
77cd51c3 | 548 | |
d88de032 DW |
549 | #if wxUSE_LOG |
550 | // do it as the very last thing because everything else can log messages | |
551 | delete wxLog::SetActiveTarget(NULL); | |
552 | #endif // wxUSE_LOG | |
8df85a61 | 553 | } // end of wxApp::CleanUp |
0e320a79 | 554 | |
7e99520b DW |
555 | //---------------------------------------------------------------------- |
556 | // Main wxWindows entry point | |
557 | //---------------------------------------------------------------------- | |
9ed0fac8 DW |
558 | int wxEntry( |
559 | int argc | |
560 | , char* argv[] | |
561 | ) | |
d88de032 | 562 | { |
9dea36ef | 563 | HAB vHab = 0; |
d88de032 | 564 | |
9ed0fac8 DW |
565 | if (!wxApp::Initialize(vHab)) |
566 | return 0; | |
d88de032 | 567 | |
9ed0fac8 DW |
568 | // |
569 | // create the application object or ensure that one already exists | |
570 | // | |
571 | if (!wxTheApp) | |
572 | { | |
426d5745 DW |
573 | // The app may have declared a global application object, but we recommend |
574 | // the IMPLEMENT_APP macro is used instead, which sets an initializer | |
575 | // function for delayed, dynamic app object construction. | |
576 | wxCHECK_MSG( wxApp::GetInitializerFunction(), 0, | |
577 | wxT("No initializer - use IMPLEMENT_APP macro.") ); | |
578 | wxTheApp = (*wxApp::GetInitializerFunction()) (); | |
9ed0fac8 | 579 | } |
426d5745 | 580 | wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") ); |
9ed0fac8 | 581 | wxTheApp->argc = argc; |
d88de032 | 582 | |
9ed0fac8 DW |
583 | #if wxUSE_UNICODE |
584 | wxTheApp->argv = new wxChar*[argc+1]; | |
585 | ||
586 | int nArgc = 0; | |
587 | ||
588 | while (nArgc < argc) | |
589 | { | |
590 | wxTheApp->argv[nArgc] = wxStrdup(wxConvLibc.cMB2WX(argv[nArgc])); | |
591 | nArgc++; | |
592 | } | |
593 | wxTheApp->argv[nArgc] = (wxChar *)NULL; | |
594 | #else | |
595 | wxTheApp->argv = argv; | |
596 | #endif | |
d88de032 | 597 | |
9ed0fac8 | 598 | wxString sName(wxFileNameFromPath(argv[0])); |
d88de032 | 599 | |
9ed0fac8 DW |
600 | wxStripExtension(sName); |
601 | wxTheApp->SetAppName(sName); | |
d88de032 | 602 | |
9ed0fac8 | 603 | int nRetValue = 0; |
d88de032 | 604 | |
9ed0fac8 DW |
605 | if (!wxTheApp->OnInitGui()) |
606 | nRetValue = -1; | |
d88de032 | 607 | |
9ed0fac8 DW |
608 | if (nRetValue == 0) |
609 | { | |
610 | if (wxTheApp->OnInit()) | |
d88de032 | 611 | { |
7bed7a50 | 612 | nRetValue = wxTheApp->OnRun(); |
7bed7a50 | 613 | } |
51c1d535 DW |
614 | // Normal exit |
615 | wxWindow* pTopWindow = wxTheApp->GetTopWindow(); | |
616 | if (pTopWindow) | |
d88de032 | 617 | { |
51c1d535 DW |
618 | // Forcibly delete the window. |
619 | if (pTopWindow->IsKindOf(CLASSINFO(wxFrame)) || | |
620 | pTopWindow->IsKindOf(CLASSINFO(wxDialog)) ) | |
621 | { | |
622 | pTopWindow->Close(TRUE); | |
623 | wxTheApp->DeletePendingObjects(); | |
624 | } | |
625 | else | |
626 | { | |
627 | delete pTopWindow; | |
628 | wxTheApp->SetTopWindow(NULL); | |
629 | } | |
d88de032 | 630 | } |
9ed0fac8 | 631 | } |
7e99520b DW |
632 | else // app initialization failed |
633 | { | |
634 | wxLogLastError(" Gui initialization failed, exitting"); | |
635 | } | |
636 | #if wxUSE_CONSOLEDEBUG | |
637 | printf("wxTheApp->OnExit "); | |
638 | fflush(stdout); | |
639 | #endif | |
9ed0fac8 | 640 | wxTheApp->OnExit(); |
7e99520b DW |
641 | #if wxUSE_CONSOLEDEBUG |
642 | printf("wxApp::CleanUp "); | |
643 | fflush(stdout); | |
644 | #endif | |
9ed0fac8 | 645 | wxApp::CleanUp(); |
7e99520b DW |
646 | #if wxUSE_CONSOLEDEBUG |
647 | printf("return %i ", nRetValue); | |
648 | fflush(stdout); | |
649 | #endif | |
9ed0fac8 | 650 | return(nRetValue); |
8df85a61 | 651 | } // end of wxEntry |
77cd51c3 | 652 | |
9ed0fac8 | 653 | bool wxApp::OnInitGui() |
d88de032 | 654 | { |
914589c2 DW |
655 | ERRORID vError; |
656 | wxString sError; | |
77cd51c3 | 657 | |
19193a2c KB |
658 | if (!wxAppBase::OnInitGui()) |
659 | return FALSE; | |
660 | ||
f23208ca | 661 | m_hMq = ::WinCreateMsgQueue(vHabmain, 0); |
914589c2 DW |
662 | if (!m_hMq) |
663 | { | |
664 | vError = ::WinGetLastError(vHabmain); | |
665 | sError = wxPMErrorToStr(vError); | |
666 | wxLogDebug(sError); | |
667 | return FALSE; | |
668 | } | |
19193a2c | 669 | |
9ed0fac8 | 670 | return TRUE; |
8df85a61 | 671 | } // end of wxApp::OnInitGui |
0e320a79 | 672 | |
9ed0fac8 DW |
673 | // |
674 | // Static member initialization | |
675 | // | |
d88de032 | 676 | wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL; |
0e320a79 DW |
677 | |
678 | wxApp::wxApp() | |
679 | { | |
d88de032 DW |
680 | m_topWindow = NULL; |
681 | wxTheApp = this; | |
d88de032 DW |
682 | |
683 | argc = 0; | |
684 | argv = NULL; | |
9ed0fac8 | 685 | m_nPrintMode = wxPRINT_WINDOWS; |
d88de032 | 686 | m_exitOnFrameDelete = TRUE; |
9ed0fac8 | 687 | m_bAuto3D = TRUE; |
468e327a | 688 | m_hMq = 0; |
3958ae62 SN |
689 | m_maxSocketHandles = 0; |
690 | m_maxSocketNr = 0; | |
691 | m_sockCallbackInfo = 0; | |
8df85a61 | 692 | } // end of wxApp::wxApp |
d88de032 DW |
693 | |
694 | wxApp::~wxApp() | |
695 | { | |
8df85a61 | 696 | // |
d88de032 | 697 | // Delete command-line args |
8df85a61 | 698 | // |
039bec17 | 699 | #if wxUSE_UNICODE |
8df85a61 DW |
700 | int i; |
701 | ||
d88de032 DW |
702 | for (i = 0; i < argc; i++) |
703 | { | |
704 | delete[] argv[i]; | |
705 | } | |
706 | delete[] argv; | |
039bec17 | 707 | #endif |
8df85a61 | 708 | } // end of wxApp::~wxApp |
0e320a79 DW |
709 | |
710 | bool wxApp::Initialized() | |
711 | { | |
d88de032 DW |
712 | if (GetTopWindow()) |
713 | return TRUE; | |
714 | else | |
715 | return FALSE; | |
8df85a61 | 716 | } // end of wxApp::Initialized |
0e320a79 | 717 | |
9ed0fac8 DW |
718 | // |
719 | // Get and process a message, returning FALSE if WM_QUIT | |
720 | // received (and also set the flag telling the app to exit the main loop) | |
721 | // | |
3958ae62 | 722 | |
d88de032 DW |
723 | bool wxApp::DoMessage() |
724 | { | |
468e327a | 725 | BOOL bRc = ::WinGetMsg(vHabmain, &svCurrentMsg, HWND(NULL), 0, 0); |
9ed0fac8 DW |
726 | |
727 | if (bRc == 0) | |
d88de032 DW |
728 | { |
729 | // got WM_QUIT | |
9ed0fac8 | 730 | m_bKeepGoing = FALSE; |
d88de032 DW |
731 | return FALSE; |
732 | } | |
9ed0fac8 | 733 | else if (bRc == -1) |
d88de032 DW |
734 | { |
735 | // should never happen, but let's test for it nevertheless | |
736 | wxLogLastError("GetMessage"); | |
737 | } | |
738 | else | |
739 | { | |
740 | #if wxUSE_THREADS | |
9ed0fac8 DW |
741 | wxASSERT_MSG( wxThread::IsMain() |
742 | ,wxT("only the main thread can process Windows messages") | |
743 | ); | |
d88de032 | 744 | |
9ed0fac8 DW |
745 | static bool sbHadGuiLock = TRUE; |
746 | static wxMsgArray svSavedMessages; | |
d88de032 | 747 | |
9ed0fac8 | 748 | // |
8df85a61 | 749 | // If a secondary thread owns is doing GUI calls, save all messages for |
d88de032 DW |
750 | // later processing - we can't process them right now because it will |
751 | // lead to recursive library calls (and we're not reentrant) | |
9ed0fac8 DW |
752 | // |
753 | if (!wxGuiOwnedByMainThread()) | |
d88de032 | 754 | { |
9ed0fac8 | 755 | sbHadGuiLock = FALSE; |
d88de032 | 756 | |
8df85a61 DW |
757 | // |
758 | // Leave out WM_COMMAND messages: too dangerous, sometimes | |
d88de032 | 759 | // the message will be processed twice |
8df85a61 | 760 | // |
d88de032 | 761 | if ( !wxIsWaitingForThread() || |
9ed0fac8 | 762 | svCurrentMsg.msg != WM_COMMAND ) |
d88de032 | 763 | { |
9ed0fac8 | 764 | svSavedMessages.Add(svCurrentMsg); |
d88de032 | 765 | } |
d88de032 DW |
766 | return TRUE; |
767 | } | |
768 | else | |
769 | { | |
9ed0fac8 | 770 | // |
8df85a61 | 771 | // Have we just regained the GUI lock? if so, post all of the saved |
d88de032 DW |
772 | // messages |
773 | // | |
9ed0fac8 | 774 | if (!sbHadGuiLock ) |
d88de032 | 775 | { |
9ed0fac8 | 776 | sbHadGuiLock = TRUE; |
d88de032 | 777 | |
9ed0fac8 DW |
778 | size_t nCount = svSavedMessages.Count(); |
779 | ||
780 | for (size_t n = 0; n < nCount; n++) | |
d88de032 | 781 | { |
9ed0fac8 | 782 | QMSG vMsg = svSavedMessages[n]; |
d88de032 | 783 | |
ed2b77fc | 784 | DoMessage((WXMSG*)&vMsg); |
d88de032 | 785 | } |
9ed0fac8 | 786 | svSavedMessages.Empty(); |
d88de032 DW |
787 | } |
788 | } | |
d88de032 DW |
789 | #endif // wxUSE_THREADS |
790 | ||
3febf684 | 791 | // |
d88de032 | 792 | // Process the message |
3febf684 DW |
793 | // |
794 | DoMessage((WXMSG *)&svCurrentMsg); | |
d88de032 | 795 | } |
d88de032 | 796 | return TRUE; |
8df85a61 | 797 | } // end of wxApp::DoMessage |
d88de032 | 798 | |
3febf684 DW |
799 | void wxApp::DoMessage( |
800 | WXMSG* pMsg | |
801 | ) | |
802 | { | |
803 | if (!ProcessMessage((WXMSG *)&svCurrentMsg)) | |
804 | { | |
805 | ::WinDispatchMsg(vHabmain, (PQMSG)&svCurrentMsg); | |
806 | } | |
807 | } // end of wxApp::DoMessage | |
808 | ||
9ed0fac8 DW |
809 | ////////////////////////////////////////////////////////////////////////////// |
810 | // | |
811 | // Keep trying to process messages until WM_QUIT | |
812 | // received. | |
813 | // | |
814 | // If there are messages to be processed, they will all be | |
815 | // processed and OnIdle will not be called. | |
816 | // When there are no more messages, OnIdle is called. | |
817 | // If OnIdle requests more time, | |
818 | // it will be repeatedly called so long as there are no pending messages. | |
819 | // A 'feature' of this is that once OnIdle has decided that no more processing | |
820 | // is required, then it won't get processing time until further messages | |
821 | // are processed (it'll sit in DoMessage). | |
822 | // | |
823 | ////////////////////////////////////////////////////////////////////////////// | |
0e320a79 DW |
824 | int wxApp::MainLoop() |
825 | { | |
9ed0fac8 | 826 | m_bKeepGoing = TRUE; |
d88de032 | 827 | |
9ed0fac8 | 828 | while (m_bKeepGoing) |
d88de032 DW |
829 | { |
830 | #if wxUSE_THREADS | |
831 | wxMutexGuiLeaveOrEnter(); | |
832 | #endif // wxUSE_THREADS | |
3958ae62 | 833 | while (!Pending() && ProcessIdle()) |
5b3ed311 | 834 | { |
3958ae62 SN |
835 | HandleSockets(); |
836 | wxUsleep(10000); | |
5b3ed311 | 837 | } |
3958ae62 SN |
838 | HandleSockets(); |
839 | if (Pending()) | |
840 | DoMessage(); | |
841 | else | |
842 | wxUsleep(10000); | |
843 | ||
d88de032 | 844 | } |
9ed0fac8 | 845 | return (int)svCurrentMsg.mp1; |
8df85a61 | 846 | } // end of wxApp::MainLoop |
0e320a79 | 847 | |
9ed0fac8 | 848 | // |
0e320a79 | 849 | // Returns TRUE if more time is needed. |
9ed0fac8 | 850 | // |
0e320a79 DW |
851 | bool wxApp::ProcessIdle() |
852 | { | |
9ed0fac8 | 853 | wxIdleEvent vEvent; |
0e320a79 | 854 | |
9ed0fac8 DW |
855 | vEvent.SetEventObject(this); |
856 | ProcessEvent(vEvent); | |
857 | return vEvent.MoreRequested(); | |
8df85a61 | 858 | } // end of wxApp::ProcessIdle |
d88de032 | 859 | |
0e320a79 DW |
860 | void wxApp::ExitMainLoop() |
861 | { | |
9ed0fac8 | 862 | m_bKeepGoing = FALSE; |
0e320a79 DW |
863 | } |
864 | ||
0e320a79 DW |
865 | bool wxApp::Pending() |
866 | { | |
43543d98 | 867 | return (::WinPeekMsg(vHabmain, (PQMSG)&svCurrentMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) != 0); |
0e320a79 DW |
868 | } |
869 | ||
0e320a79 DW |
870 | void wxApp::Dispatch() |
871 | { | |
d88de032 | 872 | DoMessage(); |
0e320a79 DW |
873 | } |
874 | ||
9ed0fac8 DW |
875 | ////////////////////////////////////////////////////////////////////////////// |
876 | // | |
877 | // Give all windows a chance to preprocess | |
878 | // the message. Some may have accelerator tables, or have | |
879 | // MDI complications. | |
880 | // | |
881 | ////////////////////////////////////////////////////////////////////////////// | |
882 | bool wxApp::ProcessMessage( | |
883 | WXMSG* pWxmsg | |
884 | ) | |
0e320a79 | 885 | { |
f6bcfd97 BP |
886 | QMSG* pMsg = (PQMSG)pWxmsg; |
887 | HWND hWnd = pMsg->hwnd; | |
9ed0fac8 DW |
888 | wxWindow* pWndThis = wxFindWinFromHandle((WXHWND)hWnd); |
889 | wxWindow* pWnd; | |
d88de032 | 890 | |
05a8bfed | 891 | // |
e58dab20 | 892 | // Pass non-system timer messages to the wxTimerProc |
05a8bfed | 893 | // |
e58dab20 DW |
894 | if (pMsg->msg == WM_TIMER && |
895 | (SHORT1FROMMP(pMsg->mp1) != TID_CURSOR && | |
896 | SHORT1FROMMP(pMsg->mp1) != TID_FLASHWINDOW && | |
b3260bce DW |
897 | SHORT1FROMMP(pMsg->mp1) != TID_SCROLL && |
898 | SHORT1FROMMP(pMsg->mp1) != 0x0000 | |
e58dab20 | 899 | )) |
496fd9fb | 900 | wxTimerProc(NULL, 0, (int)pMsg->mp1, 0); |
05a8bfed | 901 | |
54ffa107 DW |
902 | // |
903 | // Allow the window to prevent certain messages from being | |
904 | // translated/processed (this is currently used by wxTextCtrl to always | |
905 | // grab Ctrl-C/V/X, even if they are also accelerators in some parent) | |
906 | // | |
907 | if (pWndThis && !pWndThis->OS2ShouldPreProcessMessage(pWxmsg)) | |
908 | { | |
909 | return FALSE; | |
910 | } | |
911 | ||
9ed0fac8 | 912 | // |
8df85a61 | 913 | // For some composite controls (like a combobox), wndThis might be NULL |
d88de032 DW |
914 | // because the subcontrol is not a wxWindow, but only the control itself |
915 | // is - try to catch this case | |
9ed0fac8 DW |
916 | // |
917 | while (hWnd && !pWndThis) | |
d88de032 | 918 | { |
9ed0fac8 DW |
919 | hWnd = ::WinQueryWindow(hWnd, QW_PARENT); |
920 | pWndThis = wxFindWinFromHandle((WXHWND)hWnd); | |
d88de032 | 921 | } |
0e320a79 | 922 | |
f6bcfd97 BP |
923 | // |
924 | // Try translations first; find the youngest window with | |
f09d8a3b DW |
925 | // a translation table. OS/2 has case sensative accels, so |
926 | // this block, coded by BK, removes that and helps make them | |
927 | // case insensative. | |
f6bcfd97 | 928 | // |
f09d8a3b | 929 | if(pMsg->msg == WM_CHAR) |
f6bcfd97 | 930 | { |
f09d8a3b DW |
931 | PBYTE pChmsg = (PBYTE)&(pMsg->msg); |
932 | USHORT uSch = CHARMSG(pChmsg)->chr; | |
933 | bool bRc; | |
934 | ||
935 | // | |
936 | // Do not process keyup events | |
937 | // | |
938 | if(!(CHARMSG(pChmsg)->fs & KC_KEYUP)) | |
939 | { | |
940 | if((CHARMSG(pChmsg)->fs & (KC_ALT | KC_CTRL)) && CHARMSG(pChmsg)->chr != 0) | |
f1f22049 | 941 | CHARMSG(pChmsg)->chr = (USHORT)wxToupper((UCHAR)uSch); |
f09d8a3b | 942 | |
7e99520b | 943 | |
5b3ed311 DW |
944 | for(pWnd = pWndThis; pWnd; pWnd = pWnd->GetParent() ) |
945 | { | |
946 | if((bRc = pWnd->OS2TranslateMessage(pWxmsg)) == TRUE) | |
947 | break; | |
948 | } | |
7e99520b | 949 | |
f09d8a3b DW |
950 | if(!bRc) // untranslated, should restore original value |
951 | CHARMSG(pChmsg)->chr = uSch; | |
952 | } | |
f6bcfd97 | 953 | } |
8df85a61 | 954 | // |
d88de032 | 955 | // Anyone for a non-translation message? Try youngest descendants first. |
8df85a61 | 956 | // |
e604d44b DW |
957 | // for (pWnd = pWndThis; pWnd; pWnd = pWnd->GetParent()) |
958 | // { | |
959 | // if (pWnd->OS2ProcessMessage(pWxmsg)) | |
960 | // return TRUE; | |
961 | // } | |
d88de032 | 962 | return FALSE; |
8df85a61 | 963 | } // end of wxApp::ProcessMessage |
0e320a79 | 964 | |
9ed0fac8 DW |
965 | void wxApp::OnIdle( |
966 | wxIdleEvent& rEvent | |
967 | ) | |
d88de032 | 968 | { |
9ed0fac8 | 969 | static bool sbInOnIdle = FALSE; |
d88de032 | 970 | |
9ed0fac8 | 971 | // |
d88de032 | 972 | // Avoid recursion (via ProcessEvent default case) |
9ed0fac8 | 973 | // |
5b3ed311 | 974 | if (sbInOnIdle) |
d88de032 | 975 | return; |
0e320a79 | 976 | |
9ed0fac8 | 977 | sbInOnIdle = TRUE; |
0e320a79 | 978 | |
8df85a61 DW |
979 | // |
980 | // If there are pending events, we must process them: pending events | |
981 | // are either events to the threads other than main or events posted | |
982 | // with wxPostEvent() functions | |
983 | // | |
984 | ProcessPendingEvents(); | |
985 | ||
9ed0fac8 | 986 | // |
d88de032 | 987 | // 'Garbage' collection of windows deleted with Close(). |
9ed0fac8 | 988 | // |
d88de032 | 989 | DeletePendingObjects(); |
0e320a79 | 990 | |
d88de032 | 991 | #if wxUSE_LOG |
8df85a61 DW |
992 | // |
993 | // Flush the logged messages if any | |
994 | // | |
995 | wxLog::FlushActive(); | |
d88de032 | 996 | #endif // wxUSE_LOG |
0e320a79 | 997 | |
893758d5 DW |
998 | #if wxUSE_DC_CACHEING |
999 | // automated DC cache management: clear the cached DCs and bitmap | |
1000 | // if it's likely that the app has finished with them, that is, we | |
1001 | // get an idle event and we're not dragging anything. | |
19193a2c KB |
1002 | if (!::WinGetKeyState(HWND_DESKTOP, VK_BUTTON1) && |
1003 | !::WinGetKeyState(HWND_DESKTOP, VK_BUTTON3) && | |
1004 | !::WinGetKeyState(HWND_DESKTOP, VK_BUTTON2)) | |
893758d5 DW |
1005 | wxDC::ClearCache(); |
1006 | #endif // wxUSE_DC_CACHEING | |
1007 | ||
8df85a61 | 1008 | // |
d88de032 | 1009 | // Send OnIdle events to all windows |
8df85a61 | 1010 | // |
9ed0fac8 | 1011 | if (SendIdleEvents()) |
d88de032 | 1012 | { |
9ed0fac8 | 1013 | // |
d88de032 DW |
1014 | // SendIdleEvents() returns TRUE if at least one window requested more |
1015 | // idle events | |
9ed0fac8 DW |
1016 | // |
1017 | rEvent.RequestMore(TRUE); | |
d88de032 | 1018 | } |
9ed0fac8 | 1019 | sbInOnIdle = FALSE; |
8df85a61 | 1020 | } // end of wxApp::OnIdle |
9779893b | 1021 | |
0e320a79 DW |
1022 | // Send idle event to all top-level windows |
1023 | bool wxApp::SendIdleEvents() | |
1024 | { | |
9ed0fac8 DW |
1025 | bool bNeedMore = FALSE; |
1026 | wxWindowList::Node* pNode = wxTopLevelWindows.GetFirst(); | |
d88de032 | 1027 | |
9ed0fac8 | 1028 | while (pNode) |
d88de032 | 1029 | { |
9ed0fac8 | 1030 | wxWindow* pWin = pNode->GetData(); |
0e320a79 | 1031 | |
9ed0fac8 DW |
1032 | if (SendIdleEvents(pWin)) |
1033 | bNeedMore = TRUE; | |
1034 | pNode = pNode->GetNext(); | |
1035 | } | |
1036 | return bNeedMore; | |
8df85a61 | 1037 | } // end of wxApp::SendIdleEvents |
0e320a79 | 1038 | |
9ed0fac8 | 1039 | // |
0e320a79 | 1040 | // Send idle event to window and all subwindows |
9ed0fac8 DW |
1041 | // |
1042 | bool wxApp::SendIdleEvents( | |
1043 | wxWindow* pWin | |
1044 | ) | |
0e320a79 | 1045 | { |
9ed0fac8 DW |
1046 | bool bNeedMore = FALSE; |
1047 | wxIdleEvent vEvent; | |
1048 | ||
1049 | vEvent.SetEventObject(pWin); | |
1050 | pWin->GetEventHandler()->ProcessEvent(vEvent); | |
0e320a79 | 1051 | |
9ed0fac8 DW |
1052 | if (vEvent.MoreRequested()) |
1053 | bNeedMore = TRUE; | |
0e320a79 | 1054 | |
9ed0fac8 | 1055 | wxNode* pNode = pWin->GetChildren().First(); |
0e320a79 | 1056 | |
9ed0fac8 | 1057 | while (pNode) |
d88de032 | 1058 | { |
9ed0fac8 | 1059 | wxWindow* pWin = (wxWindow*) pNode->Data(); |
0e320a79 | 1060 | |
9ed0fac8 DW |
1061 | if (SendIdleEvents(pWin)) |
1062 | bNeedMore = TRUE; | |
1063 | pNode = pNode->Next(); | |
d88de032 | 1064 | } |
9ed0fac8 | 1065 | return bNeedMore; |
8df85a61 | 1066 | } // end of wxApp::SendIdleEvents |
0e320a79 DW |
1067 | |
1068 | void wxApp::DeletePendingObjects() | |
1069 | { | |
9ed0fac8 DW |
1070 | wxNode* pNode = wxPendingDelete.First(); |
1071 | ||
1072 | while (pNode) | |
d88de032 | 1073 | { |
9ed0fac8 | 1074 | wxObject* pObj = (wxObject *)pNode->Data(); |
77cd51c3 | 1075 | |
9ed0fac8 | 1076 | delete pObj; |
0e320a79 | 1077 | |
9ed0fac8 DW |
1078 | if (wxPendingDelete.Member(pObj)) |
1079 | delete pNode; | |
0e320a79 | 1080 | |
9ed0fac8 | 1081 | // |
d88de032 DW |
1082 | // Deleting one object may have deleted other pending |
1083 | // objects, so start from beginning of list again. | |
9ed0fac8 DW |
1084 | // |
1085 | pNode = wxPendingDelete.First(); | |
d88de032 | 1086 | } |
8df85a61 | 1087 | } // end of wxApp::DeletePendingObjects |
0e320a79 | 1088 | |
9ed0fac8 DW |
1089 | void wxApp::OnEndSession( |
1090 | wxCloseEvent& WXUNUSED(rEvent)) | |
0e320a79 | 1091 | { |
d88de032 DW |
1092 | if (GetTopWindow()) |
1093 | GetTopWindow()->Close(TRUE); | |
8df85a61 | 1094 | } // end of wxApp::OnEndSession |
0e320a79 | 1095 | |
9ed0fac8 | 1096 | // |
d88de032 DW |
1097 | // Default behaviour: close the application with prompts. The |
1098 | // user can veto the close, and therefore the end session. | |
9ed0fac8 DW |
1099 | // |
1100 | void wxApp::OnQueryEndSession( | |
1101 | wxCloseEvent& rEvent | |
1102 | ) | |
0e320a79 | 1103 | { |
d88de032 DW |
1104 | if (GetTopWindow()) |
1105 | { | |
9ed0fac8 DW |
1106 | if (!GetTopWindow()->Close(!rEvent.CanVeto())) |
1107 | rEvent.Veto(TRUE); | |
d88de032 | 1108 | } |
8df85a61 | 1109 | } // end of wxApp::OnQueryEndSession |
0e320a79 DW |
1110 | |
1111 | void wxExit() | |
1112 | { | |
d88de032 DW |
1113 | wxLogError(_("Fatal error: exiting")); |
1114 | ||
1115 | wxApp::CleanUp(); | |
ee453a16 | 1116 | } // end of wxExit |
0e320a79 | 1117 | |
8df85a61 | 1118 | // |
d88de032 | 1119 | // Yield to incoming messages |
8df85a61 | 1120 | // |
8461e4c2 | 1121 | bool wxApp::Yield(bool onlyIfNeeded) |
0e320a79 | 1122 | { |
8461e4c2 VZ |
1123 | static bool s_inYield = FALSE; |
1124 | ||
1125 | if ( s_inYield ) | |
1126 | { | |
1127 | if ( !onlyIfNeeded ) | |
1128 | { | |
1129 | wxFAIL_MSG( _T("wxYield() called recursively") ); | |
1130 | } | |
1131 | ||
1132 | return FALSE; | |
1133 | } | |
1134 | ||
9dea36ef | 1135 | HAB vHab = 0; |
dde11e60 | 1136 | QMSG vMsg; |
ee453a16 | 1137 | |
8df85a61 DW |
1138 | // |
1139 | // Disable log flushing from here because a call to wxYield() shouldn't | |
1140 | // normally result in message boxes popping up &c | |
1141 | // | |
1142 | wxLog::Suspend(); | |
1143 | ||
8461e4c2 | 1144 | s_inYield = TRUE; |
8b63ae37 | 1145 | |
8df85a61 | 1146 | // |
d88de032 DW |
1147 | // We want to go back to the main message loop |
1148 | // if we see a WM_QUIT. (?) | |
8df85a61 | 1149 | // |
dde11e60 | 1150 | while (::WinPeekMsg(vHab, &vMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) && vMsg.msg != WM_QUIT) |
d88de032 | 1151 | { |
8df85a61 DW |
1152 | #if wxUSE_THREADS |
1153 | wxMutexGuiLeaveOrEnter(); | |
1154 | #endif // wxUSE_THREADS | |
dde11e60 | 1155 | if (!wxTheApp->DoMessage()) |
d88de032 DW |
1156 | break; |
1157 | } | |
8df85a61 | 1158 | // |
d88de032 | 1159 | // If they are pending events, we must process them. |
8df85a61 DW |
1160 | // |
1161 | if (wxTheApp) | |
1162 | wxTheApp->ProcessPendingEvents(); | |
1163 | ||
1164 | // | |
1165 | // Let the logs be flashed again | |
1166 | // | |
1167 | wxLog::Resume(); | |
8461e4c2 | 1168 | s_inYield = FALSE; |
d88de032 | 1169 | return TRUE; |
8df85a61 | 1170 | } // end of wxYield |
d88de032 | 1171 | |
3958ae62 SN |
1172 | int wxApp::AddSocketHandler(int handle, int mask, |
1173 | void (*callback)(void*), void * gsock) | |
1174 | { | |
1175 | int find; | |
1176 | struct GsocketCallbackInfo | |
1177 | *CallbackInfo = (struct GsocketCallbackInfo *)m_sockCallbackInfo; | |
1178 | ||
1179 | for (find = 0; find < m_maxSocketHandles; find++) | |
1180 | if (CallbackInfo[find].handle == -1) | |
1181 | break; | |
1182 | if (find == m_maxSocketHandles) | |
1183 | { | |
1184 | // Allocate new memory | |
1185 | m_sockCallbackInfo = realloc(m_sockCallbackInfo, | |
1186 | (m_maxSocketHandles+=10)* | |
1187 | sizeof(struct GsocketCallbackInfo)); | |
1188 | CallbackInfo = (struct GsocketCallbackInfo *)m_sockCallbackInfo; | |
1189 | for (find = m_maxSocketHandles - 10; find < m_maxSocketHandles; find++) | |
1190 | CallbackInfo[find].handle = -1; | |
1191 | find = m_maxSocketHandles - 10; | |
1192 | } | |
1193 | CallbackInfo[find].proc = callback; | |
1194 | CallbackInfo[find].type = mask; | |
1195 | CallbackInfo[find].handle = handle; | |
1196 | CallbackInfo[find].gsock = gsock; | |
1197 | if (mask & wxSockReadMask) | |
1198 | FD_SET(handle, &m_readfds); | |
1199 | if (mask & wxSockWriteMask) | |
1200 | FD_SET(handle, &m_writefds); | |
1201 | if (handle >= m_maxSocketNr) | |
1202 | m_maxSocketNr = handle + 1; | |
1203 | return find; | |
1204 | } | |
1205 | ||
1206 | void wxApp::RemoveSocketHandler(int handle) | |
1207 | { | |
1208 | struct GsocketCallbackInfo | |
1209 | *CallbackInfo = (struct GsocketCallbackInfo *)m_sockCallbackInfo; | |
1210 | if (handle < m_maxSocketHandles) | |
1211 | { | |
1212 | if (CallbackInfo[handle].type & wxSockReadMask) | |
1213 | FD_CLR(CallbackInfo[handle].handle, &m_readfds); | |
1214 | if (CallbackInfo[handle].type & wxSockWriteMask) | |
1215 | FD_CLR(CallbackInfo[handle].handle, &m_writefds); | |
1216 | CallbackInfo[handle].handle = -1; | |
1217 | } | |
1218 | } | |
1219 | ||
8df85a61 DW |
1220 | //----------------------------------------------------------------------------- |
1221 | // wxWakeUpIdle | |
1222 | //----------------------------------------------------------------------------- | |
1223 | ||
1224 | void wxWakeUpIdle() | |
1225 | { | |
1226 | // | |
1227 | // Send the top window a dummy message so idle handler processing will | |
1228 | // start up again. Doing it this way ensures that the idle handler | |
1229 | // wakes up in the right thread (see also wxWakeUpMainThread() which does | |
1230 | // the same for the main app thread only) | |
1231 | // | |
1232 | wxWindow* pTopWindow = wxTheApp->GetTopWindow(); | |
1233 | ||
1234 | if (pTopWindow) | |
1235 | { | |
1236 | if ( !::WinPostMsg(GetHwndOf(pTopWindow), WM_NULL, (MPARAM)0, (MPARAM)0)) | |
1237 | { | |
1238 | // | |
1239 | // Should never happen | |
1240 | // | |
1241 | wxLogLastError("PostMessage(WM_NULL)"); | |
1242 | } | |
1243 | } | |
1244 | } // end of wxWakeUpIdle | |
d88de032 | 1245 | |
76990f63 | 1246 | HAB wxGetInstance() |
d88de032 | 1247 | { |
76990f63 | 1248 | return vHabmain; |
d88de032 DW |
1249 | } |
1250 | ||
76990f63 DW |
1251 | void wxSetInstance( |
1252 | HAB vHab | |
1253 | ) | |
d88de032 | 1254 | { |
76990f63 | 1255 | vHabmain = vHab; |
0e320a79 DW |
1256 | } |
1257 |