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"
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 #if wxUSE_MENUS_NATIVE
67 extern wxMenu
*wxCurrentPopupMenu
;
68 #endif // wxUSE_MENUS_NATIVE
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 BEGIN_EVENT_TABLE(wxFrame
, wxFrameBase
)
75 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged
)
78 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxTopLevelWindow
)
80 // ============================================================================
82 // ============================================================================
84 // ----------------------------------------------------------------------------
85 // static class members
86 // ----------------------------------------------------------------------------
89 #if wxUSE_NATIVE_STATUSBAR
90 bool wxFrame::m_useNativeStatusBar
= TRUE
;
92 bool wxFrame::m_useNativeStatusBar
= FALSE
;
94 #endif // wxUSE_NATIVE_STATUSBAR
96 // ----------------------------------------------------------------------------
97 // creation/destruction
98 // ----------------------------------------------------------------------------
106 // Data to save/restore when calling ShowFullScreen
107 m_fsStatusBarFields
= 0;
108 m_fsStatusBarHeight
= 0;
109 m_fsToolBarHeight
= 0;
111 m_wasMinimized
= FALSE
;
114 bool wxFrame::Create(wxWindow
*parent
,
116 const wxString
& title
,
120 const wxString
& name
)
122 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
125 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
127 wxModelessWindows
.Append(this);
134 m_isBeingDeleted
= TRUE
;
139 // ----------------------------------------------------------------------------
140 // wxFrame client size calculations
141 // ----------------------------------------------------------------------------
143 void wxFrame::DoSetClientSize(int width
, int height
)
145 // leave enough space for the status bar if we have (and show) it
147 wxStatusBar
*statbar
= GetStatusBar();
148 if ( statbar
&& statbar
->IsShown() )
150 height
+= statbar
->GetSize().y
;
152 #endif // wxUSE_STATUSBAR
154 // call GetClientAreaOrigin() to take the toolbar into account
155 wxPoint pt
= GetClientAreaOrigin();
159 wxTopLevelWindow::DoSetClientSize(width
, height
);
162 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
163 void wxFrame::DoGetClientSize(int *x
, int *y
) const
165 wxTopLevelWindow::DoGetClientSize(x
, y
);
167 // account for the possible toolbar
168 wxPoint pt
= GetClientAreaOrigin();
176 // adjust client area height to take the status bar into account
179 wxStatusBar
*statbar
= GetStatusBar();
180 if ( statbar
&& statbar
->IsShown() )
182 *y
-= statbar
->GetClientSize().y
;
185 #endif // wxUSE_STATUSBAR
188 // ----------------------------------------------------------------------------
189 // wxFrame: various geometry-related functions
190 // ----------------------------------------------------------------------------
192 void wxFrame::Raise()
194 ::SetForegroundWindow(GetHwnd());
197 // generate an artificial resize event
198 void wxFrame::SendSizeEvent()
202 RECT r
= wxGetWindowRect(GetHwnd());
204 (void)::PostMessage(GetHwnd(), WM_SIZE
,
205 IsMaximized() ? SIZE_MAXIMIZED
: SIZE_RESTORED
,
206 MAKELPARAM(r
.right
- r
.left
, r
.bottom
- r
.top
));
211 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
,
214 const wxString
& name
)
216 wxStatusBar
*statusBar
= NULL
;
218 #if wxUSE_NATIVE_STATUSBAR
219 if ( !UsesNativeStatusBar() )
221 statusBar
= (wxStatusBar
*)new wxStatusBarGeneric(this, id
, style
);
226 statusBar
= new wxStatusBar(this, id
, style
, name
);
229 statusBar
->SetFieldsCount(number
);
234 void wxFrame::PositionStatusBar()
236 if ( !m_frameStatusBar
|| !m_frameStatusBar
->IsShown() )
240 GetClientSize(&w
, &h
);
242 m_frameStatusBar
->GetSize(&sw
, &sh
);
244 // Since we wish the status bar to be directly under the client area,
245 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
246 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
248 #endif // wxUSE_STATUSBAR
250 #if wxUSE_MENUS_NATIVE
252 void wxFrame::AttachMenuBar(wxMenuBar
*menubar
)
254 wxFrameBase::AttachMenuBar(menubar
);
258 // actually remove the menu from the frame
259 m_hMenu
= (WXHMENU
)0;
260 InternalSetMenuBar();
262 else // set new non NULL menu bar
264 // Can set a menubar several times.
265 if ( menubar
->GetHMenu() )
267 m_hMenu
= menubar
->GetHMenu();
271 m_hMenu
= menubar
->Create();
275 wxFAIL_MSG( _T("failed to create menu bar") );
280 InternalSetMenuBar();
284 void wxFrame::InternalSetMenuBar()
286 #ifndef __WXMICROWIN__
287 if ( !::SetMenu(GetHwnd(), (HMENU
)m_hMenu
) )
289 wxLogLastError(wxT("SetMenu"));
294 #endif // wxUSE_MENUS_NATIVE
296 // Responds to colour changes, and passes event on to children.
297 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
299 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
303 if ( m_frameStatusBar
)
305 wxSysColourChangedEvent event2
;
306 event2
.SetEventObject( m_frameStatusBar
);
307 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
309 #endif // wxUSE_STATUSBAR
311 // Propagate the event to the non-top-level children
312 wxWindow::OnSysColourChanged(event
);
315 // Pass TRUE to show full screen, FALSE to restore.
316 bool wxFrame::ShowFullScreen(bool show
, long style
)
318 if ( IsFullScreen() == show
)
324 wxToolBar
*theToolBar
= GetToolBar();
326 theToolBar
->GetSize(NULL
, &m_fsToolBarHeight
);
328 // zap the toolbar, menubar, and statusbar
330 if ((style
& wxFULLSCREEN_NOTOOLBAR
) && theToolBar
)
332 theToolBar
->SetSize(-1,0);
333 theToolBar
->Show(FALSE
);
335 #endif // wxUSE_TOOLBAR
337 #ifndef __WXMICROWIN__
338 if (style
& wxFULLSCREEN_NOMENUBAR
)
339 SetMenu((HWND
)GetHWND(), (HMENU
) NULL
);
343 wxStatusBar
*theStatusBar
= GetStatusBar();
345 theStatusBar
->GetSize(NULL
, &m_fsStatusBarHeight
);
347 // Save the number of fields in the statusbar
348 if ((style
& wxFULLSCREEN_NOSTATUSBAR
) && theStatusBar
)
350 //m_fsStatusBarFields = theStatusBar->GetFieldsCount();
351 //SetStatusBar((wxStatusBar*) NULL);
352 //delete theStatusBar;
353 theStatusBar
->Show(FALSE
);
356 m_fsStatusBarFields
= 0;
357 #endif // wxUSE_STATUSBAR
362 wxToolBar
*theToolBar
= GetToolBar();
364 // restore the toolbar, menubar, and statusbar
365 if (theToolBar
&& (m_fsStyle
& wxFULLSCREEN_NOTOOLBAR
))
367 theToolBar
->SetSize(-1, m_fsToolBarHeight
);
368 theToolBar
->Show(TRUE
);
370 #endif // wxUSE_TOOLBAR
373 if ( m_fsStyle
& wxFULLSCREEN_NOSTATUSBAR
)
375 //CreateStatusBar(m_fsStatusBarFields);
378 GetStatusBar()->Show(TRUE
);
382 #endif // wxUSE_STATUSBAR
384 #ifndef __WXMICROWIN__
385 if ((m_fsStyle
& wxFULLSCREEN_NOMENUBAR
) && (m_hMenu
!= 0))
386 SetMenu((HWND
)GetHWND(), (HMENU
)m_hMenu
);
390 return wxFrameBase::ShowFullScreen(show
, style
);
393 // ----------------------------------------------------------------------------
394 // tool/status bar stuff
395 // ----------------------------------------------------------------------------
399 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
401 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
406 return m_frameToolBar
;
409 void wxFrame::PositionToolBar()
411 wxToolBar
*toolbar
= GetToolBar();
412 if ( toolbar
&& toolbar
->IsShown() )
414 // don't call our (or even wxTopLevelWindow) version because we want
415 // the real (full) client area size, not excluding the tool/status bar
417 wxWindow::DoGetClientSize(&width
, &height
);
420 wxStatusBar
*statbar
= GetStatusBar();
421 if ( statbar
&& statbar
->IsShown() )
423 height
-= statbar
->GetClientSize().y
;
425 #endif // wxUSE_STATUSBAR
428 toolbar
->GetSize(&tw
, &th
);
430 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
437 if ( toolbar
->GetWindowStyleFlag() & wxTB_FLAT
)
441 // use the 'real' MSW position here, don't offset relativly to the
442 // client area origin
443 toolbar
->SetSize(0, 0, tw
, th
, wxSIZE_NO_ADJUSTMENTS
);
447 #endif // wxUSE_TOOLBAR
449 // ----------------------------------------------------------------------------
450 // frame state (iconized/maximized/...)
451 // ----------------------------------------------------------------------------
453 // propagate our state change to all child frames: this allows us to emulate X
454 // Windows behaviour where child frames float independently of the parent one
455 // on the desktop, but are iconized/restored with it
456 void wxFrame::IconizeChildFrames(bool bIconize
)
458 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
460 node
= node
->GetNext() )
462 wxWindow
*win
= node
->GetData();
464 // iconizing the frames with this style under Win95 shell puts them at
465 // the bottom of the screen (as the MDI children) instead of making
466 // them appear in the taskbar because they are, by virtue of this
467 // style, not managed by the taskbar - instead leave Windows take care
470 if ( win
->GetWindowStyle() & wxFRAME_TOOL_WINDOW
)
474 // the child MDI frames are a special case and should not be touched by
475 // the parent frame - instead, they are managed by the user
476 wxFrame
*frame
= wxDynamicCast(win
, wxFrame
);
478 #if wxUSE_MDI_ARCHITECTURE
479 && !wxDynamicCast(frame
, wxMDIChildFrame
)
480 #endif // wxUSE_MDI_ARCHITECTURE
483 // we don't want to restore the child frames which had been
484 // iconized even before we were iconized, so save the child frame
485 // status when iconizing the parent frame and check it when
489 // note that we shouldn't touch the hidden frames neither
490 // because iconizing/restoring them would show them as a side
492 frame
->m_wasMinimized
= frame
->IsIconized() || !frame
->IsShown();
495 // this test works for both iconizing and restoring
496 if ( !frame
->m_wasMinimized
)
497 frame
->Iconize(bIconize
);
502 WXHICON
wxFrame::GetDefaultIcon() const
504 // we don't have any standard icons (any more)
508 // ===========================================================================
509 // message processing
510 // ===========================================================================
512 // ---------------------------------------------------------------------------
514 // ---------------------------------------------------------------------------
516 bool wxFrame::MSWTranslateMessage(WXMSG
* pMsg
)
518 if ( wxWindow::MSWTranslateMessage(pMsg
) )
521 #if wxUSE_MENUS && wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
522 // try the menu bar accels
523 wxMenuBar
*menuBar
= GetMenuBar();
527 const wxAcceleratorTable
& acceleratorTable
= menuBar
->GetAccelTable();
528 return acceleratorTable
.Translate(this, pMsg
);
531 #endif // wxUSE_MENUS && wxUSE_ACCEL
534 // ---------------------------------------------------------------------------
535 // our private (non virtual) message handlers
536 // ---------------------------------------------------------------------------
538 bool wxFrame::HandlePaint()
541 if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) )
543 #ifndef __WXMICROWIN__
546 const wxIcon
& icon
= GetIcon();
547 HICON hIcon
= icon
.Ok() ? GetHiconOf(icon
)
548 : (HICON
)GetDefaultIcon();
550 // Hold a pointer to the dc so long as the OnPaint() message
551 // is being processed
553 HDC hdc
= ::BeginPaint(GetHwnd(), &ps
);
555 // Erase background before painting or we get white background
556 MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L);
561 ::GetClientRect(GetHwnd(), &rect
);
563 // FIXME: why hardcoded?
564 static const int icon_width
= 32;
565 static const int icon_height
= 32;
567 int icon_x
= (int)((rect
.right
- icon_width
)/2);
568 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
570 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
);
573 ::EndPaint(GetHwnd(), &ps
);
580 return wxWindow::HandlePaint();
585 // nothing to paint - processed
590 bool wxFrame::HandleSize(int x
, int y
, WXUINT id
)
592 bool processed
= FALSE
;
593 #ifndef __WXMICROWIN__
598 // only do it it if we were iconized before, otherwise resizing the
599 // parent frame has a curious side effect of bringing it under it's
604 // restore all child frames too
605 IconizeChildFrames(FALSE
);
607 (void)SendIconizeEvent(FALSE
);
616 // iconize all child frames too
617 IconizeChildFrames(TRUE
);
619 (void)SendIconizeEvent();
630 #endif // wxUSE_STATUSBAR
634 #endif // wxUSE_TOOLBAR
636 processed
= wxWindow::HandleSize(x
, y
, id
);
642 bool wxFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
)
646 // In case it's e.g. a toolbar.
647 wxWindow
*win
= wxFindWinFromHandle(control
);
649 return win
->MSWCommand(cmd
, id
);
652 // handle here commands from menus and accelerators
653 if ( cmd
== 0 || cmd
== 1 )
655 #if wxUSE_MENUS_NATIVE
656 if ( wxCurrentPopupMenu
)
658 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
659 wxCurrentPopupMenu
= NULL
;
661 return popupMenu
->MSWCommand(cmd
, id
);
663 #endif // wxUSE_MENUS_NATIVE
665 if ( ProcessCommand(id
) )
674 bool wxFrame::HandleMenuSelect(WXWORD nItem
, WXWORD flags
, WXHMENU hMenu
)
677 if ( flags
== 0xFFFF && hMenu
== 0 )
679 // menu was removed from screen
682 #ifndef __WXMICROWIN__
683 else if ( !(flags
& MF_POPUP
) && !(flags
& MF_SEPARATOR
) )
690 // don't give hints for separators (doesn't make sense) nor for the
691 // items opening popup menus (they don't have them anyhow) but do clear
692 // the status line - otherwise, we would be left with the help message
693 // for the previous item which doesn't apply any more
694 DoGiveHelp(wxEmptyString
, FALSE
);
699 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, item
);
700 event
.SetEventObject(this);
702 return GetEventHandler()->ProcessEvent(event
);
705 bool wxFrame::HandleMenuLoop(const wxEventType
& evtType
, WXWORD isPopup
)
707 // we don't have the menu id here, so we use the id to specify if the event
708 // was from a popup menu or a normal one
709 wxMenuEvent
event(evtType
, isPopup
? -1 : 0);
710 event
.SetEventObject(this);
712 return GetEventHandler()->ProcessEvent(event
);
715 // ---------------------------------------------------------------------------
716 // the window proc for wxFrame
717 // ---------------------------------------------------------------------------
719 long wxFrame::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
722 bool processed
= FALSE
;
727 // if we can't close, tell the system that we processed the
728 // message - otherwise it would close us
729 processed
= !Close();
733 processed
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
);
740 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
743 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
748 processed
= HandlePaint();
751 #ifndef __WXMICROWIN__
756 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
758 processed
= HandleMenuSelect(item
, flags
, hmenu
);
763 processed
= HandleInitMenu();
766 case WM_EXITMENULOOP
:
767 processed
= HandleMenuLoop(wxEVT_MENU_CLOSE
, wParam
);
770 case WM_QUERYDRAGICON
:
772 const wxIcon
& icon
= GetIcon();
773 HICON hIcon
= icon
.Ok() ? GetHiconOf(icon
)
774 : (HICON
)GetDefaultIcon();
779 #endif // !__WXMICROWIN__
783 rc
= wxFrameBase::MSWWindowProc(message
, wParam
, lParam
);
788 // handle WM_INITMENU message
789 bool wxFrame::HandleInitMenu()
791 wxMenuEvent
event(wxEVT_MENU_OPEN
, 0);
792 event
.SetEventObject(this);
794 return GetEventHandler()->ProcessEvent(event
);