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