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