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