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