1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/app.cpp
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
18 #include "wx/dynarray.h"
21 #include "wx/gdicmn.h"
24 #include "wx/cursor.h"
26 #include "wx/palette.h"
28 #include "wx/dialog.h"
29 #include "wx/msgdlg.h"
31 #include "wx/wxchar.h"
32 #include "wx/stdpaths.h"
33 #include "wx/filename.h"
37 #include "wx/module.h"
39 #include "wx/os2/private.h"
43 #include <sys/ioctl.h>
44 #include <sys/select.h>
49 #include <sys/ioctl.h>
50 #include <sys/select.h>
55 #if defined(__WATCOMC__)
59 #elif !defined(__EMX__)
61 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
62 extern "C" int _System
bsdselect(int,
70 #include "wx/thread.h"
71 #endif // wxUSE_THREADS
74 #include "wx/tooltip.h"
75 #endif // wxUSE_TOOLTIPS
80 // ---------------------------------------------------------------------------
82 // ---------------------------------------------------------------------------
84 extern wxChar
* wxBuffer
;
85 extern wxList WXDLLEXPORT wxPendingDelete
;
86 extern wxCursor
* g_globalCursor
;
88 HAB vHabmain
= NULLHANDLE
;
91 HICON wxSTD_FRAME_ICON
= (HICON
) NULL
;
92 HICON wxSTD_MDICHILDFRAME_ICON
= (HICON
) NULL
;
93 HICON wxSTD_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
95 HICON wxDEFAULT_FRAME_ICON
= (HICON
) NULL
;
96 HICON wxDEFAULT_MDICHILDFRAME_ICON
= (HICON
) NULL
;
97 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
99 HBRUSH wxDisableButtonBrush
= (HBRUSH
) 0;
101 MRESULT EXPENTRY
wxWndProc( HWND hWnd
,ULONG message
,MPARAM mp1
,MPARAM mp2
);
102 MRESULT EXPENTRY
wxFrameWndProc( HWND hWnd
,ULONG message
,MPARAM mp1
,MPARAM mp2
);
104 // ===========================================================================
106 // ===========================================================================
108 // ---------------------------------------------------------------------------
109 // helper struct and functions for socket handling
110 // ---------------------------------------------------------------------------
112 struct GsocketCallbackInfo
{
113 void (*proc
)(void *);
119 // These defines are used here and in gsockpm.cpp
120 #define wxSockReadMask 0x01
121 #define wxSockWriteMask 0x02
123 void wxApp::HandleSockets()
125 bool pendingEvent
= false;
127 // Check whether it's time for Gsocket operation
128 if (m_maxSocketHandles
> 0 && m_maxSocketNr
> 0)
130 fd_set readfds
= m_readfds
;
131 fd_set writefds
= m_writefds
;
132 struct timeval timeout
;
134 struct GsocketCallbackInfo
135 *CallbackInfo
= (struct GsocketCallbackInfo
*)m_sockCallbackInfo
;
138 if ( select(m_maxSocketNr
, &readfds
, &writefds
, 0, &timeout
) > 0)
140 for (i
= m_lastUsedHandle
+ 1; i
!= m_lastUsedHandle
;
141 (i
< m_maxSocketNr
- 1) ? i
++ : (i
= 0))
143 if (FD_ISSET(i
, &readfds
))
146 for (r
= 0; r
< m_maxSocketHandles
; r
++){
147 if(CallbackInfo
[r
].handle
== i
&&
148 CallbackInfo
[r
].type
== wxSockReadMask
)
151 if (r
< m_maxSocketHandles
)
153 CallbackInfo
[r
].proc(CallbackInfo
[r
].gsock
);
157 if (FD_ISSET(i
, &writefds
))
160 for (r
= 0; r
< m_maxSocketHandles
; r
++)
161 if(CallbackInfo
[r
].handle
== i
&&
162 CallbackInfo
[r
].type
== wxSockWriteMask
)
164 if (r
< m_maxSocketHandles
)
166 CallbackInfo
[r
].proc(CallbackInfo
[r
].gsock
);
171 m_lastUsedHandle
= i
;
174 ProcessPendingEvents();
177 // ---------------------------------------------------------------------------
179 // ---------------------------------------------------------------------------
181 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
183 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
184 EVT_IDLE(wxApp::OnIdle
)
185 EVT_END_SESSION(wxApp::OnEndSession
)
186 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
192 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
194 if ( !wxAppBase::Initialize(argc
, argv
) )
197 #if defined(wxUSE_CONSOLEDEBUG)
198 #if wxUSE_CONSOLEDEBUG
199 /***********************************************/
200 /* Code for using stdout debug */
201 /* To use it you mast link app as "Window" - EK*/
202 /***********************************************/
207 printf("In console\n");
209 DosGetInfoBlocks(&tib
, &pib
);
210 /* Try morphing into a PM application. */
211 // if(pib->pib_ultype == 2) /* VIO */
214 /**********************************************/
215 /**********************************************/
216 #endif //wxUSE_CONSOLEDEBUG
220 // OS2 has to have an anchorblock
222 vHabmain
= WinInitialize(0);
223 wxFileName
GetPrefix(argv
[0]);
224 GetPrefix
.MakeAbsolute();
225 wxStandardPaths::SetInstallPrefix(GetPrefix
.GetPath());
228 // TODO: at least give some error message here...
229 wxAppBase::CleanUp();
234 wxBuffer
= new wxChar
[1500]; // FIXME; why?
236 // Some people may wish to use this, but
237 // probably it shouldn't be here by default.
239 // wxRedirectIOToConsole();
242 wxWinHandleHash
= new wxWinHashTable(wxKEY_INTEGER
, 100);
244 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
245 // PLEASE DO NOT ALTER THIS.
246 #if !defined(WXMAKINGDLL) && defined(__VISAGECPP__)
247 extern char wxDummyChar
;
248 if (wxDummyChar
) wxDummyChar
++;
251 // wxSetKeyboardHook(TRUE);
253 RegisterWindowClasses(vHabmain
);
256 } // end of wxApp::Initialize
258 const char* CANTREGISTERCLASS
= " Can't register Class ";
259 // ---------------------------------------------------------------------------
260 // RegisterWindowClasses
261 // ---------------------------------------------------------------------------
263 bool wxApp::RegisterWindowClasses( HAB vHab
)
268 if (!::WinRegisterClass( vHab
269 ,(PSZ
)wxFrameClassName
271 ,CS_SIZEREDRAW
| CS_SYNCPAINT
275 vError
= ::WinGetLastError(vHab
);
276 sError
= wxPMErrorToStr(vError
);
277 wxLogLastError(sError
.c_str());
281 if (!::WinRegisterClass( vHab
282 ,(PSZ
)wxFrameClassNameNoRedraw
288 vError
= ::WinGetLastError(vHab
);
289 sError
= wxPMErrorToStr(vError
);
290 wxLogLastError(sError
.c_str());
294 if (!::WinRegisterClass( vHab
295 ,(PSZ
)wxMDIFrameClassName
297 ,CS_SIZEREDRAW
| CS_MOVENOTIFY
| CS_SYNCPAINT
301 vError
= ::WinGetLastError(vHab
);
302 sError
= wxPMErrorToStr(vError
);
303 wxLogLastError(sError
.c_str());
307 if (!::WinRegisterClass( vHab
308 ,(PSZ
)wxMDIFrameClassNameNoRedraw
314 vError
= ::WinGetLastError(vHab
);
315 sError
= wxPMErrorToStr(vError
);
316 wxLogLastError(sError
.c_str());
320 if (!::WinRegisterClass( vHab
321 ,(PSZ
)wxMDIChildFrameClassName
323 ,CS_MOVENOTIFY
| CS_SIZEREDRAW
| CS_SYNCPAINT
| CS_HITTEST
327 vError
= ::WinGetLastError(vHab
);
328 sError
= wxPMErrorToStr(vError
);
329 wxLogLastError(sError
.c_str());
333 if (!::WinRegisterClass( vHab
334 ,(PSZ
)wxMDIChildFrameClassNameNoRedraw
340 vError
= ::WinGetLastError(vHab
);
341 sError
= wxPMErrorToStr(vError
);
342 wxLogLastError(sError
.c_str());
346 if (!::WinRegisterClass( vHab
347 ,(PSZ
)wxPanelClassName
349 ,CS_MOVENOTIFY
| CS_SIZEREDRAW
| CS_HITTEST
| CS_SAVEBITS
| CS_SYNCPAINT
353 vError
= ::WinGetLastError(vHab
);
354 sError
= wxPMErrorToStr(vError
);
355 wxLogLastError(sError
.c_str());
359 if (!::WinRegisterClass( vHab
360 ,(PSZ
)wxCanvasClassName
362 ,CS_SIZEREDRAW
| CS_HITTEST
| CS_SYNCPAINT
366 vError
= ::WinGetLastError(vHab
);
367 sError
= wxPMErrorToStr(vError
);
368 wxLogLastError(sError
.c_str());
371 if (!::WinRegisterClass( vHab
372 ,(PSZ
)wxCanvasClassNameNR
374 ,CS_HITTEST
| CS_SYNCPAINT
378 vError
= ::WinGetLastError(vHab
);
379 sError
= wxPMErrorToStr(vError
);
380 wxLogLastError(sError
.c_str());
384 } // end of wxApp::RegisterWindowClasses
387 // Cleans up any wxWidgets internal structures left lying around
389 void wxApp::CleanUp()
395 // PM-SPECIFIC CLEANUP
398 // wxSetKeyboardHook(false);
400 if (wxSTD_FRAME_ICON
)
401 ::WinFreeFileIcon(wxSTD_FRAME_ICON
);
402 if (wxSTD_MDICHILDFRAME_ICON
)
403 ::WinFreeFileIcon(wxSTD_MDICHILDFRAME_ICON
);
404 if (wxSTD_MDIPARENTFRAME_ICON
)
405 ::WinFreeFileIcon(wxSTD_MDIPARENTFRAME_ICON
);
407 if (wxDEFAULT_FRAME_ICON
)
408 ::WinFreeFileIcon(wxDEFAULT_FRAME_ICON
);
409 if (wxDEFAULT_MDICHILDFRAME_ICON
)
410 ::WinFreeFileIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
411 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
412 ::WinFreeFileIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
414 if ( wxDisableButtonBrush
)
416 // TODO: ::DeleteObject( wxDisableButtonBrush );
419 delete wxWinHandleHash
;
420 wxWinHandleHash
= NULL
;
422 // Delete Message queue
424 ::WinDestroyMsgQueue(wxTheApp
->m_hMq
);
426 wxAppBase::CleanUp();
427 } // end of wxApp::CleanUp
429 bool wxApp::OnInitGui()
434 if (!wxAppBase::OnInitGui())
437 m_hMq
= ::WinCreateMsgQueue(vHabmain
, 0);
440 vError
= ::WinGetLastError(vHabmain
);
441 sError
= wxPMErrorToStr(vError
);
447 } // end of wxApp::OnInitGui
453 m_nPrintMode
= wxPRINT_WINDOWS
;
455 m_maxSocketHandles
= 0;
457 m_sockCallbackInfo
= 0;
458 } // end of wxApp::wxApp
463 // Delete command-line args
468 for (i
= 0; i
< argc
; i
++)
474 } // end of wxApp::~wxApp
476 bool gbInOnIdle
= false;
478 void wxApp::OnIdle( wxIdleEvent
& rEvent
)
481 // Avoid recursion (via ProcessEvent default case)
488 wxAppBase::OnIdle(rEvent
);
490 #if wxUSE_DC_CACHEING
491 // automated DC cache management: clear the cached DCs and bitmap
492 // if it's likely that the app has finished with them, that is, we
493 // get an idle event and we're not dragging anything.
494 if (!::WinGetKeyState(HWND_DESKTOP
, VK_BUTTON1
) &&
495 !::WinGetKeyState(HWND_DESKTOP
, VK_BUTTON3
) &&
496 !::WinGetKeyState(HWND_DESKTOP
, VK_BUTTON2
))
498 #endif // wxUSE_DC_CACHEING
501 } // end of wxApp::OnIdle
503 void wxApp::OnEndSession(
504 wxCloseEvent
& WXUNUSED(rEvent
))
507 GetTopWindow()->Close(true);
508 } // end of wxApp::OnEndSession
511 // Default behaviour: close the application with prompts. The
512 // user can veto the close, and therefore the end session.
514 void wxApp::OnQueryEndSession( wxCloseEvent
& rEvent
)
518 if (!GetTopWindow()->Close(!rEvent
.CanVeto()))
521 } // end of wxApp::OnQueryEndSession
524 // Yield to incoming messages
526 bool wxApp::Yield(bool onlyIfNeeded
)
528 static bool s_inYield
= false;
534 wxFAIL_MSG( _T("wxYield() called recursively") );
544 // Disable log flushing from here because a call to wxYield() shouldn't
545 // normally result in message boxes popping up &c
552 // We want to go back to the main message loop
553 // if we see a WM_QUIT. (?)
555 while (::WinPeekMsg(vHab
, &vMsg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) && vMsg
.msg
!= WM_QUIT
)
558 wxMutexGuiLeaveOrEnter();
559 #endif // wxUSE_THREADS
560 if (!wxTheApp
->Dispatch())
564 // If they are pending events, we must process them.
567 wxTheApp
->ProcessPendingEvents();
571 // Let the logs be flashed again
578 int wxApp::AddSocketHandler(int handle
, int mask
,
579 void (*callback
)(void*), void * gsock
)
582 struct GsocketCallbackInfo
583 *CallbackInfo
= (struct GsocketCallbackInfo
*)m_sockCallbackInfo
;
585 for (find
= 0; find
< m_maxSocketHandles
; find
++)
586 if (CallbackInfo
[find
].handle
== -1)
588 if (find
== m_maxSocketHandles
)
590 // Allocate new memory
591 m_sockCallbackInfo
= realloc(m_sockCallbackInfo
,
592 (m_maxSocketHandles
+=10)*
593 sizeof(struct GsocketCallbackInfo
));
594 CallbackInfo
= (struct GsocketCallbackInfo
*)m_sockCallbackInfo
;
595 for (find
= m_maxSocketHandles
- 10; find
< m_maxSocketHandles
; find
++)
596 CallbackInfo
[find
].handle
= -1;
597 find
= m_maxSocketHandles
- 10;
599 CallbackInfo
[find
].proc
= callback
;
600 CallbackInfo
[find
].type
= mask
;
601 CallbackInfo
[find
].handle
= handle
;
602 CallbackInfo
[find
].gsock
= gsock
;
603 if (mask
& wxSockReadMask
)
604 FD_SET(handle
, &m_readfds
);
605 if (mask
& wxSockWriteMask
)
606 FD_SET(handle
, &m_writefds
);
607 if (handle
>= m_maxSocketNr
)
608 m_maxSocketNr
= handle
+ 1;
612 void wxApp::RemoveSocketHandler(int handle
)
614 struct GsocketCallbackInfo
615 *CallbackInfo
= (struct GsocketCallbackInfo
*)m_sockCallbackInfo
;
616 if (handle
< m_maxSocketHandles
)
618 if (CallbackInfo
[handle
].type
& wxSockReadMask
)
619 FD_CLR(CallbackInfo
[handle
].handle
, &m_readfds
);
620 if (CallbackInfo
[handle
].type
& wxSockWriteMask
)
621 FD_CLR(CallbackInfo
[handle
].handle
, &m_writefds
);
622 CallbackInfo
[handle
].handle
= -1;
626 //-----------------------------------------------------------------------------
628 //-----------------------------------------------------------------------------
630 void wxApp::WakeUpIdle()
633 // Send the top window a dummy message so idle handler processing will
634 // start up again. Doing it this way ensures that the idle handler
635 // wakes up in the right thread (see also wxWakeUpMainThread() which does
636 // the same for the main app thread only)
638 wxWindow
* pTopWindow
= wxTheApp
->GetTopWindow();
642 if ( !::WinPostMsg(GetHwndOf(pTopWindow
), WM_NULL
, (MPARAM
)0, (MPARAM
)0))
645 // Should never happen
647 wxLogLastError(wxT("PostMessage(WM_NULL)"));
650 } // end of wxWakeUpIdle
657 void wxSetInstance( HAB vHab
)