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