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( _T("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 wxWindow
*parent
= GetParent();
378 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
379 // app window as parent - this avoids creating modal dialogs without
381 if ( !parent
&& !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
) )
383 parent
= wxTheApp
->GetTopWindow();
387 // don't use transient windows as parents, this is dangerous as it
388 // can lead to a crash if the parent is destroyed before the child
390 // also don't use the window which is currently hidden as then the
391 // dialog would be hidden as well
392 if ( (parent
->GetExtraStyle() & wxWS_EX_TRANSIENT
) ||
400 m_hWnd
= (WXHWND
)::CreateDialogIndirect
403 (DLGTEMPLATE
*)dlgTemplate
,
404 parent
? GetHwndOf(parent
) : NULL
,
410 wxFAIL_MSG(wxT("Failed to create dialog. Incorrect DLGTEMPLATE?"));
412 wxLogSysError(wxT("Can't create dialog using memory template"));
417 #if !defined(__WXWINCE__)
418 // For some reason, the system menu is activated when we use the
419 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
420 if ( HasExtraStyle(wxWS_EX_CONTEXTHELP
) )
422 wxFrame
*winTop
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
425 wxIcon icon
= winTop
->GetIcon();
428 ::SendMessage(GetHwnd(), WM_SETICON
,
430 (LPARAM
)GetHiconOf(icon
));
434 #endif // !__WXWINCE__
436 // move the dialog to its initial position without forcing repainting
438 (void)MSWGetCreateWindowCoords(pos
, size
, x
, y
, w
, h
);
440 if ( x
== (int)CW_USEDEFAULT
)
442 // centre it on the screen - what else can we do?
443 wxSize sizeDpy
= wxGetDisplaySize();
445 x
= (sizeDpy
.x
- w
) / 2;
446 y
= (sizeDpy
.y
- h
) / 2;
449 #if !defined(__WXWINCE__) || defined(__WINCE_STANDARDSDK__)
450 if ( !::MoveWindow(GetHwnd(), x
, y
, w
, h
, FALSE
) )
452 wxLogLastError(wxT("MoveWindow"));
456 if ( !title
.empty() )
458 ::SetWindowText(GetHwnd(), title
.wx_str());
463 #ifdef __SMARTPHONE__
464 // Work around title non-display glitch
469 #endif // __WXMICROWIN__/!__WXMICROWIN__
472 bool wxTopLevelWindowMSW::CreateFrame(const wxString
& title
,
477 WXDWORD flags
= MSWGetCreateWindowFlags(&exflags
);
479 const wxSize sz
= IsAlwaysMaximized() ? wxDefaultSize
: size
;
482 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
483 exflags
|= WS_EX_LAYOUTRTL
;
486 return MSWCreate(MSWGetRegisteredClassName(),
487 title
.wx_str(), pos
, sz
, flags
, exflags
);
490 bool wxTopLevelWindowMSW::Create(wxWindow
*parent
,
492 const wxString
& title
,
496 const wxString
& name
)
498 bool ret
wxDUMMY_INITIALIZE(false);
500 wxSize sizeReal
= size
;
501 if ( !sizeReal
.IsFullySpecified() )
503 sizeReal
.SetDefaults(GetDefaultSize());
506 m_windowStyle
= style
;
510 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
512 wxTopLevelWindows
.Append(this);
515 parent
->AddChild(this);
517 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
519 // we have different dialog templates to allows creation of dialogs
520 // with & without captions under MSWindows, resizeable or not (but a
521 // resizeable dialog always has caption - otherwise it would look too
524 // we need 3 additional WORDs for dialog menu, class and title (as we
525 // don't use DS_SETFONT we don't need the fourth WORD for the font)
526 static const int dlgsize
= sizeof(DLGTEMPLATE
) + (sizeof(WORD
) * 3);
527 DLGTEMPLATE
*dlgTemplate
= (DLGTEMPLATE
*)malloc(dlgsize
);
528 memset(dlgTemplate
, 0, dlgsize
);
530 // these values are arbitrary, they won't be used normally anyhow
533 dlgTemplate
->cx
= 144;
534 dlgTemplate
->cy
= 75;
536 // reuse the code in MSWGetStyle() but correct the results slightly for
539 // NB: we need a temporary variable as we can't pass pointer to
540 // dwExtendedStyle directly, it's not aligned correctly for 64 bit
542 WXDWORD dwExtendedStyle
;
543 dlgTemplate
->style
= MSWGetStyle(style
, &dwExtendedStyle
);
544 dlgTemplate
->dwExtendedStyle
= dwExtendedStyle
;
546 // all dialogs are popups
547 dlgTemplate
->style
|= WS_POPUP
;
550 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
552 dlgTemplate
->dwExtendedStyle
|= WS_EX_LAYOUTRTL
;
555 // force 3D-look if necessary, it looks impossibly ugly otherwise
556 if ( style
& (wxRESIZE_BORDER
| wxCAPTION
) )
557 dlgTemplate
->style
|= DS_MODALFRAME
;
560 ret
= CreateDialog(dlgTemplate
, title
, pos
, sizeReal
);
565 ret
= CreateFrame(title
, pos
, sizeReal
);
569 if ( ret
&& !(GetWindowStyleFlag() & wxCLOSE_BOX
) )
571 EnableCloseButton(false);
575 // for standard dialogs the dialog manager generates WM_CHANGEUISTATE
576 // itself but for custom windows we have to do it ourselves in order to
577 // make the keyboard indicators (such as underlines for accelerators and
578 // focus rectangles) work under Win2k+
581 MSWUpdateUIState(UIS_INITIALIZE
);
584 // Note: if we include PocketPC in this test, dialogs can fail to show up,
585 // for example the text entry dialog in the dialogs sample. Problem with Maximise()?
586 #if defined(__WXWINCE__) && (defined(__SMARTPHONE__) || defined(__WINCE_STANDARDSDK__))
587 if ( ( style
& wxMAXIMIZE
) || IsAlwaysMaximized() )
593 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
594 SetRightMenu(); // to nothing for initialization
600 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
602 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
603 SHACTIVATEINFO
* info
= (SHACTIVATEINFO
*) m_activateInfo
;
605 m_activateInfo
= NULL
;
608 // after destroying an owned window, Windows activates the next top level
609 // window in Z order but it may be different from our owner (to reproduce
610 // this simply Alt-TAB to another application and back before closing the
611 // owned frame) whereas we always want to yield activation to our parent
612 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT
) )
614 wxWindow
*parent
= GetParent();
617 ::BringWindowToTop(GetHwndOf(parent
));
622 // ----------------------------------------------------------------------------
623 // wxTopLevelWindowMSW showing
624 // ----------------------------------------------------------------------------
626 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd
)
628 ::ShowWindow(GetHwnd(), nShowCmd
);
630 m_iconized
= nShowCmd
== SW_MINIMIZE
;
633 bool wxTopLevelWindowMSW::Show(bool show
)
635 // don't use wxWindow version as we want to call DoShowWindow() ourselves
636 if ( !wxWindowBase::Show(show
) )
642 if ( m_maximizeOnShow
)
645 nShowCmd
= SW_MAXIMIZE
;
647 // This is necessary, or no window appears
648 #if defined( __WINCE_STANDARDSDK__) || defined(__SMARTPHONE__)
649 DoShowWindow(SW_SHOW
);
652 m_maximizeOnShow
= false;
656 // we shouldn't use SW_SHOW which also activates the window for
657 // tool frames (as they shouldn't steal focus from the main window)
658 // nor for the currently disabled windows as they would be enabled
660 if ( HasFlag(wxFRAME_TOOL_WINDOW
) || !IsEnabled() )
661 nShowCmd
= SW_SHOWNA
;
671 DoShowWindow(nShowCmd
);
673 #if defined(__WXWINCE__) && (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__))
674 // Addornments have to be added when the frame is the correct size
675 wxFrame
* frame
= wxDynamicCast(this, wxFrame
);
676 if (frame
&& frame
->GetMenuBar())
677 frame
->GetMenuBar()->AddAdornments(GetWindowStyleFlag());
680 // we only set pending size if we're maximized before being shown, now that
681 // we're shown we don't need it any more (it is reset in size event handler
682 // for child windows but we have to do it ourselves for this parent window)
683 m_pendingSize
= wxDefaultSize
;
688 // ----------------------------------------------------------------------------
689 // wxTopLevelWindowMSW maximize/minimize
690 // ----------------------------------------------------------------------------
692 void wxTopLevelWindowMSW::Maximize(bool maximize
)
696 // just maximize it directly
697 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
701 // we can't maximize the hidden frame because it shows it as well,
702 // so just remember that we should do it later in this case
703 m_maximizeOnShow
= maximize
;
705 // after calling Maximize() the client code expects to get the frame
706 // "real" size and doesn't want to know that, because of implementation
707 // details, the frame isn't really maximized yet but will be only once
708 // it's shown, so return our size as it will be then in this case
711 // we must only change pending size here, and not call SetSize()
712 // because otherwise Windows would think that this (full screen)
713 // size is the natural size for the frame and so would use it when
714 // the user clicks on "restore" title bar button instead of the
715 // correct initial frame size
717 // NB: unfortunately we don't know which display we're on yet so we
718 // have to use the default one
719 m_pendingSize
= wxGetClientDisplayRect().GetSize();
721 //else: can't do anything in this case, we don't have the old size
725 bool wxTopLevelWindowMSW::IsMaximized() const
727 return IsAlwaysMaximized() ||
728 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__) && !defined(__WINCE_STANDARDSDK__)
730 (::IsZoomed(GetHwnd()) != 0) ||
735 void wxTopLevelWindowMSW::Iconize(bool iconize
)
737 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
740 bool wxTopLevelWindowMSW::IsIconized() const
745 // don't use m_iconized, it may be briefly out of sync with the real state
746 // as it's only modified when we receive a WM_SIZE and we could be called
747 // from an event handler from one of the messages we receive before it,
749 return ::IsIconic(GetHwnd()) != 0;
753 void wxTopLevelWindowMSW::Restore()
755 DoShowWindow(SW_RESTORE
);
758 void wxTopLevelWindowMSW::SetLayoutDirection(wxLayoutDirection dir
)
760 if ( dir
== wxLayout_Default
)
761 dir
= wxTheApp
->GetLayoutDirection();
763 if ( dir
!= wxLayout_Default
)
764 wxTopLevelWindowBase::SetLayoutDirection(dir
);
767 // ----------------------------------------------------------------------------
768 // wxTopLevelWindowMSW geometry
769 // ----------------------------------------------------------------------------
773 void wxTopLevelWindowMSW::DoGetPosition(int *x
, int *y
) const
778 wp
.length
= sizeof(WINDOWPLACEMENT
);
779 if ( ::GetWindowPlacement(GetHwnd(), &wp
) )
781 RECT
& rc
= wp
.rcNormalPosition
;
783 // the position returned by GetWindowPlacement() is in workspace
784 // coordinates except for windows with WS_EX_TOOLWINDOW style
785 if ( !HasFlag(wxFRAME_TOOL_WINDOW
) )
787 // we must use the correct display for the translation as the
788 // task bar might be shown on one display but not the other one
789 int n
= wxDisplay::GetFromWindow(this);
790 wxDisplay
dpy(n
== wxNOT_FOUND
? 0 : n
);
791 const wxPoint ptOfs
= dpy
.GetClientArea().GetPosition() -
792 dpy
.GetGeometry().GetPosition();
806 wxLogLastError(_T("GetWindowPlacement"));
810 wxTopLevelWindowBase::DoGetPosition(x
, y
);
813 void wxTopLevelWindowMSW::DoGetSize(int *width
, int *height
) const
818 wp
.length
= sizeof(WINDOWPLACEMENT
);
819 if ( ::GetWindowPlacement(GetHwnd(), &wp
) )
821 const RECT
& rc
= wp
.rcNormalPosition
;
824 *width
= rc
.right
- rc
.left
;
826 *height
= rc
.bottom
- rc
.top
;
831 wxLogLastError(_T("GetWindowPlacement"));
835 wxTopLevelWindowBase::DoGetSize(width
, height
);
838 #endif // __WXWINCE__
840 // ----------------------------------------------------------------------------
841 // wxTopLevelWindowMSW fullscreen
842 // ----------------------------------------------------------------------------
844 bool wxTopLevelWindowMSW::ShowFullScreen(bool show
, long style
)
846 if ( show
== IsFullScreen() )
852 m_fsIsShowing
= show
;
858 // zap the frame borders
860 // save the 'normal' window style
861 m_fsOldWindowStyle
= GetWindowLong(GetHwnd(), GWL_STYLE
);
863 // save the old position, width & height, maximize state
864 m_fsOldSize
= GetRect();
865 m_fsIsMaximized
= IsMaximized();
867 // decide which window style flags to turn off
868 LONG newStyle
= m_fsOldWindowStyle
;
871 if (style
& wxFULLSCREEN_NOBORDER
)
873 offFlags
|= WS_BORDER
;
875 offFlags
|= WS_THICKFRAME
;
878 if (style
& wxFULLSCREEN_NOCAPTION
)
879 offFlags
|= WS_CAPTION
| WS_SYSMENU
;
881 newStyle
&= ~offFlags
;
883 // change our window style to be compatible with full-screen mode
884 ::SetWindowLong(GetHwnd(), GWL_STYLE
, newStyle
);
888 // resize to the size of the display containing us
889 int dpy
= wxDisplay::GetFromWindow(this);
890 if ( dpy
!= wxNOT_FOUND
)
892 rect
= wxDisplay(dpy
).GetGeometry();
894 else // fall back to the main desktop
895 #endif // wxUSE_DISPLAY
897 // resize to the size of the desktop
898 wxCopyRECTToRect(wxGetWindowRect(::GetDesktopWindow()), rect
);
900 // FIXME: size of the bottom menu (toolbar)
901 // should be taken in account
902 rect
.height
+= rect
.y
;
909 // now flush the window style cache and actually go full-screen
910 long flags
= SWP_FRAMECHANGED
;
912 // showing the frame full screen should also show it if it's still
916 // don't call wxWindow version to avoid flicker from calling
917 // ::ShowWindow() -- we're going to show the window at the correct
918 // location directly below -- but do call the wxWindowBase version
919 // to sync the internal m_isShown flag
920 wxWindowBase::Show();
922 flags
|= SWP_SHOWWINDOW
;
925 SetWindowPos(GetHwnd(), HWND_TOP
,
926 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
929 #if !defined(__HANDHELDPC__) && (defined(__WXWINCE__) && (_WIN32_WCE < 400))
930 ::SHFullScreen(GetHwnd(), SHFS_HIDETASKBAR
| SHFS_HIDESIPBUTTON
);
933 // finally send an event allowing the window to relayout itself &c
934 wxSizeEvent
event(rect
.GetSize(), GetId());
935 HandleWindowEvent(event
);
937 else // stop showing full screen
939 #if !defined(__HANDHELDPC__) && (defined(__WXWINCE__) && (_WIN32_WCE < 400))
940 ::SHFullScreen(GetHwnd(), SHFS_SHOWTASKBAR
| SHFS_SHOWSIPBUTTON
);
942 Maximize(m_fsIsMaximized
);
943 SetWindowLong(GetHwnd(),GWL_STYLE
, m_fsOldWindowStyle
);
944 SetWindowPos(GetHwnd(),HWND_TOP
,m_fsOldSize
.x
, m_fsOldSize
.y
,
945 m_fsOldSize
.width
, m_fsOldSize
.height
, SWP_FRAMECHANGED
);
951 // ----------------------------------------------------------------------------
952 // wxTopLevelWindowMSW misc
953 // ----------------------------------------------------------------------------
955 void wxTopLevelWindowMSW::SetTitle( const wxString
& title
)
960 wxString
wxTopLevelWindowMSW::GetTitle() const
965 bool wxTopLevelWindowMSW::DoSelectAndSetIcon(const wxIconBundle
& icons
,
970 const wxSize
size(::GetSystemMetrics(smX
), ::GetSystemMetrics(smY
));
972 const wxIcon icon
= icons
.GetIconOfExactSize(size
);
975 ::SendMessage(GetHwnd(), WM_SETICON
, i
, (LPARAM
)GetHiconOf(icon
));
982 void wxTopLevelWindowMSW::SetIcons(const wxIconBundle
& icons
)
984 wxTopLevelWindowBase::SetIcons(icons
);
986 if ( icons
.IsEmpty() )
988 // FIXME: SetIcons(wxNullIconBundle) should unset existing icons,
989 // but we currently don't do that
990 wxASSERT_MSG( m_icons
.IsEmpty(), "unsetting icons doesn't work" );
994 if ( !DoSelectAndSetIcon(icons
, SM_CXSMICON
, SM_CYSMICON
, ICON_SMALL
) &&
995 !DoSelectAndSetIcon(icons
, SM_CXICON
, SM_CYICON
, ICON_BIG
) )
997 wxFAIL_MSG( "icon bundle doesn't contain any suitable icon" );
1001 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable
)
1003 #if !defined(__WXMICROWIN__)
1004 // get system (a.k.a. window) menu
1005 HMENU hmenu
= GetSystemMenu(GetHwnd(), FALSE
/* get it */);
1008 // no system menu at all -- ok if we want to remove the close button
1009 // anyhow, but bad if we want to show it
1013 // enabling/disabling the close item from it also automatically
1014 // disables/enables the close title bar button
1015 if ( ::EnableMenuItem(hmenu
, SC_CLOSE
,
1017 (enable
? MF_ENABLED
: MF_GRAYED
)) == -1 )
1019 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
1024 // update appearance immediately
1025 if ( !::DrawMenuBar(GetHwnd()) )
1027 wxLogLastError(_T("DrawMenuBar"));
1030 #endif // !__WXMICROWIN__
1037 bool wxTopLevelWindowMSW::SetShape(const wxRegion
& region
)
1039 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), false,
1040 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1042 // The empty region signifies that the shape should be removed from the
1044 if ( region
.IsEmpty() )
1046 if (::SetWindowRgn(GetHwnd(), NULL
, TRUE
) == 0)
1048 wxLogLastError(_T("SetWindowRgn"));
1054 // Windows takes ownership of the region, so
1055 // we'll have to make a copy of the region to give to it.
1056 DWORD noBytes
= ::GetRegionData(GetHrgnOf(region
), 0, NULL
);
1057 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
1058 ::GetRegionData(GetHrgnOf(region
), noBytes
, rgnData
);
1059 HRGN hrgn
= ::ExtCreateRegion(NULL
, noBytes
, rgnData
);
1060 delete[] (char*) rgnData
;
1062 // SetWindowRgn expects the region to be in coordinants
1063 // relative to the window, not the client area. Figure
1064 // out the offset, if any.
1066 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1067 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1068 ::GetClientRect(GetHwnd(), &rect
);
1069 ::AdjustWindowRectEx(&rect
, dwStyle
, FALSE
, dwExStyle
);
1070 ::OffsetRgn(hrgn
, -rect
.left
, -rect
.top
);
1072 // Now call the shape API with the new region.
1073 if (::SetWindowRgn(GetHwnd(), hrgn
, TRUE
) == 0)
1075 wxLogLastError(_T("SetWindowRgn"));
1081 #endif // !__WXWINCE__
1083 void wxTopLevelWindowMSW::RequestUserAttention(int flags
)
1085 // check if we can use FlashWindowEx(): unfortunately a simple test for
1086 // FLASHW_STOP doesn't work because MSVC6 headers do #define it but don't
1087 // provide FlashWindowEx() declaration, so try to detect whether we have
1088 // real headers for WINVER 0x0500 by checking for existence of a symbol not
1089 // declated in MSVC6 header
1090 #if defined(FLASHW_STOP) && defined(VK_XBUTTON1) && wxUSE_DYNLIB_CLASS
1091 // available in the headers, check if it is supported by the system
1092 typedef BOOL (WINAPI
*FlashWindowEx_t
)(FLASHWINFO
*pfwi
);
1093 static FlashWindowEx_t s_pfnFlashWindowEx
= NULL
;
1094 if ( !s_pfnFlashWindowEx
)
1096 wxDynamicLibrary
dllUser32(_T("user32.dll"));
1097 s_pfnFlashWindowEx
= (FlashWindowEx_t
)
1098 dllUser32
.GetSymbol(_T("FlashWindowEx"));
1100 // we can safely unload user32.dll here, it's going to remain loaded as
1101 // long as the program is running anyhow
1104 if ( s_pfnFlashWindowEx
)
1106 WinStruct
<FLASHWINFO
> fwi
;
1107 fwi
.hwnd
= GetHwnd();
1108 fwi
.dwFlags
= FLASHW_ALL
;
1109 if ( flags
& wxUSER_ATTENTION_INFO
)
1111 // just flash a few times
1114 else // wxUSER_ATTENTION_ERROR
1116 // flash until the user notices it
1117 fwi
.dwFlags
|= FLASHW_TIMERNOFG
;
1120 s_pfnFlashWindowEx(&fwi
);
1122 else // FlashWindowEx() not available
1123 #endif // FlashWindowEx() defined
1127 ::FlashWindow(GetHwnd(), TRUE
);
1128 #endif // __WXWINCE__
1132 // ---------------------------------------------------------------------------
1134 bool wxTopLevelWindowMSW::SetTransparent(wxByte alpha
)
1136 #if wxUSE_DYNLIB_CLASS
1137 typedef DWORD (WINAPI
*PSETLAYEREDWINDOWATTR
)(HWND
, DWORD
, BYTE
, DWORD
);
1138 static PSETLAYEREDWINDOWATTR
1139 pSetLayeredWindowAttributes
= (PSETLAYEREDWINDOWATTR
)-1;
1141 if ( pSetLayeredWindowAttributes
== (PSETLAYEREDWINDOWATTR
)-1 )
1143 wxDynamicLibrary
dllUser32(_T("user32.dll"));
1145 // use RawGetSymbol() and not GetSymbol() to avoid error messages under
1146 // Windows 95: there is nothing the user can do about this anyhow
1147 pSetLayeredWindowAttributes
= (PSETLAYEREDWINDOWATTR
)
1148 dllUser32
.RawGetSymbol(wxT("SetLayeredWindowAttributes"));
1150 // it's ok to destroy dllUser32 here, we link statically to user32.dll
1151 // anyhow so it won't be unloaded
1154 if ( !pSetLayeredWindowAttributes
)
1156 #endif // wxUSE_DYNLIB_CLASS
1158 LONG exstyle
= GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1160 // if setting alpha to fully opaque then turn off the layered style
1163 SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exstyle
& ~WS_EX_LAYERED
);
1168 #if wxUSE_DYNLIB_CLASS
1169 // Otherwise, set the layered style if needed and set the alpha value
1170 if ((exstyle
& WS_EX_LAYERED
) == 0 )
1171 SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exstyle
| WS_EX_LAYERED
);
1173 if ( pSetLayeredWindowAttributes(GetHwnd(), 0, (BYTE
)alpha
, LWA_ALPHA
) )
1175 #endif // wxUSE_DYNLIB_CLASS
1180 bool wxTopLevelWindowMSW::CanSetTransparent()
1182 // The API is available on win2k and above
1184 static int os_type
= -1;
1185 static int ver_major
= -1;
1188 os_type
= ::wxGetOsVersion(&ver_major
);
1190 return (os_type
== wxOS_WINDOWS_NT
&& ver_major
>= 5);
1194 void wxTopLevelWindowMSW::DoFreeze()
1196 // do nothing: freezing toplevel window causes paint and mouse events
1197 // to go through it any TLWs under it, so the best we can do is to freeze
1198 // all children -- and wxWindowBase::Freeze() does that
1201 void wxTopLevelWindowMSW::DoThaw()
1203 // intentionally empty -- see DoFreeze()
1207 // ----------------------------------------------------------------------------
1208 // wxTopLevelWindow event handling
1209 // ----------------------------------------------------------------------------
1211 // Default activation behaviour - set the focus for the first child
1213 void wxTopLevelWindowMSW::OnActivate(wxActivateEvent
& event
)
1215 if ( event
.GetActive() )
1217 // restore focus to the child which was last focused unless we already
1219 wxLogTrace(_T("focus"), _T("wxTLW %p activated."), m_hWnd
);
1221 wxWindow
*winFocus
= FindFocus();
1222 if ( !winFocus
|| wxGetTopLevelParent(winFocus
) != this )
1224 wxWindow
*parent
= m_winLastFocused
? m_winLastFocused
->GetParent()
1231 wxSetFocusToChild(parent
, &m_winLastFocused
);
1234 else // deactivating
1236 // remember the last focused child if it is our child
1237 m_winLastFocused
= FindFocus();
1239 if ( m_winLastFocused
)
1241 // let it know that it doesn't have focus any more
1242 // But this will already be done via WM_KILLFOCUS, so we'll get two kill
1243 // focus events if we call it explicitly.
1244 // m_winLastFocused->HandleKillFocus((WXHWND)NULL);
1246 // and don't remember it if it's a child from some other frame
1247 if ( wxGetTopLevelParent(m_winLastFocused
) != this )
1249 m_winLastFocused
= NULL
;
1253 wxLogTrace(_T("focus"),
1254 _T("wxTLW %p deactivated, last focused: %p."),
1256 m_winLastFocused
? GetHwndOf(m_winLastFocused
) : NULL
);
1262 // the DialogProc for all wxWidgets dialogs
1263 LONG APIENTRY _EXPORT
1264 wxDlgProc(HWND hDlg
,
1266 WPARAM
WXUNUSED(wParam
),
1267 LPARAM
WXUNUSED(lParam
))
1273 // under CE, add a "Ok" button in the dialog title bar and make it full
1276 // TODO: find the window for this HWND, and take into account
1277 // wxMAXIMIZE and wxCLOSE_BOX. For now, assume both are present.
1279 // Standard SDK doesn't have aygshell.dll: see
1280 // include/wx/msw/wince/libraries.h
1281 #if defined(__WXWINCE__) && !defined(__WINCE_STANDARDSDK__) && !defined(__HANDHELDPC__)
1282 SHINITDLGINFO shidi
;
1283 shidi
.dwMask
= SHIDIM_FLAGS
;
1284 shidi
.dwFlags
= SHIDIF_SIZEDLG
// take account of the SIP or menubar
1285 #ifndef __SMARTPHONE__
1290 SHInitDialog( &shidi
);
1291 #else // no SHInitDialog()
1294 // for WM_INITDIALOG, returning TRUE tells system to set focus to
1295 // the first control in the dialog box, but as we set the focus
1296 // ourselves, we return FALSE for it as well
1301 // for almost all messages, returning FALSE means that we didn't process
1306 // ============================================================================
1307 // wxTLWHiddenParentModule implementation
1308 // ============================================================================
1310 HWND
wxTLWHiddenParentModule::ms_hwnd
= NULL
;
1312 const wxChar
*wxTLWHiddenParentModule::ms_className
= NULL
;
1314 bool wxTLWHiddenParentModule::OnInit()
1317 ms_className
= NULL
;
1322 void wxTLWHiddenParentModule::OnExit()
1326 if ( !::DestroyWindow(ms_hwnd
) )
1328 wxLogLastError(_T("DestroyWindow(hidden TLW parent)"));
1336 if ( !::UnregisterClass(ms_className
, wxGetInstance()) )
1338 wxLogLastError(_T("UnregisterClass(\"wxTLWHiddenParent\")"));
1341 ms_className
= NULL
;
1346 HWND
wxTLWHiddenParentModule::GetHWND()
1350 if ( !ms_className
)
1352 static const wxChar
*HIDDEN_PARENT_CLASS
= _T("wxTLWHiddenParent");
1355 wxZeroMemory(wndclass
);
1357 wndclass
.lpfnWndProc
= DefWindowProc
;
1358 wndclass
.hInstance
= wxGetInstance();
1359 wndclass
.lpszClassName
= HIDDEN_PARENT_CLASS
;
1361 if ( !::RegisterClass(&wndclass
) )
1363 wxLogLastError(_T("RegisterClass(\"wxTLWHiddenParent\")"));
1367 ms_className
= HIDDEN_PARENT_CLASS
;
1371 ms_hwnd
= ::CreateWindow(ms_className
, wxEmptyString
, 0, 0, 0, 0, 0, NULL
,
1372 (HMENU
)NULL
, wxGetInstance(), NULL
);
1375 wxLogLastError(_T("CreateWindow(hidden TLW parent)"));