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