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"
37 #include "wx/dialog.h"
38 #include "wx/settings.h"
39 #include "wx/dcclient.h"
42 #include "wx/msw/private.h"
45 #include "wx/statusbr.h"
46 #include "wx/generic/statusbr.h"
47 #endif // wxUSE_STATUSBAR
50 #include "wx/toolbar.h"
51 #endif // wxUSE_TOOLBAR
53 #include "wx/menuitem.h"
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 extern wxWindowList wxModelessWindows
;
61 extern wxList WXDLLEXPORT wxPendingDelete
;
62 extern const wxChar
*wxFrameClassName
;
63 extern wxMenu
*wxCurrentPopupMenu
;
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 BEGIN_EVENT_TABLE(wxFrame
, wxFrameBase
)
70 EVT_ACTIVATE(wxFrame::OnActivate
)
71 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged
)
74 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxWindow
)
76 // ============================================================================
78 // ============================================================================
80 // ----------------------------------------------------------------------------
81 // static class members
82 // ----------------------------------------------------------------------------
84 #if wxUSE_NATIVE_STATUSBAR
85 bool wxFrame::m_useNativeStatusBar
= TRUE
;
87 bool wxFrame::m_useNativeStatusBar
= FALSE
;
90 // ----------------------------------------------------------------------------
91 // creation/destruction
92 // ----------------------------------------------------------------------------
103 bool wxFrame::Create(wxWindow
*parent
,
105 const wxString
& title
,
109 const wxString
& name
)
112 m_windowStyle
= style
;
113 m_frameMenuBar
= NULL
;
114 m_frameToolBar
= NULL
;
115 m_frameStatusBar
= NULL
;
117 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
122 m_windowId
= (int)NewControlId();
124 if (parent
) parent
->AddChild(this);
133 // we pass NULL as parent to MSWCreate because frames with parents behave
134 // very strangely under Win95 shell
135 // Alteration by JACS: keep normal Windows behaviour (float on top of parent)
137 if ((m_windowStyle
& wxFRAME_FLOAT_ON_PARENT
) == 0)
141 wxTopLevelWindows
.Append(this);
143 MSWCreate(m_windowId
, parent
, wxFrameClassName
, this, title
,
144 x
, y
, width
, height
, style
);
146 wxModelessWindows
.Append(this);
152 m_isBeingDeleted
= TRUE
;
153 wxTopLevelWindows
.DeleteObject(this);
157 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
159 wxTheApp
->SetTopWindow(NULL
);
161 if (wxTheApp
->GetExitOnFrameDelete())
167 wxModelessWindows
.DeleteObject(this);
169 // For some reason, wxWindows can activate another task altogether
170 // when a frame is destroyed after a modal dialog has been invoked.
171 // Try to bring the parent to the top.
172 // MT:Only do this if this frame is currently the active window, else weird
173 // things start to happen
174 if ( wxGetActiveWindow() == this )
175 if (GetParent() && GetParent()->GetHWND())
176 ::BringWindowToTop((HWND
) GetParent()->GetHWND());
179 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
180 void wxFrame::DoGetClientSize(int *x
, int *y
) const
183 ::GetClientRect(GetHwnd(), &rect
);
186 if ( GetStatusBar() )
188 int statusX
, statusY
;
189 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
190 rect
.bottom
-= statusY
;
192 #endif // wxUSE_STATUSBAR
194 wxPoint
pt(GetClientAreaOrigin());
204 // Set the client size (i.e. leave the calculation of borders etc.
206 void wxFrame::DoSetClientSize(int width
, int height
)
208 HWND hWnd
= GetHwnd();
211 ::GetClientRect(hWnd
, &rect
);
214 GetWindowRect(hWnd
, &rect2
);
216 // Find the difference between the entire window (title bar and all)
217 // and the client area; add this to the new client size to move the
219 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
220 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
223 if ( GetStatusBar() )
225 int statusX
, statusY
;
226 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
227 actual_height
+= statusY
;
229 #endif // wxUSE_STATUSBAR
231 wxPoint
pt(GetClientAreaOrigin());
232 actual_width
+= pt
.y
;
233 actual_height
+= pt
.x
;
236 point
.x
= rect2
.left
;
239 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)TRUE
);
241 wxSizeEvent
event(wxSize(width
, height
), m_windowId
);
242 event
.SetEventObject( this );
243 GetEventHandler()->ProcessEvent(event
);
246 void wxFrame::DoGetSize(int *width
, int *height
) const
249 GetWindowRect(GetHwnd(), &rect
);
250 *width
= rect
.right
- rect
.left
;
251 *height
= rect
.bottom
- rect
.top
;
254 void wxFrame::DoGetPosition(int *x
, int *y
) const
257 GetWindowRect(GetHwnd(), &rect
);
266 // ----------------------------------------------------------------------------
267 // variations around ::ShowWindow()
268 // ----------------------------------------------------------------------------
270 void wxFrame::DoShowWindow(int nShowCmd
)
272 ::ShowWindow(GetHwnd(), nShowCmd
);
274 m_iconized
= nShowCmd
== SW_MINIMIZE
;
277 bool wxFrame::Show(bool show
)
279 DoShowWindow(show
? SW_SHOW
: SW_HIDE
);
283 ::BringWindowToTop(GetHwnd());
285 wxActivateEvent
event(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
286 event
.SetEventObject( this );
287 GetEventHandler()->ProcessEvent(event
);
291 // Try to highlight the correct window (the parent)
294 HWND hWndParent
= GetHwndOf(GetParent());
296 ::BringWindowToTop(hWndParent
);
303 void wxFrame::Iconize(bool iconize
)
305 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
308 void wxFrame::Maximize(bool maximize
)
310 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
313 void wxFrame::Restore()
315 DoShowWindow(SW_RESTORE
);
318 bool wxFrame::IsIconized() const
320 ((wxFrame
*)this)->m_iconized
= (::IsIconic(GetHwnd()) != 0);
325 bool wxFrame::IsMaximized() const
327 return (::IsZoomed(GetHwnd()) != 0);
330 void wxFrame::SetIcon(const wxIcon
& icon
)
332 wxFrameBase::SetIcon(icon
);
334 #if defined(__WIN95__)
337 SendMessage(GetHwnd(), WM_SETICON
,
338 (WPARAM
)TRUE
, (LPARAM
)(HICON
) m_icon
.GetHICON());
344 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
,
347 const wxString
& name
)
349 wxStatusBar
*statusBar
= NULL
;
351 #if wxUSE_NATIVE_STATUSBAR
352 if ( UsesNativeStatusBar() )
354 statusBar
= (wxStatusBar
*)new wxStatusBar95(this, id
, style
);
356 statusBar
->SetFieldsCount(number
);
361 statusBar
= (wxStatusBar
*)new wxStatusBarGeneric(this, id
, style
, name
);
363 // Set the height according to the font and the border size
364 wxClientDC
dc(statusBar
);
365 dc
.SetFont(statusBar
->GetFont());
368 dc
.GetTextExtent(_T("X"), NULL
, &y
);
370 int height
= (int)( (11*y
)/10 + 2*statusBar
->GetBorderY());
372 statusBar
->SetSize(-1, -1, -1, height
);
374 statusBar
->SetFieldsCount(number
);
380 void wxFrame::PositionStatusBar()
382 if ( !m_frameStatusBar
)
385 // native status bar positions itself, but we must forward the WM_SIZE
387 #if wxUSE_NATIVE_STATUSBAR
388 wxStatusBar95
*sb
= wxDynamicCast(m_frameStatusBar
, wxStatusBar95
);
391 wxSizeEvent
event(GetSize(), sb
->GetId());
392 event
.SetEventObject(sb
);
394 sb
->GetEventHandler()->ProcessEvent(event
);
397 #endif // wxUSE_NATIVE_STATUSBAR
400 GetClientSize(&w
, &h
);
402 m_frameStatusBar
->GetSize(&sw
, &sh
);
404 // Since we wish the status bar to be directly under the client area,
405 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
406 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
409 #endif // wxUSE_STATUSBAR
411 void wxFrame::DetachMenuBar()
415 m_frameMenuBar
->Detach();
416 m_frameMenuBar
= NULL
;
420 void wxFrame::SetMenuBar(wxMenuBar
*menu_bar
)
428 m_frameMenuBar
= NULL
;
430 // Can set a menubar several times.
431 // TODO: how to prevent a memory leak if you have a currently-unattached
432 // menubar? wxWindows assumes that the frame will delete the menu (otherwise
433 // there are problems for MDI).
434 if (menu_bar
->GetHMenu())
436 m_hMenu
= menu_bar
->GetHMenu();
442 m_hMenu
= menu_bar
->Create();
448 InternalSetMenuBar();
450 m_frameMenuBar
= menu_bar
;
451 menu_bar
->Attach(this);
453 #if 0 // Old code that assumes only one call of SetMenuBar per frame.
460 wxCHECK_RET( !menu_bar
->GetFrame(), wxT("this menubar is already attached") );
463 delete m_frameMenuBar
;
465 m_hMenu
= menu_bar
->Create();
470 InternalSetMenuBar();
472 m_frameMenuBar
= menu_bar
;
473 menu_bar
->Attach(this);
477 void wxFrame::InternalSetMenuBar()
479 if ( !::SetMenu(GetHwnd(), (HMENU
)m_hMenu
) )
481 wxLogLastError("SetMenu");
485 // Responds to colour changes, and passes event on to children.
486 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
488 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
491 if ( m_frameStatusBar
)
493 wxSysColourChangedEvent event2
;
494 event2
.SetEventObject( m_frameStatusBar
);
495 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
498 // Propagate the event to the non-top-level children
499 wxWindow::OnSysColourChanged(event
);
507 bool wxFrame::MSWCreate(int id
, wxWindow
*parent
, const wxChar
*wclass
, wxWindow
*wx_win
, const wxChar
*title
,
508 int x
, int y
, int width
, int height
, long style
)
511 m_defaultIcon
= (WXHICON
) (wxSTD_FRAME_ICON
? wxSTD_FRAME_ICON
: wxDEFAULT_FRAME_ICON
);
513 // If child windows aren't properly drawn initially, WS_CLIPCHILDREN
514 // could be the culprit. But without it, you can get a lot of flicker.
517 if ((style
& wxCAPTION
) == wxCAPTION
)
518 msflags
= WS_OVERLAPPED
;
522 if (style
& wxMINIMIZE_BOX
)
523 msflags
|= WS_MINIMIZEBOX
;
524 if (style
& wxMAXIMIZE_BOX
)
525 msflags
|= WS_MAXIMIZEBOX
;
526 if (style
& wxTHICK_FRAME
)
527 msflags
|= WS_THICKFRAME
;
528 if (style
& wxSYSTEM_MENU
)
529 msflags
|= WS_SYSMENU
;
530 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
531 msflags
|= WS_MINIMIZE
;
532 if (style
& wxMAXIMIZE
)
533 msflags
|= WS_MAXIMIZE
;
534 if (style
& wxCAPTION
)
535 msflags
|= WS_CAPTION
;
536 if (style
& wxCLIP_CHILDREN
)
537 msflags
|= WS_CLIPCHILDREN
;
539 // Keep this in wxFrame because it saves recoding this function
542 if (style
& wxTINY_CAPTION_VERT
)
543 msflags
|= IBS_VERTCAPTION
;
544 if (style
& wxTINY_CAPTION_HORIZ
)
545 msflags
|= IBS_HORZCAPTION
;
547 if (style
& wxTINY_CAPTION_VERT
)
548 msflags
|= WS_CAPTION
;
549 if (style
& wxTINY_CAPTION_HORIZ
)
550 msflags
|= WS_CAPTION
;
552 if ((style
& wxTHICK_FRAME
) == 0)
553 msflags
|= WS_BORDER
;
555 WXDWORD extendedStyle
= MakeExtendedStyle(style
);
557 #if !defined(__WIN16__) && !defined(__SC__)
558 if (style
& wxFRAME_TOOL_WINDOW
)
559 extendedStyle
|= WS_EX_TOOLWINDOW
;
562 if (style
& wxSTAY_ON_TOP
)
563 extendedStyle
|= WS_EX_TOPMOST
;
566 if ( !wxWindow::MSWCreate(id
, parent
, wclass
, wx_win
, title
, x
, y
, width
, height
,
567 msflags
, NULL
, extendedStyle
) )
570 // Seems to be necessary if we use WS_POPUP
571 // style instead of WS_OVERLAPPED
572 if (width
> -1 && height
> -1)
573 ::PostMessage(GetHwnd(), WM_SIZE
, SIZE_RESTORED
, MAKELPARAM(width
, height
));
578 // Default activation behaviour - set the focus for the first child
580 void wxFrame::OnActivate(wxActivateEvent
& event
)
582 if ( !event
.GetActive() )
589 wxLogTrace(_T("focus"), _T("wxFrame %08x activated."), m_hWnd
);
591 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
593 node
= node
->GetNext() )
595 // FIXME all this is totally bogus - we need to do the same as wxPanel,
596 // but how to do it without duplicating the code?
599 wxWindow
*child
= node
->GetData();
601 if ( !child
->IsTopLevel()
603 && !wxDynamicCast(child
, wxToolBar
)
604 #endif // wxUSE_TOOLBAR
606 && !wxDynamicCast(child
, wxStatusBar
)
607 #endif // wxUSE_STATUSBAR
616 // ----------------------------------------------------------------------------
617 // tool/status bar stuff
618 // ----------------------------------------------------------------------------
622 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
624 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
629 return m_frameToolBar
;
632 void wxFrame::PositionToolBar()
635 ::GetClientRect(GetHwnd(), &rect
);
638 if ( GetStatusBar() )
640 int statusX
, statusY
;
641 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
642 rect
.bottom
-= statusY
;
644 #endif // wxUSE_STATUSBAR
649 GetToolBar()->GetSize(&tw
, &th
);
651 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
660 // Use the 'real' MSW position here
661 GetToolBar()->SetSize(0, 0, tw
, th
, wxSIZE_NO_ADJUSTMENTS
);
664 #endif // wxUSE_TOOLBAR
666 // ----------------------------------------------------------------------------
667 // frame state (iconized/maximized/...)
668 // ----------------------------------------------------------------------------
670 // propagate our state change to all child frames: this allows us to emulate X
671 // Windows behaviour where child frames float independently of the parent one
672 // on the desktop, but are iconized/restored with it
673 void wxFrame::IconizeChildFrames(bool bIconize
)
675 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
677 node
= node
->GetNext() )
679 wxWindow
*win
= node
->GetData();
681 if ( win
->IsKindOf(CLASSINFO(wxFrame
)) )
683 ((wxFrame
*)win
)->Iconize(bIconize
);
688 // ===========================================================================
689 // message processing
690 // ===========================================================================
692 // ---------------------------------------------------------------------------
694 // ---------------------------------------------------------------------------
696 bool wxFrame::MSWTranslateMessage(WXMSG
* pMsg
)
698 if ( wxWindow::MSWTranslateMessage(pMsg
) )
701 // try the menu bar accels
702 wxMenuBar
*menuBar
= GetMenuBar();
706 const wxAcceleratorTable
& acceleratorTable
= menuBar
->GetAccelTable();
707 return acceleratorTable
.Translate(this, pMsg
);
710 // ---------------------------------------------------------------------------
711 // our private (non virtual) message handlers
712 // ---------------------------------------------------------------------------
714 bool wxFrame::HandlePaint()
717 if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) )
721 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
722 : (HICON
)m_defaultIcon
;
724 // Hold a pointer to the dc so long as the OnPaint() message
725 // is being processed
727 HDC hdc
= ::BeginPaint(GetHwnd(), &ps
);
729 // Erase background before painting or we get white background
730 MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L);
735 ::GetClientRect(GetHwnd(), &rect
);
737 // FIXME: why hardcoded?
738 static const int icon_width
= 32;
739 static const int icon_height
= 32;
741 int icon_x
= (int)((rect
.right
- icon_width
)/2);
742 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
744 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
);
747 ::EndPaint(GetHwnd(), &ps
);
753 return wxWindow::HandlePaint();
758 // nothing to paint - processed
763 bool wxFrame::HandleSize(int x
, int y
, WXUINT id
)
765 bool processed
= FALSE
;
770 // only do it it if we were iconized before, otherwise resizing the
771 // parent frame has a curious side effect of bringing it under it's
776 // restore all child frames too
777 IconizeChildFrames(FALSE
);
786 // iconize all child frames too
787 IconizeChildFrames(TRUE
);
798 wxSizeEvent
event(wxSize(x
, y
), m_windowId
);
799 event
.SetEventObject( this );
800 processed
= GetEventHandler()->ProcessEvent(event
);
806 bool wxFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
)
810 // In case it's e.g. a toolbar.
811 wxWindow
*win
= wxFindWinFromHandle(control
);
813 return win
->MSWCommand(cmd
, id
);
816 // handle here commands from menus and accelerators
817 if ( cmd
== 0 || cmd
== 1 )
819 if ( wxCurrentPopupMenu
)
821 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
822 wxCurrentPopupMenu
= NULL
;
824 return popupMenu
->MSWCommand(cmd
, id
);
827 if ( ProcessCommand(id
) )
836 bool wxFrame::HandleMenuSelect(WXWORD nItem
, WXWORD flags
, WXHMENU hMenu
)
839 if ( flags
== 0xFFFF && hMenu
== 0 )
841 // menu was removed from screen
844 else if ( !(flags
& MF_POPUP
) && !(flags
& MF_SEPARATOR
) )
850 // don't give hints for separators (doesn't make sense) nor for the
851 // items opening popup menus (they don't have them anyhow)
855 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, item
);
856 event
.SetEventObject( this );
858 return GetEventHandler()->ProcessEvent(event
);
861 // ---------------------------------------------------------------------------
862 // the window proc for wxFrame
863 // ---------------------------------------------------------------------------
865 long wxFrame::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
868 bool processed
= FALSE
;
873 // if we can't close, tell the system that we processed the
874 // message - otherwise it would close us
875 processed
= !Close();
882 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
885 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
893 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
895 processed
= HandleMenuSelect(item
, flags
, hmenu
);
900 processed
= HandlePaint();
903 case WM_QUERYDRAGICON
:
905 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
906 : (HICON
)(m_defaultIcon
);
913 processed
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
);
918 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);