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
;
60 wxWindow
* wxTopLevelWindowOS2::m_spHiddenParent
= NULL
;
62 // ============================================================================
63 // wxTopLevelWindowMSW implementation
64 // ============================================================================
67 MRESULT EXPENTRY
wxDlgProc( HWND
WXUNUSED(hWnd
)
69 ,MPARAM
WXUNUSED(wParam
)
70 ,MPARAM
WXUNUSED(lParam
)
73 if (uMessage
== WM_INITDLG
)
76 // For this message, returning TRUE tells system to set focus to the
77 // first control in the dialog box.
84 // For all the other ones, FALSE means that we didn't process the
87 return (MRESULT
)FALSE
;
91 // ----------------------------------------------------------------------------
92 // wxTopLevelWindowOS2 creation
93 // ----------------------------------------------------------------------------
95 void wxTopLevelWindowOS2::Init()
97 m_bIconized
= m_bMaximizeOnShow
= FALSE
;
100 // Unlike (almost?) all other windows, frames are created hidden
105 // Data to save/restore when calling ShowFullScreen
107 m_lFsOldWindowStyle
= 0;
108 m_bFsIsMaximized
= FALSE
;
109 m_bFsIsShowing
= FALSE
;
111 m_hFrame
= NULLHANDLE
;
112 memset(&m_vSwp
, 0, sizeof(SWP
));
113 memset(&m_vSwpClient
, 0, sizeof(SWP
));
114 } // end of wxTopLevelWindowIOS2::Init
116 WXDWORD
wxTopLevelWindowOS2::OS2GetStyle(
118 , WXDWORD
* pdwExflags
121 long lMsflags
= wxWindow::OS2GetStyle( (lStyle
& ~wxBORDER_MASK
) | wxBORDER_NONE
125 if (lStyle
== wxDEFAULT_FRAME_STYLE
)
126 lMsflags
|= FCF_SIZEBORDER
| FCF_TITLEBAR
| FCF_SYSMENU
|
127 FCF_MINMAX
| FCF_TASKLIST
;
130 if ((lStyle
& wxCAPTION
) == wxCAPTION
)
131 lMsflags
|= FCF_TASKLIST
;
133 lMsflags
|= FCF_NOMOVEWITHOWNER
;
135 if ((lStyle
& wxVSCROLL
) == wxVSCROLL
)
136 lMsflags
|= FCF_VERTSCROLL
;
137 if ((lStyle
& wxHSCROLL
) == wxHSCROLL
)
138 lMsflags
|= FCF_HORZSCROLL
;
139 if (lStyle
& wxMINIMIZE_BOX
)
140 lMsflags
|= FCF_MINBUTTON
;
141 if (lStyle
& wxMAXIMIZE_BOX
)
142 lMsflags
|= FCF_MAXBUTTON
;
143 if (lStyle
& wxTHICK_FRAME
)
144 lMsflags
|= FCF_DLGBORDER
;
145 if (lStyle
& wxSYSTEM_MENU
)
146 lMsflags
|= FCF_SYSMENU
;
147 if (lStyle
& wxCAPTION
)
148 lMsflags
|= FCF_TASKLIST
;
149 if (lStyle
& wxCLIP_CHILDREN
)
151 // Invalid for frame windows under PM
154 if (lStyle
& wxTINY_CAPTION_VERT
)
155 lMsflags
|= FCF_TASKLIST
;
156 if (lStyle
& wxTINY_CAPTION_HORIZ
)
157 lMsflags
|= FCF_TASKLIST
;
159 if ((lStyle
& wxTHICK_FRAME
) == 0)
160 lMsflags
|= FCF_BORDER
;
161 if (lStyle
& wxFRAME_TOOL_WINDOW
)
162 *pdwExflags
= kFrameToolWindow
;
164 if (lStyle
& wxSTAY_ON_TOP
)
165 lMsflags
|= FCF_SYSMODAL
;
168 } // end of wxTopLevelWindowOS2::OS2GetCreateWindowFlags
170 WXHWND
wxTopLevelWindowOS2::OS2GetParent() const
173 // For the frames without wxFRAME_FLOAT_ON_PARENT style we should use NULL
174 // parent HWND or it would be always on top of its parent which is not what
175 // we usually want (in fact, we only want it for frames with the
176 // wxFRAME_FLOAT_ON_PARENT flag)
180 if (HasFlag(wxFRAME_FLOAT_ON_PARENT
) )
182 pParent
= GetParent();
184 // this flag doesn't make sense then and will be ignored
185 wxASSERT_MSG( pParent
,
186 _T("wxFRAME_FLOAT_ON_PARENT but no parent?") );
188 else // don't float on parent, must not be owned
192 if (HasFlag(wxFRAME_NO_TASKBAR
) && !pParent
)
194 if (!m_spHiddenParent
)
196 m_spHiddenParent
= new wxTopLevelWindowOS2(NULL
, -1, _T(""));
199 // We shouldn't leave it in wxTopLevelWindows or we wouldn't
200 // terminate the app when the last user-created frame is deleted --
201 // see ~wxTopLevelWindowMSW
203 wxTopLevelWindows
.DeleteObject(m_spHiddenParent
);
205 pParent
= m_spHiddenParent
;
207 return pParent
? pParent
->GetHWND() : NULL
;
208 } // end of wxTopLevelWindowOS2::OS2GetParent
210 bool wxTopLevelWindowOS2::CreateDialog(
212 , const wxString
& rsTitle
213 , const wxPoint
& rPos
214 , const wxSize
& rSize
217 wxWindow
* pParent
= GetParent();
220 // For the dialogs without wxDIALOG_NO_PARENT style, use the top level
221 // app window as parent - this avoids creating modal dialogs without
224 if (!pParent
&& !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
))
226 pParent
= wxTheApp
->GetTopWindow();
231 // Don't use transient windows as parents, this is dangerous as it
232 // can lead to a crash if the parent is destroyed before the child
234 // also don't use the window which is currently hidden as then the
235 // dialog would be hidden as well
236 if ((pParent
->GetExtraStyle() & wxWS_EX_TRANSIENT
) ||
248 hWndOwner
= GetHwndOf(pParent
);
250 hWndOwner
= HWND_DESKTOP
;
252 hWndDlg
= ::WinLoadDlg( HWND_DESKTOP
256 ,(ULONG
)ulDlgTemplate
260 m_hWnd
= (WXHWND
) hWndDlg
;
264 wxFAIL_MSG(_("Did you forget to include wx/os2/wx.rc in your resources?"));
266 wxLogSysError(_("Can't create dialog using template '%ul'"), ulDlgTemplate
);
272 // Move the dialog to its initial position without forcing repainting
279 if (!OS2GetCreateWindowCoords( rPos
287 nX
= nWidth
= (int)CW_USEDEFAULT
;
291 // We can't use CW_USEDEFAULT here as we're not calling CreateWindow()
292 // and passing CW_USEDEFAULT to MoveWindow() results in resizing the
293 // window to (0, 0) size which breaks quite a lot of things, e.g. the
294 // sizer calculation in wxSizer::Fit()
296 if (nWidth
== (int)CW_USEDEFAULT
)
299 // The exact number doesn't matter, the dialog will be resized
300 // again soon anyhow but it should be big enough to allow
301 // calculation relying on "totalSize - clientSize > 0" work, i.e.
302 // at least greater than the title bar height
304 nWidth
= nHeight
= 100;
306 if (nX
== (int)CW_USEDEFAULT
)
309 // Centre it on the screen - what else can we do?
311 wxSize vSizeDpy
= wxGetDisplaySize();
313 nX
= (vSizeDpy
.x
- nWidth
) / 2;
314 nY
= (vSizeDpy
.y
- nHeight
) / 2;
316 m_backgroundColour
.Set(wxString("LIGHT GREY"));
318 LONG lColor
= (LONG
)m_backgroundColour
.GetPixel();
320 if (!::WinSetPresParam( m_hWnd
329 ::WinSetWindowPos( GetHwnd()
335 ,SWP_MOVE
| SWP_SIZE
| SWP_ZORDER
| SWP_SHOW
337 ::WinQueryWindowPos(GetHwnd(), GetSwp());
341 } // end of wxTopLevelWindowOS2::CreateDialog
343 bool wxTopLevelWindowOS2::CreateFrame(
344 const wxString
& rsTitle
345 , const wxPoint
& rPos
346 , const wxSize
& rSize
350 WXDWORD lFlags
= OS2GetCreateWindowFlags(&lExflags
);
351 long lStyle
= GetWindowStyleFlag();
354 int nWidth
= rSize
.x
;
355 int nHeight
= rSize
.y
;
356 ULONG ulStyleFlags
= 0L;
359 wxWindow
* pParent
= GetParent();
365 hParent
= GetHwndOf(pParent
);
367 hParent
= HWND_DESKTOP
;
369 if ((lStyle
& wxMINIMIZE
) || (lStyle
& wxICONIZE
))
370 ulStyleFlags
|= WS_MINIMIZED
;
371 if (lStyle
& wxMAXIMIZE
)
372 ulStyleFlags
|= WS_MAXIMIZED
;
375 // Clear the visible flag, we always call show
377 ulStyleFlags
&= (unsigned long)~WS_VISIBLE
;
381 // Create the frame window: We break ranks with other ports now
382 // and instead of calling down into the base wxWindow class' OS2Create
383 // we do all our own stuff here. We will set the needed pieces
384 // of wxWindow manually, here.
387 hFrame
= ::WinCreateStdWindow( hParent
388 ,ulStyleFlags
// frame-window style
389 ,(PULONG
)&lFlags
// window style
390 ,(PSZ
)wxFrameClassName
// class name
391 ,(PSZ
)rsTitle
.c_str() // window title
392 ,0L // default client style
393 ,NULLHANDLE
// resource in executable file
395 ,&hClient
// receives client window handle
399 vError
= ::WinGetLastError(vHabmain
);
400 sError
= wxPMErrorToStr(vError
);
401 wxLogError("Error creating frame. Error: %s\n", sError
);
406 // wxWindow class' m_hWnd set here and needed associations
410 wxAssociateWinWithHandle(m_hWnd
, this);
411 wxAssociateWinWithHandle(m_hFrame
, this);
413 m_backgroundColour
.Set(wxString("MEDIUM GREY"));
415 LONG lColor
= (LONG
)m_backgroundColour
.GetPixel();
417 if (!::WinSetPresParam( m_hWnd
423 vError
= ::WinGetLastError(vHabmain
);
424 sError
= wxPMErrorToStr(vError
);
425 wxLogError("Error creating frame. Error: %s\n", sError
);
430 // Now need to subclass window. Instead of calling the SubClassWin in wxWindow
431 // we manually subclass here because we don't want to use the main wxWndProc
434 m_fnOldWndProc
= (WXFARPROC
) ::WinSubclassWindow(m_hFrame
, (PFNWP
)wxFrameMainWndProc
);
437 // Now size everything. If adding a menu the client will need to be resized.
442 nY
= pParent
->GetSize().y
- (nY
+ nHeight
);
448 ::WinQueryWindowRect(HWND_DESKTOP
, &vRect
);
449 nY
= vRect
.yTop
- (nY
+ nHeight
);
451 if (!::WinSetWindowPos( m_hFrame
457 ,SWP_SIZE
| SWP_MOVE
| SWP_ACTIVATE
| SWP_ZORDER
460 vError
= ::WinGetLastError(vHabmain
);
461 sError
= wxPMErrorToStr(vError
);
462 wxLogError("Error sizing frame. Error: %s\n", sError
);
465 lStyle
= ::WinQueryWindowULong( m_hWnd
468 lStyle
|= WS_CLIPCHILDREN
;
469 ::WinSetWindowULong( m_hWnd
474 } // end of wxTopLevelWindowOS2::CreateFrame
476 bool wxTopLevelWindowOS2::Create(
479 , const wxString
& rsTitle
480 , const wxPoint
& rPos
481 , const wxSize
& rSize
483 , const wxString
& rsName
490 m_windowStyle
= lStyle
;
492 m_windowId
= vId
== -1 ? NewControlId() : vId
;
493 wxTopLevelWindows
.Append(this);
495 pParent
->AddChild(this);
497 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
500 // We have different dialog templates to allows creation of dialogs
501 // with & without captions under OS2indows, resizeable or not (but a
502 // resizeable dialog always has caption - otherwise it would look too
507 if (lStyle
& wxRESIZE_BORDER
)
508 ulDlgTemplate
= (ULONG
)kResizeableDialog
;
509 else if (lStyle
& wxCAPTION
)
510 ulDlgTemplate
= (ULONG
)kCaptionDialog
;
512 ulDlgTemplate
= (ULONG
)kNoCaptionDialog
;
513 return CreateDialog( ulDlgTemplate
521 return CreateFrame( rsTitle
526 } // end of wxTopLevelWindowOS2::Create
528 wxTopLevelWindowOS2::~wxTopLevelWindowOS2()
530 wxTopLevelWindows
.DeleteObject(this);
532 if (wxModelessWindows
.Find(this))
533 wxModelessWindows
.DeleteObject(this);
536 // If this is the last top-level window, exit.
538 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
540 wxTheApp
->SetTopWindow(NULL
);
541 if ( wxTheApp
->GetExitOnFrameDelete() )
543 ::WinPostMsg(NULL
, WM_QUIT
, 0, 0);
546 } // end of wxTopLevelWindowOS2::~wxTopLevelWindowOS2
548 // ----------------------------------------------------------------------------
549 // wxTopLevelWindowOS2 client size
550 // ----------------------------------------------------------------------------
552 void wxTopLevelWindowOS2::DoSetClientSize(
558 // Call GetClientAreaOrigin() to take the toolbar into account
560 wxPoint vPt
= GetClientAreaOrigin();
565 wxWindow::DoSetClientSize( nWidth
568 } // end of wxTopLevelWindowOS2::DoSetClientSize
570 void wxTopLevelWindowOS2::DoGetClientSize(
575 wxWindow::DoGetClientSize( pnX
579 wxPoint vPt
= GetClientAreaOrigin();
586 } // end of wxTopLevelWindowOS2::DoGetClientSize
588 // ----------------------------------------------------------------------------
589 // wxTopLevelWindowOS2 showing
590 // ----------------------------------------------------------------------------
592 void wxTopLevelWindowOS2::DoShowWindow(
596 ::WinShowWindow(m_hFrame
, (BOOL
)(nShowCmd
& SWP_SHOW
));
599 // Need to artificially send a size event as wxApps often expect to do some
600 // final child control sizing
602 m_bIconized
= nShowCmd
== SWP_MINIMIZE
;
603 } // end of wxTopLevelWindowOS2::DoShowWindow
605 bool wxTopLevelWindowOS2::Show(
615 if (m_bMaximizeOnShow
)
617 nShowCmd
= SWP_MAXIMIZE
;
618 m_bMaximizeOnShow
= FALSE
;
629 DoShowWindow(nShowCmd
);
633 wxActivateEvent
vEvent(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
635 ::WinQueryWindowPos(m_hFrame
, &vSwp
);
636 m_bIconized
= vSwp
.fl
& SWP_MINIMIZE
;
637 ::WinQueryWindowPos(m_hWnd
, &m_vSwpClient
);
638 ::WinSendMsg(m_hFrame
, WM_UPDATEFRAME
, (MPARAM
)~0, 0);
639 ::WinQueryWindowPos(m_hWnd
, &vSwp
);
640 ::WinEnableWindow(m_hFrame
, TRUE
);
643 // Deal with children
645 MoveChildren(m_vSwpClient
.cy
- vSwp
.cy
);
646 vEvent
.SetEventObject(this);
647 GetEventHandler()->ProcessEvent(vEvent
);
652 // Try to highlight the correct window (the parent)
656 HWND hWndParent
= GetHwndOf(GetParent());
658 ::WinQueryWindowPos(hWndParent
, &vSwp
);
659 m_bIconized
= vSwp
.fl
& SWP_MINIMIZE
;
661 ::WinSetWindowPos( hWndParent
667 ,SWP_ZORDER
| SWP_ACTIVATE
| SWP_SHOW
| SWP_MOVE
669 ::WinEnableWindow(hWndParent
, TRUE
);
673 } // end of wxTopLevelWindowOS2::Show
675 // ----------------------------------------------------------------------------
676 // wxTopLevelWindowOS2 maximize/minimize
677 // ----------------------------------------------------------------------------
679 void wxTopLevelWindowOS2::Maximize(
686 // Just maximize it directly
688 DoShowWindow(bMaximize
? SWP_MAXIMIZE
: SWP_RESTORE
);
693 // We can't maximize the hidden frame because it shows it as well, so
694 // just remember that we should do it later in this case
696 m_bMaximizeOnShow
= TRUE
;
698 } // end of wxTopLevelWindowOS2::Maximize
700 bool wxTopLevelWindowOS2::IsMaximized() const
704 ::WinQueryWindowPos(m_hFrame
, (PSWP
)&m_vSwp
);
705 return (m_vSwp
.fl
& SWP_MAXIMIZE
);
706 } // end of wxTopLevelWindowOS2::IsMaximized
708 void wxTopLevelWindowOS2::Iconize(
712 DoShowWindow(bIconize
? SWP_MINIMIZE
: SWP_RESTORE
);
713 } // end of wxTopLevelWindowOS2::Iconize
715 bool wxTopLevelWindowOS2::IsIconized() const
717 // also update the current state
718 ::WinQueryWindowPos(m_hFrame
, (PSWP
)&m_vSwp
);
719 if (m_vSwp
.fl
& SWP_MINIMIZE
)
720 ((wxTopLevelWindow
*)this)->m_bIconized
= TRUE
;
722 ((wxTopLevelWindow
*)this)->m_bIconized
= FALSE
;
724 } // end of wxTopLevelWindowOS2::IsIconized
726 void wxTopLevelWindowOS2::Restore()
728 DoShowWindow(SWP_RESTORE
);
729 } // end of wxTopLevelWindowOS2::Restore
731 // generate an artificial resize event
732 void wxTopLevelWindowOS2::SendSizeEvent()
736 RECTL vRect
= wxGetWindowRect(GetHwnd());
738 (void)::WinPostMsg( m_hFrame
740 ,MPFROM2SHORT(vRect
.xRight
- vRect
.xLeft
, vRect
.yTop
- vRect
.yBottom
)
741 ,MPFROM2SHORT(vRect
.xRight
- vRect
.xLeft
, vRect
.yTop
- vRect
.yBottom
)
744 } // end of wxTopLevelWindowOS2::SendSizeEvent
746 // ----------------------------------------------------------------------------
747 // wxTopLevelWindowOS2 fullscreen
748 // ----------------------------------------------------------------------------
750 bool wxTopLevelWindowOS2::ShowFullScreen(
760 m_bFsIsShowing
= TRUE
;
764 // Zap the frame borders
768 // Save the 'normal' window lStyle
770 m_lFsOldWindowStyle
= ::WinQueryWindowULong( (HWND
)GetHWND()
775 // Save the old position, width & height, maximize state
777 m_vFsOldSize
= GetRect();
778 m_bFsIsMaximized
= IsMaximized();
781 // Decide which window lStyle flags to turn off
783 LONG lNewStyle
= m_lFsOldWindowStyle
;
786 if (lStyle
& wxFULLSCREEN_NOBORDER
)
787 lOffFlags
|= FCF_BORDER
;
788 if (lStyle
& wxFULLSCREEN_NOCAPTION
)
789 lOffFlags
|= (FCF_TASKLIST
| FCF_SYSMENU
);
791 lNewStyle
&= (~lOffFlags
);
794 // Change our window style to be compatible with full-screen mode
796 ::WinSetWindowULong( (HWND
)GetHWND()
802 // Resize to the size of the desktop
806 RECTL vRect
= wxGetWindowRect(HWND_DESKTOP
);
808 nWidth
= vRect
.xRight
- vRect
.xLeft
;
809 nHeight
= vRect
.yTop
- vRect
.yBottom
;
816 // Now flush the window style cache and actually go full-screen
818 ::WinSetWindowPos( m_hFrame
827 wxSizeEvent
vEvent( wxSize( nWidth
833 GetEventHandler()->ProcessEvent(vEvent
);
841 m_bFsIsShowing
= FALSE
;
842 Maximize(m_bFsIsMaximized
);
843 ::WinSetWindowULong( (HWND
)GetHWND()
847 ::WinSetWindowPos( m_hFrame
857 } // end of wxTopLevelWindowOS2::ShowFullScreen
859 // ----------------------------------------------------------------------------
860 // wxTopLevelWindowOS2 misc
861 // ----------------------------------------------------------------------------
863 void wxTopLevelWindowOS2::SetIcon(
867 SetIcons(wxIconBundle(rIcon
));
868 } // end of wxTopLevelWindowOS2::SetIcon
870 void wxTopLevelWindowOS2::SetIcons(
871 const wxIconBundle
& rIcons
877 wxTopLevelWindowBase::SetIcons(rIcons
);
879 const wxIcon
& vIcon
= rIcons
.GetIcon(wxSize(32, 32));
881 if (vIcon
.Ok() && vIcon
.GetWidth() == 32 && vIcon
.GetHeight() == 32)
883 ::WinSendMsg( m_hFrame
885 ,(MPARAM
)((HPOINTER
)vIcon
.GetHICON())
888 ::WinSendMsg( m_hFrame
894 } // end of wxTopLevelWindowOS2::SetIcon
896 bool wxTopLevelWindowOS2::EnableCloseButton(
901 // Get system (a.k.a. window) menu
903 HMENU hMenu
= ::WinWindowFromID(m_hFrame
, FID_SYSMENU
);
907 wxLogLastError(_T("GetSystemMenu"));
912 // Enabling/disabling the close item from it also automatically
913 // disables/enables the close title bar button
916 (void)::WinSendMsg( hMenu
918 ,MPFROM2SHORT(SC_CLOSE
, FALSE
)
919 ,MPFROM2SHORT(MIA_DISABLED
, FALSE
)
922 (void)::WinSendMsg( hMenu
924 ,MPFROM2SHORT(SC_CLOSE
, FALSE
)
925 ,MPFROM2SHORT(MIA_DISABLED
, MIA_DISABLED
)
929 // Update appearance immediately
931 ::WinSendMsg( m_hFrame
937 } // end of wxTopLevelWindowOS2::EnableCloseButton