1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "frame.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
36 #include "wx/dialog.h"
37 #include "wx/settings.h"
38 #include "wx/dcclient.h"
43 #include "wx/msw/private.h"
50 #include "wx/statusbr.h"
51 #include "wx/generic/statusbr.h"
52 #endif // wxUSE_STATUSBAR
55 #include "wx/toolbar.h"
56 #endif // wxUSE_TOOLBAR
58 #include "wx/menuitem.h"
61 #ifdef __WXUNIVERSAL__
62 #include "wx/univ/theme.h"
63 #include "wx/univ/colschem.h"
64 #endif // __WXUNIVERSAL__
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 #if wxUSE_MENUS_NATIVE
71 extern wxMenu
*wxCurrentPopupMenu
;
72 #endif // wxUSE_MENUS_NATIVE
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 BEGIN_EVENT_TABLE(wxFrame
, wxFrameBase
)
79 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged
)
82 #if wxUSE_EXTENDED_RTTI
83 IMPLEMENT_DYNAMIC_CLASS_XTI(wxFrame
, wxTopLevelWindow
,"wx/frame.h")
85 WX_BEGIN_PROPERTIES_TABLE(wxFrame
)
89 style (wxDEFAULT_FRAME_STYLE)
90 centered (bool, false )
92 WX_END_PROPERTIES_TABLE()
94 WX_BEGIN_HANDLERS_TABLE(wxFrame
)
95 WX_END_HANDLERS_TABLE()
97 WX_CONSTRUCTOR_5( wxFrame
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Title
, wxPoint
, Position
, wxSize
, Size
)
100 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxTopLevelWindow
)
103 // ============================================================================
105 // ============================================================================
107 // ----------------------------------------------------------------------------
108 // static class members
109 // ----------------------------------------------------------------------------
112 #if wxUSE_NATIVE_STATUSBAR
113 bool wxFrame::m_useNativeStatusBar
= TRUE
;
115 bool wxFrame::m_useNativeStatusBar
= FALSE
;
117 #endif // wxUSE_NATIVE_STATUSBAR
119 // ----------------------------------------------------------------------------
120 // creation/destruction
121 // ----------------------------------------------------------------------------
129 // Data to save/restore when calling ShowFullScreen
130 m_fsStatusBarFields
= 0;
131 m_fsStatusBarHeight
= 0;
132 m_fsToolBarHeight
= 0;
134 m_wasMinimized
= FALSE
;
137 bool wxFrame::Create(wxWindow
*parent
,
139 const wxString
& title
,
143 const wxString
& name
)
145 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
148 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
150 wxModelessWindows
.Append(this);
157 m_isBeingDeleted
= TRUE
;
161 // ----------------------------------------------------------------------------
162 // wxFrame client size calculations
163 // ----------------------------------------------------------------------------
165 void wxFrame::DoSetClientSize(int width
, int height
)
167 // leave enough space for the status bar if we have (and show) it
169 wxStatusBar
*statbar
= GetStatusBar();
170 if ( statbar
&& statbar
->IsShown() )
172 height
+= statbar
->GetSize().y
;
174 #endif // wxUSE_STATUSBAR
176 // call GetClientAreaOrigin() to take the toolbar into account
177 wxPoint pt
= GetClientAreaOrigin();
181 wxTopLevelWindow::DoSetClientSize(width
, height
);
184 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
185 void wxFrame::DoGetClientSize(int *x
, int *y
) const
187 wxTopLevelWindow::DoGetClientSize(x
, y
);
189 // account for the possible toolbar
190 wxPoint pt
= GetClientAreaOrigin();
198 // adjust client area height to take the status bar into account
201 wxStatusBar
*statbar
= GetStatusBar();
202 if ( statbar
&& statbar
->IsShown() )
204 *y
-= statbar
->GetClientSize().y
;
207 #endif // wxUSE_STATUSBAR
210 // ----------------------------------------------------------------------------
211 // wxFrame: various geometry-related functions
212 // ----------------------------------------------------------------------------
214 void wxFrame::Raise()
216 ::SetForegroundWindow(GetHwnd());
219 // generate an artificial resize event
220 void wxFrame::SendSizeEvent()
224 RECT r
= wxGetWindowRect(GetHwnd());
226 (void)::PostMessage(GetHwnd(), WM_SIZE
,
227 IsMaximized() ? SIZE_MAXIMIZED
: SIZE_RESTORED
,
228 MAKELPARAM(r
.right
- r
.left
, r
.bottom
- r
.top
));
233 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
,
236 const wxString
& name
)
238 wxStatusBar
*statusBar
= NULL
;
240 #if wxUSE_NATIVE_STATUSBAR
241 if ( !UsesNativeStatusBar() )
243 statusBar
= (wxStatusBar
*)new wxStatusBarGeneric(this, id
, style
);
248 statusBar
= new wxStatusBar(this, id
, style
, name
);
251 statusBar
->SetFieldsCount(number
);
256 void wxFrame::PositionStatusBar()
258 if ( !m_frameStatusBar
|| !m_frameStatusBar
->IsShown() )
262 GetClientSize(&w
, &h
);
264 m_frameStatusBar
->GetSize(&sw
, &sh
);
266 // Since we wish the status bar to be directly under the client area,
267 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
268 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
270 #endif // wxUSE_STATUSBAR
272 #if wxUSE_MENUS_NATIVE
274 void wxFrame::AttachMenuBar(wxMenuBar
*menubar
)
276 wxFrameBase::AttachMenuBar(menubar
);
280 // actually remove the menu from the frame
281 m_hMenu
= (WXHMENU
)0;
282 InternalSetMenuBar();
284 else // set new non NULL menu bar
287 // Can set a menubar several times.
288 if ( menubar
->GetHMenu() )
290 m_hMenu
= menubar
->GetHMenu();
294 m_hMenu
= menubar
->Create();
298 wxFAIL_MSG( _T("failed to create menu bar") );
303 InternalSetMenuBar();
307 void wxFrame::InternalSetMenuBar()
309 #ifdef __WXMICROWIN__
311 #elif defined(__WXWINCE__)
315 wxToolBar
* toolBar
= new wxToolBar(this, -1,
316 wxDefaultPosition
, wxDefaultSize
,
317 wxBORDER_NONE
| wxTB_HORIZONTAL
,
318 wxToolBarNameStr
, GetMenuBar());
322 if ( !::SetMenu(GetHwnd(), (HMENU
)m_hMenu
) )
324 wxLogLastError(wxT("SetMenu"));
329 #endif // wxUSE_MENUS_NATIVE
331 // Responds to colour changes, and passes event on to children.
332 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
334 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
338 if ( m_frameStatusBar
)
340 wxSysColourChangedEvent event2
;
341 event2
.SetEventObject( m_frameStatusBar
);
342 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
344 #endif // wxUSE_STATUSBAR
346 // Propagate the event to the non-top-level children
347 wxWindow::OnSysColourChanged(event
);
350 // Pass TRUE to show full screen, FALSE to restore.
351 bool wxFrame::ShowFullScreen(bool show
, long style
)
353 if ( IsFullScreen() == show
)
360 // TODO: hide commandbar
362 wxToolBar
*theToolBar
= GetToolBar();
364 theToolBar
->GetSize(NULL
, &m_fsToolBarHeight
);
366 // zap the toolbar, menubar, and statusbar
368 if ((style
& wxFULLSCREEN_NOTOOLBAR
) && theToolBar
)
370 theToolBar
->SetSize(-1,0);
371 theToolBar
->Show(FALSE
);
373 #endif // __WXWINCE__
374 #endif // wxUSE_TOOLBAR
376 // TODO: make it work for WinCE
377 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
378 if (style
& wxFULLSCREEN_NOMENUBAR
)
379 SetMenu((HWND
)GetHWND(), (HMENU
) NULL
);
383 wxStatusBar
*theStatusBar
= GetStatusBar();
385 theStatusBar
->GetSize(NULL
, &m_fsStatusBarHeight
);
387 // Save the number of fields in the statusbar
388 if ((style
& wxFULLSCREEN_NOSTATUSBAR
) && theStatusBar
)
390 //m_fsStatusBarFields = theStatusBar->GetFieldsCount();
391 //SetStatusBar((wxStatusBar*) NULL);
392 //delete theStatusBar;
393 theStatusBar
->Show(FALSE
);
396 m_fsStatusBarFields
= 0;
397 #endif // wxUSE_STATUSBAR
403 // TODO: show commandbar
405 wxToolBar
*theToolBar
= GetToolBar();
407 // restore the toolbar, menubar, and statusbar
408 if (theToolBar
&& (m_fsStyle
& wxFULLSCREEN_NOTOOLBAR
))
410 theToolBar
->SetSize(-1, m_fsToolBarHeight
);
411 theToolBar
->Show(TRUE
);
413 #endif // __WXWINCE__
414 #endif // wxUSE_TOOLBAR
417 if ( m_fsStyle
& wxFULLSCREEN_NOSTATUSBAR
)
419 //CreateStatusBar(m_fsStatusBarFields);
422 GetStatusBar()->Show(TRUE
);
426 #endif // wxUSE_STATUSBAR
428 // TODO: make it work for WinCE
429 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
430 if ((m_fsStyle
& wxFULLSCREEN_NOMENUBAR
) && (m_hMenu
!= 0))
431 SetMenu((HWND
)GetHWND(), (HMENU
)m_hMenu
);
435 return wxFrameBase::ShowFullScreen(show
, style
);
438 // ----------------------------------------------------------------------------
439 // tool/status bar stuff
440 // ----------------------------------------------------------------------------
444 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
447 // We may already have a toolbar from calling SetMenuBar.
451 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
456 return m_frameToolBar
;
459 void wxFrame::PositionToolBar()
461 wxToolBar
*toolbar
= GetToolBar();
462 if ( toolbar
&& toolbar
->IsShown() )
465 // We want to do something different in WinCE, because
466 // the toolbar should be associated with the commandbar,
467 // and not an independent window.
470 // don't call our (or even wxTopLevelWindow) version because we want
471 // the real (full) client area size, not excluding the tool/status bar
473 wxWindow::DoGetClientSize(&width
, &height
);
476 wxStatusBar
*statbar
= GetStatusBar();
477 if ( statbar
&& statbar
->IsShown() )
479 height
-= statbar
->GetClientSize().y
;
481 #endif // wxUSE_STATUSBAR
485 toolbar
->GetPosition(&tx
, &ty
);
486 toolbar
->GetSize(&tw
, &th
);
489 if (ty
< 0 && (-ty
== th
))
491 if (tx
< 0 && (-tx
== tw
))
497 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
504 // if ( toolbar->GetWindowStyleFlag() & wxTB_FLAT )
508 // use the 'real' MSW position here, don't offset relativly to the
509 // client area origin
511 // Optimise such that we don't have to always resize the toolbar
512 // when the frame changes, otherwise we'll get a lot of flicker.
513 bool heightChanging
= TRUE
;
514 bool widthChanging
= TRUE
;
516 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
518 // It's OK if the current height is greater than what can be shown.
519 heightChanging
= (desiredH
> th
) ;
520 widthChanging
= (desiredW
!= tw
) ;
522 // The next time around, we may not have to set the size
524 desiredH
= desiredH
+ 200;
528 // It's OK if the current width is greater than what can be shown.
529 widthChanging
= (desiredW
> tw
) ;
530 heightChanging
= (desiredH
!= th
) ;
532 // The next time around, we may not have to set the size
534 desiredW
= desiredW
+ 200;
537 if (tx
!= 0 || ty
!= 0 || widthChanging
|| heightChanging
)
538 toolbar
->SetSize(0, 0, desiredW
, desiredH
, wxSIZE_NO_ADJUSTMENTS
);
540 #endif // __WXWINCE__
544 #endif // wxUSE_TOOLBAR
546 // ----------------------------------------------------------------------------
547 // frame state (iconized/maximized/...)
548 // ----------------------------------------------------------------------------
550 // propagate our state change to all child frames: this allows us to emulate X
551 // Windows behaviour where child frames float independently of the parent one
552 // on the desktop, but are iconized/restored with it
553 void wxFrame::IconizeChildFrames(bool bIconize
)
555 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
557 node
= node
->GetNext() )
559 wxWindow
*win
= node
->GetData();
561 // iconizing the frames with this style under Win95 shell puts them at
562 // the bottom of the screen (as the MDI children) instead of making
563 // them appear in the taskbar because they are, by virtue of this
564 // style, not managed by the taskbar - instead leave Windows take care
567 if ( win
->GetWindowStyle() & wxFRAME_TOOL_WINDOW
)
571 // the child MDI frames are a special case and should not be touched by
572 // the parent frame - instead, they are managed by the user
573 wxFrame
*frame
= wxDynamicCast(win
, wxFrame
);
575 #if wxUSE_MDI_ARCHITECTURE
576 && !wxDynamicCast(frame
, wxMDIChildFrame
)
577 #endif // wxUSE_MDI_ARCHITECTURE
580 // we don't want to restore the child frames which had been
581 // iconized even before we were iconized, so save the child frame
582 // status when iconizing the parent frame and check it when
586 // note that we shouldn't touch the hidden frames neither
587 // because iconizing/restoring them would show them as a side
589 frame
->m_wasMinimized
= frame
->IsIconized() || !frame
->IsShown();
592 // this test works for both iconizing and restoring
593 if ( !frame
->m_wasMinimized
)
594 frame
->Iconize(bIconize
);
599 WXHICON
wxFrame::GetDefaultIcon() const
601 // we don't have any standard icons (any more)
605 // ===========================================================================
606 // message processing
607 // ===========================================================================
609 // ---------------------------------------------------------------------------
611 // ---------------------------------------------------------------------------
613 bool wxFrame::MSWTranslateMessage(WXMSG
* pMsg
)
615 if ( wxWindow::MSWTranslateMessage(pMsg
) )
618 #if wxUSE_MENUS && wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
619 // try the menu bar accels
620 wxMenuBar
*menuBar
= GetMenuBar();
624 const wxAcceleratorTable
& acceleratorTable
= menuBar
->GetAccelTable();
625 return acceleratorTable
.Translate(this, pMsg
);
628 #endif // wxUSE_MENUS && wxUSE_ACCEL
631 // ---------------------------------------------------------------------------
632 // our private (non virtual) message handlers
633 // ---------------------------------------------------------------------------
635 bool wxFrame::HandlePaint()
638 if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) )
640 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
643 const wxIcon
& icon
= GetIcon();
644 HICON hIcon
= icon
.Ok() ? GetHiconOf(icon
)
645 : (HICON
)GetDefaultIcon();
647 // Hold a pointer to the dc so long as the OnPaint() message
648 // is being processed
650 HDC hdc
= ::BeginPaint(GetHwnd(), &ps
);
652 // Erase background before painting or we get white background
653 MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L);
658 ::GetClientRect(GetHwnd(), &rect
);
660 // FIXME: why hardcoded?
661 static const int icon_width
= 32;
662 static const int icon_height
= 32;
664 int icon_x
= (int)((rect
.right
- icon_width
)/2);
665 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
667 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
);
670 ::EndPaint(GetHwnd(), &ps
);
677 return wxWindow::HandlePaint();
682 // nothing to paint - processed
687 bool wxFrame::HandleSize(int x
, int y
, WXUINT id
)
689 bool processed
= FALSE
;
690 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
695 // only do it it if we were iconized before, otherwise resizing the
696 // parent frame has a curious side effect of bringing it under it's
701 // restore all child frames too
702 IconizeChildFrames(FALSE
);
704 (void)SendIconizeEvent(FALSE
);
713 // iconize all child frames too
714 IconizeChildFrames(TRUE
);
716 (void)SendIconizeEvent();
727 #endif // wxUSE_STATUSBAR
731 #endif // wxUSE_TOOLBAR
733 processed
= wxWindow::HandleSize(x
, y
, id
);
739 bool wxFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
)
743 // In case it's e.g. a toolbar.
744 wxWindow
*win
= wxFindWinFromHandle(control
);
746 return win
->MSWCommand(cmd
, id
);
749 // handle here commands from menus and accelerators
750 if ( cmd
== 0 || cmd
== 1 )
752 #if wxUSE_MENUS_NATIVE
753 if ( wxCurrentPopupMenu
)
755 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
756 wxCurrentPopupMenu
= NULL
;
758 return popupMenu
->MSWCommand(cmd
, id
);
760 #endif // wxUSE_MENUS_NATIVE
762 if ( ProcessCommand(id
) )
771 bool wxFrame::HandleMenuSelect(WXWORD nItem
, WXWORD flags
, WXHMENU hMenu
)
774 if ( flags
== 0xFFFF && hMenu
== 0 )
776 // menu was removed from screen
779 #ifndef __WXMICROWIN__
780 else if ( !(flags
& MF_POPUP
) && !(flags
& MF_SEPARATOR
) )
787 // don't give hints for separators (doesn't make sense) nor for the
788 // items opening popup menus (they don't have them anyhow) but do clear
789 // the status line - otherwise, we would be left with the help message
790 // for the previous item which doesn't apply any more
791 DoGiveHelp(wxEmptyString
, FALSE
);
796 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, item
);
797 event
.SetEventObject(this);
799 return GetEventHandler()->ProcessEvent(event
);
802 bool wxFrame::HandleMenuLoop(const wxEventType
& evtType
, WXWORD isPopup
)
804 // we don't have the menu id here, so we use the id to specify if the event
805 // was from a popup menu or a normal one
806 wxMenuEvent
event(evtType
, isPopup
? -1 : 0);
807 event
.SetEventObject(this);
809 return GetEventHandler()->ProcessEvent(event
);
812 // ---------------------------------------------------------------------------
813 // the window proc for wxFrame
814 // ---------------------------------------------------------------------------
816 long wxFrame::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
819 bool processed
= FALSE
;
824 // if we can't close, tell the system that we processed the
825 // message - otherwise it would close us
826 processed
= !Close();
830 processed
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
);
837 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
840 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
845 processed
= HandlePaint();
848 case WM_INITMENUPOPUP
:
849 processed
= HandleInitMenuPopup((WXHMENU
) wParam
);
852 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
857 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
859 processed
= HandleMenuSelect(item
, flags
, hmenu
);
863 case WM_EXITMENULOOP
:
864 processed
= HandleMenuLoop(wxEVT_MENU_CLOSE
, wParam
);
867 case WM_QUERYDRAGICON
:
869 const wxIcon
& icon
= GetIcon();
870 HICON hIcon
= icon
.Ok() ? GetHiconOf(icon
)
871 : (HICON
)GetDefaultIcon();
876 #endif // !__WXMICROWIN__
880 rc
= wxFrameBase::MSWWindowProc(message
, wParam
, lParam
);
885 // handle WM_INITMENUPOPUP message
886 bool wxFrame::HandleInitMenuPopup(WXHMENU hMenu
)
891 int nCount
= GetMenuBar()->GetMenuCount();
892 for (int n
= 0; n
< nCount
; n
++)
894 if (GetMenuBar()->GetMenu(n
)->GetHMenu() == hMenu
)
896 menu
= GetMenuBar()->GetMenu(n
);
902 wxMenuEvent
event(wxEVT_MENU_OPEN
, 0, menu
);
903 event
.SetEventObject(this);
905 return GetEventHandler()->ProcessEvent(event
);