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