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 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 // the name of the default wxWidgets class
86 extern const wxChar
*wxCanvasClassName
;
88 // ----------------------------------------------------------------------------
89 // wxTLWHiddenParentModule: used to manage the hidden parent window (we need a
90 // module to ensure that the window is always deleted)
91 // ----------------------------------------------------------------------------
93 class wxTLWHiddenParentModule
: public wxModule
96 // module init/finalize
97 virtual bool OnInit();
98 virtual void OnExit();
100 // get the hidden window (creates on demand)
101 static HWND
GetHWND();
104 // the HWND of the hidden parent
107 // the class used to create it
108 static const wxChar
*ms_className
;
110 DECLARE_DYNAMIC_CLASS(wxTLWHiddenParentModule
)
113 IMPLEMENT_DYNAMIC_CLASS(wxTLWHiddenParentModule
, wxModule
)
115 // ============================================================================
116 // wxTopLevelWindowMSW implementation
117 // ============================================================================
119 BEGIN_EVENT_TABLE(wxTopLevelWindowMSW
, wxTopLevelWindowBase
)
120 EVT_ACTIVATE(wxTopLevelWindowMSW::OnActivate
)
123 // ----------------------------------------------------------------------------
124 // wxTopLevelWindowMSW creation
125 // ----------------------------------------------------------------------------
127 void wxTopLevelWindowMSW::Init()
130 m_maximizeOnShow
= false;
132 // Data to save/restore when calling ShowFullScreen
134 m_fsOldWindowStyle
= 0;
135 m_fsIsMaximized
= false;
136 m_fsIsShowing
= false;
138 m_winLastFocused
= (wxWindow
*)NULL
;
140 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
144 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
145 SHACTIVATEINFO
* info
= new SHACTIVATEINFO
;
147 info
->cbSize
= sizeof(SHACTIVATEINFO
);
149 m_activateInfo
= (void*) info
;
153 WXDWORD
wxTopLevelWindowMSW::MSWGetStyle(long style
, WXDWORD
*exflags
) const
155 // let the base class deal with the common styles but fix the ones which
156 // don't make sense for us (we also deal with the borders ourselves)
157 WXDWORD msflags
= wxWindow::MSWGetStyle
159 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exflags
160 ) & ~WS_CHILD
& ~WS_VISIBLE
;
162 // For some reason, WS_VISIBLE needs to be defined on creation for
163 // SmartPhone 2003. The title can fail to be displayed otherwise.
164 #if defined(__SMARTPHONE__) || (defined(__WXWINCE__) && _WIN32_WCE < 400)
165 msflags
|= WS_VISIBLE
;
166 ((wxTopLevelWindowMSW
*)this)->wxWindowBase::Show(true);
169 // first select the kind of window being created
171 // note that if we don't set WS_POPUP, Windows assumes WS_OVERLAPPED and
172 // creates a window with both caption and border, hence we need to use
173 // WS_POPUP in a few cases just to avoid having caption/border which we
176 // border and caption styles
177 if ( ( style
& wxRESIZE_BORDER
) && !IsAlwaysMaximized())
178 msflags
|= WS_THICKFRAME
;
179 else if ( exflags
&& ((style
& wxBORDER_DOUBLE
) || (style
& wxBORDER_RAISED
)) )
180 *exflags
|= WS_EX_DLGMODALFRAME
;
181 else if ( !(style
& wxBORDER_NONE
) )
182 msflags
|= WS_BORDER
;
188 // normally we consider that all windows without a caption must be popups,
189 // but CE is an exception: there windows normally do not have the caption
190 // but shouldn't be made popups as popups can't have menus and don't look
191 // like normal windows anyhow
193 // TODO: Smartphone appears to like wxCAPTION, but we should check that
195 #if defined(__SMARTPHONE__) || !defined(__WXWINCE__)
196 if ( style
& wxCAPTION
)
197 msflags
|= WS_CAPTION
;
201 #endif // !__WXWINCE__
204 // next translate the individual flags
206 // WS_EX_CONTEXTHELP is incompatible with WS_MINIMIZEBOX and WS_MAXIMIZEBOX
207 // and is ignored if we specify both of them, but chances are that if we
208 // use wxWS_EX_CONTEXTHELP, we really do want to have the context help
209 // button while wxMINIMIZE/wxMAXIMIZE are included by default, so the help
211 if ( !(GetExtraStyle() & wxWS_EX_CONTEXTHELP
) )
213 if ( style
& wxMINIMIZE_BOX
)
214 msflags
|= WS_MINIMIZEBOX
;
215 if ( style
& wxMAXIMIZE_BOX
)
216 msflags
|= WS_MAXIMIZEBOX
;
220 // notice that if wxCLOSE_BOX is specified we need to use WS_SYSMENU too as
221 // otherwise the close box doesn't appear
222 if ( style
& (wxSYSTEM_MENU
| wxCLOSE_BOX
) )
223 msflags
|= WS_SYSMENU
;
224 #endif // !__WXWINCE__
226 // NB: under CE these 2 styles are not supported currently, we should
227 // call Minimize()/Maximize() "manually" if we want to support them
228 if ( style
& wxMINIMIZE
)
229 msflags
|= WS_MINIMIZE
;
231 if ( style
& wxMAXIMIZE
)
232 msflags
|= WS_MAXIMIZE
;
234 // Keep this here because it saves recoding this function in wxTinyFrame
235 if ( style
& (wxTINY_CAPTION_VERT
| wxTINY_CAPTION_HORIZ
) )
236 msflags
|= WS_CAPTION
;
240 // there is no taskbar under CE, so omit all this
241 #if !defined(__WXWINCE__)
242 if ( !(GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
) )
244 if ( style
& wxFRAME_TOOL_WINDOW
)
246 // create the palette-like window
247 *exflags
|= WS_EX_TOOLWINDOW
;
249 // tool windows shouldn't appear on the taskbar (as documented)
250 style
|= wxFRAME_NO_TASKBAR
;
253 // We have to solve 2 different problems here:
255 // 1. frames with wxFRAME_NO_TASKBAR flag shouldn't appear in the
256 // taskbar even if they don't have a parent
258 // 2. frames without this style should appear in the taskbar even
259 // if they're owned (Windows only puts non owned windows into
260 // the taskbar normally)
262 // The second one is solved here by using WS_EX_APPWINDOW flag, the
263 // first one is dealt with in our MSWGetParent() method
265 if ( !(style
& wxFRAME_NO_TASKBAR
) && GetParent() )
267 // need to force the frame to appear in the taskbar
268 *exflags
|= WS_EX_APPWINDOW
;
270 //else: nothing to do [here]
273 if ( GetExtraStyle() & wxWS_EX_CONTEXTHELP
)
274 *exflags
|= WS_EX_CONTEXTHELP
;
275 #endif // !__WXWINCE__
277 if ( style
& wxSTAY_ON_TOP
)
278 *exflags
|= WS_EX_TOPMOST
;
284 WXHWND
wxTopLevelWindowMSW::MSWGetParent() const
286 // for the frames without wxFRAME_FLOAT_ON_PARENT style we should use NULL
287 // parent HWND or it would be always on top of its parent which is not what
288 // we usually want (in fact, we only want it for frames with the
289 // wxFRAME_FLOAT_ON_PARENT flag)
290 HWND hwndParent
= NULL
;
291 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT
) )
293 const wxWindow
*parent
= GetParent();
297 // this flag doesn't make sense then and will be ignored
298 wxFAIL_MSG( _T("wxFRAME_FLOAT_ON_PARENT but no parent?") );
302 hwndParent
= GetHwndOf(parent
);
305 //else: don't float on parent, must not be owned
307 // now deal with the 2nd taskbar-related problem (see comments above in
309 if ( HasFlag(wxFRAME_NO_TASKBAR
) && !hwndParent
)
312 hwndParent
= wxTLWHiddenParentModule::GetHWND();
315 return (WXHWND
)hwndParent
;
318 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
319 bool wxTopLevelWindowMSW::HandleSettingChange(WXWPARAM wParam
, WXLPARAM lParam
)
321 SHACTIVATEINFO
*info
= (SHACTIVATEINFO
*) m_activateInfo
;
324 SHHandleWMSettingChange(GetHwnd(), wParam
, lParam
, info
);
327 return wxWindowMSW::HandleSettingChange(wParam
, lParam
);
331 WXLRESULT
wxTopLevelWindowMSW::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
334 bool processed
= false;
336 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
341 SHACTIVATEINFO
* info
= (SHACTIVATEINFO
*) m_activateInfo
;
345 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
) flags
= SHA_INPUTDIALOG
;
346 SHHandleWMActivate(GetHwnd(), wParam
, lParam
, info
, flags
);
349 // This implicitly sends a wxEVT_ACTIVATE_APP event
351 wxTheApp
->SetActive(wParam
!= 0, FindFocus());
359 wxActivateEvent
event(wxEVT_HIBERNATE
, true, wxID_ANY
);
360 event
.SetEventObject(wxTheApp
);
361 processed
= wxTheApp
->ProcessEvent(event
);
369 rc
= wxTopLevelWindowBase::MSWWindowProc(message
, wParam
, lParam
);
374 bool wxTopLevelWindowMSW::CreateDialog(const void *dlgTemplate
,
375 const wxString
& title
,
379 #ifdef __WXMICROWIN__
380 // no dialogs support under MicroWin yet
381 return CreateFrame(title
, pos
, size
);
382 #else // !__WXMICROWIN__
383 wxWindow
*parent
= GetParent();
385 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
386 // app window as parent - this avoids creating modal dialogs without
388 if ( !parent
&& !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
) )
390 parent
= wxTheApp
->GetTopWindow();
394 // don't use transient windows as parents, this is dangerous as it
395 // can lead to a crash if the parent is destroyed before the child
397 // also don't use the window which is currently hidden as then the
398 // dialog would be hidden as well
399 if ( (parent
->GetExtraStyle() & wxWS_EX_TRANSIENT
) ||
407 m_hWnd
= (WXHWND
)::CreateDialogIndirect
410 (DLGTEMPLATE
*)dlgTemplate
,
411 parent
? GetHwndOf(parent
) : NULL
,
417 wxFAIL_MSG(wxT("Failed to create dialog. Incorrect DLGTEMPLATE?"));
419 wxLogSysError(wxT("Can't create dialog using memory template"));
424 #if !defined(__WXWINCE__)
425 // For some reason, the system menu is activated when we use the
426 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
427 if ( HasExtraStyle(wxWS_EX_CONTEXTHELP
) )
429 wxFrame
*winTop
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
432 wxIcon icon
= winTop
->GetIcon();
435 ::SendMessage(GetHwnd(), WM_SETICON
,
437 (LPARAM
)GetHiconOf(icon
));
441 #endif // !__WXWINCE__
443 // move the dialog to its initial position without forcing repainting
445 (void)MSWGetCreateWindowCoords(pos
, size
, x
, y
, w
, h
);
447 if ( x
== (int)CW_USEDEFAULT
)
449 // centre it on the screen - what else can we do?
450 wxSize sizeDpy
= wxGetDisplaySize();
452 x
= (sizeDpy
.x
- w
) / 2;
453 y
= (sizeDpy
.y
- h
) / 2;
456 #if !defined(__WXWINCE__) || defined(__WINCE_STANDARDSDK__)
457 if ( !::MoveWindow(GetHwnd(), x
, y
, w
, h
, FALSE
) )
459 wxLogLastError(wxT("MoveWindow"));
463 if ( !title
.empty() )
465 ::SetWindowText(GetHwnd(), title
.wx_str());
470 #ifdef __SMARTPHONE__
471 // Work around title non-display glitch
476 #endif // __WXMICROWIN__/!__WXMICROWIN__
479 bool wxTopLevelWindowMSW::CreateFrame(const wxString
& title
,
484 WXDWORD flags
= MSWGetCreateWindowFlags(&exflags
);
486 const wxSize sz
= IsAlwaysMaximized() ? wxDefaultSize
: size
;
489 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
490 exflags
|= WS_EX_LAYOUTRTL
;
493 return MSWCreate(wxCanvasClassName
, title
.wx_str(), pos
, sz
, flags
, exflags
);
496 bool wxTopLevelWindowMSW::Create(wxWindow
*parent
,
498 const wxString
& title
,
502 const wxString
& name
)
504 bool ret
wxDUMMY_INITIALIZE(false);
506 wxSize sizeReal
= size
;
507 if ( !sizeReal
.IsFullySpecified() )
509 sizeReal
.SetDefaults(GetDefaultSize());
512 m_windowStyle
= style
;
516 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
518 wxTopLevelWindows
.Append(this);
521 parent
->AddChild(this);
523 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
525 // we have different dialog templates to allows creation of dialogs
526 // with & without captions under MSWindows, resizeable or not (but a
527 // resizeable dialog always has caption - otherwise it would look too
530 // we need 3 additional WORDs for dialog menu, class and title (as we
531 // don't use DS_SETFONT we don't need the fourth WORD for the font)
532 static const int dlgsize
= sizeof(DLGTEMPLATE
) + (sizeof(WORD
) * 3);
533 DLGTEMPLATE
*dlgTemplate
= (DLGTEMPLATE
*)malloc(dlgsize
);
534 memset(dlgTemplate
, 0, dlgsize
);
536 // these values are arbitrary, they won't be used normally anyhow
539 dlgTemplate
->cx
= 144;
540 dlgTemplate
->cy
= 75;
542 // reuse the code in MSWGetStyle() but correct the results slightly for
545 // NB: we need a temporary variable as we can't pass pointer to
546 // dwExtendedStyle directly, it's not aligned correctly for 64 bit
548 WXDWORD dwExtendedStyle
;
549 dlgTemplate
->style
= MSWGetStyle(style
, &dwExtendedStyle
);
550 dlgTemplate
->dwExtendedStyle
= dwExtendedStyle
;
552 // all dialogs are popups
553 dlgTemplate
->style
|= WS_POPUP
;
556 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
558 dlgTemplate
->dwExtendedStyle
|= WS_EX_LAYOUTRTL
;
561 // force 3D-look if necessary, it looks impossibly ugly otherwise
562 if ( style
& (wxRESIZE_BORDER
| wxCAPTION
) )
563 dlgTemplate
->style
|= DS_MODALFRAME
;
566 ret
= CreateDialog(dlgTemplate
, title
, pos
, sizeReal
);
571 ret
= CreateFrame(title
, pos
, sizeReal
);
575 if ( ret
&& !(GetWindowStyleFlag() & wxCLOSE_BOX
) )
577 EnableCloseButton(false);
581 // for standard dialogs the dialog manager generates WM_CHANGEUISTATE
582 // itself but for custom windows we have to do it ourselves in order to
583 // make the keyboard indicators (such as underlines for accelerators and
584 // focus rectangles) work under Win2k+
587 MSWUpdateUIState(UIS_INITIALIZE
);
590 // Note: if we include PocketPC in this test, dialogs can fail to show up,
591 // for example the text entry dialog in the dialogs sample. Problem with Maximise()?
592 #if defined(__WXWINCE__) && (defined(__SMARTPHONE__) || defined(__WINCE_STANDARDSDK__))
593 if ( ( style
& wxMAXIMIZE
) || IsAlwaysMaximized() )
599 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
600 SetRightMenu(); // to nothing for initialization
606 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
608 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
609 SHACTIVATEINFO
* info
= (SHACTIVATEINFO
*) m_activateInfo
;
611 m_activateInfo
= NULL
;
614 // after destroying an owned window, Windows activates the next top level
615 // window in Z order but it may be different from our owner (to reproduce
616 // this simply Alt-TAB to another application and back before closing the
617 // owned frame) whereas we always want to yield activation to our parent
618 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT
) )
620 wxWindow
*parent
= GetParent();
623 ::BringWindowToTop(GetHwndOf(parent
));
628 // ----------------------------------------------------------------------------
629 // wxTopLevelWindowMSW showing
630 // ----------------------------------------------------------------------------
632 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd
)
634 ::ShowWindow(GetHwnd(), nShowCmd
);
636 m_iconized
= nShowCmd
== SW_MINIMIZE
;
639 bool wxTopLevelWindowMSW::Show(bool show
)
641 // don't use wxWindow version as we want to call DoShowWindow() ourselves
642 if ( !wxWindowBase::Show(show
) )
648 if ( m_maximizeOnShow
)
651 nShowCmd
= SW_MAXIMIZE
;
653 // This is necessary, or no window appears
654 #if defined( __WINCE_STANDARDSDK__) || defined(__SMARTPHONE__)
655 DoShowWindow(SW_SHOW
);
658 m_maximizeOnShow
= false;
662 // we shouldn't use SW_SHOW which also activates the window for
663 // tool frames (as they shouldn't steal focus from the main window)
664 // nor for the currently disabled windows as they would be enabled
666 if ( HasFlag(wxFRAME_TOOL_WINDOW
) || !IsEnabled() )
667 nShowCmd
= SW_SHOWNA
;
677 DoShowWindow(nShowCmd
);
679 #if defined(__WXWINCE__) && (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__))
680 // Addornments have to be added when the frame is the correct size
681 wxFrame
* frame
= wxDynamicCast(this, wxFrame
);
682 if (frame
&& frame
->GetMenuBar())
683 frame
->GetMenuBar()->AddAdornments(GetWindowStyleFlag());
686 // we only set pending size if we're maximized before being shown, now that
687 // we're shown we don't need it any more (it is reset in size event handler
688 // for child windows but we have to do it ourselves for this parent window)
689 m_pendingSize
= wxDefaultSize
;
694 // ----------------------------------------------------------------------------
695 // wxTopLevelWindowMSW maximize/minimize
696 // ----------------------------------------------------------------------------
698 void wxTopLevelWindowMSW::Maximize(bool maximize
)
702 // just maximize it directly
703 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
707 // we can't maximize the hidden frame because it shows it as well,
708 // so just remember that we should do it later in this case
709 m_maximizeOnShow
= maximize
;
711 // after calling Maximize() the client code expects to get the frame
712 // "real" size and doesn't want to know that, because of implementation
713 // details, the frame isn't really maximized yet but will be only once
714 // it's shown, so return our size as it will be then in this case
717 // we must only change pending size here, and not call SetSize()
718 // because otherwise Windows would think that this (full screen)
719 // size is the natural size for the frame and so would use it when
720 // the user clicks on "restore" title bar button instead of the
721 // correct initial frame size
723 // NB: unfortunately we don't know which display we're on yet so we
724 // have to use the default one
725 m_pendingSize
= wxGetClientDisplayRect().GetSize();
727 //else: can't do anything in this case, we don't have the old size
731 bool wxTopLevelWindowMSW::IsMaximized() const
733 return IsAlwaysMaximized() ||
734 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__) && !defined(__WINCE_STANDARDSDK__)
736 (::IsZoomed(GetHwnd()) != 0) ||
741 void wxTopLevelWindowMSW::Iconize(bool iconize
)
743 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
746 bool wxTopLevelWindowMSW::IsIconized() const
751 // don't use m_iconized, it may be briefly out of sync with the real state
752 // as it's only modified when we receive a WM_SIZE and we could be called
753 // from an event handler from one of the messages we receive before it,
755 return ::IsIconic(GetHwnd()) != 0;
759 void wxTopLevelWindowMSW::Restore()
761 DoShowWindow(SW_RESTORE
);
764 void wxTopLevelWindowMSW::SetLayoutDirection(wxLayoutDirection dir
)
766 if ( dir
== wxLayout_Default
)
767 dir
= wxTheApp
->GetLayoutDirection();
769 if ( dir
!= wxLayout_Default
)
770 wxTopLevelWindowBase::SetLayoutDirection(dir
);
773 // ----------------------------------------------------------------------------
774 // wxTopLevelWindowMSW geometry
775 // ----------------------------------------------------------------------------
779 void wxTopLevelWindowMSW::DoGetPosition(int *x
, int *y
) const
784 wp
.length
= sizeof(WINDOWPLACEMENT
);
785 if ( ::GetWindowPlacement(GetHwnd(), &wp
) )
787 RECT
& rc
= wp
.rcNormalPosition
;
789 // the position returned by GetWindowPlacement() is in workspace
790 // coordinates except for windows with WS_EX_TOOLWINDOW style
791 if ( !HasFlag(wxFRAME_TOOL_WINDOW
) )
793 // we must use the correct display for the translation as the
794 // task bar might be shown on one display but not the other one
795 int n
= wxDisplay::GetFromWindow(this);
796 wxDisplay
dpy(n
== wxNOT_FOUND
? 0 : n
);
797 const wxPoint ptOfs
= dpy
.GetClientArea().GetPosition() -
798 dpy
.GetGeometry().GetPosition();
812 wxLogLastError(_T("GetWindowPlacement"));
816 wxTopLevelWindowBase::DoGetPosition(x
, y
);
819 void wxTopLevelWindowMSW::DoGetSize(int *width
, int *height
) const
824 wp
.length
= sizeof(WINDOWPLACEMENT
);
825 if ( ::GetWindowPlacement(GetHwnd(), &wp
) )
827 const RECT
& rc
= wp
.rcNormalPosition
;
830 *width
= rc
.right
- rc
.left
;
832 *height
= rc
.bottom
- rc
.top
;
837 wxLogLastError(_T("GetWindowPlacement"));
841 wxTopLevelWindowBase::DoGetSize(width
, height
);
844 #endif // __WXWINCE__
846 // ----------------------------------------------------------------------------
847 // wxTopLevelWindowMSW fullscreen
848 // ----------------------------------------------------------------------------
850 bool wxTopLevelWindowMSW::ShowFullScreen(bool show
, long style
)
852 if ( show
== IsFullScreen() )
858 m_fsIsShowing
= show
;
864 // zap the frame borders
866 // save the 'normal' window style
867 m_fsOldWindowStyle
= GetWindowLong(GetHwnd(), GWL_STYLE
);
869 // save the old position, width & height, maximize state
870 m_fsOldSize
= GetRect();
871 m_fsIsMaximized
= IsMaximized();
873 // decide which window style flags to turn off
874 LONG newStyle
= m_fsOldWindowStyle
;
877 if (style
& wxFULLSCREEN_NOBORDER
)
879 offFlags
|= WS_BORDER
;
881 offFlags
|= WS_THICKFRAME
;
884 if (style
& wxFULLSCREEN_NOCAPTION
)
885 offFlags
|= WS_CAPTION
| WS_SYSMENU
;
887 newStyle
&= ~offFlags
;
889 // change our window style to be compatible with full-screen mode
890 ::SetWindowLong(GetHwnd(), GWL_STYLE
, newStyle
);
894 // resize to the size of the display containing us
895 int dpy
= wxDisplay::GetFromWindow(this);
896 if ( dpy
!= wxNOT_FOUND
)
898 rect
= wxDisplay(dpy
).GetGeometry();
900 else // fall back to the main desktop
901 #endif // wxUSE_DISPLAY
903 // resize to the size of the desktop
904 wxCopyRECTToRect(wxGetWindowRect(::GetDesktopWindow()), rect
);
906 // FIXME: size of the bottom menu (toolbar)
907 // should be taken in account
908 rect
.height
+= rect
.y
;
915 // now flush the window style cache and actually go full-screen
916 long flags
= SWP_FRAMECHANGED
;
918 // showing the frame full screen should also show it if it's still
922 // don't call wxWindow version to avoid flicker from calling
923 // ::ShowWindow() -- we're going to show the window at the correct
924 // location directly below -- but do call the wxWindowBase version
925 // to sync the internal m_isShown flag
926 wxWindowBase::Show();
928 flags
|= SWP_SHOWWINDOW
;
931 SetWindowPos(GetHwnd(), HWND_TOP
,
932 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
935 #if !defined(__HANDHELDPC__) && (defined(__WXWINCE__) && (_WIN32_WCE < 400))
936 ::SHFullScreen(GetHwnd(), SHFS_HIDETASKBAR
| SHFS_HIDESIPBUTTON
);
939 // finally send an event allowing the window to relayout itself &c
940 wxSizeEvent
event(rect
.GetSize(), GetId());
941 HandleWindowEvent(event
);
943 else // stop showing full screen
945 #if !defined(__HANDHELDPC__) && (defined(__WXWINCE__) && (_WIN32_WCE < 400))
946 ::SHFullScreen(GetHwnd(), SHFS_SHOWTASKBAR
| SHFS_SHOWSIPBUTTON
);
948 Maximize(m_fsIsMaximized
);
949 SetWindowLong(GetHwnd(),GWL_STYLE
, m_fsOldWindowStyle
);
950 SetWindowPos(GetHwnd(),HWND_TOP
,m_fsOldSize
.x
, m_fsOldSize
.y
,
951 m_fsOldSize
.width
, m_fsOldSize
.height
, SWP_FRAMECHANGED
);
957 // ----------------------------------------------------------------------------
958 // wxTopLevelWindowMSW misc
959 // ----------------------------------------------------------------------------
961 void wxTopLevelWindowMSW::SetTitle( const wxString
& title
)
966 wxString
wxTopLevelWindowMSW::GetTitle() const
971 bool wxTopLevelWindowMSW::DoSelectAndSetIcon(const wxIconBundle
& icons
,
976 const wxSize
size(::GetSystemMetrics(smX
), ::GetSystemMetrics(smY
));
978 const wxIcon icon
= icons
.GetIconOfExactSize(size
);
981 ::SendMessage(GetHwnd(), WM_SETICON
, i
, (LPARAM
)GetHiconOf(icon
));
988 void wxTopLevelWindowMSW::SetIcons(const wxIconBundle
& icons
)
990 wxTopLevelWindowBase::SetIcons(icons
);
992 if ( icons
.IsEmpty() )
994 // FIXME: SetIcons(wxNullIconBundle) should unset existing icons,
995 // but we currently don't do that
996 wxASSERT_MSG( m_icons
.IsEmpty(), "unsetting icons doesn't work" );
1000 if ( !DoSelectAndSetIcon(icons
, SM_CXSMICON
, SM_CYSMICON
, ICON_SMALL
) &&
1001 !DoSelectAndSetIcon(icons
, SM_CXICON
, SM_CYICON
, ICON_BIG
) )
1003 wxFAIL_MSG( "icon bundle doesn't contain any suitable icon" );
1007 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable
)
1009 #if !defined(__WXMICROWIN__)
1010 // get system (a.k.a. window) menu
1011 HMENU hmenu
= GetSystemMenu(GetHwnd(), FALSE
/* get it */);
1014 // no system menu at all -- ok if we want to remove the close button
1015 // anyhow, but bad if we want to show it
1019 // enabling/disabling the close item from it also automatically
1020 // disables/enables the close title bar button
1021 if ( ::EnableMenuItem(hmenu
, SC_CLOSE
,
1023 (enable
? MF_ENABLED
: MF_GRAYED
)) == -1 )
1025 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
1030 // update appearance immediately
1031 if ( !::DrawMenuBar(GetHwnd()) )
1033 wxLogLastError(_T("DrawMenuBar"));
1036 #endif // !__WXMICROWIN__
1043 bool wxTopLevelWindowMSW::SetShape(const wxRegion
& region
)
1045 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), false,
1046 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1048 // The empty region signifies that the shape should be removed from the
1050 if ( region
.IsEmpty() )
1052 if (::SetWindowRgn(GetHwnd(), NULL
, TRUE
) == 0)
1054 wxLogLastError(_T("SetWindowRgn"));
1060 // Windows takes ownership of the region, so
1061 // we'll have to make a copy of the region to give to it.
1062 DWORD noBytes
= ::GetRegionData(GetHrgnOf(region
), 0, NULL
);
1063 RGNDATA
*rgnData
= (RGNDATA
*) new char[noBytes
];
1064 ::GetRegionData(GetHrgnOf(region
), noBytes
, rgnData
);
1065 HRGN hrgn
= ::ExtCreateRegion(NULL
, noBytes
, rgnData
);
1066 delete[] (char*) rgnData
;
1068 // SetWindowRgn expects the region to be in coordinants
1069 // relative to the window, not the client area. Figure
1070 // out the offset, if any.
1072 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1073 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1074 ::GetClientRect(GetHwnd(), &rect
);
1075 ::AdjustWindowRectEx(&rect
, dwStyle
, FALSE
, dwExStyle
);
1076 ::OffsetRgn(hrgn
, -rect
.left
, -rect
.top
);
1078 // Now call the shape API with the new region.
1079 if (::SetWindowRgn(GetHwnd(), hrgn
, TRUE
) == 0)
1081 wxLogLastError(_T("SetWindowRgn"));
1087 #endif // !__WXWINCE__
1089 void wxTopLevelWindowMSW::RequestUserAttention(int flags
)
1091 // check if we can use FlashWindowEx(): unfortunately a simple test for
1092 // FLASHW_STOP doesn't work because MSVC6 headers do #define it but don't
1093 // provide FlashWindowEx() declaration, so try to detect whether we have
1094 // real headers for WINVER 0x0500 by checking for existence of a symbol not
1095 // declated in MSVC6 header
1096 #if defined(FLASHW_STOP) && defined(VK_XBUTTON1) && wxUSE_DYNLIB_CLASS
1097 // available in the headers, check if it is supported by the system
1098 typedef BOOL (WINAPI
*FlashWindowEx_t
)(FLASHWINFO
*pfwi
);
1099 static FlashWindowEx_t s_pfnFlashWindowEx
= NULL
;
1100 if ( !s_pfnFlashWindowEx
)
1102 wxDynamicLibrary
dllUser32(_T("user32.dll"));
1103 s_pfnFlashWindowEx
= (FlashWindowEx_t
)
1104 dllUser32
.GetSymbol(_T("FlashWindowEx"));
1106 // we can safely unload user32.dll here, it's going to remain loaded as
1107 // long as the program is running anyhow
1110 if ( s_pfnFlashWindowEx
)
1112 WinStruct
<FLASHWINFO
> fwi
;
1113 fwi
.hwnd
= GetHwnd();
1114 fwi
.dwFlags
= FLASHW_ALL
;
1115 if ( flags
& wxUSER_ATTENTION_INFO
)
1117 // just flash a few times
1120 else // wxUSER_ATTENTION_ERROR
1122 // flash until the user notices it
1123 fwi
.dwFlags
|= FLASHW_TIMERNOFG
;
1126 s_pfnFlashWindowEx(&fwi
);
1128 else // FlashWindowEx() not available
1129 #endif // FlashWindowEx() defined
1133 ::FlashWindow(GetHwnd(), TRUE
);
1134 #endif // __WXWINCE__
1138 // ---------------------------------------------------------------------------
1140 bool wxTopLevelWindowMSW::SetTransparent(wxByte alpha
)
1142 #if wxUSE_DYNLIB_CLASS
1143 typedef DWORD (WINAPI
*PSETLAYEREDWINDOWATTR
)(HWND
, DWORD
, BYTE
, DWORD
);
1144 static PSETLAYEREDWINDOWATTR
1145 pSetLayeredWindowAttributes
= (PSETLAYEREDWINDOWATTR
)-1;
1147 if ( pSetLayeredWindowAttributes
== (PSETLAYEREDWINDOWATTR
)-1 )
1149 wxDynamicLibrary
dllUser32(_T("user32.dll"));
1151 // use RawGetSymbol() and not GetSymbol() to avoid error messages under
1152 // Windows 95: there is nothing the user can do about this anyhow
1153 pSetLayeredWindowAttributes
= (PSETLAYEREDWINDOWATTR
)
1154 dllUser32
.RawGetSymbol(wxT("SetLayeredWindowAttributes"));
1156 // it's ok to destroy dllUser32 here, we link statically to user32.dll
1157 // anyhow so it won't be unloaded
1160 if ( !pSetLayeredWindowAttributes
)
1162 #endif // wxUSE_DYNLIB_CLASS
1164 LONG exstyle
= GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1166 // if setting alpha to fully opaque then turn off the layered style
1169 SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exstyle
& ~WS_EX_LAYERED
);
1174 #if wxUSE_DYNLIB_CLASS
1175 // Otherwise, set the layered style if needed and set the alpha value
1176 if ((exstyle
& WS_EX_LAYERED
) == 0 )
1177 SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exstyle
| WS_EX_LAYERED
);
1179 if ( pSetLayeredWindowAttributes(GetHwnd(), 0, (BYTE
)alpha
, LWA_ALPHA
) )
1181 #endif // wxUSE_DYNLIB_CLASS
1186 bool wxTopLevelWindowMSW::CanSetTransparent()
1188 // The API is available on win2k and above
1190 static int os_type
= -1;
1191 static int ver_major
= -1;
1194 os_type
= ::wxGetOsVersion(&ver_major
);
1196 return (os_type
== wxOS_WINDOWS_NT
&& ver_major
>= 5);
1200 void wxTopLevelWindowMSW::DoFreeze()
1202 // do nothing: freezing toplevel window causes paint and mouse events
1203 // to go through it any TLWs under it, so the best we can do is to freeze
1204 // all children -- and wxWindowBase::Freeze() does that
1207 void wxTopLevelWindowMSW::DoThaw()
1209 // intentionally empty -- see DoFreeze()
1213 // ----------------------------------------------------------------------------
1214 // wxTopLevelWindow event handling
1215 // ----------------------------------------------------------------------------
1217 // Default activation behaviour - set the focus for the first child
1219 void wxTopLevelWindowMSW::OnActivate(wxActivateEvent
& event
)
1221 if ( event
.GetActive() )
1223 // restore focus to the child which was last focused unless we already
1225 wxLogTrace(_T("focus"), _T("wxTLW %p activated."), m_hWnd
);
1227 wxWindow
*winFocus
= FindFocus();
1228 if ( !winFocus
|| wxGetTopLevelParent(winFocus
) != this )
1230 wxWindow
*parent
= m_winLastFocused
? m_winLastFocused
->GetParent()
1237 wxSetFocusToChild(parent
, &m_winLastFocused
);
1240 else // deactivating
1242 // remember the last focused child if it is our child
1243 m_winLastFocused
= FindFocus();
1245 if ( m_winLastFocused
)
1247 // let it know that it doesn't have focus any more
1248 // But this will already be done via WM_KILLFOCUS, so we'll get two kill
1249 // focus events if we call it explicitly.
1250 // m_winLastFocused->HandleKillFocus((WXHWND)NULL);
1252 // and don't remember it if it's a child from some other frame
1253 if ( wxGetTopLevelParent(m_winLastFocused
) != this )
1255 m_winLastFocused
= NULL
;
1259 wxLogTrace(_T("focus"),
1260 _T("wxTLW %p deactivated, last focused: %p."),
1262 m_winLastFocused
? GetHwndOf(m_winLastFocused
) : NULL
);
1268 // the DialogProc for all wxWidgets dialogs
1269 LONG APIENTRY _EXPORT
1270 wxDlgProc(HWND hDlg
,
1272 WPARAM
WXUNUSED(wParam
),
1273 LPARAM
WXUNUSED(lParam
))
1279 // under CE, add a "Ok" button in the dialog title bar and make it full
1282 // TODO: find the window for this HWND, and take into account
1283 // wxMAXIMIZE and wxCLOSE_BOX. For now, assume both are present.
1285 // Standard SDK doesn't have aygshell.dll: see
1286 // include/wx/msw/wince/libraries.h
1287 #if defined(__WXWINCE__) && !defined(__WINCE_STANDARDSDK__) && !defined(__HANDHELDPC__)
1288 SHINITDLGINFO shidi
;
1289 shidi
.dwMask
= SHIDIM_FLAGS
;
1290 shidi
.dwFlags
= SHIDIF_SIZEDLG
// take account of the SIP or menubar
1291 #ifndef __SMARTPHONE__
1296 SHInitDialog( &shidi
);
1297 #else // no SHInitDialog()
1300 // for WM_INITDIALOG, returning TRUE tells system to set focus to
1301 // the first control in the dialog box, but as we set the focus
1302 // ourselves, we return FALSE for it as well
1307 // for almost all messages, returning FALSE means that we didn't process
1312 // ============================================================================
1313 // wxTLWHiddenParentModule implementation
1314 // ============================================================================
1316 HWND
wxTLWHiddenParentModule::ms_hwnd
= NULL
;
1318 const wxChar
*wxTLWHiddenParentModule::ms_className
= NULL
;
1320 bool wxTLWHiddenParentModule::OnInit()
1323 ms_className
= NULL
;
1328 void wxTLWHiddenParentModule::OnExit()
1332 if ( !::DestroyWindow(ms_hwnd
) )
1334 wxLogLastError(_T("DestroyWindow(hidden TLW parent)"));
1342 if ( !::UnregisterClass(ms_className
, wxGetInstance()) )
1344 wxLogLastError(_T("UnregisterClass(\"wxTLWHiddenParent\")"));
1347 ms_className
= NULL
;
1352 HWND
wxTLWHiddenParentModule::GetHWND()
1356 if ( !ms_className
)
1358 static const wxChar
*HIDDEN_PARENT_CLASS
= _T("wxTLWHiddenParent");
1361 wxZeroMemory(wndclass
);
1363 wndclass
.lpfnWndProc
= DefWindowProc
;
1364 wndclass
.hInstance
= wxGetInstance();
1365 wndclass
.lpszClassName
= HIDDEN_PARENT_CLASS
;
1367 if ( !::RegisterClass(&wndclass
) )
1369 wxLogLastError(_T("RegisterClass(\"wxTLWHiddenParent\")"));
1373 ms_className
= HIDDEN_PARENT_CLASS
;
1377 ms_hwnd
= ::CreateWindow(ms_className
, wxEmptyString
, 0, 0, 0, 0, 0, NULL
,
1378 (HMENU
)NULL
, wxGetInstance(), NULL
);
1381 wxLogLastError(_T("CreateWindow(hidden TLW parent)"));