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