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
;
148 WXDWORD
wxTopLevelWindowMSW::MSWGetStyle(long style
, WXDWORD
*exflags
) const
150 // let the base class deal with the common styles but fix the ones which
151 // don't make sense for us (we also deal with the borders ourselves)
152 WXDWORD msflags
= wxWindow::MSWGetStyle
154 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exflags
155 ) & ~WS_CHILD
& ~WS_VISIBLE
;
157 // For some reason, WS_VISIBLE needs to be defined on creation for
158 // SmartPhone 2003. The title can fail to be displayed otherwise.
159 #if defined(__SMARTPHONE__) || (defined(__WXWINCE__) && _WIN32_WCE < 400)
160 msflags
|= WS_VISIBLE
;
161 ((wxTopLevelWindowMSW
*)this)->wxWindowBase::Show(true);
164 // first select the kind of window being created
166 // note that if we don't set WS_POPUP, Windows assumes WS_OVERLAPPED and
167 // creates a window with both caption and border, hence we need to use
168 // WS_POPUP in a few cases just to avoid having caption/border which we
171 // border and caption styles
172 if ( ( style
& wxRESIZE_BORDER
) && !IsAlwaysMaximized())
173 msflags
|= WS_THICKFRAME
;
174 else if ( exflags
&& ((style
& wxBORDER_DOUBLE
) || (style
& wxBORDER_RAISED
)) )
175 *exflags
|= WS_EX_DLGMODALFRAME
;
176 else if ( !(style
& wxBORDER_NONE
) )
177 msflags
|= WS_BORDER
;
183 // normally we consider that all windows without a caption must be popups,
184 // but CE is an exception: there windows normally do not have the caption
185 // but shouldn't be made popups as popups can't have menus and don't look
186 // like normal windows anyhow
188 // TODO: Smartphone appears to like wxCAPTION, but we should check that
190 #if defined(__SMARTPHONE__) || !defined(__WXWINCE__)
191 if ( style
& wxCAPTION
)
192 msflags
|= WS_CAPTION
;
196 #endif // !__WXWINCE__
199 // next translate the individual flags
201 // WS_EX_CONTEXTHELP is incompatible with WS_MINIMIZEBOX and WS_MAXIMIZEBOX
202 // and is ignored if we specify both of them, but chances are that if we
203 // use wxWS_EX_CONTEXTHELP, we really do want to have the context help
204 // button while wxMINIMIZE/wxMAXIMIZE are included by default, so the help
206 if ( !(GetExtraStyle() & wxWS_EX_CONTEXTHELP
) )
208 if ( style
& wxMINIMIZE_BOX
)
209 msflags
|= WS_MINIMIZEBOX
;
210 if ( style
& wxMAXIMIZE_BOX
)
211 msflags
|= WS_MAXIMIZEBOX
;
215 // notice that if wxCLOSE_BOX is specified we need to use WS_SYSMENU too as
216 // otherwise the close box doesn't appear
217 if ( style
& (wxSYSTEM_MENU
| wxCLOSE_BOX
) )
218 msflags
|= WS_SYSMENU
;
219 #endif // !__WXWINCE__
221 // NB: under CE these 2 styles are not supported currently, we should
222 // call Minimize()/Maximize() "manually" if we want to support them
223 if ( style
& wxMINIMIZE
)
224 msflags
|= WS_MINIMIZE
;
226 if ( style
& wxMAXIMIZE
)
227 msflags
|= WS_MAXIMIZE
;
229 // Keep this here because it saves recoding this function in wxTinyFrame
230 if ( style
& wxTINY_CAPTION
)
231 msflags
|= WS_CAPTION
;
235 // there is no taskbar under CE, so omit all this
236 #if !defined(__WXWINCE__)
237 if ( !(GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
) )
239 if ( style
& wxFRAME_TOOL_WINDOW
)
241 // create the palette-like window
242 *exflags
|= WS_EX_TOOLWINDOW
;
244 // tool windows shouldn't appear on the taskbar (as documented)
245 style
|= wxFRAME_NO_TASKBAR
;
248 // We have to solve 2 different problems here:
250 // 1. frames with wxFRAME_NO_TASKBAR flag shouldn't appear in the
251 // taskbar even if they don't have a parent
253 // 2. frames without this style should appear in the taskbar even
254 // if they're owned (Windows only puts non owned windows into
255 // the taskbar normally)
257 // The second one is solved here by using WS_EX_APPWINDOW flag, the
258 // first one is dealt with in our MSWGetParent() method
260 if ( !(style
& wxFRAME_NO_TASKBAR
) && GetParent() )
262 // need to force the frame to appear in the taskbar
263 *exflags
|= WS_EX_APPWINDOW
;
265 //else: nothing to do [here]
268 if ( GetExtraStyle() & wxWS_EX_CONTEXTHELP
)
269 *exflags
|= WS_EX_CONTEXTHELP
;
270 #endif // !__WXWINCE__
272 if ( style
& wxSTAY_ON_TOP
)
273 *exflags
|= WS_EX_TOPMOST
;
279 WXHWND
wxTopLevelWindowMSW::MSWGetParent() const
281 // for the frames without wxFRAME_FLOAT_ON_PARENT style we should use NULL
282 // parent HWND or it would be always on top of its parent which is not what
283 // we usually want (in fact, we only want it for frames with the
284 // wxFRAME_FLOAT_ON_PARENT flag)
285 HWND hwndParent
= NULL
;
286 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT
) )
288 const wxWindow
*parent
= GetParent();
292 // this flag doesn't make sense then and will be ignored
293 wxFAIL_MSG( wxT("wxFRAME_FLOAT_ON_PARENT but no parent?") );
297 hwndParent
= GetHwndOf(parent
);
300 //else: don't float on parent, must not be owned
302 // now deal with the 2nd taskbar-related problem (see comments above in
304 if ( HasFlag(wxFRAME_NO_TASKBAR
) && !hwndParent
)
307 hwndParent
= wxTLWHiddenParentModule::GetHWND();
310 return (WXHWND
)hwndParent
;
313 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
314 bool wxTopLevelWindowMSW::HandleSettingChange(WXWPARAM wParam
, WXLPARAM lParam
)
316 SHACTIVATEINFO
*info
= (SHACTIVATEINFO
*) m_activateInfo
;
319 SHHandleWMSettingChange(GetHwnd(), wParam
, lParam
, info
);
322 return wxWindowMSW::HandleSettingChange(wParam
, lParam
);
326 WXLRESULT
wxTopLevelWindowMSW::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
329 bool processed
= false;
333 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
336 SHACTIVATEINFO
* info
= (SHACTIVATEINFO
*) m_activateInfo
;
340 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
) flags
= SHA_INPUTDIALOG
;
341 SHHandleWMActivate(GetHwnd(), wParam
, lParam
, info
, flags
);
344 // This implicitly sends a wxEVT_ACTIVATE_APP event
346 wxTheApp
->SetActive(wParam
!= 0, FindFocus());
354 wxActivateEvent
event(wxEVT_HIBERNATE
, true, wxID_ANY
);
355 event
.SetEventObject(wxTheApp
);
356 processed
= wxTheApp
->ProcessEvent(event
);
360 #endif // __SMARTPHONE__ || __POCKETPC__
363 // We may need to generate events for the items added to the system
364 // menu if it had been created (and presumably modified).
369 // ... the four low-order bits of the wParam parameter are
370 // used internally by the system. To obtain the correct
371 // result when testing the value of wParam, an application
372 // must combine the value 0xFFF0 with the wParam value by
373 // using the bitwise AND operator.
374 unsigned id
= wParam
& 0xfff0;
376 // SC_SIZE is the first of the system-defined commands and we
377 // leave those to DefWindowProc().
380 if ( m_menuSystem
->MSWCommand(0 /* unused anyhow */, id
) )
388 rc
= wxTopLevelWindowBase::MSWWindowProc(message
, wParam
, lParam
);
393 bool wxTopLevelWindowMSW::CreateDialog(const void *dlgTemplate
,
394 const wxString
& title
,
398 #ifdef __WXMICROWIN__
399 // no dialogs support under MicroWin yet
400 return CreateFrame(title
, pos
, size
);
401 #else // !__WXMICROWIN__
402 // static cast is valid as we're only ever called for dialogs
404 parent
= static_cast<wxDialog
*>(this)->GetParentForModalDialog();
406 m_hWnd
= (WXHWND
)::CreateDialogIndirect
409 (DLGTEMPLATE
*)dlgTemplate
,
410 parent
? GetHwndOf(parent
) : NULL
,
416 wxFAIL_MSG(wxT("Failed to create dialog. Incorrect DLGTEMPLATE?"));
418 wxLogSysError(wxT("Can't create dialog using memory template"));
423 #if !defined(__WXWINCE__)
424 // For some reason, the system menu is activated when we use the
425 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
426 if ( HasExtraStyle(wxWS_EX_CONTEXTHELP
) )
428 wxFrame
*winTop
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
431 wxIcon icon
= winTop
->GetIcon();
434 ::SendMessage(GetHwnd(), WM_SETICON
,
436 (LPARAM
)GetHiconOf(icon
));
440 #endif // !__WXWINCE__
442 // move the dialog to its initial position without forcing repainting
444 (void)MSWGetCreateWindowCoords(pos
, size
, x
, y
, w
, h
);
446 if ( x
== (int)CW_USEDEFAULT
)
448 // centre it on the screen - what else can we do?
449 wxSize sizeDpy
= wxGetDisplaySize();
451 x
= (sizeDpy
.x
- w
) / 2;
452 y
= (sizeDpy
.y
- h
) / 2;
455 #if !defined(__WXWINCE__) || defined(__WINCE_STANDARDSDK__)
456 if ( !::MoveWindow(GetHwnd(), x
, y
, w
, h
, FALSE
) )
458 wxLogLastError(wxT("MoveWindow"));
462 if ( !title
.empty() )
464 ::SetWindowText(GetHwnd(), title
.wx_str());
469 #ifdef __SMARTPHONE__
470 // Work around title non-display glitch
475 #endif // __WXMICROWIN__/!__WXMICROWIN__
478 bool wxTopLevelWindowMSW::CreateFrame(const wxString
& title
,
483 WXDWORD flags
= MSWGetCreateWindowFlags(&exflags
);
485 const wxSize sz
= IsAlwaysMaximized() ? wxDefaultSize
: size
;
488 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
489 exflags
|= WS_EX_LAYOUTRTL
;
492 return MSWCreate(MSWGetRegisteredClassName(),
493 title
.wx_str(), pos
, sz
, flags
, exflags
);
496 bool wxTopLevelWindowMSW::Create(wxWindow
*parent
,
498 const wxString
& title
,
502 const wxString
& name
)
504 wxSize sizeReal
= size
;
505 if ( !sizeReal
.IsFullySpecified() )
507 sizeReal
.SetDefaults(GetDefaultSize());
510 // notice that we should append this window to wxTopLevelWindows list
511 // before calling CreateBase() as it behaves differently for TLW and
513 wxTopLevelWindows
.Append(this);
515 bool ret
= CreateBase(parent
, id
, pos
, sizeReal
, style
, name
);
520 parent
->AddChild(this);
522 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
524 // we have different dialog templates to allows creation of dialogs
525 // with & without captions under MSWindows, resizable or not (but a
526 // resizable dialog always has caption - otherwise it would look too
529 // we need 3 additional WORDs for dialog menu, class and title (as we
530 // don't use DS_SETFONT we don't need the fourth WORD for the font)
531 static const int dlgsize
= sizeof(DLGTEMPLATE
) + (sizeof(WORD
) * 3);
532 DLGTEMPLATE
*dlgTemplate
= (DLGTEMPLATE
*)malloc(dlgsize
);
533 memset(dlgTemplate
, 0, dlgsize
);
535 // these values are arbitrary, they won't be used normally anyhow
538 dlgTemplate
->cx
= 144;
539 dlgTemplate
->cy
= 75;
541 // reuse the code in MSWGetStyle() but correct the results slightly for
544 // NB: we need a temporary variable as we can't pass pointer to
545 // dwExtendedStyle directly, it's not aligned correctly for 64 bit
547 WXDWORD dwExtendedStyle
;
548 dlgTemplate
->style
= MSWGetStyle(style
, &dwExtendedStyle
);
549 dlgTemplate
->dwExtendedStyle
= dwExtendedStyle
;
551 // all dialogs are popups
552 dlgTemplate
->style
|= WS_POPUP
;
555 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
557 dlgTemplate
->dwExtendedStyle
|= WS_EX_LAYOUTRTL
;
560 // force 3D-look if necessary, it looks impossibly ugly otherwise
561 if ( style
& (wxRESIZE_BORDER
| wxCAPTION
) )
562 dlgTemplate
->style
|= DS_MODALFRAME
;
565 ret
= CreateDialog(dlgTemplate
, title
, pos
, sizeReal
);
570 ret
= CreateFrame(title
, pos
, sizeReal
);
574 if ( ret
&& !(GetWindowStyleFlag() & wxCLOSE_BOX
) )
576 EnableCloseButton(false);
580 // for standard dialogs the dialog manager generates WM_CHANGEUISTATE
581 // itself but for custom windows we have to do it ourselves in order to
582 // make the keyboard indicators (such as underlines for accelerators and
583 // focus rectangles) work under Win2k+
586 MSWUpdateUIState(UIS_INITIALIZE
);
589 // Note: if we include PocketPC in this test, dialogs can fail to show up,
590 // for example the text entry dialog in the dialogs sample. Problem with Maximise()?
591 #if defined(__WXWINCE__) && (defined(__SMARTPHONE__) || defined(__WINCE_STANDARDSDK__))
592 if ( ( style
& wxMAXIMIZE
) || IsAlwaysMaximized() )
598 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
599 SetRightMenu(); // to nothing for initialization
605 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
611 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
612 SHACTIVATEINFO
* info
= (SHACTIVATEINFO
*) m_activateInfo
;
614 m_activateInfo
= NULL
;
617 // after destroying an owned window, Windows activates the next top level
618 // window in Z order but it may be different from our owner (to reproduce
619 // this simply Alt-TAB to another application and back before closing the
620 // owned frame) whereas we always want to yield activation to our parent
621 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT
) )
623 wxWindow
*parent
= GetParent();
626 ::BringWindowToTop(GetHwndOf(parent
));
631 // ----------------------------------------------------------------------------
632 // wxTopLevelWindowMSW showing
633 // ----------------------------------------------------------------------------
635 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd
)
637 ::ShowWindow(GetHwnd(), nShowCmd
);
639 m_iconized
= nShowCmd
== SW_MINIMIZE
;
642 void wxTopLevelWindowMSW::ShowWithoutActivating()
644 if ( !wxWindowBase::Show(true) )
647 DoShowWindow(SW_SHOWNA
);
650 bool wxTopLevelWindowMSW::Show(bool show
)
652 // don't use wxWindow version as we want to call DoShowWindow() ourselves
653 if ( !wxWindowBase::Show(show
) )
659 if ( m_maximizeOnShow
)
662 nShowCmd
= SW_MAXIMIZE
;
664 // This is necessary, or no window appears
665 #if defined( __WINCE_STANDARDSDK__) || defined(__SMARTPHONE__)
666 DoShowWindow(SW_SHOW
);
669 m_maximizeOnShow
= false;
671 else if ( m_iconized
)
674 nShowCmd
= SW_MINIMIZE
;
678 // we shouldn't use SW_SHOW which also activates the window for
679 // tool frames (as they shouldn't steal focus from the main window)
680 // nor for the currently disabled windows as they would be enabled
682 if ( HasFlag(wxFRAME_TOOL_WINDOW
) || !IsEnabled() )
683 nShowCmd
= SW_SHOWNA
;
693 #if wxUSE_DEFERRED_SIZING
694 // we only set pending size if we're maximized before being shown, now that
695 // we're shown we don't need it any more (it is reset in size event handler
696 // for child windows but we have to do it ourselves for this parent window)
698 // make sure to reset it before actually showing the window as this will
699 // generate WM_SIZE events and we want to use the correct client size from
700 // them, not the size returned by WM_NCCALCSIZE in DoGetClientSize() which
701 // turns out to be wrong for maximized windows (see #11762)
702 m_pendingSize
= wxDefaultSize
;
703 #endif // wxUSE_DEFERRED_SIZING
705 DoShowWindow(nShowCmd
);
707 #if defined(__WXWINCE__) && (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__))
708 // Addornments have to be added when the frame is the correct size
709 wxFrame
* frame
= wxDynamicCast(this, wxFrame
);
710 if (frame
&& frame
->GetMenuBar())
711 frame
->GetMenuBar()->AddAdornments(GetWindowStyleFlag());
717 // ----------------------------------------------------------------------------
718 // wxTopLevelWindowMSW maximize/minimize
719 // ----------------------------------------------------------------------------
721 void wxTopLevelWindowMSW::Maximize(bool maximize
)
725 // just maximize it directly
726 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
730 // we can't maximize the hidden frame because it shows it as well,
731 // so just remember that we should do it later in this case
732 m_maximizeOnShow
= maximize
;
734 #if wxUSE_DEFERRED_SIZING
735 // after calling Maximize() the client code expects to get the frame
736 // "real" size and doesn't want to know that, because of implementation
737 // details, the frame isn't really maximized yet but will be only once
738 // it's shown, so return our size as it will be then in this case
741 // we must only change pending size here, and not call SetSize()
742 // because otherwise Windows would think that this (full screen)
743 // size is the natural size for the frame and so would use it when
744 // the user clicks on "restore" title bar button instead of the
745 // correct initial frame size
747 // NB: unfortunately we don't know which display we're on yet so we
748 // have to use the default one
749 m_pendingSize
= wxGetClientDisplayRect().GetSize();
751 //else: can't do anything in this case, we don't have the old size
752 #endif // wxUSE_DEFERRED_SIZING
756 bool wxTopLevelWindowMSW::IsMaximized() const
758 return IsAlwaysMaximized() ||
759 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__) && !defined(__WINCE_STANDARDSDK__)
761 (::IsZoomed(GetHwnd()) != 0) ||
766 void wxTopLevelWindowMSW::Iconize(bool iconize
)
768 if ( iconize
== m_iconized
)
770 // Do nothing, in particular don't restore non-iconized windows when
771 // Iconize(false) is called as this would wrongly un-maximize them.
777 // change the window state immediately
778 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
782 // iconizing the window shouldn't show it so just remember that we need
783 // to become iconized when shown later
788 bool wxTopLevelWindowMSW::IsIconized() const
796 // don't use m_iconized, it may be briefly out of sync with the real state
797 // as it's only modified when we receive a WM_SIZE and we could be called
798 // from an event handler from one of the messages we receive before it,
800 return ::IsIconic(GetHwnd()) != 0;
804 void wxTopLevelWindowMSW::Restore()
806 DoShowWindow(SW_RESTORE
);
809 void wxTopLevelWindowMSW::SetLayoutDirection(wxLayoutDirection dir
)
811 if ( dir
== wxLayout_Default
)
812 dir
= wxTheApp
->GetLayoutDirection();
814 if ( dir
!= wxLayout_Default
)
815 wxTopLevelWindowBase::SetLayoutDirection(dir
);
818 // ----------------------------------------------------------------------------
819 // wxTopLevelWindowMSW geometry
820 // ----------------------------------------------------------------------------
824 void wxTopLevelWindowMSW::DoGetPosition(int *x
, int *y
) const
829 wp
.length
= sizeof(WINDOWPLACEMENT
);
830 if ( ::GetWindowPlacement(GetHwnd(), &wp
) )
832 RECT
& rc
= wp
.rcNormalPosition
;
834 // the position returned by GetWindowPlacement() is in workspace
835 // coordinates except for windows with WS_EX_TOOLWINDOW style
836 if ( !HasFlag(wxFRAME_TOOL_WINDOW
) )
838 // we must use the correct display for the translation as the
839 // task bar might be shown on one display but not the other one
840 int n
= wxDisplay::GetFromWindow(this);
841 wxDisplay
dpy(n
== wxNOT_FOUND
? 0 : n
);
842 const wxPoint ptOfs
= dpy
.GetClientArea().GetPosition() -
843 dpy
.GetGeometry().GetPosition();
857 wxLogLastError(wxT("GetWindowPlacement"));
861 wxTopLevelWindowBase::DoGetPosition(x
, y
);
864 void wxTopLevelWindowMSW::DoGetSize(int *width
, int *height
) const
869 wp
.length
= sizeof(WINDOWPLACEMENT
);
870 if ( ::GetWindowPlacement(GetHwnd(), &wp
) )
872 const RECT
& rc
= wp
.rcNormalPosition
;
875 *width
= rc
.right
- rc
.left
;
877 *height
= rc
.bottom
- rc
.top
;
882 wxLogLastError(wxT("GetWindowPlacement"));
886 wxTopLevelWindowBase::DoGetSize(width
, height
);
889 #endif // __WXWINCE__
892 wxTopLevelWindowMSW::MSWGetCreateWindowCoords(const wxPoint
& pos
,
895 int& w
, int& h
) const
897 // let the system position the window if no explicit position was specified
898 if ( pos
.x
== wxDefaultCoord
)
900 // if x is set to CW_USEDEFAULT, y parameter is ignored anyhow so we
901 // can just as well set it to CW_USEDEFAULT as well
907 // OTOH, if x is not set to CW_USEDEFAULT, y shouldn't be set to it
908 // neither because it is not handled as a special value by Windows then
909 // and so we have to choose some default value for it, even if a
910 // completely arbitrary one
911 static const int DEFAULT_Y
= 200;
914 y
= pos
.y
== wxDefaultCoord
? DEFAULT_Y
: pos
.y
;
917 if ( size
.x
== wxDefaultCoord
|| size
.y
== wxDefaultCoord
)
919 // We don't use CW_USEDEFAULT here for several reasons:
921 // 1. It results in huge frames on modern screens (1000*800 is not
922 // uncommon on my 1280*1024 screen) which is way too big for a half
923 // empty frame of most of wxWidgets samples for example)
925 // 2. It is buggy for frames with wxFRAME_TOOL_WINDOW style for which
926 // the default is for whatever reason 8*8 which breaks client <->
927 // window size calculations (it would be nice if it didn't, but it
928 // does and the simplest way to fix it seemed to change the broken
929 // default size anyhow)
931 // 3. There is just no advantage in doing it: with x and y it is
932 // possible that [future versions of] Windows position the new top
933 // level window in some smart way which we can't do, but we can
934 // guess a reasonably good size for a new window just as well
937 // The only exception is for the Windows CE platform where the system
938 // does know better than we how should the windows be sized
943 wxSize sizeReal
= size
;
944 sizeReal
.SetDefaults(GetDefaultSize());
948 #endif // _WIN32_WCE/!_WIN32_WCE
957 // ----------------------------------------------------------------------------
958 // wxTopLevelWindowMSW fullscreen
959 // ----------------------------------------------------------------------------
961 bool wxTopLevelWindowMSW::ShowFullScreen(bool show
, long style
)
963 if ( show
== IsFullScreen() )
969 m_fsIsShowing
= show
;
975 // zap the frame borders
977 // save the 'normal' window style
978 m_fsOldWindowStyle
= GetWindowLong(GetHwnd(), GWL_STYLE
);
980 // save the old position, width & height, maximize state
981 m_fsOldSize
= GetRect();
982 m_fsIsMaximized
= IsMaximized();
984 // decide which window style flags to turn off
985 LONG newStyle
= m_fsOldWindowStyle
;
988 if (style
& wxFULLSCREEN_NOBORDER
)
990 offFlags
|= WS_BORDER
;
992 offFlags
|= WS_THICKFRAME
;
995 if (style
& wxFULLSCREEN_NOCAPTION
)
996 offFlags
|= WS_CAPTION
| WS_SYSMENU
;
998 newStyle
&= ~offFlags
;
1000 // change our window style to be compatible with full-screen mode
1001 ::SetWindowLong(GetHwnd(), GWL_STYLE
, newStyle
);
1005 // resize to the size of the display containing us
1006 int dpy
= wxDisplay::GetFromWindow(this);
1007 if ( dpy
!= wxNOT_FOUND
)
1009 rect
= wxDisplay(dpy
).GetGeometry();
1011 else // fall back to the main desktop
1012 #endif // wxUSE_DISPLAY
1014 // resize to the size of the desktop
1015 wxCopyRECTToRect(wxGetWindowRect(::GetDesktopWindow()), rect
);
1017 // FIXME: size of the bottom menu (toolbar)
1018 // should be taken in account
1019 rect
.height
+= rect
.y
;
1026 // now flush the window style cache and actually go full-screen
1027 long flags
= SWP_FRAMECHANGED
;
1029 // showing the frame full screen should also show it if it's still
1033 // don't call wxWindow version to avoid flicker from calling
1034 // ::ShowWindow() -- we're going to show the window at the correct
1035 // location directly below -- but do call the wxWindowBase version
1036 // to sync the internal m_isShown flag
1037 wxWindowBase::Show();
1039 flags
|= SWP_SHOWWINDOW
;
1042 SetWindowPos(GetHwnd(), HWND_TOP
,
1043 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
1046 #if !defined(__HANDHELDPC__) && (defined(__WXWINCE__) && (_WIN32_WCE < 400))
1047 ::SHFullScreen(GetHwnd(), SHFS_HIDETASKBAR
| SHFS_HIDESIPBUTTON
);
1050 // finally send an event allowing the window to relayout itself &c
1051 wxSizeEvent
event(rect
.GetSize(), GetId());
1052 event
.SetEventObject(this);
1053 HandleWindowEvent(event
);
1055 else // stop showing full screen
1057 #if !defined(__HANDHELDPC__) && (defined(__WXWINCE__) && (_WIN32_WCE < 400))
1058 ::SHFullScreen(GetHwnd(), SHFS_SHOWTASKBAR
| SHFS_SHOWSIPBUTTON
);
1060 Maximize(m_fsIsMaximized
);
1061 SetWindowLong(GetHwnd(),GWL_STYLE
, m_fsOldWindowStyle
);
1062 SetWindowPos(GetHwnd(),HWND_TOP
,m_fsOldSize
.x
, m_fsOldSize
.y
,
1063 m_fsOldSize
.width
, m_fsOldSize
.height
, SWP_FRAMECHANGED
);
1069 // ----------------------------------------------------------------------------
1070 // wxTopLevelWindowMSW misc
1071 // ----------------------------------------------------------------------------
1073 void wxTopLevelWindowMSW::SetTitle( const wxString
& title
)
1078 wxString
wxTopLevelWindowMSW::GetTitle() const
1083 bool wxTopLevelWindowMSW::DoSelectAndSetIcon(const wxIconBundle
& icons
,
1088 const wxSize
size(::GetSystemMetrics(smX
), ::GetSystemMetrics(smY
));
1090 // Try the exact size first.
1091 wxIcon icon
= icons
.GetIconOfExactSize(size
);
1095 // If we didn't find any, set at least some icon: it will look scaled
1096 // and ugly but in practice it's impossible to prevent this because not
1097 // everyone can provide the icons in all sizes used by all versions of
1098 // Windows in all DPIs (this would include creating them in at least
1099 // 14, 16, 22, 32, 48, 64 and 128 pixel sizes).
1100 icon
= icons
.GetIcon(size
);
1106 ::SendMessage(GetHwnd(), WM_SETICON
, i
, (LPARAM
)GetHiconOf(icon
));
1110 void wxTopLevelWindowMSW::SetIcons(const wxIconBundle
& icons
)
1112 wxTopLevelWindowBase::SetIcons(icons
);
1114 if ( icons
.IsEmpty() )
1116 // FIXME: SetIcons(wxNullIconBundle) should unset existing icons,
1117 // but we currently don't do that
1118 wxASSERT_MSG( m_icons
.IsEmpty(), "unsetting icons doesn't work" );
1122 DoSelectAndSetIcon(icons
, SM_CXSMICON
, SM_CYSMICON
, ICON_SMALL
);
1123 DoSelectAndSetIcon(icons
, SM_CXICON
, SM_CYICON
, ICON_BIG
);
1126 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable
)
1128 #if !defined(__WXMICROWIN__)
1129 // get system (a.k.a. window) menu
1130 HMENU hmenu
= GetSystemMenu(GetHwnd(), FALSE
/* get it */);
1133 // no system menu at all -- ok if we want to remove the close button
1134 // anyhow, but bad if we want to show it
1138 // enabling/disabling the close item from it also automatically
1139 // disables/enables the close title bar button
1140 if ( ::EnableMenuItem(hmenu
, SC_CLOSE
,
1142 (enable
? MF_ENABLED
: MF_GRAYED
)) == -1 )
1144 wxLogLastError(wxT("EnableMenuItem(SC_CLOSE)"));
1149 // update appearance immediately
1150 if ( !::DrawMenuBar(GetHwnd()) )
1152 wxLogLastError(wxT("DrawMenuBar"));
1155 #endif // !__WXMICROWIN__
1162 bool wxTopLevelWindowMSW::SetShape(const wxRegion
& region
)
1164 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), false,
1165 wxT("Shaped windows must be created with the wxFRAME_SHAPED style."));
1167 // The empty region signifies that the shape should be removed from the
1169 if ( region
.IsEmpty() )
1171 if (::SetWindowRgn(GetHwnd(), NULL
, TRUE
) == 0)
1173 wxLogLastError(wxT("SetWindowRgn"));
1179 // Windows takes ownership of the region, so
1180 // we'll have to make a copy of the region to give to it.
1181 DWORD noBytes
= ::GetRegionData(GetHrgnOf(region
), 0, NULL
);
1182 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
1183 ::GetRegionData(GetHrgnOf(region
), noBytes
, rgnData
);
1184 HRGN hrgn
= ::ExtCreateRegion(NULL
, noBytes
, rgnData
);
1185 delete[] (char*) rgnData
;
1187 // SetWindowRgn expects the region to be in coordinants
1188 // relative to the window, not the client area. Figure
1189 // out the offset, if any.
1191 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1192 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1193 ::GetClientRect(GetHwnd(), &rect
);
1194 ::AdjustWindowRectEx(&rect
, dwStyle
, ::GetMenu(GetHwnd()) != NULL
, dwExStyle
);
1195 ::OffsetRgn(hrgn
, -rect
.left
, -rect
.top
);
1197 // Now call the shape API with the new region.
1198 if (::SetWindowRgn(GetHwnd(), hrgn
, TRUE
) == 0)
1200 wxLogLastError(wxT("SetWindowRgn"));
1206 #endif // !__WXWINCE__
1208 void wxTopLevelWindowMSW::RequestUserAttention(int flags
)
1210 // check if we can use FlashWindowEx(): unfortunately a simple test for
1211 // FLASHW_STOP doesn't work because MSVC6 headers do #define it but don't
1212 // provide FlashWindowEx() declaration, so try to detect whether we have
1213 // real headers for WINVER 0x0500 by checking for existence of a symbol not
1214 // declated in MSVC6 header
1215 #if defined(FLASHW_STOP) && defined(VK_XBUTTON1) && wxUSE_DYNLIB_CLASS
1216 // available in the headers, check if it is supported by the system
1217 typedef BOOL (WINAPI
*FlashWindowEx_t
)(FLASHWINFO
*pfwi
);
1218 static FlashWindowEx_t s_pfnFlashWindowEx
= NULL
;
1219 if ( !s_pfnFlashWindowEx
)
1221 wxDynamicLibrary
dllUser32(wxT("user32.dll"));
1222 s_pfnFlashWindowEx
= (FlashWindowEx_t
)
1223 dllUser32
.GetSymbol(wxT("FlashWindowEx"));
1225 // we can safely unload user32.dll here, it's going to remain loaded as
1226 // long as the program is running anyhow
1229 if ( s_pfnFlashWindowEx
)
1231 WinStruct
<FLASHWINFO
> fwi
;
1232 fwi
.hwnd
= GetHwnd();
1233 fwi
.dwFlags
= FLASHW_ALL
;
1234 if ( flags
& wxUSER_ATTENTION_INFO
)
1236 // just flash a few times
1239 else // wxUSER_ATTENTION_ERROR
1241 // flash until the user notices it
1242 fwi
.dwFlags
|= FLASHW_TIMERNOFG
;
1245 s_pfnFlashWindowEx(&fwi
);
1247 else // FlashWindowEx() not available
1248 #endif // FlashWindowEx() defined
1252 ::FlashWindow(GetHwnd(), TRUE
);
1253 #endif // __WXWINCE__
1257 wxMenu
*wxTopLevelWindowMSW::MSWGetSystemMenu() const
1259 if ( !m_menuSystem
)
1261 HMENU hmenu
= ::GetSystemMenu(GetHwnd(), FALSE
);
1264 wxLogLastError(wxT("GetSystemMenu()"));
1268 wxTopLevelWindowMSW
* const
1269 self
= const_cast<wxTopLevelWindowMSW
*>(this);
1271 self
->m_menuSystem
= wxMenu::MSWNewFromHMENU(hmenu
);
1273 // We need to somehow associate this menu with this window to ensure
1274 // that we get events from it. A natural idea would be to pretend that
1275 // it's attached to our menu bar but this wouldn't work if we don't
1276 // have any menu bar which is a common case for applications using
1277 // custom items in the system menu (they mostly do it exactly because
1278 // they don't have any other menus).
1280 // So reuse the invoking window pointer instead, this is not exactly
1281 // correct but doesn't seem to have any serious drawbacks.
1282 m_menuSystem
->SetInvokingWindow(self
);
1285 return m_menuSystem
;
1288 // ----------------------------------------------------------------------------
1289 // Transparency support
1290 // ---------------------------------------------------------------------------
1292 bool wxTopLevelWindowMSW::SetTransparent(wxByte alpha
)
1294 #if wxUSE_DYNLIB_CLASS
1295 typedef DWORD (WINAPI
*PSETLAYEREDWINDOWATTR
)(HWND
, DWORD
, BYTE
, DWORD
);
1296 static PSETLAYEREDWINDOWATTR
1297 pSetLayeredWindowAttributes
= (PSETLAYEREDWINDOWATTR
)-1;
1299 if ( pSetLayeredWindowAttributes
== (PSETLAYEREDWINDOWATTR
)-1 )
1301 wxDynamicLibrary
dllUser32(wxT("user32.dll"));
1303 // use RawGetSymbol() and not GetSymbol() to avoid error messages under
1304 // Windows 95: there is nothing the user can do about this anyhow
1305 pSetLayeredWindowAttributes
= (PSETLAYEREDWINDOWATTR
)
1306 dllUser32
.RawGetSymbol(wxT("SetLayeredWindowAttributes"));
1308 // it's ok to destroy dllUser32 here, we link statically to user32.dll
1309 // anyhow so it won't be unloaded
1312 if ( !pSetLayeredWindowAttributes
)
1314 #endif // wxUSE_DYNLIB_CLASS
1316 LONG exstyle
= GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1318 // if setting alpha to fully opaque then turn off the layered style
1321 SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exstyle
& ~WS_EX_LAYERED
);
1326 #if wxUSE_DYNLIB_CLASS
1327 // Otherwise, set the layered style if needed and set the alpha value
1328 if ((exstyle
& WS_EX_LAYERED
) == 0 )
1329 SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exstyle
| WS_EX_LAYERED
);
1331 if ( pSetLayeredWindowAttributes(GetHwnd(), 0, (BYTE
)alpha
, LWA_ALPHA
) )
1333 #endif // wxUSE_DYNLIB_CLASS
1338 bool wxTopLevelWindowMSW::CanSetTransparent()
1340 // The API is available on win2k and above
1342 static int os_type
= -1;
1343 static int ver_major
= -1;
1346 os_type
= ::wxGetOsVersion(&ver_major
);
1348 return (os_type
== wxOS_WINDOWS_NT
&& ver_major
>= 5);
1351 void wxTopLevelWindowMSW::DoEnable(bool enable
)
1353 wxTopLevelWindowBase::DoEnable(enable
);
1355 // Enabling or disabling a window may change its appearance. Unfortunately,
1356 // in at least some situation, toplevel windows don't repaint themselves,
1357 // so we have to issue explicit refresh to avoid rendering artifacts.
1359 // TODO: find out just what exactly is wrong here
1363 void wxTopLevelWindowMSW::DoFreeze()
1365 // do nothing: freezing toplevel window causes paint and mouse events
1366 // to go through it any TLWs under it, so the best we can do is to freeze
1367 // all children -- and wxWindowBase::Freeze() does that
1370 void wxTopLevelWindowMSW::DoThaw()
1372 // intentionally empty -- see DoFreeze()
1376 // ----------------------------------------------------------------------------
1377 // wxTopLevelWindow event handling
1378 // ----------------------------------------------------------------------------
1380 // Default activation behaviour - set the focus for the first child
1382 void wxTopLevelWindowMSW::OnActivate(wxActivateEvent
& event
)
1384 if ( event
.GetActive() )
1386 // restore focus to the child which was last focused unless we already
1388 wxLogTrace(wxT("focus"), wxT("wxTLW %p activated."), m_hWnd
);
1390 wxWindow
*winFocus
= FindFocus();
1391 if ( !winFocus
|| wxGetTopLevelParent(winFocus
) != this )
1393 wxWindow
*parent
= m_winLastFocused
? m_winLastFocused
->GetParent()
1400 wxSetFocusToChild(parent
, &m_winLastFocused
);
1403 else // deactivating
1405 // remember the last focused child if it is our child
1406 m_winLastFocused
= FindFocus();
1408 if ( m_winLastFocused
)
1410 // let it know that it doesn't have focus any more
1411 // But this will already be done via WM_KILLFOCUS, so we'll get two kill
1412 // focus events if we call it explicitly.
1413 // m_winLastFocused->HandleKillFocus((WXHWND)NULL);
1415 // and don't remember it if it's a child from some other frame
1416 if ( wxGetTopLevelParent(m_winLastFocused
) != this )
1418 m_winLastFocused
= NULL
;
1422 wxLogTrace(wxT("focus"),
1423 wxT("wxTLW %p deactivated, last focused: %p."),
1425 m_winLastFocused
? GetHwndOf(m_winLastFocused
) : NULL
);
1431 // the DialogProc for all wxWidgets dialogs
1432 LONG APIENTRY _EXPORT
1433 wxDlgProc(HWND hDlg
,
1435 WPARAM
WXUNUSED(wParam
),
1436 LPARAM
WXUNUSED(lParam
))
1442 // under CE, add a "Ok" button in the dialog title bar and make it full
1445 // TODO: find the window for this HWND, and take into account
1446 // wxMAXIMIZE and wxCLOSE_BOX. For now, assume both are present.
1448 // Standard SDK doesn't have aygshell.dll: see
1449 // include/wx/msw/wince/libraries.h
1450 #if defined(__WXWINCE__) && !defined(__WINCE_STANDARDSDK__) && !defined(__HANDHELDPC__)
1451 SHINITDLGINFO shidi
;
1452 shidi
.dwMask
= SHIDIM_FLAGS
;
1453 shidi
.dwFlags
= SHIDIF_SIZEDLG
// take account of the SIP or menubar
1454 #ifndef __SMARTPHONE__
1459 SHInitDialog( &shidi
);
1460 #else // no SHInitDialog()
1463 // for WM_INITDIALOG, returning TRUE tells system to set focus to
1464 // the first control in the dialog box, but as we set the focus
1465 // ourselves, we return FALSE for it as well
1470 // for almost all messages, returning FALSE means that we didn't process
1475 // ============================================================================
1476 // wxTLWHiddenParentModule implementation
1477 // ============================================================================
1479 HWND
wxTLWHiddenParentModule::ms_hwnd
= NULL
;
1481 const wxChar
*wxTLWHiddenParentModule::ms_className
= NULL
;
1483 bool wxTLWHiddenParentModule::OnInit()
1486 ms_className
= NULL
;
1491 void wxTLWHiddenParentModule::OnExit()
1495 if ( !::DestroyWindow(ms_hwnd
) )
1497 wxLogLastError(wxT("DestroyWindow(hidden TLW parent)"));
1505 if ( !::UnregisterClass(ms_className
, wxGetInstance()) )
1507 wxLogLastError(wxT("UnregisterClass(\"wxTLWHiddenParent\")"));
1510 ms_className
= NULL
;
1515 HWND
wxTLWHiddenParentModule::GetHWND()
1519 if ( !ms_className
)
1521 static const wxChar
*HIDDEN_PARENT_CLASS
= wxT("wxTLWHiddenParent");
1524 wxZeroMemory(wndclass
);
1526 wndclass
.lpfnWndProc
= DefWindowProc
;
1527 wndclass
.hInstance
= wxGetInstance();
1528 wndclass
.lpszClassName
= HIDDEN_PARENT_CLASS
;
1530 if ( !::RegisterClass(&wndclass
) )
1532 wxLogLastError(wxT("RegisterClass(\"wxTLWHiddenParent\")"));
1536 ms_className
= HIDDEN_PARENT_CLASS
;
1540 ms_hwnd
= ::CreateWindow(ms_className
, wxEmptyString
, 0, 0, 0, 0, 0, NULL
,
1541 (HMENU
)NULL
, wxGetInstance(), NULL
);
1544 wxLogLastError(wxT("CreateWindow(hidden TLW parent)"));