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_winLastFocused
= (wxWindow
*)NULL
;
118 bool wxFrame::Create(wxWindow
*parent
,
120 const wxString
& title
,
124 const wxString
& name
)
126 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
129 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
131 wxModelessWindows
.Append(this);
138 m_isBeingDeleted
= TRUE
;
143 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
144 void wxFrame::DoGetClientSize(int *x
, int *y
) const
147 ::GetClientRect(GetHwnd(), &rect
);
150 if ( GetStatusBar() && GetStatusBar()->IsShown() )
152 int statusX
, statusY
;
153 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
154 rect
.bottom
-= statusY
;
156 #endif // wxUSE_STATUSBAR
158 wxPoint
pt(GetClientAreaOrigin());
168 void wxFrame::DoSetClientSize(int width
, int height
)
170 // leave enough space for the status bar if we have (and show) it
172 wxStatusBar
*statbar
= GetStatusBar();
173 if ( statbar
&& statbar
->IsShown() )
175 height
+= statbar
->GetSize().y
;
177 #endif // wxUSE_STATUSBAR
179 wxTopLevelWindow::DoSetClientSize(width
, height
);
182 // ----------------------------------------------------------------------------
183 // wxFrame: various geometry-related functions
184 // ----------------------------------------------------------------------------
186 void wxFrame::Raise()
189 // no SetForegroundWindow() in Win16
190 wxFrameBase::Raise();
192 ::SetForegroundWindow(GetHwnd());
196 // generate an artificial resize event
197 void wxFrame::SendSizeEvent()
201 RECT r
= wxGetWindowRect(GetHwnd());
203 (void)::PostMessage(GetHwnd(), WM_SIZE
,
204 IsMaximized() ? SIZE_MAXIMIZED
: SIZE_RESTORED
,
205 MAKELPARAM(r
.right
- r
.left
, r
.bottom
- r
.top
));
210 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
,
213 const wxString
& name
)
215 wxStatusBar
*statusBar
= NULL
;
217 #if wxUSE_NATIVE_STATUSBAR
218 if ( !UsesNativeStatusBar() )
220 statusBar
= (wxStatusBar
*)new wxStatusBarGeneric(this, id
, style
);
225 statusBar
= new wxStatusBar(this, id
, style
, name
);
228 statusBar
->SetFieldsCount(number
);
233 void wxFrame::PositionStatusBar()
235 if ( !m_frameStatusBar
)
239 GetClientSize(&w
, &h
);
241 m_frameStatusBar
->GetSize(&sw
, &sh
);
243 // Since we wish the status bar to be directly under the client area,
244 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
245 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
247 #endif // wxUSE_STATUSBAR
249 #if wxUSE_MENUS_NATIVE
251 void wxFrame::AttachMenuBar(wxMenuBar
*menubar
)
253 wxFrameBase::AttachMenuBar(menubar
);
257 // actually remove the menu from the frame
258 m_hMenu
= (WXHMENU
)0;
259 InternalSetMenuBar();
261 else // set new non NULL menu bar
263 // Can set a menubar several times.
264 if ( menubar
->GetHMenu() )
266 m_hMenu
= menubar
->GetHMenu();
270 m_hMenu
= menubar
->Create();
274 wxFAIL_MSG( _T("failed to create menu bar") );
279 InternalSetMenuBar();
283 void wxFrame::InternalSetMenuBar()
285 #ifndef __WXMICROWIN__
286 if ( !::SetMenu(GetHwnd(), (HMENU
)m_hMenu
) )
288 wxLogLastError(wxT("SetMenu"));
293 #endif // wxUSE_MENUS_NATIVE
295 // Responds to colour changes, and passes event on to children.
296 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
298 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
302 if ( m_frameStatusBar
)
304 wxSysColourChangedEvent event2
;
305 event2
.SetEventObject( m_frameStatusBar
);
306 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
308 #endif // wxUSE_STATUSBAR
310 // Propagate the event to the non-top-level children
311 wxWindow::OnSysColourChanged(event
);
314 // Pass TRUE to show full screen, FALSE to restore.
315 bool wxFrame::ShowFullScreen(bool show
, long style
)
317 if ( IsFullScreen() == show
)
323 wxToolBar
*theToolBar
= GetToolBar();
325 theToolBar
->GetSize(NULL
, &m_fsToolBarHeight
);
327 // zap the toolbar, menubar, and statusbar
329 if ((style
& wxFULLSCREEN_NOTOOLBAR
) && theToolBar
)
331 theToolBar
->SetSize(-1,0);
332 theToolBar
->Show(FALSE
);
334 #endif // wxUSE_TOOLBAR
336 #ifndef __WXMICROWIN__
337 if (style
& wxFULLSCREEN_NOMENUBAR
)
338 SetMenu((HWND
)GetHWND(), (HMENU
) NULL
);
342 wxStatusBar
*theStatusBar
= GetStatusBar();
344 theStatusBar
->GetSize(NULL
, &m_fsStatusBarHeight
);
346 // Save the number of fields in the statusbar
347 if ((style
& wxFULLSCREEN_NOSTATUSBAR
) && theStatusBar
)
349 //m_fsStatusBarFields = theStatusBar->GetFieldsCount();
350 //SetStatusBar((wxStatusBar*) NULL);
351 //delete theStatusBar;
352 theStatusBar
->Show(FALSE
);
355 m_fsStatusBarFields
= 0;
356 #endif // wxUSE_STATUSBAR
361 wxToolBar
*theToolBar
= GetToolBar();
363 // restore the toolbar, menubar, and statusbar
364 if (theToolBar
&& (m_fsStyle
& wxFULLSCREEN_NOTOOLBAR
))
366 theToolBar
->SetSize(-1, m_fsToolBarHeight
);
367 theToolBar
->Show(TRUE
);
369 #endif // wxUSE_TOOLBAR
372 if ( m_fsStyle
& wxFULLSCREEN_NOSTATUSBAR
)
374 //CreateStatusBar(m_fsStatusBarFields);
377 GetStatusBar()->Show(TRUE
);
381 #endif // wxUSE_STATUSBAR
383 #ifndef __WXMICROWIN__
384 if ((m_fsStyle
& wxFULLSCREEN_NOMENUBAR
) && (m_hMenu
!= 0))
385 SetMenu((HWND
)GetHWND(), (HMENU
)m_hMenu
);
389 return wxFrameBase::ShowFullScreen(show
, style
);
392 // Default activation behaviour - set the focus for the first child
394 void wxFrame::OnActivate(wxActivateEvent
& event
)
396 if ( event
.GetActive() )
398 // restore focus to the child which was last focused
399 wxLogTrace(_T("focus"), _T("wxFrame %08x activated."), m_hWnd
);
401 wxWindow
*parent
= m_winLastFocused
? m_winLastFocused
->GetParent()
408 wxSetFocusToChild(parent
, &m_winLastFocused
);
412 // remember the last focused child if it is our child
413 m_winLastFocused
= FindFocus();
415 // so we NULL it out if it's a child from some other frame
416 wxWindow
*win
= m_winLastFocused
;
419 if ( win
->IsTopLevel() )
423 m_winLastFocused
= NULL
;
429 win
= win
->GetParent();
432 wxLogTrace(_T("focus"),
433 _T("wxFrame %08x deactivated, last focused: %08x."),
435 m_winLastFocused
? GetHwndOf(m_winLastFocused
)
442 // ----------------------------------------------------------------------------
443 // tool/status bar stuff
444 // ----------------------------------------------------------------------------
448 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
450 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
455 return m_frameToolBar
;
458 void wxFrame::PositionToolBar()
461 ::GetClientRect(GetHwnd(), &rect
);
464 if ( GetStatusBar() )
466 int statusX
, statusY
;
467 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
468 rect
.bottom
-= statusY
;
470 #endif // wxUSE_STATUSBAR
472 if ( GetToolBar() && GetToolBar()->IsShown() )
475 GetToolBar()->GetSize(&tw
, &th
);
477 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
486 // Use the 'real' MSW position here
487 GetToolBar()->SetSize(0, 0, tw
, th
, wxSIZE_NO_ADJUSTMENTS
);
490 #endif // wxUSE_TOOLBAR
492 // ----------------------------------------------------------------------------
493 // frame state (iconized/maximized/...)
494 // ----------------------------------------------------------------------------
496 // propagate our state change to all child frames: this allows us to emulate X
497 // Windows behaviour where child frames float independently of the parent one
498 // on the desktop, but are iconized/restored with it
499 void wxFrame::IconizeChildFrames(bool bIconize
)
501 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
503 node
= node
->GetNext() )
505 wxWindow
*win
= node
->GetData();
507 // iconizing the frames with this style under Win95 shell puts them at
508 // the bottom of the screen (as the MDI children) instead of making
509 // them appear in the taskbar because they are, by virtue of this
510 // style, not managed by the taskbar - instead leave Windows take care
513 if ( win
->GetWindowStyle() & wxFRAME_TOOL_WINDOW
)
517 // the child MDI frames are a special case and should not be touched by
518 // the parent frame - instead, they are managed by the user
519 wxFrame
*frame
= wxDynamicCast(win
, wxFrame
);
521 #if wxUSE_MDI_ARCHITECTURE
522 && !wxDynamicCast(frame
, wxMDIChildFrame
)
523 #endif // wxUSE_MDI_ARCHITECTURE
526 frame
->Iconize(bIconize
);
531 WXHICON
wxFrame::GetDefaultIcon() const
533 return (WXHICON
)(wxSTD_FRAME_ICON
? wxSTD_FRAME_ICON
534 : wxDEFAULT_FRAME_ICON
);
537 // ===========================================================================
538 // message processing
539 // ===========================================================================
541 // ---------------------------------------------------------------------------
543 // ---------------------------------------------------------------------------
545 bool wxFrame::MSWTranslateMessage(WXMSG
* pMsg
)
547 if ( wxWindow::MSWTranslateMessage(pMsg
) )
550 #if wxUSE_MENUS && wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
551 // try the menu bar accels
552 wxMenuBar
*menuBar
= GetMenuBar();
556 const wxAcceleratorTable
& acceleratorTable
= menuBar
->GetAccelTable();
557 return acceleratorTable
.Translate(this, pMsg
);
560 #endif // wxUSE_MENUS && wxUSE_ACCEL
563 // ---------------------------------------------------------------------------
564 // our private (non virtual) message handlers
565 // ---------------------------------------------------------------------------
567 bool wxFrame::HandlePaint()
570 if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) )
572 #ifndef __WXMICROWIN__
575 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
576 : (HICON
)GetDefaultIcon();
578 // Hold a pointer to the dc so long as the OnPaint() message
579 // is being processed
581 HDC hdc
= ::BeginPaint(GetHwnd(), &ps
);
583 // Erase background before painting or we get white background
584 MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L);
589 ::GetClientRect(GetHwnd(), &rect
);
591 // FIXME: why hardcoded?
592 static const int icon_width
= 32;
593 static const int icon_height
= 32;
595 int icon_x
= (int)((rect
.right
- icon_width
)/2);
596 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
598 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
);
601 ::EndPaint(GetHwnd(), &ps
);
608 return wxWindow::HandlePaint();
613 // nothing to paint - processed
618 bool wxFrame::HandleSize(int x
, int y
, WXUINT id
)
620 bool processed
= FALSE
;
621 #ifndef __WXMICROWIN__
626 // only do it it if we were iconized before, otherwise resizing the
627 // parent frame has a curious side effect of bringing it under it's
632 // restore all child frames too
633 IconizeChildFrames(FALSE
);
635 (void)SendIconizeEvent(FALSE
);
644 // iconize all child frames too
645 IconizeChildFrames(TRUE
);
647 (void)SendIconizeEvent();
658 #endif // wxUSE_STATUSBAR
662 #endif // wxUSE_TOOLBAR
664 wxSizeEvent
event(wxSize(x
, y
), m_windowId
);
665 event
.SetEventObject( this );
666 processed
= GetEventHandler()->ProcessEvent(event
);
672 bool wxFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
)
676 // In case it's e.g. a toolbar.
677 wxWindow
*win
= wxFindWinFromHandle(control
);
679 return win
->MSWCommand(cmd
, id
);
682 // handle here commands from menus and accelerators
683 if ( cmd
== 0 || cmd
== 1 )
685 #if wxUSE_MENUS_NATIVE
686 if ( wxCurrentPopupMenu
)
688 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
689 wxCurrentPopupMenu
= NULL
;
691 return popupMenu
->MSWCommand(cmd
, id
);
693 #endif // wxUSE_MENUS_NATIVE
695 if ( ProcessCommand(id
) )
704 bool wxFrame::HandleMenuSelect(WXWORD nItem
, WXWORD flags
, WXHMENU hMenu
)
707 if ( flags
== 0xFFFF && hMenu
== 0 )
709 // menu was removed from screen
712 #ifndef __WXMICROWIN__
713 else if ( !(flags
& MF_POPUP
) && !(flags
& MF_SEPARATOR
) )
721 // don't give hints for separators (doesn't make sense) nor for the
722 // items opening popup menus (they don't have them anyhow) but do clear
723 // the status line - otherwise, we would be left with the help message
724 // for the previous item which doesn't apply any more
725 wxStatusBar
*statbar
= GetStatusBar();
728 statbar
->SetStatusText(wxEmptyString
);
730 #endif // wxUSE_STATUSBAR
735 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, item
);
736 event
.SetEventObject( this );
738 return GetEventHandler()->ProcessEvent(event
);
741 // ---------------------------------------------------------------------------
742 // the window proc for wxFrame
743 // ---------------------------------------------------------------------------
745 long wxFrame::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
748 bool processed
= FALSE
;
753 // if we can't close, tell the system that we processed the
754 // message - otherwise it would close us
755 processed
= !Close();
762 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
765 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
769 #ifndef __WXMICROWIN__
774 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
776 processed
= HandleMenuSelect(item
, flags
, hmenu
);
782 processed
= HandlePaint();
785 #ifndef __WXMICROWIN__
786 case WM_QUERYDRAGICON
:
788 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
789 : (HICON
)GetDefaultIcon();
797 processed
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
);
802 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);