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