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 // ----------------------------------------------------------------------------
110 // Data to save/restore when calling ShowFullScreen
111 m_fsStatusBarFields
= 0;
112 m_fsStatusBarHeight
= 0;
113 m_fsToolBarHeight
= 0;
115 m_wasMinimized
= FALSE
;
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::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
131 wxModelessWindows
.Append(this);
138 m_isBeingDeleted
= TRUE
;
142 // ----------------------------------------------------------------------------
143 // wxFrame client size calculations
144 // ----------------------------------------------------------------------------
146 void wxFrame::DoSetClientSize(int width
, int height
)
148 // leave enough space for the status bar if we have (and show) it
150 wxStatusBar
*statbar
= GetStatusBar();
151 if ( statbar
&& statbar
->IsShown() )
153 height
+= statbar
->GetSize().y
;
155 #endif // wxUSE_STATUSBAR
157 // call GetClientAreaOrigin() to take the toolbar into account
158 wxPoint pt
= GetClientAreaOrigin();
162 wxTopLevelWindow::DoSetClientSize(width
, height
);
165 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
166 void wxFrame::DoGetClientSize(int *x
, int *y
) const
168 wxTopLevelWindow::DoGetClientSize(x
, y
);
170 // account for the possible toolbar
171 wxPoint pt
= GetClientAreaOrigin();
179 // adjust client area height to take the status bar into account
182 wxStatusBar
*statbar
= GetStatusBar();
183 if ( statbar
&& statbar
->IsShown() )
185 *y
-= statbar
->GetClientSize().y
;
188 #endif // wxUSE_STATUSBAR
191 // ----------------------------------------------------------------------------
192 // wxFrame: various geometry-related functions
193 // ----------------------------------------------------------------------------
195 void wxFrame::Raise()
197 ::SetForegroundWindow(GetHwnd());
200 // generate an artificial resize event
201 void wxFrame::SendSizeEvent()
205 RECT r
= wxGetWindowRect(GetHwnd());
207 (void)::PostMessage(GetHwnd(), WM_SIZE
,
208 IsMaximized() ? SIZE_MAXIMIZED
: SIZE_RESTORED
,
209 MAKELPARAM(r
.right
- r
.left
, r
.bottom
- r
.top
));
214 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
,
217 const wxString
& name
)
219 wxStatusBar
*statusBar
= NULL
;
221 #if wxUSE_NATIVE_STATUSBAR
222 if ( !UsesNativeStatusBar() )
224 statusBar
= (wxStatusBar
*)new wxStatusBarGeneric(this, id
, style
);
229 statusBar
= new wxStatusBar(this, id
, style
, name
);
232 statusBar
->SetFieldsCount(number
);
237 void wxFrame::PositionStatusBar()
239 if ( !m_frameStatusBar
|| !m_frameStatusBar
->IsShown() )
243 GetClientSize(&w
, &h
);
245 m_frameStatusBar
->GetSize(&sw
, &sh
);
247 // Since we wish the status bar to be directly under the client area,
248 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
249 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
251 #endif // wxUSE_STATUSBAR
253 #if wxUSE_MENUS_NATIVE
255 void wxFrame::AttachMenuBar(wxMenuBar
*menubar
)
257 wxFrameBase::AttachMenuBar(menubar
);
261 // actually remove the menu from the frame
262 m_hMenu
= (WXHMENU
)0;
263 InternalSetMenuBar();
265 else // set new non NULL menu bar
268 // Can set a menubar several times.
269 if ( menubar
->GetHMenu() )
271 m_hMenu
= menubar
->GetHMenu();
275 m_hMenu
= menubar
->Create();
279 wxFAIL_MSG( _T("failed to create menu bar") );
284 InternalSetMenuBar();
288 void wxFrame::InternalSetMenuBar()
290 #ifdef __WXMICROWIN__
292 #elif defined(__WXWINCE__)
296 wxToolBar
* toolBar
= new wxToolBar(this, -1,
297 wxDefaultPosition
, wxDefaultSize
,
298 wxBORDER_NONE
| wxTB_HORIZONTAL
,
299 wxToolBarNameStr
, GetMenuBar());
303 if ( !::SetMenu(GetHwnd(), (HMENU
)m_hMenu
) )
305 wxLogLastError(wxT("SetMenu"));
310 #endif // wxUSE_MENUS_NATIVE
312 // Responds to colour changes, and passes event on to children.
313 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
315 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
319 if ( m_frameStatusBar
)
321 wxSysColourChangedEvent event2
;
322 event2
.SetEventObject( m_frameStatusBar
);
323 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
325 #endif // wxUSE_STATUSBAR
327 // Propagate the event to the non-top-level children
328 wxWindow::OnSysColourChanged(event
);
331 // Pass TRUE to show full screen, FALSE to restore.
332 bool wxFrame::ShowFullScreen(bool show
, long style
)
334 if ( IsFullScreen() == show
)
341 // TODO: hide commandbar
343 wxToolBar
*theToolBar
= GetToolBar();
345 theToolBar
->GetSize(NULL
, &m_fsToolBarHeight
);
347 // zap the toolbar, menubar, and statusbar
349 if ((style
& wxFULLSCREEN_NOTOOLBAR
) && theToolBar
)
351 theToolBar
->SetSize(-1,0);
352 theToolBar
->Show(FALSE
);
354 #endif // __WXWINCE__
355 #endif // wxUSE_TOOLBAR
357 // TODO: make it work for WinCE
358 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
359 if (style
& wxFULLSCREEN_NOMENUBAR
)
360 SetMenu((HWND
)GetHWND(), (HMENU
) NULL
);
364 wxStatusBar
*theStatusBar
= GetStatusBar();
366 theStatusBar
->GetSize(NULL
, &m_fsStatusBarHeight
);
368 // Save the number of fields in the statusbar
369 if ((style
& wxFULLSCREEN_NOSTATUSBAR
) && theStatusBar
)
371 //m_fsStatusBarFields = theStatusBar->GetFieldsCount();
372 //SetStatusBar((wxStatusBar*) NULL);
373 //delete theStatusBar;
374 theStatusBar
->Show(FALSE
);
377 m_fsStatusBarFields
= 0;
378 #endif // wxUSE_STATUSBAR
384 // TODO: show commandbar
386 wxToolBar
*theToolBar
= GetToolBar();
388 // restore the toolbar, menubar, and statusbar
389 if (theToolBar
&& (m_fsStyle
& wxFULLSCREEN_NOTOOLBAR
))
391 theToolBar
->SetSize(-1, m_fsToolBarHeight
);
392 theToolBar
->Show(TRUE
);
394 #endif // __WXWINCE__
395 #endif // wxUSE_TOOLBAR
398 if ( m_fsStyle
& wxFULLSCREEN_NOSTATUSBAR
)
400 //CreateStatusBar(m_fsStatusBarFields);
403 GetStatusBar()->Show(TRUE
);
407 #endif // wxUSE_STATUSBAR
409 // TODO: make it work for WinCE
410 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
411 if ((m_fsStyle
& wxFULLSCREEN_NOMENUBAR
) && (m_hMenu
!= 0))
412 SetMenu((HWND
)GetHWND(), (HMENU
)m_hMenu
);
416 return wxFrameBase::ShowFullScreen(show
, style
);
419 // ----------------------------------------------------------------------------
420 // tool/status bar stuff
421 // ----------------------------------------------------------------------------
425 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
428 // We may already have a toolbar from calling SetMenuBar.
432 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
437 return m_frameToolBar
;
440 void wxFrame::PositionToolBar()
442 wxToolBar
*toolbar
= GetToolBar();
443 if ( toolbar
&& toolbar
->IsShown() )
446 // We want to do something different in WinCE, because
447 // the toolbar should be associated with the commandbar,
448 // and not an independent window.
451 // don't call our (or even wxTopLevelWindow) version because we want
452 // the real (full) client area size, not excluding the tool/status bar
454 wxWindow::DoGetClientSize(&width
, &height
);
457 wxStatusBar
*statbar
= GetStatusBar();
458 if ( statbar
&& statbar
->IsShown() )
460 height
-= statbar
->GetClientSize().y
;
462 #endif // wxUSE_STATUSBAR
465 toolbar
->GetSize(&tw
, &th
);
467 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
474 if ( toolbar
->GetWindowStyleFlag() & wxTB_FLAT
)
478 // use the 'real' MSW position here, don't offset relativly to the
479 // client area origin
480 toolbar
->SetSize(0, 0, tw
, th
, wxSIZE_NO_ADJUSTMENTS
);
481 #endif // __WXWINCE__
485 #endif // wxUSE_TOOLBAR
487 // ----------------------------------------------------------------------------
488 // frame state (iconized/maximized/...)
489 // ----------------------------------------------------------------------------
491 // propagate our state change to all child frames: this allows us to emulate X
492 // Windows behaviour where child frames float independently of the parent one
493 // on the desktop, but are iconized/restored with it
494 void wxFrame::IconizeChildFrames(bool bIconize
)
496 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
498 node
= node
->GetNext() )
500 wxWindow
*win
= node
->GetData();
502 // iconizing the frames with this style under Win95 shell puts them at
503 // the bottom of the screen (as the MDI children) instead of making
504 // them appear in the taskbar because they are, by virtue of this
505 // style, not managed by the taskbar - instead leave Windows take care
508 if ( win
->GetWindowStyle() & wxFRAME_TOOL_WINDOW
)
512 // the child MDI frames are a special case and should not be touched by
513 // the parent frame - instead, they are managed by the user
514 wxFrame
*frame
= wxDynamicCast(win
, wxFrame
);
516 #if wxUSE_MDI_ARCHITECTURE
517 && !wxDynamicCast(frame
, wxMDIChildFrame
)
518 #endif // wxUSE_MDI_ARCHITECTURE
521 // we don't want to restore the child frames which had been
522 // iconized even before we were iconized, so save the child frame
523 // status when iconizing the parent frame and check it when
527 // note that we shouldn't touch the hidden frames neither
528 // because iconizing/restoring them would show them as a side
530 frame
->m_wasMinimized
= frame
->IsIconized() || !frame
->IsShown();
533 // this test works for both iconizing and restoring
534 if ( !frame
->m_wasMinimized
)
535 frame
->Iconize(bIconize
);
540 WXHICON
wxFrame::GetDefaultIcon() const
542 // we don't have any standard icons (any more)
546 // ===========================================================================
547 // message processing
548 // ===========================================================================
550 // ---------------------------------------------------------------------------
552 // ---------------------------------------------------------------------------
554 bool wxFrame::MSWTranslateMessage(WXMSG
* pMsg
)
556 if ( wxWindow::MSWTranslateMessage(pMsg
) )
559 #if wxUSE_MENUS && wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
560 // try the menu bar accels
561 wxMenuBar
*menuBar
= GetMenuBar();
565 const wxAcceleratorTable
& acceleratorTable
= menuBar
->GetAccelTable();
566 return acceleratorTable
.Translate(this, pMsg
);
569 #endif // wxUSE_MENUS && wxUSE_ACCEL
572 // ---------------------------------------------------------------------------
573 // our private (non virtual) message handlers
574 // ---------------------------------------------------------------------------
576 bool wxFrame::HandlePaint()
579 if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) )
581 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
584 const wxIcon
& icon
= GetIcon();
585 HICON hIcon
= icon
.Ok() ? GetHiconOf(icon
)
586 : (HICON
)GetDefaultIcon();
588 // Hold a pointer to the dc so long as the OnPaint() message
589 // is being processed
591 HDC hdc
= ::BeginPaint(GetHwnd(), &ps
);
593 // Erase background before painting or we get white background
594 MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L);
599 ::GetClientRect(GetHwnd(), &rect
);
601 // FIXME: why hardcoded?
602 static const int icon_width
= 32;
603 static const int icon_height
= 32;
605 int icon_x
= (int)((rect
.right
- icon_width
)/2);
606 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
608 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
);
611 ::EndPaint(GetHwnd(), &ps
);
618 return wxWindow::HandlePaint();
623 // nothing to paint - processed
628 bool wxFrame::HandleSize(int x
, int y
, WXUINT id
)
630 bool processed
= FALSE
;
631 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
636 // only do it it if we were iconized before, otherwise resizing the
637 // parent frame has a curious side effect of bringing it under it's
642 // restore all child frames too
643 IconizeChildFrames(FALSE
);
645 (void)SendIconizeEvent(FALSE
);
654 // iconize all child frames too
655 IconizeChildFrames(TRUE
);
657 (void)SendIconizeEvent();
668 #endif // wxUSE_STATUSBAR
672 #endif // wxUSE_TOOLBAR
674 processed
= wxWindow::HandleSize(x
, y
, id
);
680 bool wxFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
)
684 // In case it's e.g. a toolbar.
685 wxWindow
*win
= wxFindWinFromHandle(control
);
687 return win
->MSWCommand(cmd
, id
);
690 // handle here commands from menus and accelerators
691 if ( cmd
== 0 || cmd
== 1 )
693 #if wxUSE_MENUS_NATIVE
694 if ( wxCurrentPopupMenu
)
696 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
697 wxCurrentPopupMenu
= NULL
;
699 return popupMenu
->MSWCommand(cmd
, id
);
701 #endif // wxUSE_MENUS_NATIVE
703 if ( ProcessCommand(id
) )
712 bool wxFrame::HandleMenuSelect(WXWORD nItem
, WXWORD flags
, WXHMENU hMenu
)
715 if ( flags
== 0xFFFF && hMenu
== 0 )
717 // menu was removed from screen
720 #ifndef __WXMICROWIN__
721 else if ( !(flags
& MF_POPUP
) && !(flags
& MF_SEPARATOR
) )
728 // don't give hints for separators (doesn't make sense) nor for the
729 // items opening popup menus (they don't have them anyhow) but do clear
730 // the status line - otherwise, we would be left with the help message
731 // for the previous item which doesn't apply any more
732 DoGiveHelp(wxEmptyString
, FALSE
);
737 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, item
);
738 event
.SetEventObject(this);
740 return GetEventHandler()->ProcessEvent(event
);
743 bool wxFrame::HandleMenuLoop(const wxEventType
& evtType
, WXWORD isPopup
)
745 // we don't have the menu id here, so we use the id to specify if the event
746 // was from a popup menu or a normal one
747 wxMenuEvent
event(evtType
, isPopup
? -1 : 0);
748 event
.SetEventObject(this);
750 return GetEventHandler()->ProcessEvent(event
);
753 // ---------------------------------------------------------------------------
754 // the window proc for wxFrame
755 // ---------------------------------------------------------------------------
757 long wxFrame::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
760 bool processed
= FALSE
;
765 // if we can't close, tell the system that we processed the
766 // message - otherwise it would close us
767 processed
= !Close();
771 processed
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
);
778 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
781 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
786 processed
= HandlePaint();
789 case WM_INITMENUPOPUP
:
790 processed
= HandleInitMenuPopup((WXHMENU
) wParam
);
793 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
798 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
800 processed
= HandleMenuSelect(item
, flags
, hmenu
);
804 case WM_EXITMENULOOP
:
805 processed
= HandleMenuLoop(wxEVT_MENU_CLOSE
, wParam
);
808 case WM_QUERYDRAGICON
:
810 const wxIcon
& icon
= GetIcon();
811 HICON hIcon
= icon
.Ok() ? GetHiconOf(icon
)
812 : (HICON
)GetDefaultIcon();
817 #endif // !__WXMICROWIN__
821 rc
= wxFrameBase::MSWWindowProc(message
, wParam
, lParam
);
826 // handle WM_INITMENUPOPUP message
827 bool wxFrame::HandleInitMenuPopup(WXHMENU hMenu
)
832 int nCount
= GetMenuBar()->GetMenuCount();
833 for (int n
= 0; n
< nCount
; n
++)
835 if (GetMenuBar()->GetMenu(n
)->GetHMenu() == hMenu
)
837 menu
= GetMenuBar()->GetMenu(n
);
843 wxMenuEvent
event(wxEVT_MENU_OPEN
, 0, menu
);
844 event
.SetEventObject(this);
846 return GetEventHandler()->ProcessEvent(event
);