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 // License: 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_VERT
| wxTINY_CAPTION_HORIZ
) )
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)->
379 GetParentForModalDialog(GetParent());
381 m_hWnd
= (WXHWND
)::CreateDialogIndirect
384 (DLGTEMPLATE
*)dlgTemplate
,
385 parent
? GetHwndOf(parent
) : NULL
,
391 wxFAIL_MSG(wxT("Failed to create dialog. Incorrect DLGTEMPLATE?"));
393 wxLogSysError(wxT("Can't create dialog using memory template"));
398 #if !defined(__WXWINCE__)
399 // For some reason, the system menu is activated when we use the
400 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
401 if ( HasExtraStyle(wxWS_EX_CONTEXTHELP
) )
403 wxFrame
*winTop
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
406 wxIcon icon
= winTop
->GetIcon();
409 ::SendMessage(GetHwnd(), WM_SETICON
,
411 (LPARAM
)GetHiconOf(icon
));
415 #endif // !__WXWINCE__
417 // move the dialog to its initial position without forcing repainting
419 (void)MSWGetCreateWindowCoords(pos
, size
, x
, y
, w
, h
);
421 if ( x
== (int)CW_USEDEFAULT
)
423 // centre it on the screen - what else can we do?
424 wxSize sizeDpy
= wxGetDisplaySize();
426 x
= (sizeDpy
.x
- w
) / 2;
427 y
= (sizeDpy
.y
- h
) / 2;
430 #if !defined(__WXWINCE__) || defined(__WINCE_STANDARDSDK__)
431 if ( !::MoveWindow(GetHwnd(), x
, y
, w
, h
, FALSE
) )
433 wxLogLastError(wxT("MoveWindow"));
437 if ( !title
.empty() )
439 ::SetWindowText(GetHwnd(), title
.wx_str());
444 #ifdef __SMARTPHONE__
445 // Work around title non-display glitch
450 #endif // __WXMICROWIN__/!__WXMICROWIN__
453 bool wxTopLevelWindowMSW::CreateFrame(const wxString
& title
,
458 WXDWORD flags
= MSWGetCreateWindowFlags(&exflags
);
460 const wxSize sz
= IsAlwaysMaximized() ? wxDefaultSize
: size
;
463 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
464 exflags
|= WS_EX_LAYOUTRTL
;
467 return MSWCreate(MSWGetRegisteredClassName(),
468 title
.wx_str(), pos
, sz
, flags
, exflags
);
471 bool wxTopLevelWindowMSW::Create(wxWindow
*parent
,
473 const wxString
& title
,
477 const wxString
& name
)
479 wxSize sizeReal
= size
;
480 if ( !sizeReal
.IsFullySpecified() )
482 sizeReal
.SetDefaults(GetDefaultSize());
485 bool ret
= CreateBase(parent
, id
, pos
, sizeReal
, style
, name
);
489 wxTopLevelWindows
.Append(this);
492 parent
->AddChild(this);
494 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
496 // we have different dialog templates to allows creation of dialogs
497 // with & without captions under MSWindows, resizeable or not (but a
498 // resizeable dialog always has caption - otherwise it would look too
501 // we need 3 additional WORDs for dialog menu, class and title (as we
502 // don't use DS_SETFONT we don't need the fourth WORD for the font)
503 static const int dlgsize
= sizeof(DLGTEMPLATE
) + (sizeof(WORD
) * 3);
504 DLGTEMPLATE
*dlgTemplate
= (DLGTEMPLATE
*)malloc(dlgsize
);
505 memset(dlgTemplate
, 0, dlgsize
);
507 // these values are arbitrary, they won't be used normally anyhow
510 dlgTemplate
->cx
= 144;
511 dlgTemplate
->cy
= 75;
513 // reuse the code in MSWGetStyle() but correct the results slightly for
516 // NB: we need a temporary variable as we can't pass pointer to
517 // dwExtendedStyle directly, it's not aligned correctly for 64 bit
519 WXDWORD dwExtendedStyle
;
520 dlgTemplate
->style
= MSWGetStyle(style
, &dwExtendedStyle
);
521 dlgTemplate
->dwExtendedStyle
= dwExtendedStyle
;
523 // all dialogs are popups
524 dlgTemplate
->style
|= WS_POPUP
;
527 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
529 dlgTemplate
->dwExtendedStyle
|= WS_EX_LAYOUTRTL
;
532 // force 3D-look if necessary, it looks impossibly ugly otherwise
533 if ( style
& (wxRESIZE_BORDER
| wxCAPTION
) )
534 dlgTemplate
->style
|= DS_MODALFRAME
;
537 ret
= CreateDialog(dlgTemplate
, title
, pos
, sizeReal
);
542 ret
= CreateFrame(title
, pos
, sizeReal
);
546 if ( ret
&& !(GetWindowStyleFlag() & wxCLOSE_BOX
) )
548 EnableCloseButton(false);
552 // for standard dialogs the dialog manager generates WM_CHANGEUISTATE
553 // itself but for custom windows we have to do it ourselves in order to
554 // make the keyboard indicators (such as underlines for accelerators and
555 // focus rectangles) work under Win2k+
558 MSWUpdateUIState(UIS_INITIALIZE
);
561 // Note: if we include PocketPC in this test, dialogs can fail to show up,
562 // for example the text entry dialog in the dialogs sample. Problem with Maximise()?
563 #if defined(__WXWINCE__) && (defined(__SMARTPHONE__) || defined(__WINCE_STANDARDSDK__))
564 if ( ( style
& wxMAXIMIZE
) || IsAlwaysMaximized() )
570 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
571 SetRightMenu(); // to nothing for initialization
577 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
581 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
582 SHACTIVATEINFO
* info
= (SHACTIVATEINFO
*) m_activateInfo
;
584 m_activateInfo
= NULL
;
587 // after destroying an owned window, Windows activates the next top level
588 // window in Z order but it may be different from our owner (to reproduce
589 // this simply Alt-TAB to another application and back before closing the
590 // owned frame) whereas we always want to yield activation to our parent
591 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT
) )
593 wxWindow
*parent
= GetParent();
596 ::BringWindowToTop(GetHwndOf(parent
));
601 // ----------------------------------------------------------------------------
602 // wxTopLevelWindowMSW showing
603 // ----------------------------------------------------------------------------
605 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd
)
607 ::ShowWindow(GetHwnd(), nShowCmd
);
609 m_iconized
= nShowCmd
== SW_MINIMIZE
;
612 bool wxTopLevelWindowMSW::Show(bool show
)
614 // don't use wxWindow version as we want to call DoShowWindow() ourselves
615 if ( !wxWindowBase::Show(show
) )
621 if ( m_maximizeOnShow
)
624 nShowCmd
= SW_MAXIMIZE
;
626 // This is necessary, or no window appears
627 #if defined( __WINCE_STANDARDSDK__) || defined(__SMARTPHONE__)
628 DoShowWindow(SW_SHOW
);
631 m_maximizeOnShow
= false;
633 else if ( m_iconized
)
636 nShowCmd
= SW_MINIMIZE
;
640 // we shouldn't use SW_SHOW which also activates the window for
641 // tool frames (as they shouldn't steal focus from the main window)
642 // nor for the currently disabled windows as they would be enabled
644 if ( HasFlag(wxFRAME_TOOL_WINDOW
) || !IsEnabled() )
645 nShowCmd
= SW_SHOWNA
;
655 DoShowWindow(nShowCmd
);
657 #if defined(__WXWINCE__) && (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__))
658 // Addornments have to be added when the frame is the correct size
659 wxFrame
* frame
= wxDynamicCast(this, wxFrame
);
660 if (frame
&& frame
->GetMenuBar())
661 frame
->GetMenuBar()->AddAdornments(GetWindowStyleFlag());
664 // we only set pending size if we're maximized before being shown, now that
665 // we're shown we don't need it any more (it is reset in size event handler
666 // for child windows but we have to do it ourselves for this parent window)
667 m_pendingSize
= wxDefaultSize
;
672 // ----------------------------------------------------------------------------
673 // wxTopLevelWindowMSW maximize/minimize
674 // ----------------------------------------------------------------------------
676 void wxTopLevelWindowMSW::Maximize(bool maximize
)
680 // just maximize it directly
681 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
685 // we can't maximize the hidden frame because it shows it as well,
686 // so just remember that we should do it later in this case
687 m_maximizeOnShow
= maximize
;
689 // after calling Maximize() the client code expects to get the frame
690 // "real" size and doesn't want to know that, because of implementation
691 // details, the frame isn't really maximized yet but will be only once
692 // it's shown, so return our size as it will be then in this case
695 // we must only change pending size here, and not call SetSize()
696 // because otherwise Windows would think that this (full screen)
697 // size is the natural size for the frame and so would use it when
698 // the user clicks on "restore" title bar button instead of the
699 // correct initial frame size
701 // NB: unfortunately we don't know which display we're on yet so we
702 // have to use the default one
703 m_pendingSize
= wxGetClientDisplayRect().GetSize();
705 //else: can't do anything in this case, we don't have the old size
709 bool wxTopLevelWindowMSW::IsMaximized() const
711 return IsAlwaysMaximized() ||
712 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__) && !defined(__WINCE_STANDARDSDK__)
714 (::IsZoomed(GetHwnd()) != 0) ||
719 void wxTopLevelWindowMSW::Iconize(bool iconize
)
723 // change the window state immediately
724 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
728 // iconizing the window shouldn't show it so just remember that we need
729 // to become iconized when shown later
734 bool wxTopLevelWindowMSW::IsIconized() const
742 // don't use m_iconized, it may be briefly out of sync with the real state
743 // as it's only modified when we receive a WM_SIZE and we could be called
744 // from an event handler from one of the messages we receive before it,
746 return ::IsIconic(GetHwnd()) != 0;
750 void wxTopLevelWindowMSW::Restore()
752 DoShowWindow(SW_RESTORE
);
755 void wxTopLevelWindowMSW::SetLayoutDirection(wxLayoutDirection dir
)
757 if ( dir
== wxLayout_Default
)
758 dir
= wxTheApp
->GetLayoutDirection();
760 if ( dir
!= wxLayout_Default
)
761 wxTopLevelWindowBase::SetLayoutDirection(dir
);
764 // ----------------------------------------------------------------------------
765 // wxTopLevelWindowMSW geometry
766 // ----------------------------------------------------------------------------
770 void wxTopLevelWindowMSW::DoGetPosition(int *x
, int *y
) const
775 wp
.length
= sizeof(WINDOWPLACEMENT
);
776 if ( ::GetWindowPlacement(GetHwnd(), &wp
) )
778 RECT
& rc
= wp
.rcNormalPosition
;
780 // the position returned by GetWindowPlacement() is in workspace
781 // coordinates except for windows with WS_EX_TOOLWINDOW style
782 if ( !HasFlag(wxFRAME_TOOL_WINDOW
) )
784 // we must use the correct display for the translation as the
785 // task bar might be shown on one display but not the other one
786 int n
= wxDisplay::GetFromWindow(this);
787 wxDisplay
dpy(n
== wxNOT_FOUND
? 0 : n
);
788 const wxPoint ptOfs
= dpy
.GetClientArea().GetPosition() -
789 dpy
.GetGeometry().GetPosition();
803 wxLogLastError(wxT("GetWindowPlacement"));
807 wxTopLevelWindowBase::DoGetPosition(x
, y
);
810 void wxTopLevelWindowMSW::DoGetSize(int *width
, int *height
) const
815 wp
.length
= sizeof(WINDOWPLACEMENT
);
816 if ( ::GetWindowPlacement(GetHwnd(), &wp
) )
818 const RECT
& rc
= wp
.rcNormalPosition
;
821 *width
= rc
.right
- rc
.left
;
823 *height
= rc
.bottom
- rc
.top
;
828 wxLogLastError(wxT("GetWindowPlacement"));
832 wxTopLevelWindowBase::DoGetSize(width
, height
);
835 #endif // __WXWINCE__
837 // ----------------------------------------------------------------------------
838 // wxTopLevelWindowMSW fullscreen
839 // ----------------------------------------------------------------------------
841 bool wxTopLevelWindowMSW::ShowFullScreen(bool show
, long style
)
843 if ( show
== IsFullScreen() )
849 m_fsIsShowing
= show
;
855 // zap the frame borders
857 // save the 'normal' window style
858 m_fsOldWindowStyle
= GetWindowLong(GetHwnd(), GWL_STYLE
);
860 // save the old position, width & height, maximize state
861 m_fsOldSize
= GetRect();
862 m_fsIsMaximized
= IsMaximized();
864 // decide which window style flags to turn off
865 LONG newStyle
= m_fsOldWindowStyle
;
868 if (style
& wxFULLSCREEN_NOBORDER
)
870 offFlags
|= WS_BORDER
;
872 offFlags
|= WS_THICKFRAME
;
875 if (style
& wxFULLSCREEN_NOCAPTION
)
876 offFlags
|= WS_CAPTION
| WS_SYSMENU
;
878 newStyle
&= ~offFlags
;
880 // change our window style to be compatible with full-screen mode
881 ::SetWindowLong(GetHwnd(), GWL_STYLE
, newStyle
);
885 // resize to the size of the display containing us
886 int dpy
= wxDisplay::GetFromWindow(this);
887 if ( dpy
!= wxNOT_FOUND
)
889 rect
= wxDisplay(dpy
).GetGeometry();
891 else // fall back to the main desktop
892 #endif // wxUSE_DISPLAY
894 // resize to the size of the desktop
895 wxCopyRECTToRect(wxGetWindowRect(::GetDesktopWindow()), rect
);
897 // FIXME: size of the bottom menu (toolbar)
898 // should be taken in account
899 rect
.height
+= rect
.y
;
906 // now flush the window style cache and actually go full-screen
907 long flags
= SWP_FRAMECHANGED
;
909 // showing the frame full screen should also show it if it's still
913 // don't call wxWindow version to avoid flicker from calling
914 // ::ShowWindow() -- we're going to show the window at the correct
915 // location directly below -- but do call the wxWindowBase version
916 // to sync the internal m_isShown flag
917 wxWindowBase::Show();
919 flags
|= SWP_SHOWWINDOW
;
922 SetWindowPos(GetHwnd(), HWND_TOP
,
923 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
926 #if !defined(__HANDHELDPC__) && (defined(__WXWINCE__) && (_WIN32_WCE < 400))
927 ::SHFullScreen(GetHwnd(), SHFS_HIDETASKBAR
| SHFS_HIDESIPBUTTON
);
930 // finally send an event allowing the window to relayout itself &c
931 wxSizeEvent
event(rect
.GetSize(), GetId());
932 HandleWindowEvent(event
);
934 else // stop showing full screen
936 #if !defined(__HANDHELDPC__) && (defined(__WXWINCE__) && (_WIN32_WCE < 400))
937 ::SHFullScreen(GetHwnd(), SHFS_SHOWTASKBAR
| SHFS_SHOWSIPBUTTON
);
939 Maximize(m_fsIsMaximized
);
940 SetWindowLong(GetHwnd(),GWL_STYLE
, m_fsOldWindowStyle
);
941 SetWindowPos(GetHwnd(),HWND_TOP
,m_fsOldSize
.x
, m_fsOldSize
.y
,
942 m_fsOldSize
.width
, m_fsOldSize
.height
, SWP_FRAMECHANGED
);
948 // ----------------------------------------------------------------------------
949 // wxTopLevelWindowMSW misc
950 // ----------------------------------------------------------------------------
952 void wxTopLevelWindowMSW::SetTitle( const wxString
& title
)
957 wxString
wxTopLevelWindowMSW::GetTitle() const
962 bool wxTopLevelWindowMSW::DoSelectAndSetIcon(const wxIconBundle
& icons
,
967 const wxSize
size(::GetSystemMetrics(smX
), ::GetSystemMetrics(smY
));
969 const wxIcon icon
= icons
.GetIconOfExactSize(size
);
972 ::SendMessage(GetHwnd(), WM_SETICON
, i
, (LPARAM
)GetHiconOf(icon
));
979 void wxTopLevelWindowMSW::SetIcons(const wxIconBundle
& icons
)
981 wxTopLevelWindowBase::SetIcons(icons
);
983 if ( icons
.IsEmpty() )
985 // FIXME: SetIcons(wxNullIconBundle) should unset existing icons,
986 // but we currently don't do that
987 wxASSERT_MSG( m_icons
.IsEmpty(), "unsetting icons doesn't work" );
992 DoSelectAndSetIcon(icons
, SM_CXSMICON
, SM_CYSMICON
, ICON_SMALL
);
993 if ( DoSelectAndSetIcon(icons
, SM_CXICON
, SM_CYICON
, ICON_BIG
) )
998 wxFAIL_MSG( "icon bundle doesn't contain any suitable icon" );
1002 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable
)
1004 #if !defined(__WXMICROWIN__)
1005 // get system (a.k.a. window) menu
1006 HMENU hmenu
= GetSystemMenu(GetHwnd(), FALSE
/* get it */);
1009 // no system menu at all -- ok if we want to remove the close button
1010 // anyhow, but bad if we want to show it
1014 // enabling/disabling the close item from it also automatically
1015 // disables/enables the close title bar button
1016 if ( ::EnableMenuItem(hmenu
, SC_CLOSE
,
1018 (enable
? MF_ENABLED
: MF_GRAYED
)) == -1 )
1020 wxLogLastError(wxT("EnableMenuItem(SC_CLOSE)"));
1025 // update appearance immediately
1026 if ( !::DrawMenuBar(GetHwnd()) )
1028 wxLogLastError(wxT("DrawMenuBar"));
1031 #endif // !__WXMICROWIN__
1038 bool wxTopLevelWindowMSW::SetShape(const wxRegion
& region
)
1040 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), false,
1041 wxT("Shaped windows must be created with the wxFRAME_SHAPED style."));
1043 // The empty region signifies that the shape should be removed from the
1045 if ( region
.IsEmpty() )
1047 if (::SetWindowRgn(GetHwnd(), NULL
, TRUE
) == 0)
1049 wxLogLastError(wxT("SetWindowRgn"));
1055 // Windows takes ownership of the region, so
1056 // we'll have to make a copy of the region to give to it.
1057 DWORD noBytes
= ::GetRegionData(GetHrgnOf(region
), 0, NULL
);
1058 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
1059 ::GetRegionData(GetHrgnOf(region
), noBytes
, rgnData
);
1060 HRGN hrgn
= ::ExtCreateRegion(NULL
, noBytes
, rgnData
);
1061 delete[] (char*) rgnData
;
1063 // SetWindowRgn expects the region to be in coordinants
1064 // relative to the window, not the client area. Figure
1065 // out the offset, if any.
1067 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1068 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1069 ::GetClientRect(GetHwnd(), &rect
);
1070 ::AdjustWindowRectEx(&rect
, dwStyle
, ::GetMenu(GetHwnd()) != NULL
, dwExStyle
);
1071 ::OffsetRgn(hrgn
, -rect
.left
, -rect
.top
);
1073 // Now call the shape API with the new region.
1074 if (::SetWindowRgn(GetHwnd(), hrgn
, TRUE
) == 0)
1076 wxLogLastError(wxT("SetWindowRgn"));
1082 #endif // !__WXWINCE__
1084 void wxTopLevelWindowMSW::RequestUserAttention(int flags
)
1086 // check if we can use FlashWindowEx(): unfortunately a simple test for
1087 // FLASHW_STOP doesn't work because MSVC6 headers do #define it but don't
1088 // provide FlashWindowEx() declaration, so try to detect whether we have
1089 // real headers for WINVER 0x0500 by checking for existence of a symbol not
1090 // declated in MSVC6 header
1091 #if defined(FLASHW_STOP) && defined(VK_XBUTTON1) && wxUSE_DYNLIB_CLASS
1092 // available in the headers, check if it is supported by the system
1093 typedef BOOL (WINAPI
*FlashWindowEx_t
)(FLASHWINFO
*pfwi
);
1094 static FlashWindowEx_t s_pfnFlashWindowEx
= NULL
;
1095 if ( !s_pfnFlashWindowEx
)
1097 wxDynamicLibrary
dllUser32(wxT("user32.dll"));
1098 s_pfnFlashWindowEx
= (FlashWindowEx_t
)
1099 dllUser32
.GetSymbol(wxT("FlashWindowEx"));
1101 // we can safely unload user32.dll here, it's going to remain loaded as
1102 // long as the program is running anyhow
1105 if ( s_pfnFlashWindowEx
)
1107 WinStruct
<FLASHWINFO
> fwi
;
1108 fwi
.hwnd
= GetHwnd();
1109 fwi
.dwFlags
= FLASHW_ALL
;
1110 if ( flags
& wxUSER_ATTENTION_INFO
)
1112 // just flash a few times
1115 else // wxUSER_ATTENTION_ERROR
1117 // flash until the user notices it
1118 fwi
.dwFlags
|= FLASHW_TIMERNOFG
;
1121 s_pfnFlashWindowEx(&fwi
);
1123 else // FlashWindowEx() not available
1124 #endif // FlashWindowEx() defined
1128 ::FlashWindow(GetHwnd(), TRUE
);
1129 #endif // __WXWINCE__
1133 // ---------------------------------------------------------------------------
1135 bool wxTopLevelWindowMSW::SetTransparent(wxByte alpha
)
1137 #if wxUSE_DYNLIB_CLASS
1138 typedef DWORD (WINAPI
*PSETLAYEREDWINDOWATTR
)(HWND
, DWORD
, BYTE
, DWORD
);
1139 static PSETLAYEREDWINDOWATTR
1140 pSetLayeredWindowAttributes
= (PSETLAYEREDWINDOWATTR
)-1;
1142 if ( pSetLayeredWindowAttributes
== (PSETLAYEREDWINDOWATTR
)-1 )
1144 wxDynamicLibrary
dllUser32(wxT("user32.dll"));
1146 // use RawGetSymbol() and not GetSymbol() to avoid error messages under
1147 // Windows 95: there is nothing the user can do about this anyhow
1148 pSetLayeredWindowAttributes
= (PSETLAYEREDWINDOWATTR
)
1149 dllUser32
.RawGetSymbol(wxT("SetLayeredWindowAttributes"));
1151 // it's ok to destroy dllUser32 here, we link statically to user32.dll
1152 // anyhow so it won't be unloaded
1155 if ( !pSetLayeredWindowAttributes
)
1157 #endif // wxUSE_DYNLIB_CLASS
1159 LONG exstyle
= GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1161 // if setting alpha to fully opaque then turn off the layered style
1164 SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exstyle
& ~WS_EX_LAYERED
);
1169 #if wxUSE_DYNLIB_CLASS
1170 // Otherwise, set the layered style if needed and set the alpha value
1171 if ((exstyle
& WS_EX_LAYERED
) == 0 )
1172 SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exstyle
| WS_EX_LAYERED
);
1174 if ( pSetLayeredWindowAttributes(GetHwnd(), 0, (BYTE
)alpha
, LWA_ALPHA
) )
1176 #endif // wxUSE_DYNLIB_CLASS
1181 bool wxTopLevelWindowMSW::CanSetTransparent()
1183 // The API is available on win2k and above
1185 static int os_type
= -1;
1186 static int ver_major
= -1;
1189 os_type
= ::wxGetOsVersion(&ver_major
);
1191 return (os_type
== wxOS_WINDOWS_NT
&& ver_major
>= 5);
1195 void wxTopLevelWindowMSW::DoFreeze()
1197 // do nothing: freezing toplevel window causes paint and mouse events
1198 // to go through it any TLWs under it, so the best we can do is to freeze
1199 // all children -- and wxWindowBase::Freeze() does that
1202 void wxTopLevelWindowMSW::DoThaw()
1204 // intentionally empty -- see DoFreeze()
1208 // ----------------------------------------------------------------------------
1209 // wxTopLevelWindow event handling
1210 // ----------------------------------------------------------------------------
1212 // Default activation behaviour - set the focus for the first child
1214 void wxTopLevelWindowMSW::OnActivate(wxActivateEvent
& event
)
1216 if ( event
.GetActive() )
1218 // restore focus to the child which was last focused unless we already
1220 wxLogTrace(wxT("focus"), wxT("wxTLW %p activated."), m_hWnd
);
1222 wxWindow
*winFocus
= FindFocus();
1223 if ( !winFocus
|| wxGetTopLevelParent(winFocus
) != this )
1225 wxWindow
*parent
= m_winLastFocused
? m_winLastFocused
->GetParent()
1232 wxSetFocusToChild(parent
, &m_winLastFocused
);
1235 else // deactivating
1237 // remember the last focused child if it is our child
1238 m_winLastFocused
= FindFocus();
1240 if ( m_winLastFocused
)
1242 // let it know that it doesn't have focus any more
1243 // But this will already be done via WM_KILLFOCUS, so we'll get two kill
1244 // focus events if we call it explicitly.
1245 // m_winLastFocused->HandleKillFocus((WXHWND)NULL);
1247 // and don't remember it if it's a child from some other frame
1248 if ( wxGetTopLevelParent(m_winLastFocused
) != this )
1250 m_winLastFocused
= NULL
;
1254 wxLogTrace(wxT("focus"),
1255 wxT("wxTLW %p deactivated, last focused: %p."),
1257 m_winLastFocused
? GetHwndOf(m_winLastFocused
) : NULL
);
1263 // the DialogProc for all wxWidgets dialogs
1264 LONG APIENTRY _EXPORT
1265 wxDlgProc(HWND hDlg
,
1267 WPARAM
WXUNUSED(wParam
),
1268 LPARAM
WXUNUSED(lParam
))
1274 // under CE, add a "Ok" button in the dialog title bar and make it full
1277 // TODO: find the window for this HWND, and take into account
1278 // wxMAXIMIZE and wxCLOSE_BOX. For now, assume both are present.
1280 // Standard SDK doesn't have aygshell.dll: see
1281 // include/wx/msw/wince/libraries.h
1282 #if defined(__WXWINCE__) && !defined(__WINCE_STANDARDSDK__) && !defined(__HANDHELDPC__)
1283 SHINITDLGINFO shidi
;
1284 shidi
.dwMask
= SHIDIM_FLAGS
;
1285 shidi
.dwFlags
= SHIDIF_SIZEDLG
// take account of the SIP or menubar
1286 #ifndef __SMARTPHONE__
1291 SHInitDialog( &shidi
);
1292 #else // no SHInitDialog()
1295 // for WM_INITDIALOG, returning TRUE tells system to set focus to
1296 // the first control in the dialog box, but as we set the focus
1297 // ourselves, we return FALSE for it as well
1302 // for almost all messages, returning FALSE means that we didn't process
1307 // ============================================================================
1308 // wxTLWHiddenParentModule implementation
1309 // ============================================================================
1311 HWND
wxTLWHiddenParentModule::ms_hwnd
= NULL
;
1313 const wxChar
*wxTLWHiddenParentModule::ms_className
= NULL
;
1315 bool wxTLWHiddenParentModule::OnInit()
1318 ms_className
= NULL
;
1323 void wxTLWHiddenParentModule::OnExit()
1327 if ( !::DestroyWindow(ms_hwnd
) )
1329 wxLogLastError(wxT("DestroyWindow(hidden TLW parent)"));
1337 if ( !::UnregisterClass(ms_className
, wxGetInstance()) )
1339 wxLogLastError(wxT("UnregisterClass(\"wxTLWHiddenParent\")"));
1342 ms_className
= NULL
;
1347 HWND
wxTLWHiddenParentModule::GetHWND()
1351 if ( !ms_className
)
1353 static const wxChar
*HIDDEN_PARENT_CLASS
= wxT("wxTLWHiddenParent");
1356 wxZeroMemory(wndclass
);
1358 wndclass
.lpfnWndProc
= DefWindowProc
;
1359 wndclass
.hInstance
= wxGetInstance();
1360 wndclass
.lpszClassName
= HIDDEN_PARENT_CLASS
;
1362 if ( !::RegisterClass(&wndclass
) )
1364 wxLogLastError(wxT("RegisterClass(\"wxTLWHiddenParent\")"));
1368 ms_className
= HIDDEN_PARENT_CLASS
;
1372 ms_hwnd
= ::CreateWindow(ms_className
, wxEmptyString
, 0, 0, 0, 0, 0, NULL
,
1373 (HMENU
)NULL
, wxGetInstance(), NULL
);
1376 wxLogLastError(wxT("CreateWindow(hidden TLW parent)"));