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 licence
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/containr.h" // wxSetFocusToChild()
41 #include "wx/module.h"
43 #include "wx/msw/private.h"
45 #include "wx/display.h"
55 // ----------------------------------------------------------------------------
56 // stubs for missing functions under MicroWindows
57 // ----------------------------------------------------------------------------
61 // static inline bool IsIconic(HWND WXUNUSED(hwnd)) { return FALSE; }
62 static inline bool IsZoomed(HWND
WXUNUSED(hwnd
)) { return FALSE
; }
64 #endif // __WXMICROWIN__
66 // NB: wxDlgProc must be defined here and not in dialog.cpp because the latter
67 // is not included by wxUniv build which does need wxDlgProc
69 wxDlgProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
);
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 // list of all frames and modeless dialogs
76 wxWindowList wxModelessWindows
;
78 // the name of the default wxWindows class
79 extern const wxChar
*wxCanvasClassName
;
81 // ----------------------------------------------------------------------------
82 // wxTLWHiddenParentModule: used to manage the hidden parent window (we need a
83 // module to ensure that the window is always deleted)
84 // ----------------------------------------------------------------------------
86 class wxTLWHiddenParentModule
: public wxModule
89 // module init/finalize
90 virtual bool OnInit();
91 virtual void OnExit();
93 // get the hidden window (creates on demand)
94 static HWND
GetHWND();
97 // the HWND of the hidden parent
100 // the class used to create it
101 static const wxChar
*ms_className
;
103 DECLARE_DYNAMIC_CLASS(wxTLWHiddenParentModule
)
106 IMPLEMENT_DYNAMIC_CLASS(wxTLWHiddenParentModule
, wxModule
)
108 // ============================================================================
109 // wxTopLevelWindowMSW implementation
110 // ============================================================================
112 BEGIN_EVENT_TABLE(wxTopLevelWindowMSW
, wxTopLevelWindowBase
)
113 EVT_ACTIVATE(wxTopLevelWindowMSW::OnActivate
)
116 // ----------------------------------------------------------------------------
117 // wxTopLevelWindowMSW creation
118 // ----------------------------------------------------------------------------
120 void wxTopLevelWindowMSW::Init()
123 m_maximizeOnShow
= FALSE
;
125 // unlike (almost?) all other windows, frames are created hidden
128 // Data to save/restore when calling ShowFullScreen
130 m_fsOldWindowStyle
= 0;
131 m_fsIsMaximized
= FALSE
;
132 m_fsIsShowing
= FALSE
;
134 m_winLastFocused
= (wxWindow
*)NULL
;
137 WXDWORD
wxTopLevelWindowMSW::MSWGetStyle(long style
, WXDWORD
*exflags
) const
139 // let the base class deal with the common styles but fix the ones which
140 // don't make sense for us (we also deal with the borders ourselves)
141 WXDWORD msflags
= wxWindow::MSWGetStyle
143 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exflags
146 // first select the kind of window being created
148 // note that if we don't set WS_POPUP, Windows assumes WS_OVERLAPPED and
149 // creates a window with both caption and border, hence we also test it
150 // below in some other cases
151 if ( style
& wxFRAME_TOOL_WINDOW
)
154 msflags
|= WS_OVERLAPPED
;
156 // border and caption styles
157 if ( style
& wxRESIZE_BORDER
)
158 msflags
|= WS_THICKFRAME
;
159 else if ( !(style
& wxBORDER_NONE
) )
160 msflags
|= WS_BORDER
;
164 if ( style
& wxCAPTION
)
165 msflags
|= WS_CAPTION
;
169 // next translate the individual flags
170 if ( style
& wxMINIMIZE_BOX
)
171 msflags
|= WS_MINIMIZEBOX
;
172 if ( style
& wxMAXIMIZE_BOX
)
173 msflags
|= WS_MAXIMIZEBOX
;
174 if ( style
& wxSYSTEM_MENU
)
175 msflags
|= WS_SYSMENU
;
176 if ( style
& wxMINIMIZE
)
177 msflags
|= WS_MINIMIZE
;
178 if ( style
& wxMAXIMIZE
)
179 msflags
|= WS_MAXIMIZE
;
181 // Keep this here because it saves recoding this function in wxTinyFrame
182 #if wxUSE_ITSY_BITSY && !defined(__WIN32__)
183 if ( style
& wxTINY_CAPTION_VERT
)
184 msflags
|= IBS_VERTCAPTION
;
185 if ( style
& wxTINY_CAPTION_HORIZ
)
186 msflags
|= IBS_HORZCAPTION
;
188 if ( style
& (wxTINY_CAPTION_VERT
| wxTINY_CAPTION_HORIZ
) )
189 msflags
|= WS_CAPTION
;
194 #if !defined(__WIN16__) && !defined(__SC__)
195 if ( !(GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
) )
197 if ( style
& wxFRAME_TOOL_WINDOW
)
199 // create the palette-like window
200 *exflags
|= WS_EX_TOOLWINDOW
;
203 // We have to solve 2 different problems here:
205 // 1. frames with wxFRAME_NO_TASKBAR flag shouldn't appear in the
206 // taskbar even if they don't have a parent
208 // 2. frames without this style should appear in the taskbar even
209 // if they're owned (Windows only puts non owned windows into
210 // the taskbar normally)
212 // The second one is solved here by using WS_EX_APPWINDOW flag, the
213 // first one is dealt with in our MSWGetParent() method
215 if ( !(style
& wxFRAME_NO_TASKBAR
) && GetParent() )
217 // need to force the frame to appear in the taskbar
218 *exflags
|= WS_EX_APPWINDOW
;
220 //else: nothing to do [here]
224 if ( style
& wxSTAY_ON_TOP
)
225 *exflags
|= WS_EX_TOPMOST
;
228 if ( GetExtraStyle() & wxFRAME_EX_CONTEXTHELP
)
229 *exflags
|= WS_EX_CONTEXTHELP
;
236 WXHWND
wxTopLevelWindowMSW::MSWGetParent() const
238 // for the frames without wxFRAME_FLOAT_ON_PARENT style we should use NULL
239 // parent HWND or it would be always on top of its parent which is not what
240 // we usually want (in fact, we only want it for frames with the
241 // wxFRAME_FLOAT_ON_PARENT flag)
242 HWND hwndParent
= NULL
;
243 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT
) )
245 const wxWindow
*parent
= GetParent();
249 // this flag doesn't make sense then and will be ignored
250 wxFAIL_MSG( _T("wxFRAME_FLOAT_ON_PARENT but no parent?") );
254 hwndParent
= GetHwndOf(parent
);
257 //else: don't float on parent, must not be owned
259 // now deal with the 2nd taskbar-related problem (see comments above in
261 if ( HasFlag(wxFRAME_NO_TASKBAR
) && !hwndParent
)
264 hwndParent
= wxTLWHiddenParentModule::GetHWND();
267 return (WXHWND
)hwndParent
;
270 bool wxTopLevelWindowMSW::CreateDialog(const void *dlgTemplate
,
271 const wxString
& title
,
275 #ifdef __WXMICROWIN__
276 // no dialogs support under MicroWin yet
277 return CreateFrame(title
, pos
, size
);
278 #else // !__WXMICROWIN__
279 wxWindow
*parent
= GetParent();
281 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
282 // app window as parent - this avoids creating modal dialogs without
284 if ( !parent
&& !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
) )
286 parent
= wxTheApp
->GetTopWindow();
290 // don't use transient windows as parents, this is dangerous as it
291 // can lead to a crash if the parent is destroyed before the child
293 // also don't use the window which is currently hidden as then the
294 // dialog would be hidden as well
295 if ( (parent
->GetExtraStyle() & wxWS_EX_TRANSIENT
) ||
303 m_hWnd
= (WXHWND
)::CreateDialogIndirect
306 (DLGTEMPLATE
*)dlgTemplate
,
307 parent
? GetHwndOf(parent
) : NULL
,
313 wxFAIL_MSG(wxT("Failed to create dialog. Incorrect DLGTEMPLATE?"));
315 wxLogSysError(wxT("Can't create dialog using memory template"));
321 (void)MSWGetCreateWindowFlags(&exflags
);
325 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exflags
);
326 ::SetWindowPos(GetHwnd(), NULL
, 0, 0, 0, 0,
333 #if defined(__WIN95__)
334 // For some reason, the system menu is activated when we use the
335 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
336 if ( exflags
& WS_EX_CONTEXTHELP
)
338 wxFrame
*winTop
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
341 wxIcon icon
= winTop
->GetIcon();
344 ::SendMessage(GetHwnd(), WM_SETICON
,
346 (LPARAM
)GetHiconOf(icon
));
352 // move the dialog to its initial position without forcing repainting
354 if ( !MSWGetCreateWindowCoords(pos
, size
, x
, y
, w
, h
) )
357 w
= (int)CW_USEDEFAULT
;
360 // we can't use CW_USEDEFAULT here as we're not calling CreateWindow()
361 // and passing CW_USEDEFAULT to MoveWindow() results in resizing the
362 // window to (0, 0) size which breaks quite a lot of things, e.g. the
363 // sizer calculation in wxSizer::Fit()
364 if ( w
== (int)CW_USEDEFAULT
)
366 // the exact number doesn't matter, the dialog will be resized
367 // again soon anyhow but it should be big enough to allow
368 // calculation relying on "totalSize - clientSize > 0" work, i.e.
369 // at least greater than the title bar height
374 if ( x
== (int)CW_USEDEFAULT
)
376 // centre it on the screen - what else can we do?
377 wxSize sizeDpy
= wxGetDisplaySize();
379 x
= (sizeDpy
.x
- w
) / 2;
380 y
= (sizeDpy
.y
- h
) / 2;
383 if ( !::MoveWindow(GetHwnd(), x
, y
, w
, h
, FALSE
) )
385 wxLogLastError(wxT("MoveWindow"));
388 if ( !title
.empty() )
390 ::SetWindowText(GetHwnd(), title
);
396 #endif // __WXMICROWIN__/!__WXMICROWIN__
399 bool wxTopLevelWindowMSW::CreateFrame(const wxString
& title
,
404 WXDWORD flags
= MSWGetCreateWindowFlags(&exflags
);
406 return MSWCreate(wxCanvasClassName
, title
, pos
, size
, flags
, exflags
);
409 bool wxTopLevelWindowMSW::Create(wxWindow
*parent
,
411 const wxString
& title
,
415 const wxString
& name
)
422 m_windowStyle
= style
;
426 m_windowId
= id
== -1 ? NewControlId() : id
;
428 wxTopLevelWindows
.Append(this);
431 parent
->AddChild(this);
433 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
435 // we have different dialog templates to allows creation of dialogs
436 // with & without captions under MSWindows, resizeable or not (but a
437 // resizeable dialog always has caption - otherwise it would look too
440 // we need 3 additional WORDs for dialog menu, class and title (as we
441 // don't use DS_SETFONT we don't need the fourth WORD for the font)
442 static const int dlgsize
= sizeof(DLGTEMPLATE
) + (sizeof(WORD
) * 3);
443 DLGTEMPLATE
*dlgTemplate
= (DLGTEMPLATE
*)malloc(dlgsize
);
444 memset(dlgTemplate
, 0, dlgsize
);
446 // these values are arbitrary, they won't be used normally anyhow
449 dlgTemplate
->cx
= 144;
450 dlgTemplate
->cy
= 75;
452 // reuse the code in MSWGetStyle() but correct the results slightly for
454 dlgTemplate
->style
= MSWGetStyle(style
, NULL
);
456 // all dialogs are popups
457 dlgTemplate
->style
|= WS_POPUP
;
459 // force 3D-look if necessary, it looks impossibly ugly otherwise
460 if ( style
& (wxRESIZE_BORDER
| wxCAPTION
) )
461 dlgTemplate
->style
|= DS_MODALFRAME
;
463 ret
= CreateDialog(dlgTemplate
, title
, pos
, size
);
468 ret
= CreateFrame(title
, pos
, size
);
471 if ( ret
&& !(GetWindowStyleFlag() & wxCLOSE_BOX
) )
473 EnableCloseButton(false);
479 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
481 if ( wxModelessWindows
.Find(this) )
482 wxModelessWindows
.DeleteObject(this);
484 // after destroying an owned window, Windows activates the next top level
485 // window in Z order but it may be different from our owner (to reproduce
486 // this simply Alt-TAB to another application and back before closing the
487 // owned frame) whereas we always want to yield activation to our parent
488 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT
) )
490 wxWindow
*parent
= GetParent();
493 ::BringWindowToTop(GetHwndOf(parent
));
498 // ----------------------------------------------------------------------------
499 // wxTopLevelWindowMSW showing
500 // ----------------------------------------------------------------------------
502 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd
)
504 ::ShowWindow(GetHwnd(), nShowCmd
);
506 m_iconized
= nShowCmd
== SW_MINIMIZE
;
509 bool wxTopLevelWindowMSW::Show(bool show
)
511 // don't use wxWindow version as we want to call DoShowWindow() ourselves
512 if ( !wxWindowBase::Show(show
) )
518 if ( m_maximizeOnShow
)
521 nShowCmd
= SW_MAXIMIZE
;
523 m_maximizeOnShow
= FALSE
;
527 if ( GetWindowStyle() & wxFRAME_TOOL_WINDOW
)
528 nShowCmd
= SW_SHOWNA
;
538 DoShowWindow(nShowCmd
);
542 ::BringWindowToTop(GetHwnd());
544 wxActivateEvent
event(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
545 event
.SetEventObject( this );
546 GetEventHandler()->ProcessEvent(event
);
550 // Try to highlight the correct window (the parent)
553 HWND hWndParent
= GetHwndOf(GetParent());
555 ::BringWindowToTop(hWndParent
);
562 // ----------------------------------------------------------------------------
563 // wxTopLevelWindowMSW maximize/minimize
564 // ----------------------------------------------------------------------------
566 void wxTopLevelWindowMSW::Maximize(bool maximize
)
570 // just maximize it directly
571 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
575 // we can't maximize the hidden frame because it shows it as well, so
576 // just remember that we should do it later in this case
577 m_maximizeOnShow
= maximize
;
581 bool wxTopLevelWindowMSW::IsMaximized() const
583 return ::IsZoomed(GetHwnd()) != 0;
586 void wxTopLevelWindowMSW::Iconize(bool iconize
)
588 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
591 bool wxTopLevelWindowMSW::IsIconized() const
593 // also update the current state
594 ((wxTopLevelWindowMSW
*)this)->m_iconized
= ::IsIconic(GetHwnd()) != 0;
599 void wxTopLevelWindowMSW::Restore()
601 DoShowWindow(SW_RESTORE
);
604 // ----------------------------------------------------------------------------
605 // wxTopLevelWindowMSW fullscreen
606 // ----------------------------------------------------------------------------
608 bool wxTopLevelWindowMSW::ShowFullScreen(bool show
, long style
)
610 if ( show
== IsFullScreen() )
616 m_fsIsShowing
= show
;
622 // zap the frame borders
624 // save the 'normal' window style
625 m_fsOldWindowStyle
= GetWindowLong(GetHwnd(), GWL_STYLE
);
627 // save the old position, width & height, maximize state
628 m_fsOldSize
= GetRect();
629 m_fsIsMaximized
= IsMaximized();
631 // decide which window style flags to turn off
632 LONG newStyle
= m_fsOldWindowStyle
;
635 if (style
& wxFULLSCREEN_NOBORDER
)
636 offFlags
|= WS_BORDER
| WS_THICKFRAME
;
637 if (style
& wxFULLSCREEN_NOCAPTION
)
638 offFlags
|= WS_CAPTION
| WS_SYSMENU
;
640 newStyle
&= ~offFlags
;
642 // change our window style to be compatible with full-screen mode
643 ::SetWindowLong(GetHwnd(), GWL_STYLE
, newStyle
);
647 // resize to the size of the display containing us
648 int dpy
= wxDisplay::GetFromWindow(this);
649 if ( dpy
!= wxNOT_FOUND
)
651 rect
= wxDisplay(dpy
).GetGeometry();
653 else // fall back to the main desktop
654 #else // wxUSE_DISPLAY
656 // resize to the size of the desktop
657 wxCopyRECTToRect(wxGetWindowRect(::GetDesktopWindow()), rect
);
659 #endif // wxUSE_DISPLAY
663 // now flush the window style cache and actually go full-screen
664 long flags
= SWP_FRAMECHANGED
;
666 // showing the frame full screen should also show it if it's still
670 // don't call wxWindow version to avoid flicker from calling
671 // ::ShowWindow() -- we're going to show the window at the correct
672 // location directly below -- but do call the wxWindowBase version
673 // to sync the internal m_isShown flag
674 wxWindowBase::Show();
676 flags
|= SWP_SHOWWINDOW
;
679 SetWindowPos(GetHwnd(), HWND_TOP
,
680 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
683 // finally send an event allowing the window to relayout itself &c
684 wxSizeEvent
event(rect
.GetSize(), GetId());
685 GetEventHandler()->ProcessEvent(event
);
687 else // stop showing full screen
689 Maximize(m_fsIsMaximized
);
690 SetWindowLong(GetHwnd(),GWL_STYLE
, m_fsOldWindowStyle
);
691 SetWindowPos(GetHwnd(),HWND_TOP
,m_fsOldSize
.x
, m_fsOldSize
.y
,
692 m_fsOldSize
.width
, m_fsOldSize
.height
, SWP_FRAMECHANGED
);
698 // ----------------------------------------------------------------------------
699 // wxTopLevelWindowMSW misc
700 // ----------------------------------------------------------------------------
702 void wxTopLevelWindowMSW::SetIcon(const wxIcon
& icon
)
704 SetIcons( wxIconBundle( icon
) );
707 void wxTopLevelWindowMSW::SetIcons(const wxIconBundle
& icons
)
709 wxTopLevelWindowBase::SetIcons(icons
);
711 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
712 const wxIcon
& sml
= icons
.GetIcon( wxSize( 16, 16 ) );
713 if( sml
.Ok() && sml
.GetWidth() == 16 && sml
.GetHeight() == 16 )
715 ::SendMessage( GetHwndOf( this ), WM_SETICON
, ICON_SMALL
,
716 (LPARAM
)GetHiconOf(sml
) );
719 const wxIcon
& big
= icons
.GetIcon( wxSize( 32, 32 ) );
720 if( big
.Ok() && big
.GetWidth() == 32 && big
.GetHeight() == 32 )
722 ::SendMessage( GetHwndOf( this ), WM_SETICON
, ICON_BIG
,
723 (LPARAM
)GetHiconOf(big
) );
728 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable
)
730 #ifndef __WXMICROWIN__
731 // get system (a.k.a. window) menu
732 HMENU hmenu
= ::GetSystemMenu(GetHwnd(), FALSE
/* get it */);
735 wxLogLastError(_T("GetSystemMenu"));
740 // enabling/disabling the close item from it also automatically
741 // disables/enables the close title bar button
742 if ( ::EnableMenuItem(hmenu
, SC_CLOSE
,
744 (enable
? MF_ENABLED
: MF_GRAYED
)) == -1 )
746 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
751 // update appearance immediately
752 if ( !::DrawMenuBar(GetHwnd()) )
754 wxLogLastError(_T("DrawMenuBar"));
756 #endif // !__WXMICROWIN__
761 // ----------------------------------------------------------------------------
762 // wxTopLevelWindow event handling
763 // ----------------------------------------------------------------------------
765 // Default activation behaviour - set the focus for the first child
767 void wxTopLevelWindowMSW::OnActivate(wxActivateEvent
& event
)
769 if ( event
.GetActive() )
771 // restore focus to the child which was last focused
772 wxLogTrace(_T("focus"), _T("wxTLW %08x activated."), (int) m_hWnd
);
774 wxWindow
*parent
= m_winLastFocused
? m_winLastFocused
->GetParent()
781 wxSetFocusToChild(parent
, &m_winLastFocused
);
785 // remember the last focused child if it is our child
786 m_winLastFocused
= FindFocus();
788 if ( m_winLastFocused
)
790 // let it know that it doesn't have focus any more
791 m_winLastFocused
->HandleKillFocus((WXHWND
)NULL
);
794 // so we NULL it out if it's a child from some other frame
795 wxWindow
*win
= m_winLastFocused
;
798 if ( win
->IsTopLevel() )
802 m_winLastFocused
= NULL
;
808 win
= win
->GetParent();
811 wxLogTrace(_T("focus"),
812 _T("wxTLW %08x deactivated, last focused: %08x."),
814 (int) (m_winLastFocused
? GetHwndOf(m_winLastFocused
)
821 // the DialogProc for all wxWindows dialogs
822 LONG APIENTRY _EXPORT
823 wxDlgProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
828 // for this message, returning TRUE tells system to set focus to
829 // the first control in the dialog box, but as we set the focus
830 // ourselves, we return FALSE from here as well, so fall through
833 // for all the other ones, FALSE means that we didn't process the
839 // ============================================================================
840 // wxTLWHiddenParentModule implementation
841 // ============================================================================
843 HWND
wxTLWHiddenParentModule::ms_hwnd
= NULL
;
845 const wxChar
*wxTLWHiddenParentModule::ms_className
= NULL
;
847 bool wxTLWHiddenParentModule::OnInit()
855 void wxTLWHiddenParentModule::OnExit()
859 if ( !::DestroyWindow(ms_hwnd
) )
861 wxLogLastError(_T("DestroyWindow(hidden TLW parent)"));
869 if ( !::UnregisterClass(ms_className
, wxGetInstance()) )
871 wxLogLastError(_T("UnregisterClass(\"wxTLWHiddenParent\")"));
879 HWND
wxTLWHiddenParentModule::GetHWND()
885 static const wxChar
*HIDDEN_PARENT_CLASS
= _T("wxTLWHiddenParent");
888 wxZeroMemory(wndclass
);
890 wndclass
.lpfnWndProc
= DefWindowProc
;
891 wndclass
.hInstance
= wxGetInstance();
892 wndclass
.lpszClassName
= HIDDEN_PARENT_CLASS
;
894 if ( !::RegisterClass(&wndclass
) )
896 wxLogLastError(_T("RegisterClass(\"wxTLWHiddenParent\")"));
900 ms_className
= HIDDEN_PARENT_CLASS
;
904 ms_hwnd
= ::CreateWindow(ms_className
, _T(""), 0, 0, 0, 0, 0, NULL
,
905 (HMENU
)NULL
, wxGetInstance(), NULL
);
908 wxLogLastError(_T("CreateWindow(hidden TLW parent)"));