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