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