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