tool windows shouldn't appear on the taskbar (patch 808350)
[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 #include "wx/msw/winundef.h"
45
46 // This can't be undefed in winundef.h or
47 // there are further errors
48 #if defined(__WXWINCE__) && defined(CreateDialog)
49 #undef CreateDialog
50 #endif
51
52 #include "wx/display.h"
53
54 #ifndef ICON_BIG
55 #define ICON_BIG 1
56 #endif
57
58 #ifndef ICON_SMALL
59 #define ICON_SMALL 0
60 #endif
61
62 // ----------------------------------------------------------------------------
63 // stubs for missing functions under MicroWindows
64 // ----------------------------------------------------------------------------
65
66 #ifdef __WXMICROWIN__
67
68 // static inline bool IsIconic(HWND WXUNUSED(hwnd)) { return FALSE; }
69 static inline bool IsZoomed(HWND WXUNUSED(hwnd)) { return FALSE; }
70
71 #endif // __WXMICROWIN__
72
73 // NB: wxDlgProc must be defined here and not in dialog.cpp because the latter
74 // is not included by wxUniv build which does need wxDlgProc
75 LONG APIENTRY _EXPORT
76 wxDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
77
78 // ----------------------------------------------------------------------------
79 // globals
80 // ----------------------------------------------------------------------------
81
82 // list of all frames and modeless dialogs
83 wxWindowList wxModelessWindows;
84
85 // the name of the default wxWindows class
86 extern const wxChar *wxCanvasClassName;
87
88 // ----------------------------------------------------------------------------
89 // wxTLWHiddenParentModule: used to manage the hidden parent window (we need a
90 // module to ensure that the window is always deleted)
91 // ----------------------------------------------------------------------------
92
93 class wxTLWHiddenParentModule : public wxModule
94 {
95 public:
96 // module init/finalize
97 virtual bool OnInit();
98 virtual void OnExit();
99
100 // get the hidden window (creates on demand)
101 static HWND GetHWND();
102
103 private:
104 // the HWND of the hidden parent
105 static HWND ms_hwnd;
106
107 // the class used to create it
108 static const wxChar *ms_className;
109
110 DECLARE_DYNAMIC_CLASS(wxTLWHiddenParentModule)
111 };
112
113 IMPLEMENT_DYNAMIC_CLASS(wxTLWHiddenParentModule, wxModule)
114
115 // ============================================================================
116 // wxTopLevelWindowMSW implementation
117 // ============================================================================
118
119 BEGIN_EVENT_TABLE(wxTopLevelWindowMSW, wxTopLevelWindowBase)
120 EVT_ACTIVATE(wxTopLevelWindowMSW::OnActivate)
121 END_EVENT_TABLE()
122
123 // ----------------------------------------------------------------------------
124 // wxTopLevelWindowMSW creation
125 // ----------------------------------------------------------------------------
126
127 void wxTopLevelWindowMSW::Init()
128 {
129 m_iconized =
130 m_maximizeOnShow = FALSE;
131
132 // unlike (almost?) all other windows, frames are created hidden
133 m_isShown = FALSE;
134
135 // Data to save/restore when calling ShowFullScreen
136 m_fsStyle = 0;
137 m_fsOldWindowStyle = 0;
138 m_fsIsMaximized = FALSE;
139 m_fsIsShowing = FALSE;
140
141 m_winLastFocused = (wxWindow *)NULL;
142 }
143
144 WXDWORD wxTopLevelWindowMSW::MSWGetStyle(long style, WXDWORD *exflags) const
145 {
146 // let the base class deal with the common styles but fix the ones which
147 // don't make sense for us (we also deal with the borders ourselves)
148 WXDWORD msflags = wxWindow::MSWGetStyle
149 (
150 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exflags
151 ) & ~WS_CHILD & ~WS_VISIBLE;
152
153 // first select the kind of window being created
154 //
155 // note that if we don't set WS_POPUP, Windows assumes WS_OVERLAPPED and
156 // creates a window with both caption and border, hence we also test it
157 // below in some other cases
158 if ( style & wxFRAME_TOOL_WINDOW )
159 msflags |= WS_POPUP;
160 else
161 {
162 #ifdef __WXWINCE__
163 if (msflags & WS_BORDER)
164 #endif
165 msflags |= WS_OVERLAPPED;
166 }
167
168 // border and caption styles
169 if ( style & wxRESIZE_BORDER )
170 {
171 #ifndef __WXWINCE__
172 msflags |= WS_THICKFRAME;
173 #endif
174 }
175 else if ( !(style & wxBORDER_NONE) )
176 msflags |= WS_BORDER;
177 #ifndef __WXWINCE__
178 else
179 msflags |= WS_POPUP;
180 #endif
181
182 if ( style & wxCAPTION )
183 msflags |= WS_CAPTION;
184 #ifndef __WXWINCE__
185 else
186 msflags |= WS_POPUP;
187 #endif
188
189 // next translate the individual flags
190 if ( style & wxMINIMIZE_BOX )
191 msflags |= WS_MINIMIZEBOX;
192 if ( style & wxMAXIMIZE_BOX )
193 msflags |= WS_MAXIMIZEBOX;
194 if ( style & wxSYSTEM_MENU )
195 msflags |= WS_SYSMENU;
196 #ifndef __WXWINCE__
197 if ( style & wxMINIMIZE )
198 msflags |= WS_MINIMIZE;
199 if ( style & wxMAXIMIZE )
200 msflags |= WS_MAXIMIZE;
201 #endif
202
203 // Keep this here because it saves recoding this function in wxTinyFrame
204 #if wxUSE_ITSY_BITSY && !defined(__WIN32__)
205 if ( style & wxTINY_CAPTION_VERT )
206 msflags |= IBS_VERTCAPTION;
207 if ( style & wxTINY_CAPTION_HORIZ )
208 msflags |= IBS_HORZCAPTION;
209 #else
210 if ( style & (wxTINY_CAPTION_VERT | wxTINY_CAPTION_HORIZ) )
211 msflags |= WS_CAPTION;
212 #endif
213
214 if ( exflags )
215 {
216 #if !defined(__WIN16__)
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 // We have to solve 2 different problems here:
229 //
230 // 1. frames with wxFRAME_NO_TASKBAR flag shouldn't appear in the
231 // taskbar even if they don't have a parent
232 //
233 // 2. frames without this style should appear in the taskbar even
234 // if they're owned (Windows only puts non owned windows into
235 // the taskbar normally)
236 //
237 // The second one is solved here by using WS_EX_APPWINDOW flag, the
238 // first one is dealt with in our MSWGetParent() method
239 // implementation
240 #ifndef __WXWINCE__
241 if ( !(style & wxFRAME_NO_TASKBAR) && GetParent() )
242 {
243 // need to force the frame to appear in the taskbar
244 *exflags |= WS_EX_APPWINDOW;
245 }
246 #endif
247 //else: nothing to do [here]
248 }
249 #endif // !Win16
250
251 if ( style & wxSTAY_ON_TOP )
252 *exflags |= WS_EX_TOPMOST;
253
254 #ifdef __WIN32__
255 if ( GetExtraStyle() & wxFRAME_EX_CONTEXTHELP )
256 *exflags |= WS_EX_CONTEXTHELP;
257 #endif // __WIN32__
258 }
259
260 return msflags;
261 }
262
263 WXHWND wxTopLevelWindowMSW::MSWGetParent() const
264 {
265 // for the frames without wxFRAME_FLOAT_ON_PARENT style we should use NULL
266 // parent HWND or it would be always on top of its parent which is not what
267 // we usually want (in fact, we only want it for frames with the
268 // wxFRAME_FLOAT_ON_PARENT flag)
269 HWND hwndParent = NULL;
270 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT) )
271 {
272 const wxWindow *parent = GetParent();
273
274 if ( !parent )
275 {
276 // this flag doesn't make sense then and will be ignored
277 wxFAIL_MSG( _T("wxFRAME_FLOAT_ON_PARENT but no parent?") );
278 }
279 else
280 {
281 hwndParent = GetHwndOf(parent);
282 }
283 }
284 //else: don't float on parent, must not be owned
285
286 // now deal with the 2nd taskbar-related problem (see comments above in
287 // MSWGetStyle())
288 if ( HasFlag(wxFRAME_NO_TASKBAR) && !hwndParent )
289 {
290 // use hidden parent
291 hwndParent = wxTLWHiddenParentModule::GetHWND();
292 }
293
294 return (WXHWND)hwndParent;
295 }
296
297 bool wxTopLevelWindowMSW::CreateDialog(const void *dlgTemplate,
298 const wxString& title,
299 const wxPoint& pos,
300 const wxSize& size)
301 {
302 #ifdef __WXMICROWIN__
303 // no dialogs support under MicroWin yet
304 return CreateFrame(title, pos, size);
305 #else // !__WXMICROWIN__
306 wxWindow *parent = GetParent();
307
308 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
309 // app window as parent - this avoids creating modal dialogs without
310 // parent
311 if ( !parent && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) )
312 {
313 parent = wxTheApp->GetTopWindow();
314
315 if ( parent )
316 {
317 // don't use transient windows as parents, this is dangerous as it
318 // can lead to a crash if the parent is destroyed before the child
319 //
320 // also don't use the window which is currently hidden as then the
321 // dialog would be hidden as well
322 if ( (parent->GetExtraStyle() & wxWS_EX_TRANSIENT) ||
323 !parent->IsShown() )
324 {
325 parent = NULL;
326 }
327 }
328 }
329
330 m_hWnd = (WXHWND)::CreateDialogIndirect
331 (
332 wxGetInstance(),
333 (DLGTEMPLATE*)dlgTemplate,
334 parent ? GetHwndOf(parent) : NULL,
335 (DLGPROC)wxDlgProc
336 );
337
338 if ( !m_hWnd )
339 {
340 wxFAIL_MSG(wxT("Failed to create dialog. Incorrect DLGTEMPLATE?"));
341
342 wxLogSysError(wxT("Can't create dialog using memory template"));
343
344 return FALSE;
345 }
346
347 WXDWORD exflags;
348 (void)MSWGetCreateWindowFlags(&exflags);
349
350 if ( exflags )
351 {
352 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE, exflags);
353 ::SetWindowPos(GetHwnd(),
354 exflags & WS_EX_TOPMOST ? HWND_TOPMOST : 0,
355 0, 0, 0, 0,
356 SWP_NOSIZE |
357 SWP_NOMOVE |
358 (exflags & WS_EX_TOPMOST ? 0 : SWP_NOZORDER) |
359 SWP_NOACTIVATE);
360 }
361
362 #if defined(__WIN95__)
363 // For some reason, the system menu is activated when we use the
364 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
365 if ( exflags & WS_EX_CONTEXTHELP )
366 {
367 wxFrame *winTop = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
368 if ( winTop )
369 {
370 wxIcon icon = winTop->GetIcon();
371 if ( icon.Ok() )
372 {
373 ::SendMessage(GetHwnd(), WM_SETICON,
374 (WPARAM)TRUE,
375 (LPARAM)GetHiconOf(icon));
376 }
377 }
378 }
379 #endif // __WIN95__
380
381 // move the dialog to its initial position without forcing repainting
382 int x, y, w, h;
383 if ( !MSWGetCreateWindowCoords(pos, size, x, y, w, h) )
384 {
385 x =
386 w = (int)CW_USEDEFAULT;
387 }
388
389 // we can't use CW_USEDEFAULT here as we're not calling CreateWindow()
390 // and passing CW_USEDEFAULT to MoveWindow() results in resizing the
391 // window to (0, 0) size which breaks quite a lot of things, e.g. the
392 // sizer calculation in wxSizer::Fit()
393 if ( w == (int)CW_USEDEFAULT )
394 {
395 // the exact number doesn't matter, the dialog will be resized
396 // again soon anyhow but it should be big enough to allow
397 // calculation relying on "totalSize - clientSize > 0" work, i.e.
398 // at least greater than the title bar height
399 w =
400 h = 100;
401 }
402
403 if ( x == (int)CW_USEDEFAULT )
404 {
405 // centre it on the screen - what else can we do?
406 wxSize sizeDpy = wxGetDisplaySize();
407
408 x = (sizeDpy.x - w) / 2;
409 y = (sizeDpy.y - h) / 2;
410 }
411
412 if ( !::MoveWindow(GetHwnd(), x, y, w, h, FALSE) )
413 {
414 wxLogLastError(wxT("MoveWindow"));
415 }
416
417 if ( !title.empty() )
418 {
419 ::SetWindowText(GetHwnd(), title);
420 }
421
422 SubclassWin(m_hWnd);
423
424 return TRUE;
425 #endif // __WXMICROWIN__/!__WXMICROWIN__
426 }
427
428 bool wxTopLevelWindowMSW::CreateFrame(const wxString& title,
429 const wxPoint& pos,
430 const wxSize& size)
431 {
432 WXDWORD exflags;
433 WXDWORD flags = MSWGetCreateWindowFlags(&exflags);
434
435 return MSWCreate(wxCanvasClassName, title, pos, size, flags, exflags);
436 }
437
438 bool wxTopLevelWindowMSW::Create(wxWindow *parent,
439 wxWindowID id,
440 const wxString& title,
441 const wxPoint& pos,
442 const wxSize& size,
443 long style,
444 const wxString& name)
445 {
446 bool ret = false;
447
448 // init our fields
449 Init();
450
451 m_windowStyle = style;
452
453 SetName(name);
454
455 m_windowId = id == -1 ? NewControlId() : id;
456
457 wxTopLevelWindows.Append(this);
458
459 if ( parent )
460 parent->AddChild(this);
461
462 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
463 {
464 // we have different dialog templates to allows creation of dialogs
465 // with & without captions under MSWindows, resizeable or not (but a
466 // resizeable dialog always has caption - otherwise it would look too
467 // strange)
468
469 // we need 3 additional WORDs for dialog menu, class and title (as we
470 // don't use DS_SETFONT we don't need the fourth WORD for the font)
471 static const int dlgsize = sizeof(DLGTEMPLATE) + (sizeof(WORD) * 3);
472 DLGTEMPLATE *dlgTemplate = (DLGTEMPLATE *)malloc(dlgsize);
473 memset(dlgTemplate, 0, dlgsize);
474
475 // these values are arbitrary, they won't be used normally anyhow
476 dlgTemplate->x = 34;
477 dlgTemplate->y = 22;
478 dlgTemplate->cx = 144;
479 dlgTemplate->cy = 75;
480
481 // reuse the code in MSWGetStyle() but correct the results slightly for
482 // the dialog
483 dlgTemplate->style = MSWGetStyle(style, NULL);
484
485 // all dialogs are popups
486 dlgTemplate->style |= WS_POPUP;
487
488 // force 3D-look if necessary, it looks impossibly ugly otherwise
489 if ( style & (wxRESIZE_BORDER | wxCAPTION) )
490 dlgTemplate->style |= DS_MODALFRAME;
491
492 ret = CreateDialog(dlgTemplate, title, pos, size);
493 free(dlgTemplate);
494 }
495 else // !dialog
496 {
497 ret = CreateFrame(title, pos, size);
498 }
499
500 if ( ret && !(GetWindowStyleFlag() & wxCLOSE_BOX) )
501 {
502 EnableCloseButton(false);
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 // FIXME: implement for WinCE
699 #ifndef __WXWINCE__
700 // resize to the size of the desktop
701 wxCopyRECTToRect(wxGetWindowRect(::GetDesktopWindow()), rect);
702 #endif
703 }
704 #endif // wxUSE_DISPLAY
705
706 SetSize(rect);
707
708 // now flush the window style cache and actually go full-screen
709 long flags = SWP_FRAMECHANGED;
710
711 // showing the frame full screen should also show it if it's still
712 // hidden
713 if ( !IsShown() )
714 {
715 // don't call wxWindow version to avoid flicker from calling
716 // ::ShowWindow() -- we're going to show the window at the correct
717 // location directly below -- but do call the wxWindowBase version
718 // to sync the internal m_isShown flag
719 wxWindowBase::Show();
720
721 flags |= SWP_SHOWWINDOW;
722 }
723
724 SetWindowPos(GetHwnd(), HWND_TOP,
725 rect.x, rect.y, rect.width, rect.height,
726 flags);
727
728 // finally send an event allowing the window to relayout itself &c
729 wxSizeEvent event(rect.GetSize(), GetId());
730 GetEventHandler()->ProcessEvent(event);
731 }
732 else // stop showing full screen
733 {
734 Maximize(m_fsIsMaximized);
735 SetWindowLong(GetHwnd(),GWL_STYLE, m_fsOldWindowStyle);
736 SetWindowPos(GetHwnd(),HWND_TOP,m_fsOldSize.x, m_fsOldSize.y,
737 m_fsOldSize.width, m_fsOldSize.height, SWP_FRAMECHANGED);
738 }
739
740 return TRUE;
741 }
742
743 // ----------------------------------------------------------------------------
744 // wxTopLevelWindowMSW misc
745 // ----------------------------------------------------------------------------
746
747 void wxTopLevelWindowMSW::SetIcon(const wxIcon& icon)
748 {
749 SetIcons( wxIconBundle( icon ) );
750 }
751
752 void wxTopLevelWindowMSW::SetIcons(const wxIconBundle& icons)
753 {
754 wxTopLevelWindowBase::SetIcons(icons);
755
756 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
757 const wxIcon& sml = icons.GetIcon( wxSize( 16, 16 ) );
758 if( sml.Ok() && sml.GetWidth() == 16 && sml.GetHeight() == 16 )
759 {
760 ::SendMessage( GetHwndOf( this ), WM_SETICON, ICON_SMALL,
761 (LPARAM)GetHiconOf(sml) );
762 }
763
764 const wxIcon& big = icons.GetIcon( wxSize( 32, 32 ) );
765 if( big.Ok() && big.GetWidth() == 32 && big.GetHeight() == 32 )
766 {
767 ::SendMessage( GetHwndOf( this ), WM_SETICON, ICON_BIG,
768 (LPARAM)GetHiconOf(big) );
769 }
770 #endif // __WIN95__
771 }
772
773 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable)
774 {
775 #if !defined(__WXMICROWIN__)
776 // get system (a.k.a. window) menu
777 HMENU hmenu = GetSystemMenu(GetHwnd(), FALSE /* get it */);
778 if ( !hmenu )
779 {
780 // no system menu at all -- ok if we want to remove the close button
781 // anyhow, but bad if we want to show it
782 return !enable;
783 }
784
785 // enabling/disabling the close item from it also automatically
786 // disables/enables the close title bar button
787 if ( ::EnableMenuItem(hmenu, SC_CLOSE,
788 MF_BYCOMMAND |
789 (enable ? MF_ENABLED : MF_GRAYED)) == -1 )
790 {
791 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
792
793 return FALSE;
794 }
795
796 // update appearance immediately
797 if ( !::DrawMenuBar(GetHwnd()) )
798 {
799 wxLogLastError(_T("DrawMenuBar"));
800 }
801 #endif // !__WXMICROWIN__
802
803 return TRUE;
804 }
805
806 bool wxTopLevelWindowMSW::SetShape(const wxRegion& region)
807 {
808 #ifdef __WXWINCE__
809 return FALSE;
810 #else
811 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), FALSE,
812 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
813
814 // The empty region signifies that the shape should be removed from the
815 // window.
816 if ( region.IsEmpty() )
817 {
818 if (::SetWindowRgn(GetHwnd(), NULL, TRUE) == 0)
819 {
820 wxLogLastError(_T("SetWindowRgn"));
821 return FALSE;
822 }
823 return TRUE;
824 }
825
826 // Windows takes ownership of the region, so
827 // we'll have to make a copy of the region to give to it.
828 DWORD noBytes = ::GetRegionData(GetHrgnOf(region), 0, NULL);
829 RGNDATA *rgnData = (RGNDATA*) new char[noBytes];
830 ::GetRegionData(GetHrgnOf(region), noBytes, rgnData);
831 HRGN hrgn = ::ExtCreateRegion(NULL, noBytes, rgnData);
832 delete[] (char*) rgnData;
833
834 // SetWindowRgn expects the region to be in coordinants
835 // relative to the window, not the client area. Figure
836 // out the offset, if any.
837 RECT rect;
838 DWORD dwStyle = ::GetWindowLong(GetHwnd(), GWL_STYLE);
839 DWORD dwExStyle = ::GetWindowLong(GetHwnd(), GWL_EXSTYLE);
840 ::GetClientRect(GetHwnd(), &rect);
841 ::AdjustWindowRectEx(&rect, dwStyle, FALSE, dwExStyle);
842 ::OffsetRgn(hrgn, -rect.left, -rect.top);
843
844 // Now call the shape API with the new region.
845 if (::SetWindowRgn(GetHwnd(), hrgn, TRUE) == 0)
846 {
847 wxLogLastError(_T("SetWindowRgn"));
848 return FALSE;
849 }
850 return TRUE;
851 #endif
852 }
853
854 // ----------------------------------------------------------------------------
855 // wxTopLevelWindow event handling
856 // ----------------------------------------------------------------------------
857
858 // Default activation behaviour - set the focus for the first child
859 // subwindow found.
860 void wxTopLevelWindowMSW::OnActivate(wxActivateEvent& event)
861 {
862 if ( event.GetActive() )
863 {
864 // restore focus to the child which was last focused
865 wxLogTrace(_T("focus"), _T("wxTLW %08x activated."), (int) m_hWnd);
866
867 wxWindow *parent = m_winLastFocused ? m_winLastFocused->GetParent()
868 : NULL;
869 if ( !parent )
870 {
871 parent = this;
872 }
873
874 wxSetFocusToChild(parent, &m_winLastFocused);
875 }
876 else // deactivating
877 {
878 // remember the last focused child if it is our child
879 m_winLastFocused = FindFocus();
880
881 if ( m_winLastFocused )
882 {
883 // let it know that it doesn't have focus any more
884 m_winLastFocused->HandleKillFocus((WXHWND)NULL);
885 }
886
887 // so we NULL it out if it's a child from some other frame
888 wxWindow *win = m_winLastFocused;
889 while ( win )
890 {
891 if ( win->IsTopLevel() )
892 {
893 if ( win != this )
894 {
895 m_winLastFocused = NULL;
896 }
897
898 break;
899 }
900
901 win = win->GetParent();
902 }
903
904 wxLogTrace(_T("focus"),
905 _T("wxTLW %08x deactivated, last focused: %08x."),
906 (int) m_hWnd,
907 (int) (m_winLastFocused ? GetHwndOf(m_winLastFocused)
908 : NULL));
909
910 event.Skip();
911 }
912 }
913
914 // the DialogProc for all wxWindows dialogs
915 LONG APIENTRY _EXPORT
916 wxDlgProc(HWND WXUNUSED(hDlg),
917 UINT message,
918 WPARAM WXUNUSED(wParam),
919 LPARAM WXUNUSED(lParam))
920 {
921 switch ( message )
922 {
923 case WM_INITDIALOG:
924 // for this message, returning TRUE tells system to set focus to
925 // the first control in the dialog box, but as we set the focus
926 // ourselves, we return FALSE from here as well, so fall through
927
928 default:
929 // for all the other ones, FALSE means that we didn't process the
930 // message
931 return FALSE;
932 }
933 }
934
935 // ============================================================================
936 // wxTLWHiddenParentModule implementation
937 // ============================================================================
938
939 HWND wxTLWHiddenParentModule::ms_hwnd = NULL;
940
941 const wxChar *wxTLWHiddenParentModule::ms_className = NULL;
942
943 bool wxTLWHiddenParentModule::OnInit()
944 {
945 ms_hwnd = NULL;
946 ms_className = NULL;
947
948 return TRUE;
949 }
950
951 void wxTLWHiddenParentModule::OnExit()
952 {
953 if ( ms_hwnd )
954 {
955 if ( !::DestroyWindow(ms_hwnd) )
956 {
957 wxLogLastError(_T("DestroyWindow(hidden TLW parent)"));
958 }
959
960 ms_hwnd = NULL;
961 }
962
963 if ( ms_className )
964 {
965 if ( !::UnregisterClass(ms_className, wxGetInstance()) )
966 {
967 wxLogLastError(_T("UnregisterClass(\"wxTLWHiddenParent\")"));
968 }
969
970 ms_className = NULL;
971 }
972 }
973
974 /* static */
975 HWND wxTLWHiddenParentModule::GetHWND()
976 {
977 if ( !ms_hwnd )
978 {
979 if ( !ms_className )
980 {
981 static const wxChar *HIDDEN_PARENT_CLASS = _T("wxTLWHiddenParent");
982
983 WNDCLASS wndclass;
984 wxZeroMemory(wndclass);
985
986 wndclass.lpfnWndProc = DefWindowProc;
987 wndclass.hInstance = wxGetInstance();
988 wndclass.lpszClassName = HIDDEN_PARENT_CLASS;
989
990 if ( !::RegisterClass(&wndclass) )
991 {
992 wxLogLastError(_T("RegisterClass(\"wxTLWHiddenParent\")"));
993 }
994 else
995 {
996 ms_className = HIDDEN_PARENT_CLASS;
997 }
998 }
999
1000 ms_hwnd = ::CreateWindow(ms_className, wxEmptyString, 0, 0, 0, 0, 0, NULL,
1001 (HMENU)NULL, wxGetInstance(), NULL);
1002 if ( !ms_hwnd )
1003 {
1004 wxLogLastError(_T("CreateWindow(hidden TLW parent)"));
1005 }
1006 }
1007
1008 return ms_hwnd;
1009 }
1010
1011