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()
195 // no SetForegroundWindow() in Win16
196 wxFrameBase::Raise();
198 ::SetForegroundWindow(GetHwnd());
202 // generate an artificial resize event
203 void wxFrame::SendSizeEvent()
207 RECT r
= wxGetWindowRect(GetHwnd());
209 (void)::PostMessage(GetHwnd(), WM_SIZE
,
210 IsMaximized() ? SIZE_MAXIMIZED
: SIZE_RESTORED
,
211 MAKELPARAM(r
.right
- r
.left
, r
.bottom
- r
.top
));
216 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
,
219 const wxString
& name
)
221 wxStatusBar
*statusBar
= NULL
;
223 #if wxUSE_NATIVE_STATUSBAR
224 if ( !UsesNativeStatusBar() )
226 statusBar
= (wxStatusBar
*)new wxStatusBarGeneric(this, id
, style
);
231 statusBar
= new wxStatusBar(this, id
, style
, name
);
234 statusBar
->SetFieldsCount(number
);
239 void wxFrame::PositionStatusBar()
241 if ( !m_frameStatusBar
|| !m_frameStatusBar
->IsShown() )
245 GetClientSize(&w
, &h
);
247 m_frameStatusBar
->GetSize(&sw
, &sh
);
249 // Since we wish the status bar to be directly under the client area,
250 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
251 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
253 #endif // wxUSE_STATUSBAR
255 #if wxUSE_MENUS_NATIVE
257 void wxFrame::AttachMenuBar(wxMenuBar
*menubar
)
259 wxFrameBase::AttachMenuBar(menubar
);
263 // actually remove the menu from the frame
264 m_hMenu
= (WXHMENU
)0;
265 InternalSetMenuBar();
267 else // set new non NULL menu bar
269 // Can set a menubar several times.
270 if ( menubar
->GetHMenu() )
272 m_hMenu
= menubar
->GetHMenu();
276 m_hMenu
= menubar
->Create();
280 wxFAIL_MSG( _T("failed to create menu bar") );
285 InternalSetMenuBar();
289 void wxFrame::InternalSetMenuBar()
291 #ifndef __WXMICROWIN__
292 if ( !::SetMenu(GetHwnd(), (HMENU
)m_hMenu
) )
294 wxLogLastError(wxT("SetMenu"));
299 #endif // wxUSE_MENUS_NATIVE
301 // Responds to colour changes, and passes event on to children.
302 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
304 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
308 if ( m_frameStatusBar
)
310 wxSysColourChangedEvent event2
;
311 event2
.SetEventObject( m_frameStatusBar
);
312 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
314 #endif // wxUSE_STATUSBAR
316 // Propagate the event to the non-top-level children
317 wxWindow::OnSysColourChanged(event
);
320 // Pass TRUE to show full screen, FALSE to restore.
321 bool wxFrame::ShowFullScreen(bool show
, long style
)
323 if ( IsFullScreen() == show
)
329 wxToolBar
*theToolBar
= GetToolBar();
331 theToolBar
->GetSize(NULL
, &m_fsToolBarHeight
);
333 // zap the toolbar, menubar, and statusbar
335 if ((style
& wxFULLSCREEN_NOTOOLBAR
) && theToolBar
)
337 theToolBar
->SetSize(-1,0);
338 theToolBar
->Show(FALSE
);
340 #endif // wxUSE_TOOLBAR
342 #ifndef __WXMICROWIN__
343 if (style
& wxFULLSCREEN_NOMENUBAR
)
344 SetMenu((HWND
)GetHWND(), (HMENU
) NULL
);
348 wxStatusBar
*theStatusBar
= GetStatusBar();
350 theStatusBar
->GetSize(NULL
, &m_fsStatusBarHeight
);
352 // Save the number of fields in the statusbar
353 if ((style
& wxFULLSCREEN_NOSTATUSBAR
) && theStatusBar
)
355 //m_fsStatusBarFields = theStatusBar->GetFieldsCount();
356 //SetStatusBar((wxStatusBar*) NULL);
357 //delete theStatusBar;
358 theStatusBar
->Show(FALSE
);
361 m_fsStatusBarFields
= 0;
362 #endif // wxUSE_STATUSBAR
367 wxToolBar
*theToolBar
= GetToolBar();
369 // restore the toolbar, menubar, and statusbar
370 if (theToolBar
&& (m_fsStyle
& wxFULLSCREEN_NOTOOLBAR
))
372 theToolBar
->SetSize(-1, m_fsToolBarHeight
);
373 theToolBar
->Show(TRUE
);
375 #endif // wxUSE_TOOLBAR
378 if ( m_fsStyle
& wxFULLSCREEN_NOSTATUSBAR
)
380 //CreateStatusBar(m_fsStatusBarFields);
383 GetStatusBar()->Show(TRUE
);
387 #endif // wxUSE_STATUSBAR
389 #ifndef __WXMICROWIN__
390 if ((m_fsStyle
& wxFULLSCREEN_NOMENUBAR
) && (m_hMenu
!= 0))
391 SetMenu((HWND
)GetHWND(), (HMENU
)m_hMenu
);
395 return wxFrameBase::ShowFullScreen(show
, style
);
398 // ----------------------------------------------------------------------------
399 // tool/status bar stuff
400 // ----------------------------------------------------------------------------
404 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
406 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
411 return m_frameToolBar
;
414 void wxFrame::PositionToolBar()
416 wxToolBar
*toolbar
= GetToolBar();
417 if ( toolbar
&& toolbar
->IsShown() )
419 // don't call our (or even wxTopLevelWindow) version because we want
420 // the real (full) client area size, not excluding the tool/status bar
422 wxWindow::DoGetClientSize(&width
, &height
);
425 wxStatusBar
*statbar
= GetStatusBar();
426 if ( statbar
&& statbar
->IsShown() )
428 height
-= statbar
->GetClientSize().y
;
430 #endif // wxUSE_STATUSBAR
433 toolbar
->GetSize(&tw
, &th
);
435 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
442 if ( toolbar
->GetWindowStyleFlag() & wxTB_FLAT
)
446 // use the 'real' MSW position here, don't offset relativly to the
447 // client area origin
448 toolbar
->SetSize(0, 0, tw
, th
, wxSIZE_NO_ADJUSTMENTS
);
452 #endif // wxUSE_TOOLBAR
454 // ----------------------------------------------------------------------------
455 // frame state (iconized/maximized/...)
456 // ----------------------------------------------------------------------------
458 // propagate our state change to all child frames: this allows us to emulate X
459 // Windows behaviour where child frames float independently of the parent one
460 // on the desktop, but are iconized/restored with it
461 void wxFrame::IconizeChildFrames(bool bIconize
)
463 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
465 node
= node
->GetNext() )
467 wxWindow
*win
= node
->GetData();
469 // iconizing the frames with this style under Win95 shell puts them at
470 // the bottom of the screen (as the MDI children) instead of making
471 // them appear in the taskbar because they are, by virtue of this
472 // style, not managed by the taskbar - instead leave Windows take care
475 if ( win
->GetWindowStyle() & wxFRAME_TOOL_WINDOW
)
479 // the child MDI frames are a special case and should not be touched by
480 // the parent frame - instead, they are managed by the user
481 wxFrame
*frame
= wxDynamicCast(win
, wxFrame
);
483 #if wxUSE_MDI_ARCHITECTURE
484 && !wxDynamicCast(frame
, wxMDIChildFrame
)
485 #endif // wxUSE_MDI_ARCHITECTURE
488 // we don't want to restore the child frames which had been
489 // iconized even before we were iconized, so save the child frame
490 // status when iconizing the parent frame and check it when
494 // note that we shouldn't touch the hidden frames neither
495 // because iconizing/restoring them would show them as a side
497 frame
->m_wasMinimized
= frame
->IsIconized() || !frame
->IsShown();
500 // this test works for both iconizing and restoring
501 if ( !frame
->m_wasMinimized
)
502 frame
->Iconize(bIconize
);
507 WXHICON
wxFrame::GetDefaultIcon() const
509 return (WXHICON
)(wxSTD_FRAME_ICON
? wxSTD_FRAME_ICON
510 : wxDEFAULT_FRAME_ICON
);
513 // ===========================================================================
514 // message processing
515 // ===========================================================================
517 // ---------------------------------------------------------------------------
519 // ---------------------------------------------------------------------------
521 bool wxFrame::MSWTranslateMessage(WXMSG
* pMsg
)
523 if ( wxWindow::MSWTranslateMessage(pMsg
) )
526 #if wxUSE_MENUS && wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
527 // try the menu bar accels
528 wxMenuBar
*menuBar
= GetMenuBar();
532 const wxAcceleratorTable
& acceleratorTable
= menuBar
->GetAccelTable();
533 return acceleratorTable
.Translate(this, pMsg
);
536 #endif // wxUSE_MENUS && wxUSE_ACCEL
539 // ---------------------------------------------------------------------------
540 // our private (non virtual) message handlers
541 // ---------------------------------------------------------------------------
543 bool wxFrame::HandlePaint()
546 if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) )
548 #ifndef __WXMICROWIN__
551 const wxIcon
& icon
= GetIcon();
552 HICON hIcon
= icon
.Ok() ? GetHiconOf(icon
)
553 : (HICON
)GetDefaultIcon();
555 // Hold a pointer to the dc so long as the OnPaint() message
556 // is being processed
558 HDC hdc
= ::BeginPaint(GetHwnd(), &ps
);
560 // Erase background before painting or we get white background
561 MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L);
566 ::GetClientRect(GetHwnd(), &rect
);
568 // FIXME: why hardcoded?
569 static const int icon_width
= 32;
570 static const int icon_height
= 32;
572 int icon_x
= (int)((rect
.right
- icon_width
)/2);
573 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
575 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
);
578 ::EndPaint(GetHwnd(), &ps
);
585 return wxWindow::HandlePaint();
590 // nothing to paint - processed
595 bool wxFrame::HandleSize(int x
, int y
, WXUINT id
)
597 bool processed
= FALSE
;
598 #ifndef __WXMICROWIN__
603 // only do it it if we were iconized before, otherwise resizing the
604 // parent frame has a curious side effect of bringing it under it's
609 // restore all child frames too
610 IconizeChildFrames(FALSE
);
612 (void)SendIconizeEvent(FALSE
);
621 // iconize all child frames too
622 IconizeChildFrames(TRUE
);
624 (void)SendIconizeEvent();
635 #endif // wxUSE_STATUSBAR
639 #endif // wxUSE_TOOLBAR
641 processed
= wxWindow::HandleSize(x
, y
, id
);
647 bool wxFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
)
651 // In case it's e.g. a toolbar.
652 wxWindow
*win
= wxFindWinFromHandle(control
);
654 return win
->MSWCommand(cmd
, id
);
657 // handle here commands from menus and accelerators
658 if ( cmd
== 0 || cmd
== 1 )
660 #if wxUSE_MENUS_NATIVE
661 if ( wxCurrentPopupMenu
)
663 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
664 wxCurrentPopupMenu
= NULL
;
666 return popupMenu
->MSWCommand(cmd
, id
);
668 #endif // wxUSE_MENUS_NATIVE
670 if ( ProcessCommand(id
) )
679 bool wxFrame::HandleMenuSelect(WXWORD nItem
, WXWORD flags
, WXHMENU hMenu
)
682 if ( flags
== 0xFFFF && hMenu
== 0 )
684 // menu was removed from screen
687 #ifndef __WXMICROWIN__
688 else if ( !(flags
& MF_POPUP
) && !(flags
& MF_SEPARATOR
) )
695 // don't give hints for separators (doesn't make sense) nor for the
696 // items opening popup menus (they don't have them anyhow) but do clear
697 // the status line - otherwise, we would be left with the help message
698 // for the previous item which doesn't apply any more
699 DoGiveHelp(wxEmptyString
, FALSE
);
704 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, item
);
705 event
.SetEventObject(this);
707 return GetEventHandler()->ProcessEvent(event
);
710 bool wxFrame::HandleMenuLoop(const wxEventType
& evtType
, WXWORD isPopup
)
712 // we don't have the menu id here, so we use the id to specify if the event
713 // was from a popup menu or a normal one
714 wxMenuEvent
event(evtType
, isPopup
? -1 : 0);
715 event
.SetEventObject(this);
717 return GetEventHandler()->ProcessEvent(event
);
720 // ---------------------------------------------------------------------------
721 // the window proc for wxFrame
722 // ---------------------------------------------------------------------------
724 long wxFrame::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
727 bool processed
= FALSE
;
732 // if we can't close, tell the system that we processed the
733 // message - otherwise it would close us
734 processed
= !Close();
738 processed
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
);
745 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
748 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
753 processed
= HandlePaint();
756 #ifndef __WXMICROWIN__
761 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
763 processed
= HandleMenuSelect(item
, flags
, hmenu
);
768 case WM_ENTERMENULOOP
:
769 processed
= HandleMenuLoop(wxEVT_MENU_OPEN
, wParam
);
772 case WM_EXITMENULOOP
:
773 processed
= HandleMenuLoop(wxEVT_MENU_CLOSE
, wParam
);
777 case WM_QUERYDRAGICON
:
779 const wxIcon
& icon
= GetIcon();
780 HICON hIcon
= icon
.Ok() ? GetHiconOf(icon
)
781 : (HICON
)GetDefaultIcon();
786 #endif // !__WXMICROWIN__
790 rc
= wxFrameBase::MSWWindowProc(message
, wParam
, lParam
);