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