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