WinCE fixes.
[wxWidgets.git] / src / msw / toplevel.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for MSW
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 24.09.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "toplevel.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/toplevel.h"
34 #include "wx/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 #include "wx/dynlib.h"
44
45 #include "wx/msw/private.h"
46 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__)
47 #include <ole2.h>
48 #include <shellapi.h>
49 // Standard SDK doesn't have aygshell.dll: see include/wx/msw/wince/libraries.h
50 #if _WIN32_WCE < 400 || !defined(__WINCE_STANDARDSDK__)
51 #include <aygshell.h>
52 #endif
53 #include "wx/msw/wince/missing.h"
54 #endif
55
56 #include "wx/msw/missing.h"
57 #include "wx/msw/winundef.h"
58
59 #include "wx/display.h"
60
61 #ifndef ICON_BIG
62 #define ICON_BIG 1
63 #endif
64
65 #ifndef ICON_SMALL
66 #define ICON_SMALL 0
67 #endif
68
69 // ----------------------------------------------------------------------------
70 // stubs for missing functions under MicroWindows
71 // ----------------------------------------------------------------------------
72
73 #ifdef __WXMICROWIN__
74
75 // static inline bool IsIconic(HWND WXUNUSED(hwnd)) { return false; }
76 static inline bool IsZoomed(HWND WXUNUSED(hwnd)) { return false; }
77
78 #endif // __WXMICROWIN__
79
80 // NB: wxDlgProc must be defined here and not in dialog.cpp because the latter
81 // is not included by wxUniv build which does need wxDlgProc
82 LONG APIENTRY _EXPORT
83 wxDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
84
85 // ----------------------------------------------------------------------------
86 // globals
87 // ----------------------------------------------------------------------------
88
89 // the name of the default wxWidgets class
90 #ifdef __WXWINCE__
91 extern wxChar *wxCanvasClassName;
92 #else
93 extern const wxChar *wxCanvasClassName;
94 #endif
95
96 // ----------------------------------------------------------------------------
97 // wxTLWHiddenParentModule: used to manage the hidden parent window (we need a
98 // module to ensure that the window is always deleted)
99 // ----------------------------------------------------------------------------
100
101 class wxTLWHiddenParentModule : public wxModule
102 {
103 public:
104 // module init/finalize
105 virtual bool OnInit();
106 virtual void OnExit();
107
108 // get the hidden window (creates on demand)
109 static HWND GetHWND();
110
111 private:
112 // the HWND of the hidden parent
113 static HWND ms_hwnd;
114
115 // the class used to create it
116 static const wxChar *ms_className;
117
118 DECLARE_DYNAMIC_CLASS(wxTLWHiddenParentModule)
119 };
120
121 IMPLEMENT_DYNAMIC_CLASS(wxTLWHiddenParentModule, wxModule)
122
123 // ============================================================================
124 // wxTopLevelWindowMSW implementation
125 // ============================================================================
126
127 BEGIN_EVENT_TABLE(wxTopLevelWindowMSW, wxTopLevelWindowBase)
128 EVT_ACTIVATE(wxTopLevelWindowMSW::OnActivate)
129 END_EVENT_TABLE()
130
131 // ----------------------------------------------------------------------------
132 // wxTopLevelWindowMSW creation
133 // ----------------------------------------------------------------------------
134
135 void wxTopLevelWindowMSW::Init()
136 {
137 m_iconized =
138 m_maximizeOnShow = false;
139
140 // Data to save/restore when calling ShowFullScreen
141 m_fsStyle = 0;
142 m_fsOldWindowStyle = 0;
143 m_fsIsMaximized = false;
144 m_fsIsShowing = false;
145
146 m_winLastFocused = (wxWindow *)NULL;
147
148 #ifdef __SMARTPHONE__
149 m_MenuBarHWND = 0;
150 #endif
151 }
152
153 WXDWORD wxTopLevelWindowMSW::MSWGetStyle(long style, WXDWORD *exflags) const
154 {
155 // let the base class deal with the common styles but fix the ones which
156 // don't make sense for us (we also deal with the borders ourselves)
157 WXDWORD msflags = wxWindow::MSWGetStyle
158 (
159 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exflags
160 ) & ~WS_CHILD & ~WS_VISIBLE;
161
162 #if defined(__WXWINCE__) && _WIN32_WCE < 400
163 msflags |= WS_VISIBLE;
164 #endif
165
166 // first select the kind of window being created
167 //
168 // note that if we don't set WS_POPUP, Windows assumes WS_OVERLAPPED and
169 // creates a window with both caption and border, hence we also test it
170 // below in some other cases
171 if ( style & wxFRAME_TOOL_WINDOW )
172 {
173 msflags |= WS_POPUP;
174 }
175 //else: WS_OVERLAPPED is 0 anyhow, so it is on by default
176
177 #ifndef __SMARTPHONE__
178 // border and caption styles
179 if ( style & wxRESIZE_BORDER )
180 msflags |= WS_THICKFRAME;
181 else if ( exflags && ((style & wxBORDER_DOUBLE) || (style & wxBORDER_RAISED)) )
182 *exflags |= WS_EX_DLGMODALFRAME;
183 else if ( !(style & wxBORDER_NONE) )
184 msflags |= WS_BORDER;
185 else
186 msflags |= WS_POPUP;
187 #endif
188
189 // normally we consider that all windows without caption must be popups,
190 // but CE is an exception: there windows normally do not have the caption
191 // but shouldn't be made popups as popups can't have menus and don't look
192 // like normal windows anyhow
193 if ( style & wxCAPTION )
194 msflags |= WS_CAPTION;
195 #ifndef __WXWINCE__
196 else
197 msflags |= WS_POPUP;
198 #endif // !__WXWINCE__
199
200 // next translate the individual flags
201 if ( style & wxMINIMIZE_BOX )
202 msflags |= WS_MINIMIZEBOX;
203 if ( style & wxMAXIMIZE_BOX )
204 msflags |= WS_MAXIMIZEBOX;
205 if ( style & wxSYSTEM_MENU )
206 msflags |= WS_SYSMENU;
207
208 // NB: under CE these 2 styles are not supported currently, we should
209 // call Minimize()/Maximize() "manually" if we want to support them
210 if ( style & wxMINIMIZE )
211 msflags |= WS_MINIMIZE;
212 if ( style & wxMAXIMIZE )
213 msflags |= WS_MAXIMIZE;
214
215 // Keep this here because it saves recoding this function in wxTinyFrame
216 if ( style & (wxTINY_CAPTION_VERT | wxTINY_CAPTION_HORIZ) )
217 msflags |= WS_CAPTION;
218
219 if ( exflags )
220 {
221 // there is no taskbar under CE, so omit all this
222 #if !defined(__WXWINCE__)
223 if ( !(GetExtraStyle() & wxTOPLEVEL_EX_DIALOG) )
224 {
225 if ( style & wxFRAME_TOOL_WINDOW )
226 {
227 // create the palette-like window
228 *exflags |= WS_EX_TOOLWINDOW;
229
230 // tool windows shouldn't appear on the taskbar (as documented)
231 style |= wxFRAME_NO_TASKBAR;
232 }
233
234 // We have to solve 2 different problems here:
235 //
236 // 1. frames with wxFRAME_NO_TASKBAR flag shouldn't appear in the
237 // taskbar even if they don't have a parent
238 //
239 // 2. frames without this style should appear in the taskbar even
240 // if they're owned (Windows only puts non owned windows into
241 // the taskbar normally)
242 //
243 // The second one is solved here by using WS_EX_APPWINDOW flag, the
244 // first one is dealt with in our MSWGetParent() method
245 // implementation
246 if ( !(style & wxFRAME_NO_TASKBAR) && GetParent() )
247 {
248 // need to force the frame to appear in the taskbar
249 *exflags |= WS_EX_APPWINDOW;
250 }
251 //else: nothing to do [here]
252 }
253 #endif // !__WXWINCE__
254
255 if ( style & wxSTAY_ON_TOP )
256 *exflags |= WS_EX_TOPMOST;
257
258 if ( GetExtraStyle() & wxFRAME_EX_CONTEXTHELP )
259 *exflags |= WS_EX_CONTEXTHELP;
260 }
261
262 return msflags;
263 }
264
265 WXHWND wxTopLevelWindowMSW::MSWGetParent() const
266 {
267 // for the frames without wxFRAME_FLOAT_ON_PARENT style we should use NULL
268 // parent HWND or it would be always on top of its parent which is not what
269 // we usually want (in fact, we only want it for frames with the
270 // wxFRAME_FLOAT_ON_PARENT flag)
271 HWND hwndParent = NULL;
272 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT) )
273 {
274 const wxWindow *parent = GetParent();
275
276 if ( !parent )
277 {
278 // this flag doesn't make sense then and will be ignored
279 wxFAIL_MSG( _T("wxFRAME_FLOAT_ON_PARENT but no parent?") );
280 }
281 else
282 {
283 hwndParent = GetHwndOf(parent);
284 }
285 }
286 //else: don't float on parent, must not be owned
287
288 // now deal with the 2nd taskbar-related problem (see comments above in
289 // MSWGetStyle())
290 if ( HasFlag(wxFRAME_NO_TASKBAR) && !hwndParent )
291 {
292 // use hidden parent
293 hwndParent = wxTLWHiddenParentModule::GetHWND();
294 }
295
296 return (WXHWND)hwndParent;
297 }
298
299 bool wxTopLevelWindowMSW::CreateDialog(const void *dlgTemplate,
300 const wxString& title,
301 const wxPoint& pos,
302 const wxSize& size)
303 {
304 #ifdef __WXMICROWIN__
305 // no dialogs support under MicroWin yet
306 return CreateFrame(title, pos, size);
307 #else // !__WXMICROWIN__
308 wxWindow *parent = GetParent();
309
310 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
311 // app window as parent - this avoids creating modal dialogs without
312 // parent
313 if ( !parent && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) )
314 {
315 parent = wxTheApp->GetTopWindow();
316
317 if ( parent )
318 {
319 // don't use transient windows as parents, this is dangerous as it
320 // can lead to a crash if the parent is destroyed before the child
321 //
322 // also don't use the window which is currently hidden as then the
323 // dialog would be hidden as well
324 if ( (parent->GetExtraStyle() & wxWS_EX_TRANSIENT) ||
325 !parent->IsShown() )
326 {
327 parent = NULL;
328 }
329 }
330 }
331
332 m_hWnd = (WXHWND)::CreateDialogIndirect
333 (
334 wxGetInstance(),
335 (DLGTEMPLATE*)dlgTemplate,
336 parent ? GetHwndOf(parent) : NULL,
337 (DLGPROC)wxDlgProc
338 );
339
340 if ( !m_hWnd )
341 {
342 wxFAIL_MSG(wxT("Failed to create dialog. Incorrect DLGTEMPLATE?"));
343
344 wxLogSysError(wxT("Can't create dialog using memory template"));
345
346 return false;
347 }
348
349 WXDWORD exflags;
350 (void)MSWGetCreateWindowFlags(&exflags);
351
352 if ( exflags )
353 {
354 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE, exflags);
355 ::SetWindowPos(GetHwnd(),
356 exflags & WS_EX_TOPMOST ? HWND_TOPMOST : 0,
357 0, 0, 0, 0,
358 SWP_NOSIZE |
359 SWP_NOMOVE |
360 (exflags & WS_EX_TOPMOST ? 0 : SWP_NOZORDER) |
361 SWP_NOACTIVATE);
362 }
363
364 #if defined(__WIN95__)
365 // For some reason, the system menu is activated when we use the
366 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
367 if ( exflags & WS_EX_CONTEXTHELP )
368 {
369 wxFrame *winTop = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
370 if ( winTop )
371 {
372 wxIcon icon = winTop->GetIcon();
373 if ( icon.Ok() )
374 {
375 ::SendMessage(GetHwnd(), WM_SETICON,
376 (WPARAM)TRUE,
377 (LPARAM)GetHiconOf(icon));
378 }
379 }
380 }
381 #endif // __WIN95__
382
383 // move the dialog to its initial position without forcing repainting
384 int x, y, w, h;
385 (void)MSWGetCreateWindowCoords(pos, size, x, y, w, h);
386
387 if ( x == (int)CW_USEDEFAULT )
388 {
389 // centre it on the screen - what else can we do?
390 wxSize sizeDpy = wxGetDisplaySize();
391
392 x = (sizeDpy.x - w) / 2;
393 y = (sizeDpy.y - h) / 2;
394 }
395
396 #if !defined(__WXWINCE__) || defined(__WINCE_STANDARDSDK__)
397 if ( !::MoveWindow(GetHwnd(), x, y, w, h, FALSE) )
398 {
399 wxLogLastError(wxT("MoveWindow"));
400 }
401 #endif
402
403 if ( !title.empty() )
404 {
405 ::SetWindowText(GetHwnd(), title);
406 }
407
408 SubclassWin(m_hWnd);
409
410 return true;
411 #endif // __WXMICROWIN__/!__WXMICROWIN__
412 }
413
414 bool wxTopLevelWindowMSW::CreateFrame(const wxString& title,
415 const wxPoint& pos,
416 const wxSize& size)
417 {
418 WXDWORD exflags;
419 WXDWORD flags = MSWGetCreateWindowFlags(&exflags);
420
421 #if !defined(__HANDHELDPC__) && ((defined(_WIN32_WCE) && _WIN32_WCE < 400) || \
422 defined(__POCKETPC__) || \
423 defined(__SMARTPHONE__))
424 // Always expand to fit the screen in PocketPC or SmartPhone
425 wxSize sz(wxDefaultSize);
426 wxUnusedVar(size);
427 #else // other (including normal desktop) Windows
428 wxSize sz(size);
429 #endif
430
431 return MSWCreate(wxCanvasClassName, title, pos, sz, flags, exflags);
432 }
433
434 bool wxTopLevelWindowMSW::Create(wxWindow *parent,
435 wxWindowID id,
436 const wxString& title,
437 const wxPoint& pos,
438 const wxSize& size,
439 long style,
440 const wxString& name)
441 {
442 bool ret wxDUMMY_INITIALIZE(false);
443
444 // init our fields
445 Init();
446
447 wxSize sizeReal = size;
448 if ( !sizeReal.IsFullySpecified() )
449 {
450 sizeReal.SetDefaults(GetDefaultSize());
451 }
452
453 m_windowStyle = style;
454
455 SetName(name);
456
457 m_windowId = id == wxID_ANY ? NewControlId() : id;
458
459 wxTopLevelWindows.Append(this);
460
461 if ( parent )
462 parent->AddChild(this);
463
464 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
465 {
466 // we have different dialog templates to allows creation of dialogs
467 // with & without captions under MSWindows, resizeable or not (but a
468 // resizeable dialog always has caption - otherwise it would look too
469 // strange)
470
471 // we need 3 additional WORDs for dialog menu, class and title (as we
472 // don't use DS_SETFONT we don't need the fourth WORD for the font)
473 static const int dlgsize = sizeof(DLGTEMPLATE) + (sizeof(WORD) * 3);
474 DLGTEMPLATE *dlgTemplate = (DLGTEMPLATE *)malloc(dlgsize);
475 memset(dlgTemplate, 0, dlgsize);
476
477 // these values are arbitrary, they won't be used normally anyhow
478 dlgTemplate->x = 34;
479 dlgTemplate->y = 22;
480 dlgTemplate->cx = 144;
481 dlgTemplate->cy = 75;
482
483 // reuse the code in MSWGetStyle() but correct the results slightly for
484 // the dialog
485 dlgTemplate->style = MSWGetStyle(style, NULL);
486
487 // all dialogs are popups
488 dlgTemplate->style |= WS_POPUP;
489
490 // force 3D-look if necessary, it looks impossibly ugly otherwise
491 if ( style & (wxRESIZE_BORDER | wxCAPTION) )
492 dlgTemplate->style |= DS_MODALFRAME;
493
494 ret = CreateDialog(dlgTemplate, title, pos, sizeReal);
495 free(dlgTemplate);
496 }
497 else // !dialog
498 {
499 ret = CreateFrame(title, pos, sizeReal);
500 }
501
502 if ( ret && !(GetWindowStyleFlag() & wxCLOSE_BOX) )
503 {
504 EnableCloseButton(false);
505 }
506
507 // for some reason we need to manually send ourselves this message as
508 // otherwise the mnemonics are always shown -- even if they're configured
509 // to be hidden until "Alt" is pressed in the control panel
510 //
511 // this could indicate a bug somewhere else but for now this is the only
512 // fix we have
513 if ( ret )
514 {
515 ::SendMessage
516 (
517 GetHwnd(),
518 WM_UPDATEUISTATE,
519 MAKEWPARAM(UIS_INITIALIZE, UISF_HIDEFOCUS | UISF_HIDEACCEL),
520 0
521 );
522 }
523
524 // Native look is full screen window on Smartphones and Standard SDK
525 #if defined(__WXWINCE__)
526 if ( style & wxMAXIMIZE )
527 {
528 this->Maximize();
529 }
530 #endif
531
532 #ifdef __SMARTPHONE__
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 void wxTopLevelWindowMSW::RequestUserAttention(int flags)
906 {
907 // check if we can use FlashWindowEx()
908 #ifdef FLASHW_STOP
909 // available in the headers, check if it is supported by the system
910 typedef BOOL (WINAPI *FlashWindowEx_t)(FLASHWINFO *pfwi);
911 FlashWindowEx_t s_pfnFlashWindowEx = NULL;
912 if ( !s_pfnFlashWindowEx )
913 {
914 wxDynamicLibrary dllUser32(_T("user32.dll"));
915 s_pfnFlashWindowEx = (FlashWindowEx_t)
916 dllUser32.GetSymbol(_T("FlashWindowEx"));
917
918 // we can safely unload user32.dll here, it's goign to remain loaded as
919 // long as the program is running anyhow
920 }
921
922 if ( s_pfnFlashWindowEx )
923 {
924 WinStruct<FLASHWINFO> fwi;
925 fwi.hwnd = GetHwnd();
926 fwi.dwFlags = FLASHW_ALL;
927 if ( flags & wxUSER_ATTENTION_INFO )
928 {
929 // just flash a few times
930 fwi.uCount = 3;
931 }
932 else // wxUSER_ATTENTION_ERROR
933 {
934 // flash until the user notices it
935 fwi.dwFlags |= FLASHW_TIMERNOFG;
936 }
937
938 s_pfnFlashWindowEx(&fwi);
939 }
940 else // FlashWindowEx() not available
941 #endif // FLASHW_STOP
942 {
943 wxUnusedVar(flags);
944 #ifndef __WXWINCE__
945 ::FlashWindow(GetHwnd(), TRUE);
946 #endif // __WXWINCE__
947 }
948 }
949
950 // ----------------------------------------------------------------------------
951 // wxTopLevelWindow event handling
952 // ----------------------------------------------------------------------------
953
954 // Default activation behaviour - set the focus for the first child
955 // subwindow found.
956 void wxTopLevelWindowMSW::OnActivate(wxActivateEvent& event)
957 {
958 if ( event.GetActive() )
959 {
960 // restore focus to the child which was last focused unless we already
961 // have it
962 wxLogTrace(_T("focus"), _T("wxTLW %08x activated."), (int) m_hWnd);
963
964 wxWindow *winFocus = FindFocus();
965 if ( !winFocus || wxGetTopLevelParent(winFocus) != this )
966 {
967 wxWindow *parent = m_winLastFocused ? m_winLastFocused->GetParent()
968 : NULL;
969 if ( !parent )
970 {
971 parent = this;
972 }
973
974 wxSetFocusToChild(parent, &m_winLastFocused);
975 }
976 }
977 else // deactivating
978 {
979 // remember the last focused child if it is our child
980 m_winLastFocused = FindFocus();
981
982 if ( m_winLastFocused )
983 {
984 // let it know that it doesn't have focus any more
985 m_winLastFocused->HandleKillFocus((WXHWND)NULL);
986
987 // and don't remember it if it's a child from some other frame
988 if ( wxGetTopLevelParent(m_winLastFocused) != this )
989 {
990 m_winLastFocused = NULL;
991 }
992 }
993
994 wxLogTrace(_T("focus"),
995 _T("wxTLW %08x deactivated, last focused: %08x."),
996 (int) m_hWnd,
997 (int) (m_winLastFocused ? GetHwndOf(m_winLastFocused)
998 : NULL));
999
1000 event.Skip();
1001 }
1002 }
1003
1004 // the DialogProc for all wxWidgets dialogs
1005 LONG APIENTRY _EXPORT
1006 wxDlgProc(HWND hDlg,
1007 UINT message,
1008 WPARAM WXUNUSED(wParam),
1009 LPARAM WXUNUSED(lParam))
1010 {
1011 if ( message == WM_INITDIALOG )
1012 {
1013 // under CE, add a "Ok" button in the dialog title bar and make it full
1014 // screen
1015 //
1016 // VZ: we should probably allow for overriding this, e.g. by including
1017 // MAXIMIZED flag in the dialog style by default and doing this
1018 // only if it is present...
1019
1020 // Standard SDK doesn't have aygshell.dll: see
1021 // include/wx/msw/wince/libraries.h
1022 #if defined(__WXWINCE__) && !defined(__WINCE_STANDARDSDK__) && !defined(__HANDHELDPC__)
1023 SHINITDLGINFO shidi;
1024 shidi.dwMask = SHIDIM_FLAGS;
1025 shidi.dwFlags = SHIDIF_SIZEDLGFULLSCREEN
1026 #ifndef __SMARTPHONE__
1027 | SHIDIF_DONEBUTTON
1028 #endif
1029 ;
1030 shidi.hDlg = hDlg;
1031 SHInitDialog( &shidi );
1032 #else // no SHInitDialog()
1033 wxUnusedVar(hDlg);
1034 #endif
1035 }
1036
1037 // for almost all messages, returning FALSE means that we didn't process
1038 // the message
1039 //
1040 // for WM_INITDIALOG, returning TRUE tells system to set focus to
1041 // the first control in the dialog box, but as we set the focus
1042 // ourselves, we return FALSE for it as well
1043 return FALSE;
1044 }
1045
1046 // ============================================================================
1047 // wxTLWHiddenParentModule implementation
1048 // ============================================================================
1049
1050 HWND wxTLWHiddenParentModule::ms_hwnd = NULL;
1051
1052 const wxChar *wxTLWHiddenParentModule::ms_className = NULL;
1053
1054 bool wxTLWHiddenParentModule::OnInit()
1055 {
1056 ms_hwnd = NULL;
1057 ms_className = NULL;
1058
1059 return true;
1060 }
1061
1062 void wxTLWHiddenParentModule::OnExit()
1063 {
1064 if ( ms_hwnd )
1065 {
1066 if ( !::DestroyWindow(ms_hwnd) )
1067 {
1068 wxLogLastError(_T("DestroyWindow(hidden TLW parent)"));
1069 }
1070
1071 ms_hwnd = NULL;
1072 }
1073
1074 if ( ms_className )
1075 {
1076 if ( !::UnregisterClass(ms_className, wxGetInstance()) )
1077 {
1078 wxLogLastError(_T("UnregisterClass(\"wxTLWHiddenParent\")"));
1079 }
1080
1081 ms_className = NULL;
1082 }
1083 }
1084
1085 /* static */
1086 HWND wxTLWHiddenParentModule::GetHWND()
1087 {
1088 if ( !ms_hwnd )
1089 {
1090 if ( !ms_className )
1091 {
1092 static const wxChar *HIDDEN_PARENT_CLASS = _T("wxTLWHiddenParent");
1093
1094 WNDCLASS wndclass;
1095 wxZeroMemory(wndclass);
1096
1097 wndclass.lpfnWndProc = DefWindowProc;
1098 wndclass.hInstance = wxGetInstance();
1099 wndclass.lpszClassName = HIDDEN_PARENT_CLASS;
1100
1101 if ( !::RegisterClass(&wndclass) )
1102 {
1103 wxLogLastError(_T("RegisterClass(\"wxTLWHiddenParent\")"));
1104 }
1105 else
1106 {
1107 ms_className = HIDDEN_PARENT_CLASS;
1108 }
1109 }
1110
1111 ms_hwnd = ::CreateWindow(ms_className, wxEmptyString, 0, 0, 0, 0, 0, NULL,
1112 (HMENU)NULL, wxGetInstance(), NULL);
1113 if ( !ms_hwnd )
1114 {
1115 wxLogLastError(_T("CreateWindow(hidden TLW parent)"));
1116 }
1117 }
1118
1119 return ms_hwnd;
1120 }
1121
1122