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"
39 #include "wx/msw/private.h"
41 // ----------------------------------------------------------------------------
42 // stubs for missing functions under MicroWindows
43 // ----------------------------------------------------------------------------
47 static inline bool IsIconic(HWND
WXUNUSED(hwnd
)) { return FALSE
; }
48 static inline bool IsZoomed(HWND
WXUNUSED(hwnd
)) { return FALSE
; }
50 #endif // __WXMICROWIN__
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // list of all frames and modeless dialogs
57 wxWindowList wxModelessWindows
;
59 // the name of the default wxWindows class
60 extern const wxChar
*wxCanvasClassName
;
62 // ============================================================================
63 // wxTopLevelWindowMSW implementation
64 // ============================================================================
68 wxDlgProc(HWND
WXUNUSED(hWnd
), UINT message
, WPARAM
WXUNUSED(wParam
), LPARAM
WXUNUSED(lParam
))
70 if ( message
== WM_INITDIALOG
)
72 // for this message, returning TRUE tells system to set focus to the
73 // first control in the dialog box
78 // for all the other ones, FALSE means that we didn't process the
84 // ----------------------------------------------------------------------------
85 // wxTopLevelWindowMSW creation
86 // ----------------------------------------------------------------------------
88 void wxTopLevelWindowMSW::Init()
91 m_maximizeOnShow
= FALSE
;
93 // unlike (almost?) all other windows, frames are created hidden
97 long wxTopLevelWindowMSW::MSWGetCreateWindowFlags(long *exflags
) const
99 long style
= GetWindowStyle();
102 // first select the kind of window being created
103 if ( style
& wxCAPTION
)
105 if ( style
& wxFRAME_TOOL_WINDOW
)
106 msflags
|= WS_POPUPWINDOW
;
108 msflags
|= WS_OVERLAPPED
;
115 // next translate the individual flags
116 if ( style
& wxMINIMIZE_BOX
)
117 msflags
|= WS_MINIMIZEBOX
;
118 if ( style
& wxMAXIMIZE_BOX
)
119 msflags
|= WS_MAXIMIZEBOX
;
120 if ( style
& wxTHICK_FRAME
)
121 msflags
|= WS_THICKFRAME
;
122 if ( style
& wxSYSTEM_MENU
)
123 msflags
|= WS_SYSMENU
;
124 if ( style
& wxMINIMIZE
)
125 msflags
|= WS_MINIMIZE
;
126 if ( style
& wxMAXIMIZE
)
127 msflags
|= WS_MAXIMIZE
;
128 if ( style
& wxCAPTION
)
129 msflags
|= WS_CAPTION
;
130 if ( style
& wxCLIP_CHILDREN
)
131 msflags
|= WS_CLIPCHILDREN
;
133 // Keep this here because it saves recoding this function in wxTinyFrame
134 #if wxUSE_ITSY_BITSY && !defined(__WIN32__)
135 if ( style
& wxTINY_CAPTION_VERT
)
136 msflags
|= IBS_VERTCAPTION
;
137 if ( style
& wxTINY_CAPTION_HORIZ
)
138 msflags
|= IBS_HORZCAPTION
;
140 if ( style
& (wxTINY_CAPTION_VERT
| wxTINY_CAPTION_HORIZ
) )
141 msflags
|= WS_CAPTION
;
146 *exflags
= MakeExtendedStyle(style
);
148 // make all frames appear in the win9x shell taskbar unless
149 // wxFRAME_TOOL_WINDOW or wxFRAME_NO_TASKBAR is given - without giving
150 // them WS_EX_APPWINDOW style, the child (i.e. owned) frames wouldn't
152 #if !defined(__WIN16__) && !defined(__SC__)
153 if ( (style
& wxFRAME_TOOL_WINDOW
) || (style
& wxFRAME_NO_TASKBAR
) )
154 *exflags
|= WS_EX_TOOLWINDOW
;
155 else if ( !(style
& wxFRAME_NO_TASKBAR
) )
156 *exflags
|= WS_EX_APPWINDOW
;
159 if ( style
& wxSTAY_ON_TOP
)
160 *exflags
|= WS_EX_TOPMOST
;
163 if ( m_exStyle
& wxFRAME_EX_CONTEXTHELP
)
164 *exflags
|= WS_EX_CONTEXTHELP
;
171 bool wxTopLevelWindowMSW::CreateDialog(const wxChar
*dlgTemplate
,
172 const wxString
& title
,
176 #ifdef __WXMICROWIN__
177 // no dialogs support under MicroWin yet
178 return CreateFrame(title
, pos
, size
);
179 #else // !__WXMICROWIN__
180 wxWindow
*parent
= GetParent();
182 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
183 // app window as parent - this avoids creating modal dialogs without
185 if ( !parent
&& !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
) )
187 parent
= wxTheApp
->GetTopWindow();
190 m_hWnd
= (WXHWND
)::CreateDialog(wxGetInstance(),
192 parent
? GetHwndOf(parent
) : NULL
,
197 wxFAIL_MSG(_("Did you forget to include wx/msw/wx.rc in your resources?"));
199 wxLogSysError(_("Can't create dialog using template '%s'"), dlgTemplate
);
205 (void)MSWGetCreateWindowFlags(&exflags
);
209 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exflags
);
210 ::SetWindowPos(GetHwnd(), NULL
, 0, 0, 0, 0,
217 #if defined(__WIN95__)
218 // For some reason, the system menu is activated when we use the
219 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
220 if ( exflags
& WS_EX_CONTEXTHELP
)
222 wxFrame
*winTop
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
225 wxIcon icon
= winTop
->GetIcon();
228 ::SendMessage(GetHwnd(), WM_SETICON
,
230 (LPARAM
)GetHiconOf(icon
));
236 // move the dialog to its initial position without forcing repainting
238 if ( MSWGetCreateWindowCoords(pos
, size
, x
, y
, w
, h
) )
240 if ( !::MoveWindow(GetHwnd(), x
, y
, w
, h
, FALSE
) )
242 wxLogLastError(wxT("MoveWindow"));
245 //else: leave it at default position
247 if ( !title
.empty() )
249 ::SetWindowText(GetHwnd(), title
);
255 #endif // __WXMICROWIN__/!__WXMICROWIN__
258 bool wxTopLevelWindowMSW::CreateFrame(const wxString
& title
,
263 long flags
= MSWGetCreateWindowFlags(&exflags
);
265 return MSWCreate(wxCanvasClassName
, title
, pos
, size
, flags
, exflags
);
268 bool wxTopLevelWindowMSW::Create(wxWindow
*parent
,
270 const wxString
& title
,
274 const wxString
& name
)
279 m_windowStyle
= style
;
283 m_windowId
= id
== -1 ? NewControlId() : id
;
285 wxTopLevelWindows
.Append(this);
288 parent
->AddChild(this);
290 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
292 // TODO: it would be better to construct the dialog template in memory
293 // during run-time than to rely on the limited number of
294 // templates in wx.rc because:
295 // a) you wouldn't have to include wx.rc in all wxWin programs
296 // (and the number of complaints about it would dtop)
297 // b) we'd be able to provide more templates simply, i.e.
298 // we could generate the templates for all style
301 // we have different dialog templates to allows creation of dialogs
302 // with & without captions under MSWindows, resizeable or not (but a
303 // resizeable dialog always has caption - otherwise it would look too
305 const wxChar
*dlgTemplate
;
306 if ( style
& wxRESIZE_BORDER
)
307 dlgTemplate
= wxT("wxResizeableDialog");
308 else if ( style
& wxCAPTION
)
309 dlgTemplate
= wxT("wxCaptionDialog");
311 dlgTemplate
= wxT("wxNoCaptionDialog");
313 return CreateDialog(dlgTemplate
, title
, pos
, size
);
317 return CreateFrame(title
, pos
, size
);
321 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
323 wxTopLevelWindows
.DeleteObject(this);
325 if ( wxModelessWindows
.Find(this) )
326 wxModelessWindows
.DeleteObject(this);
328 // If this is the last top-level window, exit.
329 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
331 wxTheApp
->SetTopWindow(NULL
);
333 if ( wxTheApp
->GetExitOnFrameDelete() )
335 ::PostQuitMessage(0);
340 // ----------------------------------------------------------------------------
341 // wxTopLevelWindowMSW geometry
342 // ----------------------------------------------------------------------------
344 void wxTopLevelWindowMSW::DoSetClientSize(int width
, int height
)
346 HWND hWnd
= GetHwnd();
349 ::GetClientRect(hWnd
, &rectClient
);
352 ::GetWindowRect(hWnd
, &rectTotal
);
354 // Find the difference between the entire window (title bar and all)
355 // and the client area; add this to the new client size to move the
357 width
+= rectTotal
.right
- rectTotal
.left
- rectClient
.right
;
358 height
+= rectTotal
.bottom
- rectTotal
.top
- rectClient
.bottom
;
360 // note that calling GetClientAreaOrigin() takes the toolbar into account
361 wxPoint pt
= GetClientAreaOrigin();
365 if ( !::MoveWindow(hWnd
, rectTotal
.left
, rectTotal
.top
,
366 width
, height
, TRUE
/* redraw */) )
368 wxLogLastError(_T("MoveWindow"));
371 wxSizeEvent
event(wxSize(width
, height
), m_windowId
);
372 event
.SetEventObject(this);
373 (void)GetEventHandler()->ProcessEvent(event
);
376 // ----------------------------------------------------------------------------
377 // wxTopLevelWindowMSW showing
378 // ----------------------------------------------------------------------------
380 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd
)
382 ::ShowWindow(GetHwnd(), nShowCmd
);
384 m_iconized
= nShowCmd
== SW_MINIMIZE
;
387 bool wxTopLevelWindowMSW::Show(bool show
)
389 // don't use wxWindow version as we want to call DoShowWindow() ourselves
390 if ( !wxWindowBase::Show(show
) )
396 if ( m_maximizeOnShow
)
399 nShowCmd
= SW_MAXIMIZE
;
401 m_maximizeOnShow
= FALSE
;
413 DoShowWindow(nShowCmd
);
417 ::BringWindowToTop(GetHwnd());
419 wxActivateEvent
event(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
420 event
.SetEventObject( this );
421 GetEventHandler()->ProcessEvent(event
);
425 // Try to highlight the correct window (the parent)
428 HWND hWndParent
= GetHwndOf(GetParent());
430 ::BringWindowToTop(hWndParent
);
437 // ----------------------------------------------------------------------------
438 // wxTopLevelWindowMSW maximize/minimize
439 // ----------------------------------------------------------------------------
441 void wxTopLevelWindowMSW::Maximize(bool maximize
)
445 // just maximize it directly
446 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
450 // we can't maximize the hidden frame because it shows it as well, so
451 // just remember that we should do it later in this case
452 m_maximizeOnShow
= TRUE
;
456 bool wxTopLevelWindowMSW::IsMaximized() const
458 return ::IsZoomed(GetHwnd()) != 0;
461 void wxTopLevelWindowMSW::Iconize(bool iconize
)
463 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
466 bool wxTopLevelWindowMSW::IsIconized() const
468 // also update the current state
469 ((wxTopLevelWindowMSW
*)this)->m_iconized
= ::IsIconic(GetHwnd()) != 0;
474 void wxTopLevelWindowMSW::Restore()
476 DoShowWindow(SW_RESTORE
);
479 // ----------------------------------------------------------------------------
480 // wxTopLevelWindowMSW misc
481 // ----------------------------------------------------------------------------
483 void wxTopLevelWindowMSW::SetIcon(const wxIcon
& icon
)
486 wxTopLevelWindowBase::SetIcon(icon
);
488 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
491 ::SendMessage(GetHwnd(), WM_SETICON
,
492 (WPARAM
)TRUE
, (LPARAM
)GetHiconOf(m_icon
));