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(vHab
);
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 wxPendingEvents
;
457 delete wxPendingEventsLocker
;
458 // If we don't do the following, we get an apparent memory leak.
459 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
462 // Delete Message queue
464 ::WinDestroyMsgQueue(wxTheApp
->m_hMq
);
466 wxAppBase::CleanUp();
467 } // end of wxApp::CleanUp
469 bool wxApp::OnInitGui()
474 if (!wxAppBase::OnInitGui())
477 m_hMq
= ::WinCreateMsgQueue(vHabmain
, 0);
480 vError
= ::WinGetLastError(vHabmain
);
481 sError
= wxPMErrorToStr(vError
);
487 } // end of wxApp::OnInitGui
496 m_nPrintMode
= wxPRINT_WINDOWS
;
498 m_maxSocketHandles
= 0;
500 m_sockCallbackInfo
= 0;
501 } // end of wxApp::wxApp
506 // Delete command-line args
511 for (i
= 0; i
< argc
; i
++)
517 } // end of wxApp::~wxApp
519 bool wxApp::Initialized()
525 } // end of wxApp::Initialized
528 // Get and process a message, returning FALSE if WM_QUIT
529 // received (and also set the flag telling the app to exit the main loop)
532 bool wxApp::DoMessage()
534 BOOL bRc
= ::WinGetMsg(vHabmain
, &svCurrentMsg
, HWND(NULL
), 0, 0);
539 m_bKeepGoing
= FALSE
;
544 // should never happen, but let's test for it nevertheless
545 wxLogLastError("GetMessage");
550 wxASSERT_MSG( wxThread::IsMain()
551 ,wxT("only the main thread can process Windows messages")
554 static bool sbHadGuiLock
= TRUE
;
555 static wxMsgArray svSavedMessages
;
558 // If a secondary thread owns is doing GUI calls, save all messages for
559 // later processing - we can't process them right now because it will
560 // lead to recursive library calls (and we're not reentrant)
562 if (!wxGuiOwnedByMainThread())
564 sbHadGuiLock
= FALSE
;
567 // Leave out WM_COMMAND messages: too dangerous, sometimes
568 // the message will be processed twice
570 if ( !wxIsWaitingForThread() ||
571 svCurrentMsg
.msg
!= WM_COMMAND
)
573 svSavedMessages
.Add(svCurrentMsg
);
580 // Have we just regained the GUI lock? if so, post all of the saved
587 size_t nCount
= svSavedMessages
.Count();
589 for (size_t n
= 0; n
< nCount
; n
++)
591 QMSG vMsg
= svSavedMessages
[n
];
593 DoMessage((WXMSG
*)&vMsg
);
595 svSavedMessages
.Empty();
598 #endif // wxUSE_THREADS
601 // Process the message
603 DoMessage((WXMSG
*)&svCurrentMsg
);
606 } // end of wxApp::DoMessage
608 void wxApp::DoMessage(
612 if (!ProcessMessage((WXMSG
*)&svCurrentMsg
))
614 ::WinDispatchMsg(vHabmain
, (PQMSG
)&svCurrentMsg
);
616 } // end of wxApp::DoMessage
618 //////////////////////////////////////////////////////////////////////////////
620 // Keep trying to process messages until WM_QUIT
623 // If there are messages to be processed, they will all be
624 // processed and OnIdle will not be called.
625 // When there are no more messages, OnIdle is called.
626 // If OnIdle requests more time,
627 // it will be repeatedly called so long as there are no pending messages.
628 // A 'feature' of this is that once OnIdle has decided that no more processing
629 // is required, then it won't get processing time until further messages
630 // are processed (it'll sit in DoMessage).
632 //////////////////////////////////////////////////////////////////////////////
633 int wxApp::MainLoop()
640 wxMutexGuiLeaveOrEnter();
641 #endif // wxUSE_THREADS
642 while (!Pending() && ProcessIdle())
654 return (int)svCurrentMsg
.mp1
;
655 } // end of wxApp::MainLoop
657 void wxApp::ExitMainLoop()
659 ::WinPostMsg(NULL
, WM_QUIT
, 0, 0);
660 } // end of wxApp::ExitMainLoop
662 bool wxApp::Pending()
664 return (::WinPeekMsg(vHabmain
, (PQMSG
)&svCurrentMsg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) != 0);
665 } // end of wxApp::Pending
667 void wxApp::Dispatch()
672 //////////////////////////////////////////////////////////////////////////////
674 // Give all windows a chance to preprocess
675 // the message. Some may have accelerator tables, or have
676 // MDI complications.
678 //////////////////////////////////////////////////////////////////////////////
679 bool wxApp::ProcessMessage(
683 QMSG
* pMsg
= (PQMSG
)pWxmsg
;
684 HWND hWnd
= pMsg
->hwnd
;
685 wxWindow
* pWndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
689 // Pass non-system timer messages to the wxTimerProc
691 if (pMsg
->msg
== WM_TIMER
&&
692 (SHORT1FROMMP(pMsg
->mp1
) != TID_CURSOR
&&
693 SHORT1FROMMP(pMsg
->mp1
) != TID_FLASHWINDOW
&&
694 SHORT1FROMMP(pMsg
->mp1
) != TID_SCROLL
&&
695 SHORT1FROMMP(pMsg
->mp1
) != 0x0000
697 wxTimerProc(NULL
, 0, (int)pMsg
->mp1
, 0);
700 // Allow the window to prevent certain messages from being
701 // translated/processed (this is currently used by wxTextCtrl to always
702 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
704 if (pWndThis
&& !pWndThis
->OS2ShouldPreProcessMessage(pWxmsg
))
710 // For some composite controls (like a combobox), wndThis might be NULL
711 // because the subcontrol is not a wxWindow, but only the control itself
712 // is - try to catch this case
714 while (hWnd
&& !pWndThis
)
716 hWnd
= ::WinQueryWindow(hWnd
, QW_PARENT
);
717 pWndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
721 // Try translations first; find the youngest window with
722 // a translation table. OS/2 has case sensative accels, so
723 // this block, coded by BK, removes that and helps make them
726 if(pMsg
->msg
== WM_CHAR
)
728 PBYTE pChmsg
= (PBYTE
)&(pMsg
->msg
);
729 USHORT uSch
= CHARMSG(pChmsg
)->chr
;
733 // Do not process keyup events
735 if(!(CHARMSG(pChmsg
)->fs
& KC_KEYUP
))
737 if((CHARMSG(pChmsg
)->fs
& (KC_ALT
| KC_CTRL
)) && CHARMSG(pChmsg
)->chr
!= 0)
738 CHARMSG(pChmsg
)->chr
= (USHORT
)wxToupper((UCHAR
)uSch
);
741 for(pWnd
= pWndThis
; pWnd
; pWnd
= pWnd
->GetParent() )
743 if((bRc
= pWnd
->OS2TranslateMessage(pWxmsg
)) == TRUE
)
747 if(!bRc
) // untranslated, should restore original value
748 CHARMSG(pChmsg
)->chr
= uSch
;
752 // Anyone for a non-translation message? Try youngest descendants first.
754 // for (pWnd = pWndThis; pWnd; pWnd = pWnd->GetParent())
756 // if (pWnd->OS2ProcessMessage(pWxmsg))
760 } // end of wxApp::ProcessMessage
762 bool gbInOnIdle
= FALSE
;
770 // Avoid recursion (via ProcessEvent default case)
777 wxAppBase::OnIdle(event
);
779 #if wxUSE_DC_CACHEING
780 // automated DC cache management: clear the cached DCs and bitmap
781 // if it's likely that the app has finished with them, that is, we
782 // get an idle event and we're not dragging anything.
783 if (!::WinGetKeyState(HWND_DESKTOP
, VK_BUTTON1
) &&
784 !::WinGetKeyState(HWND_DESKTOP
, VK_BUTTON3
) &&
785 !::WinGetKeyState(HWND_DESKTOP
, VK_BUTTON2
))
787 #endif // wxUSE_DC_CACHEING
790 } // end of wxApp::OnIdle
792 void wxApp::OnEndSession(
793 wxCloseEvent
& WXUNUSED(rEvent
))
796 GetTopWindow()->Close(TRUE
);
797 } // end of wxApp::OnEndSession
800 // Default behaviour: close the application with prompts. The
801 // user can veto the close, and therefore the end session.
803 void wxApp::OnQueryEndSession(
809 if (!GetTopWindow()->Close(!rEvent
.CanVeto()))
812 } // end of wxApp::OnQueryEndSession
818 // VZ: must really exit somehow, insert appropriate OS/2 syscall (FIXME)
819 wxAppConsole::Exit();
823 // Yield to incoming messages
825 bool wxApp::Yield(bool onlyIfNeeded
)
827 static bool s_inYield
= FALSE
;
833 wxFAIL_MSG( _T("wxYield() called recursively") );
843 // Disable log flushing from here because a call to wxYield() shouldn't
844 // normally result in message boxes popping up &c
851 // We want to go back to the main message loop
852 // if we see a WM_QUIT. (?)
854 while (::WinPeekMsg(vHab
, &vMsg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) && vMsg
.msg
!= WM_QUIT
)
857 wxMutexGuiLeaveOrEnter();
858 #endif // wxUSE_THREADS
859 if (!wxTheApp
->DoMessage())
863 // If they are pending events, we must process them.
866 wxTheApp
->ProcessPendingEvents();
870 // Let the logs be flashed again
877 int wxApp::AddSocketHandler(int handle
, int mask
,
878 void (*callback
)(void*), void * gsock
)
881 struct GsocketCallbackInfo
882 *CallbackInfo
= (struct GsocketCallbackInfo
*)m_sockCallbackInfo
;
884 for (find
= 0; find
< m_maxSocketHandles
; find
++)
885 if (CallbackInfo
[find
].handle
== -1)
887 if (find
== m_maxSocketHandles
)
889 // Allocate new memory
890 m_sockCallbackInfo
= realloc(m_sockCallbackInfo
,
891 (m_maxSocketHandles
+=10)*
892 sizeof(struct GsocketCallbackInfo
));
893 CallbackInfo
= (struct GsocketCallbackInfo
*)m_sockCallbackInfo
;
894 for (find
= m_maxSocketHandles
- 10; find
< m_maxSocketHandles
; find
++)
895 CallbackInfo
[find
].handle
= -1;
896 find
= m_maxSocketHandles
- 10;
898 CallbackInfo
[find
].proc
= callback
;
899 CallbackInfo
[find
].type
= mask
;
900 CallbackInfo
[find
].handle
= handle
;
901 CallbackInfo
[find
].gsock
= gsock
;
902 if (mask
& wxSockReadMask
)
903 FD_SET(handle
, &m_readfds
);
904 if (mask
& wxSockWriteMask
)
905 FD_SET(handle
, &m_writefds
);
906 if (handle
>= m_maxSocketNr
)
907 m_maxSocketNr
= handle
+ 1;
911 void wxApp::RemoveSocketHandler(int handle
)
913 struct GsocketCallbackInfo
914 *CallbackInfo
= (struct GsocketCallbackInfo
*)m_sockCallbackInfo
;
915 if (handle
< m_maxSocketHandles
)
917 if (CallbackInfo
[handle
].type
& wxSockReadMask
)
918 FD_CLR(CallbackInfo
[handle
].handle
, &m_readfds
);
919 if (CallbackInfo
[handle
].type
& wxSockWriteMask
)
920 FD_CLR(CallbackInfo
[handle
].handle
, &m_writefds
);
921 CallbackInfo
[handle
].handle
= -1;
925 //-----------------------------------------------------------------------------
927 //-----------------------------------------------------------------------------
929 void wxApp::WakeUpIdle()
932 // Send the top window a dummy message so idle handler processing will
933 // start up again. Doing it this way ensures that the idle handler
934 // wakes up in the right thread (see also wxWakeUpMainThread() which does
935 // the same for the main app thread only)
937 wxWindow
* pTopWindow
= wxTheApp
->GetTopWindow();
941 if ( !::WinPostMsg(GetHwndOf(pTopWindow
), WM_NULL
, (MPARAM
)0, (MPARAM
)0))
944 // Should never happen
946 wxLogLastError("PostMessage(WM_NULL)");
949 } // end of wxWakeUpIdle