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