]> git.saurik.com Git - wxWidgets.git/blob - src/msw/toplevel.cpp
8874a8d15634d733962f2ff6cd165d40cc518733
[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 long msflags = 0;
108
109 // first select the kind of window being created
110 if ( style & wxCAPTION )
111 {
112 if ( style & wxFRAME_TOOL_WINDOW )
113 msflags |= WS_POPUPWINDOW;
114 else
115 msflags |= WS_OVERLAPPED;
116 }
117 else
118 {
119 msflags |= WS_POPUP;
120 }
121
122 // next translate the individual flags
123 if ( style & wxMINIMIZE_BOX )
124 msflags |= WS_MINIMIZEBOX;
125 if ( style & wxMAXIMIZE_BOX )
126 msflags |= WS_MAXIMIZEBOX;
127 if ( style & wxTHICK_FRAME )
128 msflags |= WS_THICKFRAME;
129 if ( style & wxSYSTEM_MENU )
130 msflags |= WS_SYSMENU;
131 if ( style & wxMINIMIZE )
132 msflags |= WS_MINIMIZE;
133 if ( style & wxMAXIMIZE )
134 msflags |= WS_MAXIMIZE;
135 if ( style & wxCAPTION )
136 msflags |= WS_CAPTION;
137 if ( style & wxCLIP_CHILDREN )
138 msflags |= WS_CLIPCHILDREN;
139
140 // Keep this here because it saves recoding this function in wxTinyFrame
141 #if wxUSE_ITSY_BITSY && !defined(__WIN32__)
142 if ( style & wxTINY_CAPTION_VERT )
143 msflags |= IBS_VERTCAPTION;
144 if ( style & wxTINY_CAPTION_HORIZ )
145 msflags |= IBS_HORZCAPTION;
146 #else
147 if ( style & (wxTINY_CAPTION_VERT | wxTINY_CAPTION_HORIZ) )
148 msflags |= WS_CAPTION;
149 #endif
150
151 if ( exflags )
152 {
153 *exflags = MakeExtendedStyle(style);
154
155 // make all frames appear in the win9x shell taskbar unless
156 // wxFRAME_TOOL_WINDOW or wxFRAME_NO_TASKBAR is given - without giving
157 // them WS_EX_APPWINDOW style, the child (i.e. owned) frames wouldn't
158 // appear in it
159 #if !defined(__WIN16__) && !defined(__SC__)
160 if ( (style & wxFRAME_TOOL_WINDOW) || (style & wxFRAME_NO_TASKBAR) )
161 *exflags |= WS_EX_TOOLWINDOW;
162 else if ( !(style & wxFRAME_NO_TASKBAR) )
163 *exflags |= WS_EX_APPWINDOW;
164 #endif
165
166 if ( style & wxSTAY_ON_TOP )
167 *exflags |= WS_EX_TOPMOST;
168
169 #ifdef __WIN32__
170 if ( m_exStyle & wxFRAME_EX_CONTEXTHELP )
171 *exflags |= WS_EX_CONTEXTHELP;
172 #endif // __WIN32__
173 }
174
175 return msflags;
176 }
177
178 bool wxTopLevelWindowMSW::CreateDialog(const wxChar *dlgTemplate,
179 const wxString& title,
180 const wxPoint& pos,
181 const wxSize& size)
182 {
183 #ifdef __WXMICROWIN__
184 // no dialogs support under MicroWin yet
185 return CreateFrame(title, pos, size);
186 #else // !__WXMICROWIN__
187 wxWindow *parent = GetParent();
188
189 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
190 // app window as parent - this avoids creating modal dialogs without
191 // parent
192 if ( !parent && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) )
193 {
194 parent = wxTheApp->GetTopWindow();
195
196 // but don't use the window which is about to be destroyed as parent
197 if ( parent->IsBeingDeleted() )
198 {
199 parent = NULL;
200 }
201 }
202
203 m_hWnd = (WXHWND)::CreateDialog(wxGetInstance(),
204 dlgTemplate,
205 parent ? GetHwndOf(parent) : NULL,
206 (DLGPROC)wxDlgProc);
207
208 if ( !m_hWnd )
209 {
210 wxFAIL_MSG(_("Did you forget to include wx/msw/wx.rc in your resources?"));
211
212 wxLogSysError(_("Can't create dialog using template '%s'"), dlgTemplate);
213
214 return FALSE;
215 }
216
217 long exflags;
218 (void)MSWGetCreateWindowFlags(&exflags);
219
220 if ( exflags )
221 {
222 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE, exflags);
223 ::SetWindowPos(GetHwnd(), NULL, 0, 0, 0, 0,
224 SWP_NOSIZE |
225 SWP_NOMOVE |
226 SWP_NOZORDER |
227 SWP_NOACTIVATE);
228 }
229
230 #if defined(__WIN95__)
231 // For some reason, the system menu is activated when we use the
232 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
233 if ( exflags & WS_EX_CONTEXTHELP )
234 {
235 wxFrame *winTop = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
236 if ( winTop )
237 {
238 wxIcon icon = winTop->GetIcon();
239 if ( icon.Ok() )
240 {
241 ::SendMessage(GetHwnd(), WM_SETICON,
242 (WPARAM)TRUE,
243 (LPARAM)GetHiconOf(icon));
244 }
245 }
246 }
247 #endif // __WIN95__
248
249 // move the dialog to its initial position without forcing repainting
250 int x, y, w, h;
251 if ( MSWGetCreateWindowCoords(pos, size, x, y, w, h) )
252 {
253 // we can't use CW_USEDEFAULT here as we're not calling CreateWindow()
254 // and passing CW_USEDEFAULT to MoveWindow() results in resizing the
255 // window to (0, 0) size which breaks quite a lot of things, e.g. the
256 // sizer calculation in wxSizer::Fit()
257 if ( w == CW_USEDEFAULT )
258 {
259 // the exact number doesn't matter, the dialog will be resized
260 // again soon anyhow but it should be big enough to allow
261 // calculation relying on "totalSize - clientSize > 0" work, i.e.
262 // at least greater than the title bar height
263 w =
264 h = 100;
265 }
266
267 if ( !::MoveWindow(GetHwnd(), x, y, w, h, FALSE) )
268 {
269 wxLogLastError(wxT("MoveWindow"));
270 }
271 }
272 //else: leave it at default position
273
274 if ( !title.empty() )
275 {
276 ::SetWindowText(GetHwnd(), title);
277 }
278
279 SubclassWin(m_hWnd);
280
281 return TRUE;
282 #endif // __WXMICROWIN__/!__WXMICROWIN__
283 }
284
285 bool wxTopLevelWindowMSW::CreateFrame(const wxString& title,
286 const wxPoint& pos,
287 const wxSize& size)
288 {
289 long exflags;
290 long flags = MSWGetCreateWindowFlags(&exflags);
291
292 return MSWCreate(wxCanvasClassName, title, pos, size, flags, exflags);
293 }
294
295 bool wxTopLevelWindowMSW::Create(wxWindow *parent,
296 wxWindowID id,
297 const wxString& title,
298 const wxPoint& pos,
299 const wxSize& size,
300 long style,
301 const wxString& name)
302 {
303 // init our fields
304 Init();
305
306 m_windowStyle = style;
307
308 SetName(name);
309
310 m_windowId = id == -1 ? NewControlId() : id;
311
312 wxTopLevelWindows.Append(this);
313
314 if ( parent )
315 parent->AddChild(this);
316
317 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
318 {
319 // TODO: it would be better to construct the dialog template in memory
320 // during run-time than to rely on the limited number of
321 // templates in wx.rc because:
322 // a) you wouldn't have to include wx.rc in all wxWin programs
323 // (and the number of complaints about it would dtop)
324 // b) we'd be able to provide more templates simply, i.e.
325 // we could generate the templates for all style
326 // combinations
327
328 // we have different dialog templates to allows creation of dialogs
329 // with & without captions under MSWindows, resizeable or not (but a
330 // resizeable dialog always has caption - otherwise it would look too
331 // strange)
332 const wxChar *dlgTemplate;
333 if ( style & wxRESIZE_BORDER )
334 dlgTemplate = wxT("wxResizeableDialog");
335 else if ( style & wxCAPTION )
336 dlgTemplate = wxT("wxCaptionDialog");
337 else
338 dlgTemplate = wxT("wxNoCaptionDialog");
339
340 return CreateDialog(dlgTemplate, title, pos, size);
341 }
342 else // !dialog
343 {
344 return CreateFrame(title, pos, size);
345 }
346 }
347
348 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
349 {
350 wxTopLevelWindows.DeleteObject(this);
351
352 if ( wxModelessWindows.Find(this) )
353 wxModelessWindows.DeleteObject(this);
354
355 // If this is the last top-level window, exit.
356 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
357 {
358 wxTheApp->SetTopWindow(NULL);
359
360 if ( wxTheApp->GetExitOnFrameDelete() )
361 {
362 ::PostQuitMessage(0);
363 }
364 }
365 }
366
367 // ----------------------------------------------------------------------------
368 // wxTopLevelWindowMSW geometry
369 // ----------------------------------------------------------------------------
370
371 void wxTopLevelWindowMSW::DoSetClientSize(int width, int height)
372 {
373 HWND hWnd = GetHwnd();
374
375 RECT rectClient;
376 ::GetClientRect(hWnd, &rectClient);
377
378 RECT rectTotal;
379 ::GetWindowRect(hWnd, &rectTotal);
380
381 // Find the difference between the entire window (title bar and all)
382 // and the client area; add this to the new client size to move the
383 // window
384 width += rectTotal.right - rectTotal.left - rectClient.right;
385 height += rectTotal.bottom - rectTotal.top - rectClient.bottom;
386
387 // note that calling GetClientAreaOrigin() takes the toolbar into account
388 wxPoint pt = GetClientAreaOrigin();
389 width += pt.x;
390 height += pt.y;
391
392 if ( !::MoveWindow(hWnd, rectTotal.left, rectTotal.top,
393 width, height, TRUE /* redraw */) )
394 {
395 wxLogLastError(_T("MoveWindow"));
396 }
397
398 wxSizeEvent event(wxSize(width, height), m_windowId);
399 event.SetEventObject(this);
400 (void)GetEventHandler()->ProcessEvent(event);
401 }
402
403 // ----------------------------------------------------------------------------
404 // wxTopLevelWindowMSW showing
405 // ----------------------------------------------------------------------------
406
407 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd)
408 {
409 ::ShowWindow(GetHwnd(), nShowCmd);
410
411 m_iconized = nShowCmd == SW_MINIMIZE;
412 }
413
414 bool wxTopLevelWindowMSW::Show(bool show)
415 {
416 // don't use wxWindow version as we want to call DoShowWindow() ourselves
417 if ( !wxWindowBase::Show(show) )
418 return FALSE;
419
420 int nShowCmd;
421 if ( show )
422 {
423 if ( m_maximizeOnShow )
424 {
425 // show and maximize
426 nShowCmd = SW_MAXIMIZE;
427
428 m_maximizeOnShow = FALSE;
429 }
430 else // just show
431 {
432 nShowCmd = SW_SHOW;
433 }
434 }
435 else // hide
436 {
437 nShowCmd = SW_HIDE;
438 }
439
440 DoShowWindow(nShowCmd);
441
442 if ( show )
443 {
444 ::BringWindowToTop(GetHwnd());
445
446 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, m_windowId);
447 event.SetEventObject( this );
448 GetEventHandler()->ProcessEvent(event);
449 }
450 else // hide
451 {
452 // Try to highlight the correct window (the parent)
453 if ( GetParent() )
454 {
455 HWND hWndParent = GetHwndOf(GetParent());
456 if (hWndParent)
457 ::BringWindowToTop(hWndParent);
458 }
459 }
460
461 return TRUE;
462 }
463
464 // ----------------------------------------------------------------------------
465 // wxTopLevelWindowMSW maximize/minimize
466 // ----------------------------------------------------------------------------
467
468 void wxTopLevelWindowMSW::Maximize(bool maximize)
469 {
470 if ( IsShown() )
471 {
472 // just maximize it directly
473 DoShowWindow(maximize ? SW_MAXIMIZE : SW_RESTORE);
474 }
475 else // hidden
476 {
477 // we can't maximize the hidden frame because it shows it as well, so
478 // just remember that we should do it later in this case
479 m_maximizeOnShow = TRUE;
480 }
481 }
482
483 bool wxTopLevelWindowMSW::IsMaximized() const
484 {
485 return ::IsZoomed(GetHwnd()) != 0;
486 }
487
488 void wxTopLevelWindowMSW::Iconize(bool iconize)
489 {
490 DoShowWindow(iconize ? SW_MINIMIZE : SW_RESTORE);
491 }
492
493 bool wxTopLevelWindowMSW::IsIconized() const
494 {
495 // also update the current state
496 ((wxTopLevelWindowMSW *)this)->m_iconized = ::IsIconic(GetHwnd()) != 0;
497
498 return m_iconized;
499 }
500
501 void wxTopLevelWindowMSW::Restore()
502 {
503 DoShowWindow(SW_RESTORE);
504 }
505
506 // ----------------------------------------------------------------------------
507 // wxTopLevelWindowMSW fullscreen
508 // ----------------------------------------------------------------------------
509
510 bool wxTopLevelWindowMSW::ShowFullScreen(bool show, long style)
511 {
512 if (show)
513 {
514 if (IsFullScreen())
515 return FALSE;
516
517 m_fsIsShowing = TRUE;
518 m_fsStyle = style;
519
520 // zap the frame borders
521
522 // save the 'normal' window style
523 m_fsOldWindowStyle = GetWindowLong((HWND)GetHWND(), GWL_STYLE);
524
525 // save the old position, width & height, maximize state
526 m_fsOldSize = GetRect();
527 m_fsIsMaximized = IsMaximized();
528
529 // decide which window style flags to turn off
530 LONG newStyle = m_fsOldWindowStyle;
531 LONG offFlags = 0;
532
533 if (style & wxFULLSCREEN_NOBORDER)
534 offFlags |= WS_BORDER | WS_THICKFRAME;
535 if (style & wxFULLSCREEN_NOCAPTION)
536 offFlags |= (WS_CAPTION | WS_SYSMENU);
537
538 newStyle &= (~offFlags);
539
540 // change our window style to be compatible with full-screen mode
541 ::SetWindowLong((HWND)GetHWND(), GWL_STYLE, newStyle);
542
543 // resize to the size of the desktop
544 int width, height;
545
546 RECT rect = wxGetWindowRect(::GetDesktopWindow());
547 width = rect.right - rect.left;
548 height = rect.bottom - rect.top;
549
550 SetSize(width, height);
551
552 // now flush the window style cache and actually go full-screen
553 SetWindowPos((HWND)GetHWND(), HWND_TOP, 0, 0, width, height, SWP_FRAMECHANGED);
554
555 wxSizeEvent event(wxSize(width, height), GetId());
556 GetEventHandler()->ProcessEvent(event);
557
558 return TRUE;
559 }
560 else
561 {
562 if (!IsFullScreen())
563 return FALSE;
564
565 m_fsIsShowing = FALSE;
566
567 Maximize(m_fsIsMaximized);
568 SetWindowLong((HWND)GetHWND(),GWL_STYLE, m_fsOldWindowStyle);
569 SetWindowPos((HWND)GetHWND(),HWND_TOP,m_fsOldSize.x, m_fsOldSize.y,
570 m_fsOldSize.width, m_fsOldSize.height, SWP_FRAMECHANGED);
571
572 return TRUE;
573 }
574 }
575
576 // ----------------------------------------------------------------------------
577 // wxTopLevelWindowMSW misc
578 // ----------------------------------------------------------------------------
579
580 void wxTopLevelWindowMSW::SetIcon(const wxIcon& icon)
581 {
582 // this sets m_icon
583 wxTopLevelWindowBase::SetIcon(icon);
584
585 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
586 if ( m_icon.Ok() )
587 {
588 ::SendMessage(GetHwnd(), WM_SETICON,
589 (WPARAM)TRUE, (LPARAM)GetHiconOf(m_icon));
590 }
591 #endif // __WIN95__
592 }