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