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