]> git.saurik.com Git - wxWidgets.git/blame - src/msw/frame.cpp
New global cursor behaviour.
[wxWidgets.git] / src / msw / frame.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: frame.cpp
3// Purpose: wxFrame
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "frame.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
9f3362c4 20 #pragma hdrstop
2bda0e17
KB
21#endif
22
23#ifndef WX_PRECOMP
9f3362c4
VZ
24 #include "wx/setup.h"
25 #include "wx/frame.h"
26 #include "wx/menu.h"
27 #include "wx/app.h"
28 #include "wx/utils.h"
29 #include "wx/dialog.h"
30 #include "wx/settings.h"
31 #include "wx/dcclient.h"
32#endif // WX_PRECOMP
2bda0e17
KB
33
34#include "wx/msw/private.h"
35#include "wx/statusbr.h"
81d66cf3 36#include "wx/toolbar.h"
2bda0e17 37#include "wx/menuitem.h"
6776a0b2 38#include "wx/log.h"
2bda0e17 39
47d67540 40#if wxUSE_NATIVE_STATUSBAR
9f3362c4 41 #include <wx/msw/statbr95.h>
2bda0e17
KB
42#endif
43
a23fd0e1 44extern wxWindowList wxModelessWindows;
cde9f08e 45extern wxList WXDLLEXPORT wxPendingDelete;
837e5743 46extern wxChar wxFrameClassName[];
e1a6fc11 47extern wxMenu *wxCurrentPopupMenu;
2bda0e17
KB
48
49#if !USE_SHARED_LIBRARY
50BEGIN_EVENT_TABLE(wxFrame, wxWindow)
51 EVT_SIZE(wxFrame::OnSize)
52 EVT_ACTIVATE(wxFrame::OnActivate)
53 EVT_MENU_HIGHLIGHT_ALL(wxFrame::OnMenuHighlight)
54 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged)
55 EVT_IDLE(wxFrame::OnIdle)
56 EVT_CLOSE(wxFrame::OnCloseWindow)
57END_EVENT_TABLE()
58
59IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow)
60#endif
61
47d67540 62#if wxUSE_NATIVE_STATUSBAR
9f3362c4 63 bool wxFrame::m_useNativeStatusBar = TRUE;
2bda0e17 64#else
9f3362c4 65 bool wxFrame::m_useNativeStatusBar = FALSE;
2bda0e17
KB
66#endif
67
bfc6fde4 68wxFrame::wxFrame()
2bda0e17 69{
73e7daa0 70 m_frameToolBar = NULL ;
2bda0e17
KB
71 m_frameMenuBar = NULL;
72 m_frameStatusBar = NULL;
73
2bda0e17
KB
74 m_iconized = FALSE;
75}
76
77bool wxFrame::Create(wxWindow *parent,
debe6624 78 wxWindowID id,
2bda0e17
KB
79 const wxString& title,
80 const wxPoint& pos,
81 const wxSize& size,
debe6624 82 long style,
2bda0e17
KB
83 const wxString& name)
84{
9f3362c4
VZ
85#if wxUSE_TOOLTIPS
86 m_hwndToolTip = 0;
87#endif
88
2bda0e17 89 SetName(name);
2bda0e17
KB
90 m_windowStyle = style;
91 m_frameMenuBar = NULL;
73e7daa0 92 m_frameToolBar = NULL ;
2bda0e17
KB
93 m_frameStatusBar = NULL;
94
95 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE));
96
97// m_icon = NULL;
98 if ( id > -1 )
99 m_windowId = id;
100 else
101 m_windowId = (int)NewControlId();
102
103 if (parent) parent->AddChild(this);
104
105 int x = pos.x;
106 int y = pos.y;
107 int width = size.x;
108 int height = size.y;
109
110 m_iconized = FALSE;
d2aef312
VZ
111
112 // we pass NULL as parent to MSWCreate because frames with parents behave
113 // very strangely under Win95 shell
aeab10d0
JS
114 // Alteration by JACS: keep normal Windows behaviour (float on top of parent)
115 // with this style.
116 if ((m_windowStyle & wxFRAME_FLOAT_ON_PARENT) == 0)
117 parent = NULL;
118
319fefa9
VZ
119 if (!parent)
120 wxTopLevelWindows.Append(this);
121
aeab10d0 122 MSWCreate(m_windowId, parent, wxFrameClassName, this, title,
d2aef312 123 x, y, width, height, style);
2bda0e17
KB
124
125 wxModelessWindows.Append(this);
126 return TRUE;
127}
128
bfc6fde4 129wxFrame::~wxFrame()
2bda0e17
KB
130{
131 m_isBeingDeleted = TRUE;
132 wxTopLevelWindows.DeleteObject(this);
133
134 if (m_frameStatusBar)
135 delete m_frameStatusBar;
136 if (m_frameMenuBar)
137 delete m_frameMenuBar;
138
139/* New behaviour March 1998: check if it's the last top-level window */
140// if (wxTheApp && (this == wxTheApp->GetTopWindow()))
141
142 if (wxTheApp && (wxTopLevelWindows.Number() == 0))
143 {
144 wxTheApp->SetTopWindow(NULL);
145
146 if (wxTheApp->GetExitOnFrameDelete())
147 {
148 PostQuitMessage(0);
149 }
150 }
151
152 wxModelessWindows.DeleteObject(this);
153
154 // For some reason, wxWindows can activate another task altogether
155 // when a frame is destroyed after a modal dialog has been invoked.
156 // Try to bring the parent to the top.
157 if (GetParent() && GetParent()->GetHWND())
158 ::BringWindowToTop((HWND) GetParent()->GetHWND());
159}
160
81d66cf3 161// Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
cc2b7472 162void wxFrame::DoGetClientSize(int *x, int *y) const
2bda0e17
KB
163{
164 RECT rect;
42e69d6b 165 ::GetClientRect(GetHwnd(), &rect);
2bda0e17 166
81d66cf3 167 if ( GetStatusBar() )
2bda0e17 168 {
81d66cf3
JS
169 int statusX, statusY;
170 GetStatusBar()->GetClientSize(&statusX, &statusY);
171 rect.bottom -= statusY;
2bda0e17 172 }
81d66cf3
JS
173
174 wxPoint pt(GetClientAreaOrigin());
175 rect.bottom -= pt.y;
176 rect.right -= pt.x;
177
2bda0e17
KB
178 *x = rect.right;
179 *y = rect.bottom;
180}
181
182// Set the client size (i.e. leave the calculation of borders etc.
183// to wxWindows)
bfc6fde4 184void wxFrame::DoSetClientSize(int width, int height)
2bda0e17 185{
42e69d6b 186 HWND hWnd = GetHwnd();
2bda0e17
KB
187
188 RECT rect;
2de8030d 189 ::GetClientRect(hWnd, &rect);
2bda0e17
KB
190
191 RECT rect2;
192 GetWindowRect(hWnd, &rect2);
193
194 // Find the difference between the entire window (title bar and all)
195 // and the client area; add this to the new client size to move the
196 // window
197 int actual_width = rect2.right - rect2.left - rect.right + width;
198 int actual_height = rect2.bottom - rect2.top - rect.bottom + height;
199
81d66cf3 200 if ( GetStatusBar() )
2bda0e17 201 {
81d66cf3
JS
202 int statusX, statusY;
203 GetStatusBar()->GetClientSize(&statusX, &statusY);
204 actual_height += statusY;
2bda0e17
KB
205 }
206
81d66cf3
JS
207 wxPoint pt(GetClientAreaOrigin());
208 actual_width += pt.y;
209 actual_height += pt.x;
210
2bda0e17
KB
211 POINT point;
212 point.x = rect2.left;
213 point.y = rect2.top;
214
215 MoveWindow(hWnd, point.x, point.y, actual_width, actual_height, (BOOL)TRUE);
debe6624 216
2bda0e17
KB
217 wxSizeEvent event(wxSize(width, height), m_windowId);
218 event.SetEventObject( this );
219 GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
220}
221
cc2b7472 222void wxFrame::DoGetSize(int *width, int *height) const
2bda0e17
KB
223{
224 RECT rect;
42e69d6b 225 GetWindowRect(GetHwnd(), &rect);
2bda0e17
KB
226 *width = rect.right - rect.left;
227 *height = rect.bottom - rect.top;
228}
229
cc2b7472 230void wxFrame::DoGetPosition(int *x, int *y) const
2bda0e17
KB
231{
232 RECT rect;
42e69d6b 233 GetWindowRect(GetHwnd(), &rect);
2bda0e17
KB
234 POINT point;
235 point.x = rect.left;
236 point.y = rect.top;
237
238 *x = point.x;
239 *y = point.y;
240}
241
bfc6fde4 242void wxFrame::DoSetSize(int x, int y, int width, int height, int sizeFlags)
2bda0e17 243{
4438caf4 244 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
2bda0e17 245
4438caf4
VZ
246 wxSizeEvent event(wxSize(width, height), m_windowId);
247 event.SetEventObject( this );
248 GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
249}
250
debe6624 251bool wxFrame::Show(bool show)
2bda0e17
KB
252{
253 int cshow;
254 if (show)
255 cshow = SW_SHOW;
256 else
257 cshow = SW_HIDE;
258
259 if (!show)
260 {
261 // Try to highlight the correct window (the parent)
262 HWND hWndParent = 0;
263 if (GetParent())
264 {
265 hWndParent = (HWND) GetParent()->GetHWND();
266 if (hWndParent)
267 ::BringWindowToTop(hWndParent);
268 }
269 }
270
42e69d6b 271 ShowWindow(GetHwnd(), (BOOL)cshow);
2bda0e17
KB
272 if (show)
273 {
42e69d6b 274 BringWindowToTop(GetHwnd());
2bda0e17 275
2bda0e17
KB
276 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, m_windowId);
277 event.SetEventObject( this );
278 GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
279 }
280 return TRUE;
281}
282
debe6624 283void wxFrame::Iconize(bool iconize)
2bda0e17
KB
284{
285 if (!iconize)
286 Show(TRUE);
287
288 int cshow;
289 if (iconize)
290 cshow = SW_MINIMIZE;
291 else
292 cshow = SW_RESTORE;
42e69d6b 293 ShowWindow(GetHwnd(), (BOOL)cshow);
2bda0e17
KB
294 m_iconized = iconize;
295}
296
297// Equivalent to maximize/restore in Windows
debe6624 298void wxFrame::Maximize(bool maximize)
2bda0e17
KB
299{
300 Show(TRUE);
301 int cshow;
302 if (maximize)
303 cshow = SW_MAXIMIZE;
304 else
305 cshow = SW_RESTORE;
42e69d6b 306 ShowWindow(GetHwnd(), cshow);
2bda0e17
KB
307 m_iconized = FALSE;
308}
309
bfc6fde4 310bool wxFrame::IsIconized() const
2bda0e17 311{
42e69d6b 312 ((wxFrame *)this)->m_iconized = (::IsIconic(GetHwnd()) != 0);
2bda0e17
KB
313 return m_iconized;
314}
315
6f63ec3f 316// Is it maximized?
bfc6fde4 317bool wxFrame::IsMaximized() const
6f63ec3f 318{
42e69d6b 319 return (::IsZoomed(GetHwnd()) != 0) ;
2bda0e17
KB
320}
321
322void wxFrame::SetIcon(const wxIcon& icon)
323{
324 m_icon = icon;
325#if defined(__WIN95__)
326 if ( m_icon.Ok() )
42e69d6b 327 SendMessage(GetHwnd(), WM_SETICON,
2bda0e17
KB
328 (WPARAM)TRUE, (LPARAM)(HICON) m_icon.GetHICON());
329#endif
330}
331
d427503c 332#if wxUSE_STATUSBAR
81d66cf3
JS
333wxStatusBar *wxFrame::OnCreateStatusBar(int number, long style, wxWindowID id,
334 const wxString& name)
2bda0e17
KB
335{
336 wxStatusBar *statusBar = NULL;
337
47d67540 338#if wxUSE_NATIVE_STATUSBAR
2bda0e17
KB
339 if (UsesNativeStatusBar())
340 {
81d66cf3 341 statusBar = new wxStatusBar95(this, id, style);
2bda0e17
KB
342 }
343 else
344#endif
345 {
81d66cf3
JS
346 statusBar = new wxStatusBar(this, id, wxPoint(0, 0), wxSize(100, 20),
347 style, name);
2bda0e17
KB
348
349 // Set the height according to the font and the border size
350 wxClientDC dc(statusBar);
c0ed460c 351 dc.SetFont(statusBar->GetFont());
2bda0e17
KB
352
353 long x, y;
354 dc.GetTextExtent("X", &x, &y);
355
356 int height = (int)( (y * 1.1) + 2* statusBar->GetBorderY());
357
358 statusBar->SetSize(-1, -1, 100, height);
359 }
360
361 statusBar->SetFieldsCount(number);
362 return statusBar;
363}
364
81d66cf3
JS
365wxStatusBar* wxFrame::CreateStatusBar(int number, long style, wxWindowID id,
366 const wxString& name)
2bda0e17
KB
367{
368 // VZ: calling CreateStatusBar twice is an error - why anyone would do it?
c2dcfdef 369 wxCHECK_MSG( m_frameStatusBar == NULL, FALSE,
837e5743 370 _T("recreating status bar in wxFrame") );
2bda0e17 371
81d66cf3
JS
372 m_frameStatusBar = OnCreateStatusBar(number, style, id,
373 name);
2bda0e17
KB
374 if ( m_frameStatusBar )
375 {
376 PositionStatusBar();
81d66cf3 377 return m_frameStatusBar;
2bda0e17
KB
378 }
379 else
81d66cf3 380 return NULL;
2bda0e17
KB
381}
382
debe6624 383void wxFrame::SetStatusText(const wxString& text, int number)
2bda0e17 384{
837e5743 385 wxCHECK_RET( m_frameStatusBar != NULL, _T("no statusbar to set text for") );
2bda0e17
KB
386
387 m_frameStatusBar->SetStatusText(text, number);
388}
389
8b9518ee 390void wxFrame::SetStatusWidths(int n, const int widths_field[])
2bda0e17 391{
837e5743 392 wxCHECK_RET( m_frameStatusBar != NULL, _T("no statusbar to set widths for") );
2bda0e17
KB
393
394 m_frameStatusBar->SetStatusWidths(n, widths_field);
395 PositionStatusBar();
396}
397
bfc6fde4 398void wxFrame::PositionStatusBar()
2bda0e17
KB
399{
400 // native status bar positions itself
401 if (m_frameStatusBar
47d67540 402#if wxUSE_NATIVE_STATUSBAR
2bda0e17
KB
403 && !m_frameStatusBar->IsKindOf(CLASSINFO(wxStatusBar95))
404#endif
405 )
406 {
407 int w, h;
408 GetClientSize(&w, &h);
409 int sw, sh;
410 m_frameStatusBar->GetSize(&sw, &sh);
81d66cf3
JS
411
412 // Since we wish the status bar to be directly under the client area,
413 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
2bda0e17
KB
414 m_frameStatusBar->SetSize(0, h, w, sh);
415 }
416}
d427503c 417#endif // wxUSE_STATUSBAR
2bda0e17
KB
418
419void wxFrame::SetMenuBar(wxMenuBar *menu_bar)
420{
c2dcfdef
VZ
421 if (!menu_bar)
422 {
423 m_frameMenuBar = NULL;
424 return;
425 }
2bda0e17 426
837e5743 427 wxCHECK_RET( !menu_bar->GetFrame(), _T("this menubar is already attached") );
2bda0e17 428
c2dcfdef
VZ
429 if (m_frameMenuBar)
430 delete m_frameMenuBar;
2bda0e17 431
c2dcfdef 432 m_hMenu = menu_bar->Create();
2bda0e17 433
c2dcfdef
VZ
434 if ( !m_hMenu )
435 return;
2bda0e17 436
42e69d6b 437 InternalSetMenuBar();
2bda0e17 438
c2dcfdef
VZ
439 m_frameMenuBar = menu_bar;
440 menu_bar->Attach(this);
2bda0e17
KB
441}
442
42e69d6b 443void wxFrame::InternalSetMenuBar()
2bda0e17 444{
42e69d6b 445 if ( !::SetMenu(GetHwnd(), (HMENU)m_hMenu) )
2bda0e17 446 {
42e69d6b 447 wxLogLastError("SetMenu");
2bda0e17 448 }
2bda0e17
KB
449}
450
451// Responds to colour changes, and passes event on to children.
452void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
453{
454 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE));
455 Refresh();
456
457 if ( m_frameStatusBar )
458 {
459 wxSysColourChangedEvent event2;
460 event2.SetEventObject( m_frameStatusBar );
02800301 461 m_frameStatusBar->GetEventHandler()->ProcessEvent(event2);
2bda0e17
KB
462 }
463
464 // Propagate the event to the non-top-level children
465 wxWindow::OnSysColourChanged(event);
466}
467
468/*
469 * Frame window
470 *
471 */
472
837e5743 473bool wxFrame::MSWCreate(int id, wxWindow *parent, const wxChar *wclass, wxWindow *wx_win, const wxChar *title,
debe6624 474 int x, int y, int width, int height, long style)
2bda0e17
KB
475
476{
477 m_defaultIcon = (WXHICON) (wxSTD_FRAME_ICON ? wxSTD_FRAME_ICON : wxDEFAULT_FRAME_ICON);
478
479 // If child windows aren't properly drawn initially, WS_CLIPCHILDREN
480 // could be the culprit. But without it, you can get a lot of flicker.
481
2bda0e17
KB
482 DWORD msflags = 0;
483 if ((style & wxCAPTION) == wxCAPTION)
1c089c47 484 msflags = WS_OVERLAPPED;
2bda0e17 485 else
1c089c47 486 msflags = WS_POPUP;
2bda0e17
KB
487
488 if (style & wxMINIMIZE_BOX)
489 msflags |= WS_MINIMIZEBOX;
490 if (style & wxMAXIMIZE_BOX)
491 msflags |= WS_MAXIMIZEBOX;
492 if (style & wxTHICK_FRAME)
493 msflags |= WS_THICKFRAME;
494 if (style & wxSYSTEM_MENU)
495 msflags |= WS_SYSMENU;
496 if ((style & wxMINIMIZE) || (style & wxICONIZE))
497 msflags |= WS_MINIMIZE;
498 if (style & wxMAXIMIZE)
499 msflags |= WS_MAXIMIZE;
500 if (style & wxCAPTION)
501 msflags |= WS_CAPTION;
1c089c47
JS
502 if (style & wxCLIP_CHILDREN)
503 msflags |= WS_CLIPCHILDREN;
2bda0e17
KB
504
505 // Keep this in wxFrame because it saves recoding this function
506 // in wxTinyFrame
47d67540 507#if wxUSE_ITSY_BITSY
2bda0e17
KB
508 if (style & wxTINY_CAPTION_VERT)
509 msflags |= IBS_VERTCAPTION;
510 if (style & wxTINY_CAPTION_HORIZ)
511 msflags |= IBS_HORZCAPTION;
512#else
513 if (style & wxTINY_CAPTION_VERT)
514 msflags |= WS_CAPTION;
515 if (style & wxTINY_CAPTION_HORIZ)
516 msflags |= WS_CAPTION;
517#endif
518 if ((style & wxTHICK_FRAME) == 0)
519 msflags |= WS_BORDER;
520
521 WXDWORD extendedStyle = MakeExtendedStyle(style);
522
2432b92d 523#if !defined(__WIN16__) && !defined(__SC__)
cd2df130
JS
524 if (style & wxFRAME_TOOL_WINDOW)
525 extendedStyle |= WS_EX_TOOLWINDOW;
1e6d9499 526#endif
cd2df130 527
2bda0e17
KB
528 if (style & wxSTAY_ON_TOP)
529 extendedStyle |= WS_EX_TOPMOST;
530
531 m_iconized = FALSE;
a23fd0e1
VZ
532 if ( !wxWindow::MSWCreate(id, parent, wclass, wx_win, title, x, y, width, height,
533 msflags, NULL, extendedStyle) )
534 return FALSE;
535
2bda0e17
KB
536 // Seems to be necessary if we use WS_POPUP
537 // style instead of WS_OVERLAPPED
538 if (width > -1 && height > -1)
42e69d6b 539 ::PostMessage(GetHwnd(), WM_SIZE, SIZE_RESTORED, MAKELPARAM(width, height));
a23fd0e1
VZ
540
541 return TRUE;
2bda0e17
KB
542}
543
a23fd0e1
VZ
544// Default resizing behaviour - if only ONE subwindow, resize to client
545// rectangle size
2bda0e17
KB
546void wxFrame::OnSize(wxSizeEvent& event)
547{
a23fd0e1
VZ
548 // if we're using constraints - do use them
549#if wxUSE_CONSTRAINTS
550 if ( GetAutoLayout() )
551 {
552 Layout();
553 return;
cd70477b 554 }
a23fd0e1 555#endif
cd70477b 556
a23fd0e1
VZ
557 // do we have _exactly_ one child?
558 wxWindow *child = NULL;
d427503c
VZ
559 for ( wxWindowList::Node *node = GetChildren().GetFirst();
560 node;
561 node = node->GetNext() )
2bda0e17 562 {
d427503c
VZ
563 wxWindow *win = node->GetData();
564 if ( !win->IsTopLevel()
565#if wxUSE_STATUSBAR
566 && (win != GetStatusBar())
567#endif // wxUSE_STATUSBAR
568#if wxUSE_TOOLBAR
569 && (win != GetToolBar())
570#endif // wxUSE_TOOLBAR
571 )
a23fd0e1
VZ
572 {
573 if ( child )
574 return; // it's our second subwindow - nothing to do
575 child = win;
576 }
2bda0e17 577 }
2bda0e17 578
a23fd0e1
VZ
579 if ( child ) {
580 // we have exactly one child - set it's size to fill the whole frame
581 int clientW, clientH;
582 GetClientSize(&clientW, &clientH);
2bda0e17 583
a23fd0e1
VZ
584 int x = 0;
585 int y = 0;
73e7daa0 586
a23fd0e1
VZ
587 child->SetSize(x, y, clientW, clientH);
588 }
2bda0e17
KB
589}
590
591// Default activation behaviour - set the focus for the first child
592// subwindow found.
593void wxFrame::OnActivate(wxActivateEvent& event)
594{
319fefa9
VZ
595 for ( wxWindowList::Node *node = GetChildren().GetFirst();
596 node;
597 node = node->GetNext() )
2bda0e17 598 {
319fefa9
VZ
599 // FIXME all this is totally bogus - we need to do the same as wxPanel,
600 // but how to do it without duplicating the code?
601
602 // restore focus
603 wxWindow *child = node->GetData();
604
605 if ( !child->IsTopLevel()
606#if wxUSE_TOOLBAR
607 && !wxDynamicCast(child, wxToolBar)
608#endif // wxUSE_TOOLBAR
609#if wxUSE_STATUSBAR
610 && !wxDynamicCast(child, wxStatusBar)
611#endif // wxUSE_STATUSBAR
612 )
613 {
614 child->SetFocus();
615 return;
616 }
2bda0e17 617 }
2bda0e17
KB
618}
619
e3065973 620// The default implementation for the close window event.
2bda0e17
KB
621void wxFrame::OnCloseWindow(wxCloseEvent& event)
622{
42e69d6b 623 Destroy();
47fa7969
JS
624}
625
2bda0e17 626// Destroy the window (delayed, if a managed window)
bfc6fde4 627bool wxFrame::Destroy()
2bda0e17
KB
628{
629 if (!wxPendingDelete.Member(this))
630 wxPendingDelete.Append(this);
631 return TRUE;
632}
633
634// Default menu selection behaviour - display a help string
635void wxFrame::OnMenuHighlight(wxMenuEvent& event)
636{
637 if (GetStatusBar())
638 {
d20eef8a
VZ
639 int menuId = event.GetMenuId();
640 if ( menuId != -1 )
2bda0e17
KB
641 {
642 wxMenuBar *menuBar = GetMenuBar();
58abfef6 643 if (menuBar && menuBar->FindItem(menuId))
2bda0e17 644 {
d20eef8a
VZ
645 // set status text even if the string is empty - this will at
646 // least remove the string from the item which was previously
647 // selected
648 SetStatusText(menuBar->GetHelpString(menuId));
2bda0e17
KB
649 }
650 }
651 }
652}
653
bfc6fde4 654wxMenuBar *wxFrame::GetMenuBar() const
2bda0e17
KB
655{
656 return m_frameMenuBar;
657}
658
42e69d6b 659bool wxFrame::ProcessCommand(int id)
2bda0e17 660{
42e69d6b
VZ
661 wxMenuBar *bar = GetMenuBar() ;
662 if ( !bar )
663 return FALSE;
2bda0e17 664
42e69d6b
VZ
665 wxMenuItem *item = bar->FindItemForId(id);
666 if ( !item )
667 return FALSE;
e702ff0f 668
42e69d6b
VZ
669 if ( item->IsCheckable() )
670 {
671 bar->Check(id, !bar->IsChecked(id)) ;
672 }
e702ff0f 673
42e69d6b
VZ
674 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
675 commandEvent.SetInt( id );
676 commandEvent.SetEventObject( this );
6c41a418 677
42e69d6b 678 return GetEventHandler()->ProcessEvent(commandEvent);
2bda0e17
KB
679}
680
81d66cf3
JS
681// Checks if there is a toolbar, and returns the first free client position
682wxPoint wxFrame::GetClientAreaOrigin() const
683{
684 wxPoint pt(0, 0);
685 if (GetToolBar())
686 {
687 int w, h;
688 GetToolBar()->GetSize(& w, & h);
689
690 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
691 {
692 pt.x += w;
693 }
694 else
695 {
696 pt.y += h;
697 }
698 }
699 return pt;
700}
701
87d1e11f
JS
702void wxFrame::ScreenToClient(int *x, int *y) const
703{
704 wxWindow::ScreenToClient(x, y);
705
706 // We may be faking the client origin.
707 // So a window that's really at (0, 30) may appear
708 // (to wxWin apps) to be at (0, 0).
709 wxPoint pt(GetClientAreaOrigin());
710 *x -= pt.x;
711 *y -= pt.y;
712}
713
714void wxFrame::ClientToScreen(int *x, int *y) const
715{
716 // We may be faking the client origin.
717 // So a window that's really at (0, 30) may appear
718 // (to wxWin apps) to be at (0, 0).
719 wxPoint pt1(GetClientAreaOrigin());
720 *x += pt1.x;
721 *y += pt1.y;
722
723 wxWindow::ClientToScreen(x, y);
724}
725
d427503c 726#if wxUSE_TOOLBAR
81d66cf3
JS
727wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
728{
729 wxCHECK_MSG( m_frameToolBar == NULL, FALSE,
837e5743 730 _T("recreating toolbar in wxFrame") );
81d66cf3
JS
731
732 wxToolBar* toolBar = OnCreateToolBar(style, id, name);
733 if (toolBar)
734 {
735 SetToolBar(toolBar);
736 PositionToolBar();
737 return toolBar;
738 }
739 else
740 {
741 return NULL;
742 }
743}
744
745wxToolBar* wxFrame::OnCreateToolBar(long style, wxWindowID id, const wxString& name)
746{
747 return new wxToolBar(this, id, wxDefaultPosition, wxDefaultSize, style, name);
748}
749
bfc6fde4 750void wxFrame::PositionToolBar()
81d66cf3 751{
81d66cf3 752 RECT rect;
42e69d6b 753 ::GetClientRect(GetHwnd(), &rect);
81d66cf3
JS
754
755 if ( GetStatusBar() )
756 {
757 int statusX, statusY;
758 GetStatusBar()->GetClientSize(&statusX, &statusY);
759 rect.bottom -= statusY;
760 }
761
762 if (GetToolBar())
763 {
764 int tw, th;
765 GetToolBar()->GetSize(& tw, & th);
766
767 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
768 {
769 // Use the 'real' MSW position
770 GetToolBar()->SetSize(0, 0, tw, rect.bottom, wxSIZE_NO_ADJUSTMENTS);
771 }
772 else
773 {
774 // Use the 'real' MSW position
775 GetToolBar()->SetSize(0, 0, rect.right, th, wxSIZE_NO_ADJUSTMENTS);
776 }
777 }
778}
d427503c 779#endif // wxUSE_TOOLBAR
d2aef312 780
a23fd0e1
VZ
781// propagate our state change to all child frames: this allows us to emulate X
782// Windows behaviour where child frames float independently of the parent one
783// on the desktop, but are iconized/restored with it
d2aef312
VZ
784void wxFrame::IconizeChildFrames(bool bIconize)
785{
a23fd0e1
VZ
786 for ( wxWindowList::Node *node = GetChildren().GetFirst();
787 node;
788 node = node->GetNext() )
789 {
790 wxWindow *win = node->GetData();
791
792 if ( win->IsKindOf(CLASSINFO(wxFrame)) )
793 {
794 ((wxFrame *)win)->Iconize(bIconize);
795 }
d2aef312 796 }
d2aef312
VZ
797}
798
b5423899
RD
799
800// make the window modal (all other windows unresponsive)
801void wxFrame::MakeModal(bool modal)
802{
803 if (modal) {
804 wxEnableTopLevelWindows(FALSE);
805 Enable(TRUE); // keep this window enabled
806 }
807 else {
808 wxEnableTopLevelWindows(TRUE);
809 }
810}
811
812
a23fd0e1 813// ===========================================================================
42e69d6b 814// message processing
a23fd0e1
VZ
815// ===========================================================================
816
42e69d6b
VZ
817// ---------------------------------------------------------------------------
818// preprocessing
819// ---------------------------------------------------------------------------
820
821bool wxFrame::MSWTranslateMessage(WXMSG* pMsg)
822{
823 if ( wxWindow::MSWTranslateMessage(pMsg) )
824 return TRUE;
825
826 // try the menu bar accels
827 wxMenuBar *menuBar = GetMenuBar();
828 if ( !menuBar )
829 return FALSE;
830
831 const wxAcceleratorTable& acceleratorTable = menuBar->GetAccelTable();
c50f1fb9 832 return acceleratorTable.Translate(this, pMsg);
42e69d6b
VZ
833}
834
835// ---------------------------------------------------------------------------
836// our private (non virtual) message handlers
837// ---------------------------------------------------------------------------
838
839bool wxFrame::HandlePaint()
840{
841 RECT rect;
842 if ( GetUpdateRect(GetHwnd(), &rect, FALSE) )
843 {
844 if ( m_iconized )
845 {
c50f1fb9 846 HICON hIcon = m_icon.Ok() ? GetHiconOf(m_icon)
42e69d6b
VZ
847 : (HICON)m_defaultIcon;
848
849 // Hold a pointer to the dc so long as the OnPaint() message
850 // is being processed
851 PAINTSTRUCT ps;
852 HDC hdc = ::BeginPaint(GetHwnd(), &ps);
853
854 // Erase background before painting or we get white background
855 MSWDefWindowProc(WM_ICONERASEBKGND, (WORD)(LONG)ps.hdc, 0L);
856
857 if ( hIcon )
858 {
859 RECT rect;
860 ::GetClientRect(GetHwnd(), &rect);
861
862 // FIXME: why hardcoded?
863 static const int icon_width = 32;
864 static const int icon_height = 32;
865
866 int icon_x = (int)((rect.right - icon_width)/2);
867 int icon_y = (int)((rect.bottom - icon_height)/2);
868
869 ::DrawIcon(hdc, icon_x, icon_y, hIcon);
870 }
871
872 ::EndPaint(GetHwnd(), &ps);
873
874 return TRUE;
875 }
876 else
877 {
878 wxPaintEvent event(m_windowId);
879 event.m_eventObject = this;
880
881 return GetEventHandler()->ProcessEvent(event);
882 }
883 }
884 else
885 {
886 // nothing to paint - processed
887 return TRUE;
888 }
889}
890
891bool wxFrame::HandleSize(int x, int y, WXUINT id)
892{
893 bool processed = FALSE;
894
895 switch ( id )
896 {
897 case SIZENORMAL:
898 // only do it it if we were iconized before, otherwise resizing the
899 // parent frame has a curious side effect of bringing it under it's
900 // children
901 if ( !m_iconized )
902 break;
903
904 // restore all child frames too
905 IconizeChildFrames(FALSE);
906
907 // fall through
908
909 case SIZEFULLSCREEN:
910 m_iconized = FALSE;
911 break;
912
913 case SIZEICONIC:
914 // iconize all child frames too
915 IconizeChildFrames(TRUE);
916
917 m_iconized = TRUE;
918 break;
919 }
920
921 if ( !m_iconized )
922 {
923 // forward WM_SIZE to status bar control
924#if wxUSE_NATIVE_STATUSBAR
925 if (m_frameStatusBar && m_frameStatusBar->IsKindOf(CLASSINFO(wxStatusBar95)))
926 {
927 wxSizeEvent event(wxSize(x, y), m_frameStatusBar->GetId());
928 event.SetEventObject( m_frameStatusBar );
929
930 ((wxStatusBar95 *)m_frameStatusBar)->OnSize(event);
931 }
932#endif // wxUSE_NATIVE_STATUSBAR
933
934 PositionStatusBar();
935 PositionToolBar();
936
937 wxSizeEvent event(wxSize(x, y), m_windowId);
938 event.SetEventObject( this );
939 processed = GetEventHandler()->ProcessEvent(event);
940 }
941
942 return processed;
943}
944
945bool wxFrame::HandleCommand(WXWORD id, WXWORD cmd, WXHWND control)
946{
947 if ( control )
948 {
949 // In case it's e.g. a toolbar.
950 wxWindow *win = wxFindWinFromHandle(control);
951 if ( win )
952 return win->MSWCommand(cmd, id);
953 }
954
955 // handle here commands from menus and accelerators
956 if ( cmd == 0 || cmd == 1 )
957 {
958 if ( wxCurrentPopupMenu )
959 {
960 wxMenu *popupMenu = wxCurrentPopupMenu;
961 wxCurrentPopupMenu = NULL;
962
963 return popupMenu->MSWCommand(cmd, id);
964 }
965
966 if ( ProcessCommand(id) )
967 {
968 return TRUE;
969 }
970 }
971
972 return FALSE;
973}
974
a23fd0e1
VZ
975bool wxFrame::HandleMenuSelect(WXWORD nItem, WXWORD nFlags, WXHMENU hMenu)
976{
977 int item;
978 if ( nFlags == 0xFFFF && hMenu == 0 )
979 {
980 // FIXME: what does this do? does it ever happen?
981 item = -1;
982 }
983 else if ((nFlags != MF_SEPARATOR) && (nItem != 0) && (nItem != 65535))
984 {
985 item = nItem;
986 }
987 else
988 {
989 return FALSE;
990 }
991
992 wxMenuEvent event(wxEVT_MENU_HIGHLIGHT, item);
993 event.SetEventObject( this );
994
995 return GetEventHandler()->ProcessEvent(event);
996}
997
998// ---------------------------------------------------------------------------
999// the window proc for wxFrame
1000// ---------------------------------------------------------------------------
1001
1002long wxFrame::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
1003{
1004 long rc = 0;
1005 bool processed = FALSE;
1006
1007 switch ( message )
1008 {
42e69d6b
VZ
1009 case WM_CLOSE:
1010 // if we can't close, tell the system that we processed the
1011 // message - otherwise it would close us
1012 processed = !Close();
1013 break;
1014
1015 case WM_COMMAND:
1016 {
1017 WORD id, cmd;
1018 WXHWND hwnd;
1019 UnpackCommand((WXWPARAM)wParam, (WXLPARAM)lParam,
1020 &id, &hwnd, &cmd);
1021
1022 processed = HandleCommand(id, cmd, (WXHWND)hwnd);
1023 }
1024 break;
1025
a23fd0e1
VZ
1026 case WM_MENUSELECT:
1027 {
42e69d6b
VZ
1028 WXWORD item, flags;
1029 WXHMENU hmenu;
1030 UnpackMenuSelect(wParam, lParam, &item, &flags, &hmenu);
1031
1032 processed = HandleMenuSelect(item, flags, hmenu);
a23fd0e1
VZ
1033 }
1034 break;
42e69d6b
VZ
1035
1036 case WM_PAINT:
1037 processed = HandlePaint();
1038 break;
1039
1040 case WM_QUERYDRAGICON:
1041 {
c50f1fb9 1042 HICON hIcon = m_icon.Ok() ? GetHiconOf(m_icon)
42e69d6b
VZ
1043 : (HICON)(m_defaultIcon);
1044 rc = (long)hIcon;
1045 processed = rc != 0;
1046 }
1047 break;
1048
1049 case WM_SIZE:
1050 processed = HandleSize(LOWORD(lParam), HIWORD(lParam), wParam);
1051 break;
a23fd0e1
VZ
1052 }
1053
1054 if ( !processed )
1055 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
1056
1057 return rc;
1058}