]> git.saurik.com Git - wxWidgets.git/blame - src/msw/toplevel.cpp
Added ability to execute more code after the widget is created
[wxWidgets.git] / src / msw / toplevel.cpp
CommitLineData
82c9f85c
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/toplevel.cpp
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)
6c9a19aa 9// License: wxWindows licence
82c9f85c
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
82c9f85c
VZ
21 #pragma implementation "toplevel.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
3a922bb4
RL
32 #include "wx/app.h"
33 #include "wx/toplevel.h"
28b68852 34 #include "wx/dialog.h"
82c9f85c
VZ
35 #include "wx/string.h"
36 #include "wx/log.h"
37 #include "wx/intl.h"
14dd645e 38 #include "wx/frame.h"
706d74ab 39 #include "wx/containr.h" // wxSetFocusToChild()
82c9f85c
VZ
40#endif //WX_PRECOMP
41
2b5f62a0
VZ
42#include "wx/module.h"
43
82c9f85c 44#include "wx/msw/private.h"
80a81a12
JS
45#if defined(__WXWINCE__)
46 #include <ole2.h>
47 #include <shellapi.h>
3874e500
JS
48 // Standard SDK doesn't have aygshell.dll: see include/wx/msw/wince/libraries.h
49 #if _WIN32_WCE < 400 || !defined(WCE_PLATFORM_STANDARDSDK)
2d36b3d8
JS
50 #include <aygshell.h>
51 #endif
52#include "wx/msw/wince/missing.h"
80a81a12
JS
53#endif
54
d61c1a6f 55#include "wx/msw/missing.h"
4676948b
JS
56#include "wx/msw/winundef.h"
57
716645d3 58#include "wx/display.h"
7e25f59e 59
e121a72a
MB
60#ifndef ICON_BIG
61 #define ICON_BIG 1
62#endif
63
64#ifndef ICON_SMALL
65 #define ICON_SMALL 0
66#endif
67
82c9f85c
VZ
68// ----------------------------------------------------------------------------
69// stubs for missing functions under MicroWindows
70// ----------------------------------------------------------------------------
71
72#ifdef __WXMICROWIN__
73
c67d6888 74// static inline bool IsIconic(HWND WXUNUSED(hwnd)) { return FALSE; }
82c9f85c
VZ
75static inline bool IsZoomed(HWND WXUNUSED(hwnd)) { return FALSE; }
76
77#endif // __WXMICROWIN__
78
12447831
VZ
79// NB: wxDlgProc must be defined here and not in dialog.cpp because the latter
80// is not included by wxUniv build which does need wxDlgProc
61179e28
VZ
81LONG APIENTRY _EXPORT
82wxDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
83
82c9f85c
VZ
84// ----------------------------------------------------------------------------
85// globals
86// ----------------------------------------------------------------------------
87
b225f659 88// the name of the default wxWindows class
46fa338b
RR
89#ifdef __WXWINCE__
90extern wxChar *wxCanvasClassName;
91#else
b225f659 92extern const wxChar *wxCanvasClassName;
46fa338b 93#endif
b225f659 94
2b5f62a0
VZ
95// ----------------------------------------------------------------------------
96// wxTLWHiddenParentModule: used to manage the hidden parent window (we need a
97// module to ensure that the window is always deleted)
98// ----------------------------------------------------------------------------
99
100class wxTLWHiddenParentModule : public wxModule
101{
102public:
103 // module init/finalize
104 virtual bool OnInit();
105 virtual void OnExit();
106
107 // get the hidden window (creates on demand)
108 static HWND GetHWND();
109
110private:
111 // the HWND of the hidden parent
112 static HWND ms_hwnd;
113
114 // the class used to create it
115 static const wxChar *ms_className;
116
117 DECLARE_DYNAMIC_CLASS(wxTLWHiddenParentModule)
118};
119
120IMPLEMENT_DYNAMIC_CLASS(wxTLWHiddenParentModule, wxModule)
9dfef5ac 121
82c9f85c
VZ
122// ============================================================================
123// wxTopLevelWindowMSW implementation
124// ============================================================================
125
085ad686
VZ
126BEGIN_EVENT_TABLE(wxTopLevelWindowMSW, wxTopLevelWindowBase)
127 EVT_ACTIVATE(wxTopLevelWindowMSW::OnActivate)
128END_EVENT_TABLE()
129
82c9f85c
VZ
130// ----------------------------------------------------------------------------
131// wxTopLevelWindowMSW creation
132// ----------------------------------------------------------------------------
133
134void wxTopLevelWindowMSW::Init()
135{
136 m_iconized =
137 m_maximizeOnShow = FALSE;
b225f659 138
c641b1d2
VS
139 // Data to save/restore when calling ShowFullScreen
140 m_fsStyle = 0;
141 m_fsOldWindowStyle = 0;
142 m_fsIsMaximized = FALSE;
143 m_fsIsShowing = FALSE;
085ad686
VZ
144
145 m_winLastFocused = (wxWindow *)NULL;
b225f659
VZ
146}
147
b2d5a7ee 148WXDWORD wxTopLevelWindowMSW::MSWGetStyle(long style, WXDWORD *exflags) const
b225f659 149{
b2d5a7ee
VZ
150 // let the base class deal with the common styles but fix the ones which
151 // don't make sense for us (we also deal with the borders ourselves)
152 WXDWORD msflags = wxWindow::MSWGetStyle
153 (
154 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exflags
31e7e89f 155 ) & ~WS_CHILD & ~WS_VISIBLE;
b225f659 156
c1855476
RR
157#if defined(__WXWINCE__) && _WIN32_WCE < 400
158 msflags |= WS_VISIBLE;
159#endif
160
b225f659 161 // first select the kind of window being created
dfb06c62
VZ
162 //
163 // note that if we don't set WS_POPUP, Windows assumes WS_OVERLAPPED and
164 // creates a window with both caption and border, hence we also test it
165 // below in some other cases
0e082993 166 if ( style & wxFRAME_TOOL_WINDOW )
39d2f9a7 167 {
f76858d8 168 msflags |= WS_POPUP;
39d2f9a7 169 }
f76858d8 170 //else: WS_OVERLAPPED is 0 anyhow, so it is on by default
0e082993 171
82eda5ec 172#ifndef __SMARTPHONE__
0e082993
VZ
173 // border and caption styles
174 if ( style & wxRESIZE_BORDER )
175 msflags |= WS_THICKFRAME;
f8d56830
JS
176 else if ( exflags && ((style & wxBORDER_DOUBLE) || (style & wxBORDER_RAISED)) )
177 *exflags |= WS_EX_DLGMODALFRAME;
0e082993
VZ
178 else if ( !(style & wxBORDER_NONE) )
179 msflags |= WS_BORDER;
dfb06c62
VZ
180 else
181 msflags |= WS_POPUP;
82eda5ec 182#endif
0e082993 183
f76858d8
VZ
184 // normally we consider that all windows without caption must be popups,
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
0e082993
VZ
188 if ( style & wxCAPTION )
189 msflags |= WS_CAPTION;
f76858d8 190#ifndef __WXWINCE__
dfb06c62 191 else
b225f659 192 msflags |= WS_POPUP;
f76858d8 193#endif // !__WXWINCE__
e5aa8b81 194
b225f659
VZ
195 // next translate the individual flags
196 if ( style & wxMINIMIZE_BOX )
197 msflags |= WS_MINIMIZEBOX;
198 if ( style & wxMAXIMIZE_BOX )
199 msflags |= WS_MAXIMIZEBOX;
b225f659
VZ
200 if ( style & wxSYSTEM_MENU )
201 msflags |= WS_SYSMENU;
2a47cb1b 202
f76858d8
VZ
203 // NB: under CE these 2 styles are not supported currently, we should
204 // call Minimize()/Maximize() "manually" if we want to support them
b225f659
VZ
205 if ( style & wxMINIMIZE )
206 msflags |= WS_MINIMIZE;
207 if ( style & wxMAXIMIZE )
208 msflags |= WS_MAXIMIZE;
0e082993 209
b225f659 210 // Keep this here because it saves recoding this function in wxTinyFrame
b225f659
VZ
211 if ( style & (wxTINY_CAPTION_VERT | wxTINY_CAPTION_HORIZ) )
212 msflags |= WS_CAPTION;
82eda5ec 213
b225f659
VZ
214 if ( exflags )
215 {
f76858d8
VZ
216 // there is no taskbar under CE, so omit all this
217#ifndef __WXWINCE__
35bf863b
VZ
218 if ( !(GetExtraStyle() & wxTOPLEVEL_EX_DIALOG) )
219 {
9dfef5ac
VZ
220 if ( style & wxFRAME_TOOL_WINDOW )
221 {
222 // create the palette-like window
35bf863b 223 *exflags |= WS_EX_TOOLWINDOW;
404a4d93
VZ
224
225 // tool windows shouldn't appear on the taskbar (as documented)
226 style |= wxFRAME_NO_TASKBAR;
9dfef5ac
VZ
227 }
228
229 // We have to solve 2 different problems here:
230 //
231 // 1. frames with wxFRAME_NO_TASKBAR flag shouldn't appear in the
232 // taskbar even if they don't have a parent
233 //
234 // 2. frames without this style should appear in the taskbar even
235 // if they're owned (Windows only puts non owned windows into
236 // the taskbar normally)
237 //
238 // The second one is solved here by using WS_EX_APPWINDOW flag, the
239 // first one is dealt with in our MSWGetParent() method
240 // implementation
241 if ( !(style & wxFRAME_NO_TASKBAR) && GetParent() )
242 {
243 // need to force the frame to appear in the taskbar
35bf863b 244 *exflags |= WS_EX_APPWINDOW;
9dfef5ac
VZ
245 }
246 //else: nothing to do [here]
35bf863b 247 }
f76858d8 248#endif // !__WXWINCE__
b225f659
VZ
249
250 if ( style & wxSTAY_ON_TOP )
251 *exflags |= WS_EX_TOPMOST;
252
b2d5a7ee 253 if ( GetExtraStyle() & wxFRAME_EX_CONTEXTHELP )
68d02db3 254 *exflags |= WS_EX_CONTEXTHELP;
b225f659
VZ
255 }
256
257 return msflags;
258}
259
9dfef5ac
VZ
260WXHWND wxTopLevelWindowMSW::MSWGetParent() const
261{
262 // for the frames without wxFRAME_FLOAT_ON_PARENT style we should use NULL
263 // parent HWND or it would be always on top of its parent which is not what
264 // we usually want (in fact, we only want it for frames with the
265 // wxFRAME_FLOAT_ON_PARENT flag)
2b5f62a0 266 HWND hwndParent = NULL;
9dfef5ac
VZ
267 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT) )
268 {
2b5f62a0 269 const wxWindow *parent = GetParent();
9dfef5ac 270
2b5f62a0
VZ
271 if ( !parent )
272 {
273 // this flag doesn't make sense then and will be ignored
274 wxFAIL_MSG( _T("wxFRAME_FLOAT_ON_PARENT but no parent?") );
275 }
276 else
277 {
278 hwndParent = GetHwndOf(parent);
279 }
9dfef5ac 280 }
2b5f62a0 281 //else: don't float on parent, must not be owned
9dfef5ac
VZ
282
283 // now deal with the 2nd taskbar-related problem (see comments above in
284 // MSWGetStyle())
2b5f62a0 285 if ( HasFlag(wxFRAME_NO_TASKBAR) && !hwndParent )
9dfef5ac 286 {
2b5f62a0
VZ
287 // use hidden parent
288 hwndParent = wxTLWHiddenParentModule::GetHWND();
9dfef5ac
VZ
289 }
290
2b5f62a0 291 return (WXHWND)hwndParent;
9dfef5ac
VZ
292}
293
6e8515a3 294bool wxTopLevelWindowMSW::CreateDialog(const void *dlgTemplate,
b225f659
VZ
295 const wxString& title,
296 const wxPoint& pos,
297 const wxSize& size)
298{
299#ifdef __WXMICROWIN__
300 // no dialogs support under MicroWin yet
301 return CreateFrame(title, pos, size);
302#else // !__WXMICROWIN__
303 wxWindow *parent = GetParent();
304
305 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
306 // app window as parent - this avoids creating modal dialogs without
307 // parent
308 if ( !parent && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) )
309 {
310 parent = wxTheApp->GetTopWindow();
e058b98d 311
39cc7a0b 312 if ( parent )
e058b98d 313 {
39cc7a0b
VZ
314 // don't use transient windows as parents, this is dangerous as it
315 // can lead to a crash if the parent is destroyed before the child
316 //
317 // also don't use the window which is currently hidden as then the
318 // dialog would be hidden as well
319 if ( (parent->GetExtraStyle() & wxWS_EX_TRANSIENT) ||
320 !parent->IsShown() )
321 {
322 parent = NULL;
323 }
e058b98d 324 }
b225f659
VZ
325 }
326
434005ca
VZ
327 m_hWnd = (WXHWND)::CreateDialogIndirect
328 (
329 wxGetInstance(),
330 (DLGTEMPLATE*)dlgTemplate,
331 parent ? GetHwndOf(parent) : NULL,
332 (DLGPROC)wxDlgProc
333 );
b225f659
VZ
334
335 if ( !m_hWnd )
336 {
7e99eddf 337 wxFAIL_MSG(wxT("Failed to create dialog. Incorrect DLGTEMPLATE?"));
b225f659 338
7e99eddf 339 wxLogSysError(wxT("Can't create dialog using memory template"));
b225f659
VZ
340
341 return FALSE;
342 }
343
b2d5a7ee 344 WXDWORD exflags;
b225f659
VZ
345 (void)MSWGetCreateWindowFlags(&exflags);
346
347 if ( exflags )
348 {
349 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE, exflags);
cef338d3
VZ
350 ::SetWindowPos(GetHwnd(),
351 exflags & WS_EX_TOPMOST ? HWND_TOPMOST : 0,
352 0, 0, 0, 0,
b225f659
VZ
353 SWP_NOSIZE |
354 SWP_NOMOVE |
cef338d3 355 (exflags & WS_EX_TOPMOST ? 0 : SWP_NOZORDER) |
b225f659
VZ
356 SWP_NOACTIVATE);
357 }
358
359#if defined(__WIN95__)
360 // For some reason, the system menu is activated when we use the
361 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
362 if ( exflags & WS_EX_CONTEXTHELP )
363 {
364 wxFrame *winTop = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
365 if ( winTop )
366 {
367 wxIcon icon = winTop->GetIcon();
368 if ( icon.Ok() )
369 {
370 ::SendMessage(GetHwnd(), WM_SETICON,
371 (WPARAM)TRUE,
372 (LPARAM)GetHiconOf(icon));
373 }
374 }
375 }
376#endif // __WIN95__
377
378 // move the dialog to its initial position without forcing repainting
379 int x, y, w, h;
72a11184 380 (void)MSWGetCreateWindowCoords(pos, size, x, y, w, h);
4c53c743 381
26268100
RD
382 if ( x == (int)CW_USEDEFAULT )
383 {
384 // centre it on the screen - what else can we do?
385 wxSize sizeDpy = wxGetDisplaySize();
386
387 x = (sizeDpy.x - w) / 2;
388 y = (sizeDpy.y - h) / 2;
389 }
390
28c191aa 391#ifndef __WXWINCE__
4c53c743
VZ
392 if ( !::MoveWindow(GetHwnd(), x, y, w, h, FALSE) )
393 {
394 wxLogLastError(wxT("MoveWindow"));
b225f659 395 }
28c191aa 396#endif
b225f659
VZ
397
398 if ( !title.empty() )
399 {
400 ::SetWindowText(GetHwnd(), title);
401 }
402
403 SubclassWin(m_hWnd);
404
405 return TRUE;
406#endif // __WXMICROWIN__/!__WXMICROWIN__
407}
408
409bool wxTopLevelWindowMSW::CreateFrame(const wxString& title,
410 const wxPoint& pos,
411 const wxSize& size)
412{
b2d5a7ee
VZ
413 WXDWORD exflags;
414 WXDWORD flags = MSWGetCreateWindowFlags(&exflags);
b225f659 415
11756f61
VZ
416#if (defined(_WIN32_WCE) && _WIN32_WCE < 400) || \
417 defined(WIN32_PLATFORM_PSPC) || \
82eda5ec 418 defined(__SMARTPHONE__)
960b193e 419 // Always expand to fit the screen in PocketPC or SmartPhone
a48e51ce
VZ
420 wxSize sz(wxDefaultSize);
421#else // other (including normal desktop) Windows
422 wxSize sz(size);
960b193e
JS
423#endif
424
425 return MSWCreate(wxCanvasClassName, title, pos, sz, flags, exflags);
82c9f85c
VZ
426}
427
428bool wxTopLevelWindowMSW::Create(wxWindow *parent,
429 wxWindowID id,
430 const wxString& title,
431 const wxPoint& pos,
432 const wxSize& size,
433 long style,
434 const wxString& name)
435{
999836aa 436 bool ret wxDUMMY_INITIALIZE(false);
2a47cb1b 437
82c9f85c
VZ
438 // init our fields
439 Init();
2a47cb1b
VZ
440
441 wxSize sizeReal = size;
442 if ( !sizeReal.IsFullySpecified() )
443 {
444 sizeReal.SetDefaults(GetDefaultSize());
445 }
82c9f85c
VZ
446
447 m_windowStyle = style;
448
449 SetName(name);
450
451 m_windowId = id == -1 ? NewControlId() : id;
452
453 wxTopLevelWindows.Append(this);
454
455 if ( parent )
456 parent->AddChild(this);
457
b225f659
VZ
458 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
459 {
b225f659
VZ
460 // we have different dialog templates to allows creation of dialogs
461 // with & without captions under MSWindows, resizeable or not (but a
462 // resizeable dialog always has caption - otherwise it would look too
463 // strange)
434005ca
VZ
464
465 // we need 3 additional WORDs for dialog menu, class and title (as we
466 // don't use DS_SETFONT we don't need the fourth WORD for the font)
467 static const int dlgsize = sizeof(DLGTEMPLATE) + (sizeof(WORD) * 3);
468 DLGTEMPLATE *dlgTemplate = (DLGTEMPLATE *)malloc(dlgsize);
469 memset(dlgTemplate, 0, dlgsize);
470
471 // these values are arbitrary, they won't be used normally anyhow
6e8515a3
JS
472 dlgTemplate->x = 34;
473 dlgTemplate->y = 22;
474 dlgTemplate->cx = 144;
475 dlgTemplate->cy = 75;
476
434005ca
VZ
477 // reuse the code in MSWGetStyle() but correct the results slightly for
478 // the dialog
479 dlgTemplate->style = MSWGetStyle(style, NULL);
480
481 // all dialogs are popups
482 dlgTemplate->style |= WS_POPUP;
483
484 // force 3D-look if necessary, it looks impossibly ugly otherwise
485 if ( style & (wxRESIZE_BORDER | wxCAPTION) )
486 dlgTemplate->style |= DS_MODALFRAME;
b225f659 487
2a47cb1b 488 ret = CreateDialog(dlgTemplate, title, pos, sizeReal);
6e8515a3 489 free(dlgTemplate);
b225f659
VZ
490 }
491 else // !dialog
492 {
2a47cb1b 493 ret = CreateFrame(title, pos, sizeReal);
9ca4dd06
VS
494 }
495
496 if ( ret && !(GetWindowStyleFlag() & wxCLOSE_BOX) )
497 {
498 EnableCloseButton(false);
b225f659 499 }
9ca4dd06 500
bb655ead
VZ
501 // for some reason we need to manually send ourselves this message as
502 // otherwise the mnemonics are always shown -- even if they're configured
503 // to be hidden until "Alt" is pressed in the control panel
504 //
505 // this could indicate a bug somewhere else but for now this is the only
506 // fix we have
507 if ( ret )
508 {
509 SendMessage
510 (
511 GetHwnd(),
512 WM_UPDATEUISTATE,
513 MAKEWPARAM(UIS_INITIALIZE, UISF_HIDEFOCUS | UISF_HIDEACCEL),
514 0
515 );
516 }
517
82eda5ec
JS
518 // Native look is full screen window on Smartphones
519#ifdef __SMARTPHONE__
520 if ( style & wxMAXIMIZE )
521 Maximize();
522#endif
523
9ca4dd06 524 return ret;
82c9f85c
VZ
525}
526
527wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
528{
d6fb86a8
VZ
529 // after destroying an owned window, Windows activates the next top level
530 // window in Z order but it may be different from our owner (to reproduce
531 // this simply Alt-TAB to another application and back before closing the
532 // owned frame) whereas we always want to yield activation to our parent
533 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT) )
534 {
535 wxWindow *parent = GetParent();
536 if ( parent )
537 {
538 ::BringWindowToTop(GetHwndOf(parent));
539 }
540 }
82c9f85c
VZ
541}
542
82c9f85c
VZ
543// ----------------------------------------------------------------------------
544// wxTopLevelWindowMSW showing
545// ----------------------------------------------------------------------------
546
547void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd)
548{
549 ::ShowWindow(GetHwnd(), nShowCmd);
550
551 m_iconized = nShowCmd == SW_MINIMIZE;
552}
553
554bool wxTopLevelWindowMSW::Show(bool show)
555{
556 // don't use wxWindow version as we want to call DoShowWindow() ourselves
557 if ( !wxWindowBase::Show(show) )
558 return FALSE;
559
560 int nShowCmd;
561 if ( show )
562 {
563 if ( m_maximizeOnShow )
564 {
565 // show and maximize
566 nShowCmd = SW_MAXIMIZE;
567
568 m_maximizeOnShow = FALSE;
569 }
570 else // just show
571 {
84cff965
JS
572 if ( GetWindowStyle() & wxFRAME_TOOL_WINDOW )
573 nShowCmd = SW_SHOWNA;
574 else
575 nShowCmd = SW_SHOW;
82c9f85c
VZ
576 }
577 }
578 else // hide
579 {
580 nShowCmd = SW_HIDE;
581 }
582
583 DoShowWindow(nShowCmd);
584
585 if ( show )
586 {
587 ::BringWindowToTop(GetHwnd());
588
589 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, m_windowId);
590 event.SetEventObject( this );
591 GetEventHandler()->ProcessEvent(event);
592 }
593 else // hide
594 {
595 // Try to highlight the correct window (the parent)
596 if ( GetParent() )
597 {
598 HWND hWndParent = GetHwndOf(GetParent());
599 if (hWndParent)
600 ::BringWindowToTop(hWndParent);
601 }
602 }
603
604 return TRUE;
605}
606
607// ----------------------------------------------------------------------------
608// wxTopLevelWindowMSW maximize/minimize
609// ----------------------------------------------------------------------------
610
611void wxTopLevelWindowMSW::Maximize(bool maximize)
612{
613 if ( IsShown() )
614 {
615 // just maximize it directly
616 DoShowWindow(maximize ? SW_MAXIMIZE : SW_RESTORE);
617 }
618 else // hidden
619 {
620 // we can't maximize the hidden frame because it shows it as well, so
621 // just remember that we should do it later in this case
a0be434e 622 m_maximizeOnShow = maximize;
82c9f85c
VZ
623 }
624}
625
626bool wxTopLevelWindowMSW::IsMaximized() const
627{
4676948b
JS
628#ifdef __WXWINCE__
629 return FALSE;
630#else
82c9f85c 631 return ::IsZoomed(GetHwnd()) != 0;
4676948b 632#endif
82c9f85c
VZ
633}
634
635void wxTopLevelWindowMSW::Iconize(bool iconize)
636{
637 DoShowWindow(iconize ? SW_MINIMIZE : SW_RESTORE);
638}
639
640bool wxTopLevelWindowMSW::IsIconized() const
641{
4676948b
JS
642#ifdef __WXWINCE__
643 return FALSE;
644#else
82c9f85c
VZ
645 // also update the current state
646 ((wxTopLevelWindowMSW *)this)->m_iconized = ::IsIconic(GetHwnd()) != 0;
647
648 return m_iconized;
4676948b 649#endif
82c9f85c
VZ
650}
651
652void wxTopLevelWindowMSW::Restore()
653{
654 DoShowWindow(SW_RESTORE);
655}
656
c641b1d2
VS
657// ----------------------------------------------------------------------------
658// wxTopLevelWindowMSW fullscreen
659// ----------------------------------------------------------------------------
660
661bool wxTopLevelWindowMSW::ShowFullScreen(bool show, long style)
662{
716645d3 663 if ( show == IsFullScreen() )
c641b1d2 664 {
716645d3
VZ
665 // nothing to do
666 return TRUE;
667 }
668
669 m_fsIsShowing = show;
c641b1d2 670
716645d3
VZ
671 if ( show )
672 {
c641b1d2
VS
673 m_fsStyle = style;
674
675 // zap the frame borders
676
677 // save the 'normal' window style
716645d3 678 m_fsOldWindowStyle = GetWindowLong(GetHwnd(), GWL_STYLE);
c641b1d2
VS
679
680 // save the old position, width & height, maximize state
681 m_fsOldSize = GetRect();
682 m_fsIsMaximized = IsMaximized();
683
684 // decide which window style flags to turn off
685 LONG newStyle = m_fsOldWindowStyle;
686 LONG offFlags = 0;
687
688 if (style & wxFULLSCREEN_NOBORDER)
4676948b
JS
689 {
690 offFlags |= WS_BORDER;
691#ifndef __WXWINCE__
692 offFlags |= WS_THICKFRAME;
693#endif
694 }
c641b1d2 695 if (style & wxFULLSCREEN_NOCAPTION)
716645d3 696 offFlags |= WS_CAPTION | WS_SYSMENU;
c641b1d2 697
716645d3 698 newStyle &= ~offFlags;
c641b1d2
VS
699
700 // change our window style to be compatible with full-screen mode
716645d3 701 ::SetWindowLong(GetHwnd(), GWL_STYLE, newStyle);
c641b1d2 702
716645d3
VZ
703 wxRect rect;
704#if wxUSE_DISPLAY
705 // resize to the size of the display containing us
706 int dpy = wxDisplay::GetFromWindow(this);
707 if ( dpy != wxNOT_FOUND )
708 {
709 rect = wxDisplay(dpy).GetGeometry();
710 }
711 else // fall back to the main desktop
712#else // wxUSE_DISPLAY
713 {
714 // resize to the size of the desktop
715 wxCopyRECTToRect(wxGetWindowRect(::GetDesktopWindow()), rect);
80a81a12
JS
716#ifdef __WXWINCE__
717 // FIXME: size of the bottom menu (toolbar)
718 // should be taken in account
719 rect.height += rect.y;
720 rect.y = 0;
4676948b 721#endif
716645d3
VZ
722 }
723#endif // wxUSE_DISPLAY
c641b1d2 724
716645d3 725 SetSize(rect);
c641b1d2
VS
726
727 // now flush the window style cache and actually go full-screen
716645d3 728 long flags = SWP_FRAMECHANGED;
c641b1d2 729
716645d3
VZ
730 // showing the frame full screen should also show it if it's still
731 // hidden
732 if ( !IsShown() )
733 {
734 // don't call wxWindow version to avoid flicker from calling
735 // ::ShowWindow() -- we're going to show the window at the correct
736 // location directly below -- but do call the wxWindowBase version
737 // to sync the internal m_isShown flag
738 wxWindowBase::Show();
c641b1d2 739
716645d3
VZ
740 flags |= SWP_SHOWWINDOW;
741 }
c641b1d2 742
716645d3
VZ
743 SetWindowPos(GetHwnd(), HWND_TOP,
744 rect.x, rect.y, rect.width, rect.height,
745 flags);
c641b1d2 746
c0f98472 747#if defined(__WXWINCE__) && _WIN32_WCE < 400
80a81a12
JS
748 ::SHFullScreen(GetHwnd(), SHFS_HIDETASKBAR | SHFS_HIDESIPBUTTON);
749#endif
750
716645d3
VZ
751 // finally send an event allowing the window to relayout itself &c
752 wxSizeEvent event(rect.GetSize(), GetId());
753 GetEventHandler()->ProcessEvent(event);
754 }
755 else // stop showing full screen
756 {
c0f98472 757#if defined(__WXWINCE__) && _WIN32_WCE < 400
80a81a12
JS
758 ::SHFullScreen(GetHwnd(), SHFS_SHOWTASKBAR | SHFS_SHOWSIPBUTTON);
759#endif
c641b1d2 760 Maximize(m_fsIsMaximized);
716645d3
VZ
761 SetWindowLong(GetHwnd(),GWL_STYLE, m_fsOldWindowStyle);
762 SetWindowPos(GetHwnd(),HWND_TOP,m_fsOldSize.x, m_fsOldSize.y,
c641b1d2 763 m_fsOldSize.width, m_fsOldSize.height, SWP_FRAMECHANGED);
c641b1d2 764 }
716645d3
VZ
765
766 return TRUE;
c641b1d2
VS
767}
768
82c9f85c
VZ
769// ----------------------------------------------------------------------------
770// wxTopLevelWindowMSW misc
771// ----------------------------------------------------------------------------
772
773void wxTopLevelWindowMSW::SetIcon(const wxIcon& icon)
774{
f618020a
MB
775 SetIcons( wxIconBundle( icon ) );
776}
777
778void wxTopLevelWindowMSW::SetIcons(const wxIconBundle& icons)
779{
780 wxTopLevelWindowBase::SetIcons(icons);
82c9f85c
VZ
781
782#if defined(__WIN95__) && !defined(__WXMICROWIN__)
f618020a
MB
783 const wxIcon& sml = icons.GetIcon( wxSize( 16, 16 ) );
784 if( sml.Ok() && sml.GetWidth() == 16 && sml.GetHeight() == 16 )
785 {
786 ::SendMessage( GetHwndOf( this ), WM_SETICON, ICON_SMALL,
787 (LPARAM)GetHiconOf(sml) );
788 }
789
790 const wxIcon& big = icons.GetIcon( wxSize( 32, 32 ) );
791 if( big.Ok() && big.GetWidth() == 32 && big.GetHeight() == 32 )
82c9f85c 792 {
f618020a
MB
793 ::SendMessage( GetHwndOf( this ), WM_SETICON, ICON_BIG,
794 (LPARAM)GetHiconOf(big) );
82c9f85c
VZ
795 }
796#endif // __WIN95__
797}
a91cf80c
VZ
798
799bool wxTopLevelWindowMSW::EnableCloseButton(bool enable)
800{
4676948b 801#if !defined(__WXMICROWIN__)
a91cf80c 802 // get system (a.k.a. window) menu
4676948b 803 HMENU hmenu = GetSystemMenu(GetHwnd(), FALSE /* get it */);
a91cf80c
VZ
804 if ( !hmenu )
805 {
660b1906
VZ
806 // no system menu at all -- ok if we want to remove the close button
807 // anyhow, but bad if we want to show it
808 return !enable;
a91cf80c
VZ
809 }
810
811 // enabling/disabling the close item from it also automatically
812 // disables/enables the close title bar button
aaf765a5
VZ
813 if ( ::EnableMenuItem(hmenu, SC_CLOSE,
814 MF_BYCOMMAND |
815 (enable ? MF_ENABLED : MF_GRAYED)) == -1 )
a91cf80c
VZ
816 {
817 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
818
819 return FALSE;
820 }
960b193e 821#ifndef __WXWINCE__
a91cf80c
VZ
822 // update appearance immediately
823 if ( !::DrawMenuBar(GetHwnd()) )
824 {
825 wxLogLastError(_T("DrawMenuBar"));
826 }
960b193e 827#endif
a91cf80c
VZ
828#endif // !__WXMICROWIN__
829
830 return TRUE;
831}
832
2a47cb1b
VZ
833#ifndef __WXWINCE__
834
1542ea39
RD
835bool wxTopLevelWindowMSW::SetShape(const wxRegion& region)
836{
6a7e6411
RD
837 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), FALSE,
838 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
839
1542ea39
RD
840 // The empty region signifies that the shape should be removed from the
841 // window.
842 if ( region.IsEmpty() )
843 {
844 if (::SetWindowRgn(GetHwnd(), NULL, TRUE) == 0)
845 {
846 wxLogLastError(_T("SetWindowRgn"));
847 return FALSE;
848 }
849 return TRUE;
850 }
851
852 // Windows takes ownership of the region, so
853 // we'll have to make a copy of the region to give to it.
854 DWORD noBytes = ::GetRegionData(GetHrgnOf(region), 0, NULL);
855 RGNDATA *rgnData = (RGNDATA*) new char[noBytes];
856 ::GetRegionData(GetHrgnOf(region), noBytes, rgnData);
857 HRGN hrgn = ::ExtCreateRegion(NULL, noBytes, rgnData);
858 delete[] (char*) rgnData;
859
860 // SetWindowRgn expects the region to be in coordinants
861 // relative to the window, not the client area. Figure
862 // out the offset, if any.
863 RECT rect;
864 DWORD dwStyle = ::GetWindowLong(GetHwnd(), GWL_STYLE);
865 DWORD dwExStyle = ::GetWindowLong(GetHwnd(), GWL_EXSTYLE);
866 ::GetClientRect(GetHwnd(), &rect);
867 ::AdjustWindowRectEx(&rect, dwStyle, FALSE, dwExStyle);
868 ::OffsetRgn(hrgn, -rect.left, -rect.top);
869
870 // Now call the shape API with the new region.
871 if (::SetWindowRgn(GetHwnd(), hrgn, TRUE) == 0)
872 {
873 wxLogLastError(_T("SetWindowRgn"));
874 return FALSE;
875 }
876 return TRUE;
877}
878
2a47cb1b
VZ
879#endif // !__WXWINCE__
880
085ad686
VZ
881// ----------------------------------------------------------------------------
882// wxTopLevelWindow event handling
883// ----------------------------------------------------------------------------
884
885// Default activation behaviour - set the focus for the first child
886// subwindow found.
887void wxTopLevelWindowMSW::OnActivate(wxActivateEvent& event)
888{
889 if ( event.GetActive() )
890 {
2736a5de
VZ
891 // restore focus to the child which was last focused unless we already
892 // have it
893 wxLogTrace(_T("focus"), _T("wxTLW %08x activated."), (int) m_hWnd);
085ad686 894
2736a5de
VZ
895 wxWindow *winFocus = FindFocus();
896 if ( !winFocus || wxGetTopLevelParent(winFocus) != this )
085ad686 897 {
2736a5de
VZ
898 wxWindow *parent = m_winLastFocused ? m_winLastFocused->GetParent()
899 : NULL;
900 if ( !parent )
901 {
902 parent = this;
903 }
085ad686 904
2736a5de
VZ
905 wxSetFocusToChild(parent, &m_winLastFocused);
906 }
085ad686
VZ
907 }
908 else // deactivating
909 {
910 // remember the last focused child if it is our child
911 m_winLastFocused = FindFocus();
912
13834828
VZ
913 if ( m_winLastFocused )
914 {
915 // let it know that it doesn't have focus any more
77e00fe9 916 m_winLastFocused->HandleKillFocus((WXHWND)NULL);
13834828 917
2736a5de
VZ
918 // and don't remember it if it's a child from some other frame
919 if ( wxGetTopLevelParent(m_winLastFocused) != this )
085ad686 920 {
2736a5de 921 m_winLastFocused = NULL;
085ad686 922 }
085ad686
VZ
923 }
924
925 wxLogTrace(_T("focus"),
926 _T("wxTLW %08x deactivated, last focused: %08x."),
9b601c24
GRG
927 (int) m_hWnd,
928 (int) (m_winLastFocused ? GetHwndOf(m_winLastFocused)
929 : NULL));
085ad686
VZ
930
931 event.Skip();
932 }
933}
934
12447831
VZ
935// the DialogProc for all wxWindows dialogs
936LONG APIENTRY _EXPORT
0fc58b86 937wxDlgProc(HWND hDlg,
2eb10e2a
VZ
938 UINT message,
939 WPARAM WXUNUSED(wParam),
940 LPARAM WXUNUSED(lParam))
12447831 941{
a48e51ce 942 if ( message == WM_INITDIALOG )
12447831 943 {
a48e51ce
VZ
944 // under CE, add a "Ok" button in the dialog title bar and make it full
945 // screen
946 //
947 // VZ: we should probably allow for overriding this, e.g. by including
948 // MAXIMIZED flag in the dialog style by default and doing this
949 // only if it is present...
950
951 // Standard SDK doesn't have aygshell.dll: see
952 // include/wx/msw/wince/libraries.h
3874e500 953#if defined(__WXWINCE__) && !defined(WCE_PLATFORM_STANDARDSDK)
a48e51ce
VZ
954 SHINITDLGINFO shidi;
955 shidi.dwMask = SHIDIM_FLAGS;
956 shidi.dwFlags = SHIDIF_DONEBUTTON |
957 SHIDIF_SIZEDLGFULLSCREEN;
958 shidi.hDlg = hDlg;
959 SHInitDialog( &shidi );
960#else // no SHInitDialog()
961 wxUnusedVar(hDlg);
0fc58b86 962#endif
12447831 963 }
a48e51ce
VZ
964
965 // for almost all messages, returning FALSE means that we didn't process
966 // the message
967 //
968 // for WM_INITDIALOG, returning TRUE tells system to set focus to
969 // the first control in the dialog box, but as we set the focus
970 // ourselves, we return FALSE for it as well
971 return FALSE;
12447831
VZ
972}
973
2b5f62a0
VZ
974// ============================================================================
975// wxTLWHiddenParentModule implementation
976// ============================================================================
977
978HWND wxTLWHiddenParentModule::ms_hwnd = NULL;
979
980const wxChar *wxTLWHiddenParentModule::ms_className = NULL;
981
982bool wxTLWHiddenParentModule::OnInit()
983{
984 ms_hwnd = NULL;
985 ms_className = NULL;
986
987 return TRUE;
988}
989
990void wxTLWHiddenParentModule::OnExit()
991{
992 if ( ms_hwnd )
993 {
994 if ( !::DestroyWindow(ms_hwnd) )
995 {
996 wxLogLastError(_T("DestroyWindow(hidden TLW parent)"));
997 }
998
999 ms_hwnd = NULL;
1000 }
1001
1002 if ( ms_className )
1003 {
1004 if ( !::UnregisterClass(ms_className, wxGetInstance()) )
1005 {
1006 wxLogLastError(_T("UnregisterClass(\"wxTLWHiddenParent\")"));
1007 }
1008
1009 ms_className = NULL;
1010 }
1011}
1012
1013/* static */
1014HWND wxTLWHiddenParentModule::GetHWND()
1015{
1016 if ( !ms_hwnd )
1017 {
1018 if ( !ms_className )
1019 {
1020 static const wxChar *HIDDEN_PARENT_CLASS = _T("wxTLWHiddenParent");
1021
1022 WNDCLASS wndclass;
1023 wxZeroMemory(wndclass);
1024
1025 wndclass.lpfnWndProc = DefWindowProc;
1026 wndclass.hInstance = wxGetInstance();
1027 wndclass.lpszClassName = HIDDEN_PARENT_CLASS;
1028
1029 if ( !::RegisterClass(&wndclass) )
1030 {
1031 wxLogLastError(_T("RegisterClass(\"wxTLWHiddenParent\")"));
1032 }
1033 else
1034 {
1035 ms_className = HIDDEN_PARENT_CLASS;
1036 }
1037 }
1038
fda7962d 1039 ms_hwnd = ::CreateWindow(ms_className, wxEmptyString, 0, 0, 0, 0, 0, NULL,
2b5f62a0
VZ
1040 (HMENU)NULL, wxGetInstance(), NULL);
1041 if ( !ms_hwnd )
1042 {
1043 wxLogLastError(_T("CreateWindow(hidden TLW parent)"));
1044 }
1045 }
1046
1047 return ms_hwnd;
1048}
1049
1542ea39 1050