]> git.saurik.com Git - wxWidgets.git/blob - src/msw/toplevel.cpp
fix crash in wxWindowGTK::GTKHandleUnrealize(), closes #14752
[wxWidgets.git] / src / msw / toplevel.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/toplevel.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/app.h"
31 #include "wx/dialog.h"
32 #include "wx/string.h"
33 #include "wx/log.h"
34 #include "wx/intl.h"
35 #include "wx/frame.h"
36 #include "wx/menu.h"
37 #include "wx/containr.h" // wxSetFocusToChild()
38 #include "wx/module.h"
39 #endif //WX_PRECOMP
40
41 #include "wx/dynlib.h"
42
43 #include "wx/msw/private.h"
44 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__)
45 #include <ole2.h>
46 #include <shellapi.h>
47 // Standard SDK doesn't have aygshell.dll: see include/wx/msw/wince/libraries.h
48 #if _WIN32_WCE < 400 || !defined(__WINCE_STANDARDSDK__)
49 #include <aygshell.h>
50 #endif
51 #endif
52
53 #include "wx/msw/winundef.h"
54 #include "wx/msw/missing.h"
55
56 #include "wx/display.h"
57
58 #ifndef ICON_BIG
59 #define ICON_BIG 1
60 #endif
61
62 #ifndef ICON_SMALL
63 #define ICON_SMALL 0
64 #endif
65
66 // ----------------------------------------------------------------------------
67 // stubs for missing functions under MicroWindows
68 // ----------------------------------------------------------------------------
69
70 #ifdef __WXMICROWIN__
71
72 // static inline bool IsIconic(HWND WXUNUSED(hwnd)) { return false; }
73 static inline bool IsZoomed(HWND WXUNUSED(hwnd)) { return false; }
74
75 #endif // __WXMICROWIN__
76
77 // NB: wxDlgProc must be defined here and not in dialog.cpp because the latter
78 // is not included by wxUniv build which does need wxDlgProc
79 LONG APIENTRY _EXPORT
80 wxDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
81
82 // ----------------------------------------------------------------------------
83 // wxTLWHiddenParentModule: used to manage the hidden parent window (we need a
84 // module to ensure that the window is always deleted)
85 // ----------------------------------------------------------------------------
86
87 class wxTLWHiddenParentModule : public wxModule
88 {
89 public:
90 // module init/finalize
91 virtual bool OnInit();
92 virtual void OnExit();
93
94 // get the hidden window (creates on demand)
95 static HWND GetHWND();
96
97 private:
98 // the HWND of the hidden parent
99 static HWND ms_hwnd;
100
101 // the class used to create it
102 static const wxChar *ms_className;
103
104 DECLARE_DYNAMIC_CLASS(wxTLWHiddenParentModule)
105 };
106
107 IMPLEMENT_DYNAMIC_CLASS(wxTLWHiddenParentModule, wxModule)
108
109 // ============================================================================
110 // wxTopLevelWindowMSW implementation
111 // ============================================================================
112
113 BEGIN_EVENT_TABLE(wxTopLevelWindowMSW, wxTopLevelWindowBase)
114 EVT_ACTIVATE(wxTopLevelWindowMSW::OnActivate)
115 END_EVENT_TABLE()
116
117 // ----------------------------------------------------------------------------
118 // wxTopLevelWindowMSW creation
119 // ----------------------------------------------------------------------------
120
121 void wxTopLevelWindowMSW::Init()
122 {
123 m_iconized =
124 m_maximizeOnShow = false;
125
126 // Data to save/restore when calling ShowFullScreen
127 m_fsStyle = 0;
128 m_fsOldWindowStyle = 0;
129 m_fsIsMaximized = false;
130 m_fsIsShowing = false;
131
132 m_winLastFocused = NULL;
133
134 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
135 m_MenuBarHWND = 0;
136 #endif
137
138 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
139 SHACTIVATEINFO* info = new SHACTIVATEINFO;
140 wxZeroMemory(*info);
141 info->cbSize = sizeof(SHACTIVATEINFO);
142
143 m_activateInfo = (void*) info;
144 #endif
145
146 m_menuSystem = NULL;
147 }
148
149 WXDWORD wxTopLevelWindowMSW::MSWGetStyle(long style, WXDWORD *exflags) const
150 {
151 // let the base class deal with the common styles but fix the ones which
152 // don't make sense for us (we also deal with the borders ourselves)
153 WXDWORD msflags = wxWindow::MSWGetStyle
154 (
155 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exflags
156 ) & ~WS_CHILD & ~WS_VISIBLE;
157
158 // For some reason, WS_VISIBLE needs to be defined on creation for
159 // SmartPhone 2003. The title can fail to be displayed otherwise.
160 #if defined(__SMARTPHONE__) || (defined(__WXWINCE__) && _WIN32_WCE < 400)
161 msflags |= WS_VISIBLE;
162 ((wxTopLevelWindowMSW*)this)->wxWindowBase::Show(true);
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 need to use
169 // WS_POPUP in a few cases just to avoid having caption/border which we
170 // don't want
171
172 // border and caption styles
173 if ( ( style & wxRESIZE_BORDER ) && !IsAlwaysMaximized())
174 msflags |= WS_THICKFRAME;
175 else if ( exflags && ((style & wxBORDER_DOUBLE) || (style & wxBORDER_RAISED)) )
176 *exflags |= WS_EX_DLGMODALFRAME;
177 else if ( !(style & wxBORDER_NONE) )
178 msflags |= WS_BORDER;
179 #ifndef __POCKETPC__
180 else
181 msflags |= WS_POPUP;
182 #endif
183
184 // normally we consider that all windows without a caption must be popups,
185 // but CE is an exception: there windows normally do not have the caption
186 // but shouldn't be made popups as popups can't have menus and don't look
187 // like normal windows anyhow
188
189 // TODO: Smartphone appears to like wxCAPTION, but we should check that
190 // we need it.
191 #if defined(__SMARTPHONE__) || !defined(__WXWINCE__)
192 if ( style & wxCAPTION )
193 msflags |= WS_CAPTION;
194 #ifndef __WXWINCE__
195 else
196 msflags |= WS_POPUP;
197 #endif // !__WXWINCE__
198 #endif
199
200 // next translate the individual flags
201
202 // WS_EX_CONTEXTHELP is incompatible with WS_MINIMIZEBOX and WS_MAXIMIZEBOX
203 // and is ignored if we specify both of them, but chances are that if we
204 // use wxWS_EX_CONTEXTHELP, we really do want to have the context help
205 // button while wxMINIMIZE/wxMAXIMIZE are included by default, so the help
206 // takes precedence
207 if ( !(GetExtraStyle() & wxWS_EX_CONTEXTHELP) )
208 {
209 if ( style & wxMINIMIZE_BOX )
210 msflags |= WS_MINIMIZEBOX;
211 if ( style & wxMAXIMIZE_BOX )
212 msflags |= WS_MAXIMIZEBOX;
213 }
214
215 #ifndef __WXWINCE__
216 // notice that if wxCLOSE_BOX is specified we need to use WS_SYSMENU too as
217 // otherwise the close box doesn't appear
218 if ( style & (wxSYSTEM_MENU | wxCLOSE_BOX) )
219 msflags |= WS_SYSMENU;
220 #endif // !__WXWINCE__
221
222 // NB: under CE these 2 styles are not supported currently, we should
223 // call Minimize()/Maximize() "manually" if we want to support them
224 if ( style & wxMINIMIZE )
225 msflags |= WS_MINIMIZE;
226
227 if ( style & wxMAXIMIZE )
228 msflags |= WS_MAXIMIZE;
229
230 // Keep this here because it saves recoding this function in wxTinyFrame
231 if ( style & wxTINY_CAPTION )
232 msflags |= WS_CAPTION;
233
234 if ( exflags )
235 {
236 // there is no taskbar under CE, so omit all this
237 #if !defined(__WXWINCE__)
238 if ( !(GetExtraStyle() & wxTOPLEVEL_EX_DIALOG) )
239 {
240 if ( style & wxFRAME_TOOL_WINDOW )
241 {
242 // create the palette-like window
243 *exflags |= WS_EX_TOOLWINDOW;
244
245 // tool windows shouldn't appear on the taskbar (as documented)
246 style |= wxFRAME_NO_TASKBAR;
247 }
248
249 // We have to solve 2 different problems here:
250 //
251 // 1. frames with wxFRAME_NO_TASKBAR flag shouldn't appear in the
252 // taskbar even if they don't have a parent
253 //
254 // 2. frames without this style should appear in the taskbar even
255 // if they're owned (Windows only puts non owned windows into
256 // the taskbar normally)
257 //
258 // The second one is solved here by using WS_EX_APPWINDOW flag, the
259 // first one is dealt with in our MSWGetParent() method
260 // implementation
261 if ( !(style & wxFRAME_NO_TASKBAR) && GetParent() )
262 {
263 // need to force the frame to appear in the taskbar
264 *exflags |= WS_EX_APPWINDOW;
265 }
266 //else: nothing to do [here]
267 }
268
269 if ( GetExtraStyle() & wxWS_EX_CONTEXTHELP )
270 *exflags |= WS_EX_CONTEXTHELP;
271 #endif // !__WXWINCE__
272
273 if ( style & wxSTAY_ON_TOP )
274 *exflags |= WS_EX_TOPMOST;
275 }
276
277 return msflags;
278 }
279
280 WXHWND wxTopLevelWindowMSW::MSWGetParent() const
281 {
282 // for the frames without wxFRAME_FLOAT_ON_PARENT style we should use NULL
283 // parent HWND or it would be always on top of its parent which is not what
284 // we usually want (in fact, we only want it for frames with the
285 // wxFRAME_FLOAT_ON_PARENT flag)
286 HWND hwndParent = NULL;
287 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT) )
288 {
289 const wxWindow *parent = GetParent();
290
291 if ( !parent )
292 {
293 // this flag doesn't make sense then and will be ignored
294 wxFAIL_MSG( wxT("wxFRAME_FLOAT_ON_PARENT but no parent?") );
295 }
296 else
297 {
298 hwndParent = GetHwndOf(parent);
299 }
300 }
301 //else: don't float on parent, must not be owned
302
303 // now deal with the 2nd taskbar-related problem (see comments above in
304 // MSWGetStyle())
305 if ( HasFlag(wxFRAME_NO_TASKBAR) && !hwndParent )
306 {
307 // use hidden parent
308 hwndParent = wxTLWHiddenParentModule::GetHWND();
309 }
310
311 return (WXHWND)hwndParent;
312 }
313
314 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
315 bool wxTopLevelWindowMSW::HandleSettingChange(WXWPARAM wParam, WXLPARAM lParam)
316 {
317 SHACTIVATEINFO *info = (SHACTIVATEINFO*) m_activateInfo;
318 if ( info )
319 {
320 SHHandleWMSettingChange(GetHwnd(), wParam, lParam, info);
321 }
322
323 return wxWindowMSW::HandleSettingChange(wParam, lParam);
324 }
325 #endif
326
327 WXLRESULT wxTopLevelWindowMSW::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
328 {
329 WXLRESULT rc = 0;
330 bool processed = false;
331
332 switch ( message )
333 {
334 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
335 case WM_ACTIVATE:
336 {
337 SHACTIVATEINFO* info = (SHACTIVATEINFO*) m_activateInfo;
338 if (info)
339 {
340 DWORD flags = 0;
341 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG) flags = SHA_INPUTDIALOG;
342 SHHandleWMActivate(GetHwnd(), wParam, lParam, info, flags);
343 }
344
345 // This implicitly sends a wxEVT_ACTIVATE_APP event
346 if (wxTheApp)
347 wxTheApp->SetActive(wParam != 0, FindFocus());
348
349 break;
350 }
351 case WM_HIBERNATE:
352 {
353 if (wxTheApp)
354 {
355 wxActivateEvent event(wxEVT_HIBERNATE, true, wxID_ANY);
356 event.SetEventObject(wxTheApp);
357 processed = wxTheApp->ProcessEvent(event);
358 }
359 break;
360 }
361 #endif // __SMARTPHONE__ || __POCKETPC__
362
363 case WM_SYSCOMMAND:
364 {
365 // From MSDN:
366 //
367 // ... the four low-order bits of the wParam parameter are
368 // used internally by the system. To obtain the correct
369 // result when testing the value of wParam, an application
370 // must combine the value 0xFFF0 with the wParam value by
371 // using the bitwise AND operator.
372 unsigned id = wParam & 0xfff0;
373
374 // Preserve the focus when minimizing/restoring the window: we
375 // need to do it manually as DefWindowProc() doesn't appear to
376 // do this for us for some reason (perhaps because we don't use
377 // WM_NEXTDLGCTL for setting focus?). Moreover, our code in
378 // OnActivate() doesn't work in this case as we receive the
379 // deactivation event too late when the window is being
380 // minimized and the focus is already NULL by then. Similarly,
381 // we receive the activation event too early and restoring
382 // focus in it fails because the window is still minimized. So
383 // we need to do it here.
384 if ( id == SC_MINIMIZE )
385 {
386 // For minimization, it's simple enough: just save the
387 // focus as usual. The important thing is that we're not
388 // minimized yet, so this works correctly.
389 DoSaveLastFocus();
390 }
391 else if ( id == SC_RESTORE )
392 {
393 // For restoring, it's trickier as DefWindowProc() sets
394 // focus to the window itself. So run it first and restore
395 // our saved focus only afterwards.
396 processed = true;
397 rc = wxTopLevelWindowBase::MSWWindowProc(message,
398 wParam, lParam);
399
400 DoRestoreLastFocus();
401 }
402
403 #ifndef __WXUNIVERSAL__
404 // We need to generate events for the custom items added to the
405 // system menu if it had been created (and presumably modified).
406 // As SC_SIZE is the first of the system-defined commands, we
407 // only do this for the custom commands before it and leave
408 // SC_SIZE and everything after it to DefWindowProc().
409 if ( m_menuSystem && id < SC_SIZE )
410 {
411 if ( m_menuSystem->MSWCommand(0 /* unused anyhow */, id) )
412 processed = true;
413 }
414 #endif // #ifndef __WXUNIVERSAL__
415 }
416 break;
417 }
418
419 if ( !processed )
420 rc = wxTopLevelWindowBase::MSWWindowProc(message, wParam, lParam);
421
422 return rc;
423 }
424
425 bool wxTopLevelWindowMSW::CreateDialog(const void *dlgTemplate,
426 const wxString& title,
427 const wxPoint& pos,
428 const wxSize& size)
429 {
430 #ifdef __WXMICROWIN__
431 // no dialogs support under MicroWin yet
432 return CreateFrame(title, pos, size);
433 #else // !__WXMICROWIN__
434 // static cast is valid as we're only ever called for dialogs
435 wxWindow * const
436 parent = static_cast<wxDialog *>(this)->GetParentForModalDialog();
437
438 m_hWnd = (WXHWND)::CreateDialogIndirect
439 (
440 wxGetInstance(),
441 (DLGTEMPLATE*)dlgTemplate,
442 parent ? GetHwndOf(parent) : NULL,
443 (DLGPROC)wxDlgProc
444 );
445
446 if ( !m_hWnd )
447 {
448 wxFAIL_MSG(wxT("Failed to create dialog. Incorrect DLGTEMPLATE?"));
449
450 wxLogSysError(wxT("Can't create dialog using memory template"));
451
452 return false;
453 }
454
455 #if !defined(__WXWINCE__)
456 // For some reason, the system menu is activated when we use the
457 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
458 if ( HasExtraStyle(wxWS_EX_CONTEXTHELP) )
459 {
460 wxFrame *winTop = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
461 if ( winTop )
462 {
463 wxIcon icon = winTop->GetIcon();
464 if ( icon.IsOk() )
465 {
466 ::SendMessage(GetHwnd(), WM_SETICON,
467 (WPARAM)TRUE,
468 (LPARAM)GetHiconOf(icon));
469 }
470 }
471 }
472 #endif // !__WXWINCE__
473
474 if ( !title.empty() )
475 {
476 ::SetWindowText(GetHwnd(), title.t_str());
477 }
478
479 SubclassWin(m_hWnd);
480
481 #if !defined(__WXWINCE__) || defined(__WINCE_STANDARDSDK__)
482 // move the dialog to its initial position without forcing repainting
483 int x, y, w, h;
484 (void)MSWGetCreateWindowCoords(pos, size, x, y, w, h);
485
486 if ( x == (int)CW_USEDEFAULT )
487 {
488 // Let the system position the window, just set its size.
489 ::SetWindowPos(GetHwnd(), 0,
490 0, 0, w, h,
491 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
492 }
493 else // Move the window to the desired location and set its size too.
494 {
495 if ( !::MoveWindow(GetHwnd(), x, y, w, h, FALSE) )
496 {
497 wxLogLastError(wxT("MoveWindow"));
498 }
499 }
500 #endif // !__WXWINCE__
501
502 #ifdef __SMARTPHONE__
503 // Work around title non-display glitch
504 Show(false);
505 #endif
506
507 return true;
508 #endif // __WXMICROWIN__/!__WXMICROWIN__
509 }
510
511 bool wxTopLevelWindowMSW::CreateFrame(const wxString& title,
512 const wxPoint& pos,
513 const wxSize& size)
514 {
515 WXDWORD exflags;
516 WXDWORD flags = MSWGetCreateWindowFlags(&exflags);
517
518 const wxSize sz = IsAlwaysMaximized() ? wxDefaultSize : size;
519
520 #ifndef __WXWINCE__
521 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
522 exflags |= WS_EX_LAYOUTRTL;
523 #endif
524
525 return MSWCreate(MSWGetRegisteredClassName(),
526 title.t_str(), pos, sz, flags, exflags);
527 }
528
529 bool wxTopLevelWindowMSW::Create(wxWindow *parent,
530 wxWindowID id,
531 const wxString& title,
532 const wxPoint& pos,
533 const wxSize& size,
534 long style,
535 const wxString& name)
536 {
537 wxSize sizeReal = size;
538 if ( !sizeReal.IsFullySpecified() )
539 {
540 sizeReal.SetDefaults(GetDefaultSize());
541 }
542
543 // notice that we should append this window to wxTopLevelWindows list
544 // before calling CreateBase() as it behaves differently for TLW and
545 // non-TLW windows
546 wxTopLevelWindows.Append(this);
547
548 bool ret = CreateBase(parent, id, pos, sizeReal, style, name);
549 if ( !ret )
550 return false;
551
552 if ( parent )
553 parent->AddChild(this);
554
555 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
556 {
557 // we have different dialog templates to allows creation of dialogs
558 // with & without captions under MSWindows, resizable or not (but a
559 // resizable dialog always has caption - otherwise it would look too
560 // strange)
561
562 // we need 3 additional WORDs for dialog menu, class and title (as we
563 // don't use DS_SETFONT we don't need the fourth WORD for the font)
564 static const int dlgsize = sizeof(DLGTEMPLATE) + (sizeof(WORD) * 3);
565 DLGTEMPLATE *dlgTemplate = (DLGTEMPLATE *)malloc(dlgsize);
566 memset(dlgTemplate, 0, dlgsize);
567
568 // these values are arbitrary, they won't be used normally anyhow
569 dlgTemplate->x = 34;
570 dlgTemplate->y = 22;
571 dlgTemplate->cx = 144;
572 dlgTemplate->cy = 75;
573
574 // reuse the code in MSWGetStyle() but correct the results slightly for
575 // the dialog
576 //
577 // NB: we need a temporary variable as we can't pass pointer to
578 // dwExtendedStyle directly, it's not aligned correctly for 64 bit
579 // architectures
580 WXDWORD dwExtendedStyle;
581 dlgTemplate->style = MSWGetStyle(style, &dwExtendedStyle);
582 dlgTemplate->dwExtendedStyle = dwExtendedStyle;
583
584 // all dialogs are popups
585 dlgTemplate->style |= WS_POPUP;
586
587 #ifndef __WXWINCE__
588 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
589 {
590 dlgTemplate->dwExtendedStyle |= WS_EX_LAYOUTRTL;
591 }
592
593 // force 3D-look if necessary, it looks impossibly ugly otherwise
594 if ( style & (wxRESIZE_BORDER | wxCAPTION) )
595 dlgTemplate->style |= DS_MODALFRAME;
596 #endif
597
598 ret = CreateDialog(dlgTemplate, title, pos, sizeReal);
599 free(dlgTemplate);
600 }
601 else // !dialog
602 {
603 ret = CreateFrame(title, pos, sizeReal);
604 }
605
606 #ifndef __WXWINCE__
607 if ( ret && !(GetWindowStyleFlag() & wxCLOSE_BOX) )
608 {
609 EnableCloseButton(false);
610 }
611 #endif
612
613 // for standard dialogs the dialog manager generates WM_CHANGEUISTATE
614 // itself but for custom windows we have to do it ourselves in order to
615 // make the keyboard indicators (such as underlines for accelerators and
616 // focus rectangles) work under Win2k+
617 if ( ret )
618 {
619 MSWUpdateUIState(UIS_INITIALIZE);
620 }
621
622 // Note: if we include PocketPC in this test, dialogs can fail to show up,
623 // for example the text entry dialog in the dialogs sample. Problem with Maximise()?
624 #if defined(__WXWINCE__) && (defined(__SMARTPHONE__) || defined(__WINCE_STANDARDSDK__))
625 if ( ( style & wxMAXIMIZE ) || IsAlwaysMaximized() )
626 {
627 this->Maximize();
628 }
629 #endif
630
631 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
632 SetRightMenu(); // to nothing for initialization
633 #endif
634
635 return ret;
636 }
637
638 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
639 {
640 delete m_menuSystem;
641
642 SendDestroyEvent();
643
644 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
645 SHACTIVATEINFO* info = (SHACTIVATEINFO*) m_activateInfo;
646 delete info;
647 m_activateInfo = NULL;
648 #endif
649
650 // after destroying an owned window, Windows activates the next top level
651 // window in Z order but it may be different from our owner (to reproduce
652 // this simply Alt-TAB to another application and back before closing the
653 // owned frame) whereas we always want to yield activation to our parent
654 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT) )
655 {
656 wxWindow *parent = GetParent();
657 if ( parent )
658 {
659 ::BringWindowToTop(GetHwndOf(parent));
660 }
661 }
662 }
663
664 // ----------------------------------------------------------------------------
665 // wxTopLevelWindowMSW showing
666 // ----------------------------------------------------------------------------
667
668 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd)
669 {
670 ::ShowWindow(GetHwnd(), nShowCmd);
671
672 // Hiding the window doesn't change its iconized state.
673 if ( nShowCmd != SW_HIDE )
674 {
675 // Otherwise restoring, maximizing or showing the window normally also
676 // makes it not iconized and only minimizing it does make it iconized.
677 m_iconized = nShowCmd == SW_MINIMIZE;
678 }
679 }
680
681 void wxTopLevelWindowMSW::ShowWithoutActivating()
682 {
683 if ( !wxWindowBase::Show(true) )
684 return;
685
686 DoShowWindow(SW_SHOWNA);
687 }
688
689 bool wxTopLevelWindowMSW::Show(bool show)
690 {
691 // don't use wxWindow version as we want to call DoShowWindow() ourselves
692 if ( !wxWindowBase::Show(show) )
693 return false;
694
695 int nShowCmd;
696 if ( show )
697 {
698 if ( m_maximizeOnShow )
699 {
700 // show and maximize
701 nShowCmd = SW_MAXIMIZE;
702
703 // This is necessary, or no window appears
704 #if defined( __WINCE_STANDARDSDK__) || defined(__SMARTPHONE__)
705 DoShowWindow(SW_SHOW);
706 #endif
707
708 m_maximizeOnShow = false;
709 }
710 else if ( m_iconized )
711 {
712 // We were iconized while we were hidden, so now we need to show
713 // the window in iconized state.
714 nShowCmd = SW_MINIMIZE;
715 }
716 else if ( ::IsIconic(GetHwnd()) )
717 {
718 // We were restored while we were hidden, so now we need to show
719 // the window in its normal state.
720 //
721 // As below, don't activate some kinds of windows.
722 if ( HasFlag(wxFRAME_TOOL_WINDOW) || !IsEnabled() )
723 nShowCmd = SW_SHOWNOACTIVATE;
724 else
725 nShowCmd = SW_RESTORE;
726 }
727 else // just show
728 {
729 // we shouldn't use SW_SHOW which also activates the window for
730 // tool frames (as they shouldn't steal focus from the main window)
731 // nor for the currently disabled windows as they would be enabled
732 // as a side effect
733 if ( HasFlag(wxFRAME_TOOL_WINDOW) || !IsEnabled() )
734 nShowCmd = SW_SHOWNA;
735 else
736 nShowCmd = SW_SHOW;
737 }
738 }
739 else // hide
740 {
741 nShowCmd = SW_HIDE;
742 }
743
744 #if wxUSE_DEFERRED_SIZING
745 // we only set pending size if we're maximized before being shown, now that
746 // we're shown we don't need it any more (it is reset in size event handler
747 // for child windows but we have to do it ourselves for this parent window)
748 //
749 // make sure to reset it before actually showing the window as this will
750 // generate WM_SIZE events and we want to use the correct client size from
751 // them, not the size returned by WM_NCCALCSIZE in DoGetClientSize() which
752 // turns out to be wrong for maximized windows (see #11762)
753 m_pendingSize = wxDefaultSize;
754 #endif // wxUSE_DEFERRED_SIZING
755
756 DoShowWindow(nShowCmd);
757
758 #if defined(__WXWINCE__) && (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__))
759 // Addornments have to be added when the frame is the correct size
760 wxFrame* frame = wxDynamicCast(this, wxFrame);
761 if (frame && frame->GetMenuBar())
762 frame->GetMenuBar()->AddAdornments(GetWindowStyleFlag());
763 #endif
764
765 return true;
766 }
767
768 void wxTopLevelWindowMSW::Raise()
769 {
770 ::SetForegroundWindow(GetHwnd());
771 }
772
773 // ----------------------------------------------------------------------------
774 // wxTopLevelWindowMSW maximize/minimize
775 // ----------------------------------------------------------------------------
776
777 void wxTopLevelWindowMSW::Maximize(bool maximize)
778 {
779 if ( IsShown() )
780 {
781 // just maximize it directly
782 DoShowWindow(maximize ? SW_MAXIMIZE : SW_RESTORE);
783 }
784 else // hidden
785 {
786 // we can't maximize the hidden frame because it shows it as well,
787 // so just remember that we should do it later in this case
788 m_maximizeOnShow = maximize;
789
790 #if wxUSE_DEFERRED_SIZING
791 // after calling Maximize() the client code expects to get the frame
792 // "real" size and doesn't want to know that, because of implementation
793 // details, the frame isn't really maximized yet but will be only once
794 // it's shown, so return our size as it will be then in this case
795 if ( maximize )
796 {
797 // we must only change pending size here, and not call SetSize()
798 // because otherwise Windows would think that this (full screen)
799 // size is the natural size for the frame and so would use it when
800 // the user clicks on "restore" title bar button instead of the
801 // correct initial frame size
802 //
803 // NB: unfortunately we don't know which display we're on yet so we
804 // have to use the default one
805 m_pendingSize = wxGetClientDisplayRect().GetSize();
806 }
807 //else: can't do anything in this case, we don't have the old size
808 #endif // wxUSE_DEFERRED_SIZING
809 }
810 }
811
812 bool wxTopLevelWindowMSW::IsMaximized() const
813 {
814 return IsAlwaysMaximized() ||
815 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__) && !defined(__WINCE_STANDARDSDK__)
816
817 (::IsZoomed(GetHwnd()) != 0) ||
818 #endif
819 m_maximizeOnShow;
820 }
821
822 void wxTopLevelWindowMSW::Iconize(bool iconize)
823 {
824 if ( iconize == m_iconized )
825 {
826 // Do nothing, in particular don't restore non-iconized windows when
827 // Iconize(false) is called as this would wrongly un-maximize them.
828 return;
829 }
830
831 if ( IsShown() )
832 {
833 // change the window state immediately
834 DoShowWindow(iconize ? SW_MINIMIZE : SW_RESTORE);
835 }
836 else // hidden
837 {
838 // iconizing the window shouldn't show it so just update the internal
839 // state (otherwise it's done by DoShowWindow() itself)
840 m_iconized = iconize;
841 }
842 }
843
844 bool wxTopLevelWindowMSW::IsIconized() const
845 {
846 #ifdef __WXWINCE__
847 return false;
848 #else
849 if ( !IsShown() )
850 return m_iconized;
851
852 // don't use m_iconized, it may be briefly out of sync with the real state
853 // as it's only modified when we receive a WM_SIZE and we could be called
854 // from an event handler from one of the messages we receive before it,
855 // such as WM_MOVE
856 return ::IsIconic(GetHwnd()) != 0;
857 #endif
858 }
859
860 void wxTopLevelWindowMSW::Restore()
861 {
862 DoShowWindow(SW_RESTORE);
863 }
864
865 void wxTopLevelWindowMSW::SetLayoutDirection(wxLayoutDirection dir)
866 {
867 if ( dir == wxLayout_Default )
868 dir = wxTheApp->GetLayoutDirection();
869
870 if ( dir != wxLayout_Default )
871 wxTopLevelWindowBase::SetLayoutDirection(dir);
872 }
873
874 // ----------------------------------------------------------------------------
875 // wxTopLevelWindowMSW geometry
876 // ----------------------------------------------------------------------------
877
878 #ifndef __WXWINCE__
879
880 void wxTopLevelWindowMSW::DoGetPosition(int *x, int *y) const
881 {
882 if ( IsIconized() )
883 {
884 WINDOWPLACEMENT wp;
885 wp.length = sizeof(WINDOWPLACEMENT);
886 if ( ::GetWindowPlacement(GetHwnd(), &wp) )
887 {
888 RECT& rc = wp.rcNormalPosition;
889
890 // the position returned by GetWindowPlacement() is in workspace
891 // coordinates except for windows with WS_EX_TOOLWINDOW style
892 if ( !HasFlag(wxFRAME_TOOL_WINDOW) )
893 {
894 // we must use the correct display for the translation as the
895 // task bar might be shown on one display but not the other one
896 int n = wxDisplay::GetFromWindow(this);
897 wxDisplay dpy(n == wxNOT_FOUND ? 0 : n);
898 const wxPoint ptOfs = dpy.GetClientArea().GetPosition() -
899 dpy.GetGeometry().GetPosition();
900
901 rc.left += ptOfs.x;
902 rc.top += ptOfs.y;
903 }
904
905 if ( x )
906 *x = rc.left;
907 if ( y )
908 *y = rc.top;
909
910 return;
911 }
912
913 wxLogLastError(wxT("GetWindowPlacement"));
914 }
915 //else: normal case
916
917 wxTopLevelWindowBase::DoGetPosition(x, y);
918 }
919
920 void wxTopLevelWindowMSW::DoGetSize(int *width, int *height) const
921 {
922 if ( IsIconized() )
923 {
924 WINDOWPLACEMENT wp;
925 wp.length = sizeof(WINDOWPLACEMENT);
926 if ( ::GetWindowPlacement(GetHwnd(), &wp) )
927 {
928 const RECT& rc = wp.rcNormalPosition;
929
930 if ( width )
931 *width = rc.right - rc.left;
932 if ( height )
933 *height = rc.bottom - rc.top;
934
935 return;
936 }
937
938 wxLogLastError(wxT("GetWindowPlacement"));
939 }
940 //else: normal case
941
942 wxTopLevelWindowBase::DoGetSize(width, height);
943 }
944
945 #endif // __WXWINCE__
946
947 void
948 wxTopLevelWindowMSW::MSWGetCreateWindowCoords(const wxPoint& pos,
949 const wxSize& size,
950 int& x, int& y,
951 int& w, int& h) const
952 {
953 // let the system position the window if no explicit position was specified
954 if ( pos.x == wxDefaultCoord )
955 {
956 // if x is set to CW_USEDEFAULT, y parameter is ignored anyhow so we
957 // can just as well set it to CW_USEDEFAULT as well
958 x =
959 y = CW_USEDEFAULT;
960 }
961 else
962 {
963 // OTOH, if x is not set to CW_USEDEFAULT, y shouldn't be set to it
964 // neither because it is not handled as a special value by Windows then
965 // and so we have to choose some default value for it, even if a
966 // completely arbitrary one
967 static const int DEFAULT_Y = 200;
968
969 x = pos.x;
970 y = pos.y == wxDefaultCoord ? DEFAULT_Y : pos.y;
971 }
972
973 if ( size.x == wxDefaultCoord || size.y == wxDefaultCoord )
974 {
975 // We don't use CW_USEDEFAULT here for several reasons:
976 //
977 // 1. It results in huge frames on modern screens (1000*800 is not
978 // uncommon on my 1280*1024 screen) which is way too big for a half
979 // empty frame of most of wxWidgets samples for example)
980 //
981 // 2. It is buggy for frames with wxFRAME_TOOL_WINDOW style for which
982 // the default is for whatever reason 8*8 which breaks client <->
983 // window size calculations (it would be nice if it didn't, but it
984 // does and the simplest way to fix it seemed to change the broken
985 // default size anyhow)
986 //
987 // 3. There is just no advantage in doing it: with x and y it is
988 // possible that [future versions of] Windows position the new top
989 // level window in some smart way which we can't do, but we can
990 // guess a reasonably good size for a new window just as well
991 // ourselves
992 //
993 // The only exception is for the Windows CE platform where the system
994 // does know better than we how should the windows be sized
995 #ifdef _WIN32_WCE
996 w =
997 h = CW_USEDEFAULT;
998 #else // !_WIN32_WCE
999 wxSize sizeReal = size;
1000 sizeReal.SetDefaults(GetDefaultSize());
1001
1002 w = sizeReal.x;
1003 h = sizeReal.y;
1004 #endif // _WIN32_WCE/!_WIN32_WCE
1005 }
1006 else
1007 {
1008 w = size.x;
1009 h = size.y;
1010 }
1011 }
1012
1013 // ----------------------------------------------------------------------------
1014 // wxTopLevelWindowMSW fullscreen
1015 // ----------------------------------------------------------------------------
1016
1017 bool wxTopLevelWindowMSW::ShowFullScreen(bool show, long style)
1018 {
1019 if ( show == IsFullScreen() )
1020 {
1021 // nothing to do
1022 return true;
1023 }
1024
1025 m_fsIsShowing = show;
1026
1027 if ( show )
1028 {
1029 m_fsStyle = style;
1030
1031 // zap the frame borders
1032
1033 // save the 'normal' window style
1034 m_fsOldWindowStyle = GetWindowLong(GetHwnd(), GWL_STYLE);
1035
1036 // save the old position, width & height, maximize state
1037 m_fsOldSize = GetRect();
1038 m_fsIsMaximized = IsMaximized();
1039
1040 // decide which window style flags to turn off
1041 LONG newStyle = m_fsOldWindowStyle;
1042 LONG offFlags = 0;
1043
1044 if (style & wxFULLSCREEN_NOBORDER)
1045 {
1046 offFlags |= WS_BORDER;
1047 #ifndef __WXWINCE__
1048 offFlags |= WS_THICKFRAME;
1049 #endif
1050 }
1051 if (style & wxFULLSCREEN_NOCAPTION)
1052 offFlags |= WS_CAPTION | WS_SYSMENU;
1053
1054 newStyle &= ~offFlags;
1055
1056 // change our window style to be compatible with full-screen mode
1057 ::SetWindowLong(GetHwnd(), GWL_STYLE, newStyle);
1058
1059 wxRect rect;
1060 #if wxUSE_DISPLAY
1061 // resize to the size of the display containing us
1062 int dpy = wxDisplay::GetFromWindow(this);
1063 if ( dpy != wxNOT_FOUND )
1064 {
1065 rect = wxDisplay(dpy).GetGeometry();
1066 }
1067 else // fall back to the main desktop
1068 #endif // wxUSE_DISPLAY
1069 {
1070 // resize to the size of the desktop
1071 wxCopyRECTToRect(wxGetWindowRect(::GetDesktopWindow()), rect);
1072 #ifdef __WXWINCE__
1073 // FIXME: size of the bottom menu (toolbar)
1074 // should be taken in account
1075 rect.height += rect.y;
1076 rect.y = 0;
1077 #endif
1078 }
1079
1080 SetSize(rect);
1081
1082 // now flush the window style cache and actually go full-screen
1083 long flags = SWP_FRAMECHANGED;
1084
1085 // showing the frame full screen should also show it if it's still
1086 // hidden
1087 if ( !IsShown() )
1088 {
1089 // don't call wxWindow version to avoid flicker from calling
1090 // ::ShowWindow() -- we're going to show the window at the correct
1091 // location directly below -- but do call the wxWindowBase version
1092 // to sync the internal m_isShown flag
1093 wxWindowBase::Show();
1094
1095 flags |= SWP_SHOWWINDOW;
1096 }
1097
1098 SetWindowPos(GetHwnd(), HWND_TOP,
1099 rect.x, rect.y, rect.width, rect.height,
1100 flags);
1101
1102 #if !defined(__HANDHELDPC__) && (defined(__WXWINCE__) && (_WIN32_WCE < 400))
1103 ::SHFullScreen(GetHwnd(), SHFS_HIDETASKBAR | SHFS_HIDESIPBUTTON);
1104 #endif
1105
1106 // finally send an event allowing the window to relayout itself &c
1107 wxSizeEvent event(rect.GetSize(), GetId());
1108 event.SetEventObject(this);
1109 HandleWindowEvent(event);
1110 }
1111 else // stop showing full screen
1112 {
1113 #if !defined(__HANDHELDPC__) && (defined(__WXWINCE__) && (_WIN32_WCE < 400))
1114 ::SHFullScreen(GetHwnd(), SHFS_SHOWTASKBAR | SHFS_SHOWSIPBUTTON);
1115 #endif
1116 Maximize(m_fsIsMaximized);
1117 SetWindowLong(GetHwnd(),GWL_STYLE, m_fsOldWindowStyle);
1118 SetWindowPos(GetHwnd(),HWND_TOP,m_fsOldSize.x, m_fsOldSize.y,
1119 m_fsOldSize.width, m_fsOldSize.height, SWP_FRAMECHANGED);
1120 }
1121
1122 return true;
1123 }
1124
1125 // ----------------------------------------------------------------------------
1126 // wxTopLevelWindowMSW misc
1127 // ----------------------------------------------------------------------------
1128
1129 void wxTopLevelWindowMSW::SetTitle( const wxString& title)
1130 {
1131 SetLabel(title);
1132 }
1133
1134 wxString wxTopLevelWindowMSW::GetTitle() const
1135 {
1136 return GetLabel();
1137 }
1138
1139 bool wxTopLevelWindowMSW::DoSelectAndSetIcon(const wxIconBundle& icons,
1140 int smX,
1141 int smY,
1142 int i)
1143 {
1144 const wxSize size(::GetSystemMetrics(smX), ::GetSystemMetrics(smY));
1145
1146 wxIcon icon = icons.GetIcon(size, wxIconBundle::FALLBACK_NEAREST_LARGER);
1147
1148 if ( !icon.IsOk() )
1149 return false;
1150
1151 ::SendMessage(GetHwnd(), WM_SETICON, i, (LPARAM)GetHiconOf(icon));
1152 return true;
1153 }
1154
1155 void wxTopLevelWindowMSW::SetIcons(const wxIconBundle& icons)
1156 {
1157 wxTopLevelWindowBase::SetIcons(icons);
1158
1159 if ( icons.IsEmpty() )
1160 {
1161 // FIXME: SetIcons(wxNullIconBundle) should unset existing icons,
1162 // but we currently don't do that
1163 wxASSERT_MSG( m_icons.IsEmpty(), "unsetting icons doesn't work" );
1164 return;
1165 }
1166
1167 DoSelectAndSetIcon(icons, SM_CXSMICON, SM_CYSMICON, ICON_SMALL);
1168 DoSelectAndSetIcon(icons, SM_CXICON, SM_CYICON, ICON_BIG);
1169 }
1170
1171 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable)
1172 {
1173 #if !defined(__WXMICROWIN__)
1174 // get system (a.k.a. window) menu
1175 HMENU hmenu = GetSystemMenu(GetHwnd(), FALSE /* get it */);
1176 if ( !hmenu )
1177 {
1178 // no system menu at all -- ok if we want to remove the close button
1179 // anyhow, but bad if we want to show it
1180 return !enable;
1181 }
1182
1183 // enabling/disabling the close item from it also automatically
1184 // disables/enables the close title bar button
1185 if ( ::EnableMenuItem(hmenu, SC_CLOSE,
1186 MF_BYCOMMAND |
1187 (enable ? MF_ENABLED : MF_GRAYED)) == -1 )
1188 {
1189 wxLogLastError(wxT("EnableMenuItem(SC_CLOSE)"));
1190
1191 return false;
1192 }
1193 #ifndef __WXWINCE__
1194 // update appearance immediately
1195 if ( !::DrawMenuBar(GetHwnd()) )
1196 {
1197 wxLogLastError(wxT("DrawMenuBar"));
1198 }
1199 #endif
1200 #endif // !__WXMICROWIN__
1201
1202 return true;
1203 }
1204
1205 void wxTopLevelWindowMSW::RequestUserAttention(int flags)
1206 {
1207 // check if we can use FlashWindowEx(): unfortunately a simple test for
1208 // FLASHW_STOP doesn't work because MSVC6 headers do #define it but don't
1209 // provide FlashWindowEx() declaration, so try to detect whether we have
1210 // real headers for WINVER 0x0500 by checking for existence of a symbol not
1211 // declated in MSVC6 header
1212 #if defined(FLASHW_STOP) && defined(VK_XBUTTON1) && wxUSE_DYNLIB_CLASS
1213 // available in the headers, check if it is supported by the system
1214 typedef BOOL (WINAPI *FlashWindowEx_t)(FLASHWINFO *pfwi);
1215 static FlashWindowEx_t s_pfnFlashWindowEx = NULL;
1216 if ( !s_pfnFlashWindowEx )
1217 {
1218 wxDynamicLibrary dllUser32(wxT("user32.dll"));
1219 s_pfnFlashWindowEx = (FlashWindowEx_t)
1220 dllUser32.GetSymbol(wxT("FlashWindowEx"));
1221
1222 // we can safely unload user32.dll here, it's going to remain loaded as
1223 // long as the program is running anyhow
1224 }
1225
1226 if ( s_pfnFlashWindowEx )
1227 {
1228 WinStruct<FLASHWINFO> fwi;
1229 fwi.hwnd = GetHwnd();
1230 fwi.dwFlags = FLASHW_ALL;
1231 if ( flags & wxUSER_ATTENTION_INFO )
1232 {
1233 // just flash a few times
1234 fwi.uCount = 3;
1235 }
1236 else // wxUSER_ATTENTION_ERROR
1237 {
1238 // flash until the user notices it
1239 fwi.dwFlags |= FLASHW_TIMERNOFG;
1240 }
1241
1242 s_pfnFlashWindowEx(&fwi);
1243 }
1244 else // FlashWindowEx() not available
1245 #endif // FlashWindowEx() defined
1246 {
1247 wxUnusedVar(flags);
1248 #ifndef __WXWINCE__
1249 ::FlashWindow(GetHwnd(), TRUE);
1250 #endif // __WXWINCE__
1251 }
1252 }
1253
1254 wxMenu *wxTopLevelWindowMSW::MSWGetSystemMenu() const
1255 {
1256 #ifndef __WXUNIVERSAL__
1257 if ( !m_menuSystem )
1258 {
1259 HMENU hmenu = ::GetSystemMenu(GetHwnd(), FALSE);
1260 if ( !hmenu )
1261 {
1262 wxLogLastError(wxT("GetSystemMenu()"));
1263 return NULL;
1264 }
1265
1266 wxTopLevelWindowMSW * const
1267 self = const_cast<wxTopLevelWindowMSW *>(this);
1268
1269 self->m_menuSystem = wxMenu::MSWNewFromHMENU(hmenu);
1270
1271 // We need to somehow associate this menu with this window to ensure
1272 // that we get events from it. A natural idea would be to pretend that
1273 // it's attached to our menu bar but this wouldn't work if we don't
1274 // have any menu bar which is a common case for applications using
1275 // custom items in the system menu (they mostly do it exactly because
1276 // they don't have any other menus).
1277 //
1278 // So reuse the invoking window pointer instead, this is not exactly
1279 // correct but doesn't seem to have any serious drawbacks.
1280 m_menuSystem->SetInvokingWindow(self);
1281 }
1282 #endif // #ifndef __WXUNIVERSAL__
1283
1284 return m_menuSystem;
1285 }
1286
1287 // ----------------------------------------------------------------------------
1288 // Transparency support
1289 // ---------------------------------------------------------------------------
1290
1291 bool wxTopLevelWindowMSW::SetTransparent(wxByte alpha)
1292 {
1293 #if wxUSE_DYNLIB_CLASS
1294 typedef DWORD (WINAPI *PSETLAYEREDWINDOWATTR)(HWND, DWORD, BYTE, DWORD);
1295 static PSETLAYEREDWINDOWATTR
1296 pSetLayeredWindowAttributes = (PSETLAYEREDWINDOWATTR)-1;
1297
1298 if ( pSetLayeredWindowAttributes == (PSETLAYEREDWINDOWATTR)-1 )
1299 {
1300 wxDynamicLibrary dllUser32(wxT("user32.dll"));
1301
1302 // use RawGetSymbol() and not GetSymbol() to avoid error messages under
1303 // Windows 95: there is nothing the user can do about this anyhow
1304 pSetLayeredWindowAttributes = (PSETLAYEREDWINDOWATTR)
1305 dllUser32.RawGetSymbol(wxT("SetLayeredWindowAttributes"));
1306
1307 // it's ok to destroy dllUser32 here, we link statically to user32.dll
1308 // anyhow so it won't be unloaded
1309 }
1310
1311 if ( !pSetLayeredWindowAttributes )
1312 return false;
1313 #endif // wxUSE_DYNLIB_CLASS
1314
1315 LONG exstyle = GetWindowLong(GetHwnd(), GWL_EXSTYLE);
1316
1317 // if setting alpha to fully opaque then turn off the layered style
1318 if (alpha == 255)
1319 {
1320 SetWindowLong(GetHwnd(), GWL_EXSTYLE, exstyle & ~WS_EX_LAYERED);
1321 Refresh();
1322 return true;
1323 }
1324
1325 #if wxUSE_DYNLIB_CLASS
1326 // Otherwise, set the layered style if needed and set the alpha value
1327 if ((exstyle & WS_EX_LAYERED) == 0 )
1328 SetWindowLong(GetHwnd(), GWL_EXSTYLE, exstyle | WS_EX_LAYERED);
1329
1330 if ( pSetLayeredWindowAttributes(GetHwnd(), 0, (BYTE)alpha, LWA_ALPHA) )
1331 return true;
1332 #endif // wxUSE_DYNLIB_CLASS
1333
1334 return false;
1335 }
1336
1337 bool wxTopLevelWindowMSW::CanSetTransparent()
1338 {
1339 // The API is available on win2k and above
1340
1341 static int os_type = -1;
1342 static int ver_major = -1;
1343
1344 if (os_type == -1)
1345 os_type = ::wxGetOsVersion(&ver_major);
1346
1347 return (os_type == wxOS_WINDOWS_NT && ver_major >= 5);
1348 }
1349
1350 void wxTopLevelWindowMSW::DoFreeze()
1351 {
1352 // do nothing: freezing toplevel window causes paint and mouse events
1353 // to go through it any TLWs under it, so the best we can do is to freeze
1354 // all children -- and wxWindowBase::Freeze() does that
1355 }
1356
1357 void wxTopLevelWindowMSW::DoThaw()
1358 {
1359 // intentionally empty -- see DoFreeze()
1360 }
1361
1362
1363 // ----------------------------------------------------------------------------
1364 // wxTopLevelWindow event handling
1365 // ----------------------------------------------------------------------------
1366
1367 void wxTopLevelWindowMSW::DoSaveLastFocus()
1368 {
1369 if ( m_iconized )
1370 return;
1371
1372 // remember the last focused child if it is our child
1373 m_winLastFocused = FindFocus();
1374
1375 if ( m_winLastFocused )
1376 {
1377 // and don't remember it if it's a child from some other frame
1378 if ( wxGetTopLevelParent(m_winLastFocused) != this )
1379 {
1380 m_winLastFocused = NULL;
1381 }
1382 }
1383 }
1384
1385 void wxTopLevelWindowMSW::DoRestoreLastFocus()
1386 {
1387 wxWindow *parent = m_winLastFocused ? m_winLastFocused->GetParent()
1388 : NULL;
1389 if ( !parent )
1390 {
1391 parent = this;
1392 }
1393
1394 wxSetFocusToChild(parent, &m_winLastFocused);
1395 }
1396
1397 void wxTopLevelWindowMSW::OnActivate(wxActivateEvent& event)
1398 {
1399 if ( event.GetActive() )
1400 {
1401 // We get WM_ACTIVATE before being restored from iconized state, so we
1402 // can be still iconized here. In this case, avoid restoring the focus
1403 // as it doesn't work anyhow and we will do when we're really restored.
1404 if ( m_iconized )
1405 {
1406 event.Skip();
1407 return;
1408 }
1409
1410 // restore focus to the child which was last focused unless we already
1411 // have it
1412 wxLogTrace(wxT("focus"), wxT("wxTLW %p activated."), m_hWnd);
1413
1414 wxWindow *winFocus = FindFocus();
1415 if ( !winFocus || wxGetTopLevelParent(winFocus) != this )
1416 DoRestoreLastFocus();
1417 }
1418 else // deactivating
1419 {
1420 DoSaveLastFocus();
1421
1422 wxLogTrace(wxT("focus"),
1423 wxT("wxTLW %p deactivated, last focused: %p."),
1424 m_hWnd,
1425 m_winLastFocused ? GetHwndOf(m_winLastFocused) : NULL);
1426
1427 event.Skip();
1428 }
1429 }
1430
1431 // the DialogProc for all wxWidgets dialogs
1432 LONG APIENTRY _EXPORT
1433 wxDlgProc(HWND hDlg,
1434 UINT message,
1435 WPARAM WXUNUSED(wParam),
1436 LPARAM WXUNUSED(lParam))
1437 {
1438 switch ( message )
1439 {
1440 case WM_INITDIALOG:
1441 {
1442 // under CE, add a "Ok" button in the dialog title bar and make it full
1443 // screen
1444 //
1445 // TODO: find the window for this HWND, and take into account
1446 // wxMAXIMIZE and wxCLOSE_BOX. For now, assume both are present.
1447 //
1448 // Standard SDK doesn't have aygshell.dll: see
1449 // include/wx/msw/wince/libraries.h
1450 #if defined(__WXWINCE__) && !defined(__WINCE_STANDARDSDK__) && !defined(__HANDHELDPC__)
1451 SHINITDLGINFO shidi;
1452 shidi.dwMask = SHIDIM_FLAGS;
1453 shidi.dwFlags = SHIDIF_SIZEDLG // take account of the SIP or menubar
1454 #ifndef __SMARTPHONE__
1455 | SHIDIF_DONEBUTTON
1456 #endif
1457 ;
1458 shidi.hDlg = hDlg;
1459 SHInitDialog( &shidi );
1460 #else // no SHInitDialog()
1461 wxUnusedVar(hDlg);
1462 #endif
1463 // for WM_INITDIALOG, returning TRUE tells system to set focus to
1464 // the first control in the dialog box, but as we set the focus
1465 // ourselves, we return FALSE for it as well
1466 return FALSE;
1467 }
1468 }
1469
1470 // for almost all messages, returning FALSE means that we didn't process
1471 // the message
1472 return FALSE;
1473 }
1474
1475 // ============================================================================
1476 // wxTLWHiddenParentModule implementation
1477 // ============================================================================
1478
1479 HWND wxTLWHiddenParentModule::ms_hwnd = NULL;
1480
1481 const wxChar *wxTLWHiddenParentModule::ms_className = NULL;
1482
1483 bool wxTLWHiddenParentModule::OnInit()
1484 {
1485 ms_hwnd = NULL;
1486 ms_className = NULL;
1487
1488 return true;
1489 }
1490
1491 void wxTLWHiddenParentModule::OnExit()
1492 {
1493 if ( ms_hwnd )
1494 {
1495 if ( !::DestroyWindow(ms_hwnd) )
1496 {
1497 wxLogLastError(wxT("DestroyWindow(hidden TLW parent)"));
1498 }
1499
1500 ms_hwnd = NULL;
1501 }
1502
1503 if ( ms_className )
1504 {
1505 if ( !::UnregisterClass(ms_className, wxGetInstance()) )
1506 {
1507 wxLogLastError(wxT("UnregisterClass(\"wxTLWHiddenParent\")"));
1508 }
1509
1510 ms_className = NULL;
1511 }
1512 }
1513
1514 /* static */
1515 HWND wxTLWHiddenParentModule::GetHWND()
1516 {
1517 if ( !ms_hwnd )
1518 {
1519 if ( !ms_className )
1520 {
1521 static const wxChar *HIDDEN_PARENT_CLASS = wxT("wxTLWHiddenParent");
1522
1523 WNDCLASS wndclass;
1524 wxZeroMemory(wndclass);
1525
1526 wndclass.lpfnWndProc = DefWindowProc;
1527 wndclass.hInstance = wxGetInstance();
1528 wndclass.lpszClassName = HIDDEN_PARENT_CLASS;
1529
1530 if ( !::RegisterClass(&wndclass) )
1531 {
1532 wxLogLastError(wxT("RegisterClass(\"wxTLWHiddenParent\")"));
1533 }
1534 else
1535 {
1536 ms_className = HIDDEN_PARENT_CLASS;
1537 }
1538 }
1539
1540 ms_hwnd = ::CreateWindow(ms_className, wxEmptyString, 0, 0, 0, 0, 0, NULL,
1541 (HMENU)NULL, wxGetInstance(), NULL);
1542 if ( !ms_hwnd )
1543 {
1544 wxLogLastError(wxT("CreateWindow(hidden TLW parent)"));
1545 }
1546 }
1547
1548 return ms_hwnd;
1549 }