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