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