1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for MSW
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "toplevel.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/toplevel.h"
34 #include "wx/string.h"
38 #include "wx/control.h"
41 #include "wx/os2/private.h"
43 // ----------------------------------------------------------------------------
44 // stubs for missing functions under MicroWindows
45 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // list of all frames and modeless dialogs
53 wxWindowList wxModelessWindows
;
55 // the name of the default wxWindows class
56 extern void wxAssociateWinWithHandle( HWND hWnd
59 bool wxTopLevelWindowOS2::m_sbInitialized
= FALSE
;
61 // ============================================================================
62 // wxTopLevelWindowMSW implementation
63 // ============================================================================
66 MRESULT EXPENTRY
wxDlgProc( HWND
WXUNUSED(hWnd
)
68 ,MPARAM
WXUNUSED(wParam
)
69 ,MPARAM
WXUNUSED(lParam
)
72 if (uMessage
== WM_INITDLG
)
75 // For this message, returning TRUE tells system to set focus to the
76 // first control in the dialog box.
83 // For all the other ones, FALSE means that we didn't process the
86 return (MRESULT
)FALSE
;
90 // ----------------------------------------------------------------------------
91 // wxTopLevelWindowOS2 creation
92 // ----------------------------------------------------------------------------
94 void wxTopLevelWindowOS2::Init()
96 m_bIconized
= m_bMaximizeOnShow
= FALSE
;
99 // Unlike (almost?) all other windows, frames are created hidden
104 // Data to save/restore when calling ShowFullScreen
106 m_lFsOldWindowStyle
= 0;
107 m_bFsIsMaximized
= FALSE
;
108 m_bFsIsShowing
= FALSE
;
110 m_hFrame
= NULLHANDLE
;
111 memset(&m_vSwp
, 0, sizeof(SWP
));
112 memset(&m_vSwpClient
, 0, sizeof(SWP
));
113 } // end of wxTopLevelWindowIOS2::Init
115 WXDWORD
wxTopLevelWindowOS2::OS2GetStyle(
117 , WXDWORD
* pdwExflags
120 long lMsflags
= wxWindow::OS2GetStyle( (lStyle
& ~wxBORDER_MASK
) | wxBORDER_NONE
124 if (lStyle
== wxDEFAULT_FRAME_STYLE
)
125 lMsflags
|= FCF_SIZEBORDER
| FCF_TITLEBAR
| FCF_SYSMENU
|
126 FCF_MINMAX
| FCF_TASKLIST
;
129 if ((lStyle
& wxCAPTION
) == wxCAPTION
)
130 lMsflags
|= FCF_TASKLIST
;
132 lMsflags
|= FCF_NOMOVEWITHOWNER
;
134 if ((lStyle
& wxVSCROLL
) == wxVSCROLL
)
135 lMsflags
|= FCF_VERTSCROLL
;
136 if ((lStyle
& wxHSCROLL
) == wxHSCROLL
)
137 lMsflags
|= FCF_HORZSCROLL
;
138 if (lStyle
& wxMINIMIZE_BOX
)
139 lMsflags
|= FCF_MINBUTTON
;
140 if (lStyle
& wxMAXIMIZE_BOX
)
141 lMsflags
|= FCF_MAXBUTTON
;
142 if (lStyle
& wxTHICK_FRAME
)
143 lMsflags
|= FCF_DLGBORDER
;
144 if (lStyle
& wxSYSTEM_MENU
)
145 lMsflags
|= FCF_SYSMENU
;
146 if (lStyle
& wxCAPTION
)
147 lMsflags
|= FCF_TASKLIST
;
148 if (lStyle
& wxCLIP_CHILDREN
)
150 // Invalid for frame windows under PM
153 if (lStyle
& wxTINY_CAPTION_VERT
)
154 lMsflags
|= FCF_TASKLIST
;
155 if (lStyle
& wxTINY_CAPTION_HORIZ
)
156 lMsflags
|= FCF_TASKLIST
;
158 if ((lStyle
& wxTHICK_FRAME
) == 0)
159 lMsflags
|= FCF_BORDER
;
160 if (lStyle
& wxFRAME_TOOL_WINDOW
)
161 *pdwExflags
= kFrameToolWindow
;
163 if (lStyle
& wxSTAY_ON_TOP
)
164 lMsflags
|= FCF_SYSMODAL
;
167 } // end of wxTopLevelWindowOS2::OS2GetCreateWindowFlags
169 bool wxTopLevelWindowOS2::CreateDialog(
171 , const wxString
& rsTitle
172 , const wxPoint
& rPos
173 , const wxSize
& rSize
176 wxWindow
* pParent
= GetParent();
179 // For the dialogs without wxDIALOG_NO_PARENT style, use the top level
180 // app window as parent - this avoids creating modal dialogs without
183 if (!pParent
&& !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
))
185 pParent
= wxTheApp
->GetTopWindow();
190 // Don't use transient windows as parents, this is dangerous as it
191 // can lead to a crash if the parent is destroyed before the child
193 // also don't use the window which is currently hidden as then the
194 // dialog would be hidden as well
195 if ((pParent
->GetExtraStyle() & wxWS_EX_TRANSIENT
) ||
207 hWndOwner
= GetHwndOf(pParent
);
209 hWndOwner
= HWND_DESKTOP
;
211 hWndDlg
= ::WinLoadDlg( HWND_DESKTOP
215 ,(ULONG
)ulDlgTemplate
219 m_hWnd
= (WXHWND
) hWndDlg
;
223 wxFAIL_MSG(_("Did you forget to include wx/os2/wx.rc in your resources?"));
225 wxLogSysError(_("Can't create dialog using template '%ul'"), ulDlgTemplate
);
231 // Move the dialog to its initial position without forcing repainting
238 if (!OS2GetCreateWindowCoords( rPos
246 nX
= nWidth
= (int)CW_USEDEFAULT
;
250 // We can't use CW_USEDEFAULT here as we're not calling CreateWindow()
251 // and passing CW_USEDEFAULT to MoveWindow() results in resizing the
252 // window to (0, 0) size which breaks quite a lot of things, e.g. the
253 // sizer calculation in wxSizer::Fit()
255 if (nWidth
== (int)CW_USEDEFAULT
)
258 // The exact number doesn't matter, the dialog will be resized
259 // again soon anyhow but it should be big enough to allow
260 // calculation relying on "totalSize - clientSize > 0" work, i.e.
261 // at least greater than the title bar height
263 nWidth
= nHeight
= 100;
265 if (nX
== (int)CW_USEDEFAULT
)
268 // Centre it on the screen - what else can we do?
270 wxSize vSizeDpy
= wxGetDisplaySize();
272 nX
= (vSizeDpy
.x
- nWidth
) / 2;
273 nY
= (vSizeDpy
.y
- nHeight
) / 2;
275 m_backgroundColour
.Set(wxString("LIGHT GREY"));
277 LONG lColor
= (LONG
)m_backgroundColour
.GetPixel();
279 if (!::WinSetPresParam( m_hWnd
288 ::WinSetWindowPos( GetHwnd()
294 ,SWP_MOVE
| SWP_SIZE
| SWP_ZORDER
| SWP_SHOW
296 ::WinQueryWindowPos(GetHwnd(), GetSwp());
300 } // end of wxTopLevelWindowOS2::CreateDialog
302 bool wxTopLevelWindowOS2::CreateFrame(
303 const wxString
& rsTitle
304 , const wxPoint
& rPos
305 , const wxSize
& rSize
309 WXDWORD lFlags
= OS2GetCreateWindowFlags(&lExflags
);
310 long lStyle
= GetWindowStyleFlag();
313 int nWidth
= rSize
.x
;
314 int nHeight
= rSize
.y
;
315 ULONG ulStyleFlags
= 0L;
318 wxWindow
* pParent
= GetParent();
324 hParent
= GetHwndOf(pParent
);
326 hParent
= HWND_DESKTOP
;
328 if ((lStyle
& wxMINIMIZE
) || (lStyle
& wxICONIZE
))
329 ulStyleFlags
|= WS_MINIMIZED
;
330 if (lStyle
& wxMAXIMIZE
)
331 ulStyleFlags
|= WS_MAXIMIZED
;
334 // Clear the visible flag, we always call show
336 ulStyleFlags
&= (unsigned long)~WS_VISIBLE
;
340 // Create the frame window: We break ranks with other ports now
341 // and instead of calling down into the base wxWindow class' OS2Create
342 // we do all our own stuff here. We will set the needed pieces
343 // of wxWindow manually, here.
346 hFrame
= ::WinCreateStdWindow( hParent
347 ,ulStyleFlags
// frame-window style
348 ,(PULONG
)&lFlags
// window style
349 ,(PSZ
)wxFrameClassName
// class name
350 ,(PSZ
)rsTitle
.c_str() // window title
351 ,0L // default client style
352 ,NULLHANDLE
// resource in executable file
354 ,&hClient
// receives client window handle
358 vError
= ::WinGetLastError(vHabmain
);
359 sError
= wxPMErrorToStr(vError
);
360 wxLogError("Error creating frame. Error: %s\n", sError
);
365 // wxWindow class' m_hWnd set here and needed associations
369 wxAssociateWinWithHandle(m_hWnd
, this);
370 wxAssociateWinWithHandle(m_hFrame
, this);
372 m_backgroundColour
.Set(wxString("MEDIUM GREY"));
374 LONG lColor
= (LONG
)m_backgroundColour
.GetPixel();
376 if (!::WinSetPresParam( m_hWnd
382 vError
= ::WinGetLastError(vHabmain
);
383 sError
= wxPMErrorToStr(vError
);
384 wxLogError("Error creating frame. Error: %s\n", sError
);
389 // Now need to subclass window. Instead of calling the SubClassWin in wxWindow
390 // we manually subclass here because we don't want to use the main wxWndProc
393 m_fnOldWndProc
= (WXFARPROC
) ::WinSubclassWindow(m_hFrame
, (PFNWP
)wxFrameMainWndProc
);
396 // Now size everything. If adding a menu the client will need to be resized.
401 nY
= pParent
->GetSize().y
- (nY
+ nHeight
);
407 ::WinQueryWindowRect(HWND_DESKTOP
, &vRect
);
408 nY
= vRect
.yTop
- (nY
+ nHeight
);
410 if (!::WinSetWindowPos( m_hFrame
416 ,SWP_SIZE
| SWP_MOVE
| SWP_ACTIVATE
| SWP_ZORDER
419 vError
= ::WinGetLastError(vHabmain
);
420 sError
= wxPMErrorToStr(vError
);
421 wxLogError("Error sizing frame. Error: %s\n", sError
);
424 lStyle
= ::WinQueryWindowULong( m_hWnd
427 lStyle
|= WS_CLIPCHILDREN
;
428 ::WinSetWindowULong( m_hWnd
433 } // end of wxTopLevelWindowOS2::CreateFrame
435 bool wxTopLevelWindowOS2::Create(
438 , const wxString
& rsTitle
439 , const wxPoint
& rPos
440 , const wxSize
& rSize
442 , const wxString
& rsName
449 m_windowStyle
= lStyle
;
451 m_windowId
= vId
== -1 ? NewControlId() : vId
;
452 wxTopLevelWindows
.Append(this);
454 pParent
->AddChild(this);
456 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
459 // We have different dialog templates to allows creation of dialogs
460 // with & without captions under OS2indows, resizeable or not (but a
461 // resizeable dialog always has caption - otherwise it would look too
466 if (lStyle
& wxRESIZE_BORDER
)
467 ulDlgTemplate
= (ULONG
)kResizeableDialog
;
468 else if (lStyle
& wxCAPTION
)
469 ulDlgTemplate
= (ULONG
)kCaptionDialog
;
471 ulDlgTemplate
= (ULONG
)kNoCaptionDialog
;
472 return CreateDialog( ulDlgTemplate
480 return CreateFrame( rsTitle
485 } // end of wxTopLevelWindowOS2::Create
487 wxTopLevelWindowOS2::~wxTopLevelWindowOS2()
489 wxTopLevelWindows
.DeleteObject(this);
491 if (wxModelessWindows
.Find(this))
492 wxModelessWindows
.DeleteObject(this);
495 // If this is the last top-level window, exit.
497 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
499 wxTheApp
->SetTopWindow(NULL
);
500 if ( wxTheApp
->GetExitOnFrameDelete() )
502 ::WinPostMsg(NULL
, WM_QUIT
, 0, 0);
505 } // end of wxTopLevelWindowOS2::~wxTopLevelWindowOS2
507 // ----------------------------------------------------------------------------
508 // wxTopLevelWindowOS2 client size
509 // ----------------------------------------------------------------------------
511 void wxTopLevelWindowOS2::DoSetClientSize(
517 // Call GetClientAreaOrigin() to take the toolbar into account
519 wxPoint vPt
= GetClientAreaOrigin();
524 wxWindow::DoSetClientSize( nWidth
527 } // end of wxTopLevelWindowOS2::DoSetClientSize
529 void wxTopLevelWindowOS2::DoGetClientSize(
534 wxWindow::DoGetClientSize( pnX
538 wxPoint vPt
= GetClientAreaOrigin();
545 } // end of wxTopLevelWindowOS2::DoGetClientSize
547 // ----------------------------------------------------------------------------
548 // wxTopLevelWindowOS2 showing
549 // ----------------------------------------------------------------------------
551 void wxTopLevelWindowOS2::DoShowWindow(
555 ::WinShowWindow(m_hFrame
, (BOOL
)(nShowCmd
& SWP_SHOW
));
558 // Need to artificially send a size event as wxApps often expect to do some
559 // final child control sizing
561 m_bIconized
= nShowCmd
== SWP_MINIMIZE
;
562 } // end of wxTopLevelWindowOS2::DoShowWindow
564 bool wxTopLevelWindowOS2::Show(
574 if (m_bMaximizeOnShow
)
576 nShowCmd
= SWP_MAXIMIZE
;
577 m_bMaximizeOnShow
= FALSE
;
588 DoShowWindow(nShowCmd
);
592 wxActivateEvent
vEvent(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
594 ::WinQueryWindowPos(m_hFrame
, &vSwp
);
595 m_bIconized
= vSwp
.fl
& SWP_MINIMIZE
;
596 ::WinQueryWindowPos(m_hWnd
, &m_vSwpClient
);
597 ::WinSendMsg(m_hFrame
, WM_UPDATEFRAME
, (MPARAM
)~0, 0);
598 ::WinQueryWindowPos(m_hWnd
, &vSwp
);
599 ::WinEnableWindow(m_hFrame
, TRUE
);
602 // Deal with children
604 MoveChildren(m_vSwpClient
.cy
- vSwp
.cy
);
605 vEvent
.SetEventObject(this);
606 GetEventHandler()->ProcessEvent(vEvent
);
611 // Try to highlight the correct window (the parent)
615 HWND hWndParent
= GetHwndOf(GetParent());
617 ::WinQueryWindowPos(hWndParent
, &vSwp
);
618 m_bIconized
= vSwp
.fl
& SWP_MINIMIZE
;
620 ::WinSetWindowPos( hWndParent
626 ,SWP_ZORDER
| SWP_ACTIVATE
| SWP_SHOW
| SWP_MOVE
628 ::WinEnableWindow(hWndParent
, TRUE
);
632 } // end of wxTopLevelWindowOS2::Show
634 // ----------------------------------------------------------------------------
635 // wxTopLevelWindowOS2 maximize/minimize
636 // ----------------------------------------------------------------------------
638 void wxTopLevelWindowOS2::Maximize(
645 // Just maximize it directly
647 DoShowWindow(bMaximize
? SWP_MAXIMIZE
: SWP_RESTORE
);
652 // We can't maximize the hidden frame because it shows it as well, so
653 // just remember that we should do it later in this case
655 m_bMaximizeOnShow
= TRUE
;
657 } // end of wxTopLevelWindowOS2::Maximize
659 bool wxTopLevelWindowOS2::IsMaximized() const
663 ::WinQueryWindowPos(m_hFrame
, (PSWP
)&m_vSwp
);
664 return (m_vSwp
.fl
& SWP_MAXIMIZE
);
665 } // end of wxTopLevelWindowOS2::IsMaximized
667 void wxTopLevelWindowOS2::Iconize(
671 DoShowWindow(bIconize
? SWP_MINIMIZE
: SWP_RESTORE
);
672 } // end of wxTopLevelWindowOS2::Iconize
674 bool wxTopLevelWindowOS2::IsIconized() const
676 // also update the current state
677 ::WinQueryWindowPos(m_hFrame
, (PSWP
)&m_vSwp
);
678 if (m_vSwp
.fl
& SWP_MINIMIZE
)
679 ((wxTopLevelWindow
*)this)->m_bIconized
= TRUE
;
681 ((wxTopLevelWindow
*)this)->m_bIconized
= FALSE
;
683 } // end of wxTopLevelWindowOS2::IsIconized
685 void wxTopLevelWindowOS2::Restore()
687 DoShowWindow(SWP_RESTORE
);
688 } // end of wxTopLevelWindowOS2::Restore
690 // generate an artificial resize event
691 void wxTopLevelWindowOS2::SendSizeEvent()
695 RECTL vRect
= wxGetWindowRect(GetHwnd());
697 (void)::WinPostMsg( m_hFrame
699 ,MPFROM2SHORT(vRect
.xRight
- vRect
.xLeft
, vRect
.yTop
- vRect
.yBottom
)
700 ,MPFROM2SHORT(vRect
.xRight
- vRect
.xLeft
, vRect
.yTop
- vRect
.yBottom
)
703 } // end of wxTopLevelWindowOS2::SendSizeEvent
705 // ----------------------------------------------------------------------------
706 // wxTopLevelWindowOS2 fullscreen
707 // ----------------------------------------------------------------------------
709 bool wxTopLevelWindowOS2::ShowFullScreen(
719 m_bFsIsShowing
= TRUE
;
723 // Zap the frame borders
727 // Save the 'normal' window lStyle
729 m_lFsOldWindowStyle
= ::WinQueryWindowULong( (HWND
)GetHWND()
734 // Save the old position, width & height, maximize state
736 m_vFsOldSize
= GetRect();
737 m_bFsIsMaximized
= IsMaximized();
740 // Decide which window lStyle flags to turn off
742 LONG lNewStyle
= m_lFsOldWindowStyle
;
745 if (lStyle
& wxFULLSCREEN_NOBORDER
)
746 lOffFlags
|= FCF_BORDER
;
747 if (lStyle
& wxFULLSCREEN_NOCAPTION
)
748 lOffFlags
|= (FCF_TASKLIST
| FCF_SYSMENU
);
750 lNewStyle
&= (~lOffFlags
);
753 // Change our window style to be compatible with full-screen mode
755 ::WinSetWindowULong( (HWND
)GetHWND()
761 // Resize to the size of the desktop
765 RECTL vRect
= wxGetWindowRect(HWND_DESKTOP
);
767 nWidth
= vRect
.xRight
- vRect
.xLeft
;
768 nHeight
= vRect
.yTop
- vRect
.yBottom
;
775 // Now flush the window style cache and actually go full-screen
777 ::WinSetWindowPos( m_hFrame
786 wxSizeEvent
vEvent( wxSize( nWidth
792 GetEventHandler()->ProcessEvent(vEvent
);
800 m_bFsIsShowing
= FALSE
;
801 Maximize(m_bFsIsMaximized
);
802 ::WinSetWindowULong( (HWND
)GetHWND()
806 ::WinSetWindowPos( m_hFrame
816 } // end of wxTopLevelWindowOS2::ShowFullScreen
818 // ----------------------------------------------------------------------------
819 // wxTopLevelWindowOS2 misc
820 // ----------------------------------------------------------------------------
822 void wxTopLevelWindowOS2::SetIcon(
829 wxTopLevelWindowBase::SetIcon(rIcon
);
833 ::WinSendMsg( m_hFrame
835 ,(MPARAM
)((HPOINTER
)m_icon
.GetHICON())
838 ::WinSendMsg( m_hFrame
844 } // end of wxTopLevelWindowOS2::SetIcon
846 bool wxTopLevelWindowOS2::EnableCloseButton(
851 // Get system (a.k.a. window) menu
853 HMENU hMenu
= ::WinWindowFromID(m_hFrame
, FID_SYSMENU
);
857 wxLogLastError(_T("GetSystemMenu"));
862 // Enabling/disabling the close item from it also automatically
863 // disables/enables the close title bar button
866 (void)::WinSendMsg( hMenu
868 ,MPFROM2SHORT(SC_CLOSE
, FALSE
)
869 ,MPFROM2SHORT(MIA_DISABLED
, FALSE
)
872 (void)::WinSendMsg( hMenu
874 ,MPFROM2SHORT(SC_CLOSE
, FALSE
)
875 ,MPFROM2SHORT(MIA_DISABLED
, MIA_DISABLED
)
879 // Update appearance immediately
881 ::WinSendMsg( m_hFrame
887 } // end of wxTopLevelWindowOS2::EnableCloseButton