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