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 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxTopLevelWindow
)
84 // ============================================================================
86 // ============================================================================
88 // ----------------------------------------------------------------------------
89 // static class members
90 // ----------------------------------------------------------------------------
93 #if wxUSE_NATIVE_STATUSBAR
94 bool wxFrame::m_useNativeStatusBar
= TRUE
;
96 bool wxFrame::m_useNativeStatusBar
= FALSE
;
98 #endif // wxUSE_NATIVE_STATUSBAR
100 // ----------------------------------------------------------------------------
101 // creation/destruction
102 // ----------------------------------------------------------------------------
113 // Data to save/restore when calling ShowFullScreen
114 m_fsStatusBarFields
= 0;
115 m_fsStatusBarHeight
= 0;
116 m_fsToolBarHeight
= 0;
118 m_wasMinimized
= FALSE
;
121 bool wxFrame::Create(wxWindow
*parent
,
123 const wxString
& title
,
127 const wxString
& name
)
129 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
132 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
134 wxModelessWindows
.Append(this);
141 m_isBeingDeleted
= TRUE
;
146 ::DestroyWindow((HWND
) m_commandBar
);
153 // ----------------------------------------------------------------------------
154 // wxFrame client size calculations
155 // ----------------------------------------------------------------------------
157 void wxFrame::DoSetClientSize(int width
, int height
)
159 // leave enough space for the status bar if we have (and show) it
161 wxStatusBar
*statbar
= GetStatusBar();
162 if ( statbar
&& statbar
->IsShown() )
164 height
+= statbar
->GetSize().y
;
166 #endif // wxUSE_STATUSBAR
168 // call GetClientAreaOrigin() to take the toolbar into account
169 wxPoint pt
= GetClientAreaOrigin();
173 wxTopLevelWindow::DoSetClientSize(width
, height
);
176 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
177 void wxFrame::DoGetClientSize(int *x
, int *y
) const
179 wxTopLevelWindow::DoGetClientSize(x
, y
);
181 // account for the possible toolbar
182 wxPoint pt
= GetClientAreaOrigin();
190 // adjust client area height to take the status bar into account
193 wxStatusBar
*statbar
= GetStatusBar();
194 if ( statbar
&& statbar
->IsShown() )
196 *y
-= statbar
->GetClientSize().y
;
199 #endif // wxUSE_STATUSBAR
202 // ----------------------------------------------------------------------------
203 // wxFrame: various geometry-related functions
204 // ----------------------------------------------------------------------------
206 void wxFrame::Raise()
208 ::SetForegroundWindow(GetHwnd());
211 // generate an artificial resize event
212 void wxFrame::SendSizeEvent()
216 RECT r
= wxGetWindowRect(GetHwnd());
218 (void)::PostMessage(GetHwnd(), WM_SIZE
,
219 IsMaximized() ? SIZE_MAXIMIZED
: SIZE_RESTORED
,
220 MAKELPARAM(r
.right
- r
.left
, r
.bottom
- r
.top
));
225 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
,
228 const wxString
& name
)
230 wxStatusBar
*statusBar
= NULL
;
232 #if wxUSE_NATIVE_STATUSBAR
233 if ( !UsesNativeStatusBar() )
235 statusBar
= (wxStatusBar
*)new wxStatusBarGeneric(this, id
, style
);
240 statusBar
= new wxStatusBar(this, id
, style
, name
);
243 statusBar
->SetFieldsCount(number
);
248 void wxFrame::PositionStatusBar()
250 if ( !m_frameStatusBar
|| !m_frameStatusBar
->IsShown() )
254 GetClientSize(&w
, &h
);
256 m_frameStatusBar
->GetSize(&sw
, &sh
);
258 // Since we wish the status bar to be directly under the client area,
259 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
260 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
262 #endif // wxUSE_STATUSBAR
264 #if wxUSE_MENUS_NATIVE
266 void wxFrame::AttachMenuBar(wxMenuBar
*menubar
)
268 wxFrameBase::AttachMenuBar(menubar
);
272 // actually remove the menu from the frame
273 m_hMenu
= (WXHMENU
)0;
274 InternalSetMenuBar();
276 else // set new non NULL menu bar
278 // Can set a menubar several times.
279 if ( menubar
->GetHMenu() )
281 m_hMenu
= menubar
->GetHMenu();
285 m_hMenu
= menubar
->Create();
289 wxFAIL_MSG( _T("failed to create menu bar") );
294 InternalSetMenuBar();
298 void wxFrame::InternalSetMenuBar()
300 #ifdef __WXMICROWIN__
302 #elif defined(__WXWINCE__)
305 // TODO: what identifer shall we use?
306 // TODO: eventually have a wxCommandBar class
307 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), GetHwnd(), 999);
311 CommandBar_InsertMenubarEx((HWND
) m_commandBar
, wxGetInstance(),
312 (LPTSTR
) (HMENU
) m_hMenu
, 0);
315 if ( !::SetMenu(GetHwnd(), (HMENU
)m_hMenu
) )
317 wxLogLastError(wxT("SetMenu"));
322 #endif // wxUSE_MENUS_NATIVE
324 // Responds to colour changes, and passes event on to children.
325 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
327 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
331 if ( m_frameStatusBar
)
333 wxSysColourChangedEvent event2
;
334 event2
.SetEventObject( m_frameStatusBar
);
335 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
337 #endif // wxUSE_STATUSBAR
339 // Propagate the event to the non-top-level children
340 wxWindow::OnSysColourChanged(event
);
343 // Pass TRUE to show full screen, FALSE to restore.
344 bool wxFrame::ShowFullScreen(bool show
, long style
)
346 if ( IsFullScreen() == show
)
352 wxToolBar
*theToolBar
= GetToolBar();
354 theToolBar
->GetSize(NULL
, &m_fsToolBarHeight
);
356 // zap the toolbar, menubar, and statusbar
358 if ((style
& wxFULLSCREEN_NOTOOLBAR
) && theToolBar
)
360 theToolBar
->SetSize(-1,0);
361 theToolBar
->Show(FALSE
);
363 #endif // wxUSE_TOOLBAR
365 // TODO: make it work for WinCE
366 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
367 if (style
& wxFULLSCREEN_NOMENUBAR
)
368 SetMenu((HWND
)GetHWND(), (HMENU
) NULL
);
372 wxStatusBar
*theStatusBar
= GetStatusBar();
374 theStatusBar
->GetSize(NULL
, &m_fsStatusBarHeight
);
376 // Save the number of fields in the statusbar
377 if ((style
& wxFULLSCREEN_NOSTATUSBAR
) && theStatusBar
)
379 //m_fsStatusBarFields = theStatusBar->GetFieldsCount();
380 //SetStatusBar((wxStatusBar*) NULL);
381 //delete theStatusBar;
382 theStatusBar
->Show(FALSE
);
385 m_fsStatusBarFields
= 0;
386 #endif // wxUSE_STATUSBAR
391 wxToolBar
*theToolBar
= GetToolBar();
393 // restore the toolbar, menubar, and statusbar
394 if (theToolBar
&& (m_fsStyle
& wxFULLSCREEN_NOTOOLBAR
))
396 theToolBar
->SetSize(-1, m_fsToolBarHeight
);
397 theToolBar
->Show(TRUE
);
399 #endif // wxUSE_TOOLBAR
402 if ( m_fsStyle
& wxFULLSCREEN_NOSTATUSBAR
)
404 //CreateStatusBar(m_fsStatusBarFields);
407 GetStatusBar()->Show(TRUE
);
411 #endif // wxUSE_STATUSBAR
413 // TODO: make it work for WinCE
414 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
415 if ((m_fsStyle
& wxFULLSCREEN_NOMENUBAR
) && (m_hMenu
!= 0))
416 SetMenu((HWND
)GetHWND(), (HMENU
)m_hMenu
);
420 return wxFrameBase::ShowFullScreen(show
, style
);
423 // ----------------------------------------------------------------------------
424 // tool/status bar stuff
425 // ----------------------------------------------------------------------------
429 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
431 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
436 return m_frameToolBar
;
439 void wxFrame::PositionToolBar()
441 wxToolBar
*toolbar
= GetToolBar();
442 if ( toolbar
&& toolbar
->IsShown() )
444 // don't call our (or even wxTopLevelWindow) version because we want
445 // the real (full) client area size, not excluding the tool/status bar
447 wxWindow::DoGetClientSize(&width
, &height
);
450 wxStatusBar
*statbar
= GetStatusBar();
451 if ( statbar
&& statbar
->IsShown() )
453 height
-= statbar
->GetClientSize().y
;
455 #endif // wxUSE_STATUSBAR
458 toolbar
->GetSize(&tw
, &th
);
460 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
467 if ( toolbar
->GetWindowStyleFlag() & wxTB_FLAT
)
471 // use the 'real' MSW position here, don't offset relativly to the
472 // client area origin
473 toolbar
->SetSize(0, 0, tw
, th
, wxSIZE_NO_ADJUSTMENTS
);
477 #endif // wxUSE_TOOLBAR
479 // ----------------------------------------------------------------------------
480 // frame state (iconized/maximized/...)
481 // ----------------------------------------------------------------------------
483 // propagate our state change to all child frames: this allows us to emulate X
484 // Windows behaviour where child frames float independently of the parent one
485 // on the desktop, but are iconized/restored with it
486 void wxFrame::IconizeChildFrames(bool bIconize
)
488 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
490 node
= node
->GetNext() )
492 wxWindow
*win
= node
->GetData();
494 // iconizing the frames with this style under Win95 shell puts them at
495 // the bottom of the screen (as the MDI children) instead of making
496 // them appear in the taskbar because they are, by virtue of this
497 // style, not managed by the taskbar - instead leave Windows take care
500 if ( win
->GetWindowStyle() & wxFRAME_TOOL_WINDOW
)
504 // the child MDI frames are a special case and should not be touched by
505 // the parent frame - instead, they are managed by the user
506 wxFrame
*frame
= wxDynamicCast(win
, wxFrame
);
508 #if wxUSE_MDI_ARCHITECTURE
509 && !wxDynamicCast(frame
, wxMDIChildFrame
)
510 #endif // wxUSE_MDI_ARCHITECTURE
513 // we don't want to restore the child frames which had been
514 // iconized even before we were iconized, so save the child frame
515 // status when iconizing the parent frame and check it when
519 // note that we shouldn't touch the hidden frames neither
520 // because iconizing/restoring them would show them as a side
522 frame
->m_wasMinimized
= frame
->IsIconized() || !frame
->IsShown();
525 // this test works for both iconizing and restoring
526 if ( !frame
->m_wasMinimized
)
527 frame
->Iconize(bIconize
);
532 WXHICON
wxFrame::GetDefaultIcon() const
534 // we don't have any standard icons (any more)
538 // ===========================================================================
539 // message processing
540 // ===========================================================================
542 // ---------------------------------------------------------------------------
544 // ---------------------------------------------------------------------------
546 bool wxFrame::MSWTranslateMessage(WXMSG
* pMsg
)
548 if ( wxWindow::MSWTranslateMessage(pMsg
) )
551 #if wxUSE_MENUS && wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
552 // try the menu bar accels
553 wxMenuBar
*menuBar
= GetMenuBar();
557 const wxAcceleratorTable
& acceleratorTable
= menuBar
->GetAccelTable();
558 return acceleratorTable
.Translate(this, pMsg
);
561 #endif // wxUSE_MENUS && wxUSE_ACCEL
564 // ---------------------------------------------------------------------------
565 // our private (non virtual) message handlers
566 // ---------------------------------------------------------------------------
568 bool wxFrame::HandlePaint()
571 if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) )
573 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
576 const wxIcon
& icon
= GetIcon();
577 HICON hIcon
= icon
.Ok() ? GetHiconOf(icon
)
578 : (HICON
)GetDefaultIcon();
580 // Hold a pointer to the dc so long as the OnPaint() message
581 // is being processed
583 HDC hdc
= ::BeginPaint(GetHwnd(), &ps
);
585 // Erase background before painting or we get white background
586 MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L);
591 ::GetClientRect(GetHwnd(), &rect
);
593 // FIXME: why hardcoded?
594 static const int icon_width
= 32;
595 static const int icon_height
= 32;
597 int icon_x
= (int)((rect
.right
- icon_width
)/2);
598 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
600 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
);
603 ::EndPaint(GetHwnd(), &ps
);
610 return wxWindow::HandlePaint();
615 // nothing to paint - processed
620 bool wxFrame::HandleSize(int x
, int y
, WXUINT id
)
622 bool processed
= FALSE
;
623 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
628 // only do it it if we were iconized before, otherwise resizing the
629 // parent frame has a curious side effect of bringing it under it's
634 // restore all child frames too
635 IconizeChildFrames(FALSE
);
637 (void)SendIconizeEvent(FALSE
);
646 // iconize all child frames too
647 IconizeChildFrames(TRUE
);
649 (void)SendIconizeEvent();
660 #endif // wxUSE_STATUSBAR
664 #endif // wxUSE_TOOLBAR
666 processed
= wxWindow::HandleSize(x
, y
, id
);
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
) )
720 // don't give hints for separators (doesn't make sense) nor for the
721 // items opening popup menus (they don't have them anyhow) but do clear
722 // the status line - otherwise, we would be left with the help message
723 // for the previous item which doesn't apply any more
724 DoGiveHelp(wxEmptyString
, FALSE
);
729 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, item
);
730 event
.SetEventObject(this);
732 return GetEventHandler()->ProcessEvent(event
);
735 bool wxFrame::HandleMenuLoop(const wxEventType
& evtType
, WXWORD isPopup
)
737 // we don't have the menu id here, so we use the id to specify if the event
738 // was from a popup menu or a normal one
739 wxMenuEvent
event(evtType
, isPopup
? -1 : 0);
740 event
.SetEventObject(this);
742 return GetEventHandler()->ProcessEvent(event
);
745 // ---------------------------------------------------------------------------
746 // the window proc for wxFrame
747 // ---------------------------------------------------------------------------
749 long wxFrame::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
752 bool processed
= FALSE
;
757 // if we can't close, tell the system that we processed the
758 // message - otherwise it would close us
759 processed
= !Close();
763 processed
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
);
770 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
773 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
778 processed
= HandlePaint();
781 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
786 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
788 processed
= HandleMenuSelect(item
, flags
, hmenu
);
793 processed
= HandleInitMenu();
796 case WM_EXITMENULOOP
:
797 processed
= HandleMenuLoop(wxEVT_MENU_CLOSE
, wParam
);
800 case WM_QUERYDRAGICON
:
802 const wxIcon
& icon
= GetIcon();
803 HICON hIcon
= icon
.Ok() ? GetHiconOf(icon
)
804 : (HICON
)GetDefaultIcon();
809 #endif // !__WXMICROWIN__
813 rc
= wxFrameBase::MSWWindowProc(message
, wParam
, lParam
);
818 // handle WM_INITMENU message
819 bool wxFrame::HandleInitMenu()
821 wxMenuEvent
event(wxEVT_MENU_OPEN
, 0);
822 event
.SetEventObject(this);
824 return GetEventHandler()->ProcessEvent(event
);