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