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 if ( style
& wxFRAME_TOOL_WINDOW
)
113 msflags
= WS_OVERLAPPED
;
115 // border and caption styles
116 if ( style
& wxRESIZE_BORDER
)
117 msflags
|= WS_THICKFRAME
;
118 else if ( !(style
& wxBORDER_NONE
) )
119 msflags
|= WS_BORDER
;
121 if ( style
& wxCAPTION
)
122 msflags
|= WS_CAPTION
;
124 // if we don't set WS_POPUP, Windows assumes WS_OVERLAPPED and creates a
125 // window with both caption and border
126 if ( msflags
& (WS_CAPTION
| WS_BORDER
) != WS_CAPTION
| WS_BORDER
)
131 // next translate the individual flags
132 if ( style
& wxMINIMIZE_BOX
)
133 msflags
|= WS_MINIMIZEBOX
;
134 if ( style
& wxMAXIMIZE_BOX
)
135 msflags
|= WS_MAXIMIZEBOX
;
136 if ( style
& wxSYSTEM_MENU
)
137 msflags
|= WS_SYSMENU
;
138 if ( style
& wxMINIMIZE
)
139 msflags
|= WS_MINIMIZE
;
140 if ( style
& wxMAXIMIZE
)
141 msflags
|= WS_MAXIMIZE
;
143 if ( style
& wxCLIP_CHILDREN
)
144 msflags
|= WS_CLIPCHILDREN
;
146 // Keep this here because it saves recoding this function in wxTinyFrame
147 #if wxUSE_ITSY_BITSY && !defined(__WIN32__)
148 if ( style
& wxTINY_CAPTION_VERT
)
149 msflags
|= IBS_VERTCAPTION
;
150 if ( style
& wxTINY_CAPTION_HORIZ
)
151 msflags
|= IBS_HORZCAPTION
;
153 if ( style
& (wxTINY_CAPTION_VERT
| wxTINY_CAPTION_HORIZ
) )
154 msflags
|= WS_CAPTION
;
159 *exflags
= MakeExtendedStyle(style
);
161 #if !defined(__WIN16__) && !defined(__SC__)
162 if ( !(GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
) )
164 // make all frames appear in the win9x shell taskbar unless
165 // wxFRAME_TOOL_WINDOW or wxFRAME_NO_TASKBAR is given - without
166 // giving them WS_EX_APPWINDOW style, the child (i.e. owned) frames
167 // wouldn't appear in it
168 if ( (style
& wxFRAME_TOOL_WINDOW
) || (style
& wxFRAME_NO_TASKBAR
) )
169 *exflags
|= WS_EX_TOOLWINDOW
;
170 else if ( !(style
& wxFRAME_NO_TASKBAR
) )
171 *exflags
|= WS_EX_APPWINDOW
;
175 if ( style
& wxSTAY_ON_TOP
)
176 *exflags
|= WS_EX_TOPMOST
;
179 if ( m_exStyle
& wxFRAME_EX_CONTEXTHELP
)
180 *exflags
|= WS_EX_CONTEXTHELP
;
187 bool wxTopLevelWindowMSW::CreateDialog(const wxChar
*dlgTemplate
,
188 const wxString
& title
,
192 #ifdef __WXMICROWIN__
193 // no dialogs support under MicroWin yet
194 return CreateFrame(title
, pos
, size
);
195 #else // !__WXMICROWIN__
196 wxWindow
*parent
= GetParent();
198 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
199 // app window as parent - this avoids creating modal dialogs without
201 if ( !parent
&& !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
) )
203 parent
= wxTheApp
->GetTopWindow();
207 // don't use transient windows as parents, this is dangerous as it
208 // can lead to a crash if the parent is destroyed before the child
210 // also don't use the window which is currently hidden as then the
211 // dialog would be hidden as well
212 if ( (parent
->GetExtraStyle() & wxWS_EX_TRANSIENT
) ||
220 m_hWnd
= (WXHWND
)::CreateDialog(wxGetInstance(),
222 parent
? GetHwndOf(parent
) : NULL
,
227 wxFAIL_MSG(_("Did you forget to include wx/msw/wx.rc in your resources?"));
229 wxLogSysError(_("Can't create dialog using template '%s'"), dlgTemplate
);
235 (void)MSWGetCreateWindowFlags(&exflags
);
239 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exflags
);
240 ::SetWindowPos(GetHwnd(), NULL
, 0, 0, 0, 0,
247 #if defined(__WIN95__)
248 // For some reason, the system menu is activated when we use the
249 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
250 if ( exflags
& WS_EX_CONTEXTHELP
)
252 wxFrame
*winTop
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
255 wxIcon icon
= winTop
->GetIcon();
258 ::SendMessage(GetHwnd(), WM_SETICON
,
260 (LPARAM
)GetHiconOf(icon
));
266 // move the dialog to its initial position without forcing repainting
268 if ( !MSWGetCreateWindowCoords(pos
, size
, x
, y
, w
, h
) )
271 w
= (int)CW_USEDEFAULT
;
274 // we can't use CW_USEDEFAULT here as we're not calling CreateWindow()
275 // and passing CW_USEDEFAULT to MoveWindow() results in resizing the
276 // window to (0, 0) size which breaks quite a lot of things, e.g. the
277 // sizer calculation in wxSizer::Fit()
278 if ( w
== (int)CW_USEDEFAULT
)
280 // the exact number doesn't matter, the dialog will be resized
281 // again soon anyhow but it should be big enough to allow
282 // calculation relying on "totalSize - clientSize > 0" work, i.e.
283 // at least greater than the title bar height
288 if ( x
== (int)CW_USEDEFAULT
)
290 // centre it on the screen - what else can we do?
291 wxSize sizeDpy
= wxGetDisplaySize();
293 x
= (sizeDpy
.x
- w
) / 2;
294 y
= (sizeDpy
.y
- h
) / 2;
297 if ( !::MoveWindow(GetHwnd(), x
, y
, w
, h
, FALSE
) )
299 wxLogLastError(wxT("MoveWindow"));
302 if ( !title
.empty() )
304 ::SetWindowText(GetHwnd(), title
);
310 #endif // __WXMICROWIN__/!__WXMICROWIN__
313 bool wxTopLevelWindowMSW::CreateFrame(const wxString
& title
,
318 long flags
= MSWGetCreateWindowFlags(&exflags
);
320 return MSWCreate(wxCanvasClassName
, title
, pos
, size
, flags
, exflags
);
323 bool wxTopLevelWindowMSW::Create(wxWindow
*parent
,
325 const wxString
& title
,
329 const wxString
& name
)
334 m_windowStyle
= style
;
338 m_windowId
= id
== -1 ? NewControlId() : id
;
340 wxTopLevelWindows
.Append(this);
343 parent
->AddChild(this);
345 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
347 // TODO: it would be better to construct the dialog template in memory
348 // during run-time than to rely on the limited number of
349 // templates in wx.rc because:
350 // a) you wouldn't have to include wx.rc in all wxWin programs
351 // (and the number of complaints about it would dtop)
352 // b) we'd be able to provide more templates simply, i.e.
353 // we could generate the templates for all style
356 // we have different dialog templates to allows creation of dialogs
357 // with & without captions under MSWindows, resizeable or not (but a
358 // resizeable dialog always has caption - otherwise it would look too
360 const wxChar
*dlgTemplate
;
361 if ( style
& wxRESIZE_BORDER
)
362 dlgTemplate
= wxT("wxResizeableDialog");
363 else if ( style
& wxCAPTION
)
364 dlgTemplate
= wxT("wxCaptionDialog");
366 dlgTemplate
= wxT("wxNoCaptionDialog");
368 return CreateDialog(dlgTemplate
, title
, pos
, size
);
372 return CreateFrame(title
, pos
, size
);
376 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
378 wxTopLevelWindows
.DeleteObject(this);
380 if ( wxModelessWindows
.Find(this) )
381 wxModelessWindows
.DeleteObject(this);
383 // If this is the last top-level window, exit.
384 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
386 wxTheApp
->SetTopWindow(NULL
);
388 if ( wxTheApp
->GetExitOnFrameDelete() )
390 ::PostQuitMessage(0);
395 // ----------------------------------------------------------------------------
396 // wxTopLevelWindowMSW showing
397 // ----------------------------------------------------------------------------
399 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd
)
401 ::ShowWindow(GetHwnd(), nShowCmd
);
403 m_iconized
= nShowCmd
== SW_MINIMIZE
;
406 bool wxTopLevelWindowMSW::Show(bool show
)
408 // don't use wxWindow version as we want to call DoShowWindow() ourselves
409 if ( !wxWindowBase::Show(show
) )
415 if ( m_maximizeOnShow
)
418 nShowCmd
= SW_MAXIMIZE
;
420 m_maximizeOnShow
= FALSE
;
432 DoShowWindow(nShowCmd
);
436 ::BringWindowToTop(GetHwnd());
438 wxActivateEvent
event(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
439 event
.SetEventObject( this );
440 GetEventHandler()->ProcessEvent(event
);
444 // Try to highlight the correct window (the parent)
447 HWND hWndParent
= GetHwndOf(GetParent());
449 ::BringWindowToTop(hWndParent
);
456 // ----------------------------------------------------------------------------
457 // wxTopLevelWindowMSW maximize/minimize
458 // ----------------------------------------------------------------------------
460 void wxTopLevelWindowMSW::Maximize(bool maximize
)
464 // just maximize it directly
465 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
469 // we can't maximize the hidden frame because it shows it as well, so
470 // just remember that we should do it later in this case
471 m_maximizeOnShow
= TRUE
;
475 bool wxTopLevelWindowMSW::IsMaximized() const
477 return ::IsZoomed(GetHwnd()) != 0;
480 void wxTopLevelWindowMSW::Iconize(bool iconize
)
482 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
485 bool wxTopLevelWindowMSW::IsIconized() const
487 // also update the current state
488 ((wxTopLevelWindowMSW
*)this)->m_iconized
= ::IsIconic(GetHwnd()) != 0;
493 void wxTopLevelWindowMSW::Restore()
495 DoShowWindow(SW_RESTORE
);
498 // ----------------------------------------------------------------------------
499 // wxTopLevelWindowMSW fullscreen
500 // ----------------------------------------------------------------------------
502 bool wxTopLevelWindowMSW::ShowFullScreen(bool show
, long style
)
509 m_fsIsShowing
= TRUE
;
512 // zap the frame borders
514 // save the 'normal' window style
515 m_fsOldWindowStyle
= GetWindowLong((HWND
)GetHWND(), GWL_STYLE
);
517 // save the old position, width & height, maximize state
518 m_fsOldSize
= GetRect();
519 m_fsIsMaximized
= IsMaximized();
521 // decide which window style flags to turn off
522 LONG newStyle
= m_fsOldWindowStyle
;
525 if (style
& wxFULLSCREEN_NOBORDER
)
526 offFlags
|= WS_BORDER
| WS_THICKFRAME
;
527 if (style
& wxFULLSCREEN_NOCAPTION
)
528 offFlags
|= (WS_CAPTION
| WS_SYSMENU
);
530 newStyle
&= (~offFlags
);
532 // change our window style to be compatible with full-screen mode
533 ::SetWindowLong((HWND
)GetHWND(), GWL_STYLE
, newStyle
);
535 // resize to the size of the desktop
538 RECT rect
= wxGetWindowRect(::GetDesktopWindow());
539 width
= rect
.right
- rect
.left
;
540 height
= rect
.bottom
- rect
.top
;
542 SetSize(width
, height
);
544 // now flush the window style cache and actually go full-screen
545 SetWindowPos((HWND
)GetHWND(), HWND_TOP
, 0, 0, width
, height
, SWP_FRAMECHANGED
);
547 wxSizeEvent
event(wxSize(width
, height
), GetId());
548 GetEventHandler()->ProcessEvent(event
);
557 m_fsIsShowing
= FALSE
;
559 Maximize(m_fsIsMaximized
);
560 SetWindowLong((HWND
)GetHWND(),GWL_STYLE
, m_fsOldWindowStyle
);
561 SetWindowPos((HWND
)GetHWND(),HWND_TOP
,m_fsOldSize
.x
, m_fsOldSize
.y
,
562 m_fsOldSize
.width
, m_fsOldSize
.height
, SWP_FRAMECHANGED
);
568 // ----------------------------------------------------------------------------
569 // wxTopLevelWindowMSW misc
570 // ----------------------------------------------------------------------------
572 void wxTopLevelWindowMSW::SetIcon(const wxIcon
& icon
)
575 wxTopLevelWindowBase::SetIcon(icon
);
577 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
580 ::SendMessage(GetHwnd(), WM_SETICON
,
581 (WPARAM
)TRUE
, (LPARAM
)GetHiconOf(m_icon
));
586 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable
)
588 #ifndef __WXMICROWIN__
589 // get system (a.k.a. window) menu
590 HMENU hmenu
= ::GetSystemMenu(GetHwnd(), FALSE
/* get it */);
593 wxLogLastError(_T("GetSystemMenu"));
598 // enabling/disabling the close item from it also automatically
599 // disables/enables the close title bar button
600 if ( ::EnableMenuItem(hmenu
, SC_CLOSE
,
602 (enable
? MF_ENABLED
: MF_GRAYED
)) == -1 )
604 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
609 // update appearance immediately
610 if ( !::DrawMenuBar(GetHwnd()) )
612 wxLogLastError(_T("DrawMenuBar"));
614 #endif // !__WXMICROWIN__