]> git.saurik.com Git - wxWidgets.git/blob - src/msw/toplevel.cpp
added constructor for compliance with wxPython
[wxWidgets.git] / src / msw / toplevel.cpp
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)
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
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
32 #include "wx/app.h"
33 #include "wx/toplevel.h"
34 #include "wx/string.h"
35 #include "wx/log.h"
36 #include "wx/intl.h"
37 #include "wx/frame.h"
38 #endif //WX_PRECOMP
39
40 #include "wx/msw/private.h"
41
42 // ----------------------------------------------------------------------------
43 // stubs for missing functions under MicroWindows
44 // ----------------------------------------------------------------------------
45
46 #ifdef __WXMICROWIN__
47
48 // static inline bool IsIconic(HWND WXUNUSED(hwnd)) { return FALSE; }
49 static inline bool IsZoomed(HWND WXUNUSED(hwnd)) { return FALSE; }
50
51 #endif // __WXMICROWIN__
52
53 // ----------------------------------------------------------------------------
54 // globals
55 // ----------------------------------------------------------------------------
56
57 // list of all frames and modeless dialogs
58 wxWindowList wxModelessWindows;
59
60 // the name of the default wxWindows class
61 extern const wxChar *wxCanvasClassName;
62
63 // ============================================================================
64 // wxTopLevelWindowMSW implementation
65 // ============================================================================
66
67 // Dialog window proc
68 LONG APIENTRY _EXPORT
69 wxDlgProc(HWND WXUNUSED(hWnd), UINT message, WPARAM WXUNUSED(wParam), LPARAM WXUNUSED(lParam))
70 {
71 if ( message == WM_INITDIALOG )
72 {
73 // for this message, returning TRUE tells system to set focus to the
74 // first control in the dialog box
75 return TRUE;
76 }
77 else
78 {
79 // for all the other ones, FALSE means that we didn't process the
80 // message
81 return FALSE;
82 }
83 }
84
85 // ----------------------------------------------------------------------------
86 // wxTopLevelWindowMSW creation
87 // ----------------------------------------------------------------------------
88
89 void wxTopLevelWindowMSW::Init()
90 {
91 m_iconized =
92 m_maximizeOnShow = FALSE;
93
94 // unlike (almost?) all other windows, frames are created hidden
95 m_isShown = FALSE;
96
97 // Data to save/restore when calling ShowFullScreen
98 m_fsStyle = 0;
99 m_fsOldWindowStyle = 0;
100 m_fsIsMaximized = FALSE;
101 m_fsIsShowing = FALSE;
102 }
103
104 long wxTopLevelWindowMSW::MSWGetCreateWindowFlags(long *exflags) const
105 {
106 long style = GetWindowStyle();
107
108 // first select the kind of window being created
109 //
110 // note that if we don't set WS_POPUP, Windows assumes WS_OVERLAPPED and
111 // creates a window with both caption and border, hence we also test it
112 // below in some other cases
113 long msflags;
114 if ( style & wxFRAME_TOOL_WINDOW )
115 msflags = WS_POPUP;
116 else
117 msflags = WS_OVERLAPPED;
118
119 // border and caption styles
120 if ( style & wxRESIZE_BORDER )
121 msflags |= WS_THICKFRAME;
122 else if ( !(style & wxBORDER_NONE) )
123 msflags |= WS_BORDER;
124 else
125 msflags |= WS_POPUP;
126
127 if ( style & wxCAPTION )
128 msflags |= WS_CAPTION;
129 else
130 msflags |= WS_POPUP;
131
132 // next translate the individual flags
133 if ( style & wxMINIMIZE_BOX )
134 msflags |= WS_MINIMIZEBOX;
135 if ( style & wxMAXIMIZE_BOX )
136 msflags |= WS_MAXIMIZEBOX;
137 if ( style & wxSYSTEM_MENU )
138 msflags |= WS_SYSMENU;
139 if ( style & wxMINIMIZE )
140 msflags |= WS_MINIMIZE;
141 if ( style & wxMAXIMIZE )
142 msflags |= WS_MAXIMIZE;
143
144 if ( style & wxCLIP_CHILDREN )
145 msflags |= WS_CLIPCHILDREN;
146
147 // Keep this here because it saves recoding this function in wxTinyFrame
148 #if wxUSE_ITSY_BITSY && !defined(__WIN32__)
149 if ( style & wxTINY_CAPTION_VERT )
150 msflags |= IBS_VERTCAPTION;
151 if ( style & wxTINY_CAPTION_HORIZ )
152 msflags |= IBS_HORZCAPTION;
153 #else
154 if ( style & (wxTINY_CAPTION_VERT | wxTINY_CAPTION_HORIZ) )
155 msflags |= WS_CAPTION;
156 #endif
157
158 if ( exflags )
159 {
160 *exflags = MakeExtendedStyle(style);
161
162 #if !defined(__WIN16__) && !defined(__SC__)
163 if ( !(GetExtraStyle() & wxTOPLEVEL_EX_DIALOG) )
164 {
165 // make all frames appear in the win9x shell taskbar unless
166 // wxFRAME_TOOL_WINDOW or wxFRAME_NO_TASKBAR is given - without
167 // giving them WS_EX_APPWINDOW style, the child (i.e. owned) frames
168 // wouldn't appear in it
169 if ( (style & wxFRAME_TOOL_WINDOW) || (style & wxFRAME_NO_TASKBAR) )
170 *exflags |= WS_EX_TOOLWINDOW;
171 else if ( !(style & wxFRAME_NO_TASKBAR) )
172 *exflags |= WS_EX_APPWINDOW;
173 }
174 #endif // !Win16
175
176 if ( style & wxSTAY_ON_TOP )
177 *exflags |= WS_EX_TOPMOST;
178
179 #ifdef __WIN32__
180 if ( m_exStyle & wxFRAME_EX_CONTEXTHELP )
181 *exflags |= WS_EX_CONTEXTHELP;
182 #endif // __WIN32__
183 }
184
185 return msflags;
186 }
187
188 bool wxTopLevelWindowMSW::CreateDialog(const wxChar *dlgTemplate,
189 const wxString& title,
190 const wxPoint& pos,
191 const wxSize& size)
192 {
193 #ifdef __WXMICROWIN__
194 // no dialogs support under MicroWin yet
195 return CreateFrame(title, pos, size);
196 #else // !__WXMICROWIN__
197 wxWindow *parent = GetParent();
198
199 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
200 // app window as parent - this avoids creating modal dialogs without
201 // parent
202 if ( !parent && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) )
203 {
204 parent = wxTheApp->GetTopWindow();
205
206 if ( parent )
207 {
208 // don't use transient windows as parents, this is dangerous as it
209 // can lead to a crash if the parent is destroyed before the child
210 //
211 // also don't use the window which is currently hidden as then the
212 // dialog would be hidden as well
213 if ( (parent->GetExtraStyle() & wxWS_EX_TRANSIENT) ||
214 !parent->IsShown() )
215 {
216 parent = NULL;
217 }
218 }
219 }
220
221 m_hWnd = (WXHWND)::CreateDialog(wxGetInstance(),
222 dlgTemplate,
223 parent ? GetHwndOf(parent) : NULL,
224 (DLGPROC)wxDlgProc);
225
226 if ( !m_hWnd )
227 {
228 wxFAIL_MSG(_("Did you forget to include wx/msw/wx.rc in your resources?"));
229
230 wxLogSysError(_("Can't create dialog using template '%s'"), dlgTemplate);
231
232 return FALSE;
233 }
234
235 long exflags;
236 (void)MSWGetCreateWindowFlags(&exflags);
237
238 if ( exflags )
239 {
240 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE, exflags);
241 ::SetWindowPos(GetHwnd(), NULL, 0, 0, 0, 0,
242 SWP_NOSIZE |
243 SWP_NOMOVE |
244 SWP_NOZORDER |
245 SWP_NOACTIVATE);
246 }
247
248 #if defined(__WIN95__)
249 // For some reason, the system menu is activated when we use the
250 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
251 if ( exflags & WS_EX_CONTEXTHELP )
252 {
253 wxFrame *winTop = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
254 if ( winTop )
255 {
256 wxIcon icon = winTop->GetIcon();
257 if ( icon.Ok() )
258 {
259 ::SendMessage(GetHwnd(), WM_SETICON,
260 (WPARAM)TRUE,
261 (LPARAM)GetHiconOf(icon));
262 }
263 }
264 }
265 #endif // __WIN95__
266
267 // move the dialog to its initial position without forcing repainting
268 int x, y, w, h;
269 if ( !MSWGetCreateWindowCoords(pos, size, x, y, w, h) )
270 {
271 x =
272 w = (int)CW_USEDEFAULT;
273 }
274
275 // we can't use CW_USEDEFAULT here as we're not calling CreateWindow()
276 // and passing CW_USEDEFAULT to MoveWindow() results in resizing the
277 // window to (0, 0) size which breaks quite a lot of things, e.g. the
278 // sizer calculation in wxSizer::Fit()
279 if ( w == (int)CW_USEDEFAULT )
280 {
281 // the exact number doesn't matter, the dialog will be resized
282 // again soon anyhow but it should be big enough to allow
283 // calculation relying on "totalSize - clientSize > 0" work, i.e.
284 // at least greater than the title bar height
285 w =
286 h = 100;
287 }
288
289 if ( x == (int)CW_USEDEFAULT )
290 {
291 // centre it on the screen - what else can we do?
292 wxSize sizeDpy = wxGetDisplaySize();
293
294 x = (sizeDpy.x - w) / 2;
295 y = (sizeDpy.y - h) / 2;
296 }
297
298 if ( !::MoveWindow(GetHwnd(), x, y, w, h, FALSE) )
299 {
300 wxLogLastError(wxT("MoveWindow"));
301 }
302
303 if ( !title.empty() )
304 {
305 ::SetWindowText(GetHwnd(), title);
306 }
307
308 SubclassWin(m_hWnd);
309
310 return TRUE;
311 #endif // __WXMICROWIN__/!__WXMICROWIN__
312 }
313
314 bool wxTopLevelWindowMSW::CreateFrame(const wxString& title,
315 const wxPoint& pos,
316 const wxSize& size)
317 {
318 long exflags;
319 long flags = MSWGetCreateWindowFlags(&exflags);
320
321 return MSWCreate(wxCanvasClassName, title, pos, size, flags, exflags);
322 }
323
324 bool wxTopLevelWindowMSW::Create(wxWindow *parent,
325 wxWindowID id,
326 const wxString& title,
327 const wxPoint& pos,
328 const wxSize& size,
329 long style,
330 const wxString& name)
331 {
332 // init our fields
333 Init();
334
335 m_windowStyle = style;
336
337 SetName(name);
338
339 m_windowId = id == -1 ? NewControlId() : id;
340
341 wxTopLevelWindows.Append(this);
342
343 if ( parent )
344 parent->AddChild(this);
345
346 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
347 {
348 // TODO: it would be better to construct the dialog template in memory
349 // during run-time than to rely on the limited number of
350 // templates in wx.rc because:
351 // a) you wouldn't have to include wx.rc in all wxWin programs
352 // (and the number of complaints about it would dtop)
353 // b) we'd be able to provide more templates simply, i.e.
354 // we could generate the templates for all style
355 // combinations
356
357 // we have different dialog templates to allows creation of dialogs
358 // with & without captions under MSWindows, resizeable or not (but a
359 // resizeable dialog always has caption - otherwise it would look too
360 // strange)
361 const wxChar *dlgTemplate;
362 if ( style & wxRESIZE_BORDER )
363 dlgTemplate = wxT("wxResizeableDialog");
364 else if ( style & wxCAPTION )
365 dlgTemplate = wxT("wxCaptionDialog");
366 else
367 dlgTemplate = wxT("wxNoCaptionDialog");
368
369 return CreateDialog(dlgTemplate, title, pos, size);
370 }
371 else // !dialog
372 {
373 return CreateFrame(title, pos, size);
374 }
375 }
376
377 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
378 {
379 wxTopLevelWindows.DeleteObject(this);
380
381 if ( wxModelessWindows.Find(this) )
382 wxModelessWindows.DeleteObject(this);
383
384 // If this is the last top-level window, exit.
385 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
386 {
387 wxTheApp->SetTopWindow(NULL);
388
389 if ( wxTheApp->GetExitOnFrameDelete() )
390 {
391 ::PostQuitMessage(0);
392 }
393 }
394 }
395
396 // ----------------------------------------------------------------------------
397 // wxTopLevelWindowMSW showing
398 // ----------------------------------------------------------------------------
399
400 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd)
401 {
402 ::ShowWindow(GetHwnd(), nShowCmd);
403
404 m_iconized = nShowCmd == SW_MINIMIZE;
405 }
406
407 bool wxTopLevelWindowMSW::Show(bool show)
408 {
409 // don't use wxWindow version as we want to call DoShowWindow() ourselves
410 if ( !wxWindowBase::Show(show) )
411 return FALSE;
412
413 int nShowCmd;
414 if ( show )
415 {
416 if ( m_maximizeOnShow )
417 {
418 // show and maximize
419 nShowCmd = SW_MAXIMIZE;
420
421 m_maximizeOnShow = FALSE;
422 }
423 else // just show
424 {
425 nShowCmd = SW_SHOW;
426 }
427 }
428 else // hide
429 {
430 nShowCmd = SW_HIDE;
431 }
432
433 DoShowWindow(nShowCmd);
434
435 if ( show )
436 {
437 ::BringWindowToTop(GetHwnd());
438
439 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, m_windowId);
440 event.SetEventObject( this );
441 GetEventHandler()->ProcessEvent(event);
442 }
443 else // hide
444 {
445 // Try to highlight the correct window (the parent)
446 if ( GetParent() )
447 {
448 HWND hWndParent = GetHwndOf(GetParent());
449 if (hWndParent)
450 ::BringWindowToTop(hWndParent);
451 }
452 }
453
454 return TRUE;
455 }
456
457 // ----------------------------------------------------------------------------
458 // wxTopLevelWindowMSW maximize/minimize
459 // ----------------------------------------------------------------------------
460
461 void wxTopLevelWindowMSW::Maximize(bool maximize)
462 {
463 if ( IsShown() )
464 {
465 // just maximize it directly
466 DoShowWindow(maximize ? SW_MAXIMIZE : SW_RESTORE);
467 }
468 else // hidden
469 {
470 // we can't maximize the hidden frame because it shows it as well, so
471 // just remember that we should do it later in this case
472 m_maximizeOnShow = TRUE;
473 }
474 }
475
476 bool wxTopLevelWindowMSW::IsMaximized() const
477 {
478 return ::IsZoomed(GetHwnd()) != 0;
479 }
480
481 void wxTopLevelWindowMSW::Iconize(bool iconize)
482 {
483 DoShowWindow(iconize ? SW_MINIMIZE : SW_RESTORE);
484 }
485
486 bool wxTopLevelWindowMSW::IsIconized() const
487 {
488 // also update the current state
489 ((wxTopLevelWindowMSW *)this)->m_iconized = ::IsIconic(GetHwnd()) != 0;
490
491 return m_iconized;
492 }
493
494 void wxTopLevelWindowMSW::Restore()
495 {
496 DoShowWindow(SW_RESTORE);
497 }
498
499 // ----------------------------------------------------------------------------
500 // wxTopLevelWindowMSW fullscreen
501 // ----------------------------------------------------------------------------
502
503 bool wxTopLevelWindowMSW::ShowFullScreen(bool show, long style)
504 {
505 if (show)
506 {
507 if (IsFullScreen())
508 return FALSE;
509
510 m_fsIsShowing = TRUE;
511 m_fsStyle = style;
512
513 // zap the frame borders
514
515 // save the 'normal' window style
516 m_fsOldWindowStyle = GetWindowLong((HWND)GetHWND(), GWL_STYLE);
517
518 // save the old position, width & height, maximize state
519 m_fsOldSize = GetRect();
520 m_fsIsMaximized = IsMaximized();
521
522 // decide which window style flags to turn off
523 LONG newStyle = m_fsOldWindowStyle;
524 LONG offFlags = 0;
525
526 if (style & wxFULLSCREEN_NOBORDER)
527 offFlags |= WS_BORDER | WS_THICKFRAME;
528 if (style & wxFULLSCREEN_NOCAPTION)
529 offFlags |= (WS_CAPTION | WS_SYSMENU);
530
531 newStyle &= (~offFlags);
532
533 // change our window style to be compatible with full-screen mode
534 ::SetWindowLong((HWND)GetHWND(), GWL_STYLE, newStyle);
535
536 // resize to the size of the desktop
537 int width, height;
538
539 RECT rect = wxGetWindowRect(::GetDesktopWindow());
540 width = rect.right - rect.left;
541 height = rect.bottom - rect.top;
542
543 SetSize(width, height);
544
545 // now flush the window style cache and actually go full-screen
546 SetWindowPos((HWND)GetHWND(), HWND_TOP, 0, 0, width, height, SWP_FRAMECHANGED);
547
548 wxSizeEvent event(wxSize(width, height), GetId());
549 GetEventHandler()->ProcessEvent(event);
550
551 return TRUE;
552 }
553 else
554 {
555 if (!IsFullScreen())
556 return FALSE;
557
558 m_fsIsShowing = FALSE;
559
560 Maximize(m_fsIsMaximized);
561 SetWindowLong((HWND)GetHWND(),GWL_STYLE, m_fsOldWindowStyle);
562 SetWindowPos((HWND)GetHWND(),HWND_TOP,m_fsOldSize.x, m_fsOldSize.y,
563 m_fsOldSize.width, m_fsOldSize.height, SWP_FRAMECHANGED);
564
565 return TRUE;
566 }
567 }
568
569 // ----------------------------------------------------------------------------
570 // wxTopLevelWindowMSW misc
571 // ----------------------------------------------------------------------------
572
573 void wxTopLevelWindowMSW::SetIcon(const wxIcon& icon)
574 {
575 // this sets m_icon
576 wxTopLevelWindowBase::SetIcon(icon);
577
578 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
579 if ( m_icon.Ok() )
580 {
581 ::SendMessage(GetHwnd(), WM_SETICON,
582 (WPARAM)TRUE, (LPARAM)GetHiconOf(m_icon));
583 }
584 #endif // __WIN95__
585 }
586
587 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable)
588 {
589 #ifndef __WXMICROWIN__
590 // get system (a.k.a. window) menu
591 HMENU hmenu = ::GetSystemMenu(GetHwnd(), FALSE /* get it */);
592 if ( !hmenu )
593 {
594 wxLogLastError(_T("GetSystemMenu"));
595
596 return FALSE;
597 }
598
599 // enabling/disabling the close item from it also automatically
600 // disables/enables the close title bar button
601 if ( ::EnableMenuItem(hmenu, SC_CLOSE,
602 MF_BYCOMMAND |
603 (enable ? MF_ENABLED : MF_GRAYED)) == -1 )
604 {
605 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
606
607 return FALSE;
608 }
609
610 // update appearance immediately
611 if ( !::DrawMenuBar(GetHwnd()) )
612 {
613 wxLogLastError(_T("DrawMenuBar"));
614 }
615 #endif // !__WXMICROWIN__
616
617 return TRUE;
618 }
619