1 /////////////////////////////////////////////////////////////////////////////
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "app.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #include "wx/gdicmn.h"
26 #include "wx/cursor.h"
28 #include "wx/palette.h"
30 #include "wx/dialog.h"
31 #include "wx/msgdlg.h"
33 #include "wx/dynarray.h"
34 #include "wx/wxchar.h"
40 #include "wx/module.h"
42 #include "wx/os2/private.h"
46 #include <sys/ioctl.h>
47 #include <sys/select.h>
52 #include <sys/ioctl.h>
53 #include <sys/select.h>
60 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
61 extern "C" int _System
bsdselect(int,
69 #include "wx/thread.h"
71 // define the array of QMSG strutures
72 WX_DECLARE_OBJARRAY(QMSG
, wxMsgArray
);
74 #include "wx/arrimpl.cpp"
76 WX_DEFINE_OBJARRAY(wxMsgArray
);
77 #endif // wxUSE_THREADS
80 #include "wx/tooltip.h"
81 #endif // wxUSE_TOOLTIPS
86 // ---------------------------------------------------------------------------
88 // ---------------------------------------------------------------------------
90 extern wxChar
* wxBuffer
;
91 extern wxList
* wxWinHandleList
;
92 extern wxList WXDLLEXPORT wxPendingDelete
;
93 extern wxCursor
* g_globalCursor
;
95 HAB vHabmain
= NULLHANDLE
;
99 HICON wxSTD_FRAME_ICON
= (HICON
) NULL
;
100 HICON wxSTD_MDICHILDFRAME_ICON
= (HICON
) NULL
;
101 HICON wxSTD_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
103 HICON wxDEFAULT_FRAME_ICON
= (HICON
) NULL
;
104 HICON wxDEFAULT_MDICHILDFRAME_ICON
= (HICON
) NULL
;
105 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
107 HBRUSH wxDisableButtonBrush
= (HBRUSH
) 0;
109 MRESULT EXPENTRY
wxWndProc( HWND hWnd
,ULONG message
,MPARAM mp1
,MPARAM mp2
);
110 MRESULT EXPENTRY
wxFrameWndProc( HWND hWnd
,ULONG message
,MPARAM mp1
,MPARAM mp2
);
112 // ===========================================================================
114 // ===========================================================================
116 // ---------------------------------------------------------------------------
117 // helper struct and functions for socket handling
118 // ---------------------------------------------------------------------------
120 struct GsocketCallbackInfo
{
121 void (*proc
)(void *);
127 // These defines and wrapper functions are used here and in gsockpm.c
128 #define wxSockReadMask 0x01
129 #define wxSockWriteMask 0x02
133 int wxAppAddSocketHandler(int handle
, int mask
,
134 void (*callback
)(void*), void * gsock
)
136 return wxTheApp
->AddSocketHandler(handle
, mask
, callback
, gsock
);
139 void wxAppRemoveSocketHandler(int handle
)
141 wxTheApp
->RemoveSocketHandler(handle
);
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
)
148 return wxTheApp
->AddSocketHandler(handle
, mask
, callback
, gsock
);
150 void wxAppRemoveSocketHandler(int handle
)
152 wxTheApp
->RemoveSocketHandler(handle
);
156 void wxApp::HandleSockets()
158 bool pendingEvent
= FALSE
;
160 // Check whether it's time for Gsocket operation
161 if (m_maxSocketHandles
> 0 && m_maxSocketNr
> 0)
163 fd_set readfds
= m_readfds
;
164 fd_set writefds
= m_writefds
;
165 struct timeval timeout
;
167 struct GsocketCallbackInfo
168 *CallbackInfo
= (struct GsocketCallbackInfo
*)m_sockCallbackInfo
;
171 if ( select(m_maxSocketNr
, &readfds
, &writefds
, 0, &timeout
) > 0)
173 for (i
= m_lastUsedHandle
+ 1; i
!= m_lastUsedHandle
;
174 (i
< m_maxSocketNr
- 1) ? i
++ : (i
= 0))
176 if (FD_ISSET(i
, &readfds
))
179 for (r
= 0; r
< m_maxSocketHandles
; r
++){
180 if(CallbackInfo
[r
].handle
== i
&&
181 CallbackInfo
[r
].type
== wxSockReadMask
)
184 if (r
< m_maxSocketHandles
)
186 CallbackInfo
[r
].proc(CallbackInfo
[r
].gsock
);
190 if (FD_ISSET(i
, &writefds
))
193 for (r
= 0; r
< m_maxSocketHandles
; r
++)
194 if(CallbackInfo
[r
].handle
== i
&&
195 CallbackInfo
[r
].type
== wxSockWriteMask
)
197 if (r
< m_maxSocketHandles
)
199 CallbackInfo
[r
].proc(CallbackInfo
[r
].gsock
);
204 m_lastUsedHandle
= i
;
207 ProcessPendingEvents();
210 // ---------------------------------------------------------------------------
212 // ---------------------------------------------------------------------------
214 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
216 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
217 EVT_IDLE(wxApp::OnIdle
)
218 EVT_END_SESSION(wxApp::OnEndSession
)
219 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
225 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
227 if ( !wxAppBase::Initialize(argc
, argv
) )
230 #if defined(wxUSE_CONSOLEDEBUG)
231 #if wxUSE_CONSOLEDEBUG
232 /***********************************************/
233 /* Code for using stdout debug */
234 /* To use it you mast link app as "Window" - EK*/
235 /***********************************************/
240 printf("In console\n");
242 DosGetInfoBlocks(&tib
, &pib
);
243 /* Try morphing into a PM application. */
244 // if(pib->pib_ultype == 2) /* VIO */
247 /**********************************************/
248 /**********************************************/
249 #endif //wxUSE_CONSOLEDEBUG
253 // OS2 has to have an anchorblock
255 vHabmain
= WinInitialize(0);
259 // TODO: at least give some error message here...
260 wxAppBase::CleanUp();
265 wxBuffer
= new wxChar
[1500]; // FIXME; why?
267 // Some people may wish to use this, but
268 // probably it shouldn't be here by default.
270 // wxRedirectIOToConsole();
273 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
275 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
276 // PLEASE DO NOT ALTER THIS.
277 #if !defined(WXMAKINGDLL) && defined(__VISAGECPP__)
278 extern char wxDummyChar
;
279 if (wxDummyChar
) wxDummyChar
++;
282 // wxSetKeyboardHook(TRUE);
284 RegisterWindowClasses(vHabmain
);
287 } // end of wxApp::Initialize
289 const char* CANTREGISTERCLASS
= " Can't register Class ";
290 // ---------------------------------------------------------------------------
291 // RegisterWindowClasses
292 // ---------------------------------------------------------------------------
294 bool wxApp::RegisterWindowClasses(
301 if (!::WinRegisterClass( vHab
304 ,CS_SIZEREDRAW
| CS_SYNCPAINT
308 vError
= ::WinGetLastError(vHab
);
309 sError
= wxPMErrorToStr(vError
);
310 wxLogLastError(sError
);
314 if (!::WinRegisterClass( vHab
315 ,wxFrameClassNameNoRedraw
321 vError
= ::WinGetLastError(vHab
);
322 sError
= wxPMErrorToStr(vError
);
323 wxLogLastError(sError
);
327 if (!::WinRegisterClass( vHab
330 ,CS_SIZEREDRAW
| CS_MOVENOTIFY
| CS_SYNCPAINT
334 vError
= ::WinGetLastError(vHab
);
335 sError
= wxPMErrorToStr(vError
);
336 wxLogLastError(sError
);
340 if (!::WinRegisterClass( vHab
341 ,wxMDIFrameClassNameNoRedraw
347 vError
= ::WinGetLastError(vHab
);
348 sError
= wxPMErrorToStr(vError
);
349 wxLogLastError(sError
);
353 if (!::WinRegisterClass( vHab
354 ,wxMDIChildFrameClassName
356 ,CS_MOVENOTIFY
| CS_SIZEREDRAW
| CS_SYNCPAINT
| CS_HITTEST
360 vError
= ::WinGetLastError(vHab
);
361 sError
= wxPMErrorToStr(vError
);
362 wxLogLastError(sError
);
366 if (!::WinRegisterClass( vHab
367 ,wxMDIChildFrameClassNameNoRedraw
373 vError
= ::WinGetLastError(vHab
);
374 sError
= wxPMErrorToStr(vError
);
375 wxLogLastError(sError
);
379 if (!::WinRegisterClass( vHab
382 ,CS_MOVENOTIFY
| CS_SIZEREDRAW
| CS_HITTEST
| CS_SAVEBITS
| CS_SYNCPAINT
386 vError
= ::WinGetLastError(vHab
);
387 sError
= wxPMErrorToStr(vError
);
388 wxLogLastError(sError
);
392 if (!::WinRegisterClass( vHab
395 ,CS_SIZEREDRAW
| CS_HITTEST
| CS_SYNCPAINT
399 vError
= ::WinGetLastError(vHab
);
400 sError
= wxPMErrorToStr(vError
);
401 wxLogLastError(sError
);
404 if (!::WinRegisterClass( vHab
407 ,CS_HITTEST
| CS_SYNCPAINT
411 vError
= ::WinGetLastError(vHab
);
412 sError
= wxPMErrorToStr(vError
);
413 wxLogLastError(sError
);
417 } // end of wxApp::RegisterWindowClasses
420 // Cleans up any wxWindows internal structures left lying around
422 void wxApp::CleanUp()
428 // PM-SPECIFIC CLEANUP
431 // wxSetKeyboardHook(FALSE);
433 if (wxSTD_FRAME_ICON
)
434 ::WinFreeFileIcon(wxSTD_FRAME_ICON
);
435 if (wxSTD_MDICHILDFRAME_ICON
)
436 ::WinFreeFileIcon(wxSTD_MDICHILDFRAME_ICON
);
437 if (wxSTD_MDIPARENTFRAME_ICON
)
438 ::WinFreeFileIcon(wxSTD_MDIPARENTFRAME_ICON
);
440 if (wxDEFAULT_FRAME_ICON
)
441 ::WinFreeFileIcon(wxDEFAULT_FRAME_ICON
);
442 if (wxDEFAULT_MDICHILDFRAME_ICON
)
443 ::WinFreeFileIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
444 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
445 ::WinFreeFileIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
447 if ( wxDisableButtonBrush
)
449 // TODO: ::DeleteObject( wxDisableButtonBrush );
453 delete wxWinHandleList
;
455 // Delete Message queue
457 ::WinDestroyMsgQueue(wxTheApp
->m_hMq
);
459 wxAppBase::CleanUp();
460 } // end of wxApp::CleanUp
462 bool wxApp::OnInitGui()
467 if (!wxAppBase::OnInitGui())
470 m_hMq
= ::WinCreateMsgQueue(vHabmain
, 0);
473 vError
= ::WinGetLastError(vHabmain
);
474 sError
= wxPMErrorToStr(vError
);
480 } // end of wxApp::OnInitGui
486 m_nPrintMode
= wxPRINT_WINDOWS
;
488 m_maxSocketHandles
= 0;
490 m_sockCallbackInfo
= 0;
491 } // end of wxApp::wxApp
496 // Delete command-line args
501 for (i
= 0; i
< argc
; i
++)
507 } // end of wxApp::~wxApp
509 bool wxApp::Initialized()
515 } // end of wxApp::Initialized
518 // Get and process a message, returning FALSE if WM_QUIT
519 // received (and also set the flag telling the app to exit the main loop)
522 bool wxApp::DoMessage()
524 BOOL bRc
= ::WinGetMsg(vHabmain
, &svCurrentMsg
, HWND(NULL
), 0, 0);
529 m_bKeepGoing
= FALSE
;
534 // should never happen, but let's test for it nevertheless
535 wxLogLastError("GetMessage");
540 wxASSERT_MSG( wxThread::IsMain()
541 ,wxT("only the main thread can process Windows messages")
544 static bool sbHadGuiLock
= TRUE
;
545 static wxMsgArray svSavedMessages
;
548 // If a secondary thread owns is doing GUI calls, save all messages for
549 // later processing - we can't process them right now because it will
550 // lead to recursive library calls (and we're not reentrant)
552 if (!wxGuiOwnedByMainThread())
554 sbHadGuiLock
= FALSE
;
557 // Leave out WM_COMMAND messages: too dangerous, sometimes
558 // the message will be processed twice
560 if ( !wxIsWaitingForThread() ||
561 svCurrentMsg
.msg
!= WM_COMMAND
)
563 svSavedMessages
.Add(svCurrentMsg
);
570 // Have we just regained the GUI lock? if so, post all of the saved
577 size_t nCount
= svSavedMessages
.Count();
579 for (size_t n
= 0; n
< nCount
; n
++)
581 QMSG vMsg
= svSavedMessages
[n
];
583 DoMessage((WXMSG
*)&vMsg
);
585 svSavedMessages
.Empty();
588 #endif // wxUSE_THREADS
591 // Process the message
593 DoMessage((WXMSG
*)&svCurrentMsg
);
596 } // end of wxApp::DoMessage
598 void wxApp::DoMessage(
602 if (!ProcessMessage((WXMSG
*)&svCurrentMsg
))
604 ::WinDispatchMsg(vHabmain
, (PQMSG
)&svCurrentMsg
);
606 } // end of wxApp::DoMessage
608 //////////////////////////////////////////////////////////////////////////////
610 // Keep trying to process messages until WM_QUIT
613 // If there are messages to be processed, they will all be
614 // processed and OnIdle will not be called.
615 // When there are no more messages, OnIdle is called.
616 // If OnIdle requests more time,
617 // it will be repeatedly called so long as there are no pending messages.
618 // A 'feature' of this is that once OnIdle has decided that no more processing
619 // is required, then it won't get processing time until further messages
620 // are processed (it'll sit in DoMessage).
622 //////////////////////////////////////////////////////////////////////////////
623 int wxApp::MainLoop()
630 wxMutexGuiLeaveOrEnter();
631 #endif // wxUSE_THREADS
632 while (!Pending() && ProcessIdle())
644 return (int)svCurrentMsg
.mp1
;
645 } // end of wxApp::MainLoop
647 void wxApp::ExitMainLoop()
649 ::WinPostMsg(NULL
, WM_QUIT
, 0, 0);
650 } // end of wxApp::ExitMainLoop
652 bool wxApp::Pending()
654 return (::WinPeekMsg(vHabmain
, (PQMSG
)&svCurrentMsg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) != 0);
655 } // end of wxApp::Pending
657 void wxApp::Dispatch()
662 //////////////////////////////////////////////////////////////////////////////
664 // Give all windows a chance to preprocess
665 // the message. Some may have accelerator tables, or have
666 // MDI complications.
668 //////////////////////////////////////////////////////////////////////////////
669 bool wxApp::ProcessMessage(
673 QMSG
* pMsg
= (PQMSG
)pWxmsg
;
674 HWND hWnd
= pMsg
->hwnd
;
675 wxWindow
* pWndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
679 // Pass non-system timer messages to the wxTimerProc
681 if (pMsg
->msg
== WM_TIMER
&&
682 (SHORT1FROMMP(pMsg
->mp1
) != TID_CURSOR
&&
683 SHORT1FROMMP(pMsg
->mp1
) != TID_FLASHWINDOW
&&
684 SHORT1FROMMP(pMsg
->mp1
) != TID_SCROLL
&&
685 SHORT1FROMMP(pMsg
->mp1
) != 0x0000
687 wxTimerProc(NULL
, 0, (int)pMsg
->mp1
, 0);
690 // Allow the window to prevent certain messages from being
691 // translated/processed (this is currently used by wxTextCtrl to always
692 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
694 if (pWndThis
&& !pWndThis
->OS2ShouldPreProcessMessage(pWxmsg
))
700 // For some composite controls (like a combobox), wndThis might be NULL
701 // because the subcontrol is not a wxWindow, but only the control itself
702 // is - try to catch this case
704 while (hWnd
&& !pWndThis
)
706 hWnd
= ::WinQueryWindow(hWnd
, QW_PARENT
);
707 pWndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
711 // Try translations first; find the youngest window with
712 // a translation table. OS/2 has case sensative accels, so
713 // this block, coded by BK, removes that and helps make them
716 if(pMsg
->msg
== WM_CHAR
)
718 PBYTE pChmsg
= (PBYTE
)&(pMsg
->msg
);
719 USHORT uSch
= CHARMSG(pChmsg
)->chr
;
723 // Do not process keyup events
725 if(!(CHARMSG(pChmsg
)->fs
& KC_KEYUP
))
727 if((CHARMSG(pChmsg
)->fs
& (KC_ALT
| KC_CTRL
)) && CHARMSG(pChmsg
)->chr
!= 0)
728 CHARMSG(pChmsg
)->chr
= (USHORT
)wxToupper((UCHAR
)uSch
);
731 for(pWnd
= pWndThis
; pWnd
; pWnd
= pWnd
->GetParent() )
733 if((bRc
= pWnd
->OS2TranslateMessage(pWxmsg
)) == TRUE
)
737 if(!bRc
) // untranslated, should restore original value
738 CHARMSG(pChmsg
)->chr
= uSch
;
742 // Anyone for a non-translation message? Try youngest descendants first.
744 // for (pWnd = pWndThis; pWnd; pWnd = pWnd->GetParent())
746 // if (pWnd->OS2ProcessMessage(pWxmsg))
750 } // end of wxApp::ProcessMessage
752 bool gbInOnIdle
= FALSE
;
760 // Avoid recursion (via ProcessEvent default case)
767 wxAppBase::OnIdle(rEvent
);
769 #if wxUSE_DC_CACHEING
770 // automated DC cache management: clear the cached DCs and bitmap
771 // if it's likely that the app has finished with them, that is, we
772 // get an idle event and we're not dragging anything.
773 if (!::WinGetKeyState(HWND_DESKTOP
, VK_BUTTON1
) &&
774 !::WinGetKeyState(HWND_DESKTOP
, VK_BUTTON3
) &&
775 !::WinGetKeyState(HWND_DESKTOP
, VK_BUTTON2
))
777 #endif // wxUSE_DC_CACHEING
780 } // end of wxApp::OnIdle
782 void wxApp::OnEndSession(
783 wxCloseEvent
& WXUNUSED(rEvent
))
786 GetTopWindow()->Close(TRUE
);
787 } // end of wxApp::OnEndSession
790 // Default behaviour: close the application with prompts. The
791 // user can veto the close, and therefore the end session.
793 void wxApp::OnQueryEndSession(
799 if (!GetTopWindow()->Close(!rEvent
.CanVeto()))
802 } // end of wxApp::OnQueryEndSession
808 // VZ: must really exit somehow, insert appropriate OS/2 syscall (FIXME)
809 wxAppConsole::Exit();
813 // Yield to incoming messages
815 bool wxApp::Yield(bool onlyIfNeeded
)
817 static bool s_inYield
= FALSE
;
823 wxFAIL_MSG( _T("wxYield() called recursively") );
833 // Disable log flushing from here because a call to wxYield() shouldn't
834 // normally result in message boxes popping up &c
841 // We want to go back to the main message loop
842 // if we see a WM_QUIT. (?)
844 while (::WinPeekMsg(vHab
, &vMsg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) && vMsg
.msg
!= WM_QUIT
)
847 wxMutexGuiLeaveOrEnter();
848 #endif // wxUSE_THREADS
849 if (!wxTheApp
->DoMessage())
853 // If they are pending events, we must process them.
856 wxTheApp
->ProcessPendingEvents();
860 // Let the logs be flashed again
867 int wxApp::AddSocketHandler(int handle
, int mask
,
868 void (*callback
)(void*), void * gsock
)
871 struct GsocketCallbackInfo
872 *CallbackInfo
= (struct GsocketCallbackInfo
*)m_sockCallbackInfo
;
874 for (find
= 0; find
< m_maxSocketHandles
; find
++)
875 if (CallbackInfo
[find
].handle
== -1)
877 if (find
== m_maxSocketHandles
)
879 // Allocate new memory
880 m_sockCallbackInfo
= realloc(m_sockCallbackInfo
,
881 (m_maxSocketHandles
+=10)*
882 sizeof(struct GsocketCallbackInfo
));
883 CallbackInfo
= (struct GsocketCallbackInfo
*)m_sockCallbackInfo
;
884 for (find
= m_maxSocketHandles
- 10; find
< m_maxSocketHandles
; find
++)
885 CallbackInfo
[find
].handle
= -1;
886 find
= m_maxSocketHandles
- 10;
888 CallbackInfo
[find
].proc
= callback
;
889 CallbackInfo
[find
].type
= mask
;
890 CallbackInfo
[find
].handle
= handle
;
891 CallbackInfo
[find
].gsock
= gsock
;
892 if (mask
& wxSockReadMask
)
893 FD_SET(handle
, &m_readfds
);
894 if (mask
& wxSockWriteMask
)
895 FD_SET(handle
, &m_writefds
);
896 if (handle
>= m_maxSocketNr
)
897 m_maxSocketNr
= handle
+ 1;
901 void wxApp::RemoveSocketHandler(int handle
)
903 struct GsocketCallbackInfo
904 *CallbackInfo
= (struct GsocketCallbackInfo
*)m_sockCallbackInfo
;
905 if (handle
< m_maxSocketHandles
)
907 if (CallbackInfo
[handle
].type
& wxSockReadMask
)
908 FD_CLR(CallbackInfo
[handle
].handle
, &m_readfds
);
909 if (CallbackInfo
[handle
].type
& wxSockWriteMask
)
910 FD_CLR(CallbackInfo
[handle
].handle
, &m_writefds
);
911 CallbackInfo
[handle
].handle
= -1;
915 //-----------------------------------------------------------------------------
917 //-----------------------------------------------------------------------------
919 void wxApp::WakeUpIdle()
922 // Send the top window a dummy message so idle handler processing will
923 // start up again. Doing it this way ensures that the idle handler
924 // wakes up in the right thread (see also wxWakeUpMainThread() which does
925 // the same for the main app thread only)
927 wxWindow
* pTopWindow
= wxTheApp
->GetTopWindow();
931 if ( !::WinPostMsg(GetHwndOf(pTopWindow
), WM_NULL
, (MPARAM
)0, (MPARAM
)0))
934 // Should never happen
936 wxLogLastError("PostMessage(WM_NULL)");
939 } // end of wxWakeUpIdle