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
60 // ============================================================================
61 // wxTopLevelWindowMSW implementation
62 // ============================================================================
65 MRESULT EXPENTRY
wxDlgProc( HWND
WXUNUSED(hWnd
)
67 ,MPARAM
WXUNUSED(wParam
)
68 ,MPARAM
WXUNUSED(lParam
)
71 if (uMessage
== WM_INITDLG
)
74 // For this message, returning TRUE tells system to set focus to the
75 // first control in the dialog box.
82 // For all the other ones, FALSE means that we didn't process the
85 return (MRESULT
)FALSE
;
89 // ----------------------------------------------------------------------------
90 // wxTopLevelWindowOS2 creation
91 // ----------------------------------------------------------------------------
93 void wxTopLevelWindowOS2::Init()
95 m_bIconized
= m_bMaximizeOnShow
= FALSE
;
98 // Unlike (almost?) all other windows, frames are created hidden
103 // Data to save/restore when calling ShowFullScreen
105 m_lFsOldWindowStyle
= 0;
106 m_bFsIsMaximized
= FALSE
;
107 m_bFsIsShowing
= FALSE
;
109 m_hFrame
= NULLHANDLE
;
110 memset(&m_vSwp
, 0, sizeof(SWP
));
111 memset(&m_vSwpClient
, 0, sizeof(SWP
));
112 } // end of wxTopLevelWindowIOS2::Init
114 long wxTopLevelWindowOS2::OS2GetCreateWindowFlags(
118 long lStyle
= GetWindowStyle();
121 if (lStyle
== wxDEFAULT_FRAME_STYLE
)
122 lMsflags
= FCF_SIZEBORDER
| FCF_TITLEBAR
| FCF_SYSMENU
|
123 FCF_MINMAX
| FCF_TASKLIST
;
126 if ((lStyle
& wxCAPTION
) == wxCAPTION
)
127 lMsflags
= FCF_TASKLIST
;
129 lMsflags
= FCF_NOMOVEWITHOWNER
;
131 if ((lStyle
& wxVSCROLL
) == wxVSCROLL
)
132 lMsflags
|= FCF_VERTSCROLL
;
133 if ((lStyle
& wxHSCROLL
) == wxHSCROLL
)
134 lMsflags
|= FCF_HORZSCROLL
;
135 if (lStyle
& wxMINIMIZE_BOX
)
136 lMsflags
|= FCF_MINBUTTON
;
137 if (lStyle
& wxMAXIMIZE_BOX
)
138 lMsflags
|= FCF_MAXBUTTON
;
139 if (lStyle
& wxTHICK_FRAME
)
140 lMsflags
|= FCF_DLGBORDER
;
141 if (lStyle
& wxSYSTEM_MENU
)
142 lMsflags
|= FCF_SYSMENU
;
143 if (lStyle
& wxCAPTION
)
144 lMsflags
|= FCF_TASKLIST
;
145 if (lStyle
& wxCLIP_CHILDREN
)
147 // Invalid for frame windows under PM
150 if (lStyle
& wxTINY_CAPTION_VERT
)
151 lMsflags
|= FCF_TASKLIST
;
152 if (lStyle
& wxTINY_CAPTION_HORIZ
)
153 lMsflags
|= FCF_TASKLIST
;
155 if ((lStyle
& wxTHICK_FRAME
) == 0)
156 lMsflags
|= FCF_BORDER
;
157 if (lStyle
& wxFRAME_TOOL_WINDOW
)
158 *plExflags
= kFrameToolWindow
;
160 if (lStyle
& wxSTAY_ON_TOP
)
161 lMsflags
|= FCF_SYSMODAL
;
164 } // end of wxTopLevelWindowOS2::OS2GetCreateWindowFlags
166 bool wxTopLevelWindowOS2::CreateDialog(
168 , const wxString
& rsTitle
169 , const wxPoint
& rPos
170 , const wxSize
& rSize
173 wxWindow
* pParent
= GetParent();
176 // For the dialogs without wxDIALOG_NO_PARENT style, use the top level
177 // app window as parent - this avoids creating modal dialogs without
180 if (!pParent
&& !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
))
182 pParent
= wxTheApp
->GetTopWindow();
187 // Don't use transient windows as parents, this is dangerous as it
188 // can lead to a crash if the parent is destroyed before the child
190 // also don't use the window which is currently hidden as then the
191 // dialog would be hidden as well
192 if ((pParent
->GetExtraStyle() & wxWS_EX_TRANSIENT
) ||
204 hWndOwner
= GetHwndOf(pParent
);
206 hWndOwner
= HWND_DESKTOP
;
208 hWndDlg
= ::WinLoadDlg( HWND_DESKTOP
212 ,(ULONG
)ulDlgTemplate
216 m_hWnd
= (WXHWND
) hWndDlg
;
220 wxFAIL_MSG(_("Did you forget to include wx/os2/wx.rc in your resources?"));
222 wxLogSysError(_("Can't create dialog using template '%ul'"), ulDlgTemplate
);
228 // Move the dialog to its initial position without forcing repainting
235 if (!OS2GetCreateWindowCoords( rPos
243 nX
= nWidth
= (int)CW_USEDEFAULT
;
247 // We can't use CW_USEDEFAULT here as we're not calling CreateWindow()
248 // and passing CW_USEDEFAULT to MoveWindow() results in resizing the
249 // window to (0, 0) size which breaks quite a lot of things, e.g. the
250 // sizer calculation in wxSizer::Fit()
252 if (nWidth
== (int)CW_USEDEFAULT
)
255 // The exact number doesn't matter, the dialog will be resized
256 // again soon anyhow but it should be big enough to allow
257 // calculation relying on "totalSize - clientSize > 0" work, i.e.
258 // at least greater than the title bar height
260 nWidth
= nHeight
= 100;
262 if (nX
== (int)CW_USEDEFAULT
)
265 // Centre it on the screen - what else can we do?
267 wxSize vSizeDpy
= wxGetDisplaySize();
269 nX
= (vSizeDpy
.x
- nWidth
) / 2;
270 nY
= (vSizeDpy
.y
- nHeight
) / 2;
272 ::WinSetWindowPos( GetHwnd()
278 ,SWP_MOVE
| SWP_SIZE
| SWP_ZORDER
| SWP_SHOW
280 if (!rsTitle
.IsNull())
282 ::WinSetWindowText(GetHwnd(), rsTitle
.c_str());
286 } // end of wxTopLevelWindowOS2::CreateDialog
288 bool wxTopLevelWindowOS2::CreateFrame(
289 const wxString
& rsTitle
290 , const wxPoint
& rPos
291 , const wxSize
& rSize
295 long lFlags
= OS2GetCreateWindowFlags(&lExflags
);
296 long lStyle
= GetWindowStyleFlag();
299 int nWidth
= rSize
.x
;
300 int nHeight
= rSize
.y
;
301 ULONG ulStyleFlags
= 0L;
304 wxWindow
* pParent
= GetParent();
310 hParent
= GetHwndOf(pParent
);
312 hParent
= HWND_DESKTOP
;
314 if ((lStyle
& wxMINIMIZE
) || (lStyle
& wxICONIZE
))
315 ulStyleFlags
|= WS_MINIMIZED
;
316 if (lStyle
& wxMAXIMIZE
)
317 ulStyleFlags
|= WS_MAXIMIZED
;
320 // Clear the visible flag, we always call show
322 ulStyleFlags
&= (unsigned long)~WS_VISIBLE
;
326 // Create the frame window: We break ranks with other ports now
327 // and instead of calling down into the base wxWindow class' OS2Create
328 // we do all our own stuff here. We will set the needed pieces
329 // of wxWindow manually, here.
332 hFrame
= ::WinCreateStdWindow( hParent
333 ,ulStyleFlags
// frame-window style
334 ,(PULONG
)&lFlags
// window style
335 ,(PSZ
)wxFrameClassName
// class name
336 ,(PSZ
)rsTitle
.c_str() // window title
337 ,0L // default client style
338 ,NULLHANDLE
// resource in executable file
340 ,&hClient
// receives client window handle
344 vError
= ::WinGetLastError(vHabmain
);
345 sError
= wxPMErrorToStr(vError
);
346 wxLogError("Error creating frame. Error: %s\n", sError
);
351 // wxWindow class' m_hWnd set here and needed associations
355 wxAssociateWinWithHandle(m_hWnd
, this);
356 wxAssociateWinWithHandle(m_hFrame
, this);
358 m_backgroundColour
.Set(wxString("GREY"));
360 LONG lColor
= (LONG
)m_backgroundColour
.GetPixel();
362 if (!::WinSetPresParam( m_hWnd
368 vError
= ::WinGetLastError(vHabmain
);
369 sError
= wxPMErrorToStr(vError
);
370 wxLogError("Error creating frame. Error: %s\n", sError
);
375 // Now need to subclass window. Instead of calling the SubClassWin in wxWindow
376 // we manually subclass here because we don't want to use the main wxWndProc
379 m_fnOldWndProc
= (WXFARPROC
) ::WinSubclassWindow(m_hFrame
, (PFNWP
)wxFrameMainWndProc
);
382 // Now size everything. If adding a menu the client will need to be resized.
387 nY
= pParent
->GetSize().y
- (nY
+ nHeight
);
393 ::WinQueryWindowRect(HWND_DESKTOP
, &vRect
);
394 nY
= vRect
.yTop
- (nY
+ nHeight
);
396 if (!::WinSetWindowPos( m_hFrame
402 ,SWP_SIZE
| SWP_MOVE
| SWP_ACTIVATE
| SWP_ZORDER
405 vError
= ::WinGetLastError(vHabmain
);
406 sError
= wxPMErrorToStr(vError
);
407 wxLogError("Error sizing frame. Error: %s\n", sError
);
411 } // end of wxTopLevelWindowOS2::CreateFrame
413 bool wxTopLevelWindowOS2::Create(
416 , const wxString
& rsTitle
417 , const wxPoint
& rPos
418 , const wxSize
& rSize
420 , const wxString
& rsName
427 m_windowStyle
= lStyle
;
429 m_windowId
= vId
== -1 ? NewControlId() : vId
;
430 wxTopLevelWindows
.Append(this);
432 pParent
->AddChild(this);
434 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
437 // We have different dialog templates to allows creation of dialogs
438 // with & without captions under OS2indows, resizeable or not (but a
439 // resizeable dialog always has caption - otherwise it would look too
444 if (lStyle
& wxRESIZE_BORDER
)
445 ulDlgTemplate
= (ULONG
)kResizeableDialog
;
446 else if (lStyle
& wxCAPTION
)
447 ulDlgTemplate
= (ULONG
)kCaptionDialog
;
449 ulDlgTemplate
= (ULONG
)kNoCaptionDialog
;
450 return CreateDialog( ulDlgTemplate
458 return CreateFrame( rsTitle
463 } // end of wxTopLevelWindowOS2::Create
465 wxTopLevelWindowOS2::~wxTopLevelWindowOS2()
467 wxTopLevelWindows
.DeleteObject(this);
469 if (wxModelessWindows
.Find(this))
470 wxModelessWindows
.DeleteObject(this);
473 // If this is the last top-level window, exit.
475 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
477 wxTheApp
->SetTopWindow(NULL
);
478 if ( wxTheApp
->GetExitOnFrameDelete() )
480 ::WinPostMsg(NULL
, WM_QUIT
, 0, 0);
483 } // end of wxTopLevelWindowOS2::~wxTopLevelWindowOS2
486 // IF we have child controls in the Frame's client we need to alter
487 // the y position, because, OS/2 controls are positioned relative to
488 // wxWindows orgin (top left) not the OS/2 origin (bottom left)
490 void wxTopLevelWindowOS2::AlterChildPos()
493 // OS/2 is the only OS concerned about this
495 wxWindow
* pChild
= NULL
;
496 wxControl
* pCtrl
= NULL
;
500 ::WinQueryWindowRect(GetHwnd(), &vRect
);
501 for (wxWindowList::Node
* pNode
= GetChildren().GetFirst();
503 pNode
= pNode
->GetNext())
505 wxWindow
* pChild
= pNode
->GetData();
507 ::WinQueryWindowPos(pChild
->GetHWND(), &vSwp
);
508 vSwp
.y
+= (vRect
.yTop
- m_vSwpClient
.cy
);
509 if (pChild
->IsKindOf(CLASSINFO(wxControl
)))
511 pCtrl
= wxDynamicCast(pChild
, wxControl
);
513 // Must deal with controls that have margins like ENTRYFIELD. The SWP
514 // struct of such a control will have and origin offset from its intended
515 // position by the width of the margins.
517 vSwp
.y
-= pCtrl
->GetYComp();
518 vSwp
.x
-= pCtrl
->GetXComp();
520 ::WinSetWindowPos( pChild
->GetHWND()
528 ::WinQueryWindowPos(pChild
->GetHWND(), &vSwp
);
531 ::WinQueryWindowPos(GetHwnd(), &m_vSwpClient
);
532 } // end of wxTopLevelWindowOS2::AlterChildPos
534 void wxTopLevelWindowOS2::UpdateInternalSize(
539 pChild
->MoveChildren(m_vSwpClient
.cy
- nHeight
);
540 ::WinQueryWindowPos(GetHwnd(), &m_vSwpClient
);
541 } // end of wxTopLevelWindowOS2::UpdateInternalSize
543 // ----------------------------------------------------------------------------
544 // wxTopLevelWindowOS2 client size
545 // ----------------------------------------------------------------------------
547 void wxTopLevelWindowOS2::DoSetClientSize(
553 // Call GetClientAreaOrigin() to take the toolbar into account
555 wxPoint vPt
= GetClientAreaOrigin();
560 wxWindow::DoSetClientSize( nWidth
563 } // end of wxTopLevelWindowOS2::DoSetClientSize
565 void wxTopLevelWindowOS2::DoGetClientSize(
570 wxWindow::DoGetClientSize( pnX
574 wxPoint vPt
= GetClientAreaOrigin();
581 } // end of wxTopLevelWindowOS2::DoGetClientSize
583 // ----------------------------------------------------------------------------
584 // wxTopLevelWindowOS2 showing
585 // ----------------------------------------------------------------------------
587 void wxTopLevelWindowOS2::DoShowWindow(
591 ::WinShowWindow(m_hFrame
, (BOOL
)nShowCmd
);
592 m_bIconized
= nShowCmd
== SWP_MINIMIZE
;
593 } // end of wxTopLevelWindowOS2::DoShowWindow
595 bool wxTopLevelWindowOS2::Show(
605 if (m_bMaximizeOnShow
)
608 m_bMaximizeOnShow
= FALSE
;
619 DoShowWindow(nShowCmd
);
623 wxActivateEvent
vEvent(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
625 ::WinQueryWindowPos(m_hFrame
, &vSwp
);
626 m_bIconized
= vSwp
.fl
& SWP_MINIMIZE
;
627 ::WinQueryWindowPos(m_hWnd
, &m_vSwpClient
);
628 ::WinSendMsg(m_hFrame
, WM_UPDATEFRAME
, (MPARAM
)~0, 0);
629 ::WinEnableWindow(m_hFrame
, TRUE
);
630 vEvent
.SetEventObject(this);
631 GetEventHandler()->ProcessEvent(vEvent
);
636 // Try to highlight the correct window (the parent)
640 HWND hWndParent
= GetHwndOf(GetParent());
642 ::WinQueryWindowPos(hWndParent
, &vSwp
);
643 m_bIconized
= vSwp
.fl
& SWP_MINIMIZE
;
645 ::WinSetWindowPos( hWndParent
651 ,SWP_ZORDER
| SWP_ACTIVATE
| SWP_SHOW
| SWP_MOVE
653 ::WinEnableWindow(hWndParent
, TRUE
);
657 } // end of wxTopLevelWindowOS2::Show
659 // ----------------------------------------------------------------------------
660 // wxTopLevelWindowOS2 maximize/minimize
661 // ----------------------------------------------------------------------------
663 void wxTopLevelWindowOS2::Maximize(
670 // Just maximize it directly
672 DoShowWindow(bMaximize
? SWP_MAXIMIZE
: SWP_RESTORE
);
677 // We can't maximize the hidden frame because it shows it as well, so
678 // just remember that we should do it later in this case
680 m_bMaximizeOnShow
= TRUE
;
682 } // end of wxTopLevelWindowOS2::Maximize
684 bool wxTopLevelWindowOS2::IsMaximized() const
688 ::WinQueryWindowPos(m_hFrame
, (PSWP
)&m_vSwp
);
689 return (m_vSwp
.fl
& SWP_MAXIMIZE
);
690 } // end of wxTopLevelWindowOS2::IsMaximized
692 void wxTopLevelWindowOS2::Iconize(
696 DoShowWindow(bIconize
? SWP_MINIMIZE
: SWP_RESTORE
);
697 } // end of wxTopLevelWindowOS2::Iconize
699 bool wxTopLevelWindowOS2::IsIconized() const
701 // also update the current state
702 ::WinQueryWindowPos(m_hFrame
, (PSWP
)&m_vSwp
);
703 if (m_vSwp
.fl
& SWP_MINIMIZE
)
704 ((wxTopLevelWindow
*)this)->m_bIconized
= TRUE
;
706 ((wxTopLevelWindow
*)this)->m_bIconized
= FALSE
;
708 } // end of wxTopLevelWindowOS2::IsIconized
710 void wxTopLevelWindowOS2::Restore()
712 DoShowWindow(SWP_RESTORE
);
713 } // end of wxTopLevelWindowOS2::Restore
715 // ----------------------------------------------------------------------------
716 // wxTopLevelWindowOS2 fullscreen
717 // ----------------------------------------------------------------------------
719 bool wxTopLevelWindowOS2::ShowFullScreen(
729 m_bFsIsShowing
= TRUE
;
733 // Zap the frame borders
737 // Save the 'normal' window lStyle
739 m_lFsOldWindowStyle
= ::WinQueryWindowULong( (HWND
)GetHWND()
744 // Save the old position, width & height, maximize state
746 m_vFsOldSize
= GetRect();
747 m_bFsIsMaximized
= IsMaximized();
750 // Decide which window lStyle flags to turn off
752 LONG lNewStyle
= m_lFsOldWindowStyle
;
755 if (lStyle
& wxFULLSCREEN_NOBORDER
)
756 lOffFlags
|= FCF_BORDER
;
757 if (lStyle
& wxFULLSCREEN_NOCAPTION
)
758 lOffFlags
|= (FCF_TASKLIST
| FCF_SYSMENU
);
760 lNewStyle
&= (~lOffFlags
);
763 // Change our window style to be compatible with full-screen mode
765 ::WinSetWindowULong( (HWND
)GetHWND()
771 // Resize to the size of the desktop
775 RECTL vRect
= wxGetWindowRect(HWND_DESKTOP
);
777 nWidth
= vRect
.xRight
- vRect
.xLeft
;
778 nHeight
= vRect
.yTop
- vRect
.yBottom
;
785 // Now flush the window style cache and actually go full-screen
787 ::WinSetWindowPos( m_hFrame
796 wxSizeEvent
vEvent( wxSize( nWidth
802 GetEventHandler()->ProcessEvent(vEvent
);
810 m_bFsIsShowing
= FALSE
;
811 Maximize(m_bFsIsMaximized
);
812 ::WinSetWindowULong( (HWND
)GetHWND()
816 ::WinSetWindowPos( m_hFrame
826 } // end of wxTopLevelWindowOS2::ShowFullScreen
828 // ----------------------------------------------------------------------------
829 // wxTopLevelWindowOS2 misc
830 // ----------------------------------------------------------------------------
832 void wxTopLevelWindowOS2::SetIcon(
839 wxTopLevelWindowBase::SetIcon(rIcon
);
843 ::WinSendMsg( m_hFrame
845 ,(MPARAM
)((HPOINTER
)m_icon
.GetHICON())
848 ::WinSendMsg( m_hFrame
854 } // end of wxTopLevelWindowOS2::SetIcon
856 bool wxTopLevelWindowOS2::EnableCloseButton(
861 // Get system (a.k.a. window) menu
863 HMENU hMenu
= ::WinWindowFromID(m_hFrame
, FID_SYSMENU
);
867 wxLogLastError(_T("GetSystemMenu"));
872 // Enabling/disabling the close item from it also automatically
873 // disables/enables the close title bar button
876 (void)::WinSendMsg( hMenu
878 ,MPFROM2SHORT(SC_CLOSE
, FALSE
)
879 ,MPFROM2SHORT(MIA_DISABLED
, FALSE
)
882 (void)::WinSendMsg( hMenu
884 ,MPFROM2SHORT(SC_CLOSE
, FALSE
)
885 ,MPFROM2SHORT(MIA_DISABLED
, MIA_DISABLED
)
889 // Update appearance immediately
891 ::WinSendMsg( m_hFrame
897 } // end of wxTopLevelWindowOS2::EnableCloseButton