1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
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"
46 #include "wx/statusbr.h"
47 #include "wx/generic/statusbr.h"
48 #endif // wxUSE_STATUSBAR
51 #include "wx/toolbar.h"
52 #endif // wxUSE_TOOLBAR
54 #include "wx/menuitem.h"
57 #ifdef __WXUNIVERSAL__
58 #include "wx/univ/theme.h"
59 #include "wx/univ/colschem.h"
60 #endif // __WXUNIVERSAL__
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 extern const wxChar
*wxFrameClassName
;
68 #if wxUSE_MENUS_NATIVE
69 extern wxMenu
*wxCurrentPopupMenu
;
70 #endif // wxUSE_MENUS_NATIVE
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 BEGIN_EVENT_TABLE(wxFrame
, wxFrameBase
)
77 EVT_ACTIVATE(wxFrame::OnActivate
)
78 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged
)
81 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxTopLevelWindow
)
83 // ============================================================================
85 // ============================================================================
87 // ----------------------------------------------------------------------------
88 // static class members
89 // ----------------------------------------------------------------------------
92 #if wxUSE_NATIVE_STATUSBAR
93 bool wxFrame::m_useNativeStatusBar
= TRUE
;
95 bool wxFrame::m_useNativeStatusBar
= FALSE
;
97 #endif // wxUSE_NATIVE_STATUSBAR
99 // ----------------------------------------------------------------------------
100 // creation/destruction
101 // ----------------------------------------------------------------------------
109 // Data to save/restore when calling ShowFullScreen
110 m_fsStatusBarFields
= 0;
111 m_fsStatusBarHeight
= 0;
112 m_fsToolBarHeight
= 0;
115 m_wasMinimized
= FALSE
;
117 m_winLastFocused
= (wxWindow
*)NULL
;
120 bool wxFrame::Create(wxWindow
*parent
,
122 const wxString
& title
,
126 const wxString
& name
)
128 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
131 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
133 wxModelessWindows
.Append(this);
140 m_isBeingDeleted
= TRUE
;
145 // ----------------------------------------------------------------------------
146 // wxFrame client size calculations
147 // ----------------------------------------------------------------------------
149 void wxFrame::DoSetClientSize(int width
, int height
)
151 // leave enough space for the status bar if we have (and show) it
153 wxStatusBar
*statbar
= GetStatusBar();
154 if ( statbar
&& statbar
->IsShown() )
156 height
+= statbar
->GetSize().y
;
158 #endif // wxUSE_STATUSBAR
160 // call GetClientAreaOrigin() to take the toolbar into account
161 wxPoint pt
= GetClientAreaOrigin();
165 wxTopLevelWindow::DoSetClientSize(width
, height
);
168 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
169 void wxFrame::DoGetClientSize(int *x
, int *y
) const
171 wxTopLevelWindow::DoGetClientSize(x
, y
);
173 // account for the possible toolbar
174 wxPoint pt
= GetClientAreaOrigin();
182 // adjust client area height to take the status bar into account
185 wxStatusBar
*statbar
= GetStatusBar();
186 if ( statbar
&& statbar
->IsShown() )
188 *y
-= statbar
->GetClientSize().y
;
191 #endif // wxUSE_STATUSBAR
194 // ----------------------------------------------------------------------------
195 // wxFrame: various geometry-related functions
196 // ----------------------------------------------------------------------------
198 void wxFrame::Raise()
201 // no SetForegroundWindow() in Win16
202 wxFrameBase::Raise();
204 ::SetForegroundWindow(GetHwnd());
208 // generate an artificial resize event
209 void wxFrame::SendSizeEvent()
213 RECT r
= wxGetWindowRect(GetHwnd());
215 (void)::PostMessage(GetHwnd(), WM_SIZE
,
216 IsMaximized() ? SIZE_MAXIMIZED
: SIZE_RESTORED
,
217 MAKELPARAM(r
.right
- r
.left
, r
.bottom
- r
.top
));
222 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
,
225 const wxString
& name
)
227 wxStatusBar
*statusBar
= NULL
;
229 #if wxUSE_NATIVE_STATUSBAR
230 if ( !UsesNativeStatusBar() )
232 statusBar
= (wxStatusBar
*)new wxStatusBarGeneric(this, id
, style
);
237 statusBar
= new wxStatusBar(this, id
, style
, name
);
240 statusBar
->SetFieldsCount(number
);
245 void wxFrame::PositionStatusBar()
247 if ( !m_frameStatusBar
|| !m_frameStatusBar
->IsShown() )
251 GetClientSize(&w
, &h
);
253 m_frameStatusBar
->GetSize(&sw
, &sh
);
255 // Since we wish the status bar to be directly under the client area,
256 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
257 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
259 #endif // wxUSE_STATUSBAR
261 #if wxUSE_MENUS_NATIVE
263 void wxFrame::AttachMenuBar(wxMenuBar
*menubar
)
265 wxFrameBase::AttachMenuBar(menubar
);
269 // actually remove the menu from the frame
270 m_hMenu
= (WXHMENU
)0;
271 InternalSetMenuBar();
273 else // set new non NULL menu bar
275 // Can set a menubar several times.
276 if ( menubar
->GetHMenu() )
278 m_hMenu
= menubar
->GetHMenu();
282 m_hMenu
= menubar
->Create();
286 wxFAIL_MSG( _T("failed to create menu bar") );
291 InternalSetMenuBar();
295 void wxFrame::InternalSetMenuBar()
297 #ifndef __WXMICROWIN__
298 if ( !::SetMenu(GetHwnd(), (HMENU
)m_hMenu
) )
300 wxLogLastError(wxT("SetMenu"));
305 #endif // wxUSE_MENUS_NATIVE
307 // Responds to colour changes, and passes event on to children.
308 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
310 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
314 if ( m_frameStatusBar
)
316 wxSysColourChangedEvent event2
;
317 event2
.SetEventObject( m_frameStatusBar
);
318 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
320 #endif // wxUSE_STATUSBAR
322 // Propagate the event to the non-top-level children
323 wxWindow::OnSysColourChanged(event
);
326 // Pass TRUE to show full screen, FALSE to restore.
327 bool wxFrame::ShowFullScreen(bool show
, long style
)
329 if ( IsFullScreen() == show
)
335 wxToolBar
*theToolBar
= GetToolBar();
337 theToolBar
->GetSize(NULL
, &m_fsToolBarHeight
);
339 // zap the toolbar, menubar, and statusbar
341 if ((style
& wxFULLSCREEN_NOTOOLBAR
) && theToolBar
)
343 theToolBar
->SetSize(-1,0);
344 theToolBar
->Show(FALSE
);
346 #endif // wxUSE_TOOLBAR
348 #ifndef __WXMICROWIN__
349 if (style
& wxFULLSCREEN_NOMENUBAR
)
350 SetMenu((HWND
)GetHWND(), (HMENU
) NULL
);
354 wxStatusBar
*theStatusBar
= GetStatusBar();
356 theStatusBar
->GetSize(NULL
, &m_fsStatusBarHeight
);
358 // Save the number of fields in the statusbar
359 if ((style
& wxFULLSCREEN_NOSTATUSBAR
) && theStatusBar
)
361 //m_fsStatusBarFields = theStatusBar->GetFieldsCount();
362 //SetStatusBar((wxStatusBar*) NULL);
363 //delete theStatusBar;
364 theStatusBar
->Show(FALSE
);
367 m_fsStatusBarFields
= 0;
368 #endif // wxUSE_STATUSBAR
373 wxToolBar
*theToolBar
= GetToolBar();
375 // restore the toolbar, menubar, and statusbar
376 if (theToolBar
&& (m_fsStyle
& wxFULLSCREEN_NOTOOLBAR
))
378 theToolBar
->SetSize(-1, m_fsToolBarHeight
);
379 theToolBar
->Show(TRUE
);
381 #endif // wxUSE_TOOLBAR
384 if ( m_fsStyle
& wxFULLSCREEN_NOSTATUSBAR
)
386 //CreateStatusBar(m_fsStatusBarFields);
389 GetStatusBar()->Show(TRUE
);
393 #endif // wxUSE_STATUSBAR
395 #ifndef __WXMICROWIN__
396 if ((m_fsStyle
& wxFULLSCREEN_NOMENUBAR
) && (m_hMenu
!= 0))
397 SetMenu((HWND
)GetHWND(), (HMENU
)m_hMenu
);
401 return wxFrameBase::ShowFullScreen(show
, style
);
404 // Default activation behaviour - set the focus for the first child
406 void wxFrame::OnActivate(wxActivateEvent
& event
)
408 if ( event
.GetActive() )
410 // restore focus to the child which was last focused
411 wxLogTrace(_T("focus"), _T("wxFrame %08x activated."), m_hWnd
);
413 wxWindow
*parent
= m_winLastFocused
? m_winLastFocused
->GetParent()
420 wxSetFocusToChild(parent
, &m_winLastFocused
);
424 // remember the last focused child if it is our child
425 m_winLastFocused
= FindFocus();
427 // so we NULL it out if it's a child from some other frame
428 wxWindow
*win
= m_winLastFocused
;
431 if ( win
->IsTopLevel() )
435 m_winLastFocused
= NULL
;
441 win
= win
->GetParent();
444 wxLogTrace(_T("focus"),
445 _T("wxFrame %08x deactivated, last focused: %08x."),
447 m_winLastFocused
? GetHwndOf(m_winLastFocused
)
454 // ----------------------------------------------------------------------------
455 // tool/status bar stuff
456 // ----------------------------------------------------------------------------
460 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
462 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
467 return m_frameToolBar
;
470 void wxFrame::PositionToolBar()
472 wxToolBar
*toolbar
= GetToolBar();
473 if ( toolbar
&& toolbar
->IsShown() )
475 // don't call our (or even wxTopLevelWindow) version because we want
476 // the real (full) client area size, not excluding the tool/status bar
478 wxWindow::DoGetClientSize(&width
, &height
);
481 wxStatusBar
*statbar
= GetStatusBar();
482 if ( statbar
&& statbar
->IsShown() )
484 height
-= statbar
->GetClientSize().y
;
486 #endif // wxUSE_STATUSBAR
489 toolbar
->GetSize(&tw
, &th
);
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
504 toolbar
->SetSize(0, 0, tw
, th
, wxSIZE_NO_ADJUSTMENTS
);
508 #endif // wxUSE_TOOLBAR
510 // ----------------------------------------------------------------------------
511 // frame state (iconized/maximized/...)
512 // ----------------------------------------------------------------------------
514 // propagate our state change to all child frames: this allows us to emulate X
515 // Windows behaviour where child frames float independently of the parent one
516 // on the desktop, but are iconized/restored with it
517 void wxFrame::IconizeChildFrames(bool bIconize
)
519 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
521 node
= node
->GetNext() )
523 wxWindow
*win
= node
->GetData();
525 // iconizing the frames with this style under Win95 shell puts them at
526 // the bottom of the screen (as the MDI children) instead of making
527 // them appear in the taskbar because they are, by virtue of this
528 // style, not managed by the taskbar - instead leave Windows take care
531 if ( win
->GetWindowStyle() & wxFRAME_TOOL_WINDOW
)
535 // the child MDI frames are a special case and should not be touched by
536 // the parent frame - instead, they are managed by the user
537 wxFrame
*frame
= wxDynamicCast(win
, wxFrame
);
539 #if wxUSE_MDI_ARCHITECTURE
540 && !wxDynamicCast(frame
, wxMDIChildFrame
)
541 #endif // wxUSE_MDI_ARCHITECTURE
544 // we don't want to restore the child frames which had been
545 // iconized even before we were iconized, so save the child frame
546 // status when iconizing the parent frame and check it when
550 // note that we shouldn't touch the hidden frames neither
551 // because iconizing/restoring them would show them as a side
553 frame
->m_wasMinimized
= frame
->IsIconized() || !frame
->IsShown();
556 // this test works for both iconizing and restoring
557 if ( !frame
->m_wasMinimized
)
558 frame
->Iconize(bIconize
);
563 WXHICON
wxFrame::GetDefaultIcon() const
565 return (WXHICON
)(wxSTD_FRAME_ICON
? wxSTD_FRAME_ICON
566 : wxDEFAULT_FRAME_ICON
);
569 // ===========================================================================
570 // message processing
571 // ===========================================================================
573 // ---------------------------------------------------------------------------
575 // ---------------------------------------------------------------------------
577 bool wxFrame::MSWTranslateMessage(WXMSG
* pMsg
)
579 if ( wxWindow::MSWTranslateMessage(pMsg
) )
582 #if wxUSE_MENUS && wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
583 // try the menu bar accels
584 wxMenuBar
*menuBar
= GetMenuBar();
588 const wxAcceleratorTable
& acceleratorTable
= menuBar
->GetAccelTable();
589 return acceleratorTable
.Translate(this, pMsg
);
592 #endif // wxUSE_MENUS && wxUSE_ACCEL
595 // ---------------------------------------------------------------------------
596 // our private (non virtual) message handlers
597 // ---------------------------------------------------------------------------
599 bool wxFrame::HandlePaint()
602 if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) )
604 #ifndef __WXMICROWIN__
607 const wxIcon
& icon
= GetIcon();
608 HICON hIcon
= icon
.Ok() ? GetHiconOf(icon
)
609 : (HICON
)GetDefaultIcon();
611 // Hold a pointer to the dc so long as the OnPaint() message
612 // is being processed
614 HDC hdc
= ::BeginPaint(GetHwnd(), &ps
);
616 // Erase background before painting or we get white background
617 MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L);
622 ::GetClientRect(GetHwnd(), &rect
);
624 // FIXME: why hardcoded?
625 static const int icon_width
= 32;
626 static const int icon_height
= 32;
628 int icon_x
= (int)((rect
.right
- icon_width
)/2);
629 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
631 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
);
634 ::EndPaint(GetHwnd(), &ps
);
641 return wxWindow::HandlePaint();
646 // nothing to paint - processed
651 bool wxFrame::HandleSize(int x
, int y
, WXUINT id
)
653 bool processed
= FALSE
;
654 #ifndef __WXMICROWIN__
659 // only do it it if we were iconized before, otherwise resizing the
660 // parent frame has a curious side effect of bringing it under it's
665 // restore all child frames too
666 IconizeChildFrames(FALSE
);
668 (void)SendIconizeEvent(FALSE
);
677 // iconize all child frames too
678 IconizeChildFrames(TRUE
);
680 (void)SendIconizeEvent();
691 #endif // wxUSE_STATUSBAR
695 #endif // wxUSE_TOOLBAR
697 wxSizeEvent
event(wxSize(x
, y
), m_windowId
);
698 event
.SetEventObject( this );
699 processed
= GetEventHandler()->ProcessEvent(event
);
705 bool wxFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
)
709 // In case it's e.g. a toolbar.
710 wxWindow
*win
= wxFindWinFromHandle(control
);
712 return win
->MSWCommand(cmd
, id
);
715 // handle here commands from menus and accelerators
716 if ( cmd
== 0 || cmd
== 1 )
718 #if wxUSE_MENUS_NATIVE
719 if ( wxCurrentPopupMenu
)
721 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
722 wxCurrentPopupMenu
= NULL
;
724 return popupMenu
->MSWCommand(cmd
, id
);
726 #endif // wxUSE_MENUS_NATIVE
728 if ( ProcessCommand(id
) )
737 bool wxFrame::HandleMenuSelect(WXWORD nItem
, WXWORD flags
, WXHMENU hMenu
)
740 if ( flags
== 0xFFFF && hMenu
== 0 )
742 // menu was removed from screen
745 #ifndef __WXMICROWIN__
746 else if ( !(flags
& MF_POPUP
) && !(flags
& MF_SEPARATOR
) )
754 // don't give hints for separators (doesn't make sense) nor for the
755 // items opening popup menus (they don't have them anyhow) but do clear
756 // the status line - otherwise, we would be left with the help message
757 // for the previous item which doesn't apply any more
758 wxStatusBar
*statbar
= GetStatusBar();
761 statbar
->SetStatusText(wxEmptyString
);
763 #endif // wxUSE_STATUSBAR
768 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, item
);
769 event
.SetEventObject(this);
771 return GetEventHandler()->ProcessEvent(event
);
774 bool wxFrame::HandleMenuLoop(const wxEventType
& evtType
, WXWORD isPopup
)
776 // we don't have the menu id here, so we use the id to specify if the event
777 // was from a popup menu or a normal one
778 wxMenuEvent
event(evtType
, isPopup
? -1 : 0);
779 event
.SetEventObject(this);
781 return GetEventHandler()->ProcessEvent(event
);
784 // ---------------------------------------------------------------------------
785 // the window proc for wxFrame
786 // ---------------------------------------------------------------------------
788 long wxFrame::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
791 bool processed
= FALSE
;
796 // if we can't close, tell the system that we processed the
797 // message - otherwise it would close us
798 processed
= !Close();
802 processed
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
);
809 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
812 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
817 processed
= HandlePaint();
820 #ifndef __WXMICROWIN__
825 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
827 processed
= HandleMenuSelect(item
, flags
, hmenu
);
832 case WM_ENTERMENULOOP
:
833 processed
= HandleMenuLoop(wxEVT_MENU_OPEN
, wParam
);
836 case WM_EXITMENULOOP
:
837 processed
= HandleMenuLoop(wxEVT_MENU_CLOSE
, wParam
);
841 case WM_QUERYDRAGICON
:
843 const wxIcon
& icon
= GetIcon();
844 HICON hIcon
= icon
.Ok() ? GetHiconOf(icon
)
845 : (HICON
)GetDefaultIcon();
850 #endif // !__WXMICROWIN__
854 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);