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 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 // list of all frames and modeless dialogs
68 wxWindowList wxModelessWindows
;
70 // the name of the default wxWindows class
71 extern const wxChar
*wxCanvasClassName
;
73 // the hidden parent for wxFRAME_NO_TASKBAR unowned frames
74 wxWindow
*wxTopLevelWindowMSW::ms_hiddenParent
= NULL
;
76 // ============================================================================
77 // wxTopLevelWindowMSW implementation
78 // ============================================================================
80 BEGIN_EVENT_TABLE(wxTopLevelWindowMSW
, wxTopLevelWindowBase
)
81 EVT_ACTIVATE(wxTopLevelWindowMSW::OnActivate
)
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
90 wxDlgProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
95 // for this message, returning TRUE tells system to set focus to
96 // the first control in the dialog box, but as we set the focus
97 // ourselves, we return FALSE from here as well, so fall through
100 // for all the other ones, FALSE means that we didn't process the
106 // ----------------------------------------------------------------------------
107 // wxTopLevelWindowMSW creation
108 // ----------------------------------------------------------------------------
110 void wxTopLevelWindowMSW::Init()
113 m_maximizeOnShow
= FALSE
;
115 // unlike (almost?) all other windows, frames are created hidden
118 // Data to save/restore when calling ShowFullScreen
120 m_fsOldWindowStyle
= 0;
121 m_fsIsMaximized
= FALSE
;
122 m_fsIsShowing
= FALSE
;
124 m_winLastFocused
= (wxWindow
*)NULL
;
127 WXDWORD
wxTopLevelWindowMSW::MSWGetStyle(long style
, WXDWORD
*exflags
) const
129 // let the base class deal with the common styles but fix the ones which
130 // don't make sense for us (we also deal with the borders ourselves)
131 WXDWORD msflags
= wxWindow::MSWGetStyle
133 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exflags
136 // first select the kind of window being created
138 // note that if we don't set WS_POPUP, Windows assumes WS_OVERLAPPED and
139 // creates a window with both caption and border, hence we also test it
140 // below in some other cases
141 if ( style
& wxFRAME_TOOL_WINDOW
)
144 msflags
|= WS_OVERLAPPED
;
146 // border and caption styles
147 if ( style
& wxRESIZE_BORDER
)
148 msflags
|= WS_THICKFRAME
;
149 else if ( !(style
& wxBORDER_NONE
) )
150 msflags
|= WS_BORDER
;
154 if ( style
& wxCAPTION
)
155 msflags
|= WS_CAPTION
;
159 // next translate the individual flags
160 if ( style
& wxMINIMIZE_BOX
)
161 msflags
|= WS_MINIMIZEBOX
;
162 if ( style
& wxMAXIMIZE_BOX
)
163 msflags
|= WS_MAXIMIZEBOX
;
164 if ( style
& wxSYSTEM_MENU
)
165 msflags
|= WS_SYSMENU
;
166 if ( style
& wxMINIMIZE
)
167 msflags
|= WS_MINIMIZE
;
168 if ( style
& wxMAXIMIZE
)
169 msflags
|= WS_MAXIMIZE
;
171 // Keep this here because it saves recoding this function in wxTinyFrame
172 #if wxUSE_ITSY_BITSY && !defined(__WIN32__)
173 if ( style
& wxTINY_CAPTION_VERT
)
174 msflags
|= IBS_VERTCAPTION
;
175 if ( style
& wxTINY_CAPTION_HORIZ
)
176 msflags
|= IBS_HORZCAPTION
;
178 if ( style
& (wxTINY_CAPTION_VERT
| wxTINY_CAPTION_HORIZ
) )
179 msflags
|= WS_CAPTION
;
184 #if !defined(__WIN16__) && !defined(__SC__)
185 if ( !(GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
) )
187 if ( style
& wxFRAME_TOOL_WINDOW
)
189 // create the palette-like window
190 *exflags
|= WS_EX_TOOLWINDOW
;
193 // We have to solve 2 different problems here:
195 // 1. frames with wxFRAME_NO_TASKBAR flag shouldn't appear in the
196 // taskbar even if they don't have a parent
198 // 2. frames without this style should appear in the taskbar even
199 // if they're owned (Windows only puts non owned windows into
200 // the taskbar normally)
202 // The second one is solved here by using WS_EX_APPWINDOW flag, the
203 // first one is dealt with in our MSWGetParent() method
205 if ( !(style
& wxFRAME_NO_TASKBAR
) && GetParent() )
207 // need to force the frame to appear in the taskbar
208 *exflags
|= WS_EX_APPWINDOW
;
210 //else: nothing to do [here]
214 if ( style
& wxSTAY_ON_TOP
)
215 *exflags
|= WS_EX_TOPMOST
;
218 if ( GetExtraStyle() & wxFRAME_EX_CONTEXTHELP
)
219 *exflags
|= WS_EX_CONTEXTHELP
;
226 WXHWND
wxTopLevelWindowMSW::MSWGetParent() const
228 // for the frames without wxFRAME_FLOAT_ON_PARENT style we should use NULL
229 // parent HWND or it would be always on top of its parent which is not what
230 // we usually want (in fact, we only want it for frames with the
231 // wxFRAME_FLOAT_ON_PARENT flag)
233 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT
) )
235 parent
= GetParent();
237 // this flag doesn't make sense then and will be ignored
238 wxASSERT_MSG( parent
,
239 _T("wxFRAME_FLOAT_ON_PARENT but no parent?") );
241 else // don't float on parent, must not be owned
246 // now deal with the 2nd taskbar-related problem (see comments above in
248 if ( HasFlag(wxFRAME_NO_TASKBAR
) && !parent
)
250 if ( !ms_hiddenParent
)
252 ms_hiddenParent
= new wxTopLevelWindowMSW(NULL
, -1, _T(""));
254 // we shouldn't leave it in wxTopLevelWindows or we wouldn't
255 // terminate the app when the last user-created frame is deleted --
256 // see ~wxTopLevelWindowMSW
257 wxTopLevelWindows
.DeleteObject(ms_hiddenParent
);
260 parent
= ms_hiddenParent
;
263 return parent
? parent
->GetHWND() : NULL
;
266 bool wxTopLevelWindowMSW::CreateDialog(const void *dlgTemplate
,
267 const wxString
& title
,
271 #ifdef __WXMICROWIN__
272 // no dialogs support under MicroWin yet
273 return CreateFrame(title
, pos
, size
);
274 #else // !__WXMICROWIN__
275 wxWindow
*parent
= GetParent();
277 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
278 // app window as parent - this avoids creating modal dialogs without
280 if ( !parent
&& !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
) )
282 parent
= wxTheApp
->GetTopWindow();
286 // don't use transient windows as parents, this is dangerous as it
287 // can lead to a crash if the parent is destroyed before the child
289 // also don't use the window which is currently hidden as then the
290 // dialog would be hidden as well
291 if ( (parent
->GetExtraStyle() & wxWS_EX_TRANSIENT
) ||
299 m_hWnd
= (WXHWND
)::CreateDialogIndirect
302 (DLGTEMPLATE
*)dlgTemplate
,
303 parent
? GetHwndOf(parent
) : NULL
,
309 wxFAIL_MSG(_("Failed to create dialog. Incorrect DLGTEMPLATE?"));
311 wxLogSysError(_("Can't create dialog using memory template"));
317 (void)MSWGetCreateWindowFlags(&exflags
);
321 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exflags
);
322 ::SetWindowPos(GetHwnd(), NULL
, 0, 0, 0, 0,
329 #if defined(__WIN95__)
330 // For some reason, the system menu is activated when we use the
331 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
332 if ( exflags
& WS_EX_CONTEXTHELP
)
334 wxFrame
*winTop
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
337 wxIcon icon
= winTop
->GetIcon();
340 ::SendMessage(GetHwnd(), WM_SETICON
,
342 (LPARAM
)GetHiconOf(icon
));
348 // move the dialog to its initial position without forcing repainting
350 if ( !MSWGetCreateWindowCoords(pos
, size
, x
, y
, w
, h
) )
353 w
= (int)CW_USEDEFAULT
;
356 // we can't use CW_USEDEFAULT here as we're not calling CreateWindow()
357 // and passing CW_USEDEFAULT to MoveWindow() results in resizing the
358 // window to (0, 0) size which breaks quite a lot of things, e.g. the
359 // sizer calculation in wxSizer::Fit()
360 if ( w
== (int)CW_USEDEFAULT
)
362 // the exact number doesn't matter, the dialog will be resized
363 // again soon anyhow but it should be big enough to allow
364 // calculation relying on "totalSize - clientSize > 0" work, i.e.
365 // at least greater than the title bar height
370 if ( x
== (int)CW_USEDEFAULT
)
372 // centre it on the screen - what else can we do?
373 wxSize sizeDpy
= wxGetDisplaySize();
375 x
= (sizeDpy
.x
- w
) / 2;
376 y
= (sizeDpy
.y
- h
) / 2;
379 if ( !::MoveWindow(GetHwnd(), x
, y
, w
, h
, FALSE
) )
381 wxLogLastError(wxT("MoveWindow"));
384 if ( !title
.empty() )
386 ::SetWindowText(GetHwnd(), title
);
392 #endif // __WXMICROWIN__/!__WXMICROWIN__
395 bool wxTopLevelWindowMSW::CreateFrame(const wxString
& title
,
400 WXDWORD flags
= MSWGetCreateWindowFlags(&exflags
);
402 return MSWCreate(wxCanvasClassName
, title
, pos
, size
, flags
, exflags
);
405 bool wxTopLevelWindowMSW::Create(wxWindow
*parent
,
407 const wxString
& title
,
411 const wxString
& name
)
416 m_windowStyle
= style
;
420 m_windowId
= id
== -1 ? NewControlId() : id
;
422 wxTopLevelWindows
.Append(this);
425 parent
->AddChild(this);
427 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
429 // we have different dialog templates to allows creation of dialogs
430 // with & without captions under MSWindows, resizeable or not (but a
431 // resizeable dialog always has caption - otherwise it would look too
434 // we need 3 additional WORDs for dialog menu, class and title (as we
435 // don't use DS_SETFONT we don't need the fourth WORD for the font)
436 static const int dlgsize
= sizeof(DLGTEMPLATE
) + (sizeof(WORD
) * 3);
437 DLGTEMPLATE
*dlgTemplate
= (DLGTEMPLATE
*)malloc(dlgsize
);
438 memset(dlgTemplate
, 0, dlgsize
);
440 // these values are arbitrary, they won't be used normally anyhow
443 dlgTemplate
->cx
= 144;
444 dlgTemplate
->cy
= 75;
446 // reuse the code in MSWGetStyle() but correct the results slightly for
448 dlgTemplate
->style
= MSWGetStyle(style
, NULL
);
450 // all dialogs are popups
451 dlgTemplate
->style
|= WS_POPUP
;
453 // force 3D-look if necessary, it looks impossibly ugly otherwise
454 if ( style
& (wxRESIZE_BORDER
| wxCAPTION
) )
455 dlgTemplate
->style
|= DS_MODALFRAME
;
457 bool ret
= CreateDialog(dlgTemplate
, title
, pos
, size
);
464 return CreateFrame(title
, pos
, size
);
468 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
470 if ( this == ms_hiddenParent
)
472 // stop [infinite] recursion which would otherwise happen when we do
473 // "delete ms_hiddenParent" below
477 wxTopLevelWindows
.DeleteObject(this);
479 if ( wxModelessWindows
.Find(this) )
480 wxModelessWindows
.DeleteObject(this);
482 // after destroying an owned window, Windows activates the next top level
483 // window in Z order but it may be different from our owner (to reproduce
484 // this simply Alt-TAB to another application and back before closing the
485 // owned frame) whereas we always want to yield activation to our parent
486 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT
) )
488 wxWindow
*parent
= GetParent();
491 ::BringWindowToTop(GetHwndOf(parent
));
495 // If this is the last top-level window, exit.
496 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
498 if ( ms_hiddenParent
)
500 delete ms_hiddenParent
;
501 ms_hiddenParent
= NULL
;
504 wxTheApp
->SetTopWindow(NULL
);
506 if ( wxTheApp
->GetExitOnFrameDelete() )
508 ::PostQuitMessage(0);
513 // ----------------------------------------------------------------------------
514 // wxTopLevelWindowMSW showing
515 // ----------------------------------------------------------------------------
517 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd
)
519 ::ShowWindow(GetHwnd(), nShowCmd
);
521 m_iconized
= nShowCmd
== SW_MINIMIZE
;
524 bool wxTopLevelWindowMSW::Show(bool show
)
526 // don't use wxWindow version as we want to call DoShowWindow() ourselves
527 if ( !wxWindowBase::Show(show
) )
533 if ( m_maximizeOnShow
)
536 nShowCmd
= SW_MAXIMIZE
;
538 m_maximizeOnShow
= FALSE
;
550 DoShowWindow(nShowCmd
);
554 ::BringWindowToTop(GetHwnd());
556 wxActivateEvent
event(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
557 event
.SetEventObject( this );
558 GetEventHandler()->ProcessEvent(event
);
562 // Try to highlight the correct window (the parent)
565 HWND hWndParent
= GetHwndOf(GetParent());
567 ::BringWindowToTop(hWndParent
);
574 // ----------------------------------------------------------------------------
575 // wxTopLevelWindowMSW maximize/minimize
576 // ----------------------------------------------------------------------------
578 void wxTopLevelWindowMSW::Maximize(bool maximize
)
582 // just maximize it directly
583 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
587 // we can't maximize the hidden frame because it shows it as well, so
588 // just remember that we should do it later in this case
589 m_maximizeOnShow
= TRUE
;
593 bool wxTopLevelWindowMSW::IsMaximized() const
595 return ::IsZoomed(GetHwnd()) != 0;
598 void wxTopLevelWindowMSW::Iconize(bool iconize
)
600 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
603 bool wxTopLevelWindowMSW::IsIconized() const
605 // also update the current state
606 ((wxTopLevelWindowMSW
*)this)->m_iconized
= ::IsIconic(GetHwnd()) != 0;
611 void wxTopLevelWindowMSW::Restore()
613 DoShowWindow(SW_RESTORE
);
616 // ----------------------------------------------------------------------------
617 // wxTopLevelWindowMSW fullscreen
618 // ----------------------------------------------------------------------------
620 bool wxTopLevelWindowMSW::ShowFullScreen(bool show
, long style
)
627 m_fsIsShowing
= TRUE
;
630 // zap the frame borders
632 // save the 'normal' window style
633 m_fsOldWindowStyle
= GetWindowLong((HWND
)GetHWND(), GWL_STYLE
);
635 // save the old position, width & height, maximize state
636 m_fsOldSize
= GetRect();
637 m_fsIsMaximized
= IsMaximized();
639 // decide which window style flags to turn off
640 LONG newStyle
= m_fsOldWindowStyle
;
643 if (style
& wxFULLSCREEN_NOBORDER
)
644 offFlags
|= WS_BORDER
| WS_THICKFRAME
;
645 if (style
& wxFULLSCREEN_NOCAPTION
)
646 offFlags
|= (WS_CAPTION
| WS_SYSMENU
);
648 newStyle
&= (~offFlags
);
650 // change our window style to be compatible with full-screen mode
651 ::SetWindowLong((HWND
)GetHWND(), GWL_STYLE
, newStyle
);
653 // resize to the size of the desktop
656 RECT rect
= wxGetWindowRect(::GetDesktopWindow());
657 width
= rect
.right
- rect
.left
;
658 height
= rect
.bottom
- rect
.top
;
660 SetSize(width
, height
);
662 // now flush the window style cache and actually go full-screen
663 SetWindowPos((HWND
)GetHWND(), HWND_TOP
, 0, 0, width
, height
, SWP_FRAMECHANGED
);
665 wxSizeEvent
event(wxSize(width
, height
), GetId());
666 GetEventHandler()->ProcessEvent(event
);
675 m_fsIsShowing
= FALSE
;
677 Maximize(m_fsIsMaximized
);
678 SetWindowLong((HWND
)GetHWND(),GWL_STYLE
, m_fsOldWindowStyle
);
679 SetWindowPos((HWND
)GetHWND(),HWND_TOP
,m_fsOldSize
.x
, m_fsOldSize
.y
,
680 m_fsOldSize
.width
, m_fsOldSize
.height
, SWP_FRAMECHANGED
);
686 // ----------------------------------------------------------------------------
687 // wxTopLevelWindowMSW misc
688 // ----------------------------------------------------------------------------
690 void wxTopLevelWindowMSW::SetIcon(const wxIcon
& icon
)
692 SetIcons( wxIconBundle( icon
) );
695 void wxTopLevelWindowMSW::SetIcons(const wxIconBundle
& icons
)
697 wxTopLevelWindowBase::SetIcons(icons
);
699 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
700 const wxIcon
& sml
= icons
.GetIcon( wxSize( 16, 16 ) );
701 if( sml
.Ok() && sml
.GetWidth() == 16 && sml
.GetHeight() == 16 )
703 ::SendMessage( GetHwndOf( this ), WM_SETICON
, ICON_SMALL
,
704 (LPARAM
)GetHiconOf(sml
) );
707 const wxIcon
& big
= icons
.GetIcon( wxSize( 32, 32 ) );
708 if( big
.Ok() && big
.GetWidth() == 32 && big
.GetHeight() == 32 )
710 ::SendMessage( GetHwndOf( this ), WM_SETICON
, ICON_BIG
,
711 (LPARAM
)GetHiconOf(big
) );
716 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable
)
718 #ifndef __WXMICROWIN__
719 // get system (a.k.a. window) menu
720 HMENU hmenu
= ::GetSystemMenu(GetHwnd(), FALSE
/* get it */);
723 wxLogLastError(_T("GetSystemMenu"));
728 // enabling/disabling the close item from it also automatically
729 // disables/enables the close title bar button
730 if ( ::EnableMenuItem(hmenu
, SC_CLOSE
,
732 (enable
? MF_ENABLED
: MF_GRAYED
)) == -1 )
734 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
739 // update appearance immediately
740 if ( !::DrawMenuBar(GetHwnd()) )
742 wxLogLastError(_T("DrawMenuBar"));
744 #endif // !__WXMICROWIN__
749 // ----------------------------------------------------------------------------
750 // wxTopLevelWindow event handling
751 // ----------------------------------------------------------------------------
753 // Default activation behaviour - set the focus for the first child
755 void wxTopLevelWindowMSW::OnActivate(wxActivateEvent
& event
)
757 if ( event
.GetActive() )
759 // restore focus to the child which was last focused
760 wxLogTrace(_T("focus"), _T("wxTLW %08x activated."), m_hWnd
);
762 wxWindow
*parent
= m_winLastFocused
? m_winLastFocused
->GetParent()
769 wxSetFocusToChild(parent
, &m_winLastFocused
);
773 // remember the last focused child if it is our child
774 m_winLastFocused
= FindFocus();
776 // so we NULL it out if it's a child from some other frame
777 wxWindow
*win
= m_winLastFocused
;
780 if ( win
->IsTopLevel() )
784 m_winLastFocused
= NULL
;
790 win
= win
->GetParent();
793 wxLogTrace(_T("focus"),
794 _T("wxTLW %08x deactivated, last focused: %08x."),
796 m_winLastFocused
? GetHwndOf(m_winLastFocused
)