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 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
146 void wxFrame::DoGetClientSize(int *x
, int *y
) const
149 ::GetClientRect(GetHwnd(), &rect
);
152 if ( GetStatusBar() && GetStatusBar()->IsShown() )
154 int statusX
, statusY
;
155 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
156 rect
.bottom
-= statusY
;
158 #endif // wxUSE_STATUSBAR
160 wxPoint
pt(GetClientAreaOrigin());
170 void wxFrame::DoSetClientSize(int width
, int height
)
172 // leave enough space for the status bar if we have (and show) it
174 wxStatusBar
*statbar
= GetStatusBar();
175 if ( statbar
&& statbar
->IsShown() )
177 height
+= statbar
->GetSize().y
;
179 #endif // wxUSE_STATUSBAR
181 wxTopLevelWindow::DoSetClientSize(width
, height
);
184 // ----------------------------------------------------------------------------
185 // wxFrame: various geometry-related functions
186 // ----------------------------------------------------------------------------
188 void wxFrame::Raise()
191 // no SetForegroundWindow() in Win16
192 wxFrameBase::Raise();
194 ::SetForegroundWindow(GetHwnd());
198 // generate an artificial resize event
199 void wxFrame::SendSizeEvent()
203 RECT r
= wxGetWindowRect(GetHwnd());
205 (void)::PostMessage(GetHwnd(), WM_SIZE
,
206 IsMaximized() ? SIZE_MAXIMIZED
: SIZE_RESTORED
,
207 MAKELPARAM(r
.right
- r
.left
, r
.bottom
- r
.top
));
212 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
,
215 const wxString
& name
)
217 wxStatusBar
*statusBar
= NULL
;
219 #if wxUSE_NATIVE_STATUSBAR
220 if ( !UsesNativeStatusBar() )
222 statusBar
= (wxStatusBar
*)new wxStatusBarGeneric(this, id
, style
);
227 statusBar
= new wxStatusBar(this, id
, style
, name
);
230 statusBar
->SetFieldsCount(number
);
235 void wxFrame::PositionStatusBar()
237 if ( !m_frameStatusBar
)
241 GetClientSize(&w
, &h
);
243 m_frameStatusBar
->GetSize(&sw
, &sh
);
245 // Since we wish the status bar to be directly under the client area,
246 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
247 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
249 #endif // wxUSE_STATUSBAR
251 #if wxUSE_MENUS_NATIVE
253 void wxFrame::AttachMenuBar(wxMenuBar
*menubar
)
255 wxFrameBase::AttachMenuBar(menubar
);
259 // actually remove the menu from the frame
260 m_hMenu
= (WXHMENU
)0;
261 InternalSetMenuBar();
263 else // set new non NULL menu bar
265 // Can set a menubar several times.
266 if ( menubar
->GetHMenu() )
268 m_hMenu
= menubar
->GetHMenu();
272 m_hMenu
= menubar
->Create();
276 wxFAIL_MSG( _T("failed to create menu bar") );
281 InternalSetMenuBar();
285 void wxFrame::InternalSetMenuBar()
287 #ifndef __WXMICROWIN__
288 if ( !::SetMenu(GetHwnd(), (HMENU
)m_hMenu
) )
290 wxLogLastError(wxT("SetMenu"));
295 #endif // wxUSE_MENUS_NATIVE
297 // Responds to colour changes, and passes event on to children.
298 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
300 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
304 if ( m_frameStatusBar
)
306 wxSysColourChangedEvent event2
;
307 event2
.SetEventObject( m_frameStatusBar
);
308 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
310 #endif // wxUSE_STATUSBAR
312 // Propagate the event to the non-top-level children
313 wxWindow::OnSysColourChanged(event
);
316 // Pass TRUE to show full screen, FALSE to restore.
317 bool wxFrame::ShowFullScreen(bool show
, long style
)
319 if ( IsFullScreen() == show
)
325 wxToolBar
*theToolBar
= GetToolBar();
327 theToolBar
->GetSize(NULL
, &m_fsToolBarHeight
);
329 // zap the toolbar, menubar, and statusbar
331 if ((style
& wxFULLSCREEN_NOTOOLBAR
) && theToolBar
)
333 theToolBar
->SetSize(-1,0);
334 theToolBar
->Show(FALSE
);
336 #endif // wxUSE_TOOLBAR
338 #ifndef __WXMICROWIN__
339 if (style
& wxFULLSCREEN_NOMENUBAR
)
340 SetMenu((HWND
)GetHWND(), (HMENU
) NULL
);
344 wxStatusBar
*theStatusBar
= GetStatusBar();
346 theStatusBar
->GetSize(NULL
, &m_fsStatusBarHeight
);
348 // Save the number of fields in the statusbar
349 if ((style
& wxFULLSCREEN_NOSTATUSBAR
) && theStatusBar
)
351 //m_fsStatusBarFields = theStatusBar->GetFieldsCount();
352 //SetStatusBar((wxStatusBar*) NULL);
353 //delete theStatusBar;
354 theStatusBar
->Show(FALSE
);
357 m_fsStatusBarFields
= 0;
358 #endif // wxUSE_STATUSBAR
363 wxToolBar
*theToolBar
= GetToolBar();
365 // restore the toolbar, menubar, and statusbar
366 if (theToolBar
&& (m_fsStyle
& wxFULLSCREEN_NOTOOLBAR
))
368 theToolBar
->SetSize(-1, m_fsToolBarHeight
);
369 theToolBar
->Show(TRUE
);
371 #endif // wxUSE_TOOLBAR
374 if ( m_fsStyle
& wxFULLSCREEN_NOSTATUSBAR
)
376 //CreateStatusBar(m_fsStatusBarFields);
379 GetStatusBar()->Show(TRUE
);
383 #endif // wxUSE_STATUSBAR
385 #ifndef __WXMICROWIN__
386 if ((m_fsStyle
& wxFULLSCREEN_NOMENUBAR
) && (m_hMenu
!= 0))
387 SetMenu((HWND
)GetHWND(), (HMENU
)m_hMenu
);
391 return wxFrameBase::ShowFullScreen(show
, style
);
394 // Default activation behaviour - set the focus for the first child
396 void wxFrame::OnActivate(wxActivateEvent
& event
)
398 if ( event
.GetActive() )
400 // restore focus to the child which was last focused
401 wxLogTrace(_T("focus"), _T("wxFrame %08x activated."), m_hWnd
);
403 wxWindow
*parent
= m_winLastFocused
? m_winLastFocused
->GetParent()
410 wxSetFocusToChild(parent
, &m_winLastFocused
);
414 // remember the last focused child if it is our child
415 m_winLastFocused
= FindFocus();
417 // so we NULL it out if it's a child from some other frame
418 wxWindow
*win
= m_winLastFocused
;
421 if ( win
->IsTopLevel() )
425 m_winLastFocused
= NULL
;
431 win
= win
->GetParent();
434 wxLogTrace(_T("focus"),
435 _T("wxFrame %08x deactivated, last focused: %08x."),
437 m_winLastFocused
? GetHwndOf(m_winLastFocused
)
444 // ----------------------------------------------------------------------------
445 // tool/status bar stuff
446 // ----------------------------------------------------------------------------
450 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
452 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
457 return m_frameToolBar
;
460 void wxFrame::PositionToolBar()
463 ::GetClientRect(GetHwnd(), &rect
);
466 if ( GetStatusBar() )
468 int statusX
, statusY
;
469 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
470 rect
.bottom
-= statusY
;
472 #endif // wxUSE_STATUSBAR
474 if ( GetToolBar() && GetToolBar()->IsShown() )
477 GetToolBar()->GetSize(&tw
, &th
);
479 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
488 // Use the 'real' MSW position here
489 GetToolBar()->SetSize(0, 0, tw
, th
, wxSIZE_NO_ADJUSTMENTS
);
492 #endif // wxUSE_TOOLBAR
494 // ----------------------------------------------------------------------------
495 // frame state (iconized/maximized/...)
496 // ----------------------------------------------------------------------------
498 // propagate our state change to all child frames: this allows us to emulate X
499 // Windows behaviour where child frames float independently of the parent one
500 // on the desktop, but are iconized/restored with it
501 void wxFrame::IconizeChildFrames(bool bIconize
)
503 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
505 node
= node
->GetNext() )
507 wxWindow
*win
= node
->GetData();
509 // iconizing the frames with this style under Win95 shell puts them at
510 // the bottom of the screen (as the MDI children) instead of making
511 // them appear in the taskbar because they are, by virtue of this
512 // style, not managed by the taskbar - instead leave Windows take care
515 if ( win
->GetWindowStyle() & wxFRAME_TOOL_WINDOW
)
519 // the child MDI frames are a special case and should not be touched by
520 // the parent frame - instead, they are managed by the user
521 wxFrame
*frame
= wxDynamicCast(win
, wxFrame
);
523 #if wxUSE_MDI_ARCHITECTURE
524 && !wxDynamicCast(frame
, wxMDIChildFrame
)
525 #endif // wxUSE_MDI_ARCHITECTURE
528 // we don't want to restore the child frames which had been
529 // iconized even before we were iconized, so save the child frame
530 // status when iconizing the parent frame and check it when
534 frame
->m_wasMinimized
= frame
->IsIconized();
537 // this test works for both iconizing and restoring
538 if ( !frame
->m_wasMinimized
)
539 frame
->Iconize(bIconize
);
544 WXHICON
wxFrame::GetDefaultIcon() const
546 return (WXHICON
)(wxSTD_FRAME_ICON
? wxSTD_FRAME_ICON
547 : wxDEFAULT_FRAME_ICON
);
550 // ===========================================================================
551 // message processing
552 // ===========================================================================
554 // ---------------------------------------------------------------------------
556 // ---------------------------------------------------------------------------
558 bool wxFrame::MSWTranslateMessage(WXMSG
* pMsg
)
560 if ( wxWindow::MSWTranslateMessage(pMsg
) )
563 #if wxUSE_MENUS && wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
564 // try the menu bar accels
565 wxMenuBar
*menuBar
= GetMenuBar();
569 const wxAcceleratorTable
& acceleratorTable
= menuBar
->GetAccelTable();
570 return acceleratorTable
.Translate(this, pMsg
);
573 #endif // wxUSE_MENUS && wxUSE_ACCEL
576 // ---------------------------------------------------------------------------
577 // our private (non virtual) message handlers
578 // ---------------------------------------------------------------------------
580 bool wxFrame::HandlePaint()
583 if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) )
585 #ifndef __WXMICROWIN__
588 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
589 : (HICON
)GetDefaultIcon();
591 // Hold a pointer to the dc so long as the OnPaint() message
592 // is being processed
594 HDC hdc
= ::BeginPaint(GetHwnd(), &ps
);
596 // Erase background before painting or we get white background
597 MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L);
602 ::GetClientRect(GetHwnd(), &rect
);
604 // FIXME: why hardcoded?
605 static const int icon_width
= 32;
606 static const int icon_height
= 32;
608 int icon_x
= (int)((rect
.right
- icon_width
)/2);
609 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
611 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
);
614 ::EndPaint(GetHwnd(), &ps
);
621 return wxWindow::HandlePaint();
626 // nothing to paint - processed
631 bool wxFrame::HandleSize(int x
, int y
, WXUINT id
)
633 bool processed
= FALSE
;
634 #ifndef __WXMICROWIN__
639 // only do it it if we were iconized before, otherwise resizing the
640 // parent frame has a curious side effect of bringing it under it's
645 // restore all child frames too
646 IconizeChildFrames(FALSE
);
648 (void)SendIconizeEvent(FALSE
);
657 // iconize all child frames too
658 IconizeChildFrames(TRUE
);
660 (void)SendIconizeEvent();
671 #endif // wxUSE_STATUSBAR
675 #endif // wxUSE_TOOLBAR
677 wxSizeEvent
event(wxSize(x
, y
), m_windowId
);
678 event
.SetEventObject( this );
679 processed
= GetEventHandler()->ProcessEvent(event
);
685 bool wxFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
)
689 // In case it's e.g. a toolbar.
690 wxWindow
*win
= wxFindWinFromHandle(control
);
692 return win
->MSWCommand(cmd
, id
);
695 // handle here commands from menus and accelerators
696 if ( cmd
== 0 || cmd
== 1 )
698 #if wxUSE_MENUS_NATIVE
699 if ( wxCurrentPopupMenu
)
701 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
702 wxCurrentPopupMenu
= NULL
;
704 return popupMenu
->MSWCommand(cmd
, id
);
706 #endif // wxUSE_MENUS_NATIVE
708 if ( ProcessCommand(id
) )
717 bool wxFrame::HandleMenuSelect(WXWORD nItem
, WXWORD flags
, WXHMENU hMenu
)
720 if ( flags
== 0xFFFF && hMenu
== 0 )
722 // menu was removed from screen
725 #ifndef __WXMICROWIN__
726 else if ( !(flags
& MF_POPUP
) && !(flags
& MF_SEPARATOR
) )
734 // don't give hints for separators (doesn't make sense) nor for the
735 // items opening popup menus (they don't have them anyhow) but do clear
736 // the status line - otherwise, we would be left with the help message
737 // for the previous item which doesn't apply any more
738 wxStatusBar
*statbar
= GetStatusBar();
741 statbar
->SetStatusText(wxEmptyString
);
743 #endif // wxUSE_STATUSBAR
748 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, item
);
749 event
.SetEventObject( this );
751 return GetEventHandler()->ProcessEvent(event
);
754 // ---------------------------------------------------------------------------
755 // the window proc for wxFrame
756 // ---------------------------------------------------------------------------
758 long wxFrame::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
761 bool processed
= FALSE
;
766 // if we can't close, tell the system that we processed the
767 // message - otherwise it would close us
768 processed
= !Close();
775 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
778 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
782 #ifndef __WXMICROWIN__
787 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
789 processed
= HandleMenuSelect(item
, flags
, hmenu
);
795 processed
= HandlePaint();
798 #ifndef __WXMICROWIN__
799 case WM_QUERYDRAGICON
:
801 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
802 : (HICON
)GetDefaultIcon();
810 processed
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
);
815 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);