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