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 statusBar
->SetFieldsCount(number
);
239 void wxFrameMSW::PositionStatusBar()
241 if ( !m_frameStatusBar
)
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 wxFrameMSW::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 wxFrameMSW::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 wxFrameMSW::OnSysColourChanged(wxSysColourChangedEvent
& event
)
304 SetBackgroundColour(wxSystemSettings::GetSystemColour(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 wxFrameMSW::ShowFullScreen(bool show
, long style
)
328 m_fsIsShowing
= TRUE
;
332 wxToolBar
*theToolBar
= GetToolBar();
334 theToolBar
->GetSize(NULL
, &m_fsToolBarHeight
);
336 // zap the toolbar, menubar, and statusbar
338 if ((style
& wxFULLSCREEN_NOTOOLBAR
) && theToolBar
)
340 theToolBar
->SetSize(-1,0);
341 theToolBar
->Show(FALSE
);
343 #endif // wxUSE_TOOLBAR
345 #ifndef __WXMICROWIN__
346 if (style
& wxFULLSCREEN_NOMENUBAR
)
347 SetMenu((HWND
)GetHWND(), (HMENU
) NULL
);
351 wxStatusBar
*theStatusBar
= GetStatusBar();
353 theStatusBar
->GetSize(NULL
, &m_fsStatusBarHeight
);
355 // Save the number of fields in the statusbar
356 if ((style
& wxFULLSCREEN_NOSTATUSBAR
) && theStatusBar
)
358 //m_fsStatusBarFields = theStatusBar->GetFieldsCount();
359 //SetStatusBar((wxStatusBar*) NULL);
360 //delete theStatusBar;
361 theStatusBar
->Show(FALSE
);
364 m_fsStatusBarFields
= 0;
365 #endif // wxUSE_STATUSBAR
367 // zap the frame borders
369 // save the 'normal' window style
370 m_fsOldWindowStyle
= GetWindowLong((HWND
)GetHWND(), GWL_STYLE
);
372 // save the old position, width & height, maximize state
373 m_fsOldSize
= GetRect();
374 m_fsIsMaximized
= IsMaximized();
376 // decide which window style flags to turn off
377 LONG newStyle
= m_fsOldWindowStyle
;
380 if (style
& wxFULLSCREEN_NOBORDER
)
381 offFlags
|= WS_BORDER
| WS_THICKFRAME
;
382 if (style
& wxFULLSCREEN_NOCAPTION
)
383 offFlags
|= (WS_CAPTION
| WS_SYSMENU
);
385 newStyle
&= (~offFlags
);
387 // change our window style to be compatible with full-screen mode
388 ::SetWindowLong((HWND
)GetHWND(), GWL_STYLE
, newStyle
);
390 // resize to the size of the desktop
393 RECT rect
= wxGetWindowRect(::GetDesktopWindow());
394 width
= rect
.right
- rect
.left
;
395 height
= rect
.bottom
- rect
.top
;
397 SetSize(width
, height
);
399 // now flush the window style cache and actually go full-screen
400 SetWindowPos((HWND
)GetHWND(), HWND_TOP
, 0, 0, width
, height
, SWP_FRAMECHANGED
);
402 wxSizeEvent
event(wxSize(width
, height
), GetId());
403 GetEventHandler()->ProcessEvent(event
);
412 m_fsIsShowing
= FALSE
;
415 wxToolBar
*theToolBar
= GetToolBar();
417 // restore the toolbar, menubar, and statusbar
418 if (theToolBar
&& (m_fsStyle
& wxFULLSCREEN_NOTOOLBAR
))
420 theToolBar
->SetSize(-1, m_fsToolBarHeight
);
421 theToolBar
->Show(TRUE
);
423 #endif // wxUSE_TOOLBAR
426 if ( m_fsStyle
& wxFULLSCREEN_NOSTATUSBAR
)
428 //CreateStatusBar(m_fsStatusBarFields);
431 GetStatusBar()->Show(TRUE
);
435 #endif // wxUSE_STATUSBAR
437 #ifndef __WXMICROWIN__
438 if ((m_fsStyle
& wxFULLSCREEN_NOMENUBAR
) && (m_hMenu
!= 0))
439 SetMenu((HWND
)GetHWND(), (HMENU
)m_hMenu
);
442 Maximize(m_fsIsMaximized
);
443 SetWindowLong((HWND
)GetHWND(),GWL_STYLE
, m_fsOldWindowStyle
);
444 SetWindowPos((HWND
)GetHWND(),HWND_TOP
,m_fsOldSize
.x
, m_fsOldSize
.y
,
445 m_fsOldSize
.width
, m_fsOldSize
.height
, SWP_FRAMECHANGED
);
451 // Default activation behaviour - set the focus for the first child
453 void wxFrameMSW::OnActivate(wxActivateEvent
& event
)
455 if ( event
.GetActive() )
457 // restore focus to the child which was last focused
458 wxLogTrace(_T("focus"), _T("wxFrameMSW %08x activated."), m_hWnd
);
460 wxWindow
*parent
= m_winLastFocused
? m_winLastFocused
->GetParent()
467 wxSetFocusToChild(parent
, &m_winLastFocused
);
471 // remember the last focused child if it is our child
472 m_winLastFocused
= FindFocus();
474 // so we NULL it out if it's a child from some other frame
475 wxWindow
*win
= m_winLastFocused
;
478 if ( win
->IsTopLevel() )
482 m_winLastFocused
= NULL
;
488 win
= win
->GetParent();
491 wxLogTrace(_T("focus"),
492 _T("wxFrameMSW %08x deactivated, last focused: %08x."),
494 m_winLastFocused
? GetHwndOf(m_winLastFocused
)
501 // ----------------------------------------------------------------------------
502 // tool/status bar stuff
503 // ----------------------------------------------------------------------------
507 wxToolBar
* wxFrameMSW::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
509 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
514 return m_frameToolBar
;
517 void wxFrameMSW::PositionToolBar()
520 ::GetClientRect(GetHwnd(), &rect
);
523 if ( GetStatusBar() )
525 int statusX
, statusY
;
526 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
527 rect
.bottom
-= statusY
;
529 #endif // wxUSE_STATUSBAR
531 if ( GetToolBar() && GetToolBar()->IsShown() )
534 GetToolBar()->GetSize(&tw
, &th
);
536 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
545 // Use the 'real' MSW position here
546 GetToolBar()->SetSize(0, 0, tw
, th
, wxSIZE_NO_ADJUSTMENTS
);
549 #endif // wxUSE_TOOLBAR
551 // ----------------------------------------------------------------------------
552 // frame state (iconized/maximized/...)
553 // ----------------------------------------------------------------------------
555 // propagate our state change to all child frames: this allows us to emulate X
556 // Windows behaviour where child frames float independently of the parent one
557 // on the desktop, but are iconized/restored with it
558 void wxFrameMSW::IconizeChildFrames(bool bIconize
)
560 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
562 node
= node
->GetNext() )
564 wxWindow
*win
= node
->GetData();
566 // iconizing the frames with this style under Win95 shell puts them at
567 // the bottom of the screen (as the MDI children) instead of making
568 // them appear in the taskbar because they are, by virtue of this
569 // style, not managed by the taskbar - instead leave Windows take care
572 if ( win
->GetWindowStyle() & wxFRAME_TOOL_WINDOW
)
576 // the child MDI frames are a special case and should not be touched by
577 // the parent frame - instead, they are managed by the user
578 wxFrame
*frame
= wxDynamicCast(win
, wxFrame
);
580 #if wxUSE_MDI_ARCHITECTURE
581 && !wxDynamicCast(frame
, wxMDIChildFrame
)
582 #endif // wxUSE_MDI_ARCHITECTURE
585 frame
->Iconize(bIconize
);
590 WXHICON
wxFrameMSW::GetDefaultIcon() const
592 return (WXHICON
)(wxSTD_FRAME_ICON
? wxSTD_FRAME_ICON
593 : wxDEFAULT_FRAME_ICON
);
596 // ===========================================================================
597 // message processing
598 // ===========================================================================
600 // ---------------------------------------------------------------------------
602 // ---------------------------------------------------------------------------
604 bool wxFrameMSW::MSWTranslateMessage(WXMSG
* pMsg
)
606 if ( wxWindow::MSWTranslateMessage(pMsg
) )
609 #if wxUSE_MENUS && wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
610 // try the menu bar accels
611 wxMenuBar
*menuBar
= GetMenuBar();
615 const wxAcceleratorTable
& acceleratorTable
= menuBar
->GetAccelTable();
616 return acceleratorTable
.Translate(this, pMsg
);
619 #endif // wxUSE_MENUS && wxUSE_ACCEL
622 // ---------------------------------------------------------------------------
623 // our private (non virtual) message handlers
624 // ---------------------------------------------------------------------------
626 bool wxFrameMSW::HandlePaint()
629 if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) )
631 #ifndef __WXMICROWIN__
634 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
635 : (HICON
)GetDefaultIcon();
637 // Hold a pointer to the dc so long as the OnPaint() message
638 // is being processed
640 HDC hdc
= ::BeginPaint(GetHwnd(), &ps
);
642 // Erase background before painting or we get white background
643 MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L);
648 ::GetClientRect(GetHwnd(), &rect
);
650 // FIXME: why hardcoded?
651 static const int icon_width
= 32;
652 static const int icon_height
= 32;
654 int icon_x
= (int)((rect
.right
- icon_width
)/2);
655 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
657 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
);
660 ::EndPaint(GetHwnd(), &ps
);
667 return wxWindow::HandlePaint();
672 // nothing to paint - processed
677 bool wxFrameMSW::HandleSize(int x
, int y
, WXUINT id
)
679 bool processed
= FALSE
;
680 #ifndef __WXMICROWIN__
685 // only do it it if we were iconized before, otherwise resizing the
686 // parent frame has a curious side effect of bringing it under it's
691 // restore all child frames too
692 IconizeChildFrames(FALSE
);
694 (void)SendIconizeEvent(FALSE
);
703 // iconize all child frames too
704 IconizeChildFrames(TRUE
);
706 (void)SendIconizeEvent();
717 #endif // wxUSE_STATUSBAR
721 #endif // wxUSE_TOOLBAR
723 wxSizeEvent
event(wxSize(x
, y
), m_windowId
);
724 event
.SetEventObject( this );
725 processed
= GetEventHandler()->ProcessEvent(event
);
731 bool wxFrameMSW::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
)
735 // In case it's e.g. a toolbar.
736 wxWindow
*win
= wxFindWinFromHandle(control
);
738 return win
->MSWCommand(cmd
, id
);
741 // handle here commands from menus and accelerators
742 if ( cmd
== 0 || cmd
== 1 )
744 #if wxUSE_MENUS_NATIVE
745 if ( wxCurrentPopupMenu
)
747 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
748 wxCurrentPopupMenu
= NULL
;
750 return popupMenu
->MSWCommand(cmd
, id
);
752 #endif // wxUSE_MENUS_NATIVE
754 if ( ProcessCommand(id
) )
763 bool wxFrameMSW::HandleMenuSelect(WXWORD nItem
, WXWORD flags
, WXHMENU hMenu
)
766 if ( flags
== 0xFFFF && hMenu
== 0 )
768 // menu was removed from screen
771 #ifndef __WXMICROWIN__
772 else if ( !(flags
& MF_POPUP
) && !(flags
& MF_SEPARATOR
) )
780 // don't give hints for separators (doesn't make sense) nor for the
781 // items opening popup menus (they don't have them anyhow) but do clear
782 // the status line - otherwise, we would be left with the help message
783 // for the previous item which doesn't apply any more
784 wxStatusBar
*statbar
= GetStatusBar();
787 statbar
->SetStatusText(wxEmptyString
);
789 #endif // wxUSE_STATUSBAR
794 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, item
);
795 event
.SetEventObject( this );
797 return GetEventHandler()->ProcessEvent(event
);
800 // ---------------------------------------------------------------------------
801 // the window proc for wxFrameMSW
802 // ---------------------------------------------------------------------------
804 long wxFrameMSW::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
807 bool processed
= FALSE
;
812 // if we can't close, tell the system that we processed the
813 // message - otherwise it would close us
814 processed
= !Close();
821 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
824 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
828 #ifndef __WXMICROWIN__
833 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
835 processed
= HandleMenuSelect(item
, flags
, hmenu
);
841 processed
= HandlePaint();
844 #ifndef __WXMICROWIN__
845 case WM_QUERYDRAGICON
:
847 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
848 : (HICON
)GetDefaultIcon();
856 processed
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
);
861 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);