]> git.saurik.com Git - wxWidgets.git/blame - src/msw/frame.cpp
*** empty log message ***
[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 {
c219cecc 639 wxString help;
d20eef8a
VZ
640 int menuId = event.GetMenuId();
641 if ( menuId != -1 )
2bda0e17
KB
642 {
643 wxMenuBar *menuBar = GetMenuBar();
58abfef6 644 if (menuBar && menuBar->FindItem(menuId))
2bda0e17 645 {
c219cecc 646 help = menuBar->GetHelpString(menuId);
2bda0e17
KB
647 }
648 }
c219cecc
VZ
649
650 // set status text even if the string is empty - this will at
651 // least remove the string from the item which was previously
652 // selected
653 SetStatusText(help);
2bda0e17
KB
654 }
655}
656
bfc6fde4 657wxMenuBar *wxFrame::GetMenuBar() const
2bda0e17
KB
658{
659 return m_frameMenuBar;
660}
661
42e69d6b 662bool wxFrame::ProcessCommand(int id)
2bda0e17 663{
42e69d6b
VZ
664 wxMenuBar *bar = GetMenuBar() ;
665 if ( !bar )
666 return FALSE;
2bda0e17 667
42e69d6b
VZ
668 wxMenuItem *item = bar->FindItemForId(id);
669 if ( !item )
670 return FALSE;
e702ff0f 671
42e69d6b
VZ
672 if ( item->IsCheckable() )
673 {
674 bar->Check(id, !bar->IsChecked(id)) ;
675 }
e702ff0f 676
42e69d6b
VZ
677 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
678 commandEvent.SetInt( id );
679 commandEvent.SetEventObject( this );
6c41a418 680
42e69d6b 681 return GetEventHandler()->ProcessEvent(commandEvent);
2bda0e17
KB
682}
683
81d66cf3
JS
684// Checks if there is a toolbar, and returns the first free client position
685wxPoint wxFrame::GetClientAreaOrigin() const
686{
687 wxPoint pt(0, 0);
688 if (GetToolBar())
689 {
690 int w, h;
691 GetToolBar()->GetSize(& w, & h);
692
693 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
694 {
695 pt.x += w;
696 }
697 else
698 {
699 pt.y += h;
700 }
701 }
702 return pt;
703}
704
87d1e11f
JS
705void wxFrame::ScreenToClient(int *x, int *y) const
706{
707 wxWindow::ScreenToClient(x, y);
708
709 // We may be faking the client origin.
710 // So a window that's really at (0, 30) may appear
711 // (to wxWin apps) to be at (0, 0).
712 wxPoint pt(GetClientAreaOrigin());
713 *x -= pt.x;
714 *y -= pt.y;
715}
716
717void wxFrame::ClientToScreen(int *x, int *y) const
718{
719 // We may be faking the client origin.
720 // So a window that's really at (0, 30) may appear
721 // (to wxWin apps) to be at (0, 0).
722 wxPoint pt1(GetClientAreaOrigin());
723 *x += pt1.x;
724 *y += pt1.y;
725
726 wxWindow::ClientToScreen(x, y);
727}
728
d427503c 729#if wxUSE_TOOLBAR
81d66cf3
JS
730wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
731{
732 wxCHECK_MSG( m_frameToolBar == NULL, FALSE,
837e5743 733 _T("recreating toolbar in wxFrame") );
81d66cf3
JS
734
735 wxToolBar* toolBar = OnCreateToolBar(style, id, name);
736 if (toolBar)
737 {
738 SetToolBar(toolBar);
739 PositionToolBar();
740 return toolBar;
741 }
742 else
743 {
744 return NULL;
745 }
746}
747
748wxToolBar* wxFrame::OnCreateToolBar(long style, wxWindowID id, const wxString& name)
749{
750 return new wxToolBar(this, id, wxDefaultPosition, wxDefaultSize, style, name);
751}
752
bfc6fde4 753void wxFrame::PositionToolBar()
81d66cf3 754{
81d66cf3 755 RECT rect;
42e69d6b 756 ::GetClientRect(GetHwnd(), &rect);
81d66cf3
JS
757
758 if ( GetStatusBar() )
759 {
760 int statusX, statusY;
761 GetStatusBar()->GetClientSize(&statusX, &statusY);
762 rect.bottom -= statusY;
763 }
764
765 if (GetToolBar())
766 {
767 int tw, th;
768 GetToolBar()->GetSize(& tw, & th);
769
770 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
771 {
772 // Use the 'real' MSW position
773 GetToolBar()->SetSize(0, 0, tw, rect.bottom, wxSIZE_NO_ADJUSTMENTS);
774 }
775 else
776 {
777 // Use the 'real' MSW position
778 GetToolBar()->SetSize(0, 0, rect.right, th, wxSIZE_NO_ADJUSTMENTS);
779 }
780 }
781}
d427503c 782#endif // wxUSE_TOOLBAR
d2aef312 783
a23fd0e1
VZ
784// propagate our state change to all child frames: this allows us to emulate X
785// Windows behaviour where child frames float independently of the parent one
786// on the desktop, but are iconized/restored with it
d2aef312
VZ
787void wxFrame::IconizeChildFrames(bool bIconize)
788{
a23fd0e1
VZ
789 for ( wxWindowList::Node *node = GetChildren().GetFirst();
790 node;
791 node = node->GetNext() )
792 {
793 wxWindow *win = node->GetData();
794
795 if ( win->IsKindOf(CLASSINFO(wxFrame)) )
796 {
797 ((wxFrame *)win)->Iconize(bIconize);
798 }
d2aef312 799 }
d2aef312
VZ
800}
801
b5423899
RD
802
803// make the window modal (all other windows unresponsive)
804void wxFrame::MakeModal(bool modal)
805{
806 if (modal) {
807 wxEnableTopLevelWindows(FALSE);
808 Enable(TRUE); // keep this window enabled
809 }
810 else {
811 wxEnableTopLevelWindows(TRUE);
812 }
813}
814
815
a23fd0e1 816// ===========================================================================
42e69d6b 817// message processing
a23fd0e1
VZ
818// ===========================================================================
819
42e69d6b
VZ
820// ---------------------------------------------------------------------------
821// preprocessing
822// ---------------------------------------------------------------------------
823
824bool wxFrame::MSWTranslateMessage(WXMSG* pMsg)
825{
826 if ( wxWindow::MSWTranslateMessage(pMsg) )
827 return TRUE;
828
829 // try the menu bar accels
830 wxMenuBar *menuBar = GetMenuBar();
831 if ( !menuBar )
832 return FALSE;
833
834 const wxAcceleratorTable& acceleratorTable = menuBar->GetAccelTable();
c50f1fb9 835 return acceleratorTable.Translate(this, pMsg);
42e69d6b
VZ
836}
837
838// ---------------------------------------------------------------------------
839// our private (non virtual) message handlers
840// ---------------------------------------------------------------------------
841
842bool wxFrame::HandlePaint()
843{
844 RECT rect;
845 if ( GetUpdateRect(GetHwnd(), &rect, FALSE) )
846 {
847 if ( m_iconized )
848 {
c50f1fb9 849 HICON hIcon = m_icon.Ok() ? GetHiconOf(m_icon)
42e69d6b
VZ
850 : (HICON)m_defaultIcon;
851
852 // Hold a pointer to the dc so long as the OnPaint() message
853 // is being processed
854 PAINTSTRUCT ps;
855 HDC hdc = ::BeginPaint(GetHwnd(), &ps);
856
857 // Erase background before painting or we get white background
858 MSWDefWindowProc(WM_ICONERASEBKGND, (WORD)(LONG)ps.hdc, 0L);
859
860 if ( hIcon )
861 {
862 RECT rect;
863 ::GetClientRect(GetHwnd(), &rect);
864
865 // FIXME: why hardcoded?
866 static const int icon_width = 32;
867 static const int icon_height = 32;
868
869 int icon_x = (int)((rect.right - icon_width)/2);
870 int icon_y = (int)((rect.bottom - icon_height)/2);
871
872 ::DrawIcon(hdc, icon_x, icon_y, hIcon);
873 }
874
875 ::EndPaint(GetHwnd(), &ps);
876
877 return TRUE;
878 }
879 else
880 {
881 wxPaintEvent event(m_windowId);
882 event.m_eventObject = this;
883
884 return GetEventHandler()->ProcessEvent(event);
885 }
886 }
887 else
888 {
889 // nothing to paint - processed
890 return TRUE;
891 }
892}
893
894bool wxFrame::HandleSize(int x, int y, WXUINT id)
895{
896 bool processed = FALSE;
897
898 switch ( id )
899 {
900 case SIZENORMAL:
901 // only do it it if we were iconized before, otherwise resizing the
902 // parent frame has a curious side effect of bringing it under it's
903 // children
904 if ( !m_iconized )
905 break;
906
907 // restore all child frames too
908 IconizeChildFrames(FALSE);
909
910 // fall through
911
912 case SIZEFULLSCREEN:
913 m_iconized = FALSE;
914 break;
915
916 case SIZEICONIC:
917 // iconize all child frames too
918 IconizeChildFrames(TRUE);
919
920 m_iconized = TRUE;
921 break;
922 }
923
924 if ( !m_iconized )
925 {
926 // forward WM_SIZE to status bar control
927#if wxUSE_NATIVE_STATUSBAR
928 if (m_frameStatusBar && m_frameStatusBar->IsKindOf(CLASSINFO(wxStatusBar95)))
929 {
930 wxSizeEvent event(wxSize(x, y), m_frameStatusBar->GetId());
931 event.SetEventObject( m_frameStatusBar );
932
933 ((wxStatusBar95 *)m_frameStatusBar)->OnSize(event);
934 }
935#endif // wxUSE_NATIVE_STATUSBAR
936
937 PositionStatusBar();
938 PositionToolBar();
939
940 wxSizeEvent event(wxSize(x, y), m_windowId);
941 event.SetEventObject( this );
942 processed = GetEventHandler()->ProcessEvent(event);
943 }
944
945 return processed;
946}
947
948bool wxFrame::HandleCommand(WXWORD id, WXWORD cmd, WXHWND control)
949{
950 if ( control )
951 {
952 // In case it's e.g. a toolbar.
953 wxWindow *win = wxFindWinFromHandle(control);
954 if ( win )
955 return win->MSWCommand(cmd, id);
956 }
957
958 // handle here commands from menus and accelerators
959 if ( cmd == 0 || cmd == 1 )
960 {
961 if ( wxCurrentPopupMenu )
962 {
963 wxMenu *popupMenu = wxCurrentPopupMenu;
964 wxCurrentPopupMenu = NULL;
965
966 return popupMenu->MSWCommand(cmd, id);
967 }
968
969 if ( ProcessCommand(id) )
970 {
971 return TRUE;
972 }
973 }
974
975 return FALSE;
976}
977
c219cecc 978bool wxFrame::HandleMenuSelect(WXWORD nItem, WXWORD flags, WXHMENU hMenu)
a23fd0e1
VZ
979{
980 int item;
c219cecc 981 if ( flags == 0xFFFF && hMenu == 0 )
a23fd0e1 982 {
c219cecc 983 // menu was removed from screen
a23fd0e1
VZ
984 item = -1;
985 }
c219cecc 986 else if ( !(flags & MF_POPUP) && !(flags & MF_SEPARATOR) )
a23fd0e1
VZ
987 {
988 item = nItem;
989 }
990 else
991 {
c219cecc
VZ
992 // don't give hints for separators (doesn't make sense) nor for the
993 // items opening popup menus (they don't have them anyhow)
a23fd0e1
VZ
994 return FALSE;
995 }
996
997 wxMenuEvent event(wxEVT_MENU_HIGHLIGHT, item);
998 event.SetEventObject( this );
999
1000 return GetEventHandler()->ProcessEvent(event);
1001}
1002
1003// ---------------------------------------------------------------------------
1004// the window proc for wxFrame
1005// ---------------------------------------------------------------------------
1006
1007long wxFrame::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
1008{
1009 long rc = 0;
1010 bool processed = FALSE;
1011
1012 switch ( message )
1013 {
42e69d6b
VZ
1014 case WM_CLOSE:
1015 // if we can't close, tell the system that we processed the
1016 // message - otherwise it would close us
1017 processed = !Close();
1018 break;
1019
1020 case WM_COMMAND:
1021 {
1022 WORD id, cmd;
1023 WXHWND hwnd;
1024 UnpackCommand((WXWPARAM)wParam, (WXLPARAM)lParam,
1025 &id, &hwnd, &cmd);
1026
1027 processed = HandleCommand(id, cmd, (WXHWND)hwnd);
1028 }
1029 break;
1030
a23fd0e1
VZ
1031 case WM_MENUSELECT:
1032 {
42e69d6b
VZ
1033 WXWORD item, flags;
1034 WXHMENU hmenu;
1035 UnpackMenuSelect(wParam, lParam, &item, &flags, &hmenu);
1036
1037 processed = HandleMenuSelect(item, flags, hmenu);
a23fd0e1
VZ
1038 }
1039 break;
42e69d6b
VZ
1040
1041 case WM_PAINT:
1042 processed = HandlePaint();
1043 break;
1044
1045 case WM_QUERYDRAGICON:
1046 {
c50f1fb9 1047 HICON hIcon = m_icon.Ok() ? GetHiconOf(m_icon)
42e69d6b
VZ
1048 : (HICON)(m_defaultIcon);
1049 rc = (long)hIcon;
1050 processed = rc != 0;
1051 }
1052 break;
1053
1054 case WM_SIZE:
1055 processed = HandleSize(LOWORD(lParam), HIWORD(lParam), wParam);
1056 break;
a23fd0e1
VZ
1057 }
1058
1059 if ( !processed )
1060 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
1061
1062 return rc;
1063}