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