1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for MSW
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/toplevel.h"
31 #include "wx/dialog.h"
32 #include "wx/string.h"
36 #include "wx/containr.h" // wxSetFocusToChild()
37 #include "wx/module.h"
40 #include "wx/dynlib.h"
42 #include "wx/msw/private.h"
43 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__)
46 // Standard SDK doesn't have aygshell.dll: see include/wx/msw/wince/libraries.h
47 #if _WIN32_WCE < 400 || !defined(__WINCE_STANDARDSDK__)
52 #include "wx/msw/winundef.h"
53 #include "wx/msw/missing.h"
55 #include "wx/display.h"
65 // ----------------------------------------------------------------------------
66 // stubs for missing functions under MicroWindows
67 // ----------------------------------------------------------------------------
71 // static inline bool IsIconic(HWND WXUNUSED(hwnd)) { return false; }
72 static inline bool IsZoomed(HWND
WXUNUSED(hwnd
)) { return false; }
74 #endif // __WXMICROWIN__
76 // NB: wxDlgProc must be defined here and not in dialog.cpp because the latter
77 // is not included by wxUniv build which does need wxDlgProc
79 wxDlgProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
);
81 // ----------------------------------------------------------------------------
82 // wxTLWHiddenParentModule: used to manage the hidden parent window (we need a
83 // module to ensure that the window is always deleted)
84 // ----------------------------------------------------------------------------
86 class wxTLWHiddenParentModule
: public wxModule
89 // module init/finalize
90 virtual bool OnInit();
91 virtual void OnExit();
93 // get the hidden window (creates on demand)
94 static HWND
GetHWND();
97 // the HWND of the hidden parent
100 // the class used to create it
101 static const wxChar
*ms_className
;
103 DECLARE_DYNAMIC_CLASS(wxTLWHiddenParentModule
)
106 IMPLEMENT_DYNAMIC_CLASS(wxTLWHiddenParentModule
, wxModule
)
108 // ============================================================================
109 // wxTopLevelWindowMSW implementation
110 // ============================================================================
112 BEGIN_EVENT_TABLE(wxTopLevelWindowMSW
, wxTopLevelWindowBase
)
113 EVT_ACTIVATE(wxTopLevelWindowMSW::OnActivate
)
116 // ----------------------------------------------------------------------------
117 // wxTopLevelWindowMSW creation
118 // ----------------------------------------------------------------------------
120 void wxTopLevelWindowMSW::Init()
123 m_maximizeOnShow
= false;
125 // Data to save/restore when calling ShowFullScreen
127 m_fsOldWindowStyle
= 0;
128 m_fsIsMaximized
= false;
129 m_fsIsShowing
= false;
131 m_winLastFocused
= NULL
;
133 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
137 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
138 SHACTIVATEINFO
* info
= new SHACTIVATEINFO
;
140 info
->cbSize
= sizeof(SHACTIVATEINFO
);
142 m_activateInfo
= (void*) info
;
146 WXDWORD
wxTopLevelWindowMSW::MSWGetStyle(long style
, WXDWORD
*exflags
) const
148 // let the base class deal with the common styles but fix the ones which
149 // don't make sense for us (we also deal with the borders ourselves)
150 WXDWORD msflags
= wxWindow::MSWGetStyle
152 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exflags
153 ) & ~WS_CHILD
& ~WS_VISIBLE
;
155 // For some reason, WS_VISIBLE needs to be defined on creation for
156 // SmartPhone 2003. The title can fail to be displayed otherwise.
157 #if defined(__SMARTPHONE__) || (defined(__WXWINCE__) && _WIN32_WCE < 400)
158 msflags
|= WS_VISIBLE
;
159 ((wxTopLevelWindowMSW
*)this)->wxWindowBase::Show(true);
162 // first select the kind of window being created
164 // note that if we don't set WS_POPUP, Windows assumes WS_OVERLAPPED and
165 // creates a window with both caption and border, hence we need to use
166 // WS_POPUP in a few cases just to avoid having caption/border which we
169 // border and caption styles
170 if ( ( style
& wxRESIZE_BORDER
) && !IsAlwaysMaximized())
171 msflags
|= WS_THICKFRAME
;
172 else if ( exflags
&& ((style
& wxBORDER_DOUBLE
) || (style
& wxBORDER_RAISED
)) )
173 *exflags
|= WS_EX_DLGMODALFRAME
;
174 else if ( !(style
& wxBORDER_NONE
) )
175 msflags
|= WS_BORDER
;
181 // normally we consider that all windows without a caption must be popups,
182 // but CE is an exception: there windows normally do not have the caption
183 // but shouldn't be made popups as popups can't have menus and don't look
184 // like normal windows anyhow
186 // TODO: Smartphone appears to like wxCAPTION, but we should check that
188 #if defined(__SMARTPHONE__) || !defined(__WXWINCE__)
189 if ( style
& wxCAPTION
)
190 msflags
|= WS_CAPTION
;
194 #endif // !__WXWINCE__
197 // next translate the individual flags
199 // WS_EX_CONTEXTHELP is incompatible with WS_MINIMIZEBOX and WS_MAXIMIZEBOX
200 // and is ignored if we specify both of them, but chances are that if we
201 // use wxWS_EX_CONTEXTHELP, we really do want to have the context help
202 // button while wxMINIMIZE/wxMAXIMIZE are included by default, so the help
204 if ( !(GetExtraStyle() & wxWS_EX_CONTEXTHELP
) )
206 if ( style
& wxMINIMIZE_BOX
)
207 msflags
|= WS_MINIMIZEBOX
;
208 if ( style
& wxMAXIMIZE_BOX
)
209 msflags
|= WS_MAXIMIZEBOX
;
213 // notice that if wxCLOSE_BOX is specified we need to use WS_SYSMENU too as
214 // otherwise the close box doesn't appear
215 if ( style
& (wxSYSTEM_MENU
| wxCLOSE_BOX
) )
216 msflags
|= WS_SYSMENU
;
217 #endif // !__WXWINCE__
219 // NB: under CE these 2 styles are not supported currently, we should
220 // call Minimize()/Maximize() "manually" if we want to support them
221 if ( style
& wxMINIMIZE
)
222 msflags
|= WS_MINIMIZE
;
224 if ( style
& wxMAXIMIZE
)
225 msflags
|= WS_MAXIMIZE
;
227 // Keep this here because it saves recoding this function in wxTinyFrame
228 if ( style
& wxTINY_CAPTION
)
229 msflags
|= WS_CAPTION
;
233 // there is no taskbar under CE, so omit all this
234 #if !defined(__WXWINCE__)
235 if ( !(GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
) )
237 if ( style
& wxFRAME_TOOL_WINDOW
)
239 // create the palette-like window
240 *exflags
|= WS_EX_TOOLWINDOW
;
242 // tool windows shouldn't appear on the taskbar (as documented)
243 style
|= wxFRAME_NO_TASKBAR
;
246 // We have to solve 2 different problems here:
248 // 1. frames with wxFRAME_NO_TASKBAR flag shouldn't appear in the
249 // taskbar even if they don't have a parent
251 // 2. frames without this style should appear in the taskbar even
252 // if they're owned (Windows only puts non owned windows into
253 // the taskbar normally)
255 // The second one is solved here by using WS_EX_APPWINDOW flag, the
256 // first one is dealt with in our MSWGetParent() method
258 if ( !(style
& wxFRAME_NO_TASKBAR
) && GetParent() )
260 // need to force the frame to appear in the taskbar
261 *exflags
|= WS_EX_APPWINDOW
;
263 //else: nothing to do [here]
266 if ( GetExtraStyle() & wxWS_EX_CONTEXTHELP
)
267 *exflags
|= WS_EX_CONTEXTHELP
;
268 #endif // !__WXWINCE__
270 if ( style
& wxSTAY_ON_TOP
)
271 *exflags
|= WS_EX_TOPMOST
;
277 WXHWND
wxTopLevelWindowMSW::MSWGetParent() const
279 // for the frames without wxFRAME_FLOAT_ON_PARENT style we should use NULL
280 // parent HWND or it would be always on top of its parent which is not what
281 // we usually want (in fact, we only want it for frames with the
282 // wxFRAME_FLOAT_ON_PARENT flag)
283 HWND hwndParent
= NULL
;
284 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT
) )
286 const wxWindow
*parent
= GetParent();
290 // this flag doesn't make sense then and will be ignored
291 wxFAIL_MSG( wxT("wxFRAME_FLOAT_ON_PARENT but no parent?") );
295 hwndParent
= GetHwndOf(parent
);
298 //else: don't float on parent, must not be owned
300 // now deal with the 2nd taskbar-related problem (see comments above in
302 if ( HasFlag(wxFRAME_NO_TASKBAR
) && !hwndParent
)
305 hwndParent
= wxTLWHiddenParentModule::GetHWND();
308 return (WXHWND
)hwndParent
;
311 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
312 bool wxTopLevelWindowMSW::HandleSettingChange(WXWPARAM wParam
, WXLPARAM lParam
)
314 SHACTIVATEINFO
*info
= (SHACTIVATEINFO
*) m_activateInfo
;
317 SHHandleWMSettingChange(GetHwnd(), wParam
, lParam
, info
);
320 return wxWindowMSW::HandleSettingChange(wParam
, lParam
);
324 WXLRESULT
wxTopLevelWindowMSW::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
327 bool processed
= false;
329 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
334 SHACTIVATEINFO
* info
= (SHACTIVATEINFO
*) m_activateInfo
;
338 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
) flags
= SHA_INPUTDIALOG
;
339 SHHandleWMActivate(GetHwnd(), wParam
, lParam
, info
, flags
);
342 // This implicitly sends a wxEVT_ACTIVATE_APP event
344 wxTheApp
->SetActive(wParam
!= 0, FindFocus());
352 wxActivateEvent
event(wxEVT_HIBERNATE
, true, wxID_ANY
);
353 event
.SetEventObject(wxTheApp
);
354 processed
= wxTheApp
->ProcessEvent(event
);
362 rc
= wxTopLevelWindowBase::MSWWindowProc(message
, wParam
, lParam
);
367 bool wxTopLevelWindowMSW::CreateDialog(const void *dlgTemplate
,
368 const wxString
& title
,
372 #ifdef __WXMICROWIN__
373 // no dialogs support under MicroWin yet
374 return CreateFrame(title
, pos
, size
);
375 #else // !__WXMICROWIN__
376 // static cast is valid as we're only ever called for dialogs
378 parent
= static_cast<wxDialog
*>(this)->GetParentForModalDialog();
380 m_hWnd
= (WXHWND
)::CreateDialogIndirect
383 (DLGTEMPLATE
*)dlgTemplate
,
384 parent
? GetHwndOf(parent
) : NULL
,
390 wxFAIL_MSG(wxT("Failed to create dialog. Incorrect DLGTEMPLATE?"));
392 wxLogSysError(wxT("Can't create dialog using memory template"));
397 #if !defined(__WXWINCE__)
398 // For some reason, the system menu is activated when we use the
399 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
400 if ( HasExtraStyle(wxWS_EX_CONTEXTHELP
) )
402 wxFrame
*winTop
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
405 wxIcon icon
= winTop
->GetIcon();
408 ::SendMessage(GetHwnd(), WM_SETICON
,
410 (LPARAM
)GetHiconOf(icon
));
414 #endif // !__WXWINCE__
416 // move the dialog to its initial position without forcing repainting
418 (void)MSWGetCreateWindowCoords(pos
, size
, x
, y
, w
, h
);
420 if ( x
== (int)CW_USEDEFAULT
)
422 // centre it on the screen - what else can we do?
423 wxSize sizeDpy
= wxGetDisplaySize();
425 x
= (sizeDpy
.x
- w
) / 2;
426 y
= (sizeDpy
.y
- h
) / 2;
429 #if !defined(__WXWINCE__) || defined(__WINCE_STANDARDSDK__)
430 if ( !::MoveWindow(GetHwnd(), x
, y
, w
, h
, FALSE
) )
432 wxLogLastError(wxT("MoveWindow"));
436 if ( !title
.empty() )
438 ::SetWindowText(GetHwnd(), title
.wx_str());
443 #ifdef __SMARTPHONE__
444 // Work around title non-display glitch
449 #endif // __WXMICROWIN__/!__WXMICROWIN__
452 bool wxTopLevelWindowMSW::CreateFrame(const wxString
& title
,
457 WXDWORD flags
= MSWGetCreateWindowFlags(&exflags
);
459 const wxSize sz
= IsAlwaysMaximized() ? wxDefaultSize
: size
;
462 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
463 exflags
|= WS_EX_LAYOUTRTL
;
466 return MSWCreate(MSWGetRegisteredClassName(),
467 title
.wx_str(), pos
, sz
, flags
, exflags
);
470 bool wxTopLevelWindowMSW::Create(wxWindow
*parent
,
472 const wxString
& title
,
476 const wxString
& name
)
478 wxSize sizeReal
= size
;
479 if ( !sizeReal
.IsFullySpecified() )
481 sizeReal
.SetDefaults(GetDefaultSize());
484 // notice that we should append this window to wxTopLevelWindows list
485 // before calling CreateBase() as it behaves differently for TLW and
487 wxTopLevelWindows
.Append(this);
489 bool ret
= CreateBase(parent
, id
, pos
, sizeReal
, style
, name
);
494 parent
->AddChild(this);
496 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
498 // we have different dialog templates to allows creation of dialogs
499 // with & without captions under MSWindows, resizable or not (but a
500 // resizable dialog always has caption - otherwise it would look too
503 // we need 3 additional WORDs for dialog menu, class and title (as we
504 // don't use DS_SETFONT we don't need the fourth WORD for the font)
505 static const int dlgsize
= sizeof(DLGTEMPLATE
) + (sizeof(WORD
) * 3);
506 DLGTEMPLATE
*dlgTemplate
= (DLGTEMPLATE
*)malloc(dlgsize
);
507 memset(dlgTemplate
, 0, dlgsize
);
509 // these values are arbitrary, they won't be used normally anyhow
512 dlgTemplate
->cx
= 144;
513 dlgTemplate
->cy
= 75;
515 // reuse the code in MSWGetStyle() but correct the results slightly for
518 // NB: we need a temporary variable as we can't pass pointer to
519 // dwExtendedStyle directly, it's not aligned correctly for 64 bit
521 WXDWORD dwExtendedStyle
;
522 dlgTemplate
->style
= MSWGetStyle(style
, &dwExtendedStyle
);
523 dlgTemplate
->dwExtendedStyle
= dwExtendedStyle
;
525 // all dialogs are popups
526 dlgTemplate
->style
|= WS_POPUP
;
529 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
531 dlgTemplate
->dwExtendedStyle
|= WS_EX_LAYOUTRTL
;
534 // force 3D-look if necessary, it looks impossibly ugly otherwise
535 if ( style
& (wxRESIZE_BORDER
| wxCAPTION
) )
536 dlgTemplate
->style
|= DS_MODALFRAME
;
539 ret
= CreateDialog(dlgTemplate
, title
, pos
, sizeReal
);
544 ret
= CreateFrame(title
, pos
, sizeReal
);
548 if ( ret
&& !(GetWindowStyleFlag() & wxCLOSE_BOX
) )
550 EnableCloseButton(false);
554 // for standard dialogs the dialog manager generates WM_CHANGEUISTATE
555 // itself but for custom windows we have to do it ourselves in order to
556 // make the keyboard indicators (such as underlines for accelerators and
557 // focus rectangles) work under Win2k+
560 MSWUpdateUIState(UIS_INITIALIZE
);
563 // Note: if we include PocketPC in this test, dialogs can fail to show up,
564 // for example the text entry dialog in the dialogs sample. Problem with Maximise()?
565 #if defined(__WXWINCE__) && (defined(__SMARTPHONE__) || defined(__WINCE_STANDARDSDK__))
566 if ( ( style
& wxMAXIMIZE
) || IsAlwaysMaximized() )
572 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
573 SetRightMenu(); // to nothing for initialization
579 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
583 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
584 SHACTIVATEINFO
* info
= (SHACTIVATEINFO
*) m_activateInfo
;
586 m_activateInfo
= NULL
;
589 // after destroying an owned window, Windows activates the next top level
590 // window in Z order but it may be different from our owner (to reproduce
591 // this simply Alt-TAB to another application and back before closing the
592 // owned frame) whereas we always want to yield activation to our parent
593 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT
) )
595 wxWindow
*parent
= GetParent();
598 ::BringWindowToTop(GetHwndOf(parent
));
603 // ----------------------------------------------------------------------------
604 // wxTopLevelWindowMSW showing
605 // ----------------------------------------------------------------------------
607 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd
)
609 ::ShowWindow(GetHwnd(), nShowCmd
);
611 m_iconized
= nShowCmd
== SW_MINIMIZE
;
614 void wxTopLevelWindowMSW::ShowWithoutActivating()
616 if ( !wxWindowBase::Show(true) )
619 DoShowWindow(SW_SHOWNA
);
622 bool wxTopLevelWindowMSW::Show(bool show
)
624 // don't use wxWindow version as we want to call DoShowWindow() ourselves
625 if ( !wxWindowBase::Show(show
) )
631 if ( m_maximizeOnShow
)
634 nShowCmd
= SW_MAXIMIZE
;
636 // This is necessary, or no window appears
637 #if defined( __WINCE_STANDARDSDK__) || defined(__SMARTPHONE__)
638 DoShowWindow(SW_SHOW
);
641 m_maximizeOnShow
= false;
643 else if ( m_iconized
)
646 nShowCmd
= SW_MINIMIZE
;
650 // we shouldn't use SW_SHOW which also activates the window for
651 // tool frames (as they shouldn't steal focus from the main window)
652 // nor for the currently disabled windows as they would be enabled
654 if ( HasFlag(wxFRAME_TOOL_WINDOW
) || !IsEnabled() )
655 nShowCmd
= SW_SHOWNA
;
665 #if wxUSE_DEFERRED_SIZING
666 // we only set pending size if we're maximized before being shown, now that
667 // we're shown we don't need it any more (it is reset in size event handler
668 // for child windows but we have to do it ourselves for this parent window)
670 // make sure to reset it before actually showing the window as this will
671 // generate WM_SIZE events and we want to use the correct client size from
672 // them, not the size returned by WM_NCCALCSIZE in DoGetClientSize() which
673 // turns out to be wrong for maximized windows (see #11762)
674 m_pendingSize
= wxDefaultSize
;
675 #endif // wxUSE_DEFERRED_SIZING
677 DoShowWindow(nShowCmd
);
679 #if defined(__WXWINCE__) && (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__))
680 // Addornments have to be added when the frame is the correct size
681 wxFrame
* frame
= wxDynamicCast(this, wxFrame
);
682 if (frame
&& frame
->GetMenuBar())
683 frame
->GetMenuBar()->AddAdornments(GetWindowStyleFlag());
689 // ----------------------------------------------------------------------------
690 // wxTopLevelWindowMSW maximize/minimize
691 // ----------------------------------------------------------------------------
693 void wxTopLevelWindowMSW::Maximize(bool maximize
)
697 // just maximize it directly
698 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
702 // we can't maximize the hidden frame because it shows it as well,
703 // so just remember that we should do it later in this case
704 m_maximizeOnShow
= maximize
;
706 #if wxUSE_DEFERRED_SIZING
707 // after calling Maximize() the client code expects to get the frame
708 // "real" size and doesn't want to know that, because of implementation
709 // details, the frame isn't really maximized yet but will be only once
710 // it's shown, so return our size as it will be then in this case
713 // we must only change pending size here, and not call SetSize()
714 // because otherwise Windows would think that this (full screen)
715 // size is the natural size for the frame and so would use it when
716 // the user clicks on "restore" title bar button instead of the
717 // correct initial frame size
719 // NB: unfortunately we don't know which display we're on yet so we
720 // have to use the default one
721 m_pendingSize
= wxGetClientDisplayRect().GetSize();
723 //else: can't do anything in this case, we don't have the old size
724 #endif // wxUSE_DEFERRED_SIZING
728 bool wxTopLevelWindowMSW::IsMaximized() const
730 return IsAlwaysMaximized() ||
731 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__) && !defined(__WINCE_STANDARDSDK__)
733 (::IsZoomed(GetHwnd()) != 0) ||
738 void wxTopLevelWindowMSW::Iconize(bool iconize
)
740 if ( iconize
== m_iconized
)
742 // Do nothing, in particular don't restore non-iconized windows when
743 // Iconize(false) is called as this would wrongly un-maximize them.
749 // change the window state immediately
750 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
754 // iconizing the window shouldn't show it so just remember that we need
755 // to become iconized when shown later
760 bool wxTopLevelWindowMSW::IsIconized() const
768 // don't use m_iconized, it may be briefly out of sync with the real state
769 // as it's only modified when we receive a WM_SIZE and we could be called
770 // from an event handler from one of the messages we receive before it,
772 return ::IsIconic(GetHwnd()) != 0;
776 void wxTopLevelWindowMSW::Restore()
778 DoShowWindow(SW_RESTORE
);
781 void wxTopLevelWindowMSW::SetLayoutDirection(wxLayoutDirection dir
)
783 if ( dir
== wxLayout_Default
)
784 dir
= wxTheApp
->GetLayoutDirection();
786 if ( dir
!= wxLayout_Default
)
787 wxTopLevelWindowBase::SetLayoutDirection(dir
);
790 // ----------------------------------------------------------------------------
791 // wxTopLevelWindowMSW geometry
792 // ----------------------------------------------------------------------------
796 void wxTopLevelWindowMSW::DoGetPosition(int *x
, int *y
) const
801 wp
.length
= sizeof(WINDOWPLACEMENT
);
802 if ( ::GetWindowPlacement(GetHwnd(), &wp
) )
804 RECT
& rc
= wp
.rcNormalPosition
;
806 // the position returned by GetWindowPlacement() is in workspace
807 // coordinates except for windows with WS_EX_TOOLWINDOW style
808 if ( !HasFlag(wxFRAME_TOOL_WINDOW
) )
810 // we must use the correct display for the translation as the
811 // task bar might be shown on one display but not the other one
812 int n
= wxDisplay::GetFromWindow(this);
813 wxDisplay
dpy(n
== wxNOT_FOUND
? 0 : n
);
814 const wxPoint ptOfs
= dpy
.GetClientArea().GetPosition() -
815 dpy
.GetGeometry().GetPosition();
829 wxLogLastError(wxT("GetWindowPlacement"));
833 wxTopLevelWindowBase::DoGetPosition(x
, y
);
836 void wxTopLevelWindowMSW::DoGetSize(int *width
, int *height
) const
841 wp
.length
= sizeof(WINDOWPLACEMENT
);
842 if ( ::GetWindowPlacement(GetHwnd(), &wp
) )
844 const RECT
& rc
= wp
.rcNormalPosition
;
847 *width
= rc
.right
- rc
.left
;
849 *height
= rc
.bottom
- rc
.top
;
854 wxLogLastError(wxT("GetWindowPlacement"));
858 wxTopLevelWindowBase::DoGetSize(width
, height
);
861 #endif // __WXWINCE__
864 wxTopLevelWindowMSW::MSWGetCreateWindowCoords(const wxPoint
& pos
,
867 int& w
, int& h
) const
869 // let the system position the window if no explicit position was specified
870 if ( pos
.x
== wxDefaultCoord
)
872 // if x is set to CW_USEDEFAULT, y parameter is ignored anyhow so we
873 // can just as well set it to CW_USEDEFAULT as well
879 // OTOH, if x is not set to CW_USEDEFAULT, y shouldn't be set to it
880 // neither because it is not handled as a special value by Windows then
881 // and so we have to choose some default value for it, even if a
882 // completely arbitrary one
883 static const int DEFAULT_Y
= 200;
886 y
= pos
.y
== wxDefaultCoord
? DEFAULT_Y
: pos
.y
;
889 if ( size
.x
== wxDefaultCoord
|| size
.y
== wxDefaultCoord
)
891 // We don't use CW_USEDEFAULT here for several reasons:
893 // 1. It results in huge frames on modern screens (1000*800 is not
894 // uncommon on my 1280*1024 screen) which is way too big for a half
895 // empty frame of most of wxWidgets samples for example)
897 // 2. It is buggy for frames with wxFRAME_TOOL_WINDOW style for which
898 // the default is for whatever reason 8*8 which breaks client <->
899 // window size calculations (it would be nice if it didn't, but it
900 // does and the simplest way to fix it seemed to change the broken
901 // default size anyhow)
903 // 3. There is just no advantage in doing it: with x and y it is
904 // possible that [future versions of] Windows position the new top
905 // level window in some smart way which we can't do, but we can
906 // guess a reasonably good size for a new window just as well
909 // The only exception is for the Windows CE platform where the system
910 // does know better than we how should the windows be sized
915 wxSize sizeReal
= size
;
916 sizeReal
.SetDefaults(GetDefaultSize());
920 #endif // _WIN32_WCE/!_WIN32_WCE
929 // ----------------------------------------------------------------------------
930 // wxTopLevelWindowMSW fullscreen
931 // ----------------------------------------------------------------------------
933 bool wxTopLevelWindowMSW::ShowFullScreen(bool show
, long style
)
935 if ( show
== IsFullScreen() )
941 m_fsIsShowing
= show
;
947 // zap the frame borders
949 // save the 'normal' window style
950 m_fsOldWindowStyle
= GetWindowLong(GetHwnd(), GWL_STYLE
);
952 // save the old position, width & height, maximize state
953 m_fsOldSize
= GetRect();
954 m_fsIsMaximized
= IsMaximized();
956 // decide which window style flags to turn off
957 LONG newStyle
= m_fsOldWindowStyle
;
960 if (style
& wxFULLSCREEN_NOBORDER
)
962 offFlags
|= WS_BORDER
;
964 offFlags
|= WS_THICKFRAME
;
967 if (style
& wxFULLSCREEN_NOCAPTION
)
968 offFlags
|= WS_CAPTION
| WS_SYSMENU
;
970 newStyle
&= ~offFlags
;
972 // change our window style to be compatible with full-screen mode
973 ::SetWindowLong(GetHwnd(), GWL_STYLE
, newStyle
);
977 // resize to the size of the display containing us
978 int dpy
= wxDisplay::GetFromWindow(this);
979 if ( dpy
!= wxNOT_FOUND
)
981 rect
= wxDisplay(dpy
).GetGeometry();
983 else // fall back to the main desktop
984 #endif // wxUSE_DISPLAY
986 // resize to the size of the desktop
987 wxCopyRECTToRect(wxGetWindowRect(::GetDesktopWindow()), rect
);
989 // FIXME: size of the bottom menu (toolbar)
990 // should be taken in account
991 rect
.height
+= rect
.y
;
998 // now flush the window style cache and actually go full-screen
999 long flags
= SWP_FRAMECHANGED
;
1001 // showing the frame full screen should also show it if it's still
1005 // don't call wxWindow version to avoid flicker from calling
1006 // ::ShowWindow() -- we're going to show the window at the correct
1007 // location directly below -- but do call the wxWindowBase version
1008 // to sync the internal m_isShown flag
1009 wxWindowBase::Show();
1011 flags
|= SWP_SHOWWINDOW
;
1014 SetWindowPos(GetHwnd(), HWND_TOP
,
1015 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
1018 #if !defined(__HANDHELDPC__) && (defined(__WXWINCE__) && (_WIN32_WCE < 400))
1019 ::SHFullScreen(GetHwnd(), SHFS_HIDETASKBAR
| SHFS_HIDESIPBUTTON
);
1022 // finally send an event allowing the window to relayout itself &c
1023 wxSizeEvent
event(rect
.GetSize(), GetId());
1024 event
.SetEventObject(this);
1025 HandleWindowEvent(event
);
1027 else // stop showing full screen
1029 #if !defined(__HANDHELDPC__) && (defined(__WXWINCE__) && (_WIN32_WCE < 400))
1030 ::SHFullScreen(GetHwnd(), SHFS_SHOWTASKBAR
| SHFS_SHOWSIPBUTTON
);
1032 Maximize(m_fsIsMaximized
);
1033 SetWindowLong(GetHwnd(),GWL_STYLE
, m_fsOldWindowStyle
);
1034 SetWindowPos(GetHwnd(),HWND_TOP
,m_fsOldSize
.x
, m_fsOldSize
.y
,
1035 m_fsOldSize
.width
, m_fsOldSize
.height
, SWP_FRAMECHANGED
);
1041 // ----------------------------------------------------------------------------
1042 // wxTopLevelWindowMSW misc
1043 // ----------------------------------------------------------------------------
1045 void wxTopLevelWindowMSW::SetTitle( const wxString
& title
)
1050 wxString
wxTopLevelWindowMSW::GetTitle() const
1055 bool wxTopLevelWindowMSW::DoSelectAndSetIcon(const wxIconBundle
& icons
,
1060 const wxSize
size(::GetSystemMetrics(smX
), ::GetSystemMetrics(smY
));
1062 // Try the exact size first.
1063 wxIcon icon
= icons
.GetIconOfExactSize(size
);
1067 // If we didn't find any, set at least some icon: it will look scaled
1068 // and ugly but in practice it's impossible to prevent this because not
1069 // everyone can provide the icons in all sizes used by all versions of
1070 // Windows in all DPIs (this would include creating them in at least
1071 // 14, 16, 22, 32, 48, 64 and 128 pixel sizes).
1072 icon
= icons
.GetIcon(size
);
1078 ::SendMessage(GetHwnd(), WM_SETICON
, i
, (LPARAM
)GetHiconOf(icon
));
1082 void wxTopLevelWindowMSW::SetIcons(const wxIconBundle
& icons
)
1084 wxTopLevelWindowBase::SetIcons(icons
);
1086 if ( icons
.IsEmpty() )
1088 // FIXME: SetIcons(wxNullIconBundle) should unset existing icons,
1089 // but we currently don't do that
1090 wxASSERT_MSG( m_icons
.IsEmpty(), "unsetting icons doesn't work" );
1094 DoSelectAndSetIcon(icons
, SM_CXSMICON
, SM_CYSMICON
, ICON_SMALL
);
1095 DoSelectAndSetIcon(icons
, SM_CXICON
, SM_CYICON
, ICON_BIG
);
1098 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable
)
1100 #if !defined(__WXMICROWIN__)
1101 // get system (a.k.a. window) menu
1102 HMENU hmenu
= GetSystemMenu(GetHwnd(), FALSE
/* get it */);
1105 // no system menu at all -- ok if we want to remove the close button
1106 // anyhow, but bad if we want to show it
1110 // enabling/disabling the close item from it also automatically
1111 // disables/enables the close title bar button
1112 if ( ::EnableMenuItem(hmenu
, SC_CLOSE
,
1114 (enable
? MF_ENABLED
: MF_GRAYED
)) == -1 )
1116 wxLogLastError(wxT("EnableMenuItem(SC_CLOSE)"));
1121 // update appearance immediately
1122 if ( !::DrawMenuBar(GetHwnd()) )
1124 wxLogLastError(wxT("DrawMenuBar"));
1127 #endif // !__WXMICROWIN__
1134 bool wxTopLevelWindowMSW::SetShape(const wxRegion
& region
)
1136 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), false,
1137 wxT("Shaped windows must be created with the wxFRAME_SHAPED style."));
1139 // The empty region signifies that the shape should be removed from the
1141 if ( region
.IsEmpty() )
1143 if (::SetWindowRgn(GetHwnd(), NULL
, TRUE
) == 0)
1145 wxLogLastError(wxT("SetWindowRgn"));
1151 // Windows takes ownership of the region, so
1152 // we'll have to make a copy of the region to give to it.
1153 DWORD noBytes
= ::GetRegionData(GetHrgnOf(region
), 0, NULL
);
1154 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
1155 ::GetRegionData(GetHrgnOf(region
), noBytes
, rgnData
);
1156 HRGN hrgn
= ::ExtCreateRegion(NULL
, noBytes
, rgnData
);
1157 delete[] (char*) rgnData
;
1159 // SetWindowRgn expects the region to be in coordinants
1160 // relative to the window, not the client area. Figure
1161 // out the offset, if any.
1163 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1164 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1165 ::GetClientRect(GetHwnd(), &rect
);
1166 ::AdjustWindowRectEx(&rect
, dwStyle
, ::GetMenu(GetHwnd()) != NULL
, dwExStyle
);
1167 ::OffsetRgn(hrgn
, -rect
.left
, -rect
.top
);
1169 // Now call the shape API with the new region.
1170 if (::SetWindowRgn(GetHwnd(), hrgn
, TRUE
) == 0)
1172 wxLogLastError(wxT("SetWindowRgn"));
1178 #endif // !__WXWINCE__
1180 void wxTopLevelWindowMSW::RequestUserAttention(int flags
)
1182 // check if we can use FlashWindowEx(): unfortunately a simple test for
1183 // FLASHW_STOP doesn't work because MSVC6 headers do #define it but don't
1184 // provide FlashWindowEx() declaration, so try to detect whether we have
1185 // real headers for WINVER 0x0500 by checking for existence of a symbol not
1186 // declated in MSVC6 header
1187 #if defined(FLASHW_STOP) && defined(VK_XBUTTON1) && wxUSE_DYNLIB_CLASS
1188 // available in the headers, check if it is supported by the system
1189 typedef BOOL (WINAPI
*FlashWindowEx_t
)(FLASHWINFO
*pfwi
);
1190 static FlashWindowEx_t s_pfnFlashWindowEx
= NULL
;
1191 if ( !s_pfnFlashWindowEx
)
1193 wxDynamicLibrary
dllUser32(wxT("user32.dll"));
1194 s_pfnFlashWindowEx
= (FlashWindowEx_t
)
1195 dllUser32
.GetSymbol(wxT("FlashWindowEx"));
1197 // we can safely unload user32.dll here, it's going to remain loaded as
1198 // long as the program is running anyhow
1201 if ( s_pfnFlashWindowEx
)
1203 WinStruct
<FLASHWINFO
> fwi
;
1204 fwi
.hwnd
= GetHwnd();
1205 fwi
.dwFlags
= FLASHW_ALL
;
1206 if ( flags
& wxUSER_ATTENTION_INFO
)
1208 // just flash a few times
1211 else // wxUSER_ATTENTION_ERROR
1213 // flash until the user notices it
1214 fwi
.dwFlags
|= FLASHW_TIMERNOFG
;
1217 s_pfnFlashWindowEx(&fwi
);
1219 else // FlashWindowEx() not available
1220 #endif // FlashWindowEx() defined
1224 ::FlashWindow(GetHwnd(), TRUE
);
1225 #endif // __WXWINCE__
1229 // ---------------------------------------------------------------------------
1231 bool wxTopLevelWindowMSW::SetTransparent(wxByte alpha
)
1233 #if wxUSE_DYNLIB_CLASS
1234 typedef DWORD (WINAPI
*PSETLAYEREDWINDOWATTR
)(HWND
, DWORD
, BYTE
, DWORD
);
1235 static PSETLAYEREDWINDOWATTR
1236 pSetLayeredWindowAttributes
= (PSETLAYEREDWINDOWATTR
)-1;
1238 if ( pSetLayeredWindowAttributes
== (PSETLAYEREDWINDOWATTR
)-1 )
1240 wxDynamicLibrary
dllUser32(wxT("user32.dll"));
1242 // use RawGetSymbol() and not GetSymbol() to avoid error messages under
1243 // Windows 95: there is nothing the user can do about this anyhow
1244 pSetLayeredWindowAttributes
= (PSETLAYEREDWINDOWATTR
)
1245 dllUser32
.RawGetSymbol(wxT("SetLayeredWindowAttributes"));
1247 // it's ok to destroy dllUser32 here, we link statically to user32.dll
1248 // anyhow so it won't be unloaded
1251 if ( !pSetLayeredWindowAttributes
)
1253 #endif // wxUSE_DYNLIB_CLASS
1255 LONG exstyle
= GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1257 // if setting alpha to fully opaque then turn off the layered style
1260 SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exstyle
& ~WS_EX_LAYERED
);
1265 #if wxUSE_DYNLIB_CLASS
1266 // Otherwise, set the layered style if needed and set the alpha value
1267 if ((exstyle
& WS_EX_LAYERED
) == 0 )
1268 SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exstyle
| WS_EX_LAYERED
);
1270 if ( pSetLayeredWindowAttributes(GetHwnd(), 0, (BYTE
)alpha
, LWA_ALPHA
) )
1272 #endif // wxUSE_DYNLIB_CLASS
1277 bool wxTopLevelWindowMSW::CanSetTransparent()
1279 // The API is available on win2k and above
1281 static int os_type
= -1;
1282 static int ver_major
= -1;
1285 os_type
= ::wxGetOsVersion(&ver_major
);
1287 return (os_type
== wxOS_WINDOWS_NT
&& ver_major
>= 5);
1290 void wxTopLevelWindowMSW::DoEnable(bool enable
)
1292 wxTopLevelWindowBase::DoEnable(enable
);
1294 // Enabling or disabling a window may change its appearance. Unfortunately,
1295 // in at least some situation, toplevel windows don't repaint themselves,
1296 // so we have to issue explicit refresh to avoid rendering artifacts.
1298 // TODO: find out just what exactly is wrong here
1302 void wxTopLevelWindowMSW::DoFreeze()
1304 // do nothing: freezing toplevel window causes paint and mouse events
1305 // to go through it any TLWs under it, so the best we can do is to freeze
1306 // all children -- and wxWindowBase::Freeze() does that
1309 void wxTopLevelWindowMSW::DoThaw()
1311 // intentionally empty -- see DoFreeze()
1315 // ----------------------------------------------------------------------------
1316 // wxTopLevelWindow event handling
1317 // ----------------------------------------------------------------------------
1319 // Default activation behaviour - set the focus for the first child
1321 void wxTopLevelWindowMSW::OnActivate(wxActivateEvent
& event
)
1323 if ( event
.GetActive() )
1325 // restore focus to the child which was last focused unless we already
1327 wxLogTrace(wxT("focus"), wxT("wxTLW %p activated."), m_hWnd
);
1329 wxWindow
*winFocus
= FindFocus();
1330 if ( !winFocus
|| wxGetTopLevelParent(winFocus
) != this )
1332 wxWindow
*parent
= m_winLastFocused
? m_winLastFocused
->GetParent()
1339 wxSetFocusToChild(parent
, &m_winLastFocused
);
1342 else // deactivating
1344 // remember the last focused child if it is our child
1345 m_winLastFocused
= FindFocus();
1347 if ( m_winLastFocused
)
1349 // let it know that it doesn't have focus any more
1350 // But this will already be done via WM_KILLFOCUS, so we'll get two kill
1351 // focus events if we call it explicitly.
1352 // m_winLastFocused->HandleKillFocus((WXHWND)NULL);
1354 // and don't remember it if it's a child from some other frame
1355 if ( wxGetTopLevelParent(m_winLastFocused
) != this )
1357 m_winLastFocused
= NULL
;
1361 wxLogTrace(wxT("focus"),
1362 wxT("wxTLW %p deactivated, last focused: %p."),
1364 m_winLastFocused
? GetHwndOf(m_winLastFocused
) : NULL
);
1370 // the DialogProc for all wxWidgets dialogs
1371 LONG APIENTRY _EXPORT
1372 wxDlgProc(HWND hDlg
,
1374 WPARAM
WXUNUSED(wParam
),
1375 LPARAM
WXUNUSED(lParam
))
1381 // under CE, add a "Ok" button in the dialog title bar and make it full
1384 // TODO: find the window for this HWND, and take into account
1385 // wxMAXIMIZE and wxCLOSE_BOX. For now, assume both are present.
1387 // Standard SDK doesn't have aygshell.dll: see
1388 // include/wx/msw/wince/libraries.h
1389 #if defined(__WXWINCE__) && !defined(__WINCE_STANDARDSDK__) && !defined(__HANDHELDPC__)
1390 SHINITDLGINFO shidi
;
1391 shidi
.dwMask
= SHIDIM_FLAGS
;
1392 shidi
.dwFlags
= SHIDIF_SIZEDLG
// take account of the SIP or menubar
1393 #ifndef __SMARTPHONE__
1398 SHInitDialog( &shidi
);
1399 #else // no SHInitDialog()
1402 // for WM_INITDIALOG, returning TRUE tells system to set focus to
1403 // the first control in the dialog box, but as we set the focus
1404 // ourselves, we return FALSE for it as well
1409 // for almost all messages, returning FALSE means that we didn't process
1414 // ============================================================================
1415 // wxTLWHiddenParentModule implementation
1416 // ============================================================================
1418 HWND
wxTLWHiddenParentModule::ms_hwnd
= NULL
;
1420 const wxChar
*wxTLWHiddenParentModule::ms_className
= NULL
;
1422 bool wxTLWHiddenParentModule::OnInit()
1425 ms_className
= NULL
;
1430 void wxTLWHiddenParentModule::OnExit()
1434 if ( !::DestroyWindow(ms_hwnd
) )
1436 wxLogLastError(wxT("DestroyWindow(hidden TLW parent)"));
1444 if ( !::UnregisterClass(ms_className
, wxGetInstance()) )
1446 wxLogLastError(wxT("UnregisterClass(\"wxTLWHiddenParent\")"));
1449 ms_className
= NULL
;
1454 HWND
wxTLWHiddenParentModule::GetHWND()
1458 if ( !ms_className
)
1460 static const wxChar
*HIDDEN_PARENT_CLASS
= wxT("wxTLWHiddenParent");
1463 wxZeroMemory(wndclass
);
1465 wndclass
.lpfnWndProc
= DefWindowProc
;
1466 wndclass
.hInstance
= wxGetInstance();
1467 wndclass
.lpszClassName
= HIDDEN_PARENT_CLASS
;
1469 if ( !::RegisterClass(&wndclass
) )
1471 wxLogLastError(wxT("RegisterClass(\"wxTLWHiddenParent\")"));
1475 ms_className
= HIDDEN_PARENT_CLASS
;
1479 ms_hwnd
= ::CreateWindow(ms_className
, wxEmptyString
, 0, 0, 0, 0, 0, NULL
,
1480 (HMENU
)NULL
, wxGetInstance(), NULL
);
1483 wxLogLastError(wxT("CreateWindow(hidden TLW parent)"));