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