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/msw/private.h"
42 #include "wx/popupwin.h"
52 // ----------------------------------------------------------------------------
53 // stubs for missing functions under MicroWindows
54 // ----------------------------------------------------------------------------
58 // static inline bool IsIconic(HWND WXUNUSED(hwnd)) { return FALSE; }
59 static inline bool IsZoomed(HWND
WXUNUSED(hwnd
)) { return FALSE
; }
61 #endif // __WXMICROWIN__
63 // this is defined in dialog.cpp
65 wxDlgProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
);
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // list of all frames and modeless dialogs
72 wxWindowList wxModelessWindows
;
74 // the name of the default wxWindows class
75 extern const wxChar
*wxCanvasClassName
;
77 // the hidden parent for wxFRAME_NO_TASKBAR unowned frames
78 wxWindow
*wxTopLevelWindowMSW::ms_hiddenParent
= NULL
;
80 // ============================================================================
81 // wxTopLevelWindowMSW implementation
82 // ============================================================================
84 BEGIN_EVENT_TABLE(wxTopLevelWindowMSW
, wxTopLevelWindowBase
)
85 EVT_ACTIVATE(wxTopLevelWindowMSW::OnActivate
)
88 // ----------------------------------------------------------------------------
89 // wxTopLevelWindowMSW creation
90 // ----------------------------------------------------------------------------
92 void wxTopLevelWindowMSW::Init()
95 m_maximizeOnShow
= FALSE
;
97 // unlike (almost?) all other windows, frames are created hidden
100 // Data to save/restore when calling ShowFullScreen
102 m_fsOldWindowStyle
= 0;
103 m_fsIsMaximized
= FALSE
;
104 m_fsIsShowing
= FALSE
;
106 m_winLastFocused
= (wxWindow
*)NULL
;
109 WXDWORD
wxTopLevelWindowMSW::MSWGetStyle(long style
, WXDWORD
*exflags
) const
111 // let the base class deal with the common styles but fix the ones which
112 // don't make sense for us (we also deal with the borders ourselves)
113 WXDWORD msflags
= wxWindow::MSWGetStyle
115 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exflags
118 // first select the kind of window being created
120 // note that if we don't set WS_POPUP, Windows assumes WS_OVERLAPPED and
121 // creates a window with both caption and border, hence we also test it
122 // below in some other cases
123 if ( style
& wxFRAME_TOOL_WINDOW
)
126 msflags
|= WS_OVERLAPPED
;
128 // border and caption styles
129 if ( style
& wxRESIZE_BORDER
)
130 msflags
|= WS_THICKFRAME
;
131 else if ( !(style
& wxBORDER_NONE
) )
132 msflags
|= WS_BORDER
;
136 if ( style
& wxCAPTION
)
137 msflags
|= WS_CAPTION
;
141 // next translate the individual flags
142 if ( style
& wxMINIMIZE_BOX
)
143 msflags
|= WS_MINIMIZEBOX
;
144 if ( style
& wxMAXIMIZE_BOX
)
145 msflags
|= WS_MAXIMIZEBOX
;
146 if ( style
& wxSYSTEM_MENU
)
147 msflags
|= WS_SYSMENU
;
148 if ( style
& wxMINIMIZE
)
149 msflags
|= WS_MINIMIZE
;
150 if ( style
& wxMAXIMIZE
)
151 msflags
|= WS_MAXIMIZE
;
153 // Keep this here because it saves recoding this function in wxTinyFrame
154 #if wxUSE_ITSY_BITSY && !defined(__WIN32__)
155 if ( style
& wxTINY_CAPTION_VERT
)
156 msflags
|= IBS_VERTCAPTION
;
157 if ( style
& wxTINY_CAPTION_HORIZ
)
158 msflags
|= IBS_HORZCAPTION
;
160 if ( style
& (wxTINY_CAPTION_VERT
| wxTINY_CAPTION_HORIZ
) )
161 msflags
|= WS_CAPTION
;
166 #if !defined(__WIN16__) && !defined(__SC__)
167 if ( !(GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
) )
169 if ( style
& wxFRAME_TOOL_WINDOW
)
171 // create the palette-like window
172 *exflags
|= WS_EX_TOOLWINDOW
;
175 // We have to solve 2 different problems here:
177 // 1. frames with wxFRAME_NO_TASKBAR flag shouldn't appear in the
178 // taskbar even if they don't have a parent
180 // 2. frames without this style should appear in the taskbar even
181 // if they're owned (Windows only puts non owned windows into
182 // the taskbar normally)
184 // The second one is solved here by using WS_EX_APPWINDOW flag, the
185 // first one is dealt with in our MSWGetParent() method
187 if ( !(style
& wxFRAME_NO_TASKBAR
) && GetParent() )
189 // need to force the frame to appear in the taskbar
190 *exflags
|= WS_EX_APPWINDOW
;
192 //else: nothing to do [here]
196 if ( style
& wxSTAY_ON_TOP
)
197 *exflags
|= WS_EX_TOPMOST
;
200 if ( GetExtraStyle() & wxFRAME_EX_CONTEXTHELP
)
201 *exflags
|= WS_EX_CONTEXTHELP
;
208 WXHWND
wxTopLevelWindowMSW::MSWGetParent() const
210 // for the frames without wxFRAME_FLOAT_ON_PARENT style we should use NULL
211 // parent HWND or it would be always on top of its parent which is not what
212 // we usually want (in fact, we only want it for frames with the
213 // wxFRAME_FLOAT_ON_PARENT flag)
215 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT
) )
217 parent
= GetParent();
219 // this flag doesn't make sense then and will be ignored
220 wxASSERT_MSG( parent
,
221 _T("wxFRAME_FLOAT_ON_PARENT but no parent?") );
223 else // don't float on parent, must not be owned
228 // now deal with the 2nd taskbar-related problem (see comments above in
230 if ( HasFlag(wxFRAME_NO_TASKBAR
) && !parent
)
232 if ( !ms_hiddenParent
)
234 ms_hiddenParent
= new wxTopLevelWindowMSW(NULL
, -1, _T(""));
236 // we shouldn't leave it in wxTopLevelWindows or we wouldn't
237 // terminate the app when the last user-created frame is deleted --
238 // see ~wxTopLevelWindowMSW
239 wxTopLevelWindows
.DeleteObject(ms_hiddenParent
);
242 parent
= ms_hiddenParent
;
245 return parent
? parent
->GetHWND() : NULL
;
248 bool wxTopLevelWindowMSW::CreateDialog(const void *dlgTemplate
,
249 const wxString
& title
,
253 #ifdef __WXMICROWIN__
254 // no dialogs support under MicroWin yet
255 return CreateFrame(title
, pos
, size
);
256 #else // !__WXMICROWIN__
257 wxWindow
*parent
= GetParent();
259 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
260 // app window as parent - this avoids creating modal dialogs without
262 if ( !parent
&& !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
) )
264 parent
= wxTheApp
->GetTopWindow();
268 // don't use transient windows as parents, this is dangerous as it
269 // can lead to a crash if the parent is destroyed before the child
271 // also don't use the window which is currently hidden as then the
272 // dialog would be hidden as well
273 if ( (parent
->GetExtraStyle() & wxWS_EX_TRANSIENT
) ||
281 m_hWnd
= (WXHWND
)::CreateDialogIndirect
284 (DLGTEMPLATE
*)dlgTemplate
,
285 parent
? GetHwndOf(parent
) : NULL
,
291 wxFAIL_MSG(_("Failed to create dialog. Incorrect DLGTEMPLATE?"));
293 wxLogSysError(_("Can't create dialog using memory template"));
299 (void)MSWGetCreateWindowFlags(&exflags
);
303 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exflags
);
304 ::SetWindowPos(GetHwnd(), NULL
, 0, 0, 0, 0,
311 #if defined(__WIN95__)
312 // For some reason, the system menu is activated when we use the
313 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
314 if ( exflags
& WS_EX_CONTEXTHELP
)
316 wxFrame
*winTop
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
319 wxIcon icon
= winTop
->GetIcon();
322 ::SendMessage(GetHwnd(), WM_SETICON
,
324 (LPARAM
)GetHiconOf(icon
));
330 // move the dialog to its initial position without forcing repainting
332 if ( !MSWGetCreateWindowCoords(pos
, size
, x
, y
, w
, h
) )
335 w
= (int)CW_USEDEFAULT
;
338 // we can't use CW_USEDEFAULT here as we're not calling CreateWindow()
339 // and passing CW_USEDEFAULT to MoveWindow() results in resizing the
340 // window to (0, 0) size which breaks quite a lot of things, e.g. the
341 // sizer calculation in wxSizer::Fit()
342 if ( w
== (int)CW_USEDEFAULT
)
344 // the exact number doesn't matter, the dialog will be resized
345 // again soon anyhow but it should be big enough to allow
346 // calculation relying on "totalSize - clientSize > 0" work, i.e.
347 // at least greater than the title bar height
352 if ( x
== (int)CW_USEDEFAULT
)
354 // centre it on the screen - what else can we do?
355 wxSize sizeDpy
= wxGetDisplaySize();
357 x
= (sizeDpy
.x
- w
) / 2;
358 y
= (sizeDpy
.y
- h
) / 2;
361 if ( !::MoveWindow(GetHwnd(), x
, y
, w
, h
, FALSE
) )
363 wxLogLastError(wxT("MoveWindow"));
366 if ( !title
.empty() )
368 ::SetWindowText(GetHwnd(), title
);
374 #endif // __WXMICROWIN__/!__WXMICROWIN__
377 bool wxTopLevelWindowMSW::CreateFrame(const wxString
& title
,
382 WXDWORD flags
= MSWGetCreateWindowFlags(&exflags
);
384 return MSWCreate(wxCanvasClassName
, title
, pos
, size
, flags
, exflags
);
387 bool wxTopLevelWindowMSW::Create(wxWindow
*parent
,
389 const wxString
& title
,
393 const wxString
& name
)
398 m_windowStyle
= style
;
402 m_windowId
= id
== -1 ? NewControlId() : id
;
404 wxTopLevelWindows
.Append(this);
407 parent
->AddChild(this);
409 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
411 // we have different dialog templates to allows creation of dialogs
412 // with & without captions under MSWindows, resizeable or not (but a
413 // resizeable dialog always has caption - otherwise it would look too
416 // we need 3 additional WORDs for dialog menu, class and title (as we
417 // don't use DS_SETFONT we don't need the fourth WORD for the font)
418 static const int dlgsize
= sizeof(DLGTEMPLATE
) + (sizeof(WORD
) * 3);
419 DLGTEMPLATE
*dlgTemplate
= (DLGTEMPLATE
*)malloc(dlgsize
);
420 memset(dlgTemplate
, 0, dlgsize
);
422 // these values are arbitrary, they won't be used normally anyhow
425 dlgTemplate
->cx
= 144;
426 dlgTemplate
->cy
= 75;
428 // reuse the code in MSWGetStyle() but correct the results slightly for
430 dlgTemplate
->style
= MSWGetStyle(style
, NULL
);
432 // all dialogs are popups
433 dlgTemplate
->style
|= WS_POPUP
;
435 // force 3D-look if necessary, it looks impossibly ugly otherwise
436 if ( style
& (wxRESIZE_BORDER
| wxCAPTION
) )
437 dlgTemplate
->style
|= DS_MODALFRAME
;
439 bool ret
= CreateDialog(dlgTemplate
, title
, pos
, size
);
446 return CreateFrame(title
, pos
, size
);
450 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
452 if ( this == ms_hiddenParent
)
454 // stop [infinite] recursion which would otherwise happen when we do
455 // "delete ms_hiddenParent" below
459 wxTopLevelWindows
.DeleteObject(this);
461 if ( wxModelessWindows
.Find(this) )
462 wxModelessWindows
.DeleteObject(this);
464 // after destroying an owned window, Windows activates the next top level
465 // window in Z order but it may be different from our owner (to reproduce
466 // this simply Alt-TAB to another application and back before closing the
467 // owned frame) whereas we always want to yield activation to our parent
468 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT
) )
470 wxWindow
*parent
= GetParent();
473 ::BringWindowToTop(GetHwndOf(parent
));
477 // If this is the last top-level window, exit.
478 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
480 if ( ms_hiddenParent
)
482 delete ms_hiddenParent
;
483 ms_hiddenParent
= NULL
;
486 wxTheApp
->SetTopWindow(NULL
);
488 if ( wxTheApp
->GetExitOnFrameDelete() )
490 ::PostQuitMessage(0);
495 // ----------------------------------------------------------------------------
496 // wxTopLevelWindowMSW showing
497 // ----------------------------------------------------------------------------
499 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd
)
501 ::ShowWindow(GetHwnd(), nShowCmd
);
503 m_iconized
= nShowCmd
== SW_MINIMIZE
;
506 bool wxTopLevelWindowMSW::Show(bool show
)
508 // don't use wxWindow version as we want to call DoShowWindow() ourselves
509 if ( !wxWindowBase::Show(show
) )
515 if ( m_maximizeOnShow
)
518 nShowCmd
= SW_MAXIMIZE
;
520 m_maximizeOnShow
= FALSE
;
532 DoShowWindow(nShowCmd
);
536 ::BringWindowToTop(GetHwnd());
538 wxActivateEvent
event(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
539 event
.SetEventObject( this );
540 GetEventHandler()->ProcessEvent(event
);
544 // Try to highlight the correct window (the parent)
547 HWND hWndParent
= GetHwndOf(GetParent());
549 ::BringWindowToTop(hWndParent
);
556 // ----------------------------------------------------------------------------
557 // wxTopLevelWindowMSW maximize/minimize
558 // ----------------------------------------------------------------------------
560 void wxTopLevelWindowMSW::Maximize(bool maximize
)
564 // just maximize it directly
565 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
569 // we can't maximize the hidden frame because it shows it as well, so
570 // just remember that we should do it later in this case
571 m_maximizeOnShow
= TRUE
;
575 bool wxTopLevelWindowMSW::IsMaximized() const
577 return ::IsZoomed(GetHwnd()) != 0;
580 void wxTopLevelWindowMSW::Iconize(bool iconize
)
582 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
585 bool wxTopLevelWindowMSW::IsIconized() const
587 // also update the current state
588 ((wxTopLevelWindowMSW
*)this)->m_iconized
= ::IsIconic(GetHwnd()) != 0;
593 void wxTopLevelWindowMSW::Restore()
595 DoShowWindow(SW_RESTORE
);
598 // ----------------------------------------------------------------------------
599 // wxTopLevelWindowMSW fullscreen
600 // ----------------------------------------------------------------------------
602 bool wxTopLevelWindowMSW::ShowFullScreen(bool show
, long style
)
609 m_fsIsShowing
= TRUE
;
612 // zap the frame borders
614 // save the 'normal' window style
615 m_fsOldWindowStyle
= GetWindowLong((HWND
)GetHWND(), GWL_STYLE
);
617 // save the old position, width & height, maximize state
618 m_fsOldSize
= GetRect();
619 m_fsIsMaximized
= IsMaximized();
621 // decide which window style flags to turn off
622 LONG newStyle
= m_fsOldWindowStyle
;
625 if (style
& wxFULLSCREEN_NOBORDER
)
626 offFlags
|= WS_BORDER
| WS_THICKFRAME
;
627 if (style
& wxFULLSCREEN_NOCAPTION
)
628 offFlags
|= (WS_CAPTION
| WS_SYSMENU
);
630 newStyle
&= (~offFlags
);
632 // change our window style to be compatible with full-screen mode
633 ::SetWindowLong((HWND
)GetHWND(), GWL_STYLE
, newStyle
);
635 // resize to the size of the desktop
638 RECT rect
= wxGetWindowRect(::GetDesktopWindow());
639 width
= rect
.right
- rect
.left
;
640 height
= rect
.bottom
- rect
.top
;
642 SetSize(width
, height
);
644 // now flush the window style cache and actually go full-screen
645 SetWindowPos((HWND
)GetHWND(), HWND_TOP
, 0, 0, width
, height
, SWP_FRAMECHANGED
);
647 wxSizeEvent
event(wxSize(width
, height
), GetId());
648 GetEventHandler()->ProcessEvent(event
);
657 m_fsIsShowing
= FALSE
;
659 Maximize(m_fsIsMaximized
);
660 SetWindowLong((HWND
)GetHWND(),GWL_STYLE
, m_fsOldWindowStyle
);
661 SetWindowPos((HWND
)GetHWND(),HWND_TOP
,m_fsOldSize
.x
, m_fsOldSize
.y
,
662 m_fsOldSize
.width
, m_fsOldSize
.height
, SWP_FRAMECHANGED
);
668 // ----------------------------------------------------------------------------
669 // wxTopLevelWindowMSW misc
670 // ----------------------------------------------------------------------------
672 void wxTopLevelWindowMSW::SetIcon(const wxIcon
& icon
)
674 SetIcons( wxIconBundle( icon
) );
677 void wxTopLevelWindowMSW::SetIcons(const wxIconBundle
& icons
)
679 wxTopLevelWindowBase::SetIcons(icons
);
681 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
682 const wxIcon
& sml
= icons
.GetIcon( wxSize( 16, 16 ) );
683 if( sml
.Ok() && sml
.GetWidth() == 16 && sml
.GetHeight() == 16 )
685 ::SendMessage( GetHwndOf( this ), WM_SETICON
, ICON_SMALL
,
686 (LPARAM
)GetHiconOf(sml
) );
689 const wxIcon
& big
= icons
.GetIcon( wxSize( 32, 32 ) );
690 if( big
.Ok() && big
.GetWidth() == 32 && big
.GetHeight() == 32 )
692 ::SendMessage( GetHwndOf( this ), WM_SETICON
, ICON_BIG
,
693 (LPARAM
)GetHiconOf(big
) );
698 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable
)
700 #ifndef __WXMICROWIN__
701 // get system (a.k.a. window) menu
702 HMENU hmenu
= ::GetSystemMenu(GetHwnd(), FALSE
/* get it */);
705 wxLogLastError(_T("GetSystemMenu"));
710 // enabling/disabling the close item from it also automatically
711 // disables/enables the close title bar button
712 if ( ::EnableMenuItem(hmenu
, SC_CLOSE
,
714 (enable
? MF_ENABLED
: MF_GRAYED
)) == -1 )
716 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
721 // update appearance immediately
722 if ( !::DrawMenuBar(GetHwnd()) )
724 wxLogLastError(_T("DrawMenuBar"));
726 #endif // !__WXMICROWIN__
731 // ----------------------------------------------------------------------------
732 // wxTopLevelWindow event handling
733 // ----------------------------------------------------------------------------
735 // Default activation behaviour - set the focus for the first child
737 void wxTopLevelWindowMSW::OnActivate(wxActivateEvent
& event
)
739 if ( event
.GetActive() )
741 // restore focus to the child which was last focused
742 wxLogTrace(_T("focus"), _T("wxTLW %08x activated."), m_hWnd
);
744 wxWindow
*parent
= m_winLastFocused
? m_winLastFocused
->GetParent()
751 wxSetFocusToChild(parent
, &m_winLastFocused
);
755 // remember the last focused child if it is our child
756 m_winLastFocused
= FindFocus();
758 // so we NULL it out if it's a child from some other frame
759 wxWindow
*win
= m_winLastFocused
;
762 if ( win
->IsTopLevel() )
766 m_winLastFocused
= NULL
;
772 win
= win
->GetParent();
775 wxLogTrace(_T("focus"),
776 _T("wxTLW %08x deactivated, last focused: %08x."),
778 m_winLastFocused
? GetHwndOf(m_winLastFocused
)