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