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