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
87 extern wxChar
*wxCanvasClassName
;
89 extern const wxChar
*wxCanvasClassName
;
92 // ----------------------------------------------------------------------------
93 // wxTLWHiddenParentModule: used to manage the hidden parent window (we need a
94 // module to ensure that the window is always deleted)
95 // ----------------------------------------------------------------------------
97 class wxTLWHiddenParentModule
: public wxModule
100 // module init/finalize
101 virtual bool OnInit();
102 virtual void OnExit();
104 // get the hidden window (creates on demand)
105 static HWND
GetHWND();
108 // the HWND of the hidden parent
111 // the class used to create it
112 static const wxChar
*ms_className
;
114 DECLARE_DYNAMIC_CLASS(wxTLWHiddenParentModule
)
117 IMPLEMENT_DYNAMIC_CLASS(wxTLWHiddenParentModule
, wxModule
)
119 // ============================================================================
120 // wxTopLevelWindowMSW implementation
121 // ============================================================================
123 BEGIN_EVENT_TABLE(wxTopLevelWindowMSW
, wxTopLevelWindowBase
)
124 EVT_ACTIVATE(wxTopLevelWindowMSW::OnActivate
)
127 // ----------------------------------------------------------------------------
128 // wxTopLevelWindowMSW creation
129 // ----------------------------------------------------------------------------
131 void wxTopLevelWindowMSW::Init()
134 m_maximizeOnShow
= false;
136 // Data to save/restore when calling ShowFullScreen
138 m_fsOldWindowStyle
= 0;
139 m_fsIsMaximized
= false;
140 m_fsIsShowing
= false;
142 m_winLastFocused
= (wxWindow
*)NULL
;
144 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
148 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
149 SHACTIVATEINFO
* info
= new SHACTIVATEINFO
;
151 info
->cbSize
= sizeof(SHACTIVATEINFO
);
153 m_activateInfo
= (void*) info
;
157 WXDWORD
wxTopLevelWindowMSW::MSWGetStyle(long style
, WXDWORD
*exflags
) const
159 // let the base class deal with the common styles but fix the ones which
160 // don't make sense for us (we also deal with the borders ourselves)
161 WXDWORD msflags
= wxWindow::MSWGetStyle
163 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exflags
164 ) & ~WS_CHILD
& ~WS_VISIBLE
;
166 // For some reason, WS_VISIBLE needs to be defined on creation for
167 // SmartPhone 2003. The title can fail to be displayed otherwise.
168 #if defined(__SMARTPHONE__) || (defined(__WXWINCE__) && _WIN32_WCE < 400)
169 msflags
|= WS_VISIBLE
;
170 ((wxTopLevelWindowMSW
*)this)->wxWindowBase::Show(true);
173 // first select the kind of window being created
175 // note that if we don't set WS_POPUP, Windows assumes WS_OVERLAPPED and
176 // creates a window with both caption and border, hence we need to use
177 // WS_POPUP in a few cases just to avoid having caption/border which we
180 // border and caption styles
181 if ( ( style
& wxRESIZE_BORDER
) && !IsAlwaysMaximized())
182 msflags
|= WS_THICKFRAME
;
183 else if ( exflags
&& ((style
& wxBORDER_DOUBLE
) || (style
& wxBORDER_RAISED
)) )
184 *exflags
|= WS_EX_DLGMODALFRAME
;
185 else if ( !(style
& wxBORDER_NONE
) )
186 msflags
|= WS_BORDER
;
192 // normally we consider that all windows without a caption must be popups,
193 // but CE is an exception: there windows normally do not have the caption
194 // but shouldn't be made popups as popups can't have menus and don't look
195 // like normal windows anyhow
197 // TODO: Smartphone appears to like wxCAPTION, but we should check that
199 #if defined(__SMARTPHONE__) || !defined(__WXWINCE__)
200 if ( style
& wxCAPTION
)
201 msflags
|= WS_CAPTION
;
205 #endif // !__WXWINCE__
208 // next translate the individual flags
210 // WS_EX_CONTEXTHELP is incompatible with WS_MINIMIZEBOX and WS_MAXIMIZEBOX
211 // and is ignored if we specify both of them, but chances are that if we
212 // use wxWS_EX_CONTEXTHELP, we really do want to have the context help
213 // button while wxMINIMIZE/wxMAXIMIZE are included by default, so the help
215 if ( !(GetExtraStyle() & wxWS_EX_CONTEXTHELP
) )
217 if ( style
& wxMINIMIZE_BOX
)
218 msflags
|= WS_MINIMIZEBOX
;
219 if ( style
& wxMAXIMIZE_BOX
)
220 msflags
|= WS_MAXIMIZEBOX
;
224 if ( style
& wxSYSTEM_MENU
)
225 msflags
|= WS_SYSMENU
;
228 // NB: under CE these 2 styles are not supported currently, we should
229 // call Minimize()/Maximize() "manually" if we want to support them
230 if ( style
& wxMINIMIZE
)
231 msflags
|= WS_MINIMIZE
;
233 if ( style
& wxMAXIMIZE
)
234 msflags
|= WS_MAXIMIZE
;
236 // Keep this here because it saves recoding this function in wxTinyFrame
237 if ( style
& (wxTINY_CAPTION_VERT
| wxTINY_CAPTION_HORIZ
) )
238 msflags
|= WS_CAPTION
;
242 // there is no taskbar under CE, so omit all this
243 #if !defined(__WXWINCE__)
244 if ( !(GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
) )
246 if ( style
& wxFRAME_TOOL_WINDOW
)
248 // create the palette-like window
249 *exflags
|= WS_EX_TOOLWINDOW
;
251 // tool windows shouldn't appear on the taskbar (as documented)
252 style
|= wxFRAME_NO_TASKBAR
;
255 // We have to solve 2 different problems here:
257 // 1. frames with wxFRAME_NO_TASKBAR flag shouldn't appear in the
258 // taskbar even if they don't have a parent
260 // 2. frames without this style should appear in the taskbar even
261 // if they're owned (Windows only puts non owned windows into
262 // the taskbar normally)
264 // The second one is solved here by using WS_EX_APPWINDOW flag, the
265 // first one is dealt with in our MSWGetParent() method
267 if ( !(style
& wxFRAME_NO_TASKBAR
) && GetParent() )
269 // need to force the frame to appear in the taskbar
270 *exflags
|= WS_EX_APPWINDOW
;
272 //else: nothing to do [here]
275 if ( GetExtraStyle() & wxWS_EX_CONTEXTHELP
)
276 *exflags
|= WS_EX_CONTEXTHELP
;
277 #endif // !__WXWINCE__
279 if ( style
& wxSTAY_ON_TOP
)
280 *exflags
|= WS_EX_TOPMOST
;
286 WXHWND
wxTopLevelWindowMSW::MSWGetParent() const
288 // for the frames without wxFRAME_FLOAT_ON_PARENT style we should use NULL
289 // parent HWND or it would be always on top of its parent which is not what
290 // we usually want (in fact, we only want it for frames with the
291 // wxFRAME_FLOAT_ON_PARENT flag)
292 HWND hwndParent
= NULL
;
293 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT
) )
295 const wxWindow
*parent
= GetParent();
299 // this flag doesn't make sense then and will be ignored
300 wxFAIL_MSG( _T("wxFRAME_FLOAT_ON_PARENT but no parent?") );
304 hwndParent
= GetHwndOf(parent
);
307 //else: don't float on parent, must not be owned
309 // now deal with the 2nd taskbar-related problem (see comments above in
311 if ( HasFlag(wxFRAME_NO_TASKBAR
) && !hwndParent
)
314 hwndParent
= wxTLWHiddenParentModule::GetHWND();
317 return (WXHWND
)hwndParent
;
320 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
321 bool wxTopLevelWindowMSW::HandleSettingChange(WXWPARAM wParam
, WXLPARAM lParam
)
323 SHACTIVATEINFO
*info
= (SHACTIVATEINFO
*) m_activateInfo
;
326 SHHandleWMSettingChange(GetHwnd(), wParam
, lParam
, info
);
329 return wxWindowMSW::HandleSettingChange(wParam
, lParam
);
333 WXLRESULT
wxTopLevelWindowMSW::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
336 bool processed
= false;
338 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
343 SHACTIVATEINFO
* info
= (SHACTIVATEINFO
*) m_activateInfo
;
347 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
) flags
= SHA_INPUTDIALOG
;
348 SHHandleWMActivate(GetHwnd(), wParam
, lParam
, info
, flags
);
351 // This implicitly sends a wxEVT_ACTIVATE_APP event
353 wxTheApp
->SetActive(wParam
!= 0, FindFocus());
361 wxActivateEvent
event(wxEVT_HIBERNATE
, true, wxID_ANY
);
362 event
.SetEventObject(wxTheApp
);
363 processed
= wxTheApp
->ProcessEvent(event
);
371 rc
= wxTopLevelWindowBase::MSWWindowProc(message
, wParam
, lParam
);
376 bool wxTopLevelWindowMSW::CreateDialog(const void *dlgTemplate
,
377 const wxString
& title
,
381 #ifdef __WXMICROWIN__
382 // no dialogs support under MicroWin yet
383 return CreateFrame(title
, pos
, size
);
384 #else // !__WXMICROWIN__
385 wxWindow
*parent
= GetParent();
387 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
388 // app window as parent - this avoids creating modal dialogs without
390 if ( !parent
&& !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
) )
392 parent
= wxTheApp
->GetTopWindow();
396 // don't use transient windows as parents, this is dangerous as it
397 // can lead to a crash if the parent is destroyed before the child
399 // also don't use the window which is currently hidden as then the
400 // dialog would be hidden as well
401 if ( (parent
->GetExtraStyle() & wxWS_EX_TRANSIENT
) ||
409 m_hWnd
= (WXHWND
)::CreateDialogIndirect
412 (DLGTEMPLATE
*)dlgTemplate
,
413 parent
? GetHwndOf(parent
) : NULL
,
419 wxFAIL_MSG(wxT("Failed to create dialog. Incorrect DLGTEMPLATE?"));
421 wxLogSysError(wxT("Can't create dialog using memory template"));
427 (void)MSWGetCreateWindowFlags(&exflags
);
431 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exflags
);
432 ::SetWindowPos(GetHwnd(),
433 exflags
& WS_EX_TOPMOST
? HWND_TOPMOST
: 0,
437 (exflags
& WS_EX_TOPMOST
? 0 : SWP_NOZORDER
) |
441 #if !defined(__WXWINCE__)
442 // For some reason, the system menu is activated when we use the
443 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
444 if ( exflags
& WS_EX_CONTEXTHELP
)
446 wxFrame
*winTop
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
449 wxIcon icon
= winTop
->GetIcon();
452 ::SendMessage(GetHwnd(), WM_SETICON
,
454 (LPARAM
)GetHiconOf(icon
));
460 // move the dialog to its initial position without forcing repainting
462 (void)MSWGetCreateWindowCoords(pos
, size
, x
, y
, w
, h
);
464 if ( x
== (int)CW_USEDEFAULT
)
466 // centre it on the screen - what else can we do?
467 wxSize sizeDpy
= wxGetDisplaySize();
469 x
= (sizeDpy
.x
- w
) / 2;
470 y
= (sizeDpy
.y
- h
) / 2;
473 #if !defined(__WXWINCE__) || defined(__WINCE_STANDARDSDK__)
474 if ( !::MoveWindow(GetHwnd(), x
, y
, w
, h
, FALSE
) )
476 wxLogLastError(wxT("MoveWindow"));
480 if ( !title
.empty() )
482 ::SetWindowText(GetHwnd(), title
.wx_str());
487 #ifdef __SMARTPHONE__
488 // Work around title non-display glitch
493 #endif // __WXMICROWIN__/!__WXMICROWIN__
496 bool wxTopLevelWindowMSW::CreateFrame(const wxString
& title
,
501 WXDWORD flags
= MSWGetCreateWindowFlags(&exflags
);
503 const wxSize sz
= IsAlwaysMaximized() ? wxDefaultSize
: size
;
506 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
507 exflags
|= WS_EX_LAYOUTRTL
;
510 return MSWCreate(wxCanvasClassName
, title
.wx_str(), pos
, sz
, flags
, exflags
);
513 bool wxTopLevelWindowMSW::Create(wxWindow
*parent
,
515 const wxString
& title
,
519 const wxString
& name
)
521 bool ret
wxDUMMY_INITIALIZE(false);
526 wxSize sizeReal
= size
;
527 if ( !sizeReal
.IsFullySpecified() )
529 sizeReal
.SetDefaults(GetDefaultSize());
532 m_windowStyle
= style
;
536 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
538 wxTopLevelWindows
.Append(this);
541 parent
->AddChild(this);
543 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
545 // we have different dialog templates to allows creation of dialogs
546 // with & without captions under MSWindows, resizeable or not (but a
547 // resizeable dialog always has caption - otherwise it would look too
550 // we need 3 additional WORDs for dialog menu, class and title (as we
551 // don't use DS_SETFONT we don't need the fourth WORD for the font)
552 static const int dlgsize
= sizeof(DLGTEMPLATE
) + (sizeof(WORD
) * 3);
553 DLGTEMPLATE
*dlgTemplate
= (DLGTEMPLATE
*)malloc(dlgsize
);
554 memset(dlgTemplate
, 0, dlgsize
);
556 // these values are arbitrary, they won't be used normally anyhow
559 dlgTemplate
->cx
= 144;
560 dlgTemplate
->cy
= 75;
562 // reuse the code in MSWGetStyle() but correct the results slightly for
564 dlgTemplate
->style
= MSWGetStyle(style
, NULL
);
566 // all dialogs are popups
567 dlgTemplate
->style
|= WS_POPUP
;
570 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
572 dlgTemplate
->dwExtendedStyle
|= WS_EX_LAYOUTRTL
;
575 // force 3D-look if necessary, it looks impossibly ugly otherwise
576 if ( style
& (wxRESIZE_BORDER
| wxCAPTION
) )
577 dlgTemplate
->style
|= DS_MODALFRAME
;
580 ret
= CreateDialog(dlgTemplate
, title
, pos
, sizeReal
);
585 ret
= CreateFrame(title
, pos
, sizeReal
);
589 if ( ret
&& !(GetWindowStyleFlag() & wxCLOSE_BOX
) )
591 EnableCloseButton(false);
595 // for standard dialogs the dialog manager generates WM_CHANGEUISTATE
596 // itself but for custom windows we have to do it ourselves in order to
597 // make the keyboard indicators (such as underlines for accelerators and
598 // focus rectangles) work under Win2k+
601 MSWUpdateUIState(UIS_INITIALIZE
);
604 // Note: if we include PocketPC in this test, dialogs can fail to show up,
605 // for example the text entry dialog in the dialogs sample. Problem with Maximise()?
606 #if defined(__WXWINCE__) && (defined(__SMARTPHONE__) || defined(__WINCE_STANDARDSDK__))
607 if ( ( style
& wxMAXIMIZE
) || IsAlwaysMaximized() )
613 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
614 SetRightMenu(); // to nothing for initialization
620 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
622 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
623 SHACTIVATEINFO
* info
= (SHACTIVATEINFO
*) m_activateInfo
;
625 m_activateInfo
= NULL
;
628 // after destroying an owned window, Windows activates the next top level
629 // window in Z order but it may be different from our owner (to reproduce
630 // this simply Alt-TAB to another application and back before closing the
631 // owned frame) whereas we always want to yield activation to our parent
632 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT
) )
634 wxWindow
*parent
= GetParent();
637 ::BringWindowToTop(GetHwndOf(parent
));
642 // ----------------------------------------------------------------------------
643 // wxTopLevelWindowMSW showing
644 // ----------------------------------------------------------------------------
646 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd
)
648 ::ShowWindow(GetHwnd(), nShowCmd
);
650 m_iconized
= nShowCmd
== SW_MINIMIZE
;
653 bool wxTopLevelWindowMSW::Show(bool show
)
655 // don't use wxWindow version as we want to call DoShowWindow() ourselves
656 if ( !wxWindowBase::Show(show
) )
662 if ( m_maximizeOnShow
)
665 nShowCmd
= SW_MAXIMIZE
;
667 // This is necessary, or no window appears
668 #if defined( __WINCE_STANDARDSDK__) || defined(__SMARTPHONE__)
669 DoShowWindow(SW_SHOW
);
672 m_maximizeOnShow
= false;
676 if ( GetWindowStyle() & wxFRAME_TOOL_WINDOW
)
677 nShowCmd
= SW_SHOWNA
;
687 DoShowWindow(nShowCmd
);
689 #if defined(__WXWINCE__) && (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__))
690 // Addornments have to be added when the frame is the correct size
691 wxFrame
* frame
= wxDynamicCast(this, wxFrame
);
692 if (frame
&& frame
->GetMenuBar())
693 frame
->GetMenuBar()->AddAdornments(GetWindowStyleFlag());
696 // we only set pending size if we're maximized before being shown, now that
697 // we're shown we don't need it any more (it is reset in size event handler
698 // for child windows but we have to do it ourselves for this parent window)
699 m_pendingSize
= wxDefaultSize
;
704 // ----------------------------------------------------------------------------
705 // wxTopLevelWindowMSW maximize/minimize
706 // ----------------------------------------------------------------------------
708 void wxTopLevelWindowMSW::Maximize(bool maximize
)
712 // just maximize it directly
713 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
717 // we can't maximize the hidden frame because it shows it as well,
718 // so just remember that we should do it later in this case
719 m_maximizeOnShow
= maximize
;
721 // after calling Maximize() the client code expects to get the frame
722 // "real" size and doesn't want to know that, because of implementation
723 // details, the frame isn't really maximized yet but will be only once
724 // it's shown, so return our size as it will be then in this case
727 // we must only change pending size here, and not call SetSize()
728 // because otherwise Windows would think that this (full screen)
729 // size is the natural size for the frame and so would use it when
730 // the user clicks on "restore" title bar button instead of the
731 // correct initial frame size
733 // NB: unfortunately we don't know which display we're on yet so we
734 // have to use the default one
735 m_pendingSize
= wxGetClientDisplayRect().GetSize();
737 //else: can't do anything in this case, we don't have the old size
741 bool wxTopLevelWindowMSW::IsMaximized() const
743 return IsAlwaysMaximized() ||
744 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__) && !defined(__WINCE_STANDARDSDK__)
746 (::IsZoomed(GetHwnd()) != 0) ||
751 void wxTopLevelWindowMSW::Iconize(bool iconize
)
753 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
756 bool wxTopLevelWindowMSW::IsIconized() const
761 // don't use m_iconized, it may be briefly out of sync with the real state
762 // as it's only modified when we receive a WM_SIZE and we could be called
763 // from an event handler from one of the messages we receive before it,
765 return ::IsIconic(GetHwnd()) != 0;
769 void wxTopLevelWindowMSW::Restore()
771 DoShowWindow(SW_RESTORE
);
774 void wxTopLevelWindowMSW::SetLayoutDirection(wxLayoutDirection dir
)
776 if ( dir
== wxLayout_Default
)
777 dir
= wxTheApp
->GetLayoutDirection();
779 if ( dir
!= wxLayout_Default
)
780 wxTopLevelWindowBase::SetLayoutDirection(dir
);
783 // ----------------------------------------------------------------------------
784 // wxTopLevelWindowMSW geometry
785 // ----------------------------------------------------------------------------
789 void wxTopLevelWindowMSW::DoGetPosition(int *x
, int *y
) const
794 wp
.length
= sizeof(WINDOWPLACEMENT
);
795 if ( ::GetWindowPlacement(GetHwnd(), &wp
) )
797 RECT
& rc
= wp
.rcNormalPosition
;
799 // the position returned by GetWindowPlacement() is in workspace
800 // coordinates except for windows with WS_EX_TOOLWINDOW style
801 if ( !HasFlag(wxFRAME_TOOL_WINDOW
) )
803 // we must use the correct display for the translation as the
804 // task bar might be shown on one display but not the other one
805 int n
= wxDisplay::GetFromWindow(this);
806 wxDisplay
dpy(n
== wxNOT_FOUND
? 0 : n
);
807 const wxPoint ptOfs
= dpy
.GetClientArea().GetPosition() -
808 dpy
.GetGeometry().GetPosition();
822 wxLogLastError(_T("GetWindowPlacement"));
826 wxTopLevelWindowBase::DoGetPosition(x
, y
);
829 void wxTopLevelWindowMSW::DoGetSize(int *width
, int *height
) const
834 wp
.length
= sizeof(WINDOWPLACEMENT
);
835 if ( ::GetWindowPlacement(GetHwnd(), &wp
) )
837 const RECT
& rc
= wp
.rcNormalPosition
;
840 *width
= rc
.right
- rc
.left
;
842 *height
= rc
.bottom
- rc
.top
;
847 wxLogLastError(_T("GetWindowPlacement"));
851 wxTopLevelWindowBase::DoGetSize(width
, height
);
854 #endif // __WXWINCE__
856 // ----------------------------------------------------------------------------
857 // wxTopLevelWindowMSW fullscreen
858 // ----------------------------------------------------------------------------
860 bool wxTopLevelWindowMSW::ShowFullScreen(bool show
, long style
)
862 if ( show
== IsFullScreen() )
868 m_fsIsShowing
= show
;
874 // zap the frame borders
876 // save the 'normal' window style
877 m_fsOldWindowStyle
= GetWindowLong(GetHwnd(), GWL_STYLE
);
879 // save the old position, width & height, maximize state
880 m_fsOldSize
= GetRect();
881 m_fsIsMaximized
= IsMaximized();
883 // decide which window style flags to turn off
884 LONG newStyle
= m_fsOldWindowStyle
;
887 if (style
& wxFULLSCREEN_NOBORDER
)
889 offFlags
|= WS_BORDER
;
891 offFlags
|= WS_THICKFRAME
;
894 if (style
& wxFULLSCREEN_NOCAPTION
)
895 offFlags
|= WS_CAPTION
| WS_SYSMENU
;
897 newStyle
&= ~offFlags
;
899 // change our window style to be compatible with full-screen mode
900 ::SetWindowLong(GetHwnd(), GWL_STYLE
, newStyle
);
904 // resize to the size of the display containing us
905 int dpy
= wxDisplay::GetFromWindow(this);
906 if ( dpy
!= wxNOT_FOUND
)
908 rect
= wxDisplay(dpy
).GetGeometry();
910 else // fall back to the main desktop
911 #endif // wxUSE_DISPLAY
913 // resize to the size of the desktop
914 wxCopyRECTToRect(wxGetWindowRect(::GetDesktopWindow()), rect
);
916 // FIXME: size of the bottom menu (toolbar)
917 // should be taken in account
918 rect
.height
+= rect
.y
;
925 // now flush the window style cache and actually go full-screen
926 long flags
= SWP_FRAMECHANGED
;
928 // showing the frame full screen should also show it if it's still
932 // don't call wxWindow version to avoid flicker from calling
933 // ::ShowWindow() -- we're going to show the window at the correct
934 // location directly below -- but do call the wxWindowBase version
935 // to sync the internal m_isShown flag
936 wxWindowBase::Show();
938 flags
|= SWP_SHOWWINDOW
;
941 SetWindowPos(GetHwnd(), HWND_TOP
,
942 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
945 #if !defined(__HANDHELDPC__) && (defined(__WXWINCE__) && (_WIN32_WCE < 400))
946 ::SHFullScreen(GetHwnd(), SHFS_HIDETASKBAR
| SHFS_HIDESIPBUTTON
);
949 // finally send an event allowing the window to relayout itself &c
950 wxSizeEvent
event(rect
.GetSize(), GetId());
951 GetEventHandler()->ProcessEvent(event
);
953 else // stop showing full screen
955 #if !defined(__HANDHELDPC__) && (defined(__WXWINCE__) && (_WIN32_WCE < 400))
956 ::SHFullScreen(GetHwnd(), SHFS_SHOWTASKBAR
| SHFS_SHOWSIPBUTTON
);
958 Maximize(m_fsIsMaximized
);
959 SetWindowLong(GetHwnd(),GWL_STYLE
, m_fsOldWindowStyle
);
960 SetWindowPos(GetHwnd(),HWND_TOP
,m_fsOldSize
.x
, m_fsOldSize
.y
,
961 m_fsOldSize
.width
, m_fsOldSize
.height
, SWP_FRAMECHANGED
);
967 // ----------------------------------------------------------------------------
968 // wxTopLevelWindowMSW misc
969 // ----------------------------------------------------------------------------
971 void wxTopLevelWindowMSW::SetTitle( const wxString
& title
)
976 wxString
wxTopLevelWindowMSW::GetTitle() const
981 void wxTopLevelWindowMSW::SetIcons(const wxIconBundle
& icons
)
983 wxTopLevelWindowBase::SetIcons(icons
);
985 #if !defined(__WXMICROWIN__)
986 const wxIcon
& sml
= icons
.GetIconOfExactSize(16);
989 ::SendMessage( GetHwndOf( this ), WM_SETICON
, ICON_SMALL
,
990 (LPARAM
)GetHiconOf(sml
) );
993 const wxIcon
& big
= icons
.GetIconOfExactSize(32);
996 ::SendMessage( GetHwndOf( this ), WM_SETICON
, ICON_BIG
,
997 (LPARAM
)GetHiconOf(big
) );
999 #endif // !__WXMICROWIN__
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(_T("EnableMenuItem(SC_CLOSE)"));
1025 // update appearance immediately
1026 if ( !::DrawMenuBar(GetHwnd()) )
1028 wxLogLastError(_T("DrawMenuBar"));
1031 #endif // !__WXMICROWIN__
1038 bool wxTopLevelWindowMSW::SetShape(const wxRegion
& region
)
1040 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), false,
1041 _T("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(_T("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
, FALSE
, 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(_T("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 FlashWindowEx_t s_pfnFlashWindowEx
= NULL
;
1095 if ( !s_pfnFlashWindowEx
)
1097 wxDynamicLibrary
dllUser32(_T("user32.dll"));
1098 s_pfnFlashWindowEx
= (FlashWindowEx_t
)
1099 dllUser32
.GetSymbol(_T("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 pSetLayeredWindowAttributes
= NULL
;
1141 if ( pSetLayeredWindowAttributes
== NULL
)
1143 wxDynamicLibrary
dllUser32(_T("user32.dll"));
1144 pSetLayeredWindowAttributes
= (PSETLAYEREDWINDOWATTR
)
1145 dllUser32
.GetSymbol(wxT("SetLayeredWindowAttributes"));
1147 if ( pSetLayeredWindowAttributes
== NULL
)
1149 #endif // wxUSE_DYNLIB_CLASS
1151 LONG exstyle
= GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1153 // if setting alpha to fully opaque then turn off the layered style
1156 SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exstyle
& ~WS_EX_LAYERED
);
1161 #if wxUSE_DYNLIB_CLASS
1162 // Otherwise, set the layered style if needed and set the alpha value
1163 if ((exstyle
& WS_EX_LAYERED
) == 0 )
1164 SetWindowLong(GetHwnd(), GWL_EXSTYLE
, exstyle
| WS_EX_LAYERED
);
1166 if ( pSetLayeredWindowAttributes(GetHwnd(), 0, (BYTE
)alpha
, LWA_ALPHA
) )
1168 #endif // wxUSE_DYNLIB_CLASS
1173 bool wxTopLevelWindowMSW::CanSetTransparent()
1175 // The API is available on win2k and above
1177 static int os_type
= -1;
1178 static int ver_major
= -1;
1181 os_type
= ::wxGetOsVersion(&ver_major
);
1183 return (os_type
== wxOS_WINDOWS_NT
&& ver_major
>= 5);
1186 // ----------------------------------------------------------------------------
1187 // wxTopLevelWindow event handling
1188 // ----------------------------------------------------------------------------
1190 // Default activation behaviour - set the focus for the first child
1192 void wxTopLevelWindowMSW::OnActivate(wxActivateEvent
& event
)
1194 if ( event
.GetActive() )
1196 // restore focus to the child which was last focused unless we already
1198 wxLogTrace(_T("focus"), _T("wxTLW %08x activated."), (int) m_hWnd
);
1200 wxWindow
*winFocus
= FindFocus();
1201 if ( !winFocus
|| wxGetTopLevelParent(winFocus
) != this )
1203 wxWindow
*parent
= m_winLastFocused
? m_winLastFocused
->GetParent()
1210 wxSetFocusToChild(parent
, &m_winLastFocused
);
1213 else // deactivating
1215 // remember the last focused child if it is our child
1216 m_winLastFocused
= FindFocus();
1218 if ( m_winLastFocused
)
1220 // let it know that it doesn't have focus any more
1221 m_winLastFocused
->HandleKillFocus((WXHWND
)NULL
);
1223 // and don't remember it if it's a child from some other frame
1224 if ( wxGetTopLevelParent(m_winLastFocused
) != this )
1226 m_winLastFocused
= NULL
;
1230 wxLogTrace(_T("focus"),
1231 _T("wxTLW %08x deactivated, last focused: %08x."),
1233 (int) (m_winLastFocused
? GetHwndOf(m_winLastFocused
)
1240 // the DialogProc for all wxWidgets dialogs
1241 LONG APIENTRY _EXPORT
1242 wxDlgProc(HWND hDlg
,
1244 WPARAM
WXUNUSED(wParam
),
1245 LPARAM
WXUNUSED(lParam
))
1251 // under CE, add a "Ok" button in the dialog title bar and make it full
1254 // TODO: find the window for this HWND, and take into account
1255 // wxMAXIMIZE and wxCLOSE_BOX. For now, assume both are present.
1257 // Standard SDK doesn't have aygshell.dll: see
1258 // include/wx/msw/wince/libraries.h
1259 #if defined(__WXWINCE__) && !defined(__WINCE_STANDARDSDK__) && !defined(__HANDHELDPC__)
1260 SHINITDLGINFO shidi
;
1261 shidi
.dwMask
= SHIDIM_FLAGS
;
1262 shidi
.dwFlags
= SHIDIF_SIZEDLG
// take account of the SIP or menubar
1263 #ifndef __SMARTPHONE__
1268 SHInitDialog( &shidi
);
1269 #else // no SHInitDialog()
1272 // for WM_INITDIALOG, returning TRUE tells system to set focus to
1273 // the first control in the dialog box, but as we set the focus
1274 // ourselves, we return FALSE for it as well
1279 // for almost all messages, returning FALSE means that we didn't process
1284 // ============================================================================
1285 // wxTLWHiddenParentModule implementation
1286 // ============================================================================
1288 HWND
wxTLWHiddenParentModule::ms_hwnd
= NULL
;
1290 const wxChar
*wxTLWHiddenParentModule::ms_className
= NULL
;
1292 bool wxTLWHiddenParentModule::OnInit()
1295 ms_className
= NULL
;
1300 void wxTLWHiddenParentModule::OnExit()
1304 if ( !::DestroyWindow(ms_hwnd
) )
1306 wxLogLastError(_T("DestroyWindow(hidden TLW parent)"));
1314 if ( !::UnregisterClass(ms_className
, wxGetInstance()) )
1316 wxLogLastError(_T("UnregisterClass(\"wxTLWHiddenParent\")"));
1319 ms_className
= NULL
;
1324 HWND
wxTLWHiddenParentModule::GetHWND()
1328 if ( !ms_className
)
1330 static const wxChar
*HIDDEN_PARENT_CLASS
= _T("wxTLWHiddenParent");
1333 wxZeroMemory(wndclass
);
1335 wndclass
.lpfnWndProc
= DefWindowProc
;
1336 wndclass
.hInstance
= wxGetInstance();
1337 wndclass
.lpszClassName
= HIDDEN_PARENT_CLASS
;
1339 if ( !::RegisterClass(&wndclass
) )
1341 wxLogLastError(_T("RegisterClass(\"wxTLWHiddenParent\")"));
1345 ms_className
= HIDDEN_PARENT_CLASS
;
1349 ms_hwnd
= ::CreateWindow(ms_className
, wxEmptyString
, 0, 0, 0, 0, 0, NULL
,
1350 (HMENU
)NULL
, wxGetInstance(), NULL
);
1353 wxLogLastError(_T("CreateWindow(hidden TLW parent)"));