]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: app.cpp | |
3 | // Purpose: wxApp | |
4 | // Author: David Webster | |
5 | // Modified by: | |
6 | // Created: 10/13/99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) David Webster | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "app.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/frame.h" | |
21 | #include "wx/app.h" | |
22 | #include "wx/utils.h" | |
23 | #include "wx/gdicmn.h" | |
24 | #include "wx/pen.h" | |
25 | #include "wx/brush.h" | |
26 | #include "wx/cursor.h" | |
27 | #include "wx/icon.h" | |
28 | #include "wx/palette.h" | |
29 | #include "wx/dc.h" | |
30 | #include "wx/dialog.h" | |
31 | #include "wx/msgdlg.h" | |
32 | #include "wx/intl.h" | |
33 | #include "wx/dynarray.h" | |
34 | #include "wx/wxchar.h" | |
35 | #include "wx/icon.h" | |
36 | #include "wx/timer.h" | |
37 | #endif | |
38 | ||
39 | #include "wx/log.h" | |
40 | #include "wx/module.h" | |
41 | ||
42 | #include "wx/os2/private.h" | |
43 | ||
44 | #ifdef __EMX__ | |
45 | ||
46 | #include <sys/ioctl.h> | |
47 | #include <sys/select.h> | |
48 | ||
49 | #else | |
50 | ||
51 | #include <nerrno.h> | |
52 | #include <sys/ioctl.h> | |
53 | #include <sys/select.h> | |
54 | #include <sys/time.h> | |
55 | ||
56 | #endif // | |
57 | ||
58 | #ifndef __EMX__ | |
59 | ||
60 | #define select(a,b,c,d,e) bsdselect(a,b,c,d,e) | |
61 | extern "C" int _System bsdselect(int, | |
62 | struct fd_set *, | |
63 | struct fd_set *, | |
64 | struct fd_set *, | |
65 | struct timeval *); | |
66 | #endif | |
67 | ||
68 | #if wxUSE_THREADS | |
69 | #include "wx/thread.h" | |
70 | ||
71 | // define the array of QMSG strutures | |
72 | WX_DECLARE_OBJARRAY(QMSG, wxMsgArray); | |
73 | ||
74 | #include "wx/arrimpl.cpp" | |
75 | ||
76 | WX_DEFINE_OBJARRAY(wxMsgArray); | |
77 | #endif // wxUSE_THREADS | |
78 | ||
79 | #if wxUSE_TOOLTIPS | |
80 | #include "wx/tooltip.h" | |
81 | #endif // wxUSE_TOOLTIPS | |
82 | ||
83 | #include <string.h> | |
84 | #include <ctype.h> | |
85 | ||
86 | // --------------------------------------------------------------------------- | |
87 | // global variables | |
88 | // --------------------------------------------------------------------------- | |
89 | ||
90 | extern wxChar* wxBuffer; | |
91 | extern wxList* wxWinHandleList; | |
92 | extern wxList WXDLLEXPORT wxPendingDelete; | |
93 | extern wxCursor* g_globalCursor; | |
94 | ||
95 | HAB vHabmain = NULLHANDLE; | |
96 | QMSG svCurrentMsg; | |
97 | ||
98 | ||
99 | HICON wxSTD_FRAME_ICON = (HICON) NULL; | |
100 | HICON wxSTD_MDICHILDFRAME_ICON = (HICON) NULL; | |
101 | HICON wxSTD_MDIPARENTFRAME_ICON = (HICON) NULL; | |
102 | ||
103 | HICON wxDEFAULT_FRAME_ICON = (HICON) NULL; | |
104 | HICON wxDEFAULT_MDICHILDFRAME_ICON = (HICON) NULL; | |
105 | HICON wxDEFAULT_MDIPARENTFRAME_ICON = (HICON) NULL; | |
106 | ||
107 | HBRUSH wxDisableButtonBrush = (HBRUSH) 0; | |
108 | ||
109 | MRESULT EXPENTRY wxWndProc( HWND hWnd,ULONG message,MPARAM mp1,MPARAM mp2); | |
110 | MRESULT EXPENTRY wxFrameWndProc( HWND hWnd,ULONG message,MPARAM mp1,MPARAM mp2); | |
111 | ||
112 | // =========================================================================== | |
113 | // implementation | |
114 | // =========================================================================== | |
115 | ||
116 | // --------------------------------------------------------------------------- | |
117 | // helper struct and functions for socket handling | |
118 | // --------------------------------------------------------------------------- | |
119 | ||
120 | struct GsocketCallbackInfo{ | |
121 | void (*proc)(void *); | |
122 | int type; | |
123 | int handle; | |
124 | void* gsock; | |
125 | }; | |
126 | ||
127 | // These defines and wrapper functions are used here and in gsockpm.c | |
128 | #define wxSockReadMask 0x01 | |
129 | #define wxSockWriteMask 0x02 | |
130 | ||
131 | #ifdef __EMX__ | |
132 | extern "C" | |
133 | int wxAppAddSocketHandler(int handle, int mask, | |
134 | void (*callback)(void*), void * gsock) | |
135 | { | |
136 | return wxTheApp->AddSocketHandler(handle, mask, callback, gsock); | |
137 | } | |
138 | extern "C" | |
139 | void wxAppRemoveSocketHandler(int handle) | |
140 | { | |
141 | wxTheApp->RemoveSocketHandler(handle); | |
142 | } | |
143 | #else | |
144 | // Linkage mode problems using callbacks with extern C in a .cpp module | |
145 | int wxAppAddSocketHandler(int handle, int mask, | |
146 | void (*callback)(void*), void * gsock) | |
147 | { | |
148 | return wxTheApp->AddSocketHandler(handle, mask, callback, gsock); | |
149 | } | |
150 | void wxAppRemoveSocketHandler(int handle) | |
151 | { | |
152 | wxTheApp->RemoveSocketHandler(handle); | |
153 | } | |
154 | #endif | |
155 | ||
156 | void wxApp::HandleSockets() | |
157 | { | |
158 | bool pendingEvent = FALSE; | |
159 | ||
160 | // Check whether it's time for Gsocket operation | |
161 | if (m_maxSocketHandles > 0 && m_maxSocketNr > 0) | |
162 | { | |
163 | fd_set readfds = m_readfds; | |
164 | fd_set writefds = m_writefds; | |
165 | struct timeval timeout; | |
166 | int i; | |
167 | struct GsocketCallbackInfo | |
168 | *CallbackInfo = (struct GsocketCallbackInfo *)m_sockCallbackInfo; | |
169 | int r = 0; | |
170 | timeout.tv_sec = 0; | |
171 | timeout.tv_usec = 0; | |
172 | if ( select(m_maxSocketNr, &readfds, &writefds, 0, &timeout) > 0) | |
173 | { | |
174 | for (i = m_lastUsedHandle + 1; i != m_lastUsedHandle; i++) | |
175 | { | |
176 | if (i == m_maxSocketNr) | |
177 | i = 0; | |
178 | if (FD_ISSET(i, &readfds)) | |
179 | { | |
180 | int r; | |
181 | for (r = 0; r < m_maxSocketHandles; r++){ | |
182 | if(CallbackInfo[r].handle == i && | |
183 | CallbackInfo[r].type == wxSockReadMask) | |
184 | break; | |
185 | } | |
186 | if (r < m_maxSocketHandles) | |
187 | { | |
188 | CallbackInfo[r].proc(CallbackInfo[r].gsock); | |
189 | pendingEvent = TRUE; | |
190 | wxYield(); | |
191 | } | |
192 | } | |
193 | if (FD_ISSET(i, &writefds)) | |
194 | { | |
195 | int r; | |
196 | for (r = 0; r < m_maxSocketHandles; r++) | |
197 | if(CallbackInfo[r].handle == i && | |
198 | CallbackInfo[r].type == wxSockWriteMask) | |
199 | break; | |
200 | if (r < m_maxSocketHandles) | |
201 | { | |
202 | CallbackInfo[r].proc(CallbackInfo[r].gsock); | |
203 | pendingEvent = TRUE; | |
204 | wxYield(); | |
205 | } | |
206 | } | |
207 | } | |
208 | m_lastUsedHandle = i; | |
209 | } | |
210 | if (pendingEvent) | |
211 | wxYield(); | |
212 | } | |
213 | } | |
214 | // --------------------------------------------------------------------------- | |
215 | // wxApp | |
216 | // --------------------------------------------------------------------------- | |
217 | ||
218 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) | |
219 | ||
220 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) | |
221 | EVT_IDLE(wxApp::OnIdle) | |
222 | EVT_END_SESSION(wxApp::OnEndSession) | |
223 | EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession) | |
224 | END_EVENT_TABLE() | |
225 | ||
226 | // | |
227 | // Initialize | |
228 | // | |
229 | bool wxApp::Initialize(int& argc, wxChar **argv) | |
230 | { | |
231 | if ( !wxAppBase::Initialize(argc, argv) ) | |
232 | return false; | |
233 | ||
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 | ||
256 | // | |
257 | // OS2 has to have an anchorblock | |
258 | // | |
259 | vHabmain = WinInitialize(0); | |
260 | ||
261 | if (!vHabmain) | |
262 | { | |
263 | // TODO: at least give some error message here... | |
264 | wxAppBase::CleanUp(); | |
265 | ||
266 | return FALSE; | |
267 | } | |
268 | ||
269 | wxBuffer = new wxChar[1500]; // FIXME; why? | |
270 | ||
271 | // Some people may wish to use this, but | |
272 | // probably it shouldn't be here by default. | |
273 | #ifdef __WXDEBUG__ | |
274 | // wxRedirectIOToConsole(); | |
275 | #endif | |
276 | ||
277 | wxWinHandleList = new wxList(wxKEY_INTEGER); | |
278 | ||
279 | // This is to foil optimizations in Visual C++ that throw out dummy.obj. | |
280 | // PLEASE DO NOT ALTER THIS. | |
281 | #if !defined(WXMAKINGDLL) && defined(__VISAGECPP__) | |
282 | extern char wxDummyChar; | |
283 | if (wxDummyChar) wxDummyChar++; | |
284 | #endif | |
285 | ||
286 | // wxSetKeyboardHook(TRUE); | |
287 | ||
288 | RegisterWindowClasses(vHab); | |
289 | ||
290 | return TRUE; | |
291 | } // end of wxApp::Initialize | |
292 | ||
293 | const char* CANTREGISTERCLASS = " Can't register Class "; | |
294 | // --------------------------------------------------------------------------- | |
295 | // RegisterWindowClasses | |
296 | // --------------------------------------------------------------------------- | |
297 | ||
298 | bool wxApp::RegisterWindowClasses( | |
299 | HAB vHab | |
300 | ) | |
301 | { | |
302 | ERRORID vError = 0L; | |
303 | wxString sError; | |
304 | ||
305 | if (!::WinRegisterClass( vHab | |
306 | ,wxFrameClassName | |
307 | ,wxFrameWndProc | |
308 | ,CS_SIZEREDRAW | CS_SYNCPAINT | |
309 | ,sizeof(ULONG) | |
310 | )) | |
311 | { | |
312 | vError = ::WinGetLastError(vHab); | |
313 | sError = wxPMErrorToStr(vError); | |
314 | wxLogLastError(sError); | |
315 | return FALSE; | |
316 | } | |
317 | ||
318 | if (!::WinRegisterClass( vHab | |
319 | ,wxFrameClassNameNoRedraw | |
320 | ,wxWndProc | |
321 | ,0 | |
322 | ,sizeof(ULONG) | |
323 | )) | |
324 | { | |
325 | vError = ::WinGetLastError(vHab); | |
326 | sError = wxPMErrorToStr(vError); | |
327 | wxLogLastError(sError); | |
328 | return FALSE; | |
329 | } | |
330 | ||
331 | if (!::WinRegisterClass( vHab | |
332 | ,wxMDIFrameClassName | |
333 | ,wxWndProc | |
334 | ,CS_SIZEREDRAW | CS_MOVENOTIFY | CS_SYNCPAINT | |
335 | ,sizeof(ULONG) | |
336 | )) | |
337 | { | |
338 | vError = ::WinGetLastError(vHab); | |
339 | sError = wxPMErrorToStr(vError); | |
340 | wxLogLastError(sError); | |
341 | return FALSE; | |
342 | } | |
343 | ||
344 | if (!::WinRegisterClass( vHab | |
345 | ,wxMDIFrameClassNameNoRedraw | |
346 | ,wxWndProc | |
347 | ,0 | |
348 | ,sizeof(ULONG) | |
349 | )) | |
350 | { | |
351 | vError = ::WinGetLastError(vHab); | |
352 | sError = wxPMErrorToStr(vError); | |
353 | wxLogLastError(sError); | |
354 | return FALSE; | |
355 | } | |
356 | ||
357 | if (!::WinRegisterClass( vHab | |
358 | ,wxMDIChildFrameClassName | |
359 | ,wxWndProc | |
360 | ,CS_MOVENOTIFY | CS_SIZEREDRAW | CS_SYNCPAINT | CS_HITTEST | |
361 | ,sizeof(ULONG) | |
362 | )) | |
363 | { | |
364 | vError = ::WinGetLastError(vHab); | |
365 | sError = wxPMErrorToStr(vError); | |
366 | wxLogLastError(sError); | |
367 | return FALSE; | |
368 | } | |
369 | ||
370 | if (!::WinRegisterClass( vHab | |
371 | ,wxMDIChildFrameClassNameNoRedraw | |
372 | ,wxWndProc | |
373 | ,CS_HITTEST | |
374 | ,sizeof(ULONG) | |
375 | )) | |
376 | { | |
377 | vError = ::WinGetLastError(vHab); | |
378 | sError = wxPMErrorToStr(vError); | |
379 | wxLogLastError(sError); | |
380 | return FALSE; | |
381 | } | |
382 | ||
383 | if (!::WinRegisterClass( vHab | |
384 | ,wxPanelClassName | |
385 | ,wxWndProc | |
386 | ,CS_MOVENOTIFY | CS_SIZEREDRAW | CS_HITTEST | CS_SAVEBITS | CS_SYNCPAINT | |
387 | ,sizeof(ULONG) | |
388 | )) | |
389 | { | |
390 | vError = ::WinGetLastError(vHab); | |
391 | sError = wxPMErrorToStr(vError); | |
392 | wxLogLastError(sError); | |
393 | return FALSE; | |
394 | } | |
395 | ||
396 | if (!::WinRegisterClass( vHab | |
397 | ,wxCanvasClassName | |
398 | ,wxWndProc | |
399 | ,CS_SIZEREDRAW | CS_HITTEST | CS_SYNCPAINT | |
400 | ,sizeof(ULONG) | |
401 | )) | |
402 | { | |
403 | vError = ::WinGetLastError(vHab); | |
404 | sError = wxPMErrorToStr(vError); | |
405 | wxLogLastError(sError); | |
406 | return FALSE; | |
407 | } | |
408 | if (!::WinRegisterClass( vHab | |
409 | ,wxCanvasClassNameNR | |
410 | ,wxWndProc | |
411 | ,CS_HITTEST | CS_SYNCPAINT | |
412 | ,sizeof(ULONG) | |
413 | )) | |
414 | { | |
415 | vError = ::WinGetLastError(vHab); | |
416 | sError = wxPMErrorToStr(vError); | |
417 | wxLogLastError(sError); | |
418 | return FALSE; | |
419 | } | |
420 | return TRUE; | |
421 | } // end of wxApp::RegisterWindowClasses | |
422 | ||
423 | // | |
424 | // Cleans up any wxWindows internal structures left lying around | |
425 | // | |
426 | void wxApp::CleanUp() | |
427 | { | |
428 | delete[] wxBuffer; | |
429 | wxBuffer = NULL; | |
430 | ||
431 | // | |
432 | // PM-SPECIFIC CLEANUP | |
433 | // | |
434 | ||
435 | // wxSetKeyboardHook(FALSE); | |
436 | ||
437 | if (wxSTD_FRAME_ICON) | |
438 | ::WinFreeFileIcon(wxSTD_FRAME_ICON); | |
439 | if (wxSTD_MDICHILDFRAME_ICON) | |
440 | ::WinFreeFileIcon(wxSTD_MDICHILDFRAME_ICON); | |
441 | if (wxSTD_MDIPARENTFRAME_ICON) | |
442 | ::WinFreeFileIcon(wxSTD_MDIPARENTFRAME_ICON); | |
443 | ||
444 | if (wxDEFAULT_FRAME_ICON) | |
445 | ::WinFreeFileIcon(wxDEFAULT_FRAME_ICON); | |
446 | if (wxDEFAULT_MDICHILDFRAME_ICON) | |
447 | ::WinFreeFileIcon(wxDEFAULT_MDICHILDFRAME_ICON); | |
448 | if (wxDEFAULT_MDIPARENTFRAME_ICON) | |
449 | ::WinFreeFileIcon(wxDEFAULT_MDIPARENTFRAME_ICON); | |
450 | ||
451 | if ( wxDisableButtonBrush ) | |
452 | { | |
453 | // TODO: ::DeleteObject( wxDisableButtonBrush ); | |
454 | } | |
455 | ||
456 | if (wxWinHandleList) | |
457 | delete wxWinHandleList; | |
458 | ||
459 | delete wxPendingEvents; | |
460 | #if wxUSE_THREADS | |
461 | delete wxPendingEventsLocker; | |
462 | // If we don't do the following, we get an apparent memory leak. | |
463 | ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker(); | |
464 | #endif | |
465 | ||
466 | // Delete Message queue | |
467 | if (wxTheApp->m_hMq) | |
468 | ::WinDestroyMsgQueue(wxTheApp->m_hMq); | |
469 | ||
470 | wxAppBase::CleanUp(); | |
471 | } // end of wxApp::CleanUp | |
472 | ||
473 | bool wxApp::OnInitGui() | |
474 | { | |
475 | ERRORID vError; | |
476 | wxString sError; | |
477 | ||
478 | if (!wxAppBase::OnInitGui()) | |
479 | return FALSE; | |
480 | ||
481 | m_hMq = ::WinCreateMsgQueue(vHabmain, 0); | |
482 | if (!m_hMq) | |
483 | { | |
484 | vError = ::WinGetLastError(vHabmain); | |
485 | sError = wxPMErrorToStr(vError); | |
486 | wxLogDebug(sError); | |
487 | return FALSE; | |
488 | } | |
489 | ||
490 | return TRUE; | |
491 | } // end of wxApp::OnInitGui | |
492 | ||
493 | wxApp::wxApp() | |
494 | { | |
495 | m_topWindow = NULL; | |
496 | wxTheApp = this; | |
497 | ||
498 | argc = 0; | |
499 | argv = NULL; | |
500 | m_nPrintMode = wxPRINT_WINDOWS; | |
501 | m_bAuto3D = TRUE; | |
502 | m_hMq = 0; | |
503 | m_maxSocketHandles = 0; | |
504 | m_maxSocketNr = 0; | |
505 | m_sockCallbackInfo = 0; | |
506 | } // end of wxApp::wxApp | |
507 | ||
508 | wxApp::~wxApp() | |
509 | { | |
510 | // | |
511 | // Delete command-line args | |
512 | // | |
513 | #if wxUSE_UNICODE | |
514 | int i; | |
515 | ||
516 | for (i = 0; i < argc; i++) | |
517 | { | |
518 | delete[] argv[i]; | |
519 | } | |
520 | delete[] argv; | |
521 | #endif | |
522 | } // end of wxApp::~wxApp | |
523 | ||
524 | bool wxApp::Initialized() | |
525 | { | |
526 | if (GetTopWindow()) | |
527 | return TRUE; | |
528 | else | |
529 | return FALSE; | |
530 | } // end of wxApp::Initialized | |
531 | ||
532 | // | |
533 | // Get and process a message, returning FALSE if WM_QUIT | |
534 | // received (and also set the flag telling the app to exit the main loop) | |
535 | // | |
536 | ||
537 | bool wxApp::DoMessage() | |
538 | { | |
539 | BOOL bRc = ::WinGetMsg(vHabmain, &svCurrentMsg, HWND(NULL), 0, 0); | |
540 | ||
541 | if (bRc == 0) | |
542 | { | |
543 | // got WM_QUIT | |
544 | m_bKeepGoing = FALSE; | |
545 | return FALSE; | |
546 | } | |
547 | else if (bRc == -1) | |
548 | { | |
549 | // should never happen, but let's test for it nevertheless | |
550 | wxLogLastError("GetMessage"); | |
551 | } | |
552 | else | |
553 | { | |
554 | #if wxUSE_THREADS | |
555 | wxASSERT_MSG( wxThread::IsMain() | |
556 | ,wxT("only the main thread can process Windows messages") | |
557 | ); | |
558 | ||
559 | static bool sbHadGuiLock = TRUE; | |
560 | static wxMsgArray svSavedMessages; | |
561 | ||
562 | // | |
563 | // If a secondary thread owns is doing GUI calls, save all messages for | |
564 | // later processing - we can't process them right now because it will | |
565 | // lead to recursive library calls (and we're not reentrant) | |
566 | // | |
567 | if (!wxGuiOwnedByMainThread()) | |
568 | { | |
569 | sbHadGuiLock = FALSE; | |
570 | ||
571 | // | |
572 | // Leave out WM_COMMAND messages: too dangerous, sometimes | |
573 | // the message will be processed twice | |
574 | // | |
575 | if ( !wxIsWaitingForThread() || | |
576 | svCurrentMsg.msg != WM_COMMAND ) | |
577 | { | |
578 | svSavedMessages.Add(svCurrentMsg); | |
579 | } | |
580 | return TRUE; | |
581 | } | |
582 | else | |
583 | { | |
584 | // | |
585 | // Have we just regained the GUI lock? if so, post all of the saved | |
586 | // messages | |
587 | // | |
588 | if (!sbHadGuiLock ) | |
589 | { | |
590 | sbHadGuiLock = TRUE; | |
591 | ||
592 | size_t nCount = svSavedMessages.Count(); | |
593 | ||
594 | for (size_t n = 0; n < nCount; n++) | |
595 | { | |
596 | QMSG vMsg = svSavedMessages[n]; | |
597 | ||
598 | DoMessage((WXMSG*)&vMsg); | |
599 | } | |
600 | svSavedMessages.Empty(); | |
601 | } | |
602 | } | |
603 | #endif // wxUSE_THREADS | |
604 | ||
605 | // | |
606 | // Process the message | |
607 | // | |
608 | DoMessage((WXMSG *)&svCurrentMsg); | |
609 | } | |
610 | return TRUE; | |
611 | } // end of wxApp::DoMessage | |
612 | ||
613 | void wxApp::DoMessage( | |
614 | WXMSG* pMsg | |
615 | ) | |
616 | { | |
617 | if (!ProcessMessage((WXMSG *)&svCurrentMsg)) | |
618 | { | |
619 | ::WinDispatchMsg(vHabmain, (PQMSG)&svCurrentMsg); | |
620 | } | |
621 | } // end of wxApp::DoMessage | |
622 | ||
623 | ////////////////////////////////////////////////////////////////////////////// | |
624 | // | |
625 | // Keep trying to process messages until WM_QUIT | |
626 | // received. | |
627 | // | |
628 | // If there are messages to be processed, they will all be | |
629 | // processed and OnIdle will not be called. | |
630 | // When there are no more messages, OnIdle is called. | |
631 | // If OnIdle requests more time, | |
632 | // it will be repeatedly called so long as there are no pending messages. | |
633 | // A 'feature' of this is that once OnIdle has decided that no more processing | |
634 | // is required, then it won't get processing time until further messages | |
635 | // are processed (it'll sit in DoMessage). | |
636 | // | |
637 | ////////////////////////////////////////////////////////////////////////////// | |
638 | int wxApp::MainLoop() | |
639 | { | |
640 | m_bKeepGoing = TRUE; | |
641 | ||
642 | while (m_bKeepGoing) | |
643 | { | |
644 | #if wxUSE_THREADS | |
645 | wxMutexGuiLeaveOrEnter(); | |
646 | #endif // wxUSE_THREADS | |
647 | while (!Pending() && ProcessIdle()) | |
648 | { | |
649 | HandleSockets(); | |
650 | wxUsleep(10000); | |
651 | } | |
652 | HandleSockets(); | |
653 | if (Pending()) | |
654 | DoMessage(); | |
655 | else | |
656 | wxUsleep(10000); | |
657 | ||
658 | } | |
659 | return (int)svCurrentMsg.mp1; | |
660 | } // end of wxApp::MainLoop | |
661 | ||
662 | void wxApp::ExitMainLoop() | |
663 | { | |
664 | ::WinPostMsg(NULL, WM_QUIT, 0, 0); | |
665 | } // end of wxApp::ExitMainLoop | |
666 | ||
667 | bool wxApp::Pending() | |
668 | { | |
669 | return (::WinPeekMsg(vHabmain, (PQMSG)&svCurrentMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) != 0); | |
670 | } // end of wxApp::Pending | |
671 | ||
672 | void wxApp::Dispatch() | |
673 | { | |
674 | DoMessage(); | |
675 | } | |
676 | ||
677 | ////////////////////////////////////////////////////////////////////////////// | |
678 | // | |
679 | // Give all windows a chance to preprocess | |
680 | // the message. Some may have accelerator tables, or have | |
681 | // MDI complications. | |
682 | // | |
683 | ////////////////////////////////////////////////////////////////////////////// | |
684 | bool wxApp::ProcessMessage( | |
685 | WXMSG* pWxmsg | |
686 | ) | |
687 | { | |
688 | QMSG* pMsg = (PQMSG)pWxmsg; | |
689 | HWND hWnd = pMsg->hwnd; | |
690 | wxWindow* pWndThis = wxFindWinFromHandle((WXHWND)hWnd); | |
691 | wxWindow* pWnd; | |
692 | ||
693 | // | |
694 | // Pass non-system timer messages to the wxTimerProc | |
695 | // | |
696 | if (pMsg->msg == WM_TIMER && | |
697 | (SHORT1FROMMP(pMsg->mp1) != TID_CURSOR && | |
698 | SHORT1FROMMP(pMsg->mp1) != TID_FLASHWINDOW && | |
699 | SHORT1FROMMP(pMsg->mp1) != TID_SCROLL && | |
700 | SHORT1FROMMP(pMsg->mp1) != 0x0000 | |
701 | )) | |
702 | wxTimerProc(NULL, 0, (int)pMsg->mp1, 0); | |
703 | ||
704 | // | |
705 | // Allow the window to prevent certain messages from being | |
706 | // translated/processed (this is currently used by wxTextCtrl to always | |
707 | // grab Ctrl-C/V/X, even if they are also accelerators in some parent) | |
708 | // | |
709 | if (pWndThis && !pWndThis->OS2ShouldPreProcessMessage(pWxmsg)) | |
710 | { | |
711 | return FALSE; | |
712 | } | |
713 | ||
714 | // | |
715 | // For some composite controls (like a combobox), wndThis might be NULL | |
716 | // because the subcontrol is not a wxWindow, but only the control itself | |
717 | // is - try to catch this case | |
718 | // | |
719 | while (hWnd && !pWndThis) | |
720 | { | |
721 | hWnd = ::WinQueryWindow(hWnd, QW_PARENT); | |
722 | pWndThis = wxFindWinFromHandle((WXHWND)hWnd); | |
723 | } | |
724 | ||
725 | // | |
726 | // Try translations first; find the youngest window with | |
727 | // a translation table. OS/2 has case sensative accels, so | |
728 | // this block, coded by BK, removes that and helps make them | |
729 | // case insensative. | |
730 | // | |
731 | if(pMsg->msg == WM_CHAR) | |
732 | { | |
733 | PBYTE pChmsg = (PBYTE)&(pMsg->msg); | |
734 | USHORT uSch = CHARMSG(pChmsg)->chr; | |
735 | bool bRc; | |
736 | ||
737 | // | |
738 | // Do not process keyup events | |
739 | // | |
740 | if(!(CHARMSG(pChmsg)->fs & KC_KEYUP)) | |
741 | { | |
742 | if((CHARMSG(pChmsg)->fs & (KC_ALT | KC_CTRL)) && CHARMSG(pChmsg)->chr != 0) | |
743 | CHARMSG(pChmsg)->chr = (USHORT)wxToupper((UCHAR)uSch); | |
744 | ||
745 | ||
746 | for(pWnd = pWndThis; pWnd; pWnd = pWnd->GetParent() ) | |
747 | { | |
748 | if((bRc = pWnd->OS2TranslateMessage(pWxmsg)) == TRUE) | |
749 | break; | |
750 | } | |
751 | ||
752 | if(!bRc) // untranslated, should restore original value | |
753 | CHARMSG(pChmsg)->chr = uSch; | |
754 | } | |
755 | } | |
756 | // | |
757 | // Anyone for a non-translation message? Try youngest descendants first. | |
758 | // | |
759 | // for (pWnd = pWndThis; pWnd; pWnd = pWnd->GetParent()) | |
760 | // { | |
761 | // if (pWnd->OS2ProcessMessage(pWxmsg)) | |
762 | // return TRUE; | |
763 | // } | |
764 | return FALSE; | |
765 | } // end of wxApp::ProcessMessage | |
766 | ||
767 | bool gbInOnIdle = FALSE; | |
768 | ||
769 | void wxApp::OnIdle( | |
770 | wxIdleEvent& rEvent | |
771 | ) | |
772 | { | |
773 | ||
774 | // | |
775 | // Avoid recursion (via ProcessEvent default case) | |
776 | // | |
777 | if (gbInOnIdle) | |
778 | return; | |
779 | ||
780 | gbInOnIdle = TRUE; | |
781 | ||
782 | // | |
783 | // If there are pending events, we must process them: pending events | |
784 | // are either events to the threads other than main or events posted | |
785 | // with wxPostEvent() functions | |
786 | // | |
787 | ProcessPendingEvents(); | |
788 | ||
789 | // | |
790 | // 'Garbage' collection of windows deleted with Close(). | |
791 | // | |
792 | DeletePendingObjects(); | |
793 | ||
794 | #if wxUSE_LOG | |
795 | // | |
796 | // Flush the logged messages if any | |
797 | // | |
798 | wxLog::FlushActive(); | |
799 | #endif // wxUSE_LOG | |
800 | ||
801 | #if wxUSE_DC_CACHEING | |
802 | // automated DC cache management: clear the cached DCs and bitmap | |
803 | // if it's likely that the app has finished with them, that is, we | |
804 | // get an idle event and we're not dragging anything. | |
805 | if (!::WinGetKeyState(HWND_DESKTOP, VK_BUTTON1) && | |
806 | !::WinGetKeyState(HWND_DESKTOP, VK_BUTTON3) && | |
807 | !::WinGetKeyState(HWND_DESKTOP, VK_BUTTON2)) | |
808 | wxDC::ClearCache(); | |
809 | #endif // wxUSE_DC_CACHEING | |
810 | ||
811 | // | |
812 | // Send OnIdle events to all windows | |
813 | // | |
814 | if (SendIdleEvents()) | |
815 | { | |
816 | // | |
817 | // SendIdleEvents() returns TRUE if at least one window requested more | |
818 | // idle events | |
819 | // | |
820 | rEvent.RequestMore(TRUE); | |
821 | } | |
822 | gbInOnIdle = FALSE; | |
823 | } // end of wxApp::OnIdle | |
824 | ||
825 | void wxApp::OnEndSession( | |
826 | wxCloseEvent& WXUNUSED(rEvent)) | |
827 | { | |
828 | if (GetTopWindow()) | |
829 | GetTopWindow()->Close(TRUE); | |
830 | } // end of wxApp::OnEndSession | |
831 | ||
832 | // | |
833 | // Default behaviour: close the application with prompts. The | |
834 | // user can veto the close, and therefore the end session. | |
835 | // | |
836 | void wxApp::OnQueryEndSession( | |
837 | wxCloseEvent& rEvent | |
838 | ) | |
839 | { | |
840 | if (GetTopWindow()) | |
841 | { | |
842 | if (!GetTopWindow()->Close(!rEvent.CanVeto())) | |
843 | rEvent.Veto(TRUE); | |
844 | } | |
845 | } // end of wxApp::OnQueryEndSession | |
846 | ||
847 | void wxApp::Exit() | |
848 | { | |
849 | wxApp::CleanUp(); | |
850 | ||
851 | // VZ: must really exit somehow, insert appropriate OS/2 syscall (FIXME) | |
852 | wxAppConsole::Exit(); | |
853 | } // end of wxExit | |
854 | ||
855 | // | |
856 | // Yield to incoming messages | |
857 | // | |
858 | bool wxApp::Yield(bool onlyIfNeeded) | |
859 | { | |
860 | static bool s_inYield = FALSE; | |
861 | ||
862 | if ( s_inYield ) | |
863 | { | |
864 | if ( !onlyIfNeeded ) | |
865 | { | |
866 | wxFAIL_MSG( _T("wxYield() called recursively") ); | |
867 | } | |
868 | ||
869 | return FALSE; | |
870 | } | |
871 | ||
872 | HAB vHab = 0; | |
873 | QMSG vMsg; | |
874 | ||
875 | // | |
876 | // Disable log flushing from here because a call to wxYield() shouldn't | |
877 | // normally result in message boxes popping up &c | |
878 | // | |
879 | wxLog::Suspend(); | |
880 | ||
881 | s_inYield = TRUE; | |
882 | ||
883 | // | |
884 | // We want to go back to the main message loop | |
885 | // if we see a WM_QUIT. (?) | |
886 | // | |
887 | while (::WinPeekMsg(vHab, &vMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) && vMsg.msg != WM_QUIT) | |
888 | { | |
889 | #if wxUSE_THREADS | |
890 | wxMutexGuiLeaveOrEnter(); | |
891 | #endif // wxUSE_THREADS | |
892 | if (!wxTheApp->DoMessage()) | |
893 | break; | |
894 | } | |
895 | // | |
896 | // If they are pending events, we must process them. | |
897 | // | |
898 | if (wxTheApp) | |
899 | wxTheApp->ProcessPendingEvents(); | |
900 | ||
901 | // | |
902 | // Let the logs be flashed again | |
903 | // | |
904 | wxLog::Resume(); | |
905 | s_inYield = FALSE; | |
906 | return TRUE; | |
907 | } // end of wxYield | |
908 | ||
909 | int wxApp::AddSocketHandler(int handle, int mask, | |
910 | void (*callback)(void*), void * gsock) | |
911 | { | |
912 | int find; | |
913 | struct GsocketCallbackInfo | |
914 | *CallbackInfo = (struct GsocketCallbackInfo *)m_sockCallbackInfo; | |
915 | ||
916 | for (find = 0; find < m_maxSocketHandles; find++) | |
917 | if (CallbackInfo[find].handle == -1) | |
918 | break; | |
919 | if (find == m_maxSocketHandles) | |
920 | { | |
921 | // Allocate new memory | |
922 | m_sockCallbackInfo = realloc(m_sockCallbackInfo, | |
923 | (m_maxSocketHandles+=10)* | |
924 | sizeof(struct GsocketCallbackInfo)); | |
925 | CallbackInfo = (struct GsocketCallbackInfo *)m_sockCallbackInfo; | |
926 | for (find = m_maxSocketHandles - 10; find < m_maxSocketHandles; find++) | |
927 | CallbackInfo[find].handle = -1; | |
928 | find = m_maxSocketHandles - 10; | |
929 | } | |
930 | CallbackInfo[find].proc = callback; | |
931 | CallbackInfo[find].type = mask; | |
932 | CallbackInfo[find].handle = handle; | |
933 | CallbackInfo[find].gsock = gsock; | |
934 | if (mask & wxSockReadMask) | |
935 | FD_SET(handle, &m_readfds); | |
936 | if (mask & wxSockWriteMask) | |
937 | FD_SET(handle, &m_writefds); | |
938 | if (handle >= m_maxSocketNr) | |
939 | m_maxSocketNr = handle + 1; | |
940 | return find; | |
941 | } | |
942 | ||
943 | void wxApp::RemoveSocketHandler(int handle) | |
944 | { | |
945 | struct GsocketCallbackInfo | |
946 | *CallbackInfo = (struct GsocketCallbackInfo *)m_sockCallbackInfo; | |
947 | if (handle < m_maxSocketHandles) | |
948 | { | |
949 | if (CallbackInfo[handle].type & wxSockReadMask) | |
950 | FD_CLR(CallbackInfo[handle].handle, &m_readfds); | |
951 | if (CallbackInfo[handle].type & wxSockWriteMask) | |
952 | FD_CLR(CallbackInfo[handle].handle, &m_writefds); | |
953 | CallbackInfo[handle].handle = -1; | |
954 | } | |
955 | } | |
956 | ||
957 | //----------------------------------------------------------------------------- | |
958 | // wxWakeUpIdle | |
959 | //----------------------------------------------------------------------------- | |
960 | ||
961 | void wxApp::WakeUpIdle() | |
962 | { | |
963 | // | |
964 | // Send the top window a dummy message so idle handler processing will | |
965 | // start up again. Doing it this way ensures that the idle handler | |
966 | // wakes up in the right thread (see also wxWakeUpMainThread() which does | |
967 | // the same for the main app thread only) | |
968 | // | |
969 | wxWindow* pTopWindow = wxTheApp->GetTopWindow(); | |
970 | ||
971 | if (pTopWindow) | |
972 | { | |
973 | if ( !::WinPostMsg(GetHwndOf(pTopWindow), WM_NULL, (MPARAM)0, (MPARAM)0)) | |
974 | { | |
975 | // | |
976 | // Should never happen | |
977 | // | |
978 | wxLogLastError("PostMessage(WM_NULL)"); | |
979 | } | |
980 | } | |
981 | } // end of wxWakeUpIdle | |
982 | ||
983 | HAB wxGetInstance() | |
984 | { | |
985 | return vHabmain; | |
986 | } | |
987 | ||
988 | void wxSetInstance( | |
989 | HAB vHab | |
990 | ) | |
991 | { | |
992 | vHabmain = vHab; | |
993 | } | |
994 |