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