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