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/containr.h" // wxSetFocusToChild()
41 #include "wx/msw/private.h"
43 #include "wx/popupwin.h"
53 // ----------------------------------------------------------------------------
54 // stubs for missing functions under MicroWindows
55 // ----------------------------------------------------------------------------
59 // static inline bool IsIconic(HWND WXUNUSED(hwnd)) { return FALSE; }
60 static inline bool IsZoomed(HWND
WXUNUSED(hwnd
)) { return FALSE
; }
62 #endif // __WXMICROWIN__
64 // this is defined in dialog.cpp
66 wxDlgProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
);
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 // list of all frames and modeless dialogs
73 wxWindowList wxModelessWindows
;
75 // the name of the default wxWindows class
76 extern const wxChar
*wxCanvasClassName
;
78 // the hidden parent for wxFRAME_NO_TASKBAR unowned frames
79 wxWindow
*wxTopLevelWindowMSW::ms_hiddenParent
= NULL
;
81 // ============================================================================
82 // wxTopLevelWindowMSW implementation
83 // ============================================================================
85 BEGIN_EVENT_TABLE(wxTopLevelWindowMSW
, wxTopLevelWindowBase
)
86 EVT_ACTIVATE(wxTopLevelWindowMSW::OnActivate
)
89 // ----------------------------------------------------------------------------
90 // wxTopLevelWindowMSW creation
91 // ----------------------------------------------------------------------------
93 void wxTopLevelWindowMSW::Init()
96 m_maximizeOnShow
= FALSE
;
98 // unlike (almost?) all other windows, frames are created hidden
101 // Data to save/restore when calling ShowFullScreen
103 m_fsOldWindowStyle
= 0;
104 m_fsIsMaximized
= FALSE
;
105 m_fsIsShowing
= FALSE
;
107 m_winLastFocused
= (wxWindow
*)NULL
;
110 WXDWORD
wxTopLevelWindowMSW::MSWGetStyle(long style
, WXDWORD
*exflags
) const
112 // let the base class deal with the common styles but fix the ones which
113 // don't make sense for us (we also deal with the borders ourselves)
114 WXDWORD msflags
= wxWindow::MSWGetStyle
116 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exflags
119 // first select the kind of window being created
121 // note that if we don't set WS_POPUP, Windows assumes WS_OVERLAPPED and
122 // creates a window with both caption and border, hence we also test it
123 // below in some other cases
124 if ( style
& wxFRAME_TOOL_WINDOW
)
127 msflags
|= WS_OVERLAPPED
;
129 // border and caption styles
130 if ( style
& wxRESIZE_BORDER
)
131 msflags
|= WS_THICKFRAME
;
132 else if ( !(style
& wxBORDER_NONE
) )
133 msflags
|= WS_BORDER
;
137 if ( style
& wxCAPTION
)
138 msflags
|= WS_CAPTION
;
142 // next translate the individual flags
143 if ( style
& wxMINIMIZE_BOX
)
144 msflags
|= WS_MINIMIZEBOX
;
145 if ( style
& wxMAXIMIZE_BOX
)
146 msflags
|= WS_MAXIMIZEBOX
;
147 if ( style
& wxSYSTEM_MENU
)
148 msflags
|= WS_SYSMENU
;
149 if ( style
& wxMINIMIZE
)
150 msflags
|= WS_MINIMIZE
;
151 if ( style
& wxMAXIMIZE
)
152 msflags
|= WS_MAXIMIZE
;
154 // Keep this here because it saves recoding this function in wxTinyFrame
155 #if wxUSE_ITSY_BITSY && !defined(__WIN32__)
156 if ( style
& wxTINY_CAPTION_VERT
)
157 msflags
|= IBS_VERTCAPTION
;
158 if ( style
& wxTINY_CAPTION_HORIZ
)
159 msflags
|= IBS_HORZCAPTION
;
161 if ( style
& (wxTINY_CAPTION_VERT
| wxTINY_CAPTION_HORIZ
) )
162 msflags
|= WS_CAPTION
;
167 #if !defined(__WIN16__) && !defined(__SC__)
168 if ( !(GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
) )
170 if ( style
& wxFRAME_TOOL_WINDOW
)
172 // create the palette-like window
173 *exflags
|= WS_EX_TOOLWINDOW
;
176 // We have to solve 2 different problems here:
178 // 1. frames with wxFRAME_NO_TASKBAR flag shouldn't appear in the
179 // taskbar even if they don't have a parent
181 // 2. frames without this style should appear in the taskbar even
182 // if they're owned (Windows only puts non owned windows into
183 // the taskbar normally)
185 // The second one is solved here by using WS_EX_APPWINDOW flag, the
186 // first one is dealt with in our MSWGetParent() method
188 if ( !(style
& wxFRAME_NO_TASKBAR
) && GetParent() )
190 // need to force the frame to appear in the taskbar
191 *exflags
|= WS_EX_APPWINDOW
;
193 //else: nothing to do [here]
197 if ( style
& wxSTAY_ON_TOP
)
198 *exflags
|= WS_EX_TOPMOST
;
201 if ( GetExtraStyle() & wxFRAME_EX_CONTEXTHELP
)
202 *exflags
|= WS_EX_CONTEXTHELP
;
209 WXHWND
wxTopLevelWindowMSW::MSWGetParent() const
211 // for the frames without wxFRAME_FLOAT_ON_PARENT style we should use NULL
212 // parent HWND or it would be always on top of its parent which is not what
213 // we usually want (in fact, we only want it for frames with the
214 // wxFRAME_FLOAT_ON_PARENT flag)
216 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT
) )
218 parent
= GetParent();
220 // this flag doesn't make sense then and will be ignored
221 wxASSERT_MSG( parent
,
222 _T("wxFRAME_FLOAT_ON_PARENT but no parent?") );
224 else // don't float on parent, must not be owned
229 // now deal with the 2nd taskbar-related problem (see comments above in
231 if ( HasFlag(wxFRAME_NO_TASKBAR
) && !parent
)
233 if ( !ms_hiddenParent
)
235 ms_hiddenParent
= new wxTopLevelWindowMSW(NULL
, -1, _T(""));
237 // we shouldn't leave it in wxTopLevelWindows or we wouldn't
238 // terminate the app when the last user-created frame is deleted --
239 // see ~wxTopLevelWindowMSW
240 wxTopLevelWindows
.DeleteObject(ms_hiddenParent
);
243 parent
= ms_hiddenParent
;
246 return parent
? parent
->GetHWND() : NULL
;
249 bool wxTopLevelWindowMSW::CreateDialog(const void *dlgTemplate
,
250 const wxString
& title
,
254 #ifdef __WXMICROWIN__
255 // no dialogs support under MicroWin yet
256 return CreateFrame(title
, pos
, size
);
257 #else // !__WXMICROWIN__
258 wxWindow
*parent
= GetParent();
260 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
261 // app window as parent - this avoids creating modal dialogs without
263 if ( !parent
&& !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
) )
265 parent
= wxTheApp
->GetTopWindow();
269 // don't use transient windows as parents, this is dangerous as it
270 // can lead to a crash if the parent is destroyed before the child
272 // also don't use the window which is currently hidden as then the
273 // dialog would be hidden as well
274 if ( (parent
->GetExtraStyle() & wxWS_EX_TRANSIENT
) ||
282 m_hWnd
= (WXHWND
)::CreateDialogIndirect
285 (DLGTEMPLATE
*)dlgTemplate
,
286 parent
? GetHwndOf(parent
) : NULL
,
292 wxFAIL_MSG(_("Failed to create dialog. Incorrect DLGTEMPLATE?"));
294 wxLogSysError(_("Can't create dialog using memory template"));
300 (void)MSWGetCreateWindowFlags(&exflags
);
304 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exflags
);
305 ::SetWindowPos(GetHwnd(), NULL
, 0, 0, 0, 0,
312 #if defined(__WIN95__)
313 // For some reason, the system menu is activated when we use the
314 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
315 if ( exflags
& WS_EX_CONTEXTHELP
)
317 wxFrame
*winTop
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
320 wxIcon icon
= winTop
->GetIcon();
323 ::SendMessage(GetHwnd(), WM_SETICON
,
325 (LPARAM
)GetHiconOf(icon
));
331 // move the dialog to its initial position without forcing repainting
333 if ( !MSWGetCreateWindowCoords(pos
, size
, x
, y
, w
, h
) )
336 w
= (int)CW_USEDEFAULT
;
339 // we can't use CW_USEDEFAULT here as we're not calling CreateWindow()
340 // and passing CW_USEDEFAULT to MoveWindow() results in resizing the
341 // window to (0, 0) size which breaks quite a lot of things, e.g. the
342 // sizer calculation in wxSizer::Fit()
343 if ( w
== (int)CW_USEDEFAULT
)
345 // the exact number doesn't matter, the dialog will be resized
346 // again soon anyhow but it should be big enough to allow
347 // calculation relying on "totalSize - clientSize > 0" work, i.e.
348 // at least greater than the title bar height
353 if ( x
== (int)CW_USEDEFAULT
)
355 // centre it on the screen - what else can we do?
356 wxSize sizeDpy
= wxGetDisplaySize();
358 x
= (sizeDpy
.x
- w
) / 2;
359 y
= (sizeDpy
.y
- h
) / 2;
362 if ( !::MoveWindow(GetHwnd(), x
, y
, w
, h
, FALSE
) )
364 wxLogLastError(wxT("MoveWindow"));
367 if ( !title
.empty() )
369 ::SetWindowText(GetHwnd(), title
);
375 #endif // __WXMICROWIN__/!__WXMICROWIN__
378 bool wxTopLevelWindowMSW::CreateFrame(const wxString
& title
,
383 WXDWORD flags
= MSWGetCreateWindowFlags(&exflags
);
385 return MSWCreate(wxCanvasClassName
, title
, pos
, size
, flags
, exflags
);
388 bool wxTopLevelWindowMSW::Create(wxWindow
*parent
,
390 const wxString
& title
,
394 const wxString
& name
)
399 m_windowStyle
= style
;
403 m_windowId
= id
== -1 ? NewControlId() : id
;
405 wxTopLevelWindows
.Append(this);
408 parent
->AddChild(this);
410 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
412 // we have different dialog templates to allows creation of dialogs
413 // with & without captions under MSWindows, resizeable or not (but a
414 // resizeable dialog always has caption - otherwise it would look too
417 // we need 3 additional WORDs for dialog menu, class and title (as we
418 // don't use DS_SETFONT we don't need the fourth WORD for the font)
419 static const int dlgsize
= sizeof(DLGTEMPLATE
) + (sizeof(WORD
) * 3);
420 DLGTEMPLATE
*dlgTemplate
= (DLGTEMPLATE
*)malloc(dlgsize
);
421 memset(dlgTemplate
, 0, dlgsize
);
423 // these values are arbitrary, they won't be used normally anyhow
426 dlgTemplate
->cx
= 144;
427 dlgTemplate
->cy
= 75;
429 // reuse the code in MSWGetStyle() but correct the results slightly for
431 dlgTemplate
->style
= MSWGetStyle(style
, NULL
);
433 // all dialogs are popups
434 dlgTemplate
->style
|= WS_POPUP
;
436 // force 3D-look if necessary, it looks impossibly ugly otherwise
437 if ( style
& (wxRESIZE_BORDER
| wxCAPTION
) )
438 dlgTemplate
->style
|= DS_MODALFRAME
;
440 bool ret
= CreateDialog(dlgTemplate
, title
, pos
, size
);
447 return CreateFrame(title
, pos
, size
);
451 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
453 if ( this == ms_hiddenParent
)
455 // stop [infinite] recursion which would otherwise happen when we do
456 // "delete ms_hiddenParent" below
460 wxTopLevelWindows
.DeleteObject(this);
462 if ( wxModelessWindows
.Find(this) )
463 wxModelessWindows
.DeleteObject(this);
465 // after destroying an owned window, Windows activates the next top level
466 // window in Z order but it may be different from our owner (to reproduce
467 // this simply Alt-TAB to another application and back before closing the
468 // owned frame) whereas we always want to yield activation to our parent
469 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT
) )
471 wxWindow
*parent
= GetParent();
474 ::BringWindowToTop(GetHwndOf(parent
));
478 // If this is the last top-level window, exit.
479 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
481 if ( ms_hiddenParent
)
483 delete ms_hiddenParent
;
484 ms_hiddenParent
= NULL
;
487 wxTheApp
->SetTopWindow(NULL
);
489 if ( wxTheApp
->GetExitOnFrameDelete() )
491 ::PostQuitMessage(0);
496 // ----------------------------------------------------------------------------
497 // wxTopLevelWindowMSW showing
498 // ----------------------------------------------------------------------------
500 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd
)
502 ::ShowWindow(GetHwnd(), nShowCmd
);
504 m_iconized
= nShowCmd
== SW_MINIMIZE
;
507 bool wxTopLevelWindowMSW::Show(bool show
)
509 // don't use wxWindow version as we want to call DoShowWindow() ourselves
510 if ( !wxWindowBase::Show(show
) )
516 if ( m_maximizeOnShow
)
519 nShowCmd
= SW_MAXIMIZE
;
521 m_maximizeOnShow
= FALSE
;
533 DoShowWindow(nShowCmd
);
537 ::BringWindowToTop(GetHwnd());
539 wxActivateEvent
event(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
540 event
.SetEventObject( this );
541 GetEventHandler()->ProcessEvent(event
);
545 // Try to highlight the correct window (the parent)
548 HWND hWndParent
= GetHwndOf(GetParent());
550 ::BringWindowToTop(hWndParent
);
557 // ----------------------------------------------------------------------------
558 // wxTopLevelWindowMSW maximize/minimize
559 // ----------------------------------------------------------------------------
561 void wxTopLevelWindowMSW::Maximize(bool maximize
)
565 // just maximize it directly
566 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
570 // we can't maximize the hidden frame because it shows it as well, so
571 // just remember that we should do it later in this case
572 m_maximizeOnShow
= TRUE
;
576 bool wxTopLevelWindowMSW::IsMaximized() const
578 return ::IsZoomed(GetHwnd()) != 0;
581 void wxTopLevelWindowMSW::Iconize(bool iconize
)
583 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
586 bool wxTopLevelWindowMSW::IsIconized() const
588 // also update the current state
589 ((wxTopLevelWindowMSW
*)this)->m_iconized
= ::IsIconic(GetHwnd()) != 0;
594 void wxTopLevelWindowMSW::Restore()
596 DoShowWindow(SW_RESTORE
);
599 // ----------------------------------------------------------------------------
600 // wxTopLevelWindowMSW fullscreen
601 // ----------------------------------------------------------------------------
603 bool wxTopLevelWindowMSW::ShowFullScreen(bool show
, long style
)
610 m_fsIsShowing
= TRUE
;
613 // zap the frame borders
615 // save the 'normal' window style
616 m_fsOldWindowStyle
= GetWindowLong((HWND
)GetHWND(), GWL_STYLE
);
618 // save the old position, width & height, maximize state
619 m_fsOldSize
= GetRect();
620 m_fsIsMaximized
= IsMaximized();
622 // decide which window style flags to turn off
623 LONG newStyle
= m_fsOldWindowStyle
;
626 if (style
& wxFULLSCREEN_NOBORDER
)
627 offFlags
|= WS_BORDER
| WS_THICKFRAME
;
628 if (style
& wxFULLSCREEN_NOCAPTION
)
629 offFlags
|= (WS_CAPTION
| WS_SYSMENU
);
631 newStyle
&= (~offFlags
);
633 // change our window style to be compatible with full-screen mode
634 ::SetWindowLong((HWND
)GetHWND(), GWL_STYLE
, newStyle
);
636 // resize to the size of the desktop
639 RECT rect
= wxGetWindowRect(::GetDesktopWindow());
640 width
= rect
.right
- rect
.left
;
641 height
= rect
.bottom
- rect
.top
;
643 SetSize(width
, height
);
645 // now flush the window style cache and actually go full-screen
646 SetWindowPos((HWND
)GetHWND(), HWND_TOP
, 0, 0, width
, height
, SWP_FRAMECHANGED
);
648 wxSizeEvent
event(wxSize(width
, height
), GetId());
649 GetEventHandler()->ProcessEvent(event
);
658 m_fsIsShowing
= FALSE
;
660 Maximize(m_fsIsMaximized
);
661 SetWindowLong((HWND
)GetHWND(),GWL_STYLE
, m_fsOldWindowStyle
);
662 SetWindowPos((HWND
)GetHWND(),HWND_TOP
,m_fsOldSize
.x
, m_fsOldSize
.y
,
663 m_fsOldSize
.width
, m_fsOldSize
.height
, SWP_FRAMECHANGED
);
669 // ----------------------------------------------------------------------------
670 // wxTopLevelWindowMSW misc
671 // ----------------------------------------------------------------------------
673 void wxTopLevelWindowMSW::SetIcon(const wxIcon
& icon
)
675 SetIcons( wxIconBundle( icon
) );
678 void wxTopLevelWindowMSW::SetIcons(const wxIconBundle
& icons
)
680 wxTopLevelWindowBase::SetIcons(icons
);
682 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
683 const wxIcon
& sml
= icons
.GetIcon( wxSize( 16, 16 ) );
684 if( sml
.Ok() && sml
.GetWidth() == 16 && sml
.GetHeight() == 16 )
686 ::SendMessage( GetHwndOf( this ), WM_SETICON
, ICON_SMALL
,
687 (LPARAM
)GetHiconOf(sml
) );
690 const wxIcon
& big
= icons
.GetIcon( wxSize( 32, 32 ) );
691 if( big
.Ok() && big
.GetWidth() == 32 && big
.GetHeight() == 32 )
693 ::SendMessage( GetHwndOf( this ), WM_SETICON
, ICON_BIG
,
694 (LPARAM
)GetHiconOf(big
) );
699 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable
)
701 #ifndef __WXMICROWIN__
702 // get system (a.k.a. window) menu
703 HMENU hmenu
= ::GetSystemMenu(GetHwnd(), FALSE
/* get it */);
706 wxLogLastError(_T("GetSystemMenu"));
711 // enabling/disabling the close item from it also automatically
712 // disables/enables the close title bar button
713 if ( ::EnableMenuItem(hmenu
, SC_CLOSE
,
715 (enable
? MF_ENABLED
: MF_GRAYED
)) == -1 )
717 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
722 // update appearance immediately
723 if ( !::DrawMenuBar(GetHwnd()) )
725 wxLogLastError(_T("DrawMenuBar"));
727 #endif // !__WXMICROWIN__
732 // ----------------------------------------------------------------------------
733 // wxTopLevelWindow event handling
734 // ----------------------------------------------------------------------------
736 // Default activation behaviour - set the focus for the first child
738 void wxTopLevelWindowMSW::OnActivate(wxActivateEvent
& event
)
740 if ( event
.GetActive() )
742 // restore focus to the child which was last focused
743 wxLogTrace(_T("focus"), _T("wxTLW %08x activated."), m_hWnd
);
745 wxWindow
*parent
= m_winLastFocused
? m_winLastFocused
->GetParent()
752 wxSetFocusToChild(parent
, &m_winLastFocused
);
756 // remember the last focused child if it is our child
757 m_winLastFocused
= FindFocus();
759 // so we NULL it out if it's a child from some other frame
760 wxWindow
*win
= m_winLastFocused
;
763 if ( win
->IsTopLevel() )
767 m_winLastFocused
= NULL
;
773 win
= win
->GetParent();
776 wxLogTrace(_T("focus"),
777 _T("wxTLW %08x deactivated, last focused: %08x."),
779 m_winLastFocused
? GetHwndOf(m_winLastFocused
)