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