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 void wxAssociateWinWithHandle( HWND hWnd
59 // ============================================================================
60 // wxTopLevelWindowMSW implementation
61 // ============================================================================
64 MRESULT EXPENTRY
wxDlgProc( HWND
WXUNUSED(hWnd
)
66 ,MPARAM
WXUNUSED(wParam
)
67 ,MPARAM
WXUNUSED(lParam
)
70 if (uMessage
== WM_INITDLG
)
73 // For this message, returning TRUE tells system to set focus to the
74 // first control in the dialog box.
81 // For all the other ones, FALSE means that we didn't process the
84 return (MRESULT
)FALSE
;
88 // ----------------------------------------------------------------------------
89 // wxTopLevelWindowOS2 creation
90 // ----------------------------------------------------------------------------
92 void wxTopLevelWindowOS2::Init()
94 m_bIconized
= m_bMaximizeOnShow
= FALSE
;
97 // Unlike (almost?) all other windows, frames are created hidden
102 // Data to save/restore when calling ShowFullScreen
104 m_lFsOldWindowStyle
= 0;
105 m_bFsIsMaximized
= FALSE
;
106 m_bFsIsShowing
= FALSE
;
108 m_hFrame
= NULLHANDLE
;
109 memset(&m_vSwp
, 0, sizeof(SWP
));
110 memset(&m_vSwpClient
, 0, sizeof(SWP
));
111 } // end of wxTopLevelWindowIOS2::Init
113 long wxTopLevelWindowOS2::OS2GetCreateWindowFlags(
117 long lStyle
= GetWindowStyle();
120 if (lStyle
== wxDEFAULT_FRAME_STYLE
)
121 lMsflags
= FCF_SIZEBORDER
| FCF_TITLEBAR
| FCF_SYSMENU
|
122 FCF_MINMAX
| FCF_TASKLIST
;
125 if ((lStyle
& wxCAPTION
) == wxCAPTION
)
126 lMsflags
= FCF_TASKLIST
;
128 lMsflags
= FCF_NOMOVEWITHOWNER
;
130 if ((lStyle
& wxVSCROLL
) == wxVSCROLL
)
131 lMsflags
|= FCF_VERTSCROLL
;
132 if ((lStyle
& wxHSCROLL
) == wxHSCROLL
)
133 lMsflags
|= FCF_HORZSCROLL
;
134 if (lStyle
& wxMINIMIZE_BOX
)
135 lMsflags
|= FCF_MINBUTTON
;
136 if (lStyle
& wxMAXIMIZE_BOX
)
137 lMsflags
|= FCF_MAXBUTTON
;
138 if (lStyle
& wxTHICK_FRAME
)
139 lMsflags
|= FCF_DLGBORDER
;
140 if (lStyle
& wxSYSTEM_MENU
)
141 lMsflags
|= FCF_SYSMENU
;
142 if (lStyle
& wxCAPTION
)
143 lMsflags
|= FCF_TASKLIST
;
144 if (lStyle
& wxCLIP_CHILDREN
)
146 // Invalid for frame windows under PM
149 if (lStyle
& wxTINY_CAPTION_VERT
)
150 lMsflags
|= FCF_TASKLIST
;
151 if (lStyle
& wxTINY_CAPTION_HORIZ
)
152 lMsflags
|= FCF_TASKLIST
;
154 if ((lStyle
& wxTHICK_FRAME
) == 0)
155 lMsflags
|= FCF_BORDER
;
156 if (lStyle
& wxFRAME_TOOL_WINDOW
)
157 *plExflags
= kFrameToolWindow
;
159 if (lStyle
& wxSTAY_ON_TOP
)
160 lMsflags
|= FCF_SYSMODAL
;
163 } // end of wxTopLevelWindowOS2::OS2GetCreateWindowFlags
165 bool wxTopLevelWindowOS2::CreateDialog(
167 , const wxString
& rsTitle
168 , const wxPoint
& rPos
169 , const wxSize
& rSize
172 wxWindow
* pParent
= GetParent();
175 // For the dialogs without wxDIALOG_NO_PARENT style, use the top level
176 // app window as parent - this avoids creating modal dialogs without
179 if (!pParent
&& !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
))
181 pParent
= wxTheApp
->GetTopWindow();
186 // Don't use transient windows as parents, this is dangerous as it
187 // can lead to a crash if the parent is destroyed before the child
189 // also don't use the window which is currently hidden as then the
190 // dialog would be hidden as well
191 if ((pParent
->GetExtraStyle() & wxWS_EX_TRANSIENT
) ||
203 hWndParent
= GetHwndOf(pParent
);
205 hWndParent
= HWND_DESKTOP
;
207 hWndDlg
= ::WinLoadDlg( hWndParent
211 ,(ULONG
)ulDlgTemplate
215 m_hWnd
= (WXHWND
) hWndDlg
;
219 wxFAIL_MSG(_("Did you forget to include wx/os2/wx.rc in your resources?"));
221 wxLogSysError(_("Can't create dialog using template '%ul'"), ulDlgTemplate
);
227 // Move the dialog to its initial position without forcing repainting
234 if (!OS2GetCreateWindowCoords( rPos
242 nX
= nWidth
= (int)CW_USEDEFAULT
;
246 // We can't use CW_USEDEFAULT here as we're not calling CreateWindow()
247 // and passing CW_USEDEFAULT to MoveWindow() results in resizing the
248 // window to (0, 0) size which breaks quite a lot of things, e.g. the
249 // sizer calculation in wxSizer::Fit()
251 if (nWidth
== (int)CW_USEDEFAULT
)
254 // The exact number doesn't matter, the dialog will be resized
255 // again soon anyhow but it should be big enough to allow
256 // calculation relying on "totalSize - clientSize > 0" work, i.e.
257 // at least greater than the title bar height
259 nWidth
= nHeight
= 100;
261 if (nX
== (int)CW_USEDEFAULT
)
264 // Centre it on the screen - what else can we do?
266 wxSize vSizeDpy
= wxGetDisplaySize();
268 nX
= (vSizeDpy
.x
- nWidth
) / 2;
269 nY
= (vSizeDpy
.y
- nHeight
) / 2;
271 ::WinSetWindowPos( GetHwnd()
277 ,SWP_MOVE
| SWP_SIZE
| SWP_ZORDER
| SWP_SHOW
279 if (!rsTitle
.IsNull())
281 ::WinSetWindowText(GetHwnd(), rsTitle
.c_str());
285 } // end of wxTopLevelWindowOS2::CreateDialog
287 bool wxTopLevelWindowOS2::CreateFrame(
288 const wxString
& rsTitle
289 , const wxPoint
& rPos
290 , const wxSize
& rSize
294 long lFlags
= OS2GetCreateWindowFlags(&lExflags
);
295 long lStyle
= GetWindowStyleFlag();
298 int nWidth
= rSize
.x
;
299 int nHeight
= rSize
.y
;
300 ULONG ulStyleFlags
= 0L;
303 wxWindow
* pParent
= GetParent();
309 hParent
= GetHwndOf(pParent
);
311 hParent
= HWND_DESKTOP
;
313 if ((lStyle
& wxMINIMIZE
) || (lStyle
& wxICONIZE
))
314 ulStyleFlags
|= WS_MINIMIZED
;
315 if (lStyle
& wxMAXIMIZE
)
316 ulStyleFlags
|= WS_MAXIMIZED
;
319 // Clear the visible flag, we always call show
321 ulStyleFlags
&= (unsigned long)~WS_VISIBLE
;
325 // Create the frame window: We break ranks with other ports now
326 // and instead of calling down into the base wxWindow class' OS2Create
327 // we do all our own stuff here. We will set the needed pieces
328 // of wxWindow manually, here.
331 hFrame
= ::WinCreateStdWindow( hParent
332 ,ulStyleFlags
// frame-window style
333 ,(PULONG
)&lFlags
// window style
334 ,(PSZ
)wxFrameClassName
// class name
335 ,(PSZ
)rsTitle
.c_str() // window title
336 ,0L // default client style
337 ,NULLHANDLE
// resource in executable file
339 ,&hClient
// receives client window handle
343 vError
= ::WinGetLastError(vHabmain
);
344 sError
= wxPMErrorToStr(vError
);
345 wxLogError("Error creating frame. Error: %s\n", sError
);
350 // wxWindow class' m_hWnd set here and needed associations
354 wxAssociateWinWithHandle(m_hWnd
, this);
355 wxAssociateWinWithHandle(m_hFrame
, this);
357 m_backgroundColour
.Set(wxString("GREY"));
359 LONG lColor
= (LONG
)m_backgroundColour
.GetPixel();
361 if (!::WinSetPresParam( m_hWnd
367 vError
= ::WinGetLastError(vHabmain
);
368 sError
= wxPMErrorToStr(vError
);
369 wxLogError("Error creating frame. Error: %s\n", sError
);
374 // Now need to subclass window. Instead of calling the SubClassWin in wxWindow
375 // we manually subclass here because we don't want to use the main wxWndProc
378 m_fnOldWndProc
= (WXFARPROC
) ::WinSubclassWindow(m_hFrame
, (PFNWP
)wxFrameMainWndProc
);
381 // Now size everything. If adding a menu the client will need to be resized.
386 nY
= pParent
->GetSize().y
- (nY
+ nHeight
);
392 ::WinQueryWindowRect(HWND_DESKTOP
, &vRect
);
393 nY
= vRect
.yTop
- (nY
+ nHeight
);
395 if (!::WinSetWindowPos( m_hFrame
401 ,SWP_SIZE
| SWP_MOVE
| SWP_ACTIVATE
| SWP_ZORDER
404 vError
= ::WinGetLastError(vHabmain
);
405 sError
= wxPMErrorToStr(vError
);
406 wxLogError("Error sizing frame. Error: %s\n", sError
);
410 } // end of wxTopLevelWindowOS2::CreateFrame
412 bool wxTopLevelWindowOS2::Create(
415 , const wxString
& rsTitle
416 , const wxPoint
& rPos
417 , const wxSize
& rSize
419 , const wxString
& rsName
426 m_windowStyle
= lStyle
;
428 m_windowId
= vId
== -1 ? NewControlId() : vId
;
429 wxTopLevelWindows
.Append(this);
431 pParent
->AddChild(this);
433 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
436 // We have different dialog templates to allows creation of dialogs
437 // with & without captions under OS2indows, resizeable or not (but a
438 // resizeable dialog always has caption - otherwise it would look too
443 if (lStyle
& wxRESIZE_BORDER
)
444 ulDlgTemplate
= (ULONG
)kResizeableDialog
;
445 else if (lStyle
& wxCAPTION
)
446 ulDlgTemplate
= (ULONG
)kCaptionDialog
;
448 ulDlgTemplate
= (ULONG
)kNoCaptionDialog
;
449 return CreateDialog( ulDlgTemplate
457 return CreateFrame( rsTitle
462 } // end of wxTopLevelWindowOS2::Create
464 wxTopLevelWindowOS2::~wxTopLevelWindowOS2()
466 wxTopLevelWindows
.DeleteObject(this);
468 if (wxModelessWindows
.Find(this))
469 wxModelessWindows
.DeleteObject(this);
472 // If this is the last top-level window, exit.
474 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
476 wxTheApp
->SetTopWindow(NULL
);
477 if ( wxTheApp
->GetExitOnFrameDelete() )
479 ::WinPostMsg(NULL
, WM_QUIT
, 0, 0);
482 } // end of wxTopLevelWindowOS2::~wxTopLevelWindowOS2
485 // IF we have child controls in the Frame's client we need to alter
486 // the y position, because, OS/2 controls are positioned relative to
487 // wxWindows orgin (top left) not the OS/2 origin (bottom left)
489 void wxTopLevelWindowOS2::AlterChildPos()
492 // OS/2 is the only OS concerned about this
494 wxWindow
* pChild
= NULL
;
495 wxControl
* pCtrl
= NULL
;
499 ::WinQueryWindowRect(GetHwnd(), &vRect
);
500 for (wxWindowList::Node
* pNode
= GetChildren().GetFirst();
502 pNode
= pNode
->GetNext())
504 wxWindow
* pChild
= pNode
->GetData();
506 ::WinQueryWindowPos(pChild
->GetHWND(), &vSwp
);
507 vSwp
.y
+= (vRect
.yTop
- m_vSwpClient
.cy
);
508 if (pChild
->IsKindOf(CLASSINFO(wxControl
)))
510 pCtrl
= wxDynamicCast(pChild
, wxControl
);
512 // Must deal with controls that have margins like ENTRYFIELD. The SWP
513 // struct of such a control will have and origin offset from its intended
514 // position by the width of the margins.
516 vSwp
.y
-= pCtrl
->GetYComp();
517 vSwp
.x
-= pCtrl
->GetXComp();
519 ::WinSetWindowPos( pChild
->GetHWND()
527 ::WinQueryWindowPos(pChild
->GetHWND(), &vSwp
);
530 ::WinQueryWindowPos(GetHwnd(), &m_vSwpClient
);
531 } // end of wxTopLevelWindowOS2::AlterChildPos
533 void wxTopLevelWindowOS2::UpdateInternalSize(
538 pChild
->MoveChildren(m_vSwpClient
.cy
- nHeight
);
539 ::WinQueryWindowPos(GetHwnd(), &m_vSwpClient
);
540 } // end of wxTopLevelWindowOS2::UpdateInternalSize
542 // ----------------------------------------------------------------------------
543 // wxTopLevelWindowOS2 client size
544 // ----------------------------------------------------------------------------
546 void wxTopLevelWindowOS2::DoSetClientSize(
552 // Call GetClientAreaOrigin() to take the toolbar into account
554 wxPoint vPt
= GetClientAreaOrigin();
559 wxWindow::DoSetClientSize( nWidth
562 } // end of wxTopLevelWindowOS2::DoSetClientSize
564 void wxTopLevelWindowOS2::DoGetClientSize(
569 wxWindow::DoGetClientSize( pnX
573 wxPoint vPt
= GetClientAreaOrigin();
580 } // end of wxTopLevelWindowOS2::DoGetClientSize
582 // ----------------------------------------------------------------------------
583 // wxTopLevelWindowOS2 showing
584 // ----------------------------------------------------------------------------
586 void wxTopLevelWindowOS2::DoShowWindow(
590 ::WinShowWindow(m_hFrame
, (BOOL
)nShowCmd
);
591 m_bIconized
= nShowCmd
== SWP_MINIMIZE
;
592 } // end of wxTopLevelWindowOS2::DoShowWindow
594 bool wxTopLevelWindowOS2::Show(
604 if (m_bMaximizeOnShow
)
607 m_bMaximizeOnShow
= FALSE
;
618 DoShowWindow(nShowCmd
);
622 wxActivateEvent
vEvent(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
624 ::WinQueryWindowPos(m_hFrame
, &vSwp
);
625 m_bIconized
= vSwp
.fl
& SWP_MINIMIZE
;
626 ::WinQueryWindowPos(m_hWnd
, &m_vSwpClient
);
627 ::WinSendMsg(m_hFrame
, WM_UPDATEFRAME
, (MPARAM
)~0, 0);
628 ::WinEnableWindow(m_hFrame
, TRUE
);
629 vEvent
.SetEventObject(this);
630 GetEventHandler()->ProcessEvent(vEvent
);
635 // Try to highlight the correct window (the parent)
639 HWND hWndParent
= GetHwndOf(GetParent());
641 ::WinQueryWindowPos(hWndParent
, &vSwp
);
642 m_bIconized
= vSwp
.fl
& SWP_MINIMIZE
;
644 ::WinSetWindowPos( hWndParent
650 ,SWP_ZORDER
| SWP_ACTIVATE
| SWP_SHOW
| SWP_MOVE
652 ::WinEnableWindow(hWndParent
, TRUE
);
656 } // end of wxTopLevelWindowOS2::Show
658 // ----------------------------------------------------------------------------
659 // wxTopLevelWindowOS2 maximize/minimize
660 // ----------------------------------------------------------------------------
662 void wxTopLevelWindowOS2::Maximize(
669 // Just maximize it directly
671 DoShowWindow(bMaximize
? SWP_MAXIMIZE
: SWP_RESTORE
);
676 // We can't maximize the hidden frame because it shows it as well, so
677 // just remember that we should do it later in this case
679 m_bMaximizeOnShow
= TRUE
;
681 } // end of wxTopLevelWindowOS2::Maximize
683 bool wxTopLevelWindowOS2::IsMaximized() const
687 ::WinQueryWindowPos(m_hFrame
, (PSWP
)&m_vSwp
);
688 return (m_vSwp
.fl
& SWP_MAXIMIZE
);
689 } // end of wxTopLevelWindowOS2::IsMaximized
691 void wxTopLevelWindowOS2::Iconize(
695 DoShowWindow(bIconize
? SWP_MINIMIZE
: SWP_RESTORE
);
696 } // end of wxTopLevelWindowOS2::Iconize
698 bool wxTopLevelWindowOS2::IsIconized() const
700 // also update the current state
701 ::WinQueryWindowPos(m_hFrame
, (PSWP
)&m_vSwp
);
702 if (m_vSwp
.fl
& SWP_MINIMIZE
)
703 ((wxTopLevelWindow
*)this)->m_bIconized
= TRUE
;
705 ((wxTopLevelWindow
*)this)->m_bIconized
= FALSE
;
707 } // end of wxTopLevelWindowOS2::IsIconized
709 void wxTopLevelWindowOS2::Restore()
711 DoShowWindow(SWP_RESTORE
);
712 } // end of wxTopLevelWindowOS2::Restore
714 // ----------------------------------------------------------------------------
715 // wxTopLevelWindowOS2 fullscreen
716 // ----------------------------------------------------------------------------
718 bool wxTopLevelWindowOS2::ShowFullScreen(
728 m_bFsIsShowing
= TRUE
;
732 // Zap the frame borders
736 // Save the 'normal' window lStyle
738 m_lFsOldWindowStyle
= ::WinQueryWindowULong( (HWND
)GetHWND()
743 // Save the old position, width & height, maximize state
745 m_vFsOldSize
= GetRect();
746 m_bFsIsMaximized
= IsMaximized();
749 // Decide which window lStyle flags to turn off
751 LONG lNewStyle
= m_lFsOldWindowStyle
;
754 if (lStyle
& wxFULLSCREEN_NOBORDER
)
755 lOffFlags
|= FCF_BORDER
;
756 if (lStyle
& wxFULLSCREEN_NOCAPTION
)
757 lOffFlags
|= (FCF_TASKLIST
| FCF_SYSMENU
);
759 lNewStyle
&= (~lOffFlags
);
762 // Change our window style to be compatible with full-screen mode
764 ::WinSetWindowULong( (HWND
)GetHWND()
770 // Resize to the size of the desktop
774 RECTL vRect
= wxGetWindowRect(HWND_DESKTOP
);
776 nWidth
= vRect
.xRight
- vRect
.xLeft
;
777 nHeight
= vRect
.yTop
- vRect
.yBottom
;
784 // Now flush the window style cache and actually go full-screen
786 ::WinSetWindowPos( m_hFrame
795 wxSizeEvent
vEvent( wxSize( nWidth
801 GetEventHandler()->ProcessEvent(vEvent
);
809 m_bFsIsShowing
= FALSE
;
810 Maximize(m_bFsIsMaximized
);
811 ::WinSetWindowULong( (HWND
)GetHWND()
815 ::WinSetWindowPos( m_hFrame
825 } // end of wxTopLevelWindowOS2::ShowFullScreen
827 // ----------------------------------------------------------------------------
828 // wxTopLevelWindowOS2 misc
829 // ----------------------------------------------------------------------------
831 void wxTopLevelWindowOS2::SetIcon(
838 wxTopLevelWindowBase::SetIcon(rIcon
);
842 ::WinSendMsg( m_hFrame
844 ,(MPARAM
)((HPOINTER
)m_icon
.GetHICON())
847 ::WinSendMsg( m_hFrame
853 } // end of wxTopLevelWindowOS2::SetIcon
855 bool wxTopLevelWindowOS2::EnableCloseButton(
860 // Get system (a.k.a. window) menu
862 HMENU hMenu
= ::WinWindowFromID(m_hFrame
, FID_SYSMENU
);
866 wxLogLastError(_T("GetSystemMenu"));
871 // Enabling/disabling the close item from it also automatically
872 // disables/enables the close title bar button
875 (void)::WinSendMsg( hMenu
877 ,MPFROM2SHORT(SC_CLOSE
, FALSE
)
878 ,MPFROM2SHORT(MIA_DISABLED
, FALSE
)
881 (void)::WinSendMsg( hMenu
883 ,MPFROM2SHORT(SC_CLOSE
, FALSE
)
884 ,MPFROM2SHORT(MIA_DISABLED
, MIA_DISABLED
)
888 // Update appearance immediately
890 ::WinSendMsg( m_hFrame
896 } // end of wxTopLevelWindowOS2::EnableCloseButton