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