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