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
)
86 WX_END_PROPERTIES_TABLE()
88 WX_BEGIN_HANDLERS_TABLE(wxFrame
)
89 WX_END_HANDLERS_TABLE()
91 WX_CONSTRUCTOR_5( wxFrame
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Title
, wxPoint
, Position
, wxSize
, Size
)
94 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxTopLevelWindow
)
97 // ============================================================================
99 // ============================================================================
101 // ----------------------------------------------------------------------------
102 // static class members
103 // ----------------------------------------------------------------------------
106 #if wxUSE_NATIVE_STATUSBAR
107 bool wxFrame::m_useNativeStatusBar
= TRUE
;
109 bool wxFrame::m_useNativeStatusBar
= FALSE
;
111 #endif // wxUSE_NATIVE_STATUSBAR
113 // ----------------------------------------------------------------------------
114 // creation/destruction
115 // ----------------------------------------------------------------------------
123 // Data to save/restore when calling ShowFullScreen
124 m_fsStatusBarFields
= 0;
125 m_fsStatusBarHeight
= 0;
126 m_fsToolBarHeight
= 0;
128 m_wasMinimized
= FALSE
;
131 bool wxFrame::Create(wxWindow
*parent
,
133 const wxString
& title
,
137 const wxString
& name
)
139 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
142 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
144 wxModelessWindows
.Append(this);
151 m_isBeingDeleted
= TRUE
;
155 // ----------------------------------------------------------------------------
156 // wxFrame client size calculations
157 // ----------------------------------------------------------------------------
159 void wxFrame::DoSetClientSize(int width
, int height
)
161 // leave enough space for the status bar if we have (and show) it
163 wxStatusBar
*statbar
= GetStatusBar();
164 if ( statbar
&& statbar
->IsShown() )
166 height
+= statbar
->GetSize().y
;
168 #endif // wxUSE_STATUSBAR
170 // call GetClientAreaOrigin() to take the toolbar into account
171 wxPoint pt
= GetClientAreaOrigin();
175 wxTopLevelWindow::DoSetClientSize(width
, height
);
178 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
179 void wxFrame::DoGetClientSize(int *x
, int *y
) const
181 wxTopLevelWindow::DoGetClientSize(x
, y
);
183 // account for the possible toolbar
184 wxPoint pt
= GetClientAreaOrigin();
192 // adjust client area height to take the status bar into account
195 wxStatusBar
*statbar
= GetStatusBar();
196 if ( statbar
&& statbar
->IsShown() )
198 *y
-= statbar
->GetClientSize().y
;
201 #endif // wxUSE_STATUSBAR
204 // ----------------------------------------------------------------------------
205 // wxFrame: various geometry-related functions
206 // ----------------------------------------------------------------------------
208 void wxFrame::Raise()
210 ::SetForegroundWindow(GetHwnd());
213 // generate an artificial resize event
214 void wxFrame::SendSizeEvent()
218 RECT r
= wxGetWindowRect(GetHwnd());
220 (void)::PostMessage(GetHwnd(), WM_SIZE
,
221 IsMaximized() ? SIZE_MAXIMIZED
: SIZE_RESTORED
,
222 MAKELPARAM(r
.right
- r
.left
, r
.bottom
- r
.top
));
227 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
,
230 const wxString
& name
)
232 wxStatusBar
*statusBar
= NULL
;
234 #if wxUSE_NATIVE_STATUSBAR
235 if ( !UsesNativeStatusBar() )
237 statusBar
= (wxStatusBar
*)new wxStatusBarGeneric(this, id
, style
);
242 statusBar
= new wxStatusBar(this, id
, style
, name
);
245 statusBar
->SetFieldsCount(number
);
250 void wxFrame::PositionStatusBar()
252 if ( !m_frameStatusBar
|| !m_frameStatusBar
->IsShown() )
256 GetClientSize(&w
, &h
);
258 m_frameStatusBar
->GetSize(&sw
, &sh
);
260 // Since we wish the status bar to be directly under the client area,
261 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
262 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
264 #endif // wxUSE_STATUSBAR
266 #if wxUSE_MENUS_NATIVE
268 void wxFrame::AttachMenuBar(wxMenuBar
*menubar
)
270 wxFrameBase::AttachMenuBar(menubar
);
274 // actually remove the menu from the frame
275 m_hMenu
= (WXHMENU
)0;
276 InternalSetMenuBar();
278 else // set new non NULL menu bar
281 // Can set a menubar several times.
282 if ( menubar
->GetHMenu() )
284 m_hMenu
= menubar
->GetHMenu();
288 m_hMenu
= menubar
->Create();
292 wxFAIL_MSG( _T("failed to create menu bar") );
297 InternalSetMenuBar();
301 void wxFrame::InternalSetMenuBar()
303 #ifdef __WXMICROWIN__
305 #elif defined(__WXWINCE__)
309 wxToolBar
* toolBar
= new wxToolBar(this, -1,
310 wxDefaultPosition
, wxDefaultSize
,
311 wxBORDER_NONE
| wxTB_HORIZONTAL
,
312 wxToolBarNameStr
, GetMenuBar());
316 if ( !::SetMenu(GetHwnd(), (HMENU
)m_hMenu
) )
318 wxLogLastError(wxT("SetMenu"));
323 #endif // wxUSE_MENUS_NATIVE
325 // Responds to colour changes, and passes event on to children.
326 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
328 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
332 if ( m_frameStatusBar
)
334 wxSysColourChangedEvent event2
;
335 event2
.SetEventObject( m_frameStatusBar
);
336 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
338 #endif // wxUSE_STATUSBAR
340 // Propagate the event to the non-top-level children
341 wxWindow::OnSysColourChanged(event
);
344 // Pass TRUE to show full screen, FALSE to restore.
345 bool wxFrame::ShowFullScreen(bool show
, long style
)
347 if ( IsFullScreen() == show
)
354 // TODO: hide commandbar
356 wxToolBar
*theToolBar
= GetToolBar();
358 theToolBar
->GetSize(NULL
, &m_fsToolBarHeight
);
360 // zap the toolbar, menubar, and statusbar
362 if ((style
& wxFULLSCREEN_NOTOOLBAR
) && theToolBar
)
364 theToolBar
->SetSize(-1,0);
365 theToolBar
->Show(FALSE
);
367 #endif // __WXWINCE__
368 #endif // wxUSE_TOOLBAR
370 // TODO: make it work for WinCE
371 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
372 if (style
& wxFULLSCREEN_NOMENUBAR
)
373 SetMenu((HWND
)GetHWND(), (HMENU
) NULL
);
377 wxStatusBar
*theStatusBar
= GetStatusBar();
379 theStatusBar
->GetSize(NULL
, &m_fsStatusBarHeight
);
381 // Save the number of fields in the statusbar
382 if ((style
& wxFULLSCREEN_NOSTATUSBAR
) && theStatusBar
)
384 //m_fsStatusBarFields = theStatusBar->GetFieldsCount();
385 //SetStatusBar((wxStatusBar*) NULL);
386 //delete theStatusBar;
387 theStatusBar
->Show(FALSE
);
390 m_fsStatusBarFields
= 0;
391 #endif // wxUSE_STATUSBAR
397 // TODO: show commandbar
399 wxToolBar
*theToolBar
= GetToolBar();
401 // restore the toolbar, menubar, and statusbar
402 if (theToolBar
&& (m_fsStyle
& wxFULLSCREEN_NOTOOLBAR
))
404 theToolBar
->SetSize(-1, m_fsToolBarHeight
);
405 theToolBar
->Show(TRUE
);
407 #endif // __WXWINCE__
408 #endif // wxUSE_TOOLBAR
411 if ( m_fsStyle
& wxFULLSCREEN_NOSTATUSBAR
)
413 //CreateStatusBar(m_fsStatusBarFields);
416 GetStatusBar()->Show(TRUE
);
420 #endif // wxUSE_STATUSBAR
422 // TODO: make it work for WinCE
423 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
424 if ((m_fsStyle
& wxFULLSCREEN_NOMENUBAR
) && (m_hMenu
!= 0))
425 SetMenu((HWND
)GetHWND(), (HMENU
)m_hMenu
);
429 return wxFrameBase::ShowFullScreen(show
, style
);
432 // ----------------------------------------------------------------------------
433 // tool/status bar stuff
434 // ----------------------------------------------------------------------------
438 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
441 // We may already have a toolbar from calling SetMenuBar.
445 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
450 return m_frameToolBar
;
453 void wxFrame::PositionToolBar()
455 wxToolBar
*toolbar
= GetToolBar();
456 if ( toolbar
&& toolbar
->IsShown() )
459 // We want to do something different in WinCE, because
460 // the toolbar should be associated with the commandbar,
461 // and not an independent window.
464 // don't call our (or even wxTopLevelWindow) version because we want
465 // the real (full) client area size, not excluding the tool/status bar
467 wxWindow::DoGetClientSize(&width
, &height
);
470 wxStatusBar
*statbar
= GetStatusBar();
471 if ( statbar
&& statbar
->IsShown() )
473 height
-= statbar
->GetClientSize().y
;
475 #endif // wxUSE_STATUSBAR
479 toolbar
->GetPosition(&tx
, &ty
);
480 toolbar
->GetSize(&tw
, &th
);
483 if (ty
< 0 && (-ty
== th
))
485 if (tx
< 0 && (-tx
== tw
))
491 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
498 // if ( toolbar->GetWindowStyleFlag() & wxTB_FLAT )
502 // use the 'real' MSW position here, don't offset relativly to the
503 // client area origin
505 // Optimise such that we don't have to always resize the toolbar
506 // when the frame changes, otherwise we'll get a lot of flicker.
507 bool heightChanging
= TRUE
;
508 bool widthChanging
= TRUE
;
510 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
512 // It's OK if the current height is greater than what can be shown.
513 heightChanging
= (desiredH
> th
) ;
514 widthChanging
= (desiredW
!= tw
) ;
516 // The next time around, we may not have to set the size
518 desiredH
= desiredH
+ 200;
522 // It's OK if the current width is greater than what can be shown.
523 widthChanging
= (desiredW
> tw
) ;
524 heightChanging
= (desiredH
!= th
) ;
526 // The next time around, we may not have to set the size
528 desiredW
= desiredW
+ 200;
531 if (tx
!= 0 || ty
!= 0 || widthChanging
|| heightChanging
)
532 toolbar
->SetSize(0, 0, desiredW
, desiredH
, wxSIZE_NO_ADJUSTMENTS
);
534 #endif // __WXWINCE__
538 #endif // wxUSE_TOOLBAR
540 // ----------------------------------------------------------------------------
541 // frame state (iconized/maximized/...)
542 // ----------------------------------------------------------------------------
544 // propagate our state change to all child frames: this allows us to emulate X
545 // Windows behaviour where child frames float independently of the parent one
546 // on the desktop, but are iconized/restored with it
547 void wxFrame::IconizeChildFrames(bool bIconize
)
549 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
551 node
= node
->GetNext() )
553 wxWindow
*win
= node
->GetData();
555 // iconizing the frames with this style under Win95 shell puts them at
556 // the bottom of the screen (as the MDI children) instead of making
557 // them appear in the taskbar because they are, by virtue of this
558 // style, not managed by the taskbar - instead leave Windows take care
561 if ( win
->GetWindowStyle() & wxFRAME_TOOL_WINDOW
)
565 // the child MDI frames are a special case and should not be touched by
566 // the parent frame - instead, they are managed by the user
567 wxFrame
*frame
= wxDynamicCast(win
, wxFrame
);
569 #if wxUSE_MDI_ARCHITECTURE
570 && !wxDynamicCast(frame
, wxMDIChildFrame
)
571 #endif // wxUSE_MDI_ARCHITECTURE
574 // we don't want to restore the child frames which had been
575 // iconized even before we were iconized, so save the child frame
576 // status when iconizing the parent frame and check it when
580 // note that we shouldn't touch the hidden frames neither
581 // because iconizing/restoring them would show them as a side
583 frame
->m_wasMinimized
= frame
->IsIconized() || !frame
->IsShown();
586 // this test works for both iconizing and restoring
587 if ( !frame
->m_wasMinimized
)
588 frame
->Iconize(bIconize
);
593 WXHICON
wxFrame::GetDefaultIcon() const
595 // we don't have any standard icons (any more)
599 // ===========================================================================
600 // message processing
601 // ===========================================================================
603 // ---------------------------------------------------------------------------
605 // ---------------------------------------------------------------------------
607 bool wxFrame::MSWTranslateMessage(WXMSG
* pMsg
)
609 if ( wxWindow::MSWTranslateMessage(pMsg
) )
612 #if wxUSE_MENUS && wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
613 // try the menu bar accels
614 wxMenuBar
*menuBar
= GetMenuBar();
618 const wxAcceleratorTable
& acceleratorTable
= menuBar
->GetAccelTable();
619 return acceleratorTable
.Translate(this, pMsg
);
622 #endif // wxUSE_MENUS && wxUSE_ACCEL
625 // ---------------------------------------------------------------------------
626 // our private (non virtual) message handlers
627 // ---------------------------------------------------------------------------
629 bool wxFrame::HandlePaint()
632 if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) )
634 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
637 const wxIcon
& icon
= GetIcon();
638 HICON hIcon
= icon
.Ok() ? GetHiconOf(icon
)
639 : (HICON
)GetDefaultIcon();
641 // Hold a pointer to the dc so long as the OnPaint() message
642 // is being processed
644 HDC hdc
= ::BeginPaint(GetHwnd(), &ps
);
646 // Erase background before painting or we get white background
647 MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L);
652 ::GetClientRect(GetHwnd(), &rect
);
654 // FIXME: why hardcoded?
655 static const int icon_width
= 32;
656 static const int icon_height
= 32;
658 int icon_x
= (int)((rect
.right
- icon_width
)/2);
659 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
661 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
);
664 ::EndPaint(GetHwnd(), &ps
);
671 return wxWindow::HandlePaint();
676 // nothing to paint - processed
681 bool wxFrame::HandleSize(int x
, int y
, WXUINT id
)
683 bool processed
= FALSE
;
684 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
689 // only do it it if we were iconized before, otherwise resizing the
690 // parent frame has a curious side effect of bringing it under it's
695 // restore all child frames too
696 IconizeChildFrames(FALSE
);
698 (void)SendIconizeEvent(FALSE
);
707 // iconize all child frames too
708 IconizeChildFrames(TRUE
);
710 (void)SendIconizeEvent();
721 #endif // wxUSE_STATUSBAR
725 #endif // wxUSE_TOOLBAR
727 processed
= wxWindow::HandleSize(x
, y
, id
);
733 bool wxFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
)
737 // In case it's e.g. a toolbar.
738 wxWindow
*win
= wxFindWinFromHandle(control
);
740 return win
->MSWCommand(cmd
, id
);
743 // handle here commands from menus and accelerators
744 if ( cmd
== 0 || cmd
== 1 )
746 #if wxUSE_MENUS_NATIVE
747 if ( wxCurrentPopupMenu
)
749 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
750 wxCurrentPopupMenu
= NULL
;
752 return popupMenu
->MSWCommand(cmd
, id
);
754 #endif // wxUSE_MENUS_NATIVE
756 if ( ProcessCommand(id
) )
765 bool wxFrame::HandleMenuSelect(WXWORD nItem
, WXWORD flags
, WXHMENU hMenu
)
768 if ( flags
== 0xFFFF && hMenu
== 0 )
770 // menu was removed from screen
773 #ifndef __WXMICROWIN__
774 else if ( !(flags
& MF_POPUP
) && !(flags
& MF_SEPARATOR
) )
781 // don't give hints for separators (doesn't make sense) nor for the
782 // items opening popup menus (they don't have them anyhow) but do clear
783 // the status line - otherwise, we would be left with the help message
784 // for the previous item which doesn't apply any more
785 DoGiveHelp(wxEmptyString
, FALSE
);
790 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, item
);
791 event
.SetEventObject(this);
793 return GetEventHandler()->ProcessEvent(event
);
796 bool wxFrame::HandleMenuLoop(const wxEventType
& evtType
, WXWORD isPopup
)
798 // we don't have the menu id here, so we use the id to specify if the event
799 // was from a popup menu or a normal one
800 wxMenuEvent
event(evtType
, isPopup
? -1 : 0);
801 event
.SetEventObject(this);
803 return GetEventHandler()->ProcessEvent(event
);
806 // ---------------------------------------------------------------------------
807 // the window proc for wxFrame
808 // ---------------------------------------------------------------------------
810 long wxFrame::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
813 bool processed
= FALSE
;
818 // if we can't close, tell the system that we processed the
819 // message - otherwise it would close us
820 processed
= !Close();
824 processed
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
);
831 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
834 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
839 processed
= HandlePaint();
842 case WM_INITMENUPOPUP
:
843 processed
= HandleInitMenuPopup((WXHMENU
) wParam
);
846 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
851 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
853 processed
= HandleMenuSelect(item
, flags
, hmenu
);
857 case WM_EXITMENULOOP
:
858 processed
= HandleMenuLoop(wxEVT_MENU_CLOSE
, wParam
);
861 case WM_QUERYDRAGICON
:
863 const wxIcon
& icon
= GetIcon();
864 HICON hIcon
= icon
.Ok() ? GetHiconOf(icon
)
865 : (HICON
)GetDefaultIcon();
870 #endif // !__WXMICROWIN__
874 rc
= wxFrameBase::MSWWindowProc(message
, wParam
, lParam
);
879 // handle WM_INITMENUPOPUP message
880 bool wxFrame::HandleInitMenuPopup(WXHMENU hMenu
)
885 int nCount
= GetMenuBar()->GetMenuCount();
886 for (int n
= 0; n
< nCount
; n
++)
888 if (GetMenuBar()->GetMenu(n
)->GetHMenu() == hMenu
)
890 menu
= GetMenuBar()->GetMenu(n
);
896 wxMenuEvent
event(wxEVT_MENU_OPEN
, 0, menu
);
897 event
.SetEventObject(this);
899 return GetEventHandler()->ProcessEvent(event
);