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