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