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"
47 #if wxUSE_NATIVE_STATUSBAR
48 #include "wx/msw/statbr95.h"
50 #endif // wxUSE_STATUSBAR
53 #include "wx/toolbar.h"
54 #endif // wxUSE_TOOLBAR
56 #include "wx/menuitem.h"
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 extern wxWindowList wxModelessWindows
;
64 extern wxList WXDLLEXPORT wxPendingDelete
;
65 extern const wxChar
*wxFrameClassName
;
66 extern wxMenu
*wxCurrentPopupMenu
;
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 BEGIN_EVENT_TABLE(wxFrame
, wxFrameBase
)
73 EVT_ACTIVATE(wxFrame::OnActivate
)
74 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged
)
77 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxWindow
)
79 // ============================================================================
81 // ============================================================================
83 // ----------------------------------------------------------------------------
84 // static class members
85 // ----------------------------------------------------------------------------
87 #if wxUSE_NATIVE_STATUSBAR
88 bool wxFrame::m_useNativeStatusBar
= TRUE
;
90 bool wxFrame::m_useNativeStatusBar
= FALSE
;
93 // ----------------------------------------------------------------------------
94 // creation/destruction
95 // ----------------------------------------------------------------------------
106 bool wxFrame::Create(wxWindow
*parent
,
108 const wxString
& title
,
112 const wxString
& name
)
115 m_windowStyle
= style
;
116 m_frameMenuBar
= NULL
;
117 m_frameToolBar
= NULL
;
118 m_frameStatusBar
= NULL
;
120 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
125 m_windowId
= (int)NewControlId();
127 if (parent
) parent
->AddChild(this);
136 // we pass NULL as parent to MSWCreate because frames with parents behave
137 // very strangely under Win95 shell
138 // Alteration by JACS: keep normal Windows behaviour (float on top of parent)
140 if ((m_windowStyle
& wxFRAME_FLOAT_ON_PARENT
) == 0)
144 wxTopLevelWindows
.Append(this);
146 MSWCreate(m_windowId
, parent
, wxFrameClassName
, this, title
,
147 x
, y
, width
, height
, style
);
149 wxModelessWindows
.Append(this);
155 m_isBeingDeleted
= TRUE
;
156 wxTopLevelWindows
.DeleteObject(this);
160 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
162 wxTheApp
->SetTopWindow(NULL
);
164 if (wxTheApp
->GetExitOnFrameDelete())
170 wxModelessWindows
.DeleteObject(this);
172 // For some reason, wxWindows can activate another task altogether
173 // when a frame is destroyed after a modal dialog has been invoked.
174 // Try to bring the parent to the top.
175 // MT:Only do this if this frame is currently the active window, else weird
176 // things start to happen
177 if ( wxGetActiveWindow() == this )
178 if (GetParent() && GetParent()->GetHWND())
179 ::BringWindowToTop((HWND
) GetParent()->GetHWND());
182 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
183 void wxFrame::DoGetClientSize(int *x
, int *y
) const
186 ::GetClientRect(GetHwnd(), &rect
);
189 if ( GetStatusBar() )
191 int statusX
, statusY
;
192 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
193 rect
.bottom
-= statusY
;
195 #endif // wxUSE_STATUSBAR
197 wxPoint
pt(GetClientAreaOrigin());
207 // Set the client size (i.e. leave the calculation of borders etc.
209 void wxFrame::DoSetClientSize(int width
, int height
)
211 HWND hWnd
= GetHwnd();
214 ::GetClientRect(hWnd
, &rect
);
217 GetWindowRect(hWnd
, &rect2
);
219 // Find the difference between the entire window (title bar and all)
220 // and the client area; add this to the new client size to move the
222 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
223 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
226 if ( GetStatusBar() )
228 int statusX
, statusY
;
229 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
230 actual_height
+= statusY
;
232 #endif // wxUSE_STATUSBAR
234 wxPoint
pt(GetClientAreaOrigin());
235 actual_width
+= pt
.y
;
236 actual_height
+= pt
.x
;
239 point
.x
= rect2
.left
;
242 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)TRUE
);
244 wxSizeEvent
event(wxSize(width
, height
), m_windowId
);
245 event
.SetEventObject( this );
246 GetEventHandler()->ProcessEvent(event
);
249 void wxFrame::DoGetSize(int *width
, int *height
) const
252 GetWindowRect(GetHwnd(), &rect
);
253 *width
= rect
.right
- rect
.left
;
254 *height
= rect
.bottom
- rect
.top
;
257 void wxFrame::DoGetPosition(int *x
, int *y
) const
260 GetWindowRect(GetHwnd(), &rect
);
269 // ----------------------------------------------------------------------------
270 // variations around ::ShowWindow()
271 // ----------------------------------------------------------------------------
273 void wxFrame::DoShowWindow(int nShowCmd
)
275 ::ShowWindow(GetHwnd(), nShowCmd
);
277 m_iconized
= nShowCmd
== SW_MINIMIZE
;
280 bool wxFrame::Show(bool show
)
282 DoShowWindow(show
? SW_SHOW
: SW_HIDE
);
286 ::BringWindowToTop(GetHwnd());
288 wxActivateEvent
event(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
289 event
.SetEventObject( this );
290 GetEventHandler()->ProcessEvent(event
);
294 // Try to highlight the correct window (the parent)
297 HWND hWndParent
= GetHwndOf(GetParent());
299 ::BringWindowToTop(hWndParent
);
306 void wxFrame::Iconize(bool iconize
)
308 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
311 void wxFrame::Maximize(bool maximize
)
313 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
316 void wxFrame::Restore()
318 DoShowWindow(SW_RESTORE
);
321 bool wxFrame::IsIconized() const
323 ((wxFrame
*)this)->m_iconized
= (::IsIconic(GetHwnd()) != 0);
328 bool wxFrame::IsMaximized() const
330 return (::IsZoomed(GetHwnd()) != 0);
333 void wxFrame::SetIcon(const wxIcon
& icon
)
335 wxFrameBase::SetIcon(icon
);
337 #if defined(__WIN95__)
340 SendMessage(GetHwnd(), WM_SETICON
,
341 (WPARAM
)TRUE
, (LPARAM
)(HICON
) m_icon
.GetHICON());
347 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
,
350 const wxString
& name
)
352 wxStatusBar
*statusBar
= NULL
;
354 #if wxUSE_NATIVE_STATUSBAR
355 if ( UsesNativeStatusBar() )
357 statusBar
= new wxStatusBar95(this, id
, style
);
359 statusBar
->SetFieldsCount(number
);
364 statusBar
= wxFrameBase::OnCreateStatusBar(number
, style
, id
, name
);
370 void wxFrame::PositionStatusBar()
372 // native status bar positions itself
373 if ( m_frameStatusBar
374 #if wxUSE_NATIVE_STATUSBAR
375 && !m_frameStatusBar
->IsKindOf(CLASSINFO(wxStatusBar95
))
380 GetClientSize(&w
, &h
);
382 m_frameStatusBar
->GetSize(&sw
, &sh
);
384 // Since we wish the status bar to be directly under the client area,
385 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
386 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
389 #endif // wxUSE_STATUSBAR
391 void wxFrame::DetachMenuBar()
395 m_frameMenuBar
->Detach();
396 m_frameMenuBar
= NULL
;
400 void wxFrame::SetMenuBar(wxMenuBar
*menu_bar
)
408 m_frameMenuBar
= NULL
;
410 // Can set a menubar several times.
411 // TODO: how to prevent a memory leak if you have a currently-unattached
412 // menubar? wxWindows assumes that the frame will delete the menu (otherwise
413 // there are problems for MDI).
414 if (menu_bar
->GetHMenu())
416 m_hMenu
= menu_bar
->GetHMenu();
422 m_hMenu
= menu_bar
->Create();
428 InternalSetMenuBar();
430 m_frameMenuBar
= menu_bar
;
431 menu_bar
->Attach(this);
433 #if 0 // Old code that assumes only one call of SetMenuBar per frame.
440 wxCHECK_RET( !menu_bar
->GetFrame(), wxT("this menubar is already attached") );
443 delete m_frameMenuBar
;
445 m_hMenu
= menu_bar
->Create();
450 InternalSetMenuBar();
452 m_frameMenuBar
= menu_bar
;
453 menu_bar
->Attach(this);
457 void wxFrame::InternalSetMenuBar()
459 if ( !::SetMenu(GetHwnd(), (HMENU
)m_hMenu
) )
461 wxLogLastError("SetMenu");
465 // Responds to colour changes, and passes event on to children.
466 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
468 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
471 if ( m_frameStatusBar
)
473 wxSysColourChangedEvent event2
;
474 event2
.SetEventObject( m_frameStatusBar
);
475 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
478 // Propagate the event to the non-top-level children
479 wxWindow::OnSysColourChanged(event
);
487 bool wxFrame::MSWCreate(int id
, wxWindow
*parent
, const wxChar
*wclass
, wxWindow
*wx_win
, const wxChar
*title
,
488 int x
, int y
, int width
, int height
, long style
)
491 m_defaultIcon
= (WXHICON
) (wxSTD_FRAME_ICON
? wxSTD_FRAME_ICON
: wxDEFAULT_FRAME_ICON
);
493 // If child windows aren't properly drawn initially, WS_CLIPCHILDREN
494 // could be the culprit. But without it, you can get a lot of flicker.
497 if ((style
& wxCAPTION
) == wxCAPTION
)
498 msflags
= WS_OVERLAPPED
;
502 if (style
& wxMINIMIZE_BOX
)
503 msflags
|= WS_MINIMIZEBOX
;
504 if (style
& wxMAXIMIZE_BOX
)
505 msflags
|= WS_MAXIMIZEBOX
;
506 if (style
& wxTHICK_FRAME
)
507 msflags
|= WS_THICKFRAME
;
508 if (style
& wxSYSTEM_MENU
)
509 msflags
|= WS_SYSMENU
;
510 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
511 msflags
|= WS_MINIMIZE
;
512 if (style
& wxMAXIMIZE
)
513 msflags
|= WS_MAXIMIZE
;
514 if (style
& wxCAPTION
)
515 msflags
|= WS_CAPTION
;
516 if (style
& wxCLIP_CHILDREN
)
517 msflags
|= WS_CLIPCHILDREN
;
519 // Keep this in wxFrame because it saves recoding this function
522 if (style
& wxTINY_CAPTION_VERT
)
523 msflags
|= IBS_VERTCAPTION
;
524 if (style
& wxTINY_CAPTION_HORIZ
)
525 msflags
|= IBS_HORZCAPTION
;
527 if (style
& wxTINY_CAPTION_VERT
)
528 msflags
|= WS_CAPTION
;
529 if (style
& wxTINY_CAPTION_HORIZ
)
530 msflags
|= WS_CAPTION
;
532 if ((style
& wxTHICK_FRAME
) == 0)
533 msflags
|= WS_BORDER
;
535 WXDWORD extendedStyle
= MakeExtendedStyle(style
);
537 #if !defined(__WIN16__) && !defined(__SC__)
538 if (style
& wxFRAME_TOOL_WINDOW
)
539 extendedStyle
|= WS_EX_TOOLWINDOW
;
542 if (style
& wxSTAY_ON_TOP
)
543 extendedStyle
|= WS_EX_TOPMOST
;
546 if ( !wxWindow::MSWCreate(id
, parent
, wclass
, wx_win
, title
, x
, y
, width
, height
,
547 msflags
, NULL
, extendedStyle
) )
550 // Seems to be necessary if we use WS_POPUP
551 // style instead of WS_OVERLAPPED
552 if (width
> -1 && height
> -1)
553 ::PostMessage(GetHwnd(), WM_SIZE
, SIZE_RESTORED
, MAKELPARAM(width
, height
));
558 // Default activation behaviour - set the focus for the first child
560 void wxFrame::OnActivate(wxActivateEvent
& event
)
562 if ( !event
.GetActive() )
569 wxLogTrace(_T("focus"), _T("wxFrame %08x activated."), m_hWnd
);
571 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
573 node
= node
->GetNext() )
575 // FIXME all this is totally bogus - we need to do the same as wxPanel,
576 // but how to do it without duplicating the code?
579 wxWindow
*child
= node
->GetData();
581 if ( !child
->IsTopLevel()
583 && !wxDynamicCast(child
, wxToolBar
)
584 #endif // wxUSE_TOOLBAR
586 && !wxDynamicCast(child
, wxStatusBar
)
587 #endif // wxUSE_STATUSBAR
596 // ----------------------------------------------------------------------------
597 // tool/status bar stuff
598 // ----------------------------------------------------------------------------
602 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
604 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
609 return m_frameToolBar
;
612 void wxFrame::PositionToolBar()
615 ::GetClientRect(GetHwnd(), &rect
);
618 if ( GetStatusBar() )
620 int statusX
, statusY
;
621 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
622 rect
.bottom
-= statusY
;
624 #endif // wxUSE_STATUSBAR
629 GetToolBar()->GetSize(&tw
, &th
);
631 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
640 // Use the 'real' MSW position here
641 GetToolBar()->SetSize(0, 0, tw
, th
, wxSIZE_NO_ADJUSTMENTS
);
644 #endif // wxUSE_TOOLBAR
646 // ----------------------------------------------------------------------------
647 // frame state (iconized/maximized/...)
648 // ----------------------------------------------------------------------------
650 // propagate our state change to all child frames: this allows us to emulate X
651 // Windows behaviour where child frames float independently of the parent one
652 // on the desktop, but are iconized/restored with it
653 void wxFrame::IconizeChildFrames(bool bIconize
)
655 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
657 node
= node
->GetNext() )
659 wxWindow
*win
= node
->GetData();
661 if ( win
->IsKindOf(CLASSINFO(wxFrame
)) )
663 ((wxFrame
*)win
)->Iconize(bIconize
);
668 // ===========================================================================
669 // message processing
670 // ===========================================================================
672 // ---------------------------------------------------------------------------
674 // ---------------------------------------------------------------------------
676 bool wxFrame::MSWTranslateMessage(WXMSG
* pMsg
)
678 if ( wxWindow::MSWTranslateMessage(pMsg
) )
681 // try the menu bar accels
682 wxMenuBar
*menuBar
= GetMenuBar();
686 const wxAcceleratorTable
& acceleratorTable
= menuBar
->GetAccelTable();
687 return acceleratorTable
.Translate(this, pMsg
);
690 // ---------------------------------------------------------------------------
691 // our private (non virtual) message handlers
692 // ---------------------------------------------------------------------------
694 bool wxFrame::HandlePaint()
697 if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) )
701 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
702 : (HICON
)m_defaultIcon
;
704 // Hold a pointer to the dc so long as the OnPaint() message
705 // is being processed
707 HDC hdc
= ::BeginPaint(GetHwnd(), &ps
);
709 // Erase background before painting or we get white background
710 MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L);
715 ::GetClientRect(GetHwnd(), &rect
);
717 // FIXME: why hardcoded?
718 static const int icon_width
= 32;
719 static const int icon_height
= 32;
721 int icon_x
= (int)((rect
.right
- icon_width
)/2);
722 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
724 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
);
727 ::EndPaint(GetHwnd(), &ps
);
733 return wxWindow::HandlePaint();
738 // nothing to paint - processed
743 bool wxFrame::HandleSize(int x
, int y
, WXUINT id
)
745 bool processed
= FALSE
;
750 // only do it it if we were iconized before, otherwise resizing the
751 // parent frame has a curious side effect of bringing it under it's
756 // restore all child frames too
757 IconizeChildFrames(FALSE
);
766 // iconize all child frames too
767 IconizeChildFrames(TRUE
);
775 // forward WM_SIZE to status bar control
776 #if wxUSE_NATIVE_STATUSBAR
777 if (m_frameStatusBar
&& m_frameStatusBar
->IsKindOf(CLASSINFO(wxStatusBar95
)))
779 wxSizeEvent
event(wxSize(x
, y
), m_frameStatusBar
->GetId());
780 event
.SetEventObject( m_frameStatusBar
);
782 ((wxStatusBar95
*)m_frameStatusBar
)->OnSize(event
);
784 #endif // wxUSE_NATIVE_STATUSBAR
789 wxSizeEvent
event(wxSize(x
, y
), m_windowId
);
790 event
.SetEventObject( this );
791 processed
= GetEventHandler()->ProcessEvent(event
);
797 bool wxFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
)
801 // In case it's e.g. a toolbar.
802 wxWindow
*win
= wxFindWinFromHandle(control
);
804 return win
->MSWCommand(cmd
, id
);
807 // handle here commands from menus and accelerators
808 if ( cmd
== 0 || cmd
== 1 )
810 if ( wxCurrentPopupMenu
)
812 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
813 wxCurrentPopupMenu
= NULL
;
815 return popupMenu
->MSWCommand(cmd
, id
);
818 if ( ProcessCommand(id
) )
827 bool wxFrame::HandleMenuSelect(WXWORD nItem
, WXWORD flags
, WXHMENU hMenu
)
830 if ( flags
== 0xFFFF && hMenu
== 0 )
832 // menu was removed from screen
835 else if ( !(flags
& MF_POPUP
) && !(flags
& MF_SEPARATOR
) )
841 // don't give hints for separators (doesn't make sense) nor for the
842 // items opening popup menus (they don't have them anyhow)
846 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, item
);
847 event
.SetEventObject( this );
849 return GetEventHandler()->ProcessEvent(event
);
852 // ---------------------------------------------------------------------------
853 // the window proc for wxFrame
854 // ---------------------------------------------------------------------------
856 long wxFrame::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
859 bool processed
= FALSE
;
864 // if we can't close, tell the system that we processed the
865 // message - otherwise it would close us
866 processed
= !Close();
873 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
876 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
884 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
886 processed
= HandleMenuSelect(item
, flags
, hmenu
);
891 processed
= HandlePaint();
894 case WM_QUERYDRAGICON
:
896 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
897 : (HICON
)(m_defaultIcon
);
904 processed
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
);
909 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);