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
, wxWindow
)
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::GetSystemColour(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 wxTopLevelWindow::DoSetClientSize(width
, height
);
163 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
164 void wxFrame::DoGetClientSize(int *x
, int *y
) const
166 wxTopLevelWindow::DoGetClientSize(x
, y
);
169 // adjust client area height to take the status bar into account
172 wxStatusBar
*statbar
= GetStatusBar();
173 if ( statbar
&& statbar
->IsShown() )
175 *y
-= statbar
->GetClientSize().y
;
178 #endif // wxUSE_STATUSBAR
181 // ----------------------------------------------------------------------------
182 // wxFrame: various geometry-related functions
183 // ----------------------------------------------------------------------------
185 void wxFrame::Raise()
188 // no SetForegroundWindow() in Win16
189 wxFrameBase::Raise();
191 ::SetForegroundWindow(GetHwnd());
195 // generate an artificial resize event
196 void wxFrame::SendSizeEvent()
200 RECT r
= wxGetWindowRect(GetHwnd());
202 (void)::PostMessage(GetHwnd(), WM_SIZE
,
203 IsMaximized() ? SIZE_MAXIMIZED
: SIZE_RESTORED
,
204 MAKELPARAM(r
.right
- r
.left
, r
.bottom
- r
.top
));
209 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
,
212 const wxString
& name
)
214 wxStatusBar
*statusBar
= NULL
;
216 #if wxUSE_NATIVE_STATUSBAR
217 if ( !UsesNativeStatusBar() )
219 statusBar
= (wxStatusBar
*)new wxStatusBarGeneric(this, id
, style
);
224 statusBar
= new wxStatusBar(this, id
, style
, name
);
227 statusBar
->SetFieldsCount(number
);
232 void wxFrame::PositionStatusBar()
234 if ( !m_frameStatusBar
|| !m_frameStatusBar
->IsShown() )
238 GetClientSize(&w
, &h
);
240 m_frameStatusBar
->GetSize(&sw
, &sh
);
242 // Since we wish the status bar to be directly under the client area,
243 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
244 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
246 #endif // wxUSE_STATUSBAR
248 #if wxUSE_MENUS_NATIVE
250 void wxFrame::AttachMenuBar(wxMenuBar
*menubar
)
252 wxFrameBase::AttachMenuBar(menubar
);
256 // actually remove the menu from the frame
257 m_hMenu
= (WXHMENU
)0;
258 InternalSetMenuBar();
260 else // set new non NULL menu bar
262 // Can set a menubar several times.
263 if ( menubar
->GetHMenu() )
265 m_hMenu
= menubar
->GetHMenu();
269 m_hMenu
= menubar
->Create();
273 wxFAIL_MSG( _T("failed to create menu bar") );
278 InternalSetMenuBar();
282 void wxFrame::InternalSetMenuBar()
284 #ifndef __WXMICROWIN__
285 if ( !::SetMenu(GetHwnd(), (HMENU
)m_hMenu
) )
287 wxLogLastError(wxT("SetMenu"));
292 #endif // wxUSE_MENUS_NATIVE
294 // Responds to colour changes, and passes event on to children.
295 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
297 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
301 if ( m_frameStatusBar
)
303 wxSysColourChangedEvent event2
;
304 event2
.SetEventObject( m_frameStatusBar
);
305 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
307 #endif // wxUSE_STATUSBAR
309 // Propagate the event to the non-top-level children
310 wxWindow::OnSysColourChanged(event
);
313 // Pass TRUE to show full screen, FALSE to restore.
314 bool wxFrame::ShowFullScreen(bool show
, long style
)
316 if ( IsFullScreen() == show
)
322 wxToolBar
*theToolBar
= GetToolBar();
324 theToolBar
->GetSize(NULL
, &m_fsToolBarHeight
);
326 // zap the toolbar, menubar, and statusbar
328 if ((style
& wxFULLSCREEN_NOTOOLBAR
) && theToolBar
)
330 theToolBar
->SetSize(-1,0);
331 theToolBar
->Show(FALSE
);
333 #endif // wxUSE_TOOLBAR
335 #ifndef __WXMICROWIN__
336 if (style
& wxFULLSCREEN_NOMENUBAR
)
337 SetMenu((HWND
)GetHWND(), (HMENU
) NULL
);
341 wxStatusBar
*theStatusBar
= GetStatusBar();
343 theStatusBar
->GetSize(NULL
, &m_fsStatusBarHeight
);
345 // Save the number of fields in the statusbar
346 if ((style
& wxFULLSCREEN_NOSTATUSBAR
) && theStatusBar
)
348 //m_fsStatusBarFields = theStatusBar->GetFieldsCount();
349 //SetStatusBar((wxStatusBar*) NULL);
350 //delete theStatusBar;
351 theStatusBar
->Show(FALSE
);
354 m_fsStatusBarFields
= 0;
355 #endif // wxUSE_STATUSBAR
360 wxToolBar
*theToolBar
= GetToolBar();
362 // restore the toolbar, menubar, and statusbar
363 if (theToolBar
&& (m_fsStyle
& wxFULLSCREEN_NOTOOLBAR
))
365 theToolBar
->SetSize(-1, m_fsToolBarHeight
);
366 theToolBar
->Show(TRUE
);
368 #endif // wxUSE_TOOLBAR
371 if ( m_fsStyle
& wxFULLSCREEN_NOSTATUSBAR
)
373 //CreateStatusBar(m_fsStatusBarFields);
376 GetStatusBar()->Show(TRUE
);
380 #endif // wxUSE_STATUSBAR
382 #ifndef __WXMICROWIN__
383 if ((m_fsStyle
& wxFULLSCREEN_NOMENUBAR
) && (m_hMenu
!= 0))
384 SetMenu((HWND
)GetHWND(), (HMENU
)m_hMenu
);
388 return wxFrameBase::ShowFullScreen(show
, style
);
391 // Default activation behaviour - set the focus for the first child
393 void wxFrame::OnActivate(wxActivateEvent
& event
)
395 if ( event
.GetActive() )
397 // restore focus to the child which was last focused
398 wxLogTrace(_T("focus"), _T("wxFrame %08x activated."), m_hWnd
);
400 wxWindow
*parent
= m_winLastFocused
? m_winLastFocused
->GetParent()
407 wxSetFocusToChild(parent
, &m_winLastFocused
);
411 // remember the last focused child if it is our child
412 m_winLastFocused
= FindFocus();
414 // so we NULL it out if it's a child from some other frame
415 wxWindow
*win
= m_winLastFocused
;
418 if ( win
->IsTopLevel() )
422 m_winLastFocused
= NULL
;
428 win
= win
->GetParent();
431 wxLogTrace(_T("focus"),
432 _T("wxFrame %08x deactivated, last focused: %08x."),
434 m_winLastFocused
? GetHwndOf(m_winLastFocused
)
441 // ----------------------------------------------------------------------------
442 // tool/status bar stuff
443 // ----------------------------------------------------------------------------
447 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
449 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
454 return m_frameToolBar
;
457 void wxFrame::PositionToolBar()
459 wxToolBar
*toolbar
= GetToolBar();
460 if ( toolbar
&& toolbar
->IsShown() )
462 // don't call our (or even wxTopLevelWindow) version because we want
463 // the real (full) client area size, not excluding the tool/status bar
465 wxWindow::DoGetClientSize(&width
, &height
);
468 wxStatusBar
*statbar
= GetStatusBar();
469 if ( statbar
&& statbar
->IsShown() )
471 height
-= statbar
->GetClientSize().y
;
473 #endif // wxUSE_STATUSBAR
476 toolbar
->GetSize(&tw
, &th
);
478 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
487 // use the 'real' MSW position here, don't offset relativly to the
488 // client area origin
489 toolbar
->SetSize(0, 0, tw
, th
, wxSIZE_NO_ADJUSTMENTS
);
493 #endif // wxUSE_TOOLBAR
495 // ----------------------------------------------------------------------------
496 // frame state (iconized/maximized/...)
497 // ----------------------------------------------------------------------------
499 // propagate our state change to all child frames: this allows us to emulate X
500 // Windows behaviour where child frames float independently of the parent one
501 // on the desktop, but are iconized/restored with it
502 void wxFrame::IconizeChildFrames(bool bIconize
)
504 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
506 node
= node
->GetNext() )
508 wxWindow
*win
= node
->GetData();
510 // iconizing the frames with this style under Win95 shell puts them at
511 // the bottom of the screen (as the MDI children) instead of making
512 // them appear in the taskbar because they are, by virtue of this
513 // style, not managed by the taskbar - instead leave Windows take care
516 if ( win
->GetWindowStyle() & wxFRAME_TOOL_WINDOW
)
520 // the child MDI frames are a special case and should not be touched by
521 // the parent frame - instead, they are managed by the user
522 wxFrame
*frame
= wxDynamicCast(win
, wxFrame
);
524 #if wxUSE_MDI_ARCHITECTURE
525 && !wxDynamicCast(frame
, wxMDIChildFrame
)
526 #endif // wxUSE_MDI_ARCHITECTURE
529 // we don't want to restore the child frames which had been
530 // iconized even before we were iconized, so save the child frame
531 // status when iconizing the parent frame and check it when
535 // note that we shouldn't touch the hidden frames neither
536 // because iconizing/restoring them would show them as a side
538 frame
->m_wasMinimized
= frame
->IsIconized() || !frame
->IsShown();
541 // this test works for both iconizing and restoring
542 if ( !frame
->m_wasMinimized
)
543 frame
->Iconize(bIconize
);
548 WXHICON
wxFrame::GetDefaultIcon() const
550 return (WXHICON
)(wxSTD_FRAME_ICON
? wxSTD_FRAME_ICON
551 : wxDEFAULT_FRAME_ICON
);
554 // ===========================================================================
555 // message processing
556 // ===========================================================================
558 // ---------------------------------------------------------------------------
560 // ---------------------------------------------------------------------------
562 bool wxFrame::MSWTranslateMessage(WXMSG
* pMsg
)
564 if ( wxWindow::MSWTranslateMessage(pMsg
) )
567 #if wxUSE_MENUS && wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
568 // try the menu bar accels
569 wxMenuBar
*menuBar
= GetMenuBar();
573 const wxAcceleratorTable
& acceleratorTable
= menuBar
->GetAccelTable();
574 return acceleratorTable
.Translate(this, pMsg
);
577 #endif // wxUSE_MENUS && wxUSE_ACCEL
580 // ---------------------------------------------------------------------------
581 // our private (non virtual) message handlers
582 // ---------------------------------------------------------------------------
584 bool wxFrame::HandlePaint()
587 if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) )
589 #ifndef __WXMICROWIN__
592 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
593 : (HICON
)GetDefaultIcon();
595 // Hold a pointer to the dc so long as the OnPaint() message
596 // is being processed
598 HDC hdc
= ::BeginPaint(GetHwnd(), &ps
);
600 // Erase background before painting or we get white background
601 MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L);
606 ::GetClientRect(GetHwnd(), &rect
);
608 // FIXME: why hardcoded?
609 static const int icon_width
= 32;
610 static const int icon_height
= 32;
612 int icon_x
= (int)((rect
.right
- icon_width
)/2);
613 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
615 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
);
618 ::EndPaint(GetHwnd(), &ps
);
625 return wxWindow::HandlePaint();
630 // nothing to paint - processed
635 bool wxFrame::HandleSize(int x
, int y
, WXUINT id
)
637 bool processed
= FALSE
;
638 #ifndef __WXMICROWIN__
643 // only do it it if we were iconized before, otherwise resizing the
644 // parent frame has a curious side effect of bringing it under it's
649 // restore all child frames too
650 IconizeChildFrames(FALSE
);
652 (void)SendIconizeEvent(FALSE
);
661 // iconize all child frames too
662 IconizeChildFrames(TRUE
);
664 (void)SendIconizeEvent();
675 #endif // wxUSE_STATUSBAR
679 #endif // wxUSE_TOOLBAR
681 wxSizeEvent
event(wxSize(x
, y
), m_windowId
);
682 event
.SetEventObject( this );
683 processed
= GetEventHandler()->ProcessEvent(event
);
689 bool wxFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
)
693 // In case it's e.g. a toolbar.
694 wxWindow
*win
= wxFindWinFromHandle(control
);
696 return win
->MSWCommand(cmd
, id
);
699 // handle here commands from menus and accelerators
700 if ( cmd
== 0 || cmd
== 1 )
702 #if wxUSE_MENUS_NATIVE
703 if ( wxCurrentPopupMenu
)
705 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
706 wxCurrentPopupMenu
= NULL
;
708 return popupMenu
->MSWCommand(cmd
, id
);
710 #endif // wxUSE_MENUS_NATIVE
712 if ( ProcessCommand(id
) )
721 bool wxFrame::HandleMenuSelect(WXWORD nItem
, WXWORD flags
, WXHMENU hMenu
)
724 if ( flags
== 0xFFFF && hMenu
== 0 )
726 // menu was removed from screen
729 #ifndef __WXMICROWIN__
730 else if ( !(flags
& MF_POPUP
) && !(flags
& MF_SEPARATOR
) )
738 // don't give hints for separators (doesn't make sense) nor for the
739 // items opening popup menus (they don't have them anyhow) but do clear
740 // the status line - otherwise, we would be left with the help message
741 // for the previous item which doesn't apply any more
742 wxStatusBar
*statbar
= GetStatusBar();
745 statbar
->SetStatusText(wxEmptyString
);
747 #endif // wxUSE_STATUSBAR
752 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, item
);
753 event
.SetEventObject( this );
755 return GetEventHandler()->ProcessEvent(event
);
758 // ---------------------------------------------------------------------------
759 // the window proc for wxFrame
760 // ---------------------------------------------------------------------------
762 long wxFrame::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
765 bool processed
= FALSE
;
770 // if we can't close, tell the system that we processed the
771 // message - otherwise it would close us
772 processed
= !Close();
779 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
782 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
786 #ifndef __WXMICROWIN__
791 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
793 processed
= HandleMenuSelect(item
, flags
, hmenu
);
799 processed
= HandlePaint();
802 #ifndef __WXMICROWIN__
803 case WM_QUERYDRAGICON
:
805 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
806 : (HICON
)GetDefaultIcon();
814 processed
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
);
819 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);