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