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 wxChar wxFrameClassName
[];
66 extern wxMenu
*wxCurrentPopupMenu
;
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 #if !USE_SHARED_LIBRARY
73 BEGIN_EVENT_TABLE(wxFrame
, wxFrameBase
)
74 EVT_ACTIVATE(wxFrame::OnActivate
)
75 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged
)
78 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxWindow
)
81 // ============================================================================
83 // ============================================================================
85 // ----------------------------------------------------------------------------
86 // static class members
87 // ----------------------------------------------------------------------------
89 #if wxUSE_NATIVE_STATUSBAR
90 bool wxFrame::m_useNativeStatusBar
= TRUE
;
92 bool wxFrame::m_useNativeStatusBar
= FALSE
;
95 // ----------------------------------------------------------------------------
96 // creation/destruction
97 // ----------------------------------------------------------------------------
108 bool wxFrame::Create(wxWindow
*parent
,
110 const wxString
& title
,
114 const wxString
& name
)
117 m_windowStyle
= style
;
118 m_frameMenuBar
= NULL
;
119 m_frameToolBar
= NULL
;
120 m_frameStatusBar
= NULL
;
122 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
127 m_windowId
= (int)NewControlId();
129 if (parent
) parent
->AddChild(this);
138 // we pass NULL as parent to MSWCreate because frames with parents behave
139 // very strangely under Win95 shell
140 // Alteration by JACS: keep normal Windows behaviour (float on top of parent)
142 if ((m_windowStyle
& wxFRAME_FLOAT_ON_PARENT
) == 0)
146 wxTopLevelWindows
.Append(this);
148 MSWCreate(m_windowId
, parent
, wxFrameClassName
, this, title
,
149 x
, y
, width
, height
, style
);
151 wxModelessWindows
.Append(this);
157 m_isBeingDeleted
= TRUE
;
158 wxTopLevelWindows
.DeleteObject(this);
162 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
164 wxTheApp
->SetTopWindow(NULL
);
166 if (wxTheApp
->GetExitOnFrameDelete())
172 wxModelessWindows
.DeleteObject(this);
174 // For some reason, wxWindows can activate another task altogether
175 // when a frame is destroyed after a modal dialog has been invoked.
176 // Try to bring the parent to the top.
177 // MT:Only do this if this frame is currently the active window, else weird
178 // things start to happen
179 if ( wxGetActiveWindow() == this )
180 if (GetParent() && GetParent()->GetHWND())
181 ::BringWindowToTop((HWND
) GetParent()->GetHWND());
184 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
185 void wxFrame::DoGetClientSize(int *x
, int *y
) const
188 ::GetClientRect(GetHwnd(), &rect
);
191 if ( GetStatusBar() )
193 int statusX
, statusY
;
194 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
195 rect
.bottom
-= statusY
;
197 #endif // wxUSE_STATUSBAR
199 wxPoint
pt(GetClientAreaOrigin());
209 // Set the client size (i.e. leave the calculation of borders etc.
211 void wxFrame::DoSetClientSize(int width
, int height
)
213 HWND hWnd
= GetHwnd();
216 ::GetClientRect(hWnd
, &rect
);
219 GetWindowRect(hWnd
, &rect2
);
221 // Find the difference between the entire window (title bar and all)
222 // and the client area; add this to the new client size to move the
224 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
225 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
228 if ( GetStatusBar() )
230 int statusX
, statusY
;
231 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
232 actual_height
+= statusY
;
234 #endif // wxUSE_STATUSBAR
236 wxPoint
pt(GetClientAreaOrigin());
237 actual_width
+= pt
.y
;
238 actual_height
+= pt
.x
;
241 point
.x
= rect2
.left
;
244 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)TRUE
);
246 wxSizeEvent
event(wxSize(width
, height
), m_windowId
);
247 event
.SetEventObject( this );
248 GetEventHandler()->ProcessEvent(event
);
251 void wxFrame::DoGetSize(int *width
, int *height
) const
254 GetWindowRect(GetHwnd(), &rect
);
255 *width
= rect
.right
- rect
.left
;
256 *height
= rect
.bottom
- rect
.top
;
259 void wxFrame::DoGetPosition(int *x
, int *y
) const
262 GetWindowRect(GetHwnd(), &rect
);
271 // ----------------------------------------------------------------------------
272 // variations around ::ShowWindow()
273 // ----------------------------------------------------------------------------
275 void wxFrame::DoShowWindow(int nShowCmd
)
277 ::ShowWindow(GetHwnd(), nShowCmd
);
279 m_iconized
= nShowCmd
== SW_MINIMIZE
;
282 bool wxFrame::Show(bool show
)
284 DoShowWindow(show
? SW_SHOW
: SW_HIDE
);
288 ::BringWindowToTop(GetHwnd());
290 wxActivateEvent
event(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
291 event
.SetEventObject( this );
292 GetEventHandler()->ProcessEvent(event
);
296 // Try to highlight the correct window (the parent)
299 HWND hWndParent
= GetHwndOf(GetParent());
301 ::BringWindowToTop(hWndParent
);
308 void wxFrame::Iconize(bool iconize
)
310 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
313 void wxFrame::Maximize(bool maximize
)
315 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
318 void wxFrame::Restore()
320 DoShowWindow(SW_RESTORE
);
323 bool wxFrame::IsIconized() const
325 ((wxFrame
*)this)->m_iconized
= (::IsIconic(GetHwnd()) != 0);
330 bool wxFrame::IsMaximized() const
332 return (::IsZoomed(GetHwnd()) != 0);
335 void wxFrame::SetIcon(const wxIcon
& icon
)
337 wxFrameBase::SetIcon(icon
);
339 #if defined(__WIN95__)
342 SendMessage(GetHwnd(), WM_SETICON
,
343 (WPARAM
)TRUE
, (LPARAM
)(HICON
) m_icon
.GetHICON());
349 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
,
352 const wxString
& name
)
354 wxStatusBar
*statusBar
= NULL
;
356 #if wxUSE_NATIVE_STATUSBAR
357 if ( UsesNativeStatusBar() )
359 statusBar
= new wxStatusBar95(this, id
, style
);
361 statusBar
->SetFieldsCount(number
);
366 statusBar
= wxFrameBase::OnCreateStatusBar(number
, style
, id
, name
);
372 void wxFrame::PositionStatusBar()
374 // native status bar positions itself
375 if ( m_frameStatusBar
376 #if wxUSE_NATIVE_STATUSBAR
377 && !m_frameStatusBar
->IsKindOf(CLASSINFO(wxStatusBar95
))
382 GetClientSize(&w
, &h
);
384 m_frameStatusBar
->GetSize(&sw
, &sh
);
386 // Since we wish the status bar to be directly under the client area,
387 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
388 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
391 #endif // wxUSE_STATUSBAR
393 void wxFrame::DetachMenuBar()
397 m_frameMenuBar
->Detach();
398 m_frameMenuBar
= NULL
;
402 void wxFrame::SetMenuBar(wxMenuBar
*menu_bar
)
410 wxCHECK_RET( !menu_bar
->GetFrame(), wxT("this menubar is already attached") );
413 delete m_frameMenuBar
;
415 m_hMenu
= menu_bar
->Create();
420 InternalSetMenuBar();
422 m_frameMenuBar
= menu_bar
;
423 menu_bar
->Attach(this);
426 void wxFrame::InternalSetMenuBar()
428 if ( !::SetMenu(GetHwnd(), (HMENU
)m_hMenu
) )
430 wxLogLastError("SetMenu");
434 // Responds to colour changes, and passes event on to children.
435 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
437 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
440 if ( m_frameStatusBar
)
442 wxSysColourChangedEvent event2
;
443 event2
.SetEventObject( m_frameStatusBar
);
444 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
447 // Propagate the event to the non-top-level children
448 wxWindow::OnSysColourChanged(event
);
456 bool wxFrame::MSWCreate(int id
, wxWindow
*parent
, const wxChar
*wclass
, wxWindow
*wx_win
, const wxChar
*title
,
457 int x
, int y
, int width
, int height
, long style
)
460 m_defaultIcon
= (WXHICON
) (wxSTD_FRAME_ICON
? wxSTD_FRAME_ICON
: wxDEFAULT_FRAME_ICON
);
462 // If child windows aren't properly drawn initially, WS_CLIPCHILDREN
463 // could be the culprit. But without it, you can get a lot of flicker.
466 if ((style
& wxCAPTION
) == wxCAPTION
)
467 msflags
= WS_OVERLAPPED
;
471 if (style
& wxMINIMIZE_BOX
)
472 msflags
|= WS_MINIMIZEBOX
;
473 if (style
& wxMAXIMIZE_BOX
)
474 msflags
|= WS_MAXIMIZEBOX
;
475 if (style
& wxTHICK_FRAME
)
476 msflags
|= WS_THICKFRAME
;
477 if (style
& wxSYSTEM_MENU
)
478 msflags
|= WS_SYSMENU
;
479 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
480 msflags
|= WS_MINIMIZE
;
481 if (style
& wxMAXIMIZE
)
482 msflags
|= WS_MAXIMIZE
;
483 if (style
& wxCAPTION
)
484 msflags
|= WS_CAPTION
;
485 if (style
& wxCLIP_CHILDREN
)
486 msflags
|= WS_CLIPCHILDREN
;
488 // Keep this in wxFrame because it saves recoding this function
491 if (style
& wxTINY_CAPTION_VERT
)
492 msflags
|= IBS_VERTCAPTION
;
493 if (style
& wxTINY_CAPTION_HORIZ
)
494 msflags
|= IBS_HORZCAPTION
;
496 if (style
& wxTINY_CAPTION_VERT
)
497 msflags
|= WS_CAPTION
;
498 if (style
& wxTINY_CAPTION_HORIZ
)
499 msflags
|= WS_CAPTION
;
501 if ((style
& wxTHICK_FRAME
) == 0)
502 msflags
|= WS_BORDER
;
504 WXDWORD extendedStyle
= MakeExtendedStyle(style
);
506 #if !defined(__WIN16__) && !defined(__SC__)
507 if (style
& wxFRAME_TOOL_WINDOW
)
508 extendedStyle
|= WS_EX_TOOLWINDOW
;
511 if (style
& wxSTAY_ON_TOP
)
512 extendedStyle
|= WS_EX_TOPMOST
;
515 if ( !wxWindow::MSWCreate(id
, parent
, wclass
, wx_win
, title
, x
, y
, width
, height
,
516 msflags
, NULL
, extendedStyle
) )
519 // Seems to be necessary if we use WS_POPUP
520 // style instead of WS_OVERLAPPED
521 if (width
> -1 && height
> -1)
522 ::PostMessage(GetHwnd(), WM_SIZE
, SIZE_RESTORED
, MAKELPARAM(width
, height
));
527 // Default activation behaviour - set the focus for the first child
529 void wxFrame::OnActivate(wxActivateEvent
& event
)
531 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
533 node
= node
->GetNext() )
535 // FIXME all this is totally bogus - we need to do the same as wxPanel,
536 // but how to do it without duplicating the code?
539 wxWindow
*child
= node
->GetData();
541 if ( !child
->IsTopLevel()
543 && !wxDynamicCast(child
, wxToolBar
)
544 #endif // wxUSE_TOOLBAR
546 && !wxDynamicCast(child
, wxStatusBar
)
547 #endif // wxUSE_STATUSBAR
556 // ----------------------------------------------------------------------------
557 // wxFrame size management: we exclude the areas taken by menu/status/toolbars
558 // from the client area, so the client area is what's really available for the
560 // ----------------------------------------------------------------------------
562 // Checks if there is a toolbar, and returns the first free client position
563 wxPoint
wxFrame::GetClientAreaOrigin() const
569 GetToolBar()->GetSize(& w
, & h
);
571 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
583 void wxFrame::DoScreenToClient(int *x
, int *y
) const
585 wxWindow::DoScreenToClient(x
, y
);
587 // We may be faking the client origin.
588 // So a window that's really at (0, 30) may appear
589 // (to wxWin apps) to be at (0, 0).
590 wxPoint
pt(GetClientAreaOrigin());
595 void wxFrame::DoClientToScreen(int *x
, int *y
) const
597 // We may be faking the client origin.
598 // So a window that's really at (0, 30) may appear
599 // (to wxWin apps) to be at (0, 0).
600 wxPoint
pt1(GetClientAreaOrigin());
604 wxWindow::DoClientToScreen(x
, y
);
607 // ----------------------------------------------------------------------------
608 // tool/status bar stuff
609 // ----------------------------------------------------------------------------
613 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
615 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
620 return m_frameToolBar
;
623 void wxFrame::PositionToolBar()
626 ::GetClientRect(GetHwnd(), &rect
);
629 if ( GetStatusBar() )
631 int statusX
, statusY
;
632 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
633 rect
.bottom
-= statusY
;
635 #endif // wxUSE_STATUSBAR
640 GetToolBar()->GetSize(&tw
, &th
);
642 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
651 // Use the 'real' MSW position here
652 GetToolBar()->SetSize(0, 0, tw
, th
, wxSIZE_NO_ADJUSTMENTS
);
655 #endif // wxUSE_TOOLBAR
657 // ----------------------------------------------------------------------------
658 // frame state (iconized/maximized/...)
659 // ----------------------------------------------------------------------------
661 // propagate our state change to all child frames: this allows us to emulate X
662 // Windows behaviour where child frames float independently of the parent one
663 // on the desktop, but are iconized/restored with it
664 void wxFrame::IconizeChildFrames(bool bIconize
)
666 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
668 node
= node
->GetNext() )
670 wxWindow
*win
= node
->GetData();
672 if ( win
->IsKindOf(CLASSINFO(wxFrame
)) )
674 ((wxFrame
*)win
)->Iconize(bIconize
);
679 // ===========================================================================
680 // message processing
681 // ===========================================================================
683 // ---------------------------------------------------------------------------
685 // ---------------------------------------------------------------------------
687 bool wxFrame::MSWTranslateMessage(WXMSG
* pMsg
)
689 if ( wxWindow::MSWTranslateMessage(pMsg
) )
692 // try the menu bar accels
693 wxMenuBar
*menuBar
= GetMenuBar();
697 const wxAcceleratorTable
& acceleratorTable
= menuBar
->GetAccelTable();
698 return acceleratorTable
.Translate(this, pMsg
);
701 // ---------------------------------------------------------------------------
702 // our private (non virtual) message handlers
703 // ---------------------------------------------------------------------------
705 bool wxFrame::HandlePaint()
708 if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) )
712 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
713 : (HICON
)m_defaultIcon
;
715 // Hold a pointer to the dc so long as the OnPaint() message
716 // is being processed
718 HDC hdc
= ::BeginPaint(GetHwnd(), &ps
);
720 // Erase background before painting or we get white background
721 MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L);
726 ::GetClientRect(GetHwnd(), &rect
);
728 // FIXME: why hardcoded?
729 static const int icon_width
= 32;
730 static const int icon_height
= 32;
732 int icon_x
= (int)((rect
.right
- icon_width
)/2);
733 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
735 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
);
738 ::EndPaint(GetHwnd(), &ps
);
744 return wxWindow::HandlePaint();
749 // nothing to paint - processed
754 bool wxFrame::HandleSize(int x
, int y
, WXUINT id
)
756 bool processed
= FALSE
;
761 // only do it it if we were iconized before, otherwise resizing the
762 // parent frame has a curious side effect of bringing it under it's
767 // restore all child frames too
768 IconizeChildFrames(FALSE
);
777 // iconize all child frames too
778 IconizeChildFrames(TRUE
);
786 // forward WM_SIZE to status bar control
787 #if wxUSE_NATIVE_STATUSBAR
788 if (m_frameStatusBar
&& m_frameStatusBar
->IsKindOf(CLASSINFO(wxStatusBar95
)))
790 wxSizeEvent
event(wxSize(x
, y
), m_frameStatusBar
->GetId());
791 event
.SetEventObject( m_frameStatusBar
);
793 ((wxStatusBar95
*)m_frameStatusBar
)->OnSize(event
);
795 #endif // wxUSE_NATIVE_STATUSBAR
800 wxSizeEvent
event(wxSize(x
, y
), m_windowId
);
801 event
.SetEventObject( this );
802 processed
= GetEventHandler()->ProcessEvent(event
);
808 bool wxFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
)
812 // In case it's e.g. a toolbar.
813 wxWindow
*win
= wxFindWinFromHandle(control
);
815 return win
->MSWCommand(cmd
, id
);
818 // handle here commands from menus and accelerators
819 if ( cmd
== 0 || cmd
== 1 )
821 if ( wxCurrentPopupMenu
)
823 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
824 wxCurrentPopupMenu
= NULL
;
826 return popupMenu
->MSWCommand(cmd
, id
);
829 if ( ProcessCommand(id
) )
838 bool wxFrame::HandleMenuSelect(WXWORD nItem
, WXWORD flags
, WXHMENU hMenu
)
841 if ( flags
== 0xFFFF && hMenu
== 0 )
843 // menu was removed from screen
846 else if ( !(flags
& MF_POPUP
) && !(flags
& MF_SEPARATOR
) )
852 // don't give hints for separators (doesn't make sense) nor for the
853 // items opening popup menus (they don't have them anyhow)
857 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, item
);
858 event
.SetEventObject( this );
860 return GetEventHandler()->ProcessEvent(event
);
863 // ---------------------------------------------------------------------------
864 // the window proc for wxFrame
865 // ---------------------------------------------------------------------------
867 long wxFrame::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
870 bool processed
= FALSE
;
875 // if we can't close, tell the system that we processed the
876 // message - otherwise it would close us
877 processed
= !Close();
884 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
887 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
895 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
897 processed
= HandleMenuSelect(item
, flags
, hmenu
);
902 processed
= HandlePaint();
905 case WM_QUERYDRAGICON
:
907 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
908 : (HICON
)(m_defaultIcon
);
915 processed
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
);
920 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);