Fix to make frame visible under eVC 3.0
[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 if ( !::MoveWindow(GetHwnd(), x, y, w, h, FALSE) )
388 {
389 wxLogLastError(wxT("MoveWindow"));
390 }
391
392 if ( !title.empty() )
393 {
394 ::SetWindowText(GetHwnd(), title);
395 }
396
397 SubclassWin(m_hWnd);
398
399 return TRUE;
400 #endif // __WXMICROWIN__/!__WXMICROWIN__
401 }
402
403 bool wxTopLevelWindowMSW::CreateFrame(const wxString& title,
404 const wxPoint& pos,
405 const wxSize& size)
406 {
407 WXDWORD exflags;
408 WXDWORD flags = MSWGetCreateWindowFlags(&exflags);
409
410 return MSWCreate(wxCanvasClassName, title, pos, size, flags, exflags);
411 }
412
413 bool wxTopLevelWindowMSW::Create(wxWindow *parent,
414 wxWindowID id,
415 const wxString& title,
416 const wxPoint& pos,
417 const wxSize& size,
418 long style,
419 const wxString& name)
420 {
421 bool ret wxDUMMY_INITIALIZE(false);
422
423 // init our fields
424 Init();
425
426 wxSize sizeReal = size;
427 if ( !sizeReal.IsFullySpecified() )
428 {
429 sizeReal.SetDefaults(GetDefaultSize());
430 }
431
432 m_windowStyle = style;
433
434 SetName(name);
435
436 m_windowId = id == -1 ? NewControlId() : id;
437
438 wxTopLevelWindows.Append(this);
439
440 if ( parent )
441 parent->AddChild(this);
442
443 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
444 {
445 // we have different dialog templates to allows creation of dialogs
446 // with & without captions under MSWindows, resizeable or not (but a
447 // resizeable dialog always has caption - otherwise it would look too
448 // strange)
449
450 // we need 3 additional WORDs for dialog menu, class and title (as we
451 // don't use DS_SETFONT we don't need the fourth WORD for the font)
452 static const int dlgsize = sizeof(DLGTEMPLATE) + (sizeof(WORD) * 3);
453 DLGTEMPLATE *dlgTemplate = (DLGTEMPLATE *)malloc(dlgsize);
454 memset(dlgTemplate, 0, dlgsize);
455
456 // these values are arbitrary, they won't be used normally anyhow
457 dlgTemplate->x = 34;
458 dlgTemplate->y = 22;
459 dlgTemplate->cx = 144;
460 dlgTemplate->cy = 75;
461
462 // reuse the code in MSWGetStyle() but correct the results slightly for
463 // the dialog
464 dlgTemplate->style = MSWGetStyle(style, NULL);
465
466 // all dialogs are popups
467 dlgTemplate->style |= WS_POPUP;
468
469 // force 3D-look if necessary, it looks impossibly ugly otherwise
470 if ( style & (wxRESIZE_BORDER | wxCAPTION) )
471 dlgTemplate->style |= DS_MODALFRAME;
472
473 ret = CreateDialog(dlgTemplate, title, pos, sizeReal);
474 free(dlgTemplate);
475 }
476 else // !dialog
477 {
478 ret = CreateFrame(title, pos, sizeReal);
479 }
480
481 if ( ret && !(GetWindowStyleFlag() & wxCLOSE_BOX) )
482 {
483 EnableCloseButton(false);
484 }
485
486 // for some reason we need to manually send ourselves this message as
487 // otherwise the mnemonics are always shown -- even if they're configured
488 // to be hidden until "Alt" is pressed in the control panel
489 //
490 // this could indicate a bug somewhere else but for now this is the only
491 // fix we have
492 if ( ret )
493 {
494 SendMessage
495 (
496 GetHwnd(),
497 WM_UPDATEUISTATE,
498 MAKEWPARAM(UIS_INITIALIZE, UISF_HIDEFOCUS | UISF_HIDEACCEL),
499 0
500 );
501 }
502
503 return ret;
504 }
505
506 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
507 {
508 if ( wxModelessWindows.Find(this) )
509 wxModelessWindows.DeleteObject(this);
510
511 // after destroying an owned window, Windows activates the next top level
512 // window in Z order but it may be different from our owner (to reproduce
513 // this simply Alt-TAB to another application and back before closing the
514 // owned frame) whereas we always want to yield activation to our parent
515 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT) )
516 {
517 wxWindow *parent = GetParent();
518 if ( parent )
519 {
520 ::BringWindowToTop(GetHwndOf(parent));
521 }
522 }
523 }
524
525 // ----------------------------------------------------------------------------
526 // wxTopLevelWindowMSW showing
527 // ----------------------------------------------------------------------------
528
529 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd)
530 {
531 ::ShowWindow(GetHwnd(), nShowCmd);
532
533 m_iconized = nShowCmd == SW_MINIMIZE;
534 }
535
536 bool wxTopLevelWindowMSW::Show(bool show)
537 {
538 // don't use wxWindow version as we want to call DoShowWindow() ourselves
539 if ( !wxWindowBase::Show(show) )
540 return FALSE;
541
542 int nShowCmd;
543 if ( show )
544 {
545 if ( m_maximizeOnShow )
546 {
547 // show and maximize
548 nShowCmd = SW_MAXIMIZE;
549
550 m_maximizeOnShow = FALSE;
551 }
552 else // just show
553 {
554 if ( GetWindowStyle() & wxFRAME_TOOL_WINDOW )
555 nShowCmd = SW_SHOWNA;
556 else
557 nShowCmd = SW_SHOW;
558 }
559 }
560 else // hide
561 {
562 nShowCmd = SW_HIDE;
563 }
564
565 DoShowWindow(nShowCmd);
566
567 if ( show )
568 {
569 ::BringWindowToTop(GetHwnd());
570
571 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, m_windowId);
572 event.SetEventObject( this );
573 GetEventHandler()->ProcessEvent(event);
574 }
575 else // hide
576 {
577 // Try to highlight the correct window (the parent)
578 if ( GetParent() )
579 {
580 HWND hWndParent = GetHwndOf(GetParent());
581 if (hWndParent)
582 ::BringWindowToTop(hWndParent);
583 }
584 }
585
586 return TRUE;
587 }
588
589 // ----------------------------------------------------------------------------
590 // wxTopLevelWindowMSW maximize/minimize
591 // ----------------------------------------------------------------------------
592
593 void wxTopLevelWindowMSW::Maximize(bool maximize)
594 {
595 if ( IsShown() )
596 {
597 // just maximize it directly
598 DoShowWindow(maximize ? SW_MAXIMIZE : SW_RESTORE);
599 }
600 else // hidden
601 {
602 // we can't maximize the hidden frame because it shows it as well, so
603 // just remember that we should do it later in this case
604 m_maximizeOnShow = maximize;
605 }
606 }
607
608 bool wxTopLevelWindowMSW::IsMaximized() const
609 {
610 #ifdef __WXWINCE__
611 return FALSE;
612 #else
613 return ::IsZoomed(GetHwnd()) != 0;
614 #endif
615 }
616
617 void wxTopLevelWindowMSW::Iconize(bool iconize)
618 {
619 DoShowWindow(iconize ? SW_MINIMIZE : SW_RESTORE);
620 }
621
622 bool wxTopLevelWindowMSW::IsIconized() const
623 {
624 #ifdef __WXWINCE__
625 return FALSE;
626 #else
627 // also update the current state
628 ((wxTopLevelWindowMSW *)this)->m_iconized = ::IsIconic(GetHwnd()) != 0;
629
630 return m_iconized;
631 #endif
632 }
633
634 void wxTopLevelWindowMSW::Restore()
635 {
636 DoShowWindow(SW_RESTORE);
637 }
638
639 // ----------------------------------------------------------------------------
640 // wxTopLevelWindowMSW fullscreen
641 // ----------------------------------------------------------------------------
642
643 bool wxTopLevelWindowMSW::ShowFullScreen(bool show, long style)
644 {
645 if ( show == IsFullScreen() )
646 {
647 // nothing to do
648 return TRUE;
649 }
650
651 m_fsIsShowing = show;
652
653 if ( show )
654 {
655 m_fsStyle = style;
656
657 // zap the frame borders
658
659 // save the 'normal' window style
660 m_fsOldWindowStyle = GetWindowLong(GetHwnd(), GWL_STYLE);
661
662 // save the old position, width & height, maximize state
663 m_fsOldSize = GetRect();
664 m_fsIsMaximized = IsMaximized();
665
666 // decide which window style flags to turn off
667 LONG newStyle = m_fsOldWindowStyle;
668 LONG offFlags = 0;
669
670 if (style & wxFULLSCREEN_NOBORDER)
671 {
672 offFlags |= WS_BORDER;
673 #ifndef __WXWINCE__
674 offFlags |= WS_THICKFRAME;
675 #endif
676 }
677 if (style & wxFULLSCREEN_NOCAPTION)
678 offFlags |= WS_CAPTION | WS_SYSMENU;
679
680 newStyle &= ~offFlags;
681
682 // change our window style to be compatible with full-screen mode
683 ::SetWindowLong(GetHwnd(), GWL_STYLE, newStyle);
684
685 wxRect rect;
686 #if wxUSE_DISPLAY
687 // resize to the size of the display containing us
688 int dpy = wxDisplay::GetFromWindow(this);
689 if ( dpy != wxNOT_FOUND )
690 {
691 rect = wxDisplay(dpy).GetGeometry();
692 }
693 else // fall back to the main desktop
694 #else // wxUSE_DISPLAY
695 {
696 // resize to the size of the desktop
697 wxCopyRECTToRect(wxGetWindowRect(::GetDesktopWindow()), rect);
698 #ifdef __WXWINCE__
699 // FIXME: size of the bottom menu (toolbar)
700 // should be taken in account
701 rect.height += rect.y;
702 rect.y = 0;
703 #endif
704 }
705 #endif // wxUSE_DISPLAY
706
707 SetSize(rect);
708
709 // now flush the window style cache and actually go full-screen
710 long flags = SWP_FRAMECHANGED;
711
712 // showing the frame full screen should also show it if it's still
713 // hidden
714 if ( !IsShown() )
715 {
716 // don't call wxWindow version to avoid flicker from calling
717 // ::ShowWindow() -- we're going to show the window at the correct
718 // location directly below -- but do call the wxWindowBase version
719 // to sync the internal m_isShown flag
720 wxWindowBase::Show();
721
722 flags |= SWP_SHOWWINDOW;
723 }
724
725 SetWindowPos(GetHwnd(), HWND_TOP,
726 rect.x, rect.y, rect.width, rect.height,
727 flags);
728
729 #if defined(__WXWINCE__) && _WIN32_WCE < 400
730 ::SHFullScreen(GetHwnd(), SHFS_HIDETASKBAR | SHFS_HIDESIPBUTTON);
731 #endif
732
733 // finally send an event allowing the window to relayout itself &c
734 wxSizeEvent event(rect.GetSize(), GetId());
735 GetEventHandler()->ProcessEvent(event);
736 }
737 else // stop showing full screen
738 {
739 #if defined(__WXWINCE__) && _WIN32_WCE < 400
740 ::SHFullScreen(GetHwnd(), SHFS_SHOWTASKBAR | SHFS_SHOWSIPBUTTON);
741 #endif
742 Maximize(m_fsIsMaximized);
743 SetWindowLong(GetHwnd(),GWL_STYLE, m_fsOldWindowStyle);
744 SetWindowPos(GetHwnd(),HWND_TOP,m_fsOldSize.x, m_fsOldSize.y,
745 m_fsOldSize.width, m_fsOldSize.height, SWP_FRAMECHANGED);
746 }
747
748 return TRUE;
749 }
750
751 // ----------------------------------------------------------------------------
752 // wxTopLevelWindowMSW misc
753 // ----------------------------------------------------------------------------
754
755 void wxTopLevelWindowMSW::SetIcon(const wxIcon& icon)
756 {
757 SetIcons( wxIconBundle( icon ) );
758 }
759
760 void wxTopLevelWindowMSW::SetIcons(const wxIconBundle& icons)
761 {
762 wxTopLevelWindowBase::SetIcons(icons);
763
764 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
765 const wxIcon& sml = icons.GetIcon( wxSize( 16, 16 ) );
766 if( sml.Ok() && sml.GetWidth() == 16 && sml.GetHeight() == 16 )
767 {
768 ::SendMessage( GetHwndOf( this ), WM_SETICON, ICON_SMALL,
769 (LPARAM)GetHiconOf(sml) );
770 }
771
772 const wxIcon& big = icons.GetIcon( wxSize( 32, 32 ) );
773 if( big.Ok() && big.GetWidth() == 32 && big.GetHeight() == 32 )
774 {
775 ::SendMessage( GetHwndOf( this ), WM_SETICON, ICON_BIG,
776 (LPARAM)GetHiconOf(big) );
777 }
778 #endif // __WIN95__
779 }
780
781 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable)
782 {
783 #if !defined(__WXMICROWIN__)
784 // get system (a.k.a. window) menu
785 HMENU hmenu = GetSystemMenu(GetHwnd(), FALSE /* get it */);
786 if ( !hmenu )
787 {
788 // no system menu at all -- ok if we want to remove the close button
789 // anyhow, but bad if we want to show it
790 return !enable;
791 }
792
793 // enabling/disabling the close item from it also automatically
794 // disables/enables the close title bar button
795 if ( ::EnableMenuItem(hmenu, SC_CLOSE,
796 MF_BYCOMMAND |
797 (enable ? MF_ENABLED : MF_GRAYED)) == -1 )
798 {
799 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
800
801 return FALSE;
802 }
803
804 // update appearance immediately
805 if ( !::DrawMenuBar(GetHwnd()) )
806 {
807 wxLogLastError(_T("DrawMenuBar"));
808 }
809 #endif // !__WXMICROWIN__
810
811 return TRUE;
812 }
813
814 #ifndef __WXWINCE__
815
816 bool wxTopLevelWindowMSW::SetShape(const wxRegion& region)
817 {
818 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), FALSE,
819 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
820
821 // The empty region signifies that the shape should be removed from the
822 // window.
823 if ( region.IsEmpty() )
824 {
825 if (::SetWindowRgn(GetHwnd(), NULL, TRUE) == 0)
826 {
827 wxLogLastError(_T("SetWindowRgn"));
828 return FALSE;
829 }
830 return TRUE;
831 }
832
833 // Windows takes ownership of the region, so
834 // we'll have to make a copy of the region to give to it.
835 DWORD noBytes = ::GetRegionData(GetHrgnOf(region), 0, NULL);
836 RGNDATA *rgnData = (RGNDATA*) new char[noBytes];
837 ::GetRegionData(GetHrgnOf(region), noBytes, rgnData);
838 HRGN hrgn = ::ExtCreateRegion(NULL, noBytes, rgnData);
839 delete[] (char*) rgnData;
840
841 // SetWindowRgn expects the region to be in coordinants
842 // relative to the window, not the client area. Figure
843 // out the offset, if any.
844 RECT rect;
845 DWORD dwStyle = ::GetWindowLong(GetHwnd(), GWL_STYLE);
846 DWORD dwExStyle = ::GetWindowLong(GetHwnd(), GWL_EXSTYLE);
847 ::GetClientRect(GetHwnd(), &rect);
848 ::AdjustWindowRectEx(&rect, dwStyle, FALSE, dwExStyle);
849 ::OffsetRgn(hrgn, -rect.left, -rect.top);
850
851 // Now call the shape API with the new region.
852 if (::SetWindowRgn(GetHwnd(), hrgn, TRUE) == 0)
853 {
854 wxLogLastError(_T("SetWindowRgn"));
855 return FALSE;
856 }
857 return TRUE;
858 }
859
860 #endif // !__WXWINCE__
861
862 // ----------------------------------------------------------------------------
863 // wxTopLevelWindow event handling
864 // ----------------------------------------------------------------------------
865
866 // Default activation behaviour - set the focus for the first child
867 // subwindow found.
868 void wxTopLevelWindowMSW::OnActivate(wxActivateEvent& event)
869 {
870 if ( event.GetActive() )
871 {
872 // restore focus to the child which was last focused unless we already
873 // have it
874 wxLogTrace(_T("focus"), _T("wxTLW %08x activated."), (int) m_hWnd);
875
876 wxWindow *winFocus = FindFocus();
877 if ( !winFocus || wxGetTopLevelParent(winFocus) != this )
878 {
879 wxWindow *parent = m_winLastFocused ? m_winLastFocused->GetParent()
880 : NULL;
881 if ( !parent )
882 {
883 parent = this;
884 }
885
886 wxSetFocusToChild(parent, &m_winLastFocused);
887 }
888 }
889 else // deactivating
890 {
891 // remember the last focused child if it is our child
892 m_winLastFocused = FindFocus();
893
894 if ( m_winLastFocused )
895 {
896 // let it know that it doesn't have focus any more
897 m_winLastFocused->HandleKillFocus((WXHWND)NULL);
898
899 // and don't remember it if it's a child from some other frame
900 if ( wxGetTopLevelParent(m_winLastFocused) != this )
901 {
902 m_winLastFocused = NULL;
903 }
904 }
905
906 wxLogTrace(_T("focus"),
907 _T("wxTLW %08x deactivated, last focused: %08x."),
908 (int) m_hWnd,
909 (int) (m_winLastFocused ? GetHwndOf(m_winLastFocused)
910 : NULL));
911
912 event.Skip();
913 }
914 }
915
916 // the DialogProc for all wxWindows dialogs
917 LONG APIENTRY _EXPORT
918 wxDlgProc(HWND WXUNUSED(hDlg),
919 UINT message,
920 WPARAM WXUNUSED(wParam),
921 LPARAM WXUNUSED(lParam))
922 {
923 switch ( message )
924 {
925 case WM_INITDIALOG:
926 // for this message, returning TRUE tells system to set focus to
927 // the first control in the dialog box, but as we set the focus
928 // ourselves, we return FALSE from here as well, so fall through
929
930 default:
931 // for all the other ones, FALSE means that we didn't process the
932 // message
933 return FALSE;
934 }
935 }
936
937 // ============================================================================
938 // wxTLWHiddenParentModule implementation
939 // ============================================================================
940
941 HWND wxTLWHiddenParentModule::ms_hwnd = NULL;
942
943 const wxChar *wxTLWHiddenParentModule::ms_className = NULL;
944
945 bool wxTLWHiddenParentModule::OnInit()
946 {
947 ms_hwnd = NULL;
948 ms_className = NULL;
949
950 return TRUE;
951 }
952
953 void wxTLWHiddenParentModule::OnExit()
954 {
955 if ( ms_hwnd )
956 {
957 if ( !::DestroyWindow(ms_hwnd) )
958 {
959 wxLogLastError(_T("DestroyWindow(hidden TLW parent)"));
960 }
961
962 ms_hwnd = NULL;
963 }
964
965 if ( ms_className )
966 {
967 if ( !::UnregisterClass(ms_className, wxGetInstance()) )
968 {
969 wxLogLastError(_T("UnregisterClass(\"wxTLWHiddenParent\")"));
970 }
971
972 ms_className = NULL;
973 }
974 }
975
976 /* static */
977 HWND wxTLWHiddenParentModule::GetHWND()
978 {
979 if ( !ms_hwnd )
980 {
981 if ( !ms_className )
982 {
983 static const wxChar *HIDDEN_PARENT_CLASS = _T("wxTLWHiddenParent");
984
985 WNDCLASS wndclass;
986 wxZeroMemory(wndclass);
987
988 wndclass.lpfnWndProc = DefWindowProc;
989 wndclass.hInstance = wxGetInstance();
990 wndclass.lpszClassName = HIDDEN_PARENT_CLASS;
991
992 if ( !::RegisterClass(&wndclass) )
993 {
994 wxLogLastError(_T("RegisterClass(\"wxTLWHiddenParent\")"));
995 }
996 else
997 {
998 ms_className = HIDDEN_PARENT_CLASS;
999 }
1000 }
1001
1002 ms_hwnd = ::CreateWindow(ms_className, wxEmptyString, 0, 0, 0, 0, 0, NULL,
1003 (HMENU)NULL, wxGetInstance(), NULL);
1004 if ( !ms_hwnd )
1005 {
1006 wxLogLastError(_T("CreateWindow(hidden TLW parent)"));
1007 }
1008 }
1009
1010 return ms_hwnd;
1011 }
1012
1013