1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
)
86 WX_PROPERTY( Title
,wxString
, SetTitle
, GetTitle
, wxT("") )
90 style (wxDEFAULT_FRAME_STYLE)
91 centered (bool, false )
93 WX_END_PROPERTIES_TABLE()
95 WX_BEGIN_HANDLERS_TABLE(wxFrame
)
96 WX_END_HANDLERS_TABLE()
98 WX_CONSTRUCTOR_6( wxFrame
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Title
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
101 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxTopLevelWindow
)
104 // ============================================================================
106 // ============================================================================
108 // ----------------------------------------------------------------------------
109 // static class members
110 // ----------------------------------------------------------------------------
113 #if wxUSE_NATIVE_STATUSBAR
114 bool wxFrame::m_useNativeStatusBar
= TRUE
;
116 bool wxFrame::m_useNativeStatusBar
= FALSE
;
118 #endif // wxUSE_NATIVE_STATUSBAR
120 // ----------------------------------------------------------------------------
121 // creation/destruction
122 // ----------------------------------------------------------------------------
130 // Data to save/restore when calling ShowFullScreen
131 m_fsStatusBarFields
= 0;
132 m_fsStatusBarHeight
= 0;
133 m_fsToolBarHeight
= 0;
135 m_wasMinimized
= FALSE
;
138 bool wxFrame::Create(wxWindow
*parent
,
140 const wxString
& title
,
144 const wxString
& name
)
146 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
149 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
151 wxModelessWindows
.Append(this);
158 m_isBeingDeleted
= TRUE
;
162 // ----------------------------------------------------------------------------
163 // wxFrame client size calculations
164 // ----------------------------------------------------------------------------
166 void wxFrame::DoSetClientSize(int width
, int height
)
168 // leave enough space for the status bar if we have (and show) it
170 wxStatusBar
*statbar
= GetStatusBar();
171 if ( statbar
&& statbar
->IsShown() )
173 height
+= statbar
->GetSize().y
;
175 #endif // wxUSE_STATUSBAR
177 // call GetClientAreaOrigin() to take the toolbar into account
178 wxPoint pt
= GetClientAreaOrigin();
182 wxTopLevelWindow::DoSetClientSize(width
, height
);
185 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
186 void wxFrame::DoGetClientSize(int *x
, int *y
) const
188 wxTopLevelWindow::DoGetClientSize(x
, y
);
190 // account for the possible toolbar
191 wxPoint pt
= GetClientAreaOrigin();
199 // adjust client area height to take the status bar into account
202 wxStatusBar
*statbar
= GetStatusBar();
203 if ( statbar
&& statbar
->IsShown() )
205 *y
-= statbar
->GetClientSize().y
;
208 #endif // wxUSE_STATUSBAR
211 // ----------------------------------------------------------------------------
212 // wxFrame: various geometry-related functions
213 // ----------------------------------------------------------------------------
215 void wxFrame::Raise()
217 ::SetForegroundWindow(GetHwnd());
220 // generate an artificial resize event
221 void wxFrame::SendSizeEvent()
225 RECT r
= wxGetWindowRect(GetHwnd());
227 (void)::PostMessage(GetHwnd(), WM_SIZE
,
228 IsMaximized() ? SIZE_MAXIMIZED
: SIZE_RESTORED
,
229 MAKELPARAM(r
.right
- r
.left
, r
.bottom
- r
.top
));
234 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
,
237 const wxString
& name
)
239 wxStatusBar
*statusBar
= NULL
;
241 #if wxUSE_NATIVE_STATUSBAR
242 if ( !UsesNativeStatusBar() )
244 statusBar
= (wxStatusBar
*)new wxStatusBarGeneric(this, id
, style
);
249 statusBar
= new wxStatusBar(this, id
, style
, name
);
252 statusBar
->SetFieldsCount(number
);
257 void wxFrame::PositionStatusBar()
259 if ( !m_frameStatusBar
|| !m_frameStatusBar
->IsShown() )
263 GetClientSize(&w
, &h
);
265 m_frameStatusBar
->GetSize(&sw
, &sh
);
267 // Since we wish the status bar to be directly under the client area,
268 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
269 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
271 #endif // wxUSE_STATUSBAR
273 #if wxUSE_MENUS_NATIVE
275 void wxFrame::AttachMenuBar(wxMenuBar
*menubar
)
277 wxFrameBase::AttachMenuBar(menubar
);
281 // actually remove the menu from the frame
282 m_hMenu
= (WXHMENU
)0;
283 InternalSetMenuBar();
285 else // set new non NULL menu bar
288 // Can set a menubar several times.
289 if ( menubar
->GetHMenu() )
291 m_hMenu
= menubar
->GetHMenu();
295 m_hMenu
= menubar
->Create();
299 wxFAIL_MSG( _T("failed to create menu bar") );
304 InternalSetMenuBar();
308 void wxFrame::InternalSetMenuBar()
310 #ifdef __WXMICROWIN__
312 #elif defined(__WXWINCE__)
316 wxToolBar
* toolBar
= new wxToolBar(this, -1,
317 wxDefaultPosition
, wxDefaultSize
,
318 wxBORDER_NONE
| wxTB_HORIZONTAL
,
319 wxToolBarNameStr
, GetMenuBar());
323 if ( !::SetMenu(GetHwnd(), (HMENU
)m_hMenu
) )
325 wxLogLastError(wxT("SetMenu"));
330 #endif // wxUSE_MENUS_NATIVE
332 // Responds to colour changes, and passes event on to children.
333 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
335 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
339 if ( m_frameStatusBar
)
341 wxSysColourChangedEvent event2
;
342 event2
.SetEventObject( m_frameStatusBar
);
343 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
345 #endif // wxUSE_STATUSBAR
347 // Propagate the event to the non-top-level children
348 wxWindow::OnSysColourChanged(event
);
351 // Pass TRUE to show full screen, FALSE to restore.
352 bool wxFrame::ShowFullScreen(bool show
, long style
)
354 if ( IsFullScreen() == show
)
361 // TODO: hide commandbar
363 wxToolBar
*theToolBar
= GetToolBar();
365 theToolBar
->GetSize(NULL
, &m_fsToolBarHeight
);
367 // zap the toolbar, menubar, and statusbar
369 if ((style
& wxFULLSCREEN_NOTOOLBAR
) && theToolBar
)
371 theToolBar
->SetSize(-1,0);
372 theToolBar
->Show(FALSE
);
374 #endif // __WXWINCE__
375 #endif // wxUSE_TOOLBAR
377 // TODO: make it work for WinCE
378 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
379 if (style
& wxFULLSCREEN_NOMENUBAR
)
380 SetMenu((HWND
)GetHWND(), (HMENU
) NULL
);
384 wxStatusBar
*theStatusBar
= GetStatusBar();
386 theStatusBar
->GetSize(NULL
, &m_fsStatusBarHeight
);
388 // Save the number of fields in the statusbar
389 if ((style
& wxFULLSCREEN_NOSTATUSBAR
) && theStatusBar
)
391 //m_fsStatusBarFields = theStatusBar->GetFieldsCount();
392 //SetStatusBar((wxStatusBar*) NULL);
393 //delete theStatusBar;
394 theStatusBar
->Show(FALSE
);
397 m_fsStatusBarFields
= 0;
398 #endif // wxUSE_STATUSBAR
404 // TODO: show commandbar
406 wxToolBar
*theToolBar
= GetToolBar();
408 // restore the toolbar, menubar, and statusbar
409 if (theToolBar
&& (m_fsStyle
& wxFULLSCREEN_NOTOOLBAR
))
411 theToolBar
->SetSize(-1, m_fsToolBarHeight
);
412 theToolBar
->Show(TRUE
);
414 #endif // __WXWINCE__
415 #endif // wxUSE_TOOLBAR
418 if ( m_fsStyle
& wxFULLSCREEN_NOSTATUSBAR
)
420 //CreateStatusBar(m_fsStatusBarFields);
423 GetStatusBar()->Show(TRUE
);
427 #endif // wxUSE_STATUSBAR
429 // TODO: make it work for WinCE
430 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
431 if ((m_fsStyle
& wxFULLSCREEN_NOMENUBAR
) && (m_hMenu
!= 0))
432 SetMenu((HWND
)GetHWND(), (HMENU
)m_hMenu
);
436 return wxFrameBase::ShowFullScreen(show
, style
);
439 // ----------------------------------------------------------------------------
440 // tool/status bar stuff
441 // ----------------------------------------------------------------------------
445 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
448 // We may already have a toolbar from calling SetMenuBar.
452 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
457 return m_frameToolBar
;
460 void wxFrame::PositionToolBar()
462 wxToolBar
*toolbar
= GetToolBar();
463 if ( toolbar
&& toolbar
->IsShown() )
466 // We want to do something different in WinCE, because
467 // the toolbar should be associated with the commandbar,
468 // and not an independent window.
471 // don't call our (or even wxTopLevelWindow) version because we want
472 // the real (full) client area size, not excluding the tool/status bar
474 wxWindow::DoGetClientSize(&width
, &height
);
477 wxStatusBar
*statbar
= GetStatusBar();
478 if ( statbar
&& statbar
->IsShown() )
480 height
-= statbar
->GetClientSize().y
;
482 #endif // wxUSE_STATUSBAR
486 toolbar
->GetPosition(&tx
, &ty
);
487 toolbar
->GetSize(&tw
, &th
);
490 if (ty
< 0 && (-ty
== th
))
492 if (tx
< 0 && (-tx
== tw
))
498 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
505 // if ( toolbar->GetWindowStyleFlag() & wxTB_FLAT )
509 // use the 'real' MSW position here, don't offset relativly to the
510 // client area origin
512 // Optimise such that we don't have to always resize the toolbar
513 // when the frame changes, otherwise we'll get a lot of flicker.
514 bool heightChanging
= TRUE
;
515 bool widthChanging
= TRUE
;
517 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
519 // It's OK if the current height is greater than what can be shown.
520 heightChanging
= (desiredH
> th
) ;
521 widthChanging
= (desiredW
!= tw
) ;
523 // The next time around, we may not have to set the size
525 desiredH
= desiredH
+ 200;
529 // It's OK if the current width is greater than what can be shown.
530 widthChanging
= (desiredW
> tw
) ;
531 heightChanging
= (desiredH
!= th
) ;
533 // The next time around, we may not have to set the size
535 desiredW
= desiredW
+ 200;
538 if (tx
!= 0 || ty
!= 0 || widthChanging
|| heightChanging
)
539 toolbar
->SetSize(0, 0, desiredW
, desiredH
, wxSIZE_NO_ADJUSTMENTS
);
541 #endif // __WXWINCE__
545 #endif // wxUSE_TOOLBAR
547 // ----------------------------------------------------------------------------
548 // frame state (iconized/maximized/...)
549 // ----------------------------------------------------------------------------
551 // propagate our state change to all child frames: this allows us to emulate X
552 // Windows behaviour where child frames float independently of the parent one
553 // on the desktop, but are iconized/restored with it
554 void wxFrame::IconizeChildFrames(bool bIconize
)
556 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
558 node
= node
->GetNext() )
560 wxWindow
*win
= node
->GetData();
562 // iconizing the frames with this style under Win95 shell puts them at
563 // the bottom of the screen (as the MDI children) instead of making
564 // them appear in the taskbar because they are, by virtue of this
565 // style, not managed by the taskbar - instead leave Windows take care
568 if ( win
->GetWindowStyle() & wxFRAME_TOOL_WINDOW
)
572 // the child MDI frames are a special case and should not be touched by
573 // the parent frame - instead, they are managed by the user
574 wxFrame
*frame
= wxDynamicCast(win
, wxFrame
);
576 #if wxUSE_MDI_ARCHITECTURE
577 && !wxDynamicCast(frame
, wxMDIChildFrame
)
578 #endif // wxUSE_MDI_ARCHITECTURE
581 // we don't want to restore the child frames which had been
582 // iconized even before we were iconized, so save the child frame
583 // status when iconizing the parent frame and check it when
587 // note that we shouldn't touch the hidden frames neither
588 // because iconizing/restoring them would show them as a side
590 frame
->m_wasMinimized
= frame
->IsIconized() || !frame
->IsShown();
593 // this test works for both iconizing and restoring
594 if ( !frame
->m_wasMinimized
)
595 frame
->Iconize(bIconize
);
600 WXHICON
wxFrame::GetDefaultIcon() const
602 // we don't have any standard icons (any more)
606 // ===========================================================================
607 // message processing
608 // ===========================================================================
610 // ---------------------------------------------------------------------------
612 // ---------------------------------------------------------------------------
614 bool wxFrame::MSWTranslateMessage(WXMSG
* pMsg
)
616 if ( wxWindow::MSWTranslateMessage(pMsg
) )
619 #if wxUSE_MENUS && wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
620 // try the menu bar accels
621 wxMenuBar
*menuBar
= GetMenuBar();
625 const wxAcceleratorTable
& acceleratorTable
= menuBar
->GetAccelTable();
626 return acceleratorTable
.Translate(this, pMsg
);
629 #endif // wxUSE_MENUS && wxUSE_ACCEL
632 // ---------------------------------------------------------------------------
633 // our private (non virtual) message handlers
634 // ---------------------------------------------------------------------------
636 bool wxFrame::HandlePaint()
639 if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) )
641 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
644 const wxIcon
& icon
= GetIcon();
645 HICON hIcon
= icon
.Ok() ? GetHiconOf(icon
)
646 : (HICON
)GetDefaultIcon();
648 // Hold a pointer to the dc so long as the OnPaint() message
649 // is being processed
651 HDC hdc
= ::BeginPaint(GetHwnd(), &ps
);
653 // Erase background before painting or we get white background
654 MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L);
659 ::GetClientRect(GetHwnd(), &rect
);
661 // FIXME: why hardcoded?
662 static const int icon_width
= 32;
663 static const int icon_height
= 32;
665 int icon_x
= (int)((rect
.right
- icon_width
)/2);
666 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
668 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
);
671 ::EndPaint(GetHwnd(), &ps
);
678 return wxWindow::HandlePaint();
683 // nothing to paint - processed
688 bool wxFrame::HandleSize(int x
, int y
, WXUINT id
)
690 bool processed
= FALSE
;
691 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
696 // only do it it if we were iconized before, otherwise resizing the
697 // parent frame has a curious side effect of bringing it under it's
702 // restore all child frames too
703 IconizeChildFrames(FALSE
);
705 (void)SendIconizeEvent(FALSE
);
714 // iconize all child frames too
715 IconizeChildFrames(TRUE
);
717 (void)SendIconizeEvent();
728 #endif // wxUSE_STATUSBAR
732 #endif // wxUSE_TOOLBAR
734 processed
= wxWindow::HandleSize(x
, y
, id
);
740 bool wxFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
)
744 // In case it's e.g. a toolbar.
745 wxWindow
*win
= wxFindWinFromHandle(control
);
747 return win
->MSWCommand(cmd
, id
);
750 // handle here commands from menus and accelerators
751 if ( cmd
== 0 || cmd
== 1 )
753 #if wxUSE_MENUS_NATIVE
754 if ( wxCurrentPopupMenu
)
756 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
757 wxCurrentPopupMenu
= NULL
;
759 return popupMenu
->MSWCommand(cmd
, id
);
761 #endif // wxUSE_MENUS_NATIVE
763 if ( ProcessCommand(id
) )
772 bool wxFrame::HandleMenuSelect(WXWORD nItem
, WXWORD flags
, WXHMENU hMenu
)
775 if ( flags
== 0xFFFF && hMenu
== 0 )
777 // menu was removed from screen
780 #ifndef __WXMICROWIN__
781 else if ( !(flags
& MF_POPUP
) && !(flags
& MF_SEPARATOR
) )
788 // don't give hints for separators (doesn't make sense) nor for the
789 // items opening popup menus (they don't have them anyhow) but do clear
790 // the status line - otherwise, we would be left with the help message
791 // for the previous item which doesn't apply any more
792 DoGiveHelp(wxEmptyString
, FALSE
);
797 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, item
);
798 event
.SetEventObject(this);
800 return GetEventHandler()->ProcessEvent(event
);
803 bool wxFrame::HandleMenuLoop(const wxEventType
& evtType
, WXWORD isPopup
)
805 // we don't have the menu id here, so we use the id to specify if the event
806 // was from a popup menu or a normal one
807 wxMenuEvent
event(evtType
, isPopup
? -1 : 0);
808 event
.SetEventObject(this);
810 return GetEventHandler()->ProcessEvent(event
);
813 // ---------------------------------------------------------------------------
814 // the window proc for wxFrame
815 // ---------------------------------------------------------------------------
817 long wxFrame::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
820 bool processed
= FALSE
;
825 // if we can't close, tell the system that we processed the
826 // message - otherwise it would close us
827 processed
= !Close();
831 processed
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
);
838 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
841 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
846 processed
= HandlePaint();
849 case WM_INITMENUPOPUP
:
850 processed
= HandleInitMenuPopup((WXHMENU
) wParam
);
853 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
858 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
860 processed
= HandleMenuSelect(item
, flags
, hmenu
);
864 case WM_EXITMENULOOP
:
865 processed
= HandleMenuLoop(wxEVT_MENU_CLOSE
, wParam
);
868 case WM_QUERYDRAGICON
:
870 const wxIcon
& icon
= GetIcon();
871 HICON hIcon
= icon
.Ok() ? GetHiconOf(icon
)
872 : (HICON
)GetDefaultIcon();
877 #endif // !__WXMICROWIN__
881 rc
= wxFrameBase::MSWWindowProc(message
, wParam
, lParam
);
886 // handle WM_INITMENUPOPUP message
887 bool wxFrame::HandleInitMenuPopup(WXHMENU hMenu
)
892 int nCount
= GetMenuBar()->GetMenuCount();
893 for (int n
= 0; n
< nCount
; n
++)
895 if (GetMenuBar()->GetMenu(n
)->GetHMenu() == hMenu
)
897 menu
= GetMenuBar()->GetMenu(n
);
903 wxMenuEvent
event(wxEVT_MENU_OPEN
, 0, menu
);
904 event
.SetEventObject(this);
906 return GetEventHandler()->ProcessEvent(event
);