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: eventually have a wxCommandBar class
306 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), GetHwnd(), NewControlId());
309 wxFAIL_MSG( _T("failed to create commandbar") );
315 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
,
316 (LPTSTR
) (HMENU
) m_hMenu
, 0))
318 wxFAIL_MSG( _T("failed to set menubar") );
321 CommandBar_DrawMenuBar((HWND
) m_commandBar
, 0);
324 if ( !::SetMenu(GetHwnd(), (HMENU
)m_hMenu
) )
326 wxLogLastError(wxT("SetMenu"));
331 #endif // wxUSE_MENUS_NATIVE
333 // Responds to colour changes, and passes event on to children.
334 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
336 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
340 if ( m_frameStatusBar
)
342 wxSysColourChangedEvent event2
;
343 event2
.SetEventObject( m_frameStatusBar
);
344 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
346 #endif // wxUSE_STATUSBAR
348 // Propagate the event to the non-top-level children
349 wxWindow::OnSysColourChanged(event
);
352 // Pass TRUE to show full screen, FALSE to restore.
353 bool wxFrame::ShowFullScreen(bool show
, long style
)
355 if ( IsFullScreen() == show
)
362 // TODO: hide commandbar
364 wxToolBar
*theToolBar
= GetToolBar();
366 theToolBar
->GetSize(NULL
, &m_fsToolBarHeight
);
368 // zap the toolbar, menubar, and statusbar
370 if ((style
& wxFULLSCREEN_NOTOOLBAR
) && theToolBar
)
372 theToolBar
->SetSize(-1,0);
373 theToolBar
->Show(FALSE
);
375 #endif // __WXWINCE__
376 #endif // wxUSE_TOOLBAR
378 // TODO: make it work for WinCE
379 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
380 if (style
& wxFULLSCREEN_NOMENUBAR
)
381 SetMenu((HWND
)GetHWND(), (HMENU
) NULL
);
385 wxStatusBar
*theStatusBar
= GetStatusBar();
387 theStatusBar
->GetSize(NULL
, &m_fsStatusBarHeight
);
389 // Save the number of fields in the statusbar
390 if ((style
& wxFULLSCREEN_NOSTATUSBAR
) && theStatusBar
)
392 //m_fsStatusBarFields = theStatusBar->GetFieldsCount();
393 //SetStatusBar((wxStatusBar*) NULL);
394 //delete theStatusBar;
395 theStatusBar
->Show(FALSE
);
398 m_fsStatusBarFields
= 0;
399 #endif // wxUSE_STATUSBAR
405 // TODO: show commandbar
407 wxToolBar
*theToolBar
= GetToolBar();
409 // restore the toolbar, menubar, and statusbar
410 if (theToolBar
&& (m_fsStyle
& wxFULLSCREEN_NOTOOLBAR
))
412 theToolBar
->SetSize(-1, m_fsToolBarHeight
);
413 theToolBar
->Show(TRUE
);
415 #endif // __WXWINCE__
416 #endif // wxUSE_TOOLBAR
419 if ( m_fsStyle
& wxFULLSCREEN_NOSTATUSBAR
)
421 //CreateStatusBar(m_fsStatusBarFields);
424 GetStatusBar()->Show(TRUE
);
428 #endif // wxUSE_STATUSBAR
430 // TODO: make it work for WinCE
431 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
432 if ((m_fsStyle
& wxFULLSCREEN_NOMENUBAR
) && (m_hMenu
!= 0))
433 SetMenu((HWND
)GetHWND(), (HMENU
)m_hMenu
);
437 return wxFrameBase::ShowFullScreen(show
, style
);
440 // ----------------------------------------------------------------------------
441 // tool/status bar stuff
442 // ----------------------------------------------------------------------------
446 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
448 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
453 return m_frameToolBar
;
456 void wxFrame::PositionToolBar()
458 wxToolBar
*toolbar
= GetToolBar();
459 if ( toolbar
&& toolbar
->IsShown() )
462 // We want to do something different in WinCE, because
463 // the toolbar should be associated with the commandbar,
464 // and not an independent window.
467 // don't call our (or even wxTopLevelWindow) version because we want
468 // the real (full) client area size, not excluding the tool/status bar
470 wxWindow::DoGetClientSize(&width
, &height
);
473 wxStatusBar
*statbar
= GetStatusBar();
474 if ( statbar
&& statbar
->IsShown() )
476 height
-= statbar
->GetClientSize().y
;
478 #endif // wxUSE_STATUSBAR
481 toolbar
->GetSize(&tw
, &th
);
483 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
490 if ( toolbar
->GetWindowStyleFlag() & wxTB_FLAT
)
494 // use the 'real' MSW position here, don't offset relativly to the
495 // client area origin
496 toolbar
->SetSize(0, 0, tw
, th
, wxSIZE_NO_ADJUSTMENTS
);
497 #endif // __WXWINCE__
501 #endif // wxUSE_TOOLBAR
503 // ----------------------------------------------------------------------------
504 // frame state (iconized/maximized/...)
505 // ----------------------------------------------------------------------------
507 // propagate our state change to all child frames: this allows us to emulate X
508 // Windows behaviour where child frames float independently of the parent one
509 // on the desktop, but are iconized/restored with it
510 void wxFrame::IconizeChildFrames(bool bIconize
)
512 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
514 node
= node
->GetNext() )
516 wxWindow
*win
= node
->GetData();
518 // iconizing the frames with this style under Win95 shell puts them at
519 // the bottom of the screen (as the MDI children) instead of making
520 // them appear in the taskbar because they are, by virtue of this
521 // style, not managed by the taskbar - instead leave Windows take care
524 if ( win
->GetWindowStyle() & wxFRAME_TOOL_WINDOW
)
528 // the child MDI frames are a special case and should not be touched by
529 // the parent frame - instead, they are managed by the user
530 wxFrame
*frame
= wxDynamicCast(win
, wxFrame
);
532 #if wxUSE_MDI_ARCHITECTURE
533 && !wxDynamicCast(frame
, wxMDIChildFrame
)
534 #endif // wxUSE_MDI_ARCHITECTURE
537 // we don't want to restore the child frames which had been
538 // iconized even before we were iconized, so save the child frame
539 // status when iconizing the parent frame and check it when
543 // note that we shouldn't touch the hidden frames neither
544 // because iconizing/restoring them would show them as a side
546 frame
->m_wasMinimized
= frame
->IsIconized() || !frame
->IsShown();
549 // this test works for both iconizing and restoring
550 if ( !frame
->m_wasMinimized
)
551 frame
->Iconize(bIconize
);
556 WXHICON
wxFrame::GetDefaultIcon() const
558 // we don't have any standard icons (any more)
562 // ===========================================================================
563 // message processing
564 // ===========================================================================
566 // ---------------------------------------------------------------------------
568 // ---------------------------------------------------------------------------
570 bool wxFrame::MSWTranslateMessage(WXMSG
* pMsg
)
572 if ( wxWindow::MSWTranslateMessage(pMsg
) )
575 #if wxUSE_MENUS && wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
576 // try the menu bar accels
577 wxMenuBar
*menuBar
= GetMenuBar();
581 const wxAcceleratorTable
& acceleratorTable
= menuBar
->GetAccelTable();
582 return acceleratorTable
.Translate(this, pMsg
);
585 #endif // wxUSE_MENUS && wxUSE_ACCEL
588 // ---------------------------------------------------------------------------
589 // our private (non virtual) message handlers
590 // ---------------------------------------------------------------------------
592 bool wxFrame::HandlePaint()
595 if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) )
597 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
600 const wxIcon
& icon
= GetIcon();
601 HICON hIcon
= icon
.Ok() ? GetHiconOf(icon
)
602 : (HICON
)GetDefaultIcon();
604 // Hold a pointer to the dc so long as the OnPaint() message
605 // is being processed
607 HDC hdc
= ::BeginPaint(GetHwnd(), &ps
);
609 // Erase background before painting or we get white background
610 MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L);
615 ::GetClientRect(GetHwnd(), &rect
);
617 // FIXME: why hardcoded?
618 static const int icon_width
= 32;
619 static const int icon_height
= 32;
621 int icon_x
= (int)((rect
.right
- icon_width
)/2);
622 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
624 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
);
627 ::EndPaint(GetHwnd(), &ps
);
634 return wxWindow::HandlePaint();
639 // nothing to paint - processed
644 bool wxFrame::HandleSize(int x
, int y
, WXUINT id
)
646 bool processed
= FALSE
;
647 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
652 // only do it it if we were iconized before, otherwise resizing the
653 // parent frame has a curious side effect of bringing it under it's
658 // restore all child frames too
659 IconizeChildFrames(FALSE
);
661 (void)SendIconizeEvent(FALSE
);
670 // iconize all child frames too
671 IconizeChildFrames(TRUE
);
673 (void)SendIconizeEvent();
684 #endif // wxUSE_STATUSBAR
688 #endif // wxUSE_TOOLBAR
690 processed
= wxWindow::HandleSize(x
, y
, id
);
696 bool wxFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
)
700 // In case it's e.g. a toolbar.
701 wxWindow
*win
= wxFindWinFromHandle(control
);
703 return win
->MSWCommand(cmd
, id
);
706 // handle here commands from menus and accelerators
707 if ( cmd
== 0 || cmd
== 1 )
709 #if wxUSE_MENUS_NATIVE
710 if ( wxCurrentPopupMenu
)
712 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
713 wxCurrentPopupMenu
= NULL
;
715 return popupMenu
->MSWCommand(cmd
, id
);
717 #endif // wxUSE_MENUS_NATIVE
719 if ( ProcessCommand(id
) )
728 bool wxFrame::HandleMenuSelect(WXWORD nItem
, WXWORD flags
, WXHMENU hMenu
)
731 if ( flags
== 0xFFFF && hMenu
== 0 )
733 // menu was removed from screen
736 #ifndef __WXMICROWIN__
737 else if ( !(flags
& MF_POPUP
) && !(flags
& MF_SEPARATOR
) )
744 // don't give hints for separators (doesn't make sense) nor for the
745 // items opening popup menus (they don't have them anyhow) but do clear
746 // the status line - otherwise, we would be left with the help message
747 // for the previous item which doesn't apply any more
748 DoGiveHelp(wxEmptyString
, FALSE
);
753 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, item
);
754 event
.SetEventObject(this);
756 return GetEventHandler()->ProcessEvent(event
);
759 bool wxFrame::HandleMenuLoop(const wxEventType
& evtType
, WXWORD isPopup
)
761 // we don't have the menu id here, so we use the id to specify if the event
762 // was from a popup menu or a normal one
763 wxMenuEvent
event(evtType
, isPopup
? -1 : 0);
764 event
.SetEventObject(this);
766 return GetEventHandler()->ProcessEvent(event
);
769 // ---------------------------------------------------------------------------
770 // the window proc for wxFrame
771 // ---------------------------------------------------------------------------
773 long wxFrame::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
776 bool processed
= FALSE
;
781 // if we can't close, tell the system that we processed the
782 // message - otherwise it would close us
783 processed
= !Close();
787 processed
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
);
794 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
797 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
802 processed
= HandlePaint();
805 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
810 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
812 processed
= HandleMenuSelect(item
, flags
, hmenu
);
817 processed
= HandleInitMenu();
820 case WM_EXITMENULOOP
:
821 processed
= HandleMenuLoop(wxEVT_MENU_CLOSE
, wParam
);
824 case WM_QUERYDRAGICON
:
826 const wxIcon
& icon
= GetIcon();
827 HICON hIcon
= icon
.Ok() ? GetHiconOf(icon
)
828 : (HICON
)GetDefaultIcon();
833 #endif // !__WXMICROWIN__
837 rc
= wxFrameBase::MSWWindowProc(message
, wParam
, lParam
);
842 // handle WM_INITMENU message
843 bool wxFrame::HandleInitMenu()
845 wxMenuEvent
event(wxEVT_MENU_OPEN
, 0);
846 event
.SetEventObject(this);
848 return GetEventHandler()->ProcessEvent(event
);