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