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()
40 scoped_ptr
<views::ViewTargeter
>(new views::ViewTargeter(this)));
43 NonClientView::~NonClientView() {
44 // This value may have been reset before the window hierarchy shuts down,
45 // so we need to manually remove it.
46 RemoveChildView(frame_view_
.get());
49 void NonClientView::SetFrameView(NonClientFrameView
* frame_view
) {
50 // See comment in header about ownership.
51 frame_view
->set_owned_by_client();
52 if (frame_view_
.get())
53 RemoveChildView(frame_view_
.get());
54 frame_view_
.reset(frame_view
);
56 AddChildViewAt(frame_view_
.get(), kFrameViewIndex
);
59 void NonClientView::SetOverlayView(View
* view
) {
61 RemoveChildView(overlay_view_
);
68 AddChildView(overlay_view_
);
71 bool NonClientView::CanClose() {
72 return client_view_
->CanClose();
75 void NonClientView::WindowClosing() {
76 client_view_
->WidgetClosing();
79 void NonClientView::UpdateFrame() {
80 Widget
* widget
= GetWidget();
81 SetFrameView(widget
->CreateNonClientFrameView());
82 widget
->ThemeChanged();
87 void NonClientView::SetInactiveRenderingDisabled(bool disable
) {
88 frame_view_
->SetInactiveRenderingDisabled(disable
);
91 gfx::Rect
NonClientView::GetWindowBoundsForClientBounds(
92 const gfx::Rect client_bounds
) const {
93 return frame_view_
->GetWindowBoundsForClientBounds(client_bounds
);
96 int NonClientView::NonClientHitTest(const gfx::Point
& point
) {
97 // The NonClientFrameView is responsible for also asking the ClientView.
98 return frame_view_
->NonClientHitTest(point
);
101 void NonClientView::GetWindowMask(const gfx::Size
& size
,
102 gfx::Path
* window_mask
) {
103 frame_view_
->GetWindowMask(size
, window_mask
);
106 void NonClientView::ResetWindowControls() {
107 frame_view_
->ResetWindowControls();
110 void NonClientView::UpdateWindowIcon() {
111 frame_view_
->UpdateWindowIcon();
114 void NonClientView::UpdateWindowTitle() {
115 frame_view_
->UpdateWindowTitle();
118 void NonClientView::LayoutFrameView() {
119 // First layout the NonClientFrameView, which determines the size of the
121 frame_view_
->SetBounds(0, 0, width(), height());
123 // We need to manually call Layout here because layout for the frame view can
124 // change independently of the bounds changing - e.g. after the initial
125 // display of the window the metrics of the native window controls can change,
126 // which does not change the bounds of the window but requires a re-layout to
127 // trigger a repaint. We override OnBoundsChanged() for the NonClientFrameView
128 // to do nothing so that SetBounds above doesn't cause Layout to be called
130 frame_view_
->Layout();
133 void NonClientView::SetAccessibleName(const base::string16
& name
) {
134 accessible_name_
= name
;
137 ////////////////////////////////////////////////////////////////////////////////
138 // NonClientView, View overrides:
140 gfx::Size
NonClientView::GetPreferredSize() const {
141 // TODO(pkasting): This should probably be made to look similar to
142 // GetMinimumSize() below. This will require implementing GetPreferredSize()
143 // better in the various frame views.
144 gfx::Rect
client_bounds(gfx::Point(), client_view_
->GetPreferredSize());
145 return GetWindowBoundsForClientBounds(client_bounds
).size();
148 gfx::Size
NonClientView::GetMinimumSize() const {
149 return frame_view_
->GetMinimumSize();
152 gfx::Size
NonClientView::GetMaximumSize() const {
153 return frame_view_
->GetMaximumSize();
156 void NonClientView::Layout() {
159 // Then layout the ClientView, using those bounds.
160 client_view_
->SetBoundsRect(frame_view_
->GetBoundsForClientView());
162 // We need to manually call Layout on the ClientView as well for the same
164 client_view_
->Layout();
166 if (overlay_view_
&& overlay_view_
->visible())
167 overlay_view_
->SetBoundsRect(GetLocalBounds());
170 void NonClientView::ViewHierarchyChanged(
171 const ViewHierarchyChangedDetails
& details
) {
172 // Add our two child views here as we are added to the Widget so that if we
173 // are subsequently resized all the parent-child relationships are
175 if (details
.is_add
&& GetWidget() && details
.child
== this) {
176 AddChildViewAt(frame_view_
.get(), kFrameViewIndex
);
177 AddChildViewAt(client_view_
, kClientViewIndex
);
179 AddChildView(overlay_view_
);
183 void NonClientView::GetAccessibleState(ui::AXViewState
* state
) {
184 state
->role
= ui::AX_ROLE_CLIENT
;
185 state
->name
= accessible_name_
;
188 const char* NonClientView::GetClassName() const {
189 return kViewClassName
;
192 View
* NonClientView::GetTooltipHandlerForPoint(const gfx::Point
& point
) {
193 // The same logic as for |TargetForRect()| applies here.
194 if (frame_view_
->parent() == this) {
195 // During the reset of the frame_view_ it's possible to be in this code
196 // after it's been removed from the view hierarchy but before it's been
197 // removed from the NonClientView.
198 gfx::Point
point_in_child_coords(point
);
199 View::ConvertPointToTarget(this, frame_view_
.get(), &point_in_child_coords
);
201 frame_view_
->GetTooltipHandlerForPoint(point_in_child_coords
);
206 return View::GetTooltipHandlerForPoint(point
);
209 View
* NonClientView::TargetForRect(View
* root
, const gfx::Rect
& rect
) {
210 CHECK_EQ(root
, this);
212 if (!UsePointBasedTargeting(rect
))
213 return ViewTargeterDelegate::TargetForRect(root
, rect
);
215 // Because of the z-ordering of our child views (the client view is positioned
216 // over the non-client frame view, if the client view ever overlaps the frame
217 // view visually (as it does for the browser window), then it will eat
218 // events for the window controls. We override this method here so that we can
219 // detect this condition and re-route the events to the non-client frame view.
220 // The assumption is that the frame view's implementation of HitTest will only
221 // return true for area not occupied by the client view.
222 if (frame_view_
->parent() == this) {
223 // During the reset of the frame_view_ it's possible to be in this code
224 // after it's been removed from the view hierarchy but before it's been
225 // removed from the NonClientView.
226 gfx::RectF
rect_in_child_coords_f(rect
);
227 View::ConvertRectToTarget(this, frame_view_
.get(), &rect_in_child_coords_f
);
228 gfx::Rect rect_in_child_coords
= gfx::ToEnclosingRect(
229 rect_in_child_coords_f
);
230 if (frame_view_
->HitTestRect(rect_in_child_coords
))
231 return frame_view_
->GetEventHandlerForRect(rect_in_child_coords
);
234 return ViewTargeterDelegate::TargetForRect(root
, rect
);
237 ////////////////////////////////////////////////////////////////////////////////
238 // NonClientFrameView, public:
240 NonClientFrameView::~NonClientFrameView() {
243 void NonClientFrameView::SetInactiveRenderingDisabled(bool disable
) {
244 if (inactive_rendering_disabled_
== disable
)
247 bool should_paint_as_active_old
= ShouldPaintAsActive();
248 inactive_rendering_disabled_
= disable
;
250 // The widget schedules a paint when the activation changes.
251 if (should_paint_as_active_old
!= ShouldPaintAsActive())
255 bool NonClientFrameView::ShouldPaintAsActive() const {
256 return inactive_rendering_disabled_
|| GetWidget()->IsActive();
259 int NonClientFrameView::GetHTComponentForFrame(const gfx::Point
& point
,
260 int top_resize_border_height
,
261 int resize_border_thickness
,
262 int top_resize_corner_height
,
263 int resize_corner_width
,
265 // Tricky: In XP, native behavior is to return HTTOPLEFT and HTTOPRIGHT for
266 // a |resize_corner_size|-length strip of both the side and top borders, but
267 // only to return HTBOTTOMLEFT/HTBOTTOMRIGHT along the bottom border + corner
268 // (not the side border). Vista goes further and doesn't return these on any
269 // of the side borders. We allow callers to match either behavior.
271 if (point
.x() < resize_border_thickness
) {
272 if (point
.y() < top_resize_corner_height
)
273 component
= HTTOPLEFT
;
274 else if (point
.y() >= (height() - resize_border_thickness
))
275 component
= HTBOTTOMLEFT
;
278 } else if (point
.x() >= (width() - resize_border_thickness
)) {
279 if (point
.y() < top_resize_corner_height
)
280 component
= HTTOPRIGHT
;
281 else if (point
.y() >= (height() - resize_border_thickness
))
282 component
= HTBOTTOMRIGHT
;
285 } else if (point
.y() < top_resize_border_height
) {
286 if (point
.x() < resize_corner_width
)
287 component
= HTTOPLEFT
;
288 else if (point
.x() >= (width() - resize_corner_width
))
289 component
= HTTOPRIGHT
;
292 } else if (point
.y() >= (height() - resize_border_thickness
)) {
293 if (point
.x() < resize_corner_width
)
294 component
= HTBOTTOMLEFT
;
295 else if (point
.x() >= (width() - resize_corner_width
))
296 component
= HTBOTTOMRIGHT
;
298 component
= HTBOTTOM
;
303 // If the window can't be resized, there are no resize boundaries, just
305 return can_resize
? component
: HTBORDER
;
308 ////////////////////////////////////////////////////////////////////////////////
309 // NonClientFrameView, protected:
311 void NonClientFrameView::GetAccessibleState(ui::AXViewState
* state
) {
312 state
->role
= ui::AX_ROLE_CLIENT
;
315 const char* NonClientFrameView::GetClassName() const {
316 return kViewClassName
;
319 void NonClientFrameView::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
320 // Overridden to do nothing. The NonClientView manually calls Layout on the
321 // FrameView when it is itself laid out, see comment in NonClientView::Layout.
324 NonClientFrameView::NonClientFrameView() : inactive_rendering_disabled_(false) {
326 scoped_ptr
<views::ViewTargeter
>(new views::ViewTargeter(this)));
329 // ViewTargeterDelegate:
330 bool NonClientFrameView::DoesIntersectRect(const View
* target
,
331 const gfx::Rect
& rect
) const {
332 CHECK_EQ(target
, this);
334 // For the default case, we assume the non-client frame view never overlaps
336 return !GetWidget()->client_view()->bounds().Intersects(rect
);