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