Always Maximize() top-level windows on Smartphone.
[wxWidgets.git] / src / msw / toplevel.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for MSW
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 24.09.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "toplevel.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/toplevel.h"
34 #include "wx/dialog.h"
35 #include "wx/string.h"
36 #include "wx/log.h"
37 #include "wx/intl.h"
38 #include "wx/frame.h"
39 #include "wx/containr.h" // wxSetFocusToChild()
40 #endif //WX_PRECOMP
41
42 #include "wx/module.h"
43 #include "wx/dynlib.h"
44
45 #include "wx/msw/private.h"
46 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__)
47 #include <ole2.h>
48 #include <shellapi.h>
49 // Standard SDK doesn't have aygshell.dll: see include/wx/msw/wince/libraries.h
50 #if _WIN32_WCE < 400 || !defined(__WINCE_STANDARDSDK__)
51 #include <aygshell.h>
52 #endif
53 #include "wx/msw/wince/missing.h"
54 #endif
55
56 #include "wx/msw/missing.h"
57 #include "wx/msw/winundef.h"
58
59 #include "wx/display.h"
60
61 #ifndef ICON_BIG
62 #define ICON_BIG 1
63 #endif
64
65 #ifndef ICON_SMALL
66 #define ICON_SMALL 0
67 #endif
68
69 // ----------------------------------------------------------------------------
70 // stubs for missing functions under MicroWindows
71 // ----------------------------------------------------------------------------
72
73 #ifdef __WXMICROWIN__
74
75 // static inline bool IsIconic(HWND WXUNUSED(hwnd)) { return false; }
76 static inline bool IsZoomed(HWND WXUNUSED(hwnd)) { return false; }
77
78 #endif // __WXMICROWIN__
79
80 // NB: wxDlgProc must be defined here and not in dialog.cpp because the latter
81 // is not included by wxUniv build which does need wxDlgProc
82 LONG APIENTRY _EXPORT
83 wxDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
84
85 // ----------------------------------------------------------------------------
86 // globals
87 // ----------------------------------------------------------------------------
88
89 // the name of the default wxWidgets class
90 #ifdef __WXWINCE__
91 extern wxChar *wxCanvasClassName;
92 #else
93 extern const wxChar *wxCanvasClassName;
94 #endif
95
96 // ----------------------------------------------------------------------------
97 // wxTLWHiddenParentModule: used to manage the hidden parent window (we need a
98 // module to ensure that the window is always deleted)
99 // ----------------------------------------------------------------------------
100
101 class wxTLWHiddenParentModule : public wxModule
102 {
103 public:
104 // module init/finalize
105 virtual bool OnInit();
106 virtual void OnExit();
107
108 // get the hidden window (creates on demand)
109 static HWND GetHWND();
110
111 private:
112 // the HWND of the hidden parent
113 static HWND ms_hwnd;
114
115 // the class used to create it
116 static const wxChar *ms_className;
117
118 DECLARE_DYNAMIC_CLASS(wxTLWHiddenParentModule)
119 };
120
121 IMPLEMENT_DYNAMIC_CLASS(wxTLWHiddenParentModule, wxModule)
122
123 // ============================================================================
124 // wxTopLevelWindowMSW implementation
125 // ============================================================================
126
127 BEGIN_EVENT_TABLE(wxTopLevelWindowMSW, wxTopLevelWindowBase)
128 EVT_ACTIVATE(wxTopLevelWindowMSW::OnActivate)
129 END_EVENT_TABLE()
130
131 // ----------------------------------------------------------------------------
132 // wxTopLevelWindowMSW creation
133 // ----------------------------------------------------------------------------
134
135 void wxTopLevelWindowMSW::Init()
136 {
137 m_iconized =
138 m_maximizeOnShow = false;
139
140 // Data to save/restore when calling ShowFullScreen
141 m_fsStyle = 0;
142 m_fsOldWindowStyle = 0;
143 m_fsIsMaximized = false;
144 m_fsIsShowing = false;
145
146 m_winLastFocused = (wxWindow *)NULL;
147
148 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
149 m_MenuBarHWND = 0;
150 #endif
151 }
152
153 WXDWORD wxTopLevelWindowMSW::MSWGetStyle(long style, WXDWORD *exflags) const
154 {
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
158 (
159 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exflags
160 ) & ~WS_CHILD & ~WS_VISIBLE;
161
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);
167 #endif
168
169 // first select the kind of window being created
170 //
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
174 // don't want
175
176 #if !(defined(__SMARTPHONE__) && defined(__WXWINCE__))
177 // border and caption styles
178 if ( style & wxRESIZE_BORDER )
179 msflags |= WS_THICKFRAME;
180 else if ( exflags && ((style & wxBORDER_DOUBLE) || (style & wxBORDER_RAISED)) )
181 *exflags |= WS_EX_DLGMODALFRAME;
182 else if ( !(style & wxBORDER_NONE) )
183 msflags |= WS_BORDER;
184 #ifndef __POCKETPC__
185 else
186 msflags |= WS_POPUP;
187 #endif
188 #endif
189
190 // normally we consider that all windows without a caption must be popups,
191 // but CE is an exception: there windows normally do not have the caption
192 // but shouldn't be made popups as popups can't have menus and don't look
193 // like normal windows anyhow
194
195 // TODO: Smartphone appears to like wxCAPTION, but we should check that
196 // we need it.
197 #if defined(__SMARTPHONE__) || !defined(__WXWINCE__)
198 if ( style & wxCAPTION )
199 msflags |= WS_CAPTION;
200 #ifndef __WXWINCE__
201 else
202 msflags |= WS_POPUP;
203 #endif // !__WXWINCE__
204 #endif
205
206 // next translate the individual flags
207 if ( style & wxMINIMIZE_BOX )
208 msflags |= WS_MINIMIZEBOX;
209 if ( style & wxMAXIMIZE_BOX )
210 msflags |= WS_MAXIMIZEBOX;
211
212 #ifndef __WXWINCE__
213 if ( style & wxSYSTEM_MENU )
214 msflags |= WS_SYSMENU;
215 #endif
216
217 // NB: under CE these 2 styles are not supported currently, we should
218 // call Minimize()/Maximize() "manually" if we want to support them
219 if ( style & wxMINIMIZE )
220 msflags |= WS_MINIMIZE;
221
222 #if !defined(__POCKETPC__)
223 if ( style & wxMAXIMIZE )
224 msflags |= WS_MAXIMIZE;
225 #endif
226
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;
230
231 if ( exflags )
232 {
233 // there is no taskbar under CE, so omit all this
234 #if !defined(__WXWINCE__)
235 if ( !(GetExtraStyle() & wxTOPLEVEL_EX_DIALOG) )
236 {
237 if ( style & wxFRAME_TOOL_WINDOW )
238 {
239 // create the palette-like window
240 *exflags |= WS_EX_TOOLWINDOW;
241
242 // tool windows shouldn't appear on the taskbar (as documented)
243 style |= wxFRAME_NO_TASKBAR;
244 }
245
246 // We have to solve 2 different problems here:
247 //
248 // 1. frames with wxFRAME_NO_TASKBAR flag shouldn't appear in the
249 // taskbar even if they don't have a parent
250 //
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)
254 //
255 // The second one is solved here by using WS_EX_APPWINDOW flag, the
256 // first one is dealt with in our MSWGetParent() method
257 // implementation
258 if ( !(style & wxFRAME_NO_TASKBAR) && GetParent() )
259 {
260 // need to force the frame to appear in the taskbar
261 *exflags |= WS_EX_APPWINDOW;
262 }
263 //else: nothing to do [here]
264 }
265
266 if ( GetExtraStyle() & wxFRAME_EX_CONTEXTHELP )
267 *exflags |= WS_EX_CONTEXTHELP;
268 #endif // !__WXWINCE__
269
270 if ( style & wxSTAY_ON_TOP )
271 *exflags |= WS_EX_TOPMOST;
272 }
273
274 return msflags;
275 }
276
277 WXHWND wxTopLevelWindowMSW::MSWGetParent() const
278 {
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) )
285 {
286 const wxWindow *parent = GetParent();
287
288 if ( !parent )
289 {
290 // this flag doesn't make sense then and will be ignored
291 wxFAIL_MSG( _T("wxFRAME_FLOAT_ON_PARENT but no parent?") );
292 }
293 else
294 {
295 hwndParent = GetHwndOf(parent);
296 }
297 }
298 //else: don't float on parent, must not be owned
299
300 // now deal with the 2nd taskbar-related problem (see comments above in
301 // MSWGetStyle())
302 if ( HasFlag(wxFRAME_NO_TASKBAR) && !hwndParent )
303 {
304 // use hidden parent
305 hwndParent = wxTLWHiddenParentModule::GetHWND();
306 }
307
308 return (WXHWND)hwndParent;
309 }
310
311 bool wxTopLevelWindowMSW::CreateDialog(const void *dlgTemplate,
312 const wxString& title,
313 const wxPoint& pos,
314 const wxSize& size)
315 {
316 #ifdef __WXMICROWIN__
317 // no dialogs support under MicroWin yet
318 return CreateFrame(title, pos, size);
319 #else // !__WXMICROWIN__
320 wxWindow *parent = GetParent();
321
322 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
323 // app window as parent - this avoids creating modal dialogs without
324 // parent
325 if ( !parent && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) )
326 {
327 parent = wxTheApp->GetTopWindow();
328
329 if ( parent )
330 {
331 // don't use transient windows as parents, this is dangerous as it
332 // can lead to a crash if the parent is destroyed before the child
333 //
334 // also don't use the window which is currently hidden as then the
335 // dialog would be hidden as well
336 if ( (parent->GetExtraStyle() & wxWS_EX_TRANSIENT) ||
337 !parent->IsShown() )
338 {
339 parent = NULL;
340 }
341 }
342 }
343
344 m_hWnd = (WXHWND)::CreateDialogIndirect
345 (
346 wxGetInstance(),
347 (DLGTEMPLATE*)dlgTemplate,
348 parent ? GetHwndOf(parent) : NULL,
349 (DLGPROC)wxDlgProc
350 );
351
352 if ( !m_hWnd )
353 {
354 wxFAIL_MSG(wxT("Failed to create dialog. Incorrect DLGTEMPLATE?"));
355
356 wxLogSysError(wxT("Can't create dialog using memory template"));
357
358 return false;
359 }
360
361 WXDWORD exflags;
362 (void)MSWGetCreateWindowFlags(&exflags);
363
364 if ( exflags )
365 {
366 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE, exflags);
367 ::SetWindowPos(GetHwnd(),
368 exflags & WS_EX_TOPMOST ? HWND_TOPMOST : 0,
369 0, 0, 0, 0,
370 SWP_NOSIZE |
371 SWP_NOMOVE |
372 (exflags & WS_EX_TOPMOST ? 0 : SWP_NOZORDER) |
373 SWP_NOACTIVATE);
374 }
375
376 #if !defined(__WXWINCE__)
377 // For some reason, the system menu is activated when we use the
378 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
379 if ( exflags & WS_EX_CONTEXTHELP )
380 {
381 wxFrame *winTop = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
382 if ( winTop )
383 {
384 wxIcon icon = winTop->GetIcon();
385 if ( icon.Ok() )
386 {
387 ::SendMessage(GetHwnd(), WM_SETICON,
388 (WPARAM)TRUE,
389 (LPARAM)GetHiconOf(icon));
390 }
391 }
392 }
393 #endif
394
395 // move the dialog to its initial position without forcing repainting
396 int x, y, w, h;
397 (void)MSWGetCreateWindowCoords(pos, size, x, y, w, h);
398
399 if ( x == (int)CW_USEDEFAULT )
400 {
401 // centre it on the screen - what else can we do?
402 wxSize sizeDpy = wxGetDisplaySize();
403
404 x = (sizeDpy.x - w) / 2;
405 y = (sizeDpy.y - h) / 2;
406 }
407
408 #if !defined(__WXWINCE__) || defined(__WINCE_STANDARDSDK__)
409 if ( !::MoveWindow(GetHwnd(), x, y, w, h, FALSE) )
410 {
411 wxLogLastError(wxT("MoveWindow"));
412 }
413 #endif
414
415 if ( !title.empty() )
416 {
417 ::SetWindowText(GetHwnd(), title);
418 }
419
420 SubclassWin(m_hWnd);
421
422 #ifdef __SMARTPHONE__
423 // Work around title non-display glitch
424 Show(false);
425 #endif
426
427 return true;
428 #endif // __WXMICROWIN__/!__WXMICROWIN__
429 }
430
431 bool wxTopLevelWindowMSW::CreateFrame(const wxString& title,
432 const wxPoint& pos,
433 const wxSize& size)
434 {
435 WXDWORD exflags;
436 WXDWORD flags = MSWGetCreateWindowFlags(&exflags);
437
438 #if !defined(__HANDHELDPC__) && ((defined(_WIN32_WCE) && _WIN32_WCE < 400) || \
439 defined(__POCKETPC__) || \
440 defined(__SMARTPHONE__))
441 // Always expand to fit the screen in PocketPC or SmartPhone
442 wxSize sz(wxDefaultSize);
443 wxUnusedVar(size);
444 #else // other (including normal desktop) Windows
445 wxSize sz(size);
446 #endif
447
448 bool result = MSWCreate(wxCanvasClassName, title, pos, sz, flags, exflags);
449
450 #ifdef __SMARTPHONE__
451 // Work around title non-display glitch
452 Show(false);
453 #endif
454 return result;
455 }
456
457 bool wxTopLevelWindowMSW::Create(wxWindow *parent,
458 wxWindowID id,
459 const wxString& title,
460 const wxPoint& pos,
461 const wxSize& size,
462 long style,
463 const wxString& name)
464 {
465 bool ret wxDUMMY_INITIALIZE(false);
466
467 // init our fields
468 Init();
469
470 wxSize sizeReal = size;
471 if ( !sizeReal.IsFullySpecified() )
472 {
473 sizeReal.SetDefaults(GetDefaultSize());
474 }
475
476 m_windowStyle = style;
477
478 SetName(name);
479
480 m_windowId = id == wxID_ANY ? NewControlId() : id;
481
482 wxTopLevelWindows.Append(this);
483
484 if ( parent )
485 parent->AddChild(this);
486
487 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
488 {
489 // we have different dialog templates to allows creation of dialogs
490 // with & without captions under MSWindows, resizeable or not (but a
491 // resizeable dialog always has caption - otherwise it would look too
492 // strange)
493
494 // we need 3 additional WORDs for dialog menu, class and title (as we
495 // don't use DS_SETFONT we don't need the fourth WORD for the font)
496 static const int dlgsize = sizeof(DLGTEMPLATE) + (sizeof(WORD) * 3);
497 DLGTEMPLATE *dlgTemplate = (DLGTEMPLATE *)malloc(dlgsize);
498 memset(dlgTemplate, 0, dlgsize);
499
500 // these values are arbitrary, they won't be used normally anyhow
501 dlgTemplate->x = 34;
502 dlgTemplate->y = 22;
503 dlgTemplate->cx = 144;
504 dlgTemplate->cy = 75;
505
506 // reuse the code in MSWGetStyle() but correct the results slightly for
507 // the dialog
508 dlgTemplate->style = MSWGetStyle(style, NULL);
509
510 // all dialogs are popups
511 dlgTemplate->style |= WS_POPUP;
512
513 #ifndef __WXWINCE__
514 // force 3D-look if necessary, it looks impossibly ugly otherwise
515 if ( style & (wxRESIZE_BORDER | wxCAPTION) )
516 dlgTemplate->style |= DS_MODALFRAME;
517 #endif
518
519 ret = CreateDialog(dlgTemplate, title, pos, sizeReal);
520 free(dlgTemplate);
521 }
522 else // !dialog
523 {
524 ret = CreateFrame(title, pos, sizeReal);
525 }
526
527 #ifndef __WXWINCE__
528 if ( ret && !(GetWindowStyleFlag() & wxCLOSE_BOX) )
529 {
530 EnableCloseButton(false);
531 }
532 #endif
533
534 // for some reason we need to manually send ourselves this message as
535 // otherwise the mnemonics are always shown -- even if they're configured
536 // to be hidden until "Alt" is pressed in the control panel
537 //
538 // this could indicate a bug somewhere else but for now this is the only
539 // fix we have
540 if ( ret )
541 {
542 ::SendMessage
543 (
544 GetHwnd(),
545 WM_UPDATEUISTATE,
546 MAKEWPARAM(UIS_INITIALIZE, UISF_HIDEFOCUS | UISF_HIDEACCEL),
547 0
548 );
549 }
550
551 // Note: if we include PocketPC in this test, dialogs can fail to show up,
552 // for example the text entry dialog in the dialogs sample. Problem with Maximise()?
553 #if defined(__WXWINCE__) && (defined(__SMARTPHONE__) || defined(__WINCE_STANDARDSDK__))
554 // Smartphone dialogs should always be maximized.
555 #if !defined(__SMARTPHONE__)
556 if ( style & wxMAXIMIZE )
557 #endif
558 {
559 this->Maximize();
560 }
561 #endif
562
563 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
564 SetRightMenu(); // to nothing for initialization
565 #endif
566
567 return ret;
568 }
569
570 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
571 {
572 // after destroying an owned window, Windows activates the next top level
573 // window in Z order but it may be different from our owner (to reproduce
574 // this simply Alt-TAB to another application and back before closing the
575 // owned frame) whereas we always want to yield activation to our parent
576 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT) )
577 {
578 wxWindow *parent = GetParent();
579 if ( parent )
580 {
581 ::BringWindowToTop(GetHwndOf(parent));
582 }
583 }
584 }
585
586 // ----------------------------------------------------------------------------
587 // wxTopLevelWindowMSW showing
588 // ----------------------------------------------------------------------------
589
590 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd)
591 {
592 ::ShowWindow(GetHwnd(), nShowCmd);
593
594 m_iconized = nShowCmd == SW_MINIMIZE;
595 }
596
597 bool wxTopLevelWindowMSW::Show(bool show)
598 {
599 // don't use wxWindow version as we want to call DoShowWindow() ourselves
600 if ( !wxWindowBase::Show(show) )
601 return false;
602
603 int nShowCmd;
604 if ( show )
605 {
606 if ( m_maximizeOnShow )
607 {
608 // show and maximize
609 nShowCmd = SW_MAXIMIZE;
610
611 // This is necessary, or no window appears
612 #if defined( __WINCE_STANDARDSDK__) || defined(__SMARTPHONE__)
613 DoShowWindow(SW_SHOW);
614 #endif
615
616 m_maximizeOnShow = false;
617 }
618 else // just show
619 {
620 if ( GetWindowStyle() & wxFRAME_TOOL_WINDOW )
621 nShowCmd = SW_SHOWNA;
622 else
623 nShowCmd = SW_SHOW;
624 }
625 }
626 else // hide
627 {
628 nShowCmd = SW_HIDE;
629 }
630
631 DoShowWindow(nShowCmd);
632
633 #if defined(__WXWINCE__) && (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__))
634 // Addornments have to be added when the frame is the correct size
635 wxFrame* frame = wxDynamicCast(this, wxFrame);
636 if (frame && frame->GetMenuBar())
637 frame->GetMenuBar()->AddAdornments(GetWindowStyleFlag());
638 #endif
639
640 if ( show )
641 {
642 ::BringWindowToTop(GetHwnd());
643
644 wxActivateEvent event(wxEVT_ACTIVATE, true, m_windowId);
645 event.SetEventObject( this );
646 GetEventHandler()->ProcessEvent(event);
647 }
648 else // hide
649 {
650 // Try to highlight the correct window (the parent)
651 if ( GetParent() )
652 {
653 HWND hWndParent = GetHwndOf(GetParent());
654 if (hWndParent)
655 ::BringWindowToTop(hWndParent);
656 }
657 }
658
659 return true;
660 }
661
662 // ----------------------------------------------------------------------------
663 // wxTopLevelWindowMSW maximize/minimize
664 // ----------------------------------------------------------------------------
665
666 void wxTopLevelWindowMSW::Maximize(bool maximize)
667 {
668 if ( IsShown() )
669 {
670 // just maximize it directly
671 DoShowWindow(maximize ? SW_MAXIMIZE : SW_RESTORE);
672 }
673 else // hidden
674 {
675 // we can't maximize the hidden frame because it shows it as well, so
676 // just remember that we should do it later in this case
677 m_maximizeOnShow = maximize;
678 }
679 }
680
681 bool wxTopLevelWindowMSW::IsMaximized() const
682 {
683 #ifdef __WXWINCE__
684 return false;
685 #else
686 return ::IsZoomed(GetHwnd()) != 0;
687 #endif
688 }
689
690 void wxTopLevelWindowMSW::Iconize(bool iconize)
691 {
692 DoShowWindow(iconize ? SW_MINIMIZE : SW_RESTORE);
693 }
694
695 bool wxTopLevelWindowMSW::IsIconized() const
696 {
697 #ifdef __WXWINCE__
698 return false;
699 #else
700 // also update the current state
701 ((wxTopLevelWindowMSW *)this)->m_iconized = ::IsIconic(GetHwnd()) != 0;
702
703 return m_iconized;
704 #endif
705 }
706
707 void wxTopLevelWindowMSW::Restore()
708 {
709 DoShowWindow(SW_RESTORE);
710 }
711
712 // ----------------------------------------------------------------------------
713 // wxTopLevelWindowMSW fullscreen
714 // ----------------------------------------------------------------------------
715
716 bool wxTopLevelWindowMSW::ShowFullScreen(bool show, long style)
717 {
718 if ( show == IsFullScreen() )
719 {
720 // nothing to do
721 return true;
722 }
723
724 m_fsIsShowing = show;
725
726 if ( show )
727 {
728 m_fsStyle = style;
729
730 // zap the frame borders
731
732 // save the 'normal' window style
733 m_fsOldWindowStyle = GetWindowLong(GetHwnd(), GWL_STYLE);
734
735 // save the old position, width & height, maximize state
736 m_fsOldSize = GetRect();
737 m_fsIsMaximized = IsMaximized();
738
739 // decide which window style flags to turn off
740 LONG newStyle = m_fsOldWindowStyle;
741 LONG offFlags = 0;
742
743 if (style & wxFULLSCREEN_NOBORDER)
744 {
745 offFlags |= WS_BORDER;
746 #ifndef __WXWINCE__
747 offFlags |= WS_THICKFRAME;
748 #endif
749 }
750 if (style & wxFULLSCREEN_NOCAPTION)
751 offFlags |= WS_CAPTION | WS_SYSMENU;
752
753 newStyle &= ~offFlags;
754
755 // change our window style to be compatible with full-screen mode
756 ::SetWindowLong(GetHwnd(), GWL_STYLE, newStyle);
757
758 wxRect rect;
759 #if wxUSE_DISPLAY
760 // resize to the size of the display containing us
761 int dpy = wxDisplay::GetFromWindow(this);
762 if ( dpy != wxNOT_FOUND )
763 {
764 rect = wxDisplay(dpy).GetGeometry();
765 }
766 else // fall back to the main desktop
767 #else // wxUSE_DISPLAY
768 {
769 // resize to the size of the desktop
770 wxCopyRECTToRect(wxGetWindowRect(::GetDesktopWindow()), rect);
771 #ifdef __WXWINCE__
772 // FIXME: size of the bottom menu (toolbar)
773 // should be taken in account
774 rect.height += rect.y;
775 rect.y = 0;
776 #endif
777 }
778 #endif // wxUSE_DISPLAY
779
780 SetSize(rect);
781
782 // now flush the window style cache and actually go full-screen
783 long flags = SWP_FRAMECHANGED;
784
785 // showing the frame full screen should also show it if it's still
786 // hidden
787 if ( !IsShown() )
788 {
789 // don't call wxWindow version to avoid flicker from calling
790 // ::ShowWindow() -- we're going to show the window at the correct
791 // location directly below -- but do call the wxWindowBase version
792 // to sync the internal m_isShown flag
793 wxWindowBase::Show();
794
795 flags |= SWP_SHOWWINDOW;
796 }
797
798 SetWindowPos(GetHwnd(), HWND_TOP,
799 rect.x, rect.y, rect.width, rect.height,
800 flags);
801
802 #if !defined(__HANDHELDPC__) && (defined(__WXWINCE__) && (_WIN32_WCE < 400))
803 ::SHFullScreen(GetHwnd(), SHFS_HIDETASKBAR | SHFS_HIDESIPBUTTON);
804 #endif
805
806 // finally send an event allowing the window to relayout itself &c
807 wxSizeEvent event(rect.GetSize(), GetId());
808 GetEventHandler()->ProcessEvent(event);
809 }
810 else // stop showing full screen
811 {
812 #if !defined(__HANDHELDPC__) && (defined(__WXWINCE__) && (_WIN32_WCE < 400))
813 ::SHFullScreen(GetHwnd(), SHFS_SHOWTASKBAR | SHFS_SHOWSIPBUTTON);
814 #endif
815 Maximize(m_fsIsMaximized);
816 SetWindowLong(GetHwnd(),GWL_STYLE, m_fsOldWindowStyle);
817 SetWindowPos(GetHwnd(),HWND_TOP,m_fsOldSize.x, m_fsOldSize.y,
818 m_fsOldSize.width, m_fsOldSize.height, SWP_FRAMECHANGED);
819 }
820
821 return true;
822 }
823
824 // ----------------------------------------------------------------------------
825 // wxTopLevelWindowMSW misc
826 // ----------------------------------------------------------------------------
827
828 void wxTopLevelWindowMSW::SetIcon(const wxIcon& icon)
829 {
830 SetIcons( wxIconBundle( icon ) );
831 }
832
833 void wxTopLevelWindowMSW::SetIcons(const wxIconBundle& icons)
834 {
835 wxTopLevelWindowBase::SetIcons(icons);
836
837 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
838 const wxIcon& sml = icons.GetIcon( wxSize( 16, 16 ) );
839 if( sml.Ok() && sml.GetWidth() == 16 && sml.GetHeight() == 16 )
840 {
841 ::SendMessage( GetHwndOf( this ), WM_SETICON, ICON_SMALL,
842 (LPARAM)GetHiconOf(sml) );
843 }
844
845 const wxIcon& big = icons.GetIcon( wxSize( 32, 32 ) );
846 if( big.Ok() && big.GetWidth() == 32 && big.GetHeight() == 32 )
847 {
848 ::SendMessage( GetHwndOf( this ), WM_SETICON, ICON_BIG,
849 (LPARAM)GetHiconOf(big) );
850 }
851 #endif // __WIN95__
852 }
853
854 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable)
855 {
856 #if !defined(__WXMICROWIN__)
857 // get system (a.k.a. window) menu
858 HMENU hmenu = GetSystemMenu(GetHwnd(), FALSE /* get it */);
859 if ( !hmenu )
860 {
861 // no system menu at all -- ok if we want to remove the close button
862 // anyhow, but bad if we want to show it
863 return !enable;
864 }
865
866 // enabling/disabling the close item from it also automatically
867 // disables/enables the close title bar button
868 if ( ::EnableMenuItem(hmenu, SC_CLOSE,
869 MF_BYCOMMAND |
870 (enable ? MF_ENABLED : MF_GRAYED)) == -1 )
871 {
872 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
873
874 return false;
875 }
876 #ifndef __WXWINCE__
877 // update appearance immediately
878 if ( !::DrawMenuBar(GetHwnd()) )
879 {
880 wxLogLastError(_T("DrawMenuBar"));
881 }
882 #endif
883 #endif // !__WXMICROWIN__
884
885 return true;
886 }
887
888 #ifndef __WXWINCE__
889
890 bool wxTopLevelWindowMSW::SetShape(const wxRegion& region)
891 {
892 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
893 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
894
895 // The empty region signifies that the shape should be removed from the
896 // window.
897 if ( region.IsEmpty() )
898 {
899 if (::SetWindowRgn(GetHwnd(), NULL, TRUE) == 0)
900 {
901 wxLogLastError(_T("SetWindowRgn"));
902 return false;
903 }
904 return true;
905 }
906
907 // Windows takes ownership of the region, so
908 // we'll have to make a copy of the region to give to it.
909 DWORD noBytes = ::GetRegionData(GetHrgnOf(region), 0, NULL);
910 RGNDATA *rgnData = (RGNDATA*) new char[noBytes];
911 ::GetRegionData(GetHrgnOf(region), noBytes, rgnData);
912 HRGN hrgn = ::ExtCreateRegion(NULL, noBytes, rgnData);
913 delete[] (char*) rgnData;
914
915 // SetWindowRgn expects the region to be in coordinants
916 // relative to the window, not the client area. Figure
917 // out the offset, if any.
918 RECT rect;
919 DWORD dwStyle = ::GetWindowLong(GetHwnd(), GWL_STYLE);
920 DWORD dwExStyle = ::GetWindowLong(GetHwnd(), GWL_EXSTYLE);
921 ::GetClientRect(GetHwnd(), &rect);
922 ::AdjustWindowRectEx(&rect, dwStyle, FALSE, dwExStyle);
923 ::OffsetRgn(hrgn, -rect.left, -rect.top);
924
925 // Now call the shape API with the new region.
926 if (::SetWindowRgn(GetHwnd(), hrgn, TRUE) == 0)
927 {
928 wxLogLastError(_T("SetWindowRgn"));
929 return false;
930 }
931 return true;
932 }
933
934 #endif // !__WXWINCE__
935
936 void wxTopLevelWindowMSW::RequestUserAttention(int flags)
937 {
938 // check if we can use FlashWindowEx(): unfortunately an explicit test for
939 // FLASHW_STOP, for example, doesn't work because MSVC6 headers do #define
940 // it but don't provide FlashWindowEx() declaration
941 #if (WINVER >= 0x0500 && (defined FLASHW_STOP))
942 // available in the headers, check if it is supported by the system
943 typedef BOOL (WINAPI *FlashWindowEx_t)(FLASHWINFO *pfwi);
944 FlashWindowEx_t s_pfnFlashWindowEx = NULL;
945 if ( !s_pfnFlashWindowEx )
946 {
947 wxDynamicLibrary dllUser32(_T("user32.dll"));
948 s_pfnFlashWindowEx = (FlashWindowEx_t)
949 dllUser32.GetSymbol(_T("FlashWindowEx"));
950
951 // we can safely unload user32.dll here, it's goign to remain loaded as
952 // long as the program is running anyhow
953 }
954
955 if ( s_pfnFlashWindowEx )
956 {
957 WinStruct<FLASHWINFO> fwi;
958 fwi.hwnd = GetHwnd();
959 fwi.dwFlags = FLASHW_ALL;
960 if ( flags & wxUSER_ATTENTION_INFO )
961 {
962 // just flash a few times
963 fwi.uCount = 3;
964 }
965 else // wxUSER_ATTENTION_ERROR
966 {
967 // flash until the user notices it
968 fwi.dwFlags |= FLASHW_TIMERNOFG;
969 }
970
971 s_pfnFlashWindowEx(&fwi);
972 }
973 else // FlashWindowEx() not available
974 #endif // FlashWindowEx() defined
975 {
976 wxUnusedVar(flags);
977 #ifndef __WXWINCE__
978 ::FlashWindow(GetHwnd(), TRUE);
979 #endif // __WXWINCE__
980 }
981 }
982
983 // ----------------------------------------------------------------------------
984 // wxTopLevelWindow event handling
985 // ----------------------------------------------------------------------------
986
987 // Default activation behaviour - set the focus for the first child
988 // subwindow found.
989 void wxTopLevelWindowMSW::OnActivate(wxActivateEvent& event)
990 {
991 if ( event.GetActive() )
992 {
993 // restore focus to the child which was last focused unless we already
994 // have it
995 wxLogTrace(_T("focus"), _T("wxTLW %08x activated."), (int) m_hWnd);
996
997 wxWindow *winFocus = FindFocus();
998 if ( !winFocus || wxGetTopLevelParent(winFocus) != this )
999 {
1000 wxWindow *parent = m_winLastFocused ? m_winLastFocused->GetParent()
1001 : NULL;
1002 if ( !parent )
1003 {
1004 parent = this;
1005 }
1006
1007 wxSetFocusToChild(parent, &m_winLastFocused);
1008 }
1009 }
1010 else // deactivating
1011 {
1012 // remember the last focused child if it is our child
1013 m_winLastFocused = FindFocus();
1014
1015 if ( m_winLastFocused )
1016 {
1017 // let it know that it doesn't have focus any more
1018 m_winLastFocused->HandleKillFocus((WXHWND)NULL);
1019
1020 // and don't remember it if it's a child from some other frame
1021 if ( wxGetTopLevelParent(m_winLastFocused) != this )
1022 {
1023 m_winLastFocused = NULL;
1024 }
1025 }
1026
1027 wxLogTrace(_T("focus"),
1028 _T("wxTLW %08x deactivated, last focused: %08x."),
1029 (int) m_hWnd,
1030 (int) (m_winLastFocused ? GetHwndOf(m_winLastFocused)
1031 : NULL));
1032
1033 event.Skip();
1034 }
1035 }
1036
1037 // the DialogProc for all wxWidgets dialogs
1038 LONG APIENTRY _EXPORT
1039 wxDlgProc(HWND hDlg,
1040 UINT message,
1041 WPARAM WXUNUSED(wParam),
1042 LPARAM WXUNUSED(lParam))
1043 {
1044 if ( message == WM_INITDIALOG )
1045 {
1046 // under CE, add a "Ok" button in the dialog title bar and make it full
1047 // screen
1048 //
1049 // TODO: find the window for this HWND, and take into account
1050 // wxMAXIMIZE and wxCLOSE_BOX. For now, assume both are present.
1051 //
1052 // Standard SDK doesn't have aygshell.dll: see
1053 // include/wx/msw/wince/libraries.h
1054 #if defined(__WXWINCE__) && !defined(__WINCE_STANDARDSDK__) && !defined(__HANDHELDPC__)
1055 SHINITDLGINFO shidi;
1056 shidi.dwMask = SHIDIM_FLAGS;
1057 shidi.dwFlags = SHIDIF_SIZEDLG // take account of the SIP or menubar
1058 #ifndef __SMARTPHONE__
1059 | SHIDIF_DONEBUTTON
1060 #endif
1061 ;
1062 shidi.hDlg = hDlg;
1063 SHInitDialog( &shidi );
1064 #else // no SHInitDialog()
1065 wxUnusedVar(hDlg);
1066 #endif
1067 }
1068
1069 // for almost all messages, returning FALSE means that we didn't process
1070 // the message
1071 //
1072 // for WM_INITDIALOG, returning TRUE tells system to set focus to
1073 // the first control in the dialog box, but as we set the focus
1074 // ourselves, we return FALSE for it as well
1075 return FALSE;
1076 }
1077
1078 // ============================================================================
1079 // wxTLWHiddenParentModule implementation
1080 // ============================================================================
1081
1082 HWND wxTLWHiddenParentModule::ms_hwnd = NULL;
1083
1084 const wxChar *wxTLWHiddenParentModule::ms_className = NULL;
1085
1086 bool wxTLWHiddenParentModule::OnInit()
1087 {
1088 ms_hwnd = NULL;
1089 ms_className = NULL;
1090
1091 return true;
1092 }
1093
1094 void wxTLWHiddenParentModule::OnExit()
1095 {
1096 if ( ms_hwnd )
1097 {
1098 if ( !::DestroyWindow(ms_hwnd) )
1099 {
1100 wxLogLastError(_T("DestroyWindow(hidden TLW parent)"));
1101 }
1102
1103 ms_hwnd = NULL;
1104 }
1105
1106 if ( ms_className )
1107 {
1108 if ( !::UnregisterClass(ms_className, wxGetInstance()) )
1109 {
1110 wxLogLastError(_T("UnregisterClass(\"wxTLWHiddenParent\")"));
1111 }
1112
1113 ms_className = NULL;
1114 }
1115 }
1116
1117 /* static */
1118 HWND wxTLWHiddenParentModule::GetHWND()
1119 {
1120 if ( !ms_hwnd )
1121 {
1122 if ( !ms_className )
1123 {
1124 static const wxChar *HIDDEN_PARENT_CLASS = _T("wxTLWHiddenParent");
1125
1126 WNDCLASS wndclass;
1127 wxZeroMemory(wndclass);
1128
1129 wndclass.lpfnWndProc = DefWindowProc;
1130 wndclass.hInstance = wxGetInstance();
1131 wndclass.lpszClassName = HIDDEN_PARENT_CLASS;
1132
1133 if ( !::RegisterClass(&wndclass) )
1134 {
1135 wxLogLastError(_T("RegisterClass(\"wxTLWHiddenParent\")"));
1136 }
1137 else
1138 {
1139 ms_className = HIDDEN_PARENT_CLASS;
1140 }
1141 }
1142
1143 ms_hwnd = ::CreateWindow(ms_className, wxEmptyString, 0, 0, 0, 0, 0, NULL,
1144 (HMENU)NULL, wxGetInstance(), NULL);
1145 if ( !ms_hwnd )
1146 {
1147 wxLogLastError(_T("CreateWindow(hidden TLW parent)"));
1148 }
1149 }
1150
1151 return ms_hwnd;
1152 }
1153
1154