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