1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/window/non_client_view.h"
7 #include "ui/accessibility/ax_view_state.h"
8 #include "ui/base/hit_test.h"
9 #include "ui/gfx/rect_conversions.h"
10 #include "ui/views/rect_based_targeting_utils.h"
11 #include "ui/views/view_targeter.h"
12 #include "ui/views/widget/root_view.h"
13 #include "ui/views/widget/widget.h"
14 #include "ui/views/window/client_view.h"
19 const char NonClientFrameView::kViewClassName
[] =
20 "ui/views/window/NonClientFrameView";
22 const char NonClientView::kViewClassName
[] =
23 "ui/views/window/NonClientView";
25 // The frame view and the client view are always at these specific indices,
26 // because the RootView message dispatch sends messages to items higher in the
27 // z-order first and we always want the client view to have first crack at
28 // handling mouse messages.
29 static const int kFrameViewIndex
= 0;
30 static const int kClientViewIndex
= 1;
31 // The overlay view is always on top (index == child_count() - 1).
33 ////////////////////////////////////////////////////////////////////////////////
34 // NonClientView, public:
36 NonClientView::NonClientView()
41 NonClientView::~NonClientView() {
42 // This value may have been reset before the window hierarchy shuts down,
43 // so we need to manually remove it.
44 RemoveChildView(frame_view_
.get());
47 void NonClientView::SetFrameView(NonClientFrameView
* frame_view
) {
48 // See comment in header about ownership.
49 frame_view
->set_owned_by_client();
50 if (frame_view_
.get())
51 RemoveChildView(frame_view_
.get());
52 frame_view_
.reset(frame_view
);
54 AddChildViewAt(frame_view_
.get(), kFrameViewIndex
);
57 void NonClientView::SetOverlayView(View
* view
) {
59 RemoveChildView(overlay_view_
);
66 AddChildView(overlay_view_
);
69 bool NonClientView::CanClose() {
70 return client_view_
->CanClose();
73 void NonClientView::WindowClosing() {
74 client_view_
->WidgetClosing();
77 void NonClientView::UpdateFrame() {
78 Widget
* widget
= GetWidget();
79 SetFrameView(widget
->CreateNonClientFrameView());
80 widget
->ThemeChanged();
85 void NonClientView::SetInactiveRenderingDisabled(bool disable
) {
86 frame_view_
->SetInactiveRenderingDisabled(disable
);
89 gfx::Rect
NonClientView::GetWindowBoundsForClientBounds(
90 const gfx::Rect client_bounds
) const {
91 return frame_view_
->GetWindowBoundsForClientBounds(client_bounds
);
94 int NonClientView::NonClientHitTest(const gfx::Point
& point
) {
95 // The NonClientFrameView is responsible for also asking the ClientView.
96 return frame_view_
->NonClientHitTest(point
);
99 void NonClientView::GetWindowMask(const gfx::Size
& size
,
100 gfx::Path
* window_mask
) {
101 frame_view_
->GetWindowMask(size
, window_mask
);
104 void NonClientView::ResetWindowControls() {
105 frame_view_
->ResetWindowControls();
108 void NonClientView::UpdateWindowIcon() {
109 frame_view_
->UpdateWindowIcon();
112 void NonClientView::UpdateWindowTitle() {
113 frame_view_
->UpdateWindowTitle();
116 void NonClientView::LayoutFrameView() {
117 // First layout the NonClientFrameView, which determines the size of the
119 frame_view_
->SetBounds(0, 0, width(), height());
121 // We need to manually call Layout here because layout for the frame view can
122 // change independently of the bounds changing - e.g. after the initial
123 // display of the window the metrics of the native window controls can change,
124 // which does not change the bounds of the window but requires a re-layout to
125 // trigger a repaint. We override OnBoundsChanged() for the NonClientFrameView
126 // to do nothing so that SetBounds above doesn't cause Layout to be called
128 frame_view_
->Layout();
131 void NonClientView::SetAccessibleName(const base::string16
& name
) {
132 accessible_name_
= name
;
135 ////////////////////////////////////////////////////////////////////////////////
136 // NonClientView, View overrides:
138 gfx::Size
NonClientView::GetPreferredSize() const {
139 // TODO(pkasting): This should probably be made to look similar to
140 // GetMinimumSize() below. This will require implementing GetPreferredSize()
141 // better in the various frame views.
142 gfx::Rect
client_bounds(gfx::Point(), client_view_
->GetPreferredSize());
143 return GetWindowBoundsForClientBounds(client_bounds
).size();
146 gfx::Size
NonClientView::GetMinimumSize() const {
147 return frame_view_
->GetMinimumSize();
150 gfx::Size
NonClientView::GetMaximumSize() const {
151 return frame_view_
->GetMaximumSize();
154 void NonClientView::Layout() {
157 // Then layout the ClientView, using those bounds.
158 client_view_
->SetBoundsRect(frame_view_
->GetBoundsForClientView());
160 // We need to manually call Layout on the ClientView as well for the same
162 client_view_
->Layout();
164 if (overlay_view_
&& overlay_view_
->visible())
165 overlay_view_
->SetBoundsRect(GetLocalBounds());
168 void NonClientView::ViewHierarchyChanged(
169 const ViewHierarchyChangedDetails
& details
) {
170 // Add our two child views here as we are added to the Widget so that if we
171 // are subsequently resized all the parent-child relationships are
173 if (details
.is_add
&& GetWidget() && details
.child
== this) {
174 AddChildViewAt(frame_view_
.get(), kFrameViewIndex
);
175 AddChildViewAt(client_view_
, kClientViewIndex
);
177 AddChildView(overlay_view_
);
181 void NonClientView::GetAccessibleState(ui::AXViewState
* state
) {
182 state
->role
= ui::AX_ROLE_CLIENT
;
183 state
->name
= accessible_name_
;
186 const char* NonClientView::GetClassName() const {
187 return kViewClassName
;
190 View
* NonClientView::GetEventHandlerForRect(const gfx::Rect
& rect
) {
191 if (!UsePointBasedTargeting(rect
))
192 return View::GetEventHandlerForRect(rect
);
194 // Because of the z-ordering of our child views (the client view is positioned
195 // over the non-client frame view, if the client view ever overlaps the frame
196 // view visually (as it does for the browser window), then it will eat
197 // events for the window controls. We override this method here so that we can
198 // detect this condition and re-route the events to the non-client frame view.
199 // The assumption is that the frame view's implementation of HitTest will only
200 // return true for area not occupied by the client view.
201 if (frame_view_
->parent() == this) {
202 // During the reset of the frame_view_ it's possible to be in this code
203 // after it's been removed from the view hierarchy but before it's been
204 // removed from the NonClientView.
205 gfx::RectF
rect_in_child_coords_f(rect
);
206 View::ConvertRectToTarget(this, frame_view_
.get(), &rect_in_child_coords_f
);
207 gfx::Rect rect_in_child_coords
= gfx::ToEnclosingRect(
208 rect_in_child_coords_f
);
209 if (frame_view_
->HitTestRect(rect_in_child_coords
))
210 return frame_view_
->GetEventHandlerForRect(rect_in_child_coords
);
213 return View::GetEventHandlerForRect(rect
);
216 View
* NonClientView::GetTooltipHandlerForPoint(const gfx::Point
& point
) {
217 // The same logic as for |GetEventHandlerForRect()| applies here.
218 if (frame_view_
->parent() == this) {
219 // During the reset of the frame_view_ it's possible to be in this code
220 // after it's been removed from the view hierarchy but before it's been
221 // removed from the NonClientView.
222 gfx::Point
point_in_child_coords(point
);
223 View::ConvertPointToTarget(this, frame_view_
.get(), &point_in_child_coords
);
225 frame_view_
->GetTooltipHandlerForPoint(point_in_child_coords
);
230 return View::GetTooltipHandlerForPoint(point
);
233 ////////////////////////////////////////////////////////////////////////////////
234 // NonClientFrameView, public:
236 NonClientFrameView::~NonClientFrameView() {
239 void NonClientFrameView::SetInactiveRenderingDisabled(bool disable
) {
240 if (inactive_rendering_disabled_
== disable
)
243 bool should_paint_as_active_old
= ShouldPaintAsActive();
244 inactive_rendering_disabled_
= disable
;
246 // The widget schedules a paint when the activation changes.
247 if (should_paint_as_active_old
!= ShouldPaintAsActive())
251 bool NonClientFrameView::ShouldPaintAsActive() const {
252 return inactive_rendering_disabled_
|| GetWidget()->IsActive();
255 int NonClientFrameView::GetHTComponentForFrame(const gfx::Point
& point
,
256 int top_resize_border_height
,
257 int resize_border_thickness
,
258 int top_resize_corner_height
,
259 int resize_corner_width
,
261 // Tricky: In XP, native behavior is to return HTTOPLEFT and HTTOPRIGHT for
262 // a |resize_corner_size|-length strip of both the side and top borders, but
263 // only to return HTBOTTOMLEFT/HTBOTTOMRIGHT along the bottom border + corner
264 // (not the side border). Vista goes further and doesn't return these on any
265 // of the side borders. We allow callers to match either behavior.
267 if (point
.x() < resize_border_thickness
) {
268 if (point
.y() < top_resize_corner_height
)
269 component
= HTTOPLEFT
;
270 else if (point
.y() >= (height() - resize_border_thickness
))
271 component
= HTBOTTOMLEFT
;
274 } else if (point
.x() >= (width() - resize_border_thickness
)) {
275 if (point
.y() < top_resize_corner_height
)
276 component
= HTTOPRIGHT
;
277 else if (point
.y() >= (height() - resize_border_thickness
))
278 component
= HTBOTTOMRIGHT
;
281 } else if (point
.y() < top_resize_border_height
) {
282 if (point
.x() < resize_corner_width
)
283 component
= HTTOPLEFT
;
284 else if (point
.x() >= (width() - resize_corner_width
))
285 component
= HTTOPRIGHT
;
288 } else if (point
.y() >= (height() - resize_border_thickness
)) {
289 if (point
.x() < resize_corner_width
)
290 component
= HTBOTTOMLEFT
;
291 else if (point
.x() >= (width() - resize_corner_width
))
292 component
= HTBOTTOMRIGHT
;
294 component
= HTBOTTOM
;
299 // If the window can't be resized, there are no resize boundaries, just
301 return can_resize
? component
: HTBORDER
;
304 ////////////////////////////////////////////////////////////////////////////////
305 // NonClientFrameView, protected:
307 void NonClientFrameView::GetAccessibleState(ui::AXViewState
* state
) {
308 state
->role
= ui::AX_ROLE_CLIENT
;
311 const char* NonClientFrameView::GetClassName() const {
312 return kViewClassName
;
315 void NonClientFrameView::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
316 // Overridden to do nothing. The NonClientView manually calls Layout on the
317 // FrameView when it is itself laid out, see comment in NonClientView::Layout.
320 NonClientFrameView::NonClientFrameView() : inactive_rendering_disabled_(false) {
322 scoped_ptr
<views::ViewTargeter
>(new views::ViewTargeter(this)));
325 // ViewTargeterDelegate:
326 bool NonClientFrameView::DoesIntersectRect(const View
* target
,
327 const gfx::Rect
& rect
) const {
328 CHECK_EQ(target
, this);
330 // For the default case, we assume the non-client frame view never overlaps
332 return !GetWidget()->client_view()->bounds().Intersects(rect
);