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