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"
50 // ----------------------------------------------------------------------------
51 // stubs for missing functions under MicroWindows
52 // ----------------------------------------------------------------------------
56 // static inline bool IsIconic(HWND WXUNUSED(hwnd)) { return FALSE; }
57 static inline bool IsZoomed(HWND
WXUNUSED(hwnd
)) { return FALSE
; }
59 #endif // __WXMICROWIN__
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 // list of all frames and modeless dialogs
66 wxWindowList wxModelessWindows
;
68 // the name of the default wxWindows class
69 extern const wxChar
*wxCanvasClassName
;
71 // ============================================================================
72 // wxTopLevelWindowMSW implementation
73 // ============================================================================
77 wxDlgProc(HWND
WXUNUSED(hWnd
), UINT message
, WPARAM
WXUNUSED(wParam
), LPARAM
WXUNUSED(lParam
))
79 if ( message
== WM_INITDIALOG
)
81 // for this message, returning TRUE tells system to set focus to the
82 // first control in the dialog box
87 // for all the other ones, FALSE means that we didn't process the
93 // ----------------------------------------------------------------------------
94 // wxTopLevelWindowMSW creation
95 // ----------------------------------------------------------------------------
97 void wxTopLevelWindowMSW::Init()
100 m_maximizeOnShow
= FALSE
;
102 // unlike (almost?) all other windows, frames are created hidden
105 // Data to save/restore when calling ShowFullScreen
107 m_fsOldWindowStyle
= 0;
108 m_fsIsMaximized
= FALSE
;
109 m_fsIsShowing
= FALSE
;
112 WXDWORD
wxTopLevelWindowMSW::MSWGetStyle(long style
, WXDWORD
*exflags
) const
114 // let the base class deal with the common styles but fix the ones which
115 // don't make sense for us (we also deal with the borders ourselves)
116 WXDWORD msflags
= wxWindow::MSWGetStyle
118 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exflags
121 // first select the kind of window being created
123 // note that if we don't set WS_POPUP, Windows assumes WS_OVERLAPPED and
124 // creates a window with both caption and border, hence we also test it
125 // below in some other cases
126 if ( style
& wxFRAME_TOOL_WINDOW
)
129 msflags
|= WS_OVERLAPPED
;
131 // border and caption styles
132 if ( style
& wxRESIZE_BORDER
)
133 msflags
|= WS_THICKFRAME
;
134 else if ( !(style
& wxBORDER_NONE
) )
135 msflags
|= WS_BORDER
;
139 if ( style
& wxCAPTION
)
140 msflags
|= WS_CAPTION
;
144 // next translate the individual flags
145 if ( style
& wxMINIMIZE_BOX
)
146 msflags
|= WS_MINIMIZEBOX
;
147 if ( style
& wxMAXIMIZE_BOX
)
148 msflags
|= WS_MAXIMIZEBOX
;
149 if ( style
& wxSYSTEM_MENU
)
150 msflags
|= WS_SYSMENU
;
151 if ( style
& wxMINIMIZE
)
152 msflags
|= WS_MINIMIZE
;
153 if ( style
& wxMAXIMIZE
)
154 msflags
|= WS_MAXIMIZE
;
156 // Keep this here because it saves recoding this function in wxTinyFrame
157 #if wxUSE_ITSY_BITSY && !defined(__WIN32__)
158 if ( style
& wxTINY_CAPTION_VERT
)
159 msflags
|= IBS_VERTCAPTION
;
160 if ( style
& wxTINY_CAPTION_HORIZ
)
161 msflags
|= IBS_HORZCAPTION
;
163 if ( style
& (wxTINY_CAPTION_VERT
| wxTINY_CAPTION_HORIZ
) )
164 msflags
|= WS_CAPTION
;
169 #if !defined(__WIN16__) && !defined(__SC__)
170 if ( !(GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
) )
172 // make all frames appear in the win9x shell taskbar unless
173 // wxFRAME_TOOL_WINDOW or wxFRAME_NO_TASKBAR is given - without
174 // giving them WS_EX_APPWINDOW style, the child (i.e. owned) frames
175 // wouldn't appear in it
176 if ( (style
& wxFRAME_TOOL_WINDOW
) || (style
& wxFRAME_NO_TASKBAR
) )
177 *exflags
|= WS_EX_TOOLWINDOW
;
178 else if ( !(style
& wxFRAME_NO_TASKBAR
) )
179 *exflags
|= WS_EX_APPWINDOW
;
183 if ( style
& wxSTAY_ON_TOP
)
184 *exflags
|= WS_EX_TOPMOST
;
187 if ( GetExtraStyle() & wxFRAME_EX_CONTEXTHELP
)
188 *exflags
|= WS_EX_CONTEXTHELP
;
195 bool wxTopLevelWindowMSW::CreateDialog(const void *dlgTemplate
,
196 const wxString
& title
,
200 #ifdef __WXMICROWIN__
201 // no dialogs support under MicroWin yet
202 return CreateFrame(title
, pos
, size
);
203 #else // !__WXMICROWIN__
204 wxWindow
*parent
= GetParent();
206 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
207 // app window as parent - this avoids creating modal dialogs without
209 if ( !parent
&& !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
) )
211 parent
= wxTheApp
->GetTopWindow();
215 // don't use transient windows as parents, this is dangerous as it
216 // can lead to a crash if the parent is destroyed before the child
218 // also don't use the window which is currently hidden as then the
219 // dialog would be hidden as well
220 if ( (parent
->GetExtraStyle() & wxWS_EX_TRANSIENT
) ||
228 m_hWnd
= (WXHWND
)::CreateDialogIndirect
231 (DLGTEMPLATE
*)dlgTemplate
,
232 parent
? GetHwndOf(parent
) : NULL
,
238 wxFAIL_MSG(_("Failed to create dialog. Incorrect DLGTEMPLATE?"));
240 wxLogSysError(_("Can't create dialog using memory template"));
246 (void)MSWGetCreateWindowFlags(&exflags
);
250 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exflags
);
251 ::SetWindowPos(GetHwnd(), NULL
, 0, 0, 0, 0,
258 #if defined(__WIN95__)
259 // For some reason, the system menu is activated when we use the
260 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
261 if ( exflags
& WS_EX_CONTEXTHELP
)
263 wxFrame
*winTop
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
266 wxIcon icon
= winTop
->GetIcon();
269 ::SendMessage(GetHwnd(), WM_SETICON
,
271 (LPARAM
)GetHiconOf(icon
));
277 // move the dialog to its initial position without forcing repainting
279 if ( !MSWGetCreateWindowCoords(pos
, size
, x
, y
, w
, h
) )
282 w
= (int)CW_USEDEFAULT
;
285 // we can't use CW_USEDEFAULT here as we're not calling CreateWindow()
286 // and passing CW_USEDEFAULT to MoveWindow() results in resizing the
287 // window to (0, 0) size which breaks quite a lot of things, e.g. the
288 // sizer calculation in wxSizer::Fit()
289 if ( w
== (int)CW_USEDEFAULT
)
291 // the exact number doesn't matter, the dialog will be resized
292 // again soon anyhow but it should be big enough to allow
293 // calculation relying on "totalSize - clientSize > 0" work, i.e.
294 // at least greater than the title bar height
299 if ( x
== (int)CW_USEDEFAULT
)
301 // centre it on the screen - what else can we do?
302 wxSize sizeDpy
= wxGetDisplaySize();
304 x
= (sizeDpy
.x
- w
) / 2;
305 y
= (sizeDpy
.y
- h
) / 2;
308 if ( !::MoveWindow(GetHwnd(), x
, y
, w
, h
, FALSE
) )
310 wxLogLastError(wxT("MoveWindow"));
313 if ( !title
.empty() )
315 ::SetWindowText(GetHwnd(), title
);
321 #endif // __WXMICROWIN__/!__WXMICROWIN__
324 bool wxTopLevelWindowMSW::CreateFrame(const wxString
& title
,
329 WXDWORD flags
= MSWGetCreateWindowFlags(&exflags
);
331 return MSWCreate(wxCanvasClassName
, title
, pos
, size
, flags
, exflags
);
334 bool wxTopLevelWindowMSW::Create(wxWindow
*parent
,
336 const wxString
& title
,
340 const wxString
& name
)
345 m_windowStyle
= style
;
349 m_windowId
= id
== -1 ? NewControlId() : id
;
351 wxTopLevelWindows
.Append(this);
354 parent
->AddChild(this);
356 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
358 // we have different dialog templates to allows creation of dialogs
359 // with & without captions under MSWindows, resizeable or not (but a
360 // resizeable dialog always has caption - otherwise it would look too
363 // we need 3 additional WORDs for dialog menu, class and title (as we
364 // don't use DS_SETFONT we don't need the fourth WORD for the font)
365 static const int dlgsize
= sizeof(DLGTEMPLATE
) + (sizeof(WORD
) * 3);
366 DLGTEMPLATE
*dlgTemplate
= (DLGTEMPLATE
*)malloc(dlgsize
);
367 memset(dlgTemplate
, 0, dlgsize
);
369 // these values are arbitrary, they won't be used normally anyhow
372 dlgTemplate
->cx
= 144;
373 dlgTemplate
->cy
= 75;
375 // reuse the code in MSWGetStyle() but correct the results slightly for
377 dlgTemplate
->style
= MSWGetStyle(style
, NULL
);
379 // all dialogs are popups
380 dlgTemplate
->style
|= WS_POPUP
;
382 // force 3D-look if necessary, it looks impossibly ugly otherwise
383 if ( style
& (wxRESIZE_BORDER
| wxCAPTION
) )
384 dlgTemplate
->style
|= DS_MODALFRAME
;
386 bool ret
= CreateDialog(dlgTemplate
, title
, pos
, size
);
393 return CreateFrame(title
, pos
, size
);
397 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
399 wxTopLevelWindows
.DeleteObject(this);
401 if ( wxModelessWindows
.Find(this) )
402 wxModelessWindows
.DeleteObject(this);
404 // If this is the last top-level window, exit.
405 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
407 wxTheApp
->SetTopWindow(NULL
);
409 if ( wxTheApp
->GetExitOnFrameDelete() )
411 ::PostQuitMessage(0);
416 // ----------------------------------------------------------------------------
417 // wxTopLevelWindowMSW showing
418 // ----------------------------------------------------------------------------
420 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd
)
422 ::ShowWindow(GetHwnd(), nShowCmd
);
424 m_iconized
= nShowCmd
== SW_MINIMIZE
;
427 bool wxTopLevelWindowMSW::Show(bool show
)
429 // don't use wxWindow version as we want to call DoShowWindow() ourselves
430 if ( !wxWindowBase::Show(show
) )
436 if ( m_maximizeOnShow
)
439 nShowCmd
= SW_MAXIMIZE
;
441 m_maximizeOnShow
= FALSE
;
453 DoShowWindow(nShowCmd
);
457 ::BringWindowToTop(GetHwnd());
459 wxActivateEvent
event(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
460 event
.SetEventObject( this );
461 GetEventHandler()->ProcessEvent(event
);
465 // Try to highlight the correct window (the parent)
468 HWND hWndParent
= GetHwndOf(GetParent());
470 ::BringWindowToTop(hWndParent
);
477 // ----------------------------------------------------------------------------
478 // wxTopLevelWindowMSW maximize/minimize
479 // ----------------------------------------------------------------------------
481 void wxTopLevelWindowMSW::Maximize(bool maximize
)
485 // just maximize it directly
486 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
490 // we can't maximize the hidden frame because it shows it as well, so
491 // just remember that we should do it later in this case
492 m_maximizeOnShow
= TRUE
;
496 bool wxTopLevelWindowMSW::IsMaximized() const
498 return ::IsZoomed(GetHwnd()) != 0;
501 void wxTopLevelWindowMSW::Iconize(bool iconize
)
503 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
506 bool wxTopLevelWindowMSW::IsIconized() const
508 // also update the current state
509 ((wxTopLevelWindowMSW
*)this)->m_iconized
= ::IsIconic(GetHwnd()) != 0;
514 void wxTopLevelWindowMSW::Restore()
516 DoShowWindow(SW_RESTORE
);
519 // ----------------------------------------------------------------------------
520 // wxTopLevelWindowMSW fullscreen
521 // ----------------------------------------------------------------------------
523 bool wxTopLevelWindowMSW::ShowFullScreen(bool show
, long style
)
530 m_fsIsShowing
= TRUE
;
533 // zap the frame borders
535 // save the 'normal' window style
536 m_fsOldWindowStyle
= GetWindowLong((HWND
)GetHWND(), GWL_STYLE
);
538 // save the old position, width & height, maximize state
539 m_fsOldSize
= GetRect();
540 m_fsIsMaximized
= IsMaximized();
542 // decide which window style flags to turn off
543 LONG newStyle
= m_fsOldWindowStyle
;
546 if (style
& wxFULLSCREEN_NOBORDER
)
547 offFlags
|= WS_BORDER
| WS_THICKFRAME
;
548 if (style
& wxFULLSCREEN_NOCAPTION
)
549 offFlags
|= (WS_CAPTION
| WS_SYSMENU
);
551 newStyle
&= (~offFlags
);
553 // change our window style to be compatible with full-screen mode
554 ::SetWindowLong((HWND
)GetHWND(), GWL_STYLE
, newStyle
);
556 // resize to the size of the desktop
559 RECT rect
= wxGetWindowRect(::GetDesktopWindow());
560 width
= rect
.right
- rect
.left
;
561 height
= rect
.bottom
- rect
.top
;
563 SetSize(width
, height
);
565 // now flush the window style cache and actually go full-screen
566 SetWindowPos((HWND
)GetHWND(), HWND_TOP
, 0, 0, width
, height
, SWP_FRAMECHANGED
);
568 wxSizeEvent
event(wxSize(width
, height
), GetId());
569 GetEventHandler()->ProcessEvent(event
);
578 m_fsIsShowing
= FALSE
;
580 Maximize(m_fsIsMaximized
);
581 SetWindowLong((HWND
)GetHWND(),GWL_STYLE
, m_fsOldWindowStyle
);
582 SetWindowPos((HWND
)GetHWND(),HWND_TOP
,m_fsOldSize
.x
, m_fsOldSize
.y
,
583 m_fsOldSize
.width
, m_fsOldSize
.height
, SWP_FRAMECHANGED
);
589 // ----------------------------------------------------------------------------
590 // wxTopLevelWindowMSW misc
591 // ----------------------------------------------------------------------------
593 void wxTopLevelWindowMSW::SetIcon(const wxIcon
& icon
)
595 SetIcons( wxIconBundle( icon
) );
598 void wxTopLevelWindowMSW::SetIcons(const wxIconBundle
& icons
)
600 wxTopLevelWindowBase::SetIcons(icons
);
602 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
603 const wxIcon
& sml
= icons
.GetIcon( wxSize( 16, 16 ) );
604 if( sml
.Ok() && sml
.GetWidth() == 16 && sml
.GetHeight() == 16 )
606 ::SendMessage( GetHwndOf( this ), WM_SETICON
, ICON_SMALL
,
607 (LPARAM
)GetHiconOf(sml
) );
610 const wxIcon
& big
= icons
.GetIcon( wxSize( 32, 32 ) );
611 if( big
.Ok() && big
.GetWidth() == 32 && big
.GetHeight() == 32 )
613 ::SendMessage( GetHwndOf( this ), WM_SETICON
, ICON_BIG
,
614 (LPARAM
)GetHiconOf(big
) );
619 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable
)
621 #ifndef __WXMICROWIN__
622 // get system (a.k.a. window) menu
623 HMENU hmenu
= ::GetSystemMenu(GetHwnd(), FALSE
/* get it */);
626 wxLogLastError(_T("GetSystemMenu"));
631 // enabling/disabling the close item from it also automatically
632 // disables/enables the close title bar button
633 if ( ::EnableMenuItem(hmenu
, SC_CLOSE
,
635 (enable
? MF_ENABLED
: MF_GRAYED
)) == -1 )
637 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
642 // update appearance immediately
643 if ( !::DrawMenuBar(GetHwnd()) )
645 wxLogLastError(_T("DrawMenuBar"));
647 #endif // !__WXMICROWIN__