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