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"
40 #include "wx/os2/private.h"
42 // ----------------------------------------------------------------------------
43 // stubs for missing functions under MicroWindows
44 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // list of all frames and modeless dialogs
52 wxWindowList wxModelessWindows
;
54 // the name of the default wxWindows class
55 extern const wxChar
* wxCanvasClassName
;
56 extern const wxChar
* wxFrameClassName
;
57 extern void wxAssociateWinWithHandle( HWND hWnd
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 long wxTopLevelWindowOS2::OS2GetCreateWindowFlags(
119 long lStyle
= GetWindowStyle();
122 if (lStyle
== wxDEFAULT_FRAME_STYLE
)
123 lMsflags
= FCF_SIZEBORDER
| FCF_TITLEBAR
| FCF_SYSMENU
|
124 FCF_MINMAX
| FCF_TASKLIST
;
127 if ((lStyle
& wxCAPTION
) == wxCAPTION
)
128 lMsflags
= FCF_TASKLIST
;
130 lMsflags
= FCF_NOMOVEWITHOWNER
;
132 if ((lStyle
& wxVSCROLL
) == wxVSCROLL
)
133 lMsflags
|= FCF_VERTSCROLL
;
134 if ((lStyle
& wxHSCROLL
) == wxHSCROLL
)
135 lMsflags
|= FCF_HORZSCROLL
;
136 if (lStyle
& wxMINIMIZE_BOX
)
137 lMsflags
|= FCF_MINBUTTON
;
138 if (lStyle
& wxMAXIMIZE_BOX
)
139 lMsflags
|= FCF_MAXBUTTON
;
140 if (lStyle
& wxTHICK_FRAME
)
141 lMsflags
|= FCF_DLGBORDER
;
142 if (lStyle
& wxSYSTEM_MENU
)
143 lMsflags
|= FCF_SYSMENU
;
144 if (lStyle
& wxCAPTION
)
145 lMsflags
|= FCF_TASKLIST
;
146 if (lStyle
& wxCLIP_CHILDREN
)
148 // Invalid for frame windows under PM
151 if (lStyle
& wxTINY_CAPTION_VERT
)
152 lMsflags
|= FCF_TASKLIST
;
153 if (lStyle
& wxTINY_CAPTION_HORIZ
)
154 lMsflags
|= FCF_TASKLIST
;
156 if ((lStyle
& wxTHICK_FRAME
) == 0)
157 lMsflags
|= FCF_BORDER
;
158 if (lStyle
& wxFRAME_TOOL_WINDOW
)
159 *plExflags
= kFrameToolWindow
;
161 if (lStyle
& wxSTAY_ON_TOP
)
162 lMsflags
|= FCF_SYSMODAL
;
165 } // end of wxTopLevelWindowOS2::OS2GetCreateWindowFlags
167 bool wxTopLevelWindowOS2::CreateDialog(
169 , const wxString
& rsTitle
170 , const wxPoint
& rPos
171 , const wxSize
& rSize
174 wxWindow
* pParent
= GetParent();
177 // For the dialogs without wxDIALOG_NO_PARENT style, use the top level
178 // app window as parent - this avoids creating modal dialogs without
181 if (!pParent
&& !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
))
183 pParent
= wxTheApp
->GetTopWindow();
188 // Don't use transient windows as parents, this is dangerous as it
189 // can lead to a crash if the parent is destroyed before the child
191 // also don't use the window which is currently hidden as then the
192 // dialog would be hidden as well
193 if ((pParent
->GetExtraStyle() & wxWS_EX_TRANSIENT
) ||
205 hWndParent
= GetHwndOf(pParent
);
207 hWndParent
= HWND_DESKTOP
;
209 hWndDlg
= ::WinLoadDlg( hWndParent
213 ,(ULONG
)ulDlgTemplate
217 m_hWnd
= (WXHWND
) hWndDlg
;
221 wxFAIL_MSG(_("Did you forget to include wx/os2/wx.rc in your resources?"));
223 wxLogSysError(_("Can't create dialog using template '%ul'"), ulDlgTemplate
);
229 // Move the dialog to its initial position without forcing repainting
236 if (!OS2GetCreateWindowCoords( rPos
244 nX
= nWidth
= (int)CW_USEDEFAULT
;
248 // We can't use CW_USEDEFAULT here as we're not calling CreateWindow()
249 // and passing CW_USEDEFAULT to MoveWindow() results in resizing the
250 // window to (0, 0) size which breaks quite a lot of things, e.g. the
251 // sizer calculation in wxSizer::Fit()
253 if (nWidth
== (int)CW_USEDEFAULT
)
256 // The exact number doesn't matter, the dialog will be resized
257 // again soon anyhow but it should be big enough to allow
258 // calculation relying on "totalSize - clientSize > 0" work, i.e.
259 // at least greater than the title bar height
261 nWidth
= nHeight
= 100;
263 if (nX
== (int)CW_USEDEFAULT
)
266 // Centre it on the screen - what else can we do?
268 wxSize vSizeDpy
= wxGetDisplaySize();
270 nX
= (vSizeDpy
.x
- nWidth
) / 2;
271 nY
= (vSizeDpy
.y
- nHeight
) / 2;
273 ::WinSetWindowPos( GetHwnd()
279 ,SWP_MOVE
| SWP_SIZE
| SWP_ZORDER
| SWP_SHOW
281 if (!rsTitle
.IsNull())
283 ::WinSetWindowText(GetHwnd(), rsTitle
.c_str());
287 } // end of wxTopLevelWindowOS2::CreateDialog
289 bool wxTopLevelWindowOS2::CreateFrame(
290 const wxString
& rsTitle
291 , const wxPoint
& rPos
292 , const wxSize
& rSize
296 long lFlags
= OS2GetCreateWindowFlags(&lExflags
);
297 long lStyle
= GetWindowStyleFlag();
300 int nWidth
= rSize
.x
;
301 int nHeight
= rSize
.y
;
302 ULONG ulStyleFlags
= 0L;
305 wxWindow
* pParent
= GetParent();
311 hParent
= GetHwndOf(pParent
);
313 hParent
= HWND_DESKTOP
;
315 if ((lStyle
& wxMINIMIZE
) || (lStyle
& wxICONIZE
))
316 ulStyleFlags
|= WS_MINIMIZED
;
317 if (lStyle
& wxMAXIMIZE
)
318 ulStyleFlags
|= WS_MAXIMIZED
;
321 // Clear the visible flag, we always call show
323 ulStyleFlags
&= (unsigned long)~WS_VISIBLE
;
327 // Create the frame window: We break ranks with other ports now
328 // and instead of calling down into the base wxWindow class' OS2Create
329 // we do all our own stuff here. We will set the needed pieces
330 // of wxWindow manually, here.
333 hFrame
= ::WinCreateStdWindow( hParent
334 ,ulStyleFlags
// frame-window style
335 ,(PULONG
)&lFlags
// window style
336 ,(PSZ
)wxFrameClassName
// class name
337 ,(PSZ
)rsTitle
.c_str() // window title
338 ,0L // default client style
339 ,NULLHANDLE
// resource in executable file
341 ,&hClient
// receives client window handle
345 vError
= ::WinGetLastError(vHabmain
);
346 sError
= wxPMErrorToStr(vError
);
347 wxLogError("Error creating frame. Error: %s\n", sError
);
352 // wxWindow class' m_hWnd set here and needed associations
356 wxAssociateWinWithHandle(m_hWnd
, this);
357 wxAssociateWinWithHandle(m_hFrame
, this);
359 m_backgroundColour
.Set(wxString("GREY"));
361 LONG lColor
= (LONG
)m_backgroundColour
.GetPixel();
363 if (!::WinSetPresParam( m_hWnd
369 vError
= ::WinGetLastError(vHabmain
);
370 sError
= wxPMErrorToStr(vError
);
371 wxLogError("Error creating frame. Error: %s\n", sError
);
376 // Now need to subclass window. Instead of calling the SubClassWin in wxWindow
377 // we manually subclass here because we don't want to use the main wxWndProc
380 m_fnOldWndProc
= (WXFARPROC
) ::WinSubclassWindow(m_hFrame
, (PFNWP
)wxFrameMainWndProc
);
383 // Now size everything. If adding a menu the client will need to be resized.
388 nY
= pParent
->GetSize().y
- (nY
+ nHeight
);
394 ::WinQueryWindowRect(HWND_DESKTOP
, &vRect
);
395 nY
= vRect
.yTop
- (nY
+ nHeight
);
397 if (!::WinSetWindowPos( m_hFrame
403 ,SWP_SIZE
| SWP_MOVE
| SWP_ACTIVATE
| SWP_ZORDER
406 vError
= ::WinGetLastError(vHabmain
);
407 sError
= wxPMErrorToStr(vError
);
408 wxLogError("Error sizing frame. Error: %s\n", sError
);
412 } // end of wxTopLevelWindowOS2::CreateFrame
414 bool wxTopLevelWindowOS2::Create(
417 , const wxString
& rsTitle
418 , const wxPoint
& rPos
419 , const wxSize
& rSize
421 , const wxString
& rsName
428 m_windowStyle
= lStyle
;
430 m_windowId
= vId
== -1 ? NewControlId() : vId
;
431 wxTopLevelWindows
.Append(this);
433 pParent
->AddChild(this);
435 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
438 // We have different dialog templates to allows creation of dialogs
439 // with & without captions under OS2indows, resizeable or not (but a
440 // resizeable dialog always has caption - otherwise it would look too
445 if (lStyle
& wxRESIZE_BORDER
)
446 ulDlgTemplate
= (ULONG
)kResizeableDialog
;
447 else if (lStyle
& wxCAPTION
)
448 ulDlgTemplate
= (ULONG
)kCaptionDialog
;
450 ulDlgTemplate
= (ULONG
)kNoCaptionDialog
;
451 return CreateDialog( ulDlgTemplate
459 return CreateFrame( rsTitle
464 } // end of wxTopLevelWindowOS2::Create
466 wxTopLevelWindowOS2::~wxTopLevelWindowOS2()
468 wxTopLevelWindows
.DeleteObject(this);
470 if (wxModelessWindows
.Find(this))
471 wxModelessWindows
.DeleteObject(this);
474 // If this is the last top-level window, exit.
476 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
478 wxTheApp
->SetTopWindow(NULL
);
479 if ( wxTheApp
->GetExitOnFrameDelete() )
481 ::WinPostMsg(NULL
, WM_QUIT
, 0, 0);
484 } // end of wxTopLevelWindowOS2::~wxTopLevelWindowOS2
487 // IF we have child controls in the Frame's client we need to alter
488 // the y position, because, OS/2 controls are positioned relative to
489 // wxWindows orgin (top left) not the OS/2 origin (bottom left)
491 void wxTopLevelWindowOS2::AlterChildPos()
494 // OS/2 is the only OS concerned about this
496 wxWindow
* pChild
= NULL
;
497 wxControl
* pCtrl
= NULL
;
501 ::WinQueryWindowRect(GetHwnd(), &vRect
);
502 for (wxWindowList::Node
* pNode
= GetChildren().GetFirst();
504 pNode
= pNode
->GetNext())
506 wxWindow
* pChild
= pNode
->GetData();
508 ::WinQueryWindowPos(pChild
->GetHWND(), &vSwp
);
509 vSwp
.y
+= (vRect
.yTop
- m_vSwpClient
.cy
);
510 if (pChild
->IsKindOf(CLASSINFO(wxControl
)))
512 pCtrl
= wxDynamicCast(pChild
, wxControl
);
514 // Must deal with controls that have margins like ENTRYFIELD. The SWP
515 // struct of such a control will have and origin offset from its intended
516 // position by the width of the margins.
518 vSwp
.y
-= pCtrl
->GetYComp();
519 vSwp
.x
-= pCtrl
->GetXComp();
521 ::WinSetWindowPos( pChild
->GetHWND()
529 ::WinQueryWindowPos(pChild
->GetHWND(), &vSwp
);
532 ::WinQueryWindowPos(GetHwnd(), &m_vSwpClient
);
533 } // end of wxTopLevelWindowOS2::AlterChildPos
535 void wxTopLevelWindowOS2::UpdateInternalSize(
540 pChild
->MoveChildren(m_vSwpClient
.cy
- nHeight
);
541 ::WinQueryWindowPos(GetHwnd(), &m_vSwpClient
);
542 } // end of wxTopLevelWindowOS2::UpdateInternalSize
544 // ----------------------------------------------------------------------------
545 // wxTopLevelWindowOS2 client size
546 // ----------------------------------------------------------------------------
548 void wxTopLevelWindowOS2::DoSetClientSize(
554 // Call GetClientAreaOrigin() to take the toolbar into account
556 wxPoint vPt
= GetClientAreaOrigin();
561 wxWindow::DoSetClientSize( nWidth
564 } // end of wxTopLevelWindowOS2::DoSetClientSize
566 void wxTopLevelWindowOS2::DoGetClientSize(
571 wxWindow::DoGetClientSize( pnX
575 wxPoint vPt
= GetClientAreaOrigin();
582 } // end of wxTopLevelWindowOS2::DoGetClientSize
584 // ----------------------------------------------------------------------------
585 // wxTopLevelWindowOS2 showing
586 // ----------------------------------------------------------------------------
588 void wxTopLevelWindowOS2::DoShowWindow(
592 ::WinShowWindow(m_hFrame
, (BOOL
)nShowCmd
);
593 m_bIconized
= nShowCmd
== SWP_MINIMIZE
;
594 } // end of wxTopLevelWindowOS2::DoShowWindow
596 bool wxTopLevelWindowOS2::Show(
606 if (m_bMaximizeOnShow
)
609 m_bMaximizeOnShow
= FALSE
;
620 DoShowWindow(nShowCmd
);
624 wxActivateEvent
vEvent(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
626 ::WinQueryWindowPos(m_hFrame
, &vSwp
);
627 m_bIconized
= vSwp
.fl
& SWP_MINIMIZE
;
628 ::WinQueryWindowPos(m_hWnd
, &m_vSwpClient
);
629 ::WinSendMsg(m_hFrame
, WM_UPDATEFRAME
, (MPARAM
)~0, 0);
630 ::WinEnableWindow(m_hFrame
, TRUE
);
631 vEvent
.SetEventObject(this);
632 GetEventHandler()->ProcessEvent(vEvent
);
637 // Try to highlight the correct window (the parent)
641 HWND hWndParent
= GetHwndOf(GetParent());
643 ::WinQueryWindowPos(hWndParent
, &vSwp
);
644 m_bIconized
= vSwp
.fl
& SWP_MINIMIZE
;
646 ::WinSetWindowPos( hWndParent
652 ,SWP_ZORDER
| SWP_ACTIVATE
| SWP_SHOW
| SWP_MOVE
654 ::WinEnableWindow(hWndParent
, TRUE
);
658 } // end of wxTopLevelWindowOS2::Show
660 // ----------------------------------------------------------------------------
661 // wxTopLevelWindowOS2 maximize/minimize
662 // ----------------------------------------------------------------------------
664 void wxTopLevelWindowOS2::Maximize(
671 // Just maximize it directly
673 DoShowWindow(bMaximize
? SWP_MAXIMIZE
: SWP_RESTORE
);
678 // We can't maximize the hidden frame because it shows it as well, so
679 // just remember that we should do it later in this case
681 m_bMaximizeOnShow
= TRUE
;
683 } // end of wxTopLevelWindowOS2::Maximize
685 bool wxTopLevelWindowOS2::IsMaximized() const
689 ::WinQueryWindowPos(m_hFrame
, (PSWP
)&m_vSwp
);
690 return (m_vSwp
.fl
& SWP_MAXIMIZE
);
691 } // end of wxTopLevelWindowOS2::IsMaximized
693 void wxTopLevelWindowOS2::Iconize(
697 DoShowWindow(bIconize
? SWP_MINIMIZE
: SWP_RESTORE
);
698 } // end of wxTopLevelWindowOS2::Iconize
700 bool wxTopLevelWindowOS2::IsIconized() const
702 // also update the current state
703 ::WinQueryWindowPos(m_hFrame
, (PSWP
)&m_vSwp
);
704 if (m_vSwp
.fl
& SWP_MINIMIZE
)
705 ((wxTopLevelWindow
*)this)->m_bIconized
= TRUE
;
707 ((wxTopLevelWindow
*)this)->m_bIconized
= FALSE
;
709 } // end of wxTopLevelWindowOS2::IsIconized
711 void wxTopLevelWindowOS2::Restore()
713 DoShowWindow(SWP_RESTORE
);
714 } // end of wxTopLevelWindowOS2::Restore
716 // ----------------------------------------------------------------------------
717 // wxTopLevelWindowOS2 fullscreen
718 // ----------------------------------------------------------------------------
720 bool wxTopLevelWindowOS2::ShowFullScreen(
730 m_bFsIsShowing
= TRUE
;
734 // Zap the frame borders
738 // Save the 'normal' window lStyle
740 m_lFsOldWindowStyle
= ::WinQueryWindowULong( (HWND
)GetHWND()
745 // Save the old position, width & height, maximize state
747 m_vFsOldSize
= GetRect();
748 m_bFsIsMaximized
= IsMaximized();
751 // Decide which window lStyle flags to turn off
753 LONG lNewStyle
= m_lFsOldWindowStyle
;
756 if (lStyle
& wxFULLSCREEN_NOBORDER
)
757 lOffFlags
|= FCF_BORDER
;
758 if (lStyle
& wxFULLSCREEN_NOCAPTION
)
759 lOffFlags
|= (FCF_TASKLIST
| FCF_SYSMENU
);
761 lNewStyle
&= (~lOffFlags
);
764 // Change our window style to be compatible with full-screen mode
766 ::WinSetWindowULong( (HWND
)GetHWND()
772 // Resize to the size of the desktop
776 RECTL vRect
= wxGetWindowRect(HWND_DESKTOP
);
778 nWidth
= vRect
.xRight
- vRect
.xLeft
;
779 nHeight
= vRect
.yTop
- vRect
.yBottom
;
786 // Now flush the window style cache and actually go full-screen
788 ::WinSetWindowPos( m_hFrame
797 wxSizeEvent
vEvent( wxSize( nWidth
803 GetEventHandler()->ProcessEvent(vEvent
);
811 m_bFsIsShowing
= FALSE
;
812 Maximize(m_bFsIsMaximized
);
813 ::WinSetWindowULong( (HWND
)GetHWND()
817 ::WinSetWindowPos( m_hFrame
827 } // end of wxTopLevelWindowOS2::ShowFullScreen
829 // ----------------------------------------------------------------------------
830 // wxTopLevelWindowOS2 misc
831 // ----------------------------------------------------------------------------
833 void wxTopLevelWindowOS2::SetIcon(
840 wxTopLevelWindowBase::SetIcon(rIcon
);
844 ::WinSendMsg( m_hFrame
846 ,(MPARAM
)((HPOINTER
)m_icon
.GetHICON())
849 ::WinSendMsg( m_hFrame
855 } // end of wxTopLevelWindowOS2::SetIcon
857 bool wxTopLevelWindowOS2::EnableCloseButton(
862 // Get system (a.k.a. window) menu
864 HMENU hMenu
= ::WinWindowFromID(m_hFrame
, FID_SYSMENU
);
868 wxLogLastError(_T("GetSystemMenu"));
873 // Enabling/disabling the close item from it also automatically
874 // disables/enables the close title bar button
877 (void)::WinSendMsg( hMenu
879 ,MPFROM2SHORT(SC_CLOSE
, FALSE
)
880 ,MPFROM2SHORT(MIA_DISABLED
, FALSE
)
883 (void)::WinSendMsg( hMenu
885 ,MPFROM2SHORT(SC_CLOSE
, FALSE
)
886 ,MPFROM2SHORT(MIA_DISABLED
, MIA_DISABLED
)
890 // Update appearance immediately
892 ::WinSendMsg( m_hFrame
898 } // end of wxTopLevelWindowOS2::EnableCloseButton