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 void *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
)::CreateDialogIndirect(wxGetInstance(),
222 (DLGTEMPLATE
*)dlgTemplate
,
223 parent
? GetHwndOf(parent
) : NULL
,
228 wxFAIL_MSG(_("Failed to create dialog. Incorrect DLGTEMPLATE?"));
230 wxLogSysError(_("Can't create dialog using memory template"));
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 int dlgsize
= sizeof(DLGTEMPLATE
) + (sizeof(WORD
) * 3);
362 DLGTEMPLATE
* dlgTemplate
= (DLGTEMPLATE
*)malloc( dlgsize
);
363 memset (dlgTemplate
, 0, dlgsize
);
366 dlgTemplate
->cx
= 144;
367 dlgTemplate
->cy
= 75;
369 if ( style
& wxRESIZE_BORDER
)
370 dlgTemplate
->style
= DS_MODALFRAME
| WS_CAPTION
| WS_POPUP
| WS_SYSMENU
| WS_THICKFRAME
;
371 else if ( style
& wxCAPTION
)
372 dlgTemplate
->style
= DS_MODALFRAME
| WS_CAPTION
| WS_POPUP
| WS_SYSMENU
;
374 dlgTemplate
->style
= WS_POPUP
;
376 bool ret
= CreateDialog(dlgTemplate
, title
, pos
, size
);
382 return CreateFrame(title
, pos
, size
);
386 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
388 wxTopLevelWindows
.DeleteObject(this);
390 if ( wxModelessWindows
.Find(this) )
391 wxModelessWindows
.DeleteObject(this);
393 // If this is the last top-level window, exit.
394 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
396 wxTheApp
->SetTopWindow(NULL
);
398 if ( wxTheApp
->GetExitOnFrameDelete() )
400 ::PostQuitMessage(0);
405 // ----------------------------------------------------------------------------
406 // wxTopLevelWindowMSW showing
407 // ----------------------------------------------------------------------------
409 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd
)
411 ::ShowWindow(GetHwnd(), nShowCmd
);
413 m_iconized
= nShowCmd
== SW_MINIMIZE
;
416 bool wxTopLevelWindowMSW::Show(bool show
)
418 // don't use wxWindow version as we want to call DoShowWindow() ourselves
419 if ( !wxWindowBase::Show(show
) )
425 if ( m_maximizeOnShow
)
428 nShowCmd
= SW_MAXIMIZE
;
430 m_maximizeOnShow
= FALSE
;
442 DoShowWindow(nShowCmd
);
446 ::BringWindowToTop(GetHwnd());
448 wxActivateEvent
event(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
449 event
.SetEventObject( this );
450 GetEventHandler()->ProcessEvent(event
);
454 // Try to highlight the correct window (the parent)
457 HWND hWndParent
= GetHwndOf(GetParent());
459 ::BringWindowToTop(hWndParent
);
466 // ----------------------------------------------------------------------------
467 // wxTopLevelWindowMSW maximize/minimize
468 // ----------------------------------------------------------------------------
470 void wxTopLevelWindowMSW::Maximize(bool maximize
)
474 // just maximize it directly
475 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
479 // we can't maximize the hidden frame because it shows it as well, so
480 // just remember that we should do it later in this case
481 m_maximizeOnShow
= TRUE
;
485 bool wxTopLevelWindowMSW::IsMaximized() const
487 return ::IsZoomed(GetHwnd()) != 0;
490 void wxTopLevelWindowMSW::Iconize(bool iconize
)
492 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
495 bool wxTopLevelWindowMSW::IsIconized() const
497 // also update the current state
498 ((wxTopLevelWindowMSW
*)this)->m_iconized
= ::IsIconic(GetHwnd()) != 0;
503 void wxTopLevelWindowMSW::Restore()
505 DoShowWindow(SW_RESTORE
);
508 // ----------------------------------------------------------------------------
509 // wxTopLevelWindowMSW fullscreen
510 // ----------------------------------------------------------------------------
512 bool wxTopLevelWindowMSW::ShowFullScreen(bool show
, long style
)
519 m_fsIsShowing
= TRUE
;
522 // zap the frame borders
524 // save the 'normal' window style
525 m_fsOldWindowStyle
= GetWindowLong((HWND
)GetHWND(), GWL_STYLE
);
527 // save the old position, width & height, maximize state
528 m_fsOldSize
= GetRect();
529 m_fsIsMaximized
= IsMaximized();
531 // decide which window style flags to turn off
532 LONG newStyle
= m_fsOldWindowStyle
;
535 if (style
& wxFULLSCREEN_NOBORDER
)
536 offFlags
|= WS_BORDER
| WS_THICKFRAME
;
537 if (style
& wxFULLSCREEN_NOCAPTION
)
538 offFlags
|= (WS_CAPTION
| WS_SYSMENU
);
540 newStyle
&= (~offFlags
);
542 // change our window style to be compatible with full-screen mode
543 ::SetWindowLong((HWND
)GetHWND(), GWL_STYLE
, newStyle
);
545 // resize to the size of the desktop
548 RECT rect
= wxGetWindowRect(::GetDesktopWindow());
549 width
= rect
.right
- rect
.left
;
550 height
= rect
.bottom
- rect
.top
;
552 SetSize(width
, height
);
554 // now flush the window style cache and actually go full-screen
555 SetWindowPos((HWND
)GetHWND(), HWND_TOP
, 0, 0, width
, height
, SWP_FRAMECHANGED
);
557 wxSizeEvent
event(wxSize(width
, height
), GetId());
558 GetEventHandler()->ProcessEvent(event
);
567 m_fsIsShowing
= FALSE
;
569 Maximize(m_fsIsMaximized
);
570 SetWindowLong((HWND
)GetHWND(),GWL_STYLE
, m_fsOldWindowStyle
);
571 SetWindowPos((HWND
)GetHWND(),HWND_TOP
,m_fsOldSize
.x
, m_fsOldSize
.y
,
572 m_fsOldSize
.width
, m_fsOldSize
.height
, SWP_FRAMECHANGED
);
578 // ----------------------------------------------------------------------------
579 // wxTopLevelWindowMSW misc
580 // ----------------------------------------------------------------------------
582 void wxTopLevelWindowMSW::SetIcon(const wxIcon
& icon
)
585 wxTopLevelWindowBase::SetIcon(icon
);
587 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
590 ::SendMessage(GetHwnd(), WM_SETICON
,
591 (WPARAM
)TRUE
, (LPARAM
)GetHiconOf(m_icon
));
596 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable
)
598 #ifndef __WXMICROWIN__
599 // get system (a.k.a. window) menu
600 HMENU hmenu
= ::GetSystemMenu(GetHwnd(), FALSE
/* get it */);
603 wxLogLastError(_T("GetSystemMenu"));
608 // enabling/disabling the close item from it also automatically
609 // disables/enables the close title bar button
610 if ( ::EnableMenuItem(hmenu
, SC_CLOSE
,
612 (enable
? MF_ENABLED
: MF_GRAYED
)) == -1 )
614 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
619 // update appearance immediately
620 if ( !::DrawMenuBar(GetHwnd()) )
622 wxLogLastError(_T("DrawMenuBar"));
624 #endif // !__WXMICROWIN__