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