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