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