]> git.saurik.com Git - wxWidgets.git/blob - src/msw/toplevel.cpp
mingw compilation fix
[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
98 long wxTopLevelWindowMSW::MSWGetCreateWindowFlags(long *exflags) const
99 {
100 long style = GetWindowStyle();
101 long msflags = 0;
102
103 // first select the kind of window being created
104 if ( style & wxCAPTION )
105 {
106 if ( style & wxFRAME_TOOL_WINDOW )
107 msflags |= WS_POPUPWINDOW;
108 else
109 msflags |= WS_OVERLAPPED;
110 }
111 else
112 {
113 msflags |= WS_POPUP;
114 }
115
116 // next translate the individual flags
117 if ( style & wxMINIMIZE_BOX )
118 msflags |= WS_MINIMIZEBOX;
119 if ( style & wxMAXIMIZE_BOX )
120 msflags |= WS_MAXIMIZEBOX;
121 if ( style & wxTHICK_FRAME )
122 msflags |= WS_THICKFRAME;
123 if ( style & wxSYSTEM_MENU )
124 msflags |= WS_SYSMENU;
125 if ( style & wxMINIMIZE )
126 msflags |= WS_MINIMIZE;
127 if ( style & wxMAXIMIZE )
128 msflags |= WS_MAXIMIZE;
129 if ( style & wxCAPTION )
130 msflags |= WS_CAPTION;
131 if ( style & wxCLIP_CHILDREN )
132 msflags |= WS_CLIPCHILDREN;
133
134 // Keep this here because it saves recoding this function in wxTinyFrame
135 #if wxUSE_ITSY_BITSY && !defined(__WIN32__)
136 if ( style & wxTINY_CAPTION_VERT )
137 msflags |= IBS_VERTCAPTION;
138 if ( style & wxTINY_CAPTION_HORIZ )
139 msflags |= IBS_HORZCAPTION;
140 #else
141 if ( style & (wxTINY_CAPTION_VERT | wxTINY_CAPTION_HORIZ) )
142 msflags |= WS_CAPTION;
143 #endif
144
145 if ( exflags )
146 {
147 *exflags = MakeExtendedStyle(style);
148
149 // make all frames appear in the win9x shell taskbar unless
150 // wxFRAME_TOOL_WINDOW or wxFRAME_NO_TASKBAR is given - without giving
151 // them WS_EX_APPWINDOW style, the child (i.e. owned) frames wouldn't
152 // appear in it
153 #if !defined(__WIN16__) && !defined(__SC__)
154 if ( (style & wxFRAME_TOOL_WINDOW) || (style & wxFRAME_NO_TASKBAR) )
155 *exflags |= WS_EX_TOOLWINDOW;
156 else if ( !(style & wxFRAME_NO_TASKBAR) )
157 *exflags |= WS_EX_APPWINDOW;
158 #endif
159
160 if ( style & wxSTAY_ON_TOP )
161 *exflags |= WS_EX_TOPMOST;
162
163 #ifdef __WIN32__
164 if ( m_exStyle & wxFRAME_EX_CONTEXTHELP )
165 *exflags |= WS_EX_CONTEXTHELP;
166 #endif // __WIN32__
167 }
168
169 return msflags;
170 }
171
172 bool wxTopLevelWindowMSW::CreateDialog(const wxChar *dlgTemplate,
173 const wxString& title,
174 const wxPoint& pos,
175 const wxSize& size)
176 {
177 #ifdef __WXMICROWIN__
178 // no dialogs support under MicroWin yet
179 return CreateFrame(title, pos, size);
180 #else // !__WXMICROWIN__
181 wxWindow *parent = GetParent();
182
183 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
184 // app window as parent - this avoids creating modal dialogs without
185 // parent
186 if ( !parent && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) )
187 {
188 parent = wxTheApp->GetTopWindow();
189 }
190
191 m_hWnd = (WXHWND)::CreateDialog(wxGetInstance(),
192 dlgTemplate,
193 parent ? GetHwndOf(parent) : NULL,
194 (DLGPROC)wxDlgProc);
195
196 if ( !m_hWnd )
197 {
198 wxFAIL_MSG(_("Did you forget to include wx/msw/wx.rc in your resources?"));
199
200 wxLogSysError(_("Can't create dialog using template '%s'"), dlgTemplate);
201
202 return FALSE;
203 }
204
205 long exflags;
206 (void)MSWGetCreateWindowFlags(&exflags);
207
208 if ( exflags )
209 {
210 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE, exflags);
211 ::SetWindowPos(GetHwnd(), NULL, 0, 0, 0, 0,
212 SWP_NOSIZE |
213 SWP_NOMOVE |
214 SWP_NOZORDER |
215 SWP_NOACTIVATE);
216 }
217
218 #if defined(__WIN95__)
219 // For some reason, the system menu is activated when we use the
220 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
221 if ( exflags & WS_EX_CONTEXTHELP )
222 {
223 wxFrame *winTop = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
224 if ( winTop )
225 {
226 wxIcon icon = winTop->GetIcon();
227 if ( icon.Ok() )
228 {
229 ::SendMessage(GetHwnd(), WM_SETICON,
230 (WPARAM)TRUE,
231 (LPARAM)GetHiconOf(icon));
232 }
233 }
234 }
235 #endif // __WIN95__
236
237 // move the dialog to its initial position without forcing repainting
238 int x, y, w, h;
239 if ( MSWGetCreateWindowCoords(pos, size, x, y, w, h) )
240 {
241 if ( !::MoveWindow(GetHwnd(), x, y, w, h, FALSE) )
242 {
243 wxLogLastError(wxT("MoveWindow"));
244 }
245 }
246 //else: leave it at default position
247
248 if ( !title.empty() )
249 {
250 ::SetWindowText(GetHwnd(), title);
251 }
252
253 SubclassWin(m_hWnd);
254
255 return TRUE;
256 #endif // __WXMICROWIN__/!__WXMICROWIN__
257 }
258
259 bool wxTopLevelWindowMSW::CreateFrame(const wxString& title,
260 const wxPoint& pos,
261 const wxSize& size)
262 {
263 long exflags;
264 long flags = MSWGetCreateWindowFlags(&exflags);
265
266 return MSWCreate(wxCanvasClassName, title, pos, size, flags, exflags);
267 }
268
269 bool wxTopLevelWindowMSW::Create(wxWindow *parent,
270 wxWindowID id,
271 const wxString& title,
272 const wxPoint& pos,
273 const wxSize& size,
274 long style,
275 const wxString& name)
276 {
277 // init our fields
278 Init();
279
280 m_windowStyle = style;
281
282 SetName(name);
283
284 m_windowId = id == -1 ? NewControlId() : id;
285
286 wxTopLevelWindows.Append(this);
287
288 if ( parent )
289 parent->AddChild(this);
290
291 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
292 {
293 // TODO: it would be better to construct the dialog template in memory
294 // during run-time than to rely on the limited number of
295 // templates in wx.rc because:
296 // a) you wouldn't have to include wx.rc in all wxWin programs
297 // (and the number of complaints about it would dtop)
298 // b) we'd be able to provide more templates simply, i.e.
299 // we could generate the templates for all style
300 // combinations
301
302 // we have different dialog templates to allows creation of dialogs
303 // with & without captions under MSWindows, resizeable or not (but a
304 // resizeable dialog always has caption - otherwise it would look too
305 // strange)
306 const wxChar *dlgTemplate;
307 if ( style & wxRESIZE_BORDER )
308 dlgTemplate = wxT("wxResizeableDialog");
309 else if ( style & wxCAPTION )
310 dlgTemplate = wxT("wxCaptionDialog");
311 else
312 dlgTemplate = wxT("wxNoCaptionDialog");
313
314 return CreateDialog(dlgTemplate, title, pos, size);
315 }
316 else // !dialog
317 {
318 return CreateFrame(title, pos, size);
319 }
320 }
321
322 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
323 {
324 wxTopLevelWindows.DeleteObject(this);
325
326 if ( wxModelessWindows.Find(this) )
327 wxModelessWindows.DeleteObject(this);
328
329 // If this is the last top-level window, exit.
330 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
331 {
332 wxTheApp->SetTopWindow(NULL);
333
334 if ( wxTheApp->GetExitOnFrameDelete() )
335 {
336 ::PostQuitMessage(0);
337 }
338 }
339 }
340
341 // ----------------------------------------------------------------------------
342 // wxTopLevelWindowMSW geometry
343 // ----------------------------------------------------------------------------
344
345 void wxTopLevelWindowMSW::DoSetClientSize(int width, int height)
346 {
347 HWND hWnd = GetHwnd();
348
349 RECT rectClient;
350 ::GetClientRect(hWnd, &rectClient);
351
352 RECT rectTotal;
353 ::GetWindowRect(hWnd, &rectTotal);
354
355 // Find the difference between the entire window (title bar and all)
356 // and the client area; add this to the new client size to move the
357 // window
358 width += rectTotal.right - rectTotal.left - rectClient.right;
359 height += rectTotal.bottom - rectTotal.top - rectClient.bottom;
360
361 // note that calling GetClientAreaOrigin() takes the toolbar into account
362 wxPoint pt = GetClientAreaOrigin();
363 width += pt.x;
364 height += pt.y;
365
366 if ( !::MoveWindow(hWnd, rectTotal.left, rectTotal.top,
367 width, height, TRUE /* redraw */) )
368 {
369 wxLogLastError(_T("MoveWindow"));
370 }
371
372 wxSizeEvent event(wxSize(width, height), m_windowId);
373 event.SetEventObject(this);
374 (void)GetEventHandler()->ProcessEvent(event);
375 }
376
377 // ----------------------------------------------------------------------------
378 // wxTopLevelWindowMSW showing
379 // ----------------------------------------------------------------------------
380
381 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd)
382 {
383 ::ShowWindow(GetHwnd(), nShowCmd);
384
385 m_iconized = nShowCmd == SW_MINIMIZE;
386 }
387
388 bool wxTopLevelWindowMSW::Show(bool show)
389 {
390 // don't use wxWindow version as we want to call DoShowWindow() ourselves
391 if ( !wxWindowBase::Show(show) )
392 return FALSE;
393
394 int nShowCmd;
395 if ( show )
396 {
397 if ( m_maximizeOnShow )
398 {
399 // show and maximize
400 nShowCmd = SW_MAXIMIZE;
401
402 m_maximizeOnShow = FALSE;
403 }
404 else // just show
405 {
406 nShowCmd = SW_SHOW;
407 }
408 }
409 else // hide
410 {
411 nShowCmd = SW_HIDE;
412 }
413
414 DoShowWindow(nShowCmd);
415
416 if ( show )
417 {
418 ::BringWindowToTop(GetHwnd());
419
420 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, m_windowId);
421 event.SetEventObject( this );
422 GetEventHandler()->ProcessEvent(event);
423 }
424 else // hide
425 {
426 // Try to highlight the correct window (the parent)
427 if ( GetParent() )
428 {
429 HWND hWndParent = GetHwndOf(GetParent());
430 if (hWndParent)
431 ::BringWindowToTop(hWndParent);
432 }
433 }
434
435 return TRUE;
436 }
437
438 // ----------------------------------------------------------------------------
439 // wxTopLevelWindowMSW maximize/minimize
440 // ----------------------------------------------------------------------------
441
442 void wxTopLevelWindowMSW::Maximize(bool maximize)
443 {
444 if ( IsShown() )
445 {
446 // just maximize it directly
447 DoShowWindow(maximize ? SW_MAXIMIZE : SW_RESTORE);
448 }
449 else // hidden
450 {
451 // we can't maximize the hidden frame because it shows it as well, so
452 // just remember that we should do it later in this case
453 m_maximizeOnShow = TRUE;
454 }
455 }
456
457 bool wxTopLevelWindowMSW::IsMaximized() const
458 {
459 return ::IsZoomed(GetHwnd()) != 0;
460 }
461
462 void wxTopLevelWindowMSW::Iconize(bool iconize)
463 {
464 DoShowWindow(iconize ? SW_MINIMIZE : SW_RESTORE);
465 }
466
467 bool wxTopLevelWindowMSW::IsIconized() const
468 {
469 // also update the current state
470 ((wxTopLevelWindowMSW *)this)->m_iconized = ::IsIconic(GetHwnd()) != 0;
471
472 return m_iconized;
473 }
474
475 void wxTopLevelWindowMSW::Restore()
476 {
477 DoShowWindow(SW_RESTORE);
478 }
479
480 // ----------------------------------------------------------------------------
481 // wxTopLevelWindowMSW misc
482 // ----------------------------------------------------------------------------
483
484 void wxTopLevelWindowMSW::SetIcon(const wxIcon& icon)
485 {
486 // this sets m_icon
487 wxTopLevelWindowBase::SetIcon(icon);
488
489 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
490 if ( m_icon.Ok() )
491 {
492 ::SendMessage(GetHwnd(), WM_SETICON,
493 (WPARAM)TRUE, (LPARAM)GetHiconOf(m_icon));
494 }
495 #endif // __WIN95__
496 }