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 // ----------------------------------------------------------------------------
43 // stubs for missing functions under MicroWindows
44 // ----------------------------------------------------------------------------
48 // static inline bool IsIconic(HWND WXUNUSED(hwnd)) { return FALSE; }
49 static inline bool IsZoomed(HWND
WXUNUSED(hwnd
)) { return FALSE
; }
51 #endif // __WXMICROWIN__
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // list of all frames and modeless dialogs
58 wxWindowList wxModelessWindows
;
60 // the name of the default wxWindows class
61 extern const wxChar
*wxCanvasClassName
;
63 // ============================================================================
64 // wxTopLevelWindowMSW implementation
65 // ============================================================================
69 wxDlgProc(HWND
WXUNUSED(hWnd
), UINT message
, WPARAM
WXUNUSED(wParam
), LPARAM
WXUNUSED(lParam
))
71 if ( message
== WM_INITDIALOG
)
73 // for this message, returning TRUE tells system to set focus to the
74 // first control in the dialog box
79 // for all the other ones, FALSE means that we didn't process the
85 // ----------------------------------------------------------------------------
86 // wxTopLevelWindowMSW creation
87 // ----------------------------------------------------------------------------
89 void wxTopLevelWindowMSW::Init()
92 m_maximizeOnShow
= FALSE
;
94 // unlike (almost?) all other windows, frames are created hidden
97 // Data to save/restore when calling ShowFullScreen
99 m_fsOldWindowStyle
= 0;
100 m_fsIsMaximized
= FALSE
;
101 m_fsIsShowing
= FALSE
;
104 long wxTopLevelWindowMSW::MSWGetCreateWindowFlags(long *exflags
) const
106 long style
= GetWindowStyle();
108 // first select the kind of window being created
110 // note that if we don't set WS_POPUP, Windows assumes WS_OVERLAPPED and
111 // creates a window with both caption and border, hence we also test it
112 // below in some other cases
114 if ( style
& wxFRAME_TOOL_WINDOW
)
117 msflags
= WS_OVERLAPPED
;
119 // border and caption styles
120 if ( style
& wxRESIZE_BORDER
)
121 msflags
|= WS_THICKFRAME
;
122 else if ( !(style
& wxBORDER_NONE
) )
123 msflags
|= WS_BORDER
;
127 if ( style
& wxCAPTION
)
128 msflags
|= WS_CAPTION
;
132 // next translate the individual flags
133 if ( style
& wxMINIMIZE_BOX
)
134 msflags
|= WS_MINIMIZEBOX
;
135 if ( style
& wxMAXIMIZE_BOX
)
136 msflags
|= WS_MAXIMIZEBOX
;
137 if ( style
& wxSYSTEM_MENU
)
138 msflags
|= WS_SYSMENU
;
139 if ( style
& wxMINIMIZE
)
140 msflags
|= WS_MINIMIZE
;
141 if ( style
& wxMAXIMIZE
)
142 msflags
|= WS_MAXIMIZE
;
144 if ( style
& wxCLIP_CHILDREN
)
145 msflags
|= WS_CLIPCHILDREN
;
147 // Keep this here because it saves recoding this function in wxTinyFrame
148 #if wxUSE_ITSY_BITSY && !defined(__WIN32__)
149 if ( style
& wxTINY_CAPTION_VERT
)
150 msflags
|= IBS_VERTCAPTION
;
151 if ( style
& wxTINY_CAPTION_HORIZ
)
152 msflags
|= IBS_HORZCAPTION
;
154 if ( style
& (wxTINY_CAPTION_VERT
| wxTINY_CAPTION_HORIZ
) )
155 msflags
|= WS_CAPTION
;
160 *exflags
= MakeExtendedStyle(style
);
162 #if !defined(__WIN16__) && !defined(__SC__)
163 if ( !(GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
) )
165 // make all frames appear in the win9x shell taskbar unless
166 // wxFRAME_TOOL_WINDOW or wxFRAME_NO_TASKBAR is given - without
167 // giving them WS_EX_APPWINDOW style, the child (i.e. owned) frames
168 // wouldn't appear in it
169 if ( (style
& wxFRAME_TOOL_WINDOW
) || (style
& wxFRAME_NO_TASKBAR
) )
170 *exflags
|= WS_EX_TOOLWINDOW
;
171 else if ( !(style
& wxFRAME_NO_TASKBAR
) )
172 *exflags
|= WS_EX_APPWINDOW
;
176 if ( style
& wxSTAY_ON_TOP
)
177 *exflags
|= WS_EX_TOPMOST
;
180 if ( m_exStyle
& wxFRAME_EX_CONTEXTHELP
)
181 *exflags
|= WS_EX_CONTEXTHELP
;
188 bool wxTopLevelWindowMSW::CreateDialog(const wxChar
*dlgTemplate
,
189 const wxString
& title
,
193 #ifdef __WXMICROWIN__
194 // no dialogs support under MicroWin yet
195 return CreateFrame(title
, pos
, size
);
196 #else // !__WXMICROWIN__
197 wxWindow
*parent
= GetParent();
199 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
200 // app window as parent - this avoids creating modal dialogs without
202 if ( !parent
&& !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
) )
204 parent
= wxTheApp
->GetTopWindow();
208 // don't use transient windows as parents, this is dangerous as it
209 // can lead to a crash if the parent is destroyed before the child
211 // also don't use the window which is currently hidden as then the
212 // dialog would be hidden as well
213 if ( (parent
->GetExtraStyle() & wxWS_EX_TRANSIENT
) ||
221 m_hWnd
= (WXHWND
)::CreateDialog(wxGetInstance(),
223 parent
? GetHwndOf(parent
) : NULL
,
228 wxFAIL_MSG(_("Did you forget to include wx/msw/wx.rc in your resources?"));
230 wxLogSysError(_("Can't create dialog using template '%s'"), dlgTemplate
);
236 (void)MSWGetCreateWindowFlags(&exflags
);
240 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exflags
);
241 ::SetWindowPos(GetHwnd(), NULL
, 0, 0, 0, 0,
248 #if defined(__WIN95__)
249 // For some reason, the system menu is activated when we use the
250 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
251 if ( exflags
& WS_EX_CONTEXTHELP
)
253 wxFrame
*winTop
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
256 wxIcon icon
= winTop
->GetIcon();
259 ::SendMessage(GetHwnd(), WM_SETICON
,
261 (LPARAM
)GetHiconOf(icon
));
267 // move the dialog to its initial position without forcing repainting
269 if ( !MSWGetCreateWindowCoords(pos
, size
, x
, y
, w
, h
) )
272 w
= (int)CW_USEDEFAULT
;
275 // we can't use CW_USEDEFAULT here as we're not calling CreateWindow()
276 // and passing CW_USEDEFAULT to MoveWindow() results in resizing the
277 // window to (0, 0) size which breaks quite a lot of things, e.g. the
278 // sizer calculation in wxSizer::Fit()
279 if ( w
== (int)CW_USEDEFAULT
)
281 // the exact number doesn't matter, the dialog will be resized
282 // again soon anyhow but it should be big enough to allow
283 // calculation relying on "totalSize - clientSize > 0" work, i.e.
284 // at least greater than the title bar height
289 if ( x
== (int)CW_USEDEFAULT
)
291 // centre it on the screen - what else can we do?
292 wxSize sizeDpy
= wxGetDisplaySize();
294 x
= (sizeDpy
.x
- w
) / 2;
295 y
= (sizeDpy
.y
- h
) / 2;
298 if ( !::MoveWindow(GetHwnd(), x
, y
, w
, h
, FALSE
) )
300 wxLogLastError(wxT("MoveWindow"));
303 if ( !title
.empty() )
305 ::SetWindowText(GetHwnd(), title
);
311 #endif // __WXMICROWIN__/!__WXMICROWIN__
314 bool wxTopLevelWindowMSW::CreateFrame(const wxString
& title
,
319 long flags
= MSWGetCreateWindowFlags(&exflags
);
321 return MSWCreate(wxCanvasClassName
, title
, pos
, size
, flags
, exflags
);
324 bool wxTopLevelWindowMSW::Create(wxWindow
*parent
,
326 const wxString
& title
,
330 const wxString
& name
)
335 m_windowStyle
= style
;
339 m_windowId
= id
== -1 ? NewControlId() : id
;
341 wxTopLevelWindows
.Append(this);
344 parent
->AddChild(this);
346 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
348 // TODO: it would be better to construct the dialog template in memory
349 // during run-time than to rely on the limited number of
350 // templates in wx.rc because:
351 // a) you wouldn't have to include wx.rc in all wxWin programs
352 // (and the number of complaints about it would dtop)
353 // b) we'd be able to provide more templates simply, i.e.
354 // we could generate the templates for all style
357 // we have different dialog templates to allows creation of dialogs
358 // with & without captions under MSWindows, resizeable or not (but a
359 // resizeable dialog always has caption - otherwise it would look too
361 const wxChar
*dlgTemplate
;
362 if ( style
& wxRESIZE_BORDER
)
363 dlgTemplate
= wxT("wxResizeableDialog");
364 else if ( style
& wxCAPTION
)
365 dlgTemplate
= wxT("wxCaptionDialog");
367 dlgTemplate
= wxT("wxNoCaptionDialog");
369 return CreateDialog(dlgTemplate
, title
, pos
, size
);
373 return CreateFrame(title
, pos
, size
);
377 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
379 wxTopLevelWindows
.DeleteObject(this);
381 if ( wxModelessWindows
.Find(this) )
382 wxModelessWindows
.DeleteObject(this);
384 // If this is the last top-level window, exit.
385 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
387 wxTheApp
->SetTopWindow(NULL
);
389 if ( wxTheApp
->GetExitOnFrameDelete() )
391 ::PostQuitMessage(0);
396 // ----------------------------------------------------------------------------
397 // wxTopLevelWindowMSW showing
398 // ----------------------------------------------------------------------------
400 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd
)
402 ::ShowWindow(GetHwnd(), nShowCmd
);
404 m_iconized
= nShowCmd
== SW_MINIMIZE
;
407 bool wxTopLevelWindowMSW::Show(bool show
)
409 // don't use wxWindow version as we want to call DoShowWindow() ourselves
410 if ( !wxWindowBase::Show(show
) )
416 if ( m_maximizeOnShow
)
419 nShowCmd
= SW_MAXIMIZE
;
421 m_maximizeOnShow
= FALSE
;
433 DoShowWindow(nShowCmd
);
437 ::BringWindowToTop(GetHwnd());
439 wxActivateEvent
event(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
440 event
.SetEventObject( this );
441 GetEventHandler()->ProcessEvent(event
);
445 // Try to highlight the correct window (the parent)
448 HWND hWndParent
= GetHwndOf(GetParent());
450 ::BringWindowToTop(hWndParent
);
457 // ----------------------------------------------------------------------------
458 // wxTopLevelWindowMSW maximize/minimize
459 // ----------------------------------------------------------------------------
461 void wxTopLevelWindowMSW::Maximize(bool maximize
)
465 // just maximize it directly
466 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
470 // we can't maximize the hidden frame because it shows it as well, so
471 // just remember that we should do it later in this case
472 m_maximizeOnShow
= TRUE
;
476 bool wxTopLevelWindowMSW::IsMaximized() const
478 return ::IsZoomed(GetHwnd()) != 0;
481 void wxTopLevelWindowMSW::Iconize(bool iconize
)
483 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
486 bool wxTopLevelWindowMSW::IsIconized() const
488 // also update the current state
489 ((wxTopLevelWindowMSW
*)this)->m_iconized
= ::IsIconic(GetHwnd()) != 0;
494 void wxTopLevelWindowMSW::Restore()
496 DoShowWindow(SW_RESTORE
);
499 // ----------------------------------------------------------------------------
500 // wxTopLevelWindowMSW fullscreen
501 // ----------------------------------------------------------------------------
503 bool wxTopLevelWindowMSW::ShowFullScreen(bool show
, long style
)
510 m_fsIsShowing
= TRUE
;
513 // zap the frame borders
515 // save the 'normal' window style
516 m_fsOldWindowStyle
= GetWindowLong((HWND
)GetHWND(), GWL_STYLE
);
518 // save the old position, width & height, maximize state
519 m_fsOldSize
= GetRect();
520 m_fsIsMaximized
= IsMaximized();
522 // decide which window style flags to turn off
523 LONG newStyle
= m_fsOldWindowStyle
;
526 if (style
& wxFULLSCREEN_NOBORDER
)
527 offFlags
|= WS_BORDER
| WS_THICKFRAME
;
528 if (style
& wxFULLSCREEN_NOCAPTION
)
529 offFlags
|= (WS_CAPTION
| WS_SYSMENU
);
531 newStyle
&= (~offFlags
);
533 // change our window style to be compatible with full-screen mode
534 ::SetWindowLong((HWND
)GetHWND(), GWL_STYLE
, newStyle
);
536 // resize to the size of the desktop
539 RECT rect
= wxGetWindowRect(::GetDesktopWindow());
540 width
= rect
.right
- rect
.left
;
541 height
= rect
.bottom
- rect
.top
;
543 SetSize(width
, height
);
545 // now flush the window style cache and actually go full-screen
546 SetWindowPos((HWND
)GetHWND(), HWND_TOP
, 0, 0, width
, height
, SWP_FRAMECHANGED
);
548 wxSizeEvent
event(wxSize(width
, height
), GetId());
549 GetEventHandler()->ProcessEvent(event
);
558 m_fsIsShowing
= FALSE
;
560 Maximize(m_fsIsMaximized
);
561 SetWindowLong((HWND
)GetHWND(),GWL_STYLE
, m_fsOldWindowStyle
);
562 SetWindowPos((HWND
)GetHWND(),HWND_TOP
,m_fsOldSize
.x
, m_fsOldSize
.y
,
563 m_fsOldSize
.width
, m_fsOldSize
.height
, SWP_FRAMECHANGED
);
569 // ----------------------------------------------------------------------------
570 // wxTopLevelWindowMSW misc
571 // ----------------------------------------------------------------------------
573 void wxTopLevelWindowMSW::SetIcon(const wxIcon
& icon
)
576 wxTopLevelWindowBase::SetIcon(icon
);
578 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
581 ::SendMessage(GetHwnd(), WM_SETICON
,
582 (WPARAM
)TRUE
, (LPARAM
)GetHiconOf(m_icon
));
587 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable
)
589 #ifndef __WXMICROWIN__
590 // get system (a.k.a. window) menu
591 HMENU hmenu
= ::GetSystemMenu(GetHwnd(), FALSE
/* get it */);
594 wxLogLastError(_T("GetSystemMenu"));
599 // enabling/disabling the close item from it also automatically
600 // disables/enables the close title bar button
601 if ( ::EnableMenuItem(hmenu
, SC_CLOSE
,
603 (enable
? MF_ENABLED
: MF_GRAYED
)) == -1 )
605 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
610 // update appearance immediately
611 if ( !::DrawMenuBar(GetHwnd()) )
613 wxLogLastError(_T("DrawMenuBar"));
615 #endif // !__WXMICROWIN__