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