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();
109 // first select the kind of window being created
110 if ( style
& wxCAPTION
)
112 if ( style
& wxFRAME_TOOL_WINDOW
)
113 msflags
|= WS_POPUPWINDOW
;
115 msflags
|= WS_OVERLAPPED
;
122 // next translate the individual flags
123 if ( style
& wxMINIMIZE_BOX
)
124 msflags
|= WS_MINIMIZEBOX
;
125 if ( style
& wxMAXIMIZE_BOX
)
126 msflags
|= WS_MAXIMIZEBOX
;
127 if ( style
& wxTHICK_FRAME
)
128 msflags
|= WS_THICKFRAME
;
129 if ( style
& wxSYSTEM_MENU
)
130 msflags
|= WS_SYSMENU
;
131 if ( style
& wxMINIMIZE
)
132 msflags
|= WS_MINIMIZE
;
133 if ( style
& wxMAXIMIZE
)
134 msflags
|= WS_MAXIMIZE
;
135 if ( style
& wxCAPTION
)
136 msflags
|= WS_CAPTION
;
137 if ( style
& wxCLIP_CHILDREN
)
138 msflags
|= WS_CLIPCHILDREN
;
140 // Keep this here because it saves recoding this function in wxTinyFrame
141 #if wxUSE_ITSY_BITSY && !defined(__WIN32__)
142 if ( style
& wxTINY_CAPTION_VERT
)
143 msflags
|= IBS_VERTCAPTION
;
144 if ( style
& wxTINY_CAPTION_HORIZ
)
145 msflags
|= IBS_HORZCAPTION
;
147 if ( style
& (wxTINY_CAPTION_VERT
| wxTINY_CAPTION_HORIZ
) )
148 msflags
|= WS_CAPTION
;
153 *exflags
= MakeExtendedStyle(style
);
155 #if !defined(__WIN16__) && !defined(__SC__)
156 if ( !(GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
) )
158 // make all frames appear in the win9x shell taskbar unless
159 // wxFRAME_TOOL_WINDOW or wxFRAME_NO_TASKBAR is given - without
160 // giving them WS_EX_APPWINDOW style, the child (i.e. owned) frames
161 // wouldn't appear in it
162 if ( (style
& wxFRAME_TOOL_WINDOW
) || (style
& wxFRAME_NO_TASKBAR
) )
163 *exflags
|= WS_EX_TOOLWINDOW
;
164 else if ( !(style
& wxFRAME_NO_TASKBAR
) )
165 *exflags
|= WS_EX_APPWINDOW
;
169 if ( style
& wxSTAY_ON_TOP
)
170 *exflags
|= WS_EX_TOPMOST
;
173 if ( m_exStyle
& wxFRAME_EX_CONTEXTHELP
)
174 *exflags
|= WS_EX_CONTEXTHELP
;
181 bool wxTopLevelWindowMSW::CreateDialog(const wxChar
*dlgTemplate
,
182 const wxString
& title
,
186 #ifdef __WXMICROWIN__
187 // no dialogs support under MicroWin yet
188 return CreateFrame(title
, pos
, size
);
189 #else // !__WXMICROWIN__
190 wxWindow
*parent
= GetParent();
192 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
193 // app window as parent - this avoids creating modal dialogs without
195 if ( !parent
&& !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
) )
197 parent
= wxTheApp
->GetTopWindow();
201 // don't use transient windows as parents, this is dangerous as it
202 // can lead to a crash if the parent is destroyed before the child
204 // also don't use the window which is currently hidden as then the
205 // dialog would be hidden as well
206 if ( (parent
->GetExtraStyle() & wxWS_EX_TRANSIENT
) ||
214 m_hWnd
= (WXHWND
)::CreateDialog(wxGetInstance(),
216 parent
? GetHwndOf(parent
) : NULL
,
221 wxFAIL_MSG(_("Did you forget to include wx/msw/wx.rc in your resources?"));
223 wxLogSysError(_("Can't create dialog using template '%s'"), dlgTemplate
);
229 (void)MSWGetCreateWindowFlags(&exflags
);
233 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exflags
);
234 ::SetWindowPos(GetHwnd(), NULL
, 0, 0, 0, 0,
241 #if defined(__WIN95__)
242 // For some reason, the system menu is activated when we use the
243 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
244 if ( exflags
& WS_EX_CONTEXTHELP
)
246 wxFrame
*winTop
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
249 wxIcon icon
= winTop
->GetIcon();
252 ::SendMessage(GetHwnd(), WM_SETICON
,
254 (LPARAM
)GetHiconOf(icon
));
260 // move the dialog to its initial position without forcing repainting
262 if ( !MSWGetCreateWindowCoords(pos
, size
, x
, y
, w
, h
) )
265 w
= (int)CW_USEDEFAULT
;
268 // we can't use CW_USEDEFAULT here as we're not calling CreateWindow()
269 // and passing CW_USEDEFAULT to MoveWindow() results in resizing the
270 // window to (0, 0) size which breaks quite a lot of things, e.g. the
271 // sizer calculation in wxSizer::Fit()
272 if ( w
== (int)CW_USEDEFAULT
)
274 // the exact number doesn't matter, the dialog will be resized
275 // again soon anyhow but it should be big enough to allow
276 // calculation relying on "totalSize - clientSize > 0" work, i.e.
277 // at least greater than the title bar height
282 if ( x
== (int)CW_USEDEFAULT
)
284 // centre it on the screen - what else can we do?
285 wxSize sizeDpy
= wxGetDisplaySize();
287 x
= (sizeDpy
.x
- w
) / 2;
288 y
= (sizeDpy
.y
- h
) / 2;
291 if ( !::MoveWindow(GetHwnd(), x
, y
, w
, h
, FALSE
) )
293 wxLogLastError(wxT("MoveWindow"));
296 if ( !title
.empty() )
298 ::SetWindowText(GetHwnd(), title
);
304 #endif // __WXMICROWIN__/!__WXMICROWIN__
307 bool wxTopLevelWindowMSW::CreateFrame(const wxString
& title
,
312 long flags
= MSWGetCreateWindowFlags(&exflags
);
314 return MSWCreate(wxCanvasClassName
, title
, pos
, size
, flags
, exflags
);
317 bool wxTopLevelWindowMSW::Create(wxWindow
*parent
,
319 const wxString
& title
,
323 const wxString
& name
)
328 m_windowStyle
= style
;
332 m_windowId
= id
== -1 ? NewControlId() : id
;
334 wxTopLevelWindows
.Append(this);
337 parent
->AddChild(this);
339 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
341 // TODO: it would be better to construct the dialog template in memory
342 // during run-time than to rely on the limited number of
343 // templates in wx.rc because:
344 // a) you wouldn't have to include wx.rc in all wxWin programs
345 // (and the number of complaints about it would dtop)
346 // b) we'd be able to provide more templates simply, i.e.
347 // we could generate the templates for all style
350 // we have different dialog templates to allows creation of dialogs
351 // with & without captions under MSWindows, resizeable or not (but a
352 // resizeable dialog always has caption - otherwise it would look too
354 const wxChar
*dlgTemplate
;
355 if ( style
& wxRESIZE_BORDER
)
356 dlgTemplate
= wxT("wxResizeableDialog");
357 else if ( style
& wxCAPTION
)
358 dlgTemplate
= wxT("wxCaptionDialog");
360 dlgTemplate
= wxT("wxNoCaptionDialog");
362 return CreateDialog(dlgTemplate
, title
, pos
, size
);
366 return CreateFrame(title
, pos
, size
);
370 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
372 wxTopLevelWindows
.DeleteObject(this);
374 if ( wxModelessWindows
.Find(this) )
375 wxModelessWindows
.DeleteObject(this);
377 // If this is the last top-level window, exit.
378 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
380 wxTheApp
->SetTopWindow(NULL
);
382 if ( wxTheApp
->GetExitOnFrameDelete() )
384 ::PostQuitMessage(0);
389 // ----------------------------------------------------------------------------
390 // wxTopLevelWindowMSW showing
391 // ----------------------------------------------------------------------------
393 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd
)
395 ::ShowWindow(GetHwnd(), nShowCmd
);
397 m_iconized
= nShowCmd
== SW_MINIMIZE
;
400 bool wxTopLevelWindowMSW::Show(bool show
)
402 // don't use wxWindow version as we want to call DoShowWindow() ourselves
403 if ( !wxWindowBase::Show(show
) )
409 if ( m_maximizeOnShow
)
412 nShowCmd
= SW_MAXIMIZE
;
414 m_maximizeOnShow
= FALSE
;
426 DoShowWindow(nShowCmd
);
430 ::BringWindowToTop(GetHwnd());
432 wxActivateEvent
event(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
433 event
.SetEventObject( this );
434 GetEventHandler()->ProcessEvent(event
);
438 // Try to highlight the correct window (the parent)
441 HWND hWndParent
= GetHwndOf(GetParent());
443 ::BringWindowToTop(hWndParent
);
450 // ----------------------------------------------------------------------------
451 // wxTopLevelWindowMSW maximize/minimize
452 // ----------------------------------------------------------------------------
454 void wxTopLevelWindowMSW::Maximize(bool maximize
)
458 // just maximize it directly
459 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
463 // we can't maximize the hidden frame because it shows it as well, so
464 // just remember that we should do it later in this case
465 m_maximizeOnShow
= TRUE
;
469 bool wxTopLevelWindowMSW::IsMaximized() const
471 return ::IsZoomed(GetHwnd()) != 0;
474 void wxTopLevelWindowMSW::Iconize(bool iconize
)
476 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
479 bool wxTopLevelWindowMSW::IsIconized() const
481 // also update the current state
482 ((wxTopLevelWindowMSW
*)this)->m_iconized
= ::IsIconic(GetHwnd()) != 0;
487 void wxTopLevelWindowMSW::Restore()
489 DoShowWindow(SW_RESTORE
);
492 // ----------------------------------------------------------------------------
493 // wxTopLevelWindowMSW fullscreen
494 // ----------------------------------------------------------------------------
496 bool wxTopLevelWindowMSW::ShowFullScreen(bool show
, long style
)
503 m_fsIsShowing
= TRUE
;
506 // zap the frame borders
508 // save the 'normal' window style
509 m_fsOldWindowStyle
= GetWindowLong((HWND
)GetHWND(), GWL_STYLE
);
511 // save the old position, width & height, maximize state
512 m_fsOldSize
= GetRect();
513 m_fsIsMaximized
= IsMaximized();
515 // decide which window style flags to turn off
516 LONG newStyle
= m_fsOldWindowStyle
;
519 if (style
& wxFULLSCREEN_NOBORDER
)
520 offFlags
|= WS_BORDER
| WS_THICKFRAME
;
521 if (style
& wxFULLSCREEN_NOCAPTION
)
522 offFlags
|= (WS_CAPTION
| WS_SYSMENU
);
524 newStyle
&= (~offFlags
);
526 // change our window style to be compatible with full-screen mode
527 ::SetWindowLong((HWND
)GetHWND(), GWL_STYLE
, newStyle
);
529 // resize to the size of the desktop
532 RECT rect
= wxGetWindowRect(::GetDesktopWindow());
533 width
= rect
.right
- rect
.left
;
534 height
= rect
.bottom
- rect
.top
;
536 SetSize(width
, height
);
538 // now flush the window style cache and actually go full-screen
539 SetWindowPos((HWND
)GetHWND(), HWND_TOP
, 0, 0, width
, height
, SWP_FRAMECHANGED
);
541 wxSizeEvent
event(wxSize(width
, height
), GetId());
542 GetEventHandler()->ProcessEvent(event
);
551 m_fsIsShowing
= FALSE
;
553 Maximize(m_fsIsMaximized
);
554 SetWindowLong((HWND
)GetHWND(),GWL_STYLE
, m_fsOldWindowStyle
);
555 SetWindowPos((HWND
)GetHWND(),HWND_TOP
,m_fsOldSize
.x
, m_fsOldSize
.y
,
556 m_fsOldSize
.width
, m_fsOldSize
.height
, SWP_FRAMECHANGED
);
562 // ----------------------------------------------------------------------------
563 // wxTopLevelWindowMSW misc
564 // ----------------------------------------------------------------------------
566 void wxTopLevelWindowMSW::SetIcon(const wxIcon
& icon
)
569 wxTopLevelWindowBase::SetIcon(icon
);
571 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
574 ::SendMessage(GetHwnd(), WM_SETICON
,
575 (WPARAM
)TRUE
, (LPARAM
)GetHiconOf(m_icon
));
580 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable
)
582 #ifndef __WXMICROWIN__
583 // get system (a.k.a. window) menu
584 HMENU hmenu
= ::GetSystemMenu(GetHwnd(), FALSE
/* get it */);
587 wxLogLastError(_T("GetSystemMenu"));
592 // enabling/disabling the close item from it also automatically
593 // disables/enables the close title bar button
594 if ( ::EnableMenuItem(hmenu
, SC_CLOSE
,
596 (enable
? MF_ENABLED
: MF_GRAYED
)) == -1 )
598 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
603 // update appearance immediately
604 if ( !::DrawMenuBar(GetHwnd()) )
606 wxLogLastError(_T("DrawMenuBar"));
608 #endif // !__WXMICROWIN__