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