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