1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
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 extern const wxChar
*wxFrameClassName
;
68 #if wxUSE_MENUS_NATIVE
69 extern wxMenu
*wxCurrentPopupMenu
;
70 #endif // wxUSE_MENUS_NATIVE
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 BEGIN_EVENT_TABLE(wxFrameMSW
, wxFrameBase
)
77 EVT_ACTIVATE(wxFrameMSW::OnActivate
)
78 EVT_SYS_COLOUR_CHANGED(wxFrameMSW::OnSysColourChanged
)
81 #ifndef __WXUNIVERSAL__
82 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxWindow
)
85 // ============================================================================
87 // ============================================================================
89 // ----------------------------------------------------------------------------
90 // static class members
91 // ----------------------------------------------------------------------------
94 #if wxUSE_NATIVE_STATUSBAR
95 bool wxFrameMSW::m_useNativeStatusBar
= TRUE
;
97 bool wxFrameMSW::m_useNativeStatusBar
= FALSE
;
99 #endif // wxUSE_NATIVE_STATUSBAR
101 // ----------------------------------------------------------------------------
102 // creation/destruction
103 // ----------------------------------------------------------------------------
105 void wxFrameMSW::Init()
111 // Data to save/restore when calling ShowFullScreen
113 m_fsOldWindowStyle
= 0;
114 m_fsStatusBarFields
= 0;
115 m_fsStatusBarHeight
= 0;
116 m_fsToolBarHeight
= 0;
118 m_fsIsMaximized
= FALSE
;
119 m_fsIsShowing
= FALSE
;
121 m_winLastFocused
= (wxWindow
*)NULL
;
124 bool wxFrameMSW::Create(wxWindow
*parent
,
126 const wxString
& title
,
130 const wxString
& name
)
132 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
135 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
137 wxModelessWindows
.Append(this);
142 wxFrameMSW::~wxFrameMSW()
144 m_isBeingDeleted
= TRUE
;
149 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
150 void wxFrameMSW::DoGetClientSize(int *x
, int *y
) const
153 ::GetClientRect(GetHwnd(), &rect
);
156 if ( GetStatusBar() && GetStatusBar()->IsShown() )
158 int statusX
, statusY
;
159 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
160 rect
.bottom
-= statusY
;
162 #endif // wxUSE_STATUSBAR
164 wxPoint
pt(GetClientAreaOrigin());
174 void wxFrameMSW::DoSetClientSize(int width
, int height
)
176 // leave enough space for the status bar if we have (and show) it
178 wxStatusBar
*statbar
= GetStatusBar();
179 if ( statbar
&& statbar
->IsShown() )
181 height
+= statbar
->GetSize().y
;
183 #endif // wxUSE_STATUSBAR
185 wxTopLevelWindow::DoSetClientSize(width
, height
);
188 // ----------------------------------------------------------------------------
189 // wxFrameMSW: various geometry-related functions
190 // ----------------------------------------------------------------------------
192 void wxFrameMSW::Raise()
195 // no SetForegroundWindow() in Win16
196 wxFrameBase::Raise();
198 ::SetForegroundWindow(GetHwnd());
202 // generate an artificial resize event
203 void wxFrameMSW::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
*wxFrameMSW::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 // Set the height according to the font and the border size
235 wxClientDC
dc(statusBar
);
236 dc
.SetFont(statusBar
->GetFont());
239 dc
.GetTextExtent(_T("X"), NULL
, &y
);
241 int height
= (int)( (11*y
)/10 + 2*statusBar
->GetBorderY());
243 statusBar
->SetSize(-1, -1, -1, height
);
245 statusBar
->SetFieldsCount(number
);
250 void wxFrameMSW::PositionStatusBar()
252 if ( !m_frameStatusBar
)
256 GetClientSize(&w
, &h
);
258 m_frameStatusBar
->GetSize(&sw
, &sh
);
260 // Since we wish the status bar to be directly under the client area,
261 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
262 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
264 #endif // wxUSE_STATUSBAR
266 #if wxUSE_MENUS_NATIVE
268 void wxFrameMSW::AttachMenuBar(wxMenuBar
*menubar
)
270 wxFrameBase::AttachMenuBar(menubar
);
274 // actually remove the menu from the frame
275 m_hMenu
= (WXHMENU
)0;
276 InternalSetMenuBar();
278 else // set new non NULL menu bar
280 // Can set a menubar several times.
281 if ( menubar
->GetHMenu() )
283 m_hMenu
= menubar
->GetHMenu();
287 m_hMenu
= menubar
->Create();
291 wxFAIL_MSG( _T("failed to create menu bar") );
296 InternalSetMenuBar();
300 void wxFrameMSW::InternalSetMenuBar()
302 #ifndef __WXMICROWIN__
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 wxFrameMSW::OnSysColourChanged(wxSysColourChangedEvent
& event
)
315 SetBackgroundColour(wxSystemSettings::GetSystemColour(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 wxFrameMSW::ShowFullScreen(bool show
, long style
)
339 m_fsIsShowing
= TRUE
;
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 // wxUSE_TOOLBAR
356 #ifndef __WXMICROWIN__
357 if (style
& wxFULLSCREEN_NOMENUBAR
)
358 SetMenu((HWND
)GetHWND(), (HMENU
) NULL
);
362 wxStatusBar
*theStatusBar
= GetStatusBar();
364 theStatusBar
->GetSize(NULL
, &m_fsStatusBarHeight
);
366 // Save the number of fields in the statusbar
367 if ((style
& wxFULLSCREEN_NOSTATUSBAR
) && theStatusBar
)
369 //m_fsStatusBarFields = theStatusBar->GetFieldsCount();
370 //SetStatusBar((wxStatusBar*) NULL);
371 //delete theStatusBar;
372 theStatusBar
->Show(FALSE
);
375 m_fsStatusBarFields
= 0;
376 #endif // wxUSE_STATUSBAR
378 // zap the frame borders
380 // save the 'normal' window style
381 m_fsOldWindowStyle
= GetWindowLong((HWND
)GetHWND(), GWL_STYLE
);
383 // save the old position, width & height, maximize state
384 m_fsOldSize
= GetRect();
385 m_fsIsMaximized
= IsMaximized();
387 // decide which window style flags to turn off
388 LONG newStyle
= m_fsOldWindowStyle
;
391 if (style
& wxFULLSCREEN_NOBORDER
)
392 offFlags
|= WS_BORDER
| WS_THICKFRAME
;
393 if (style
& wxFULLSCREEN_NOCAPTION
)
394 offFlags
|= (WS_CAPTION
| WS_SYSMENU
);
396 newStyle
&= (~offFlags
);
398 // change our window style to be compatible with full-screen mode
399 ::SetWindowLong((HWND
)GetHWND(), GWL_STYLE
, newStyle
);
401 // resize to the size of the desktop
404 RECT rect
= wxGetWindowRect(::GetDesktopWindow());
405 width
= rect
.right
- rect
.left
;
406 height
= rect
.bottom
- rect
.top
;
408 SetSize(width
, height
);
410 // now flush the window style cache and actually go full-screen
411 SetWindowPos((HWND
)GetHWND(), HWND_TOP
, 0, 0, width
, height
, SWP_FRAMECHANGED
);
413 wxSizeEvent
event(wxSize(width
, height
), GetId());
414 GetEventHandler()->ProcessEvent(event
);
423 m_fsIsShowing
= FALSE
;
426 wxToolBar
*theToolBar
= GetToolBar();
428 // restore the toolbar, menubar, and statusbar
429 if (theToolBar
&& (m_fsStyle
& wxFULLSCREEN_NOTOOLBAR
))
431 theToolBar
->SetSize(-1, m_fsToolBarHeight
);
432 theToolBar
->Show(TRUE
);
434 #endif // wxUSE_TOOLBAR
437 if ( m_fsStyle
& wxFULLSCREEN_NOSTATUSBAR
)
439 //CreateStatusBar(m_fsStatusBarFields);
442 GetStatusBar()->Show(TRUE
);
446 #endif // wxUSE_STATUSBAR
448 #ifndef __WXMICROWIN__
449 if ((m_fsStyle
& wxFULLSCREEN_NOMENUBAR
) && (m_hMenu
!= 0))
450 SetMenu((HWND
)GetHWND(), (HMENU
)m_hMenu
);
453 Maximize(m_fsIsMaximized
);
454 SetWindowLong((HWND
)GetHWND(),GWL_STYLE
, m_fsOldWindowStyle
);
455 SetWindowPos((HWND
)GetHWND(),HWND_TOP
,m_fsOldSize
.x
, m_fsOldSize
.y
,
456 m_fsOldSize
.width
, m_fsOldSize
.height
, SWP_FRAMECHANGED
);
462 // Default activation behaviour - set the focus for the first child
464 void wxFrameMSW::OnActivate(wxActivateEvent
& event
)
466 if ( event
.GetActive() )
468 // restore focus to the child which was last focused
469 wxLogTrace(_T("focus"), _T("wxFrameMSW %08x activated."), m_hWnd
);
471 wxWindow
*parent
= m_winLastFocused
? m_winLastFocused
->GetParent()
478 wxSetFocusToChild(parent
, &m_winLastFocused
);
482 // remember the last focused child if it is our child
483 m_winLastFocused
= FindFocus();
485 // so we NULL it out if it's a child from some other frame
486 wxWindow
*win
= m_winLastFocused
;
489 if ( win
->IsTopLevel() )
493 m_winLastFocused
= NULL
;
499 win
= win
->GetParent();
502 wxLogTrace(_T("focus"),
503 _T("wxFrameMSW %08x deactivated, last focused: %08x."),
505 m_winLastFocused
? GetHwndOf(m_winLastFocused
)
512 // ----------------------------------------------------------------------------
513 // tool/status bar stuff
514 // ----------------------------------------------------------------------------
518 wxToolBar
* wxFrameMSW::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
520 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
525 return m_frameToolBar
;
528 void wxFrameMSW::PositionToolBar()
531 ::GetClientRect(GetHwnd(), &rect
);
534 if ( GetStatusBar() )
536 int statusX
, statusY
;
537 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
538 rect
.bottom
-= statusY
;
540 #endif // wxUSE_STATUSBAR
542 if ( GetToolBar() && GetToolBar()->IsShown() )
545 GetToolBar()->GetSize(&tw
, &th
);
547 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
556 // Use the 'real' MSW position here
557 GetToolBar()->SetSize(0, 0, tw
, th
, wxSIZE_NO_ADJUSTMENTS
);
560 #endif // wxUSE_TOOLBAR
562 // ----------------------------------------------------------------------------
563 // frame state (iconized/maximized/...)
564 // ----------------------------------------------------------------------------
566 // propagate our state change to all child frames: this allows us to emulate X
567 // Windows behaviour where child frames float independently of the parent one
568 // on the desktop, but are iconized/restored with it
569 void wxFrameMSW::IconizeChildFrames(bool bIconize
)
571 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
573 node
= node
->GetNext() )
575 wxWindow
*win
= node
->GetData();
577 // iconizing the frames with this style under Win95 shell puts them at
578 // the bottom of the screen (as the MDI children) instead of making
579 // them appear in the taskbar because they are, by virtue of this
580 // style, not managed by the taskbar - instead leave Windows take care
583 if ( win
->GetWindowStyle() & wxFRAME_TOOL_WINDOW
)
587 // the child MDI frames are a special case and should not be touched by
588 // the parent frame - instead, they are managed by the user
589 wxFrame
*frame
= wxDynamicCast(win
, wxFrame
);
591 #if wxUSE_MDI_ARCHITECTURE
592 && !wxDynamicCast(frame
, wxMDIChildFrame
)
593 #endif // wxUSE_MDI_ARCHITECTURE
596 frame
->Iconize(bIconize
);
601 WXHICON
wxFrameMSW::GetDefaultIcon() const
603 return (WXHICON
)(wxSTD_FRAME_ICON
? wxSTD_FRAME_ICON
604 : wxDEFAULT_FRAME_ICON
);
607 // ===========================================================================
608 // message processing
609 // ===========================================================================
611 // ---------------------------------------------------------------------------
613 // ---------------------------------------------------------------------------
615 bool wxFrameMSW::MSWTranslateMessage(WXMSG
* pMsg
)
617 if ( wxWindow::MSWTranslateMessage(pMsg
) )
620 #if wxUSE_MENUS && wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
621 // try the menu bar accels
622 wxMenuBar
*menuBar
= GetMenuBar();
626 const wxAcceleratorTable
& acceleratorTable
= menuBar
->GetAccelTable();
627 return acceleratorTable
.Translate(this, pMsg
);
630 #endif // wxUSE_MENUS && wxUSE_ACCEL
633 // ---------------------------------------------------------------------------
634 // our private (non virtual) message handlers
635 // ---------------------------------------------------------------------------
637 bool wxFrameMSW::HandlePaint()
640 if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) )
642 #ifndef __WXMICROWIN__
645 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
646 : (HICON
)GetDefaultIcon();
648 // Hold a pointer to the dc so long as the OnPaint() message
649 // is being processed
651 HDC hdc
= ::BeginPaint(GetHwnd(), &ps
);
653 // Erase background before painting or we get white background
654 MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L);
659 ::GetClientRect(GetHwnd(), &rect
);
661 // FIXME: why hardcoded?
662 static const int icon_width
= 32;
663 static const int icon_height
= 32;
665 int icon_x
= (int)((rect
.right
- icon_width
)/2);
666 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
668 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
);
671 ::EndPaint(GetHwnd(), &ps
);
678 return wxWindow::HandlePaint();
683 // nothing to paint - processed
688 bool wxFrameMSW::HandleSize(int x
, int y
, WXUINT id
)
690 bool processed
= FALSE
;
691 #ifndef __WXMICROWIN__
696 // only do it it if we were iconized before, otherwise resizing the
697 // parent frame has a curious side effect of bringing it under it's
702 // restore all child frames too
703 IconizeChildFrames(FALSE
);
705 (void)SendIconizeEvent(FALSE
);
714 // iconize all child frames too
715 IconizeChildFrames(TRUE
);
717 (void)SendIconizeEvent();
728 #endif // wxUSE_STATUSBAR
732 #endif // wxUSE_TOOLBAR
734 wxSizeEvent
event(wxSize(x
, y
), m_windowId
);
735 event
.SetEventObject( this );
736 processed
= GetEventHandler()->ProcessEvent(event
);
742 bool wxFrameMSW::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
)
746 // In case it's e.g. a toolbar.
747 wxWindow
*win
= wxFindWinFromHandle(control
);
749 return win
->MSWCommand(cmd
, id
);
752 // handle here commands from menus and accelerators
753 if ( cmd
== 0 || cmd
== 1 )
755 #if wxUSE_MENUS_NATIVE
756 if ( wxCurrentPopupMenu
)
758 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
759 wxCurrentPopupMenu
= NULL
;
761 return popupMenu
->MSWCommand(cmd
, id
);
763 #endif // wxUSE_MENUS_NATIVE
765 if ( ProcessCommand(id
) )
774 bool wxFrameMSW::HandleMenuSelect(WXWORD nItem
, WXWORD flags
, WXHMENU hMenu
)
777 if ( flags
== 0xFFFF && hMenu
== 0 )
779 // menu was removed from screen
782 #ifndef __WXMICROWIN__
783 else if ( !(flags
& MF_POPUP
) && !(flags
& MF_SEPARATOR
) )
791 // don't give hints for separators (doesn't make sense) nor for the
792 // items opening popup menus (they don't have them anyhow) but do clear
793 // the status line - otherwise, we would be left with the help message
794 // for the previous item which doesn't apply any more
795 wxStatusBar
*statbar
= GetStatusBar();
798 statbar
->SetStatusText(wxEmptyString
);
800 #endif // wxUSE_STATUSBAR
805 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, item
);
806 event
.SetEventObject( this );
808 return GetEventHandler()->ProcessEvent(event
);
811 // ---------------------------------------------------------------------------
812 // the window proc for wxFrameMSW
813 // ---------------------------------------------------------------------------
815 long wxFrameMSW::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
818 bool processed
= FALSE
;
823 // if we can't close, tell the system that we processed the
824 // message - otherwise it would close us
825 processed
= !Close();
832 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
835 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
839 #ifndef __WXMICROWIN__
844 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
846 processed
= HandleMenuSelect(item
, flags
, hmenu
);
852 processed
= HandlePaint();
855 #ifndef __WXMICROWIN__
856 case WM_QUERYDRAGICON
:
858 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
859 : (HICON
)GetDefaultIcon();
867 processed
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
);
872 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);