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 // make all frames appear in the win9x shell taskbar unless
156 // wxFRAME_TOOL_WINDOW or wxFRAME_NO_TASKBAR is given - without giving
157 // them WS_EX_APPWINDOW style, the child (i.e. owned) frames wouldn't
159 #if !defined(__WIN16__) && !defined(__SC__)
160 if ( (style
& wxFRAME_TOOL_WINDOW
) || (style
& wxFRAME_NO_TASKBAR
) )
161 *exflags
|= WS_EX_TOOLWINDOW
;
162 else if ( !(style
& wxFRAME_NO_TASKBAR
) )
163 *exflags
|= WS_EX_APPWINDOW
;
166 if ( style
& wxSTAY_ON_TOP
)
167 *exflags
|= WS_EX_TOPMOST
;
170 if ( m_exStyle
& wxFRAME_EX_CONTEXTHELP
)
171 *exflags
|= WS_EX_CONTEXTHELP
;
178 bool wxTopLevelWindowMSW::CreateDialog(const wxChar
*dlgTemplate
,
179 const wxString
& title
,
183 #ifdef __WXMICROWIN__
184 // no dialogs support under MicroWin yet
185 return CreateFrame(title
, pos
, size
);
186 #else // !__WXMICROWIN__
187 wxWindow
*parent
= GetParent();
189 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
190 // app window as parent - this avoids creating modal dialogs without
192 if ( !parent
&& !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
) )
194 parent
= wxTheApp
->GetTopWindow();
197 m_hWnd
= (WXHWND
)::CreateDialog(wxGetInstance(),
199 parent
? GetHwndOf(parent
) : NULL
,
204 wxFAIL_MSG(_("Did you forget to include wx/msw/wx.rc in your resources?"));
206 wxLogSysError(_("Can't create dialog using template '%s'"), dlgTemplate
);
212 (void)MSWGetCreateWindowFlags(&exflags
);
216 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exflags
);
217 ::SetWindowPos(GetHwnd(), NULL
, 0, 0, 0, 0,
224 #if defined(__WIN95__)
225 // For some reason, the system menu is activated when we use the
226 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
227 if ( exflags
& WS_EX_CONTEXTHELP
)
229 wxFrame
*winTop
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
232 wxIcon icon
= winTop
->GetIcon();
235 ::SendMessage(GetHwnd(), WM_SETICON
,
237 (LPARAM
)GetHiconOf(icon
));
243 // move the dialog to its initial position without forcing repainting
245 if ( MSWGetCreateWindowCoords(pos
, size
, x
, y
, w
, h
) )
247 if ( !::MoveWindow(GetHwnd(), x
, y
, w
, h
, FALSE
) )
249 wxLogLastError(wxT("MoveWindow"));
252 //else: leave it at default position
254 if ( !title
.empty() )
256 ::SetWindowText(GetHwnd(), title
);
262 #endif // __WXMICROWIN__/!__WXMICROWIN__
265 bool wxTopLevelWindowMSW::CreateFrame(const wxString
& title
,
270 long flags
= MSWGetCreateWindowFlags(&exflags
);
272 return MSWCreate(wxCanvasClassName
, title
, pos
, size
, flags
, exflags
);
275 bool wxTopLevelWindowMSW::Create(wxWindow
*parent
,
277 const wxString
& title
,
281 const wxString
& name
)
286 m_windowStyle
= style
;
290 m_windowId
= id
== -1 ? NewControlId() : id
;
292 wxTopLevelWindows
.Append(this);
295 parent
->AddChild(this);
297 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
299 // TODO: it would be better to construct the dialog template in memory
300 // during run-time than to rely on the limited number of
301 // templates in wx.rc because:
302 // a) you wouldn't have to include wx.rc in all wxWin programs
303 // (and the number of complaints about it would dtop)
304 // b) we'd be able to provide more templates simply, i.e.
305 // we could generate the templates for all style
308 // we have different dialog templates to allows creation of dialogs
309 // with & without captions under MSWindows, resizeable or not (but a
310 // resizeable dialog always has caption - otherwise it would look too
312 const wxChar
*dlgTemplate
;
313 if ( style
& wxRESIZE_BORDER
)
314 dlgTemplate
= wxT("wxResizeableDialog");
315 else if ( style
& wxCAPTION
)
316 dlgTemplate
= wxT("wxCaptionDialog");
318 dlgTemplate
= wxT("wxNoCaptionDialog");
320 return CreateDialog(dlgTemplate
, title
, pos
, size
);
324 return CreateFrame(title
, pos
, size
);
328 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
330 wxTopLevelWindows
.DeleteObject(this);
332 if ( wxModelessWindows
.Find(this) )
333 wxModelessWindows
.DeleteObject(this);
335 // If this is the last top-level window, exit.
336 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
338 wxTheApp
->SetTopWindow(NULL
);
340 if ( wxTheApp
->GetExitOnFrameDelete() )
342 ::PostQuitMessage(0);
347 // ----------------------------------------------------------------------------
348 // wxTopLevelWindowMSW geometry
349 // ----------------------------------------------------------------------------
351 void wxTopLevelWindowMSW::DoSetClientSize(int width
, int height
)
353 HWND hWnd
= GetHwnd();
356 ::GetClientRect(hWnd
, &rectClient
);
359 ::GetWindowRect(hWnd
, &rectTotal
);
361 // Find the difference between the entire window (title bar and all)
362 // and the client area; add this to the new client size to move the
364 width
+= rectTotal
.right
- rectTotal
.left
- rectClient
.right
;
365 height
+= rectTotal
.bottom
- rectTotal
.top
- rectClient
.bottom
;
367 // note that calling GetClientAreaOrigin() takes the toolbar into account
368 wxPoint pt
= GetClientAreaOrigin();
372 if ( !::MoveWindow(hWnd
, rectTotal
.left
, rectTotal
.top
,
373 width
, height
, TRUE
/* redraw */) )
375 wxLogLastError(_T("MoveWindow"));
378 wxSizeEvent
event(wxSize(width
, height
), m_windowId
);
379 event
.SetEventObject(this);
380 (void)GetEventHandler()->ProcessEvent(event
);
383 // ----------------------------------------------------------------------------
384 // wxTopLevelWindowMSW showing
385 // ----------------------------------------------------------------------------
387 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd
)
389 ::ShowWindow(GetHwnd(), nShowCmd
);
391 m_iconized
= nShowCmd
== SW_MINIMIZE
;
394 bool wxTopLevelWindowMSW::Show(bool show
)
396 // don't use wxWindow version as we want to call DoShowWindow() ourselves
397 if ( !wxWindowBase::Show(show
) )
403 if ( m_maximizeOnShow
)
406 nShowCmd
= SW_MAXIMIZE
;
408 m_maximizeOnShow
= FALSE
;
420 DoShowWindow(nShowCmd
);
424 ::BringWindowToTop(GetHwnd());
426 wxActivateEvent
event(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
427 event
.SetEventObject( this );
428 GetEventHandler()->ProcessEvent(event
);
432 // Try to highlight the correct window (the parent)
435 HWND hWndParent
= GetHwndOf(GetParent());
437 ::BringWindowToTop(hWndParent
);
444 // ----------------------------------------------------------------------------
445 // wxTopLevelWindowMSW maximize/minimize
446 // ----------------------------------------------------------------------------
448 void wxTopLevelWindowMSW::Maximize(bool maximize
)
452 // just maximize it directly
453 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
457 // we can't maximize the hidden frame because it shows it as well, so
458 // just remember that we should do it later in this case
459 m_maximizeOnShow
= TRUE
;
463 bool wxTopLevelWindowMSW::IsMaximized() const
465 return ::IsZoomed(GetHwnd()) != 0;
468 void wxTopLevelWindowMSW::Iconize(bool iconize
)
470 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
473 bool wxTopLevelWindowMSW::IsIconized() const
475 // also update the current state
476 ((wxTopLevelWindowMSW
*)this)->m_iconized
= ::IsIconic(GetHwnd()) != 0;
481 void wxTopLevelWindowMSW::Restore()
483 DoShowWindow(SW_RESTORE
);
486 // ----------------------------------------------------------------------------
487 // wxTopLevelWindowMSW fullscreen
488 // ----------------------------------------------------------------------------
490 bool wxTopLevelWindowMSW::ShowFullScreen(bool show
, long style
)
497 m_fsIsShowing
= TRUE
;
500 // zap the frame borders
502 // save the 'normal' window style
503 m_fsOldWindowStyle
= GetWindowLong((HWND
)GetHWND(), GWL_STYLE
);
505 // save the old position, width & height, maximize state
506 m_fsOldSize
= GetRect();
507 m_fsIsMaximized
= IsMaximized();
509 // decide which window style flags to turn off
510 LONG newStyle
= m_fsOldWindowStyle
;
513 if (style
& wxFULLSCREEN_NOBORDER
)
514 offFlags
|= WS_BORDER
| WS_THICKFRAME
;
515 if (style
& wxFULLSCREEN_NOCAPTION
)
516 offFlags
|= (WS_CAPTION
| WS_SYSMENU
);
518 newStyle
&= (~offFlags
);
520 // change our window style to be compatible with full-screen mode
521 ::SetWindowLong((HWND
)GetHWND(), GWL_STYLE
, newStyle
);
523 // resize to the size of the desktop
526 RECT rect
= wxGetWindowRect(::GetDesktopWindow());
527 width
= rect
.right
- rect
.left
;
528 height
= rect
.bottom
- rect
.top
;
530 SetSize(width
, height
);
532 // now flush the window style cache and actually go full-screen
533 SetWindowPos((HWND
)GetHWND(), HWND_TOP
, 0, 0, width
, height
, SWP_FRAMECHANGED
);
535 wxSizeEvent
event(wxSize(width
, height
), GetId());
536 GetEventHandler()->ProcessEvent(event
);
545 m_fsIsShowing
= FALSE
;
547 Maximize(m_fsIsMaximized
);
548 SetWindowLong((HWND
)GetHWND(),GWL_STYLE
, m_fsOldWindowStyle
);
549 SetWindowPos((HWND
)GetHWND(),HWND_TOP
,m_fsOldSize
.x
, m_fsOldSize
.y
,
550 m_fsOldSize
.width
, m_fsOldSize
.height
, SWP_FRAMECHANGED
);
556 // ----------------------------------------------------------------------------
557 // wxTopLevelWindowMSW misc
558 // ----------------------------------------------------------------------------
560 void wxTopLevelWindowMSW::SetIcon(const wxIcon
& icon
)
563 wxTopLevelWindowBase::SetIcon(icon
);
565 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
568 ::SendMessage(GetHwnd(), WM_SETICON
,
569 (WPARAM
)TRUE
, (LPARAM
)GetHiconOf(m_icon
));