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