Weekly catchup as well as better font and fontdlg support
[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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/frame.h"
17 #include "wx/app.h"
18 #include "wx/utils.h"
19 #include "wx/gdicmn.h"
20 #include "wx/pen.h"
21 #include "wx/brush.h"
22 #include "wx/cursor.h"
23 #include "wx/icon.h"
24 #include "wx/palette.h"
25 #include "wx/dc.h"
26 #include "wx/dialog.h"
27 #include "wx/msgdlg.h"
28 #include "wx/intl.h"
29 #include "wx/dynarray.h"
30 #include "wx/wxchar.h"
31 #include "wx/icon.h"
32 #include "wx/timer.h"
33 #endif
34
35 #include "wx/log.h"
36 #include "wx/module.h"
37
38 #include "wx/os2/private.h"
39
40 #ifdef __EMX__
41
42 #include <sys/ioctl.h>
43 #include <sys/select.h>
44
45 #else
46
47 #include <nerrno.h>
48 #include <sys/ioctl.h>
49 #include <sys/select.h>
50 #include <sys/time.h>
51
52 #endif //
53
54 #ifndef __EMX__
55
56 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
57 extern "C" int _System bsdselect(int,
58 struct fd_set *,
59 struct fd_set *,
60 struct fd_set *,
61 struct timeval *);
62 #endif
63
64 #if wxUSE_THREADS
65 #include "wx/thread.h"
66
67 // define the array of QMSG strutures
68 WX_DECLARE_OBJARRAY(QMSG, wxMsgArray);
69
70 #include "wx/arrimpl.cpp"
71
72 WX_DEFINE_OBJARRAY(wxMsgArray);
73 #endif // wxUSE_THREADS
74
75 #if wxUSE_WX_RESOURCES
76 #include "wx/resource.h"
77 #endif
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 #if wxUSE_WX_RESOURCES
271 wxInitializeResourceSystem();
272 #endif
273
274 wxBitmap::InitStandardHandlers();
275
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
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.
296 #if !defined(WXMAKINGDLL) && defined(__VISAGECPP__)
297 extern char wxDummyChar;
298 if (wxDummyChar) wxDummyChar++;
299 #endif
300
301 // wxSetKeyboardHook(TRUE);
302
303 wxModule::RegisterModules();
304 if (!wxModule::InitializeModules())
305 return FALSE;
306 RegisterWindowClasses(vHab);
307 return TRUE;
308 } // end of wxApp::Initialize
309
310 const char* CANTREGISTERCLASS = " Can't register Class ";
311 // ---------------------------------------------------------------------------
312 // RegisterWindowClasses
313 // ---------------------------------------------------------------------------
314
315 bool wxApp::RegisterWindowClasses(
316 HAB vHab
317 )
318 {
319 ERRORID vError = 0L;
320 wxString sError;
321
322 if (!::WinRegisterClass( vHab
323 ,wxFrameClassName
324 ,wxFrameWndProc
325 ,CS_SIZEREDRAW | CS_SYNCPAINT
326 ,sizeof(ULONG)
327 ))
328 {
329 vError = ::WinGetLastError(vHab);
330 sError = wxPMErrorToStr(vError);
331 wxLogLastError(sError);
332 return FALSE;
333 }
334
335 if (!::WinRegisterClass( vHab
336 ,wxFrameClassNameNoRedraw
337 ,wxWndProc
338 ,0
339 ,sizeof(ULONG)
340 ))
341 {
342 vError = ::WinGetLastError(vHab);
343 sError = wxPMErrorToStr(vError);
344 wxLogLastError(sError);
345 return FALSE;
346 }
347
348 if (!::WinRegisterClass( vHab
349 ,wxMDIFrameClassName
350 ,wxWndProc
351 ,CS_SIZEREDRAW | CS_MOVENOTIFY | CS_SYNCPAINT
352 ,sizeof(ULONG)
353 ))
354 {
355 vError = ::WinGetLastError(vHab);
356 sError = wxPMErrorToStr(vError);
357 wxLogLastError(sError);
358 return FALSE;
359 }
360
361 if (!::WinRegisterClass( vHab
362 ,wxMDIFrameClassNameNoRedraw
363 ,wxWndProc
364 ,0
365 ,sizeof(ULONG)
366 ))
367 {
368 vError = ::WinGetLastError(vHab);
369 sError = wxPMErrorToStr(vError);
370 wxLogLastError(sError);
371 return FALSE;
372 }
373
374 if (!::WinRegisterClass( vHab
375 ,wxMDIChildFrameClassName
376 ,wxWndProc
377 ,CS_MOVENOTIFY | CS_SIZEREDRAW | CS_SYNCPAINT | CS_HITTEST
378 ,sizeof(ULONG)
379 ))
380 {
381 vError = ::WinGetLastError(vHab);
382 sError = wxPMErrorToStr(vError);
383 wxLogLastError(sError);
384 return FALSE;
385 }
386
387 if (!::WinRegisterClass( vHab
388 ,wxMDIChildFrameClassNameNoRedraw
389 ,wxWndProc
390 ,CS_HITTEST
391 ,sizeof(ULONG)
392 ))
393 {
394 vError = ::WinGetLastError(vHab);
395 sError = wxPMErrorToStr(vError);
396 wxLogLastError(sError);
397 return FALSE;
398 }
399
400 if (!::WinRegisterClass( vHab
401 ,wxPanelClassName
402 ,wxWndProc
403 ,CS_MOVENOTIFY | CS_SIZEREDRAW | CS_HITTEST | CS_SAVEBITS | CS_SYNCPAINT
404 ,sizeof(ULONG)
405 ))
406 {
407 vError = ::WinGetLastError(vHab);
408 sError = wxPMErrorToStr(vError);
409 wxLogLastError(sError);
410 return FALSE;
411 }
412
413 if (!::WinRegisterClass( vHab
414 ,wxCanvasClassName
415 ,wxWndProc
416 ,CS_SIZEREDRAW | CS_HITTEST | CS_SYNCPAINT
417 ,sizeof(ULONG)
418 ))
419 {
420 vError = ::WinGetLastError(vHab);
421 sError = wxPMErrorToStr(vError);
422 wxLogLastError(sError);
423 return FALSE;
424 }
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 }
437 return TRUE;
438 } // end of wxApp::RegisterWindowClasses
439
440 //
441 // Cleans up any wxWindows internal structures left lying around
442 //
443 void wxApp::CleanUp()
444 {
445 //
446 // COMMON CLEANUP
447 //
448
449 #if wxUSE_LOG
450
451 //
452 // Flush the logged messages if any and install a 'safer' log target: the
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)
456 //
457 wxLog::DontCreateOnDemand();
458
459 //
460 // This will flush the old messages if any
461 //
462 delete wxLog::SetActiveTarget(new wxLogStderr);
463 #endif // wxUSE_LOG
464
465 //
466 // One last chance for pending objects to be cleaned up
467 //
468 wxTheApp->DeletePendingObjects();
469
470 wxModule::CleanUpModules();
471
472 #if wxUSE_WX_RESOURCES
473 wxCleanUpResourceSystem();
474 #endif
475
476 wxDeleteStockObjects();
477
478 //
479 // Destroy all GDI lists, etc.
480 //
481 wxDeleteStockLists();
482
483 delete wxTheColourDatabase;
484 wxTheColourDatabase = NULL;
485
486 wxBitmap::CleanUpHandlers();
487
488 delete[] wxBuffer;
489 wxBuffer = NULL;
490
491 //
492 // PM-SPECIFIC CLEANUP
493 //
494
495 // wxSetKeyboardHook(FALSE);
496
497 if (wxSTD_FRAME_ICON)
498 ::WinFreeFileIcon(wxSTD_FRAME_ICON);
499 if (wxSTD_MDICHILDFRAME_ICON)
500 ::WinFreeFileIcon(wxSTD_MDICHILDFRAME_ICON);
501 if (wxSTD_MDIPARENTFRAME_ICON)
502 ::WinFreeFileIcon(wxSTD_MDIPARENTFRAME_ICON);
503
504 if (wxDEFAULT_FRAME_ICON)
505 ::WinFreeFileIcon(wxDEFAULT_FRAME_ICON);
506 if (wxDEFAULT_MDICHILDFRAME_ICON)
507 ::WinFreeFileIcon(wxDEFAULT_MDICHILDFRAME_ICON);
508 if (wxDEFAULT_MDIPARENTFRAME_ICON)
509 ::WinFreeFileIcon(wxDEFAULT_MDIPARENTFRAME_ICON);
510
511 if ( wxDisableButtonBrush )
512 {
513 // TODO: ::DeleteObject( wxDisableButtonBrush );
514 }
515
516 if (wxWinHandleList)
517 delete wxWinHandleList;
518
519 delete wxPendingEvents;
520 #if wxUSE_THREADS
521 delete wxPendingEventsLocker;
522 // If we don't do the following, we get an apparent memory leak.
523 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
524 #endif
525
526 wxClassInfo::CleanUpClasses();
527
528 // Delete Message queue
529 if (wxTheApp->m_hMq)
530 ::WinDestroyMsgQueue(wxTheApp->m_hMq);
531
532 delete wxTheApp;
533 wxTheApp = NULL;
534
535 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
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);
547 #endif
548
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
553 } // end of wxApp::CleanUp
554
555 //----------------------------------------------------------------------
556 // Main wxWindows entry point
557 //----------------------------------------------------------------------
558 int wxEntry(
559 int argc
560 , char* argv[]
561 )
562 {
563 HAB vHab = 0;
564
565 if (!wxApp::Initialize(vHab))
566 return 0;
567
568 //
569 // create the application object or ensure that one already exists
570 //
571 if (!wxTheApp)
572 {
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()) ();
579 }
580 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
581 wxTheApp->argc = argc;
582
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
597
598 wxString sName(wxFileNameFromPath(argv[0]));
599
600 wxStripExtension(sName);
601 wxTheApp->SetAppName(sName);
602
603 int nRetValue = 0;
604
605 if (!wxTheApp->OnInitGui())
606 nRetValue = -1;
607
608 if (nRetValue == 0)
609 {
610 if (wxTheApp->OnInit())
611 {
612 nRetValue = wxTheApp->OnRun();
613 }
614 // Normal exit
615 wxWindow* pTopWindow = wxTheApp->GetTopWindow();
616 if (pTopWindow)
617 {
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 }
630 }
631 }
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
640 wxTheApp->OnExit();
641 #if wxUSE_CONSOLEDEBUG
642 printf("wxApp::CleanUp ");
643 fflush(stdout);
644 #endif
645 wxApp::CleanUp();
646 #if wxUSE_CONSOLEDEBUG
647 printf("return %i ", nRetValue);
648 fflush(stdout);
649 #endif
650 return(nRetValue);
651 } // end of wxEntry
652
653 bool wxApp::OnInitGui()
654 {
655 ERRORID vError;
656 wxString sError;
657
658 if (!wxAppBase::OnInitGui())
659 return FALSE;
660
661 m_hMq = ::WinCreateMsgQueue(vHabmain, 0);
662 if (!m_hMq)
663 {
664 vError = ::WinGetLastError(vHabmain);
665 sError = wxPMErrorToStr(vError);
666 wxLogDebug(sError);
667 return FALSE;
668 }
669
670 return TRUE;
671 } // end of wxApp::OnInitGui
672
673 //
674 // Static member initialization
675 //
676 wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
677
678 wxApp::wxApp()
679 {
680 m_topWindow = NULL;
681 wxTheApp = this;
682
683 argc = 0;
684 argv = NULL;
685 m_nPrintMode = wxPRINT_WINDOWS;
686 m_bAuto3D = TRUE;
687 m_hMq = 0;
688 m_maxSocketHandles = 0;
689 m_maxSocketNr = 0;
690 m_sockCallbackInfo = 0;
691 } // end of wxApp::wxApp
692
693 wxApp::~wxApp()
694 {
695 //
696 // Delete command-line args
697 //
698 #if wxUSE_UNICODE
699 int i;
700
701 for (i = 0; i < argc; i++)
702 {
703 delete[] argv[i];
704 }
705 delete[] argv;
706 #endif
707 } // end of wxApp::~wxApp
708
709 bool wxApp::Initialized()
710 {
711 if (GetTopWindow())
712 return TRUE;
713 else
714 return FALSE;
715 } // end of wxApp::Initialized
716
717 //
718 // Get and process a message, returning FALSE if WM_QUIT
719 // received (and also set the flag telling the app to exit the main loop)
720 //
721
722 bool wxApp::DoMessage()
723 {
724 BOOL bRc = ::WinGetMsg(vHabmain, &svCurrentMsg, HWND(NULL), 0, 0);
725
726 if (bRc == 0)
727 {
728 // got WM_QUIT
729 m_bKeepGoing = FALSE;
730 return FALSE;
731 }
732 else if (bRc == -1)
733 {
734 // should never happen, but let's test for it nevertheless
735 wxLogLastError("GetMessage");
736 }
737 else
738 {
739 #if wxUSE_THREADS
740 wxASSERT_MSG( wxThread::IsMain()
741 ,wxT("only the main thread can process Windows messages")
742 );
743
744 static bool sbHadGuiLock = TRUE;
745 static wxMsgArray svSavedMessages;
746
747 //
748 // If a secondary thread owns is doing GUI calls, save all messages for
749 // later processing - we can't process them right now because it will
750 // lead to recursive library calls (and we're not reentrant)
751 //
752 if (!wxGuiOwnedByMainThread())
753 {
754 sbHadGuiLock = FALSE;
755
756 //
757 // Leave out WM_COMMAND messages: too dangerous, sometimes
758 // the message will be processed twice
759 //
760 if ( !wxIsWaitingForThread() ||
761 svCurrentMsg.msg != WM_COMMAND )
762 {
763 svSavedMessages.Add(svCurrentMsg);
764 }
765 return TRUE;
766 }
767 else
768 {
769 //
770 // Have we just regained the GUI lock? if so, post all of the saved
771 // messages
772 //
773 if (!sbHadGuiLock )
774 {
775 sbHadGuiLock = TRUE;
776
777 size_t nCount = svSavedMessages.Count();
778
779 for (size_t n = 0; n < nCount; n++)
780 {
781 QMSG vMsg = svSavedMessages[n];
782
783 DoMessage((WXMSG*)&vMsg);
784 }
785 svSavedMessages.Empty();
786 }
787 }
788 #endif // wxUSE_THREADS
789
790 //
791 // Process the message
792 //
793 DoMessage((WXMSG *)&svCurrentMsg);
794 }
795 return TRUE;
796 } // end of wxApp::DoMessage
797
798 void wxApp::DoMessage(
799 WXMSG* pMsg
800 )
801 {
802 if (!ProcessMessage((WXMSG *)&svCurrentMsg))
803 {
804 ::WinDispatchMsg(vHabmain, (PQMSG)&svCurrentMsg);
805 }
806 } // end of wxApp::DoMessage
807
808 //////////////////////////////////////////////////////////////////////////////
809 //
810 // Keep trying to process messages until WM_QUIT
811 // received.
812 //
813 // If there are messages to be processed, they will all be
814 // processed and OnIdle will not be called.
815 // When there are no more messages, OnIdle is called.
816 // If OnIdle requests more time,
817 // it will be repeatedly called so long as there are no pending messages.
818 // A 'feature' of this is that once OnIdle has decided that no more processing
819 // is required, then it won't get processing time until further messages
820 // are processed (it'll sit in DoMessage).
821 //
822 //////////////////////////////////////////////////////////////////////////////
823 int wxApp::MainLoop()
824 {
825 m_bKeepGoing = TRUE;
826
827 while (m_bKeepGoing)
828 {
829 #if wxUSE_THREADS
830 wxMutexGuiLeaveOrEnter();
831 #endif // wxUSE_THREADS
832 while (!Pending() && ProcessIdle())
833 {
834 HandleSockets();
835 wxUsleep(10000);
836 }
837 HandleSockets();
838 if (Pending())
839 DoMessage();
840 else
841 wxUsleep(10000);
842
843 }
844 return (int)svCurrentMsg.mp1;
845 } // end of wxApp::MainLoop
846
847 //
848 // Returns TRUE if more time is needed.
849 //
850 bool wxApp::ProcessIdle()
851 {
852 wxIdleEvent vEvent;
853
854 vEvent.SetEventObject(this);
855 ProcessEvent(vEvent);
856 return vEvent.MoreRequested();
857 } // end of wxApp::ProcessIdle
858
859 void wxApp::ExitMainLoop()
860 {
861 ::WinPostMsg(NULL, WM_QUIT, 0, 0);
862 } // end of wxApp::ExitMainLoop
863
864 bool wxApp::Pending()
865 {
866 return (::WinPeekMsg(vHabmain, (PQMSG)&svCurrentMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) != 0);
867 } // end of wxApp::Pending
868
869 void wxApp::Dispatch()
870 {
871 DoMessage();
872 }
873
874 //////////////////////////////////////////////////////////////////////////////
875 //
876 // Give all windows a chance to preprocess
877 // the message. Some may have accelerator tables, or have
878 // MDI complications.
879 //
880 //////////////////////////////////////////////////////////////////////////////
881 bool wxApp::ProcessMessage(
882 WXMSG* pWxmsg
883 )
884 {
885 QMSG* pMsg = (PQMSG)pWxmsg;
886 HWND hWnd = pMsg->hwnd;
887 wxWindow* pWndThis = wxFindWinFromHandle((WXHWND)hWnd);
888 wxWindow* pWnd;
889
890 //
891 // Pass non-system timer messages to the wxTimerProc
892 //
893 if (pMsg->msg == WM_TIMER &&
894 (SHORT1FROMMP(pMsg->mp1) != TID_CURSOR &&
895 SHORT1FROMMP(pMsg->mp1) != TID_FLASHWINDOW &&
896 SHORT1FROMMP(pMsg->mp1) != TID_SCROLL &&
897 SHORT1FROMMP(pMsg->mp1) != 0x0000
898 ))
899 wxTimerProc(NULL, 0, (int)pMsg->mp1, 0);
900
901 //
902 // Allow the window to prevent certain messages from being
903 // translated/processed (this is currently used by wxTextCtrl to always
904 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
905 //
906 if (pWndThis && !pWndThis->OS2ShouldPreProcessMessage(pWxmsg))
907 {
908 return FALSE;
909 }
910
911 //
912 // For some composite controls (like a combobox), wndThis might be NULL
913 // because the subcontrol is not a wxWindow, but only the control itself
914 // is - try to catch this case
915 //
916 while (hWnd && !pWndThis)
917 {
918 hWnd = ::WinQueryWindow(hWnd, QW_PARENT);
919 pWndThis = wxFindWinFromHandle((WXHWND)hWnd);
920 }
921
922 //
923 // Try translations first; find the youngest window with
924 // a translation table. OS/2 has case sensative accels, so
925 // this block, coded by BK, removes that and helps make them
926 // case insensative.
927 //
928 if(pMsg->msg == WM_CHAR)
929 {
930 PBYTE pChmsg = (PBYTE)&(pMsg->msg);
931 USHORT uSch = CHARMSG(pChmsg)->chr;
932 bool bRc;
933
934 //
935 // Do not process keyup events
936 //
937 if(!(CHARMSG(pChmsg)->fs & KC_KEYUP))
938 {
939 if((CHARMSG(pChmsg)->fs & (KC_ALT | KC_CTRL)) && CHARMSG(pChmsg)->chr != 0)
940 CHARMSG(pChmsg)->chr = (USHORT)wxToupper((UCHAR)uSch);
941
942
943 for(pWnd = pWndThis; pWnd; pWnd = pWnd->GetParent() )
944 {
945 if((bRc = pWnd->OS2TranslateMessage(pWxmsg)) == TRUE)
946 break;
947 }
948
949 if(!bRc) // untranslated, should restore original value
950 CHARMSG(pChmsg)->chr = uSch;
951 }
952 }
953 //
954 // Anyone for a non-translation message? Try youngest descendants first.
955 //
956 // for (pWnd = pWndThis; pWnd; pWnd = pWnd->GetParent())
957 // {
958 // if (pWnd->OS2ProcessMessage(pWxmsg))
959 // return TRUE;
960 // }
961 return FALSE;
962 } // end of wxApp::ProcessMessage
963
964 void wxApp::OnIdle(
965 wxIdleEvent& rEvent
966 )
967 {
968 static bool sbInOnIdle = FALSE;
969
970 //
971 // Avoid recursion (via ProcessEvent default case)
972 //
973 if (sbInOnIdle)
974 return;
975
976 sbInOnIdle = TRUE;
977
978 //
979 // If there are pending events, we must process them: pending events
980 // are either events to the threads other than main or events posted
981 // with wxPostEvent() functions
982 //
983 ProcessPendingEvents();
984
985 //
986 // 'Garbage' collection of windows deleted with Close().
987 //
988 DeletePendingObjects();
989
990 #if wxUSE_LOG
991 //
992 // Flush the logged messages if any
993 //
994 wxLog::FlushActive();
995 #endif // wxUSE_LOG
996
997 #if wxUSE_DC_CACHEING
998 // automated DC cache management: clear the cached DCs and bitmap
999 // if it's likely that the app has finished with them, that is, we
1000 // get an idle event and we're not dragging anything.
1001 if (!::WinGetKeyState(HWND_DESKTOP, VK_BUTTON1) &&
1002 !::WinGetKeyState(HWND_DESKTOP, VK_BUTTON3) &&
1003 !::WinGetKeyState(HWND_DESKTOP, VK_BUTTON2))
1004 wxDC::ClearCache();
1005 #endif // wxUSE_DC_CACHEING
1006
1007 //
1008 // Send OnIdle events to all windows
1009 //
1010 if (SendIdleEvents())
1011 {
1012 //
1013 // SendIdleEvents() returns TRUE if at least one window requested more
1014 // idle events
1015 //
1016 rEvent.RequestMore(TRUE);
1017 }
1018 sbInOnIdle = FALSE;
1019 } // end of wxApp::OnIdle
1020
1021 // Send idle event to all top-level windows
1022 bool wxApp::SendIdleEvents()
1023 {
1024 bool bNeedMore = FALSE;
1025 wxWindowList::Node* pNode = wxTopLevelWindows.GetFirst();
1026
1027 while (pNode)
1028 {
1029 wxWindow* pWin = pNode->GetData();
1030
1031 if (SendIdleEvents(pWin))
1032 bNeedMore = TRUE;
1033 pNode = pNode->GetNext();
1034 }
1035 return bNeedMore;
1036 } // end of wxApp::SendIdleEvents
1037
1038 //
1039 // Send idle event to window and all subwindows
1040 //
1041 bool wxApp::SendIdleEvents(
1042 wxWindow* pWin
1043 )
1044 {
1045 bool bNeedMore = FALSE;
1046 wxIdleEvent vEvent;
1047
1048 vEvent.SetEventObject(pWin);
1049 pWin->GetEventHandler()->ProcessEvent(vEvent);
1050
1051 if (vEvent.MoreRequested())
1052 bNeedMore = TRUE;
1053
1054 wxNode* pNode = pWin->GetChildren().First();
1055
1056 while (pNode)
1057 {
1058 wxWindow* pWin = (wxWindow*) pNode->Data();
1059
1060 if (SendIdleEvents(pWin))
1061 bNeedMore = TRUE;
1062 pNode = pNode->Next();
1063 }
1064 return bNeedMore;
1065 } // end of wxApp::SendIdleEvents
1066
1067 void wxApp::DeletePendingObjects()
1068 {
1069 wxNode* pNode = wxPendingDelete.First();
1070
1071 while (pNode)
1072 {
1073 wxObject* pObj = (wxObject *)pNode->Data();
1074
1075 delete pObj;
1076
1077 if (wxPendingDelete.Member(pObj))
1078 delete pNode;
1079
1080 //
1081 // Deleting one object may have deleted other pending
1082 // objects, so start from beginning of list again.
1083 //
1084 pNode = wxPendingDelete.First();
1085 }
1086 } // end of wxApp::DeletePendingObjects
1087
1088 void wxApp::OnEndSession(
1089 wxCloseEvent& WXUNUSED(rEvent))
1090 {
1091 if (GetTopWindow())
1092 GetTopWindow()->Close(TRUE);
1093 } // end of wxApp::OnEndSession
1094
1095 //
1096 // Default behaviour: close the application with prompts. The
1097 // user can veto the close, and therefore the end session.
1098 //
1099 void wxApp::OnQueryEndSession(
1100 wxCloseEvent& rEvent
1101 )
1102 {
1103 if (GetTopWindow())
1104 {
1105 if (!GetTopWindow()->Close(!rEvent.CanVeto()))
1106 rEvent.Veto(TRUE);
1107 }
1108 } // end of wxApp::OnQueryEndSession
1109
1110 void wxExit()
1111 {
1112 wxLogError(_("Fatal error: exiting"));
1113
1114 wxApp::CleanUp();
1115 } // end of wxExit
1116
1117 //
1118 // Yield to incoming messages
1119 //
1120 bool wxApp::Yield(bool onlyIfNeeded)
1121 {
1122 static bool s_inYield = FALSE;
1123
1124 if ( s_inYield )
1125 {
1126 if ( !onlyIfNeeded )
1127 {
1128 wxFAIL_MSG( _T("wxYield() called recursively") );
1129 }
1130
1131 return FALSE;
1132 }
1133
1134 HAB vHab = 0;
1135 QMSG vMsg;
1136
1137 //
1138 // Disable log flushing from here because a call to wxYield() shouldn't
1139 // normally result in message boxes popping up &c
1140 //
1141 wxLog::Suspend();
1142
1143 s_inYield = TRUE;
1144
1145 //
1146 // We want to go back to the main message loop
1147 // if we see a WM_QUIT. (?)
1148 //
1149 while (::WinPeekMsg(vHab, &vMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) && vMsg.msg != WM_QUIT)
1150 {
1151 #if wxUSE_THREADS
1152 wxMutexGuiLeaveOrEnter();
1153 #endif // wxUSE_THREADS
1154 if (!wxTheApp->DoMessage())
1155 break;
1156 }
1157 //
1158 // If they are pending events, we must process them.
1159 //
1160 if (wxTheApp)
1161 wxTheApp->ProcessPendingEvents();
1162
1163 //
1164 // Let the logs be flashed again
1165 //
1166 wxLog::Resume();
1167 s_inYield = FALSE;
1168 return TRUE;
1169 } // end of wxYield
1170
1171 int wxApp::AddSocketHandler(int handle, int mask,
1172 void (*callback)(void*), void * gsock)
1173 {
1174 int find;
1175 struct GsocketCallbackInfo
1176 *CallbackInfo = (struct GsocketCallbackInfo *)m_sockCallbackInfo;
1177
1178 for (find = 0; find < m_maxSocketHandles; find++)
1179 if (CallbackInfo[find].handle == -1)
1180 break;
1181 if (find == m_maxSocketHandles)
1182 {
1183 // Allocate new memory
1184 m_sockCallbackInfo = realloc(m_sockCallbackInfo,
1185 (m_maxSocketHandles+=10)*
1186 sizeof(struct GsocketCallbackInfo));
1187 CallbackInfo = (struct GsocketCallbackInfo *)m_sockCallbackInfo;
1188 for (find = m_maxSocketHandles - 10; find < m_maxSocketHandles; find++)
1189 CallbackInfo[find].handle = -1;
1190 find = m_maxSocketHandles - 10;
1191 }
1192 CallbackInfo[find].proc = callback;
1193 CallbackInfo[find].type = mask;
1194 CallbackInfo[find].handle = handle;
1195 CallbackInfo[find].gsock = gsock;
1196 if (mask & wxSockReadMask)
1197 FD_SET(handle, &m_readfds);
1198 if (mask & wxSockWriteMask)
1199 FD_SET(handle, &m_writefds);
1200 if (handle >= m_maxSocketNr)
1201 m_maxSocketNr = handle + 1;
1202 return find;
1203 }
1204
1205 void wxApp::RemoveSocketHandler(int handle)
1206 {
1207 struct GsocketCallbackInfo
1208 *CallbackInfo = (struct GsocketCallbackInfo *)m_sockCallbackInfo;
1209 if (handle < m_maxSocketHandles)
1210 {
1211 if (CallbackInfo[handle].type & wxSockReadMask)
1212 FD_CLR(CallbackInfo[handle].handle, &m_readfds);
1213 if (CallbackInfo[handle].type & wxSockWriteMask)
1214 FD_CLR(CallbackInfo[handle].handle, &m_writefds);
1215 CallbackInfo[handle].handle = -1;
1216 }
1217 }
1218
1219 //-----------------------------------------------------------------------------
1220 // wxWakeUpIdle
1221 //-----------------------------------------------------------------------------
1222
1223 void wxWakeUpIdle()
1224 {
1225 //
1226 // Send the top window a dummy message so idle handler processing will
1227 // start up again. Doing it this way ensures that the idle handler
1228 // wakes up in the right thread (see also wxWakeUpMainThread() which does
1229 // the same for the main app thread only)
1230 //
1231 wxWindow* pTopWindow = wxTheApp->GetTopWindow();
1232
1233 if (pTopWindow)
1234 {
1235 if ( !::WinPostMsg(GetHwndOf(pTopWindow), WM_NULL, (MPARAM)0, (MPARAM)0))
1236 {
1237 //
1238 // Should never happen
1239 //
1240 wxLogLastError("PostMessage(WM_NULL)");
1241 }
1242 }
1243 } // end of wxWakeUpIdle
1244
1245 HAB wxGetInstance()
1246 {
1247 return vHabmain;
1248 }
1249
1250 void wxSetInstance(
1251 HAB vHab
1252 )
1253 {
1254 vHabmain = vHab;
1255 }
1256