]> git.saurik.com Git - wxWidgets.git/blob - src/msw/toplevel.cpp
51972423ad0f4e1328f6ed190371f0bf42d202ae
[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 // Keep the #ifdef block inside the case to fix a potential MSVC
365 // warning regarding switch statement containing no case or
366 // default labels (or a default only).
367 #ifndef __WXUNIVERSAL__
368 // We may need to generate events for the items added to the system
369 // menu if it had been created (and presumably modified).
370 if ( m_menuSystem )
371 {
372 // From MSDN:
373 //
374 // ... the four low-order bits of the wParam parameter are
375 // used internally by the system. To obtain the correct
376 // result when testing the value of wParam, an application
377 // must combine the value 0xFFF0 with the wParam value by
378 // using the bitwise AND operator.
379 unsigned id = wParam & 0xfff0;
380
381 // SC_SIZE is the first of the system-defined commands and we
382 // leave those to DefWindowProc().
383 if ( id < SC_SIZE )
384 {
385 if ( m_menuSystem->MSWCommand(0 /* unused anyhow */, id) )
386 processed = true;
387 }
388 }
389 #endif // #ifndef __WXUNIVERSAL__
390 break;
391 }
392
393 if ( !processed )
394 rc = wxTopLevelWindowBase::MSWWindowProc(message, wParam, lParam);
395
396 return rc;
397 }
398
399 bool wxTopLevelWindowMSW::CreateDialog(const void *dlgTemplate,
400 const wxString& title,
401 const wxPoint& pos,
402 const wxSize& size)
403 {
404 #ifdef __WXMICROWIN__
405 // no dialogs support under MicroWin yet
406 return CreateFrame(title, pos, size);
407 #else // !__WXMICROWIN__
408 // static cast is valid as we're only ever called for dialogs
409 wxWindow * const
410 parent = static_cast<wxDialog *>(this)->GetParentForModalDialog();
411
412 m_hWnd = (WXHWND)::CreateDialogIndirect
413 (
414 wxGetInstance(),
415 (DLGTEMPLATE*)dlgTemplate,
416 parent ? GetHwndOf(parent) : NULL,
417 (DLGPROC)wxDlgProc
418 );
419
420 if ( !m_hWnd )
421 {
422 wxFAIL_MSG(wxT("Failed to create dialog. Incorrect DLGTEMPLATE?"));
423
424 wxLogSysError(wxT("Can't create dialog using memory template"));
425
426 return false;
427 }
428
429 #if !defined(__WXWINCE__)
430 // For some reason, the system menu is activated when we use the
431 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
432 if ( HasExtraStyle(wxWS_EX_CONTEXTHELP) )
433 {
434 wxFrame *winTop = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
435 if ( winTop )
436 {
437 wxIcon icon = winTop->GetIcon();
438 if ( icon.IsOk() )
439 {
440 ::SendMessage(GetHwnd(), WM_SETICON,
441 (WPARAM)TRUE,
442 (LPARAM)GetHiconOf(icon));
443 }
444 }
445 }
446 #endif // !__WXWINCE__
447
448 if ( !title.empty() )
449 {
450 ::SetWindowText(GetHwnd(), title.wx_str());
451 }
452
453 SubclassWin(m_hWnd);
454
455 #if !defined(__WXWINCE__) || defined(__WINCE_STANDARDSDK__)
456 // move the dialog to its initial position without forcing repainting
457 int x, y, w, h;
458 (void)MSWGetCreateWindowCoords(pos, size, x, y, w, h);
459
460 if ( x == (int)CW_USEDEFAULT )
461 {
462 // Let the system position the window, just set its size.
463 ::SetWindowPos(GetHwnd(), 0,
464 0, 0, w, h,
465 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
466 }
467 else // Move the window to the desired location and set its size too.
468 {
469 if ( !::MoveWindow(GetHwnd(), x, y, w, h, FALSE) )
470 {
471 wxLogLastError(wxT("MoveWindow"));
472 }
473 }
474 #endif // !__WXWINCE__
475
476 #ifdef __SMARTPHONE__
477 // Work around title non-display glitch
478 Show(false);
479 #endif
480
481 return true;
482 #endif // __WXMICROWIN__/!__WXMICROWIN__
483 }
484
485 bool wxTopLevelWindowMSW::CreateFrame(const wxString& title,
486 const wxPoint& pos,
487 const wxSize& size)
488 {
489 WXDWORD exflags;
490 WXDWORD flags = MSWGetCreateWindowFlags(&exflags);
491
492 const wxSize sz = IsAlwaysMaximized() ? wxDefaultSize : size;
493
494 #ifndef __WXWINCE__
495 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
496 exflags |= WS_EX_LAYOUTRTL;
497 #endif
498
499 return MSWCreate(MSWGetRegisteredClassName(),
500 title.wx_str(), pos, sz, flags, exflags);
501 }
502
503 bool wxTopLevelWindowMSW::Create(wxWindow *parent,
504 wxWindowID id,
505 const wxString& title,
506 const wxPoint& pos,
507 const wxSize& size,
508 long style,
509 const wxString& name)
510 {
511 wxSize sizeReal = size;
512 if ( !sizeReal.IsFullySpecified() )
513 {
514 sizeReal.SetDefaults(GetDefaultSize());
515 }
516
517 // notice that we should append this window to wxTopLevelWindows list
518 // before calling CreateBase() as it behaves differently for TLW and
519 // non-TLW windows
520 wxTopLevelWindows.Append(this);
521
522 bool ret = CreateBase(parent, id, pos, sizeReal, style, name);
523 if ( !ret )
524 return false;
525
526 if ( parent )
527 parent->AddChild(this);
528
529 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
530 {
531 // we have different dialog templates to allows creation of dialogs
532 // with & without captions under MSWindows, resizable or not (but a
533 // resizable dialog always has caption - otherwise it would look too
534 // strange)
535
536 // we need 3 additional WORDs for dialog menu, class and title (as we
537 // don't use DS_SETFONT we don't need the fourth WORD for the font)
538 static const int dlgsize = sizeof(DLGTEMPLATE) + (sizeof(WORD) * 3);
539 DLGTEMPLATE *dlgTemplate = (DLGTEMPLATE *)malloc(dlgsize);
540 memset(dlgTemplate, 0, dlgsize);
541
542 // these values are arbitrary, they won't be used normally anyhow
543 dlgTemplate->x = 34;
544 dlgTemplate->y = 22;
545 dlgTemplate->cx = 144;
546 dlgTemplate->cy = 75;
547
548 // reuse the code in MSWGetStyle() but correct the results slightly for
549 // the dialog
550 //
551 // NB: we need a temporary variable as we can't pass pointer to
552 // dwExtendedStyle directly, it's not aligned correctly for 64 bit
553 // architectures
554 WXDWORD dwExtendedStyle;
555 dlgTemplate->style = MSWGetStyle(style, &dwExtendedStyle);
556 dlgTemplate->dwExtendedStyle = dwExtendedStyle;
557
558 // all dialogs are popups
559 dlgTemplate->style |= WS_POPUP;
560
561 #ifndef __WXWINCE__
562 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
563 {
564 dlgTemplate->dwExtendedStyle |= WS_EX_LAYOUTRTL;
565 }
566
567 // force 3D-look if necessary, it looks impossibly ugly otherwise
568 if ( style & (wxRESIZE_BORDER | wxCAPTION) )
569 dlgTemplate->style |= DS_MODALFRAME;
570 #endif
571
572 ret = CreateDialog(dlgTemplate, title, pos, sizeReal);
573 free(dlgTemplate);
574 }
575 else // !dialog
576 {
577 ret = CreateFrame(title, pos, sizeReal);
578 }
579
580 #ifndef __WXWINCE__
581 if ( ret && !(GetWindowStyleFlag() & wxCLOSE_BOX) )
582 {
583 EnableCloseButton(false);
584 }
585 #endif
586
587 // for standard dialogs the dialog manager generates WM_CHANGEUISTATE
588 // itself but for custom windows we have to do it ourselves in order to
589 // make the keyboard indicators (such as underlines for accelerators and
590 // focus rectangles) work under Win2k+
591 if ( ret )
592 {
593 MSWUpdateUIState(UIS_INITIALIZE);
594 }
595
596 // Note: if we include PocketPC in this test, dialogs can fail to show up,
597 // for example the text entry dialog in the dialogs sample. Problem with Maximise()?
598 #if defined(__WXWINCE__) && (defined(__SMARTPHONE__) || defined(__WINCE_STANDARDSDK__))
599 if ( ( style & wxMAXIMIZE ) || IsAlwaysMaximized() )
600 {
601 this->Maximize();
602 }
603 #endif
604
605 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
606 SetRightMenu(); // to nothing for initialization
607 #endif
608
609 return ret;
610 }
611
612 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
613 {
614 delete m_menuSystem;
615
616 SendDestroyEvent();
617
618 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
619 SHACTIVATEINFO* info = (SHACTIVATEINFO*) m_activateInfo;
620 delete info;
621 m_activateInfo = NULL;
622 #endif
623
624 // after destroying an owned window, Windows activates the next top level
625 // window in Z order but it may be different from our owner (to reproduce
626 // this simply Alt-TAB to another application and back before closing the
627 // owned frame) whereas we always want to yield activation to our parent
628 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT) )
629 {
630 wxWindow *parent = GetParent();
631 if ( parent )
632 {
633 ::BringWindowToTop(GetHwndOf(parent));
634 }
635 }
636 }
637
638 // ----------------------------------------------------------------------------
639 // wxTopLevelWindowMSW showing
640 // ----------------------------------------------------------------------------
641
642 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd)
643 {
644 ::ShowWindow(GetHwnd(), nShowCmd);
645
646 // Hiding the window doesn't change its iconized state.
647 if ( nShowCmd != SW_HIDE )
648 {
649 // Otherwise restoring, maximizing or showing the window normally also
650 // makes it not iconized and only minimizing it does make it iconized.
651 m_iconized = nShowCmd == SW_MINIMIZE;
652 }
653 }
654
655 void wxTopLevelWindowMSW::ShowWithoutActivating()
656 {
657 if ( !wxWindowBase::Show(true) )
658 return;
659
660 DoShowWindow(SW_SHOWNA);
661 }
662
663 bool wxTopLevelWindowMSW::Show(bool show)
664 {
665 // don't use wxWindow version as we want to call DoShowWindow() ourselves
666 if ( !wxWindowBase::Show(show) )
667 return false;
668
669 int nShowCmd;
670 if ( show )
671 {
672 if ( m_maximizeOnShow )
673 {
674 // show and maximize
675 nShowCmd = SW_MAXIMIZE;
676
677 // This is necessary, or no window appears
678 #if defined( __WINCE_STANDARDSDK__) || defined(__SMARTPHONE__)
679 DoShowWindow(SW_SHOW);
680 #endif
681
682 m_maximizeOnShow = false;
683 }
684 else if ( m_iconized )
685 {
686 // iconize and show
687 nShowCmd = SW_MINIMIZE;
688 }
689 else // just show
690 {
691 // we shouldn't use SW_SHOW which also activates the window for
692 // tool frames (as they shouldn't steal focus from the main window)
693 // nor for the currently disabled windows as they would be enabled
694 // as a side effect
695 if ( HasFlag(wxFRAME_TOOL_WINDOW) || !IsEnabled() )
696 nShowCmd = SW_SHOWNA;
697 else
698 nShowCmd = SW_SHOW;
699 }
700 }
701 else // hide
702 {
703 nShowCmd = SW_HIDE;
704 }
705
706 #if wxUSE_DEFERRED_SIZING
707 // we only set pending size if we're maximized before being shown, now that
708 // we're shown we don't need it any more (it is reset in size event handler
709 // for child windows but we have to do it ourselves for this parent window)
710 //
711 // make sure to reset it before actually showing the window as this will
712 // generate WM_SIZE events and we want to use the correct client size from
713 // them, not the size returned by WM_NCCALCSIZE in DoGetClientSize() which
714 // turns out to be wrong for maximized windows (see #11762)
715 m_pendingSize = wxDefaultSize;
716 #endif // wxUSE_DEFERRED_SIZING
717
718 DoShowWindow(nShowCmd);
719
720 #if defined(__WXWINCE__) && (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__))
721 // Addornments have to be added when the frame is the correct size
722 wxFrame* frame = wxDynamicCast(this, wxFrame);
723 if (frame && frame->GetMenuBar())
724 frame->GetMenuBar()->AddAdornments(GetWindowStyleFlag());
725 #endif
726
727 return true;
728 }
729
730 void wxTopLevelWindowMSW::Raise()
731 {
732 ::SetForegroundWindow(GetHwnd());
733 }
734
735 // ----------------------------------------------------------------------------
736 // wxTopLevelWindowMSW maximize/minimize
737 // ----------------------------------------------------------------------------
738
739 void wxTopLevelWindowMSW::Maximize(bool maximize)
740 {
741 if ( IsShown() )
742 {
743 // just maximize it directly
744 DoShowWindow(maximize ? SW_MAXIMIZE : SW_RESTORE);
745 }
746 else // hidden
747 {
748 // we can't maximize the hidden frame because it shows it as well,
749 // so just remember that we should do it later in this case
750 m_maximizeOnShow = maximize;
751
752 #if wxUSE_DEFERRED_SIZING
753 // after calling Maximize() the client code expects to get the frame
754 // "real" size and doesn't want to know that, because of implementation
755 // details, the frame isn't really maximized yet but will be only once
756 // it's shown, so return our size as it will be then in this case
757 if ( maximize )
758 {
759 // we must only change pending size here, and not call SetSize()
760 // because otherwise Windows would think that this (full screen)
761 // size is the natural size for the frame and so would use it when
762 // the user clicks on "restore" title bar button instead of the
763 // correct initial frame size
764 //
765 // NB: unfortunately we don't know which display we're on yet so we
766 // have to use the default one
767 m_pendingSize = wxGetClientDisplayRect().GetSize();
768 }
769 //else: can't do anything in this case, we don't have the old size
770 #endif // wxUSE_DEFERRED_SIZING
771 }
772 }
773
774 bool wxTopLevelWindowMSW::IsMaximized() const
775 {
776 return IsAlwaysMaximized() ||
777 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__) && !defined(__WINCE_STANDARDSDK__)
778
779 (::IsZoomed(GetHwnd()) != 0) ||
780 #endif
781 m_maximizeOnShow;
782 }
783
784 void wxTopLevelWindowMSW::Iconize(bool iconize)
785 {
786 if ( iconize == m_iconized )
787 {
788 // Do nothing, in particular don't restore non-iconized windows when
789 // Iconize(false) is called as this would wrongly un-maximize them.
790 return;
791 }
792
793 if ( IsShown() )
794 {
795 // change the window state immediately
796 DoShowWindow(iconize ? SW_MINIMIZE : SW_RESTORE);
797 }
798 else // hidden
799 {
800 // iconizing the window shouldn't show it so just remember that we need
801 // to become iconized when shown later
802 m_iconized = true;
803 }
804 }
805
806 bool wxTopLevelWindowMSW::IsIconized() const
807 {
808 #ifdef __WXWINCE__
809 return false;
810 #else
811 if ( !IsShown() )
812 return m_iconized;
813
814 // don't use m_iconized, it may be briefly out of sync with the real state
815 // as it's only modified when we receive a WM_SIZE and we could be called
816 // from an event handler from one of the messages we receive before it,
817 // such as WM_MOVE
818 return ::IsIconic(GetHwnd()) != 0;
819 #endif
820 }
821
822 void wxTopLevelWindowMSW::Restore()
823 {
824 DoShowWindow(SW_RESTORE);
825 }
826
827 void wxTopLevelWindowMSW::SetLayoutDirection(wxLayoutDirection dir)
828 {
829 if ( dir == wxLayout_Default )
830 dir = wxTheApp->GetLayoutDirection();
831
832 if ( dir != wxLayout_Default )
833 wxTopLevelWindowBase::SetLayoutDirection(dir);
834 }
835
836 // ----------------------------------------------------------------------------
837 // wxTopLevelWindowMSW geometry
838 // ----------------------------------------------------------------------------
839
840 #ifndef __WXWINCE__
841
842 void wxTopLevelWindowMSW::DoGetPosition(int *x, int *y) const
843 {
844 if ( IsIconized() )
845 {
846 WINDOWPLACEMENT wp;
847 wp.length = sizeof(WINDOWPLACEMENT);
848 if ( ::GetWindowPlacement(GetHwnd(), &wp) )
849 {
850 RECT& rc = wp.rcNormalPosition;
851
852 // the position returned by GetWindowPlacement() is in workspace
853 // coordinates except for windows with WS_EX_TOOLWINDOW style
854 if ( !HasFlag(wxFRAME_TOOL_WINDOW) )
855 {
856 // we must use the correct display for the translation as the
857 // task bar might be shown on one display but not the other one
858 int n = wxDisplay::GetFromWindow(this);
859 wxDisplay dpy(n == wxNOT_FOUND ? 0 : n);
860 const wxPoint ptOfs = dpy.GetClientArea().GetPosition() -
861 dpy.GetGeometry().GetPosition();
862
863 rc.left += ptOfs.x;
864 rc.top += ptOfs.y;
865 }
866
867 if ( x )
868 *x = rc.left;
869 if ( y )
870 *y = rc.top;
871
872 return;
873 }
874
875 wxLogLastError(wxT("GetWindowPlacement"));
876 }
877 //else: normal case
878
879 wxTopLevelWindowBase::DoGetPosition(x, y);
880 }
881
882 void wxTopLevelWindowMSW::DoGetSize(int *width, int *height) const
883 {
884 if ( IsIconized() )
885 {
886 WINDOWPLACEMENT wp;
887 wp.length = sizeof(WINDOWPLACEMENT);
888 if ( ::GetWindowPlacement(GetHwnd(), &wp) )
889 {
890 const RECT& rc = wp.rcNormalPosition;
891
892 if ( width )
893 *width = rc.right - rc.left;
894 if ( height )
895 *height = rc.bottom - rc.top;
896
897 return;
898 }
899
900 wxLogLastError(wxT("GetWindowPlacement"));
901 }
902 //else: normal case
903
904 wxTopLevelWindowBase::DoGetSize(width, height);
905 }
906
907 #endif // __WXWINCE__
908
909 void
910 wxTopLevelWindowMSW::MSWGetCreateWindowCoords(const wxPoint& pos,
911 const wxSize& size,
912 int& x, int& y,
913 int& w, int& h) const
914 {
915 // let the system position the window if no explicit position was specified
916 if ( pos.x == wxDefaultCoord )
917 {
918 // if x is set to CW_USEDEFAULT, y parameter is ignored anyhow so we
919 // can just as well set it to CW_USEDEFAULT as well
920 x =
921 y = CW_USEDEFAULT;
922 }
923 else
924 {
925 // OTOH, if x is not set to CW_USEDEFAULT, y shouldn't be set to it
926 // neither because it is not handled as a special value by Windows then
927 // and so we have to choose some default value for it, even if a
928 // completely arbitrary one
929 static const int DEFAULT_Y = 200;
930
931 x = pos.x;
932 y = pos.y == wxDefaultCoord ? DEFAULT_Y : pos.y;
933 }
934
935 if ( size.x == wxDefaultCoord || size.y == wxDefaultCoord )
936 {
937 // We don't use CW_USEDEFAULT here for several reasons:
938 //
939 // 1. It results in huge frames on modern screens (1000*800 is not
940 // uncommon on my 1280*1024 screen) which is way too big for a half
941 // empty frame of most of wxWidgets samples for example)
942 //
943 // 2. It is buggy for frames with wxFRAME_TOOL_WINDOW style for which
944 // the default is for whatever reason 8*8 which breaks client <->
945 // window size calculations (it would be nice if it didn't, but it
946 // does and the simplest way to fix it seemed to change the broken
947 // default size anyhow)
948 //
949 // 3. There is just no advantage in doing it: with x and y it is
950 // possible that [future versions of] Windows position the new top
951 // level window in some smart way which we can't do, but we can
952 // guess a reasonably good size for a new window just as well
953 // ourselves
954 //
955 // The only exception is for the Windows CE platform where the system
956 // does know better than we how should the windows be sized
957 #ifdef _WIN32_WCE
958 w =
959 h = CW_USEDEFAULT;
960 #else // !_WIN32_WCE
961 wxSize sizeReal = size;
962 sizeReal.SetDefaults(GetDefaultSize());
963
964 w = sizeReal.x;
965 h = sizeReal.y;
966 #endif // _WIN32_WCE/!_WIN32_WCE
967 }
968 else
969 {
970 w = size.x;
971 h = size.y;
972 }
973 }
974
975 // ----------------------------------------------------------------------------
976 // wxTopLevelWindowMSW fullscreen
977 // ----------------------------------------------------------------------------
978
979 bool wxTopLevelWindowMSW::ShowFullScreen(bool show, long style)
980 {
981 if ( show == IsFullScreen() )
982 {
983 // nothing to do
984 return true;
985 }
986
987 m_fsIsShowing = show;
988
989 if ( show )
990 {
991 m_fsStyle = style;
992
993 // zap the frame borders
994
995 // save the 'normal' window style
996 m_fsOldWindowStyle = GetWindowLong(GetHwnd(), GWL_STYLE);
997
998 // save the old position, width & height, maximize state
999 m_fsOldSize = GetRect();
1000 m_fsIsMaximized = IsMaximized();
1001
1002 // decide which window style flags to turn off
1003 LONG newStyle = m_fsOldWindowStyle;
1004 LONG offFlags = 0;
1005
1006 if (style & wxFULLSCREEN_NOBORDER)
1007 {
1008 offFlags |= WS_BORDER;
1009 #ifndef __WXWINCE__
1010 offFlags |= WS_THICKFRAME;
1011 #endif
1012 }
1013 if (style & wxFULLSCREEN_NOCAPTION)
1014 offFlags |= WS_CAPTION | WS_SYSMENU;
1015
1016 newStyle &= ~offFlags;
1017
1018 // change our window style to be compatible with full-screen mode
1019 ::SetWindowLong(GetHwnd(), GWL_STYLE, newStyle);
1020
1021 wxRect rect;
1022 #if wxUSE_DISPLAY
1023 // resize to the size of the display containing us
1024 int dpy = wxDisplay::GetFromWindow(this);
1025 if ( dpy != wxNOT_FOUND )
1026 {
1027 rect = wxDisplay(dpy).GetGeometry();
1028 }
1029 else // fall back to the main desktop
1030 #endif // wxUSE_DISPLAY
1031 {
1032 // resize to the size of the desktop
1033 wxCopyRECTToRect(wxGetWindowRect(::GetDesktopWindow()), rect);
1034 #ifdef __WXWINCE__
1035 // FIXME: size of the bottom menu (toolbar)
1036 // should be taken in account
1037 rect.height += rect.y;
1038 rect.y = 0;
1039 #endif
1040 }
1041
1042 SetSize(rect);
1043
1044 // now flush the window style cache and actually go full-screen
1045 long flags = SWP_FRAMECHANGED;
1046
1047 // showing the frame full screen should also show it if it's still
1048 // hidden
1049 if ( !IsShown() )
1050 {
1051 // don't call wxWindow version to avoid flicker from calling
1052 // ::ShowWindow() -- we're going to show the window at the correct
1053 // location directly below -- but do call the wxWindowBase version
1054 // to sync the internal m_isShown flag
1055 wxWindowBase::Show();
1056
1057 flags |= SWP_SHOWWINDOW;
1058 }
1059
1060 SetWindowPos(GetHwnd(), HWND_TOP,
1061 rect.x, rect.y, rect.width, rect.height,
1062 flags);
1063
1064 #if !defined(__HANDHELDPC__) && (defined(__WXWINCE__) && (_WIN32_WCE < 400))
1065 ::SHFullScreen(GetHwnd(), SHFS_HIDETASKBAR | SHFS_HIDESIPBUTTON);
1066 #endif
1067
1068 // finally send an event allowing the window to relayout itself &c
1069 wxSizeEvent event(rect.GetSize(), GetId());
1070 event.SetEventObject(this);
1071 HandleWindowEvent(event);
1072 }
1073 else // stop showing full screen
1074 {
1075 #if !defined(__HANDHELDPC__) && (defined(__WXWINCE__) && (_WIN32_WCE < 400))
1076 ::SHFullScreen(GetHwnd(), SHFS_SHOWTASKBAR | SHFS_SHOWSIPBUTTON);
1077 #endif
1078 Maximize(m_fsIsMaximized);
1079 SetWindowLong(GetHwnd(),GWL_STYLE, m_fsOldWindowStyle);
1080 SetWindowPos(GetHwnd(),HWND_TOP,m_fsOldSize.x, m_fsOldSize.y,
1081 m_fsOldSize.width, m_fsOldSize.height, SWP_FRAMECHANGED);
1082 }
1083
1084 return true;
1085 }
1086
1087 // ----------------------------------------------------------------------------
1088 // wxTopLevelWindowMSW misc
1089 // ----------------------------------------------------------------------------
1090
1091 void wxTopLevelWindowMSW::SetTitle( const wxString& title)
1092 {
1093 SetLabel(title);
1094 }
1095
1096 wxString wxTopLevelWindowMSW::GetTitle() const
1097 {
1098 return GetLabel();
1099 }
1100
1101 bool wxTopLevelWindowMSW::DoSelectAndSetIcon(const wxIconBundle& icons,
1102 int smX,
1103 int smY,
1104 int i)
1105 {
1106 const wxSize size(::GetSystemMetrics(smX), ::GetSystemMetrics(smY));
1107
1108 wxIcon icon = icons.GetIcon(size, wxIconBundle::FALLBACK_NEAREST_LARGER);
1109
1110 if ( !icon.IsOk() )
1111 return false;
1112
1113 ::SendMessage(GetHwnd(), WM_SETICON, i, (LPARAM)GetHiconOf(icon));
1114 return true;
1115 }
1116
1117 void wxTopLevelWindowMSW::SetIcons(const wxIconBundle& icons)
1118 {
1119 wxTopLevelWindowBase::SetIcons(icons);
1120
1121 if ( icons.IsEmpty() )
1122 {
1123 // FIXME: SetIcons(wxNullIconBundle) should unset existing icons,
1124 // but we currently don't do that
1125 wxASSERT_MSG( m_icons.IsEmpty(), "unsetting icons doesn't work" );
1126 return;
1127 }
1128
1129 DoSelectAndSetIcon(icons, SM_CXSMICON, SM_CYSMICON, ICON_SMALL);
1130 DoSelectAndSetIcon(icons, SM_CXICON, SM_CYICON, ICON_BIG);
1131 }
1132
1133 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable)
1134 {
1135 #if !defined(__WXMICROWIN__)
1136 // get system (a.k.a. window) menu
1137 HMENU hmenu = GetSystemMenu(GetHwnd(), FALSE /* get it */);
1138 if ( !hmenu )
1139 {
1140 // no system menu at all -- ok if we want to remove the close button
1141 // anyhow, but bad if we want to show it
1142 return !enable;
1143 }
1144
1145 // enabling/disabling the close item from it also automatically
1146 // disables/enables the close title bar button
1147 if ( ::EnableMenuItem(hmenu, SC_CLOSE,
1148 MF_BYCOMMAND |
1149 (enable ? MF_ENABLED : MF_GRAYED)) == -1 )
1150 {
1151 wxLogLastError(wxT("EnableMenuItem(SC_CLOSE)"));
1152
1153 return false;
1154 }
1155 #ifndef __WXWINCE__
1156 // update appearance immediately
1157 if ( !::DrawMenuBar(GetHwnd()) )
1158 {
1159 wxLogLastError(wxT("DrawMenuBar"));
1160 }
1161 #endif
1162 #endif // !__WXMICROWIN__
1163
1164 return true;
1165 }
1166
1167 void wxTopLevelWindowMSW::RequestUserAttention(int flags)
1168 {
1169 // check if we can use FlashWindowEx(): unfortunately a simple test for
1170 // FLASHW_STOP doesn't work because MSVC6 headers do #define it but don't
1171 // provide FlashWindowEx() declaration, so try to detect whether we have
1172 // real headers for WINVER 0x0500 by checking for existence of a symbol not
1173 // declated in MSVC6 header
1174 #if defined(FLASHW_STOP) && defined(VK_XBUTTON1) && wxUSE_DYNLIB_CLASS
1175 // available in the headers, check if it is supported by the system
1176 typedef BOOL (WINAPI *FlashWindowEx_t)(FLASHWINFO *pfwi);
1177 static FlashWindowEx_t s_pfnFlashWindowEx = NULL;
1178 if ( !s_pfnFlashWindowEx )
1179 {
1180 wxDynamicLibrary dllUser32(wxT("user32.dll"));
1181 s_pfnFlashWindowEx = (FlashWindowEx_t)
1182 dllUser32.GetSymbol(wxT("FlashWindowEx"));
1183
1184 // we can safely unload user32.dll here, it's going to remain loaded as
1185 // long as the program is running anyhow
1186 }
1187
1188 if ( s_pfnFlashWindowEx )
1189 {
1190 WinStruct<FLASHWINFO> fwi;
1191 fwi.hwnd = GetHwnd();
1192 fwi.dwFlags = FLASHW_ALL;
1193 if ( flags & wxUSER_ATTENTION_INFO )
1194 {
1195 // just flash a few times
1196 fwi.uCount = 3;
1197 }
1198 else // wxUSER_ATTENTION_ERROR
1199 {
1200 // flash until the user notices it
1201 fwi.dwFlags |= FLASHW_TIMERNOFG;
1202 }
1203
1204 s_pfnFlashWindowEx(&fwi);
1205 }
1206 else // FlashWindowEx() not available
1207 #endif // FlashWindowEx() defined
1208 {
1209 wxUnusedVar(flags);
1210 #ifndef __WXWINCE__
1211 ::FlashWindow(GetHwnd(), TRUE);
1212 #endif // __WXWINCE__
1213 }
1214 }
1215
1216 wxMenu *wxTopLevelWindowMSW::MSWGetSystemMenu() const
1217 {
1218 #ifndef __WXUNIVERSAL__
1219 if ( !m_menuSystem )
1220 {
1221 HMENU hmenu = ::GetSystemMenu(GetHwnd(), FALSE);
1222 if ( !hmenu )
1223 {
1224 wxLogLastError(wxT("GetSystemMenu()"));
1225 return NULL;
1226 }
1227
1228 wxTopLevelWindowMSW * const
1229 self = const_cast<wxTopLevelWindowMSW *>(this);
1230
1231 self->m_menuSystem = wxMenu::MSWNewFromHMENU(hmenu);
1232
1233 // We need to somehow associate this menu with this window to ensure
1234 // that we get events from it. A natural idea would be to pretend that
1235 // it's attached to our menu bar but this wouldn't work if we don't
1236 // have any menu bar which is a common case for applications using
1237 // custom items in the system menu (they mostly do it exactly because
1238 // they don't have any other menus).
1239 //
1240 // So reuse the invoking window pointer instead, this is not exactly
1241 // correct but doesn't seem to have any serious drawbacks.
1242 m_menuSystem->SetInvokingWindow(self);
1243 }
1244 #endif // #ifndef __WXUNIVERSAL__
1245
1246 return m_menuSystem;
1247 }
1248
1249 // ----------------------------------------------------------------------------
1250 // Transparency support
1251 // ---------------------------------------------------------------------------
1252
1253 bool wxTopLevelWindowMSW::SetTransparent(wxByte alpha)
1254 {
1255 #if wxUSE_DYNLIB_CLASS
1256 typedef DWORD (WINAPI *PSETLAYEREDWINDOWATTR)(HWND, DWORD, BYTE, DWORD);
1257 static PSETLAYEREDWINDOWATTR
1258 pSetLayeredWindowAttributes = (PSETLAYEREDWINDOWATTR)-1;
1259
1260 if ( pSetLayeredWindowAttributes == (PSETLAYEREDWINDOWATTR)-1 )
1261 {
1262 wxDynamicLibrary dllUser32(wxT("user32.dll"));
1263
1264 // use RawGetSymbol() and not GetSymbol() to avoid error messages under
1265 // Windows 95: there is nothing the user can do about this anyhow
1266 pSetLayeredWindowAttributes = (PSETLAYEREDWINDOWATTR)
1267 dllUser32.RawGetSymbol(wxT("SetLayeredWindowAttributes"));
1268
1269 // it's ok to destroy dllUser32 here, we link statically to user32.dll
1270 // anyhow so it won't be unloaded
1271 }
1272
1273 if ( !pSetLayeredWindowAttributes )
1274 return false;
1275 #endif // wxUSE_DYNLIB_CLASS
1276
1277 LONG exstyle = GetWindowLong(GetHwnd(), GWL_EXSTYLE);
1278
1279 // if setting alpha to fully opaque then turn off the layered style
1280 if (alpha == 255)
1281 {
1282 SetWindowLong(GetHwnd(), GWL_EXSTYLE, exstyle & ~WS_EX_LAYERED);
1283 Refresh();
1284 return true;
1285 }
1286
1287 #if wxUSE_DYNLIB_CLASS
1288 // Otherwise, set the layered style if needed and set the alpha value
1289 if ((exstyle & WS_EX_LAYERED) == 0 )
1290 SetWindowLong(GetHwnd(), GWL_EXSTYLE, exstyle | WS_EX_LAYERED);
1291
1292 if ( pSetLayeredWindowAttributes(GetHwnd(), 0, (BYTE)alpha, LWA_ALPHA) )
1293 return true;
1294 #endif // wxUSE_DYNLIB_CLASS
1295
1296 return false;
1297 }
1298
1299 bool wxTopLevelWindowMSW::CanSetTransparent()
1300 {
1301 // The API is available on win2k and above
1302
1303 static int os_type = -1;
1304 static int ver_major = -1;
1305
1306 if (os_type == -1)
1307 os_type = ::wxGetOsVersion(&ver_major);
1308
1309 return (os_type == wxOS_WINDOWS_NT && ver_major >= 5);
1310 }
1311
1312 void wxTopLevelWindowMSW::DoEnable(bool enable)
1313 {
1314 wxTopLevelWindowBase::DoEnable(enable);
1315
1316 // Enabling or disabling a window may change its appearance. Unfortunately,
1317 // in at least some situation, toplevel windows don't repaint themselves,
1318 // so we have to issue explicit refresh to avoid rendering artifacts.
1319 //
1320 // TODO: find out just what exactly is wrong here
1321 Refresh();
1322 }
1323
1324 void wxTopLevelWindowMSW::DoFreeze()
1325 {
1326 // do nothing: freezing toplevel window causes paint and mouse events
1327 // to go through it any TLWs under it, so the best we can do is to freeze
1328 // all children -- and wxWindowBase::Freeze() does that
1329 }
1330
1331 void wxTopLevelWindowMSW::DoThaw()
1332 {
1333 // intentionally empty -- see DoFreeze()
1334 }
1335
1336
1337 // ----------------------------------------------------------------------------
1338 // wxTopLevelWindow event handling
1339 // ----------------------------------------------------------------------------
1340
1341 // Default activation behaviour - set the focus for the first child
1342 // subwindow found.
1343 void wxTopLevelWindowMSW::OnActivate(wxActivateEvent& event)
1344 {
1345 if ( event.GetActive() )
1346 {
1347 // restore focus to the child which was last focused unless we already
1348 // have it
1349 wxLogTrace(wxT("focus"), wxT("wxTLW %p activated."), m_hWnd);
1350
1351 wxWindow *winFocus = FindFocus();
1352 if ( !winFocus || wxGetTopLevelParent(winFocus) != this )
1353 {
1354 wxWindow *parent = m_winLastFocused ? m_winLastFocused->GetParent()
1355 : NULL;
1356 if ( !parent )
1357 {
1358 parent = this;
1359 }
1360
1361 wxSetFocusToChild(parent, &m_winLastFocused);
1362 }
1363 }
1364 else // deactivating
1365 {
1366 // remember the last focused child if it is our child
1367 m_winLastFocused = FindFocus();
1368
1369 if ( m_winLastFocused )
1370 {
1371 // let it know that it doesn't have focus any more
1372 // But this will already be done via WM_KILLFOCUS, so we'll get two kill
1373 // focus events if we call it explicitly.
1374 // m_winLastFocused->HandleKillFocus((WXHWND)NULL);
1375
1376 // and don't remember it if it's a child from some other frame
1377 if ( wxGetTopLevelParent(m_winLastFocused) != this )
1378 {
1379 m_winLastFocused = NULL;
1380 }
1381 }
1382
1383 wxLogTrace(wxT("focus"),
1384 wxT("wxTLW %p deactivated, last focused: %p."),
1385 m_hWnd,
1386 m_winLastFocused ? GetHwndOf(m_winLastFocused) : NULL);
1387
1388 event.Skip();
1389 }
1390 }
1391
1392 // the DialogProc for all wxWidgets dialogs
1393 LONG APIENTRY _EXPORT
1394 wxDlgProc(HWND hDlg,
1395 UINT message,
1396 WPARAM WXUNUSED(wParam),
1397 LPARAM WXUNUSED(lParam))
1398 {
1399 switch ( message )
1400 {
1401 case WM_INITDIALOG:
1402 {
1403 // under CE, add a "Ok" button in the dialog title bar and make it full
1404 // screen
1405 //
1406 // TODO: find the window for this HWND, and take into account
1407 // wxMAXIMIZE and wxCLOSE_BOX. For now, assume both are present.
1408 //
1409 // Standard SDK doesn't have aygshell.dll: see
1410 // include/wx/msw/wince/libraries.h
1411 #if defined(__WXWINCE__) && !defined(__WINCE_STANDARDSDK__) && !defined(__HANDHELDPC__)
1412 SHINITDLGINFO shidi;
1413 shidi.dwMask = SHIDIM_FLAGS;
1414 shidi.dwFlags = SHIDIF_SIZEDLG // take account of the SIP or menubar
1415 #ifndef __SMARTPHONE__
1416 | SHIDIF_DONEBUTTON
1417 #endif
1418 ;
1419 shidi.hDlg = hDlg;
1420 SHInitDialog( &shidi );
1421 #else // no SHInitDialog()
1422 wxUnusedVar(hDlg);
1423 #endif
1424 // for WM_INITDIALOG, returning TRUE tells system to set focus to
1425 // the first control in the dialog box, but as we set the focus
1426 // ourselves, we return FALSE for it as well
1427 return FALSE;
1428 }
1429 }
1430
1431 // for almost all messages, returning FALSE means that we didn't process
1432 // the message
1433 return FALSE;
1434 }
1435
1436 // ============================================================================
1437 // wxTLWHiddenParentModule implementation
1438 // ============================================================================
1439
1440 HWND wxTLWHiddenParentModule::ms_hwnd = NULL;
1441
1442 const wxChar *wxTLWHiddenParentModule::ms_className = NULL;
1443
1444 bool wxTLWHiddenParentModule::OnInit()
1445 {
1446 ms_hwnd = NULL;
1447 ms_className = NULL;
1448
1449 return true;
1450 }
1451
1452 void wxTLWHiddenParentModule::OnExit()
1453 {
1454 if ( ms_hwnd )
1455 {
1456 if ( !::DestroyWindow(ms_hwnd) )
1457 {
1458 wxLogLastError(wxT("DestroyWindow(hidden TLW parent)"));
1459 }
1460
1461 ms_hwnd = NULL;
1462 }
1463
1464 if ( ms_className )
1465 {
1466 if ( !::UnregisterClass(ms_className, wxGetInstance()) )
1467 {
1468 wxLogLastError(wxT("UnregisterClass(\"wxTLWHiddenParent\")"));
1469 }
1470
1471 ms_className = NULL;
1472 }
1473 }
1474
1475 /* static */
1476 HWND wxTLWHiddenParentModule::GetHWND()
1477 {
1478 if ( !ms_hwnd )
1479 {
1480 if ( !ms_className )
1481 {
1482 static const wxChar *HIDDEN_PARENT_CLASS = wxT("wxTLWHiddenParent");
1483
1484 WNDCLASS wndclass;
1485 wxZeroMemory(wndclass);
1486
1487 wndclass.lpfnWndProc = DefWindowProc;
1488 wndclass.hInstance = wxGetInstance();
1489 wndclass.lpszClassName = HIDDEN_PARENT_CLASS;
1490
1491 if ( !::RegisterClass(&wndclass) )
1492 {
1493 wxLogLastError(wxT("RegisterClass(\"wxTLWHiddenParent\")"));
1494 }
1495 else
1496 {
1497 ms_className = HIDDEN_PARENT_CLASS;
1498 }
1499 }
1500
1501 ms_hwnd = ::CreateWindow(ms_className, wxEmptyString, 0, 0, 0, 0, 0, NULL,
1502 (HMENU)NULL, wxGetInstance(), NULL);
1503 if ( !ms_hwnd )
1504 {
1505 wxLogLastError(wxT("CreateWindow(hidden TLW parent)"));
1506 }
1507 }
1508
1509 return ms_hwnd;
1510 }