• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "ui/views/window/non_client_view.h"
6 
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/widget/root_view.h"
12 #include "ui/views/widget/widget.h"
13 #include "ui/views/window/client_view.h"
14 
15 namespace views {
16 
17 // static
18 const char NonClientFrameView::kViewClassName[] =
19     "ui/views/window/NonClientFrameView";
20 
21 const char NonClientView::kViewClassName[] =
22     "ui/views/window/NonClientView";
23 
24 // The frame view and the client view are always at these specific indices,
25 // because the RootView message dispatch sends messages to items higher in the
26 // z-order first and we always want the client view to have first crack at
27 // handling mouse messages.
28 static const int kFrameViewIndex = 0;
29 static const int kClientViewIndex = 1;
30 // The overlay view is always on top (index == child_count() - 1).
31 
32 ////////////////////////////////////////////////////////////////////////////////
33 // NonClientView, public:
34 
NonClientView()35 NonClientView::NonClientView()
36     : client_view_(NULL),
37       overlay_view_(NULL) {
38 }
39 
~NonClientView()40 NonClientView::~NonClientView() {
41   // This value may have been reset before the window hierarchy shuts down,
42   // so we need to manually remove it.
43   RemoveChildView(frame_view_.get());
44 }
45 
SetFrameView(NonClientFrameView * frame_view)46 void NonClientView::SetFrameView(NonClientFrameView* frame_view) {
47   // See comment in header about ownership.
48   frame_view->set_owned_by_client();
49   if (frame_view_.get())
50     RemoveChildView(frame_view_.get());
51   frame_view_.reset(frame_view);
52   if (parent())
53     AddChildViewAt(frame_view_.get(), kFrameViewIndex);
54 }
55 
SetOverlayView(View * view)56 void NonClientView::SetOverlayView(View* view) {
57   if (overlay_view_)
58     RemoveChildView(overlay_view_);
59 
60   if (!view)
61     return;
62 
63   overlay_view_ = view;
64   if (parent())
65     AddChildView(overlay_view_);
66 }
67 
CanClose()68 bool NonClientView::CanClose() {
69   return client_view_->CanClose();
70 }
71 
WindowClosing()72 void NonClientView::WindowClosing() {
73   client_view_->WidgetClosing();
74 }
75 
UpdateFrame()76 void NonClientView::UpdateFrame() {
77   Widget* widget = GetWidget();
78   SetFrameView(widget->CreateNonClientFrameView());
79   widget->ThemeChanged();
80   Layout();
81   SchedulePaint();
82 }
83 
SetInactiveRenderingDisabled(bool disable)84 void NonClientView::SetInactiveRenderingDisabled(bool disable) {
85   frame_view_->SetInactiveRenderingDisabled(disable);
86 }
87 
GetWindowBoundsForClientBounds(const gfx::Rect client_bounds) const88 gfx::Rect NonClientView::GetWindowBoundsForClientBounds(
89     const gfx::Rect client_bounds) const {
90   return frame_view_->GetWindowBoundsForClientBounds(client_bounds);
91 }
92 
NonClientHitTest(const gfx::Point & point)93 int NonClientView::NonClientHitTest(const gfx::Point& point) {
94   // The NonClientFrameView is responsible for also asking the ClientView.
95   return frame_view_->NonClientHitTest(point);
96 }
97 
GetWindowMask(const gfx::Size & size,gfx::Path * window_mask)98 void NonClientView::GetWindowMask(const gfx::Size& size,
99                                   gfx::Path* window_mask) {
100   frame_view_->GetWindowMask(size, window_mask);
101 }
102 
ResetWindowControls()103 void NonClientView::ResetWindowControls() {
104   frame_view_->ResetWindowControls();
105 }
106 
UpdateWindowIcon()107 void NonClientView::UpdateWindowIcon() {
108   frame_view_->UpdateWindowIcon();
109 }
110 
UpdateWindowTitle()111 void NonClientView::UpdateWindowTitle() {
112   frame_view_->UpdateWindowTitle();
113 }
114 
LayoutFrameView()115 void NonClientView::LayoutFrameView() {
116   // First layout the NonClientFrameView, which determines the size of the
117   // ClientView...
118   frame_view_->SetBounds(0, 0, width(), height());
119 
120   // We need to manually call Layout here because layout for the frame view can
121   // change independently of the bounds changing - e.g. after the initial
122   // display of the window the metrics of the native window controls can change,
123   // which does not change the bounds of the window but requires a re-layout to
124   // trigger a repaint. We override OnBoundsChanged() for the NonClientFrameView
125   // to do nothing so that SetBounds above doesn't cause Layout to be called
126   // twice.
127   frame_view_->Layout();
128 }
129 
SetAccessibleName(const base::string16 & name)130 void NonClientView::SetAccessibleName(const base::string16& name) {
131   accessible_name_ = name;
132 }
133 
134 ////////////////////////////////////////////////////////////////////////////////
135 // NonClientView, View overrides:
136 
GetPreferredSize() const137 gfx::Size NonClientView::GetPreferredSize() const {
138   // TODO(pkasting): This should probably be made to look similar to
139   // GetMinimumSize() below.  This will require implementing GetPreferredSize()
140   // better in the various frame views.
141   gfx::Rect client_bounds(gfx::Point(), client_view_->GetPreferredSize());
142   return GetWindowBoundsForClientBounds(client_bounds).size();
143 }
144 
GetMinimumSize() const145 gfx::Size NonClientView::GetMinimumSize() const {
146   return frame_view_->GetMinimumSize();
147 }
148 
GetMaximumSize() const149 gfx::Size NonClientView::GetMaximumSize() const {
150   return frame_view_->GetMaximumSize();
151 }
152 
Layout()153 void NonClientView::Layout() {
154   LayoutFrameView();
155 
156   // Then layout the ClientView, using those bounds.
157   client_view_->SetBoundsRect(frame_view_->GetBoundsForClientView());
158 
159   // We need to manually call Layout on the ClientView as well for the same
160   // reason as above.
161   client_view_->Layout();
162 
163   if (overlay_view_ && overlay_view_->visible())
164     overlay_view_->SetBoundsRect(GetLocalBounds());
165 }
166 
ViewHierarchyChanged(const ViewHierarchyChangedDetails & details)167 void NonClientView::ViewHierarchyChanged(
168     const ViewHierarchyChangedDetails& details) {
169   // Add our two child views here as we are added to the Widget so that if we
170   // are subsequently resized all the parent-child relationships are
171   // established.
172   if (details.is_add && GetWidget() && details.child == this) {
173     AddChildViewAt(frame_view_.get(), kFrameViewIndex);
174     AddChildViewAt(client_view_, kClientViewIndex);
175     if (overlay_view_)
176       AddChildView(overlay_view_);
177   }
178 }
179 
GetAccessibleState(ui::AXViewState * state)180 void NonClientView::GetAccessibleState(ui::AXViewState* state) {
181   state->role = ui::AX_ROLE_CLIENT;
182   state->name = accessible_name_;
183 }
184 
GetClassName() const185 const char* NonClientView::GetClassName() const {
186   return kViewClassName;
187 }
188 
GetEventHandlerForRect(const gfx::Rect & rect)189 View* NonClientView::GetEventHandlerForRect(const gfx::Rect& rect) {
190   if (!UsePointBasedTargeting(rect))
191     return View::GetEventHandlerForRect(rect);
192 
193   // Because of the z-ordering of our child views (the client view is positioned
194   // over the non-client frame view, if the client view ever overlaps the frame
195   // view visually (as it does for the browser window), then it will eat
196   // events for the window controls. We override this method here so that we can
197   // detect this condition and re-route the events to the non-client frame view.
198   // The assumption is that the frame view's implementation of HitTest will only
199   // return true for area not occupied by the client view.
200   if (frame_view_->parent() == this) {
201     // During the reset of the frame_view_ it's possible to be in this code
202     // after it's been removed from the view hierarchy but before it's been
203     // removed from the NonClientView.
204     gfx::RectF rect_in_child_coords_f(rect);
205     View::ConvertRectToTarget(this, frame_view_.get(), &rect_in_child_coords_f);
206     gfx::Rect rect_in_child_coords = gfx::ToEnclosingRect(
207         rect_in_child_coords_f);
208     if (frame_view_->HitTestRect(rect_in_child_coords))
209       return frame_view_->GetEventHandlerForRect(rect_in_child_coords);
210   }
211 
212   return View::GetEventHandlerForRect(rect);
213 }
214 
GetTooltipHandlerForPoint(const gfx::Point & point)215 View* NonClientView::GetTooltipHandlerForPoint(const gfx::Point& point) {
216   // The same logic as for |GetEventHandlerForRect()| applies here.
217   if (frame_view_->parent() == this) {
218     // During the reset of the frame_view_ it's possible to be in this code
219     // after it's been removed from the view hierarchy but before it's been
220     // removed from the NonClientView.
221     gfx::Point point_in_child_coords(point);
222     View::ConvertPointToTarget(this, frame_view_.get(), &point_in_child_coords);
223     View* handler =
224         frame_view_->GetTooltipHandlerForPoint(point_in_child_coords);
225     if (handler)
226       return handler;
227   }
228 
229   return View::GetTooltipHandlerForPoint(point);
230 }
231 
232 ////////////////////////////////////////////////////////////////////////////////
233 // NonClientFrameView, public:
234 
~NonClientFrameView()235 NonClientFrameView::~NonClientFrameView() {
236 }
237 
SetInactiveRenderingDisabled(bool disable)238 void NonClientFrameView::SetInactiveRenderingDisabled(bool disable) {
239   if (inactive_rendering_disabled_ == disable)
240     return;
241 
242   bool should_paint_as_active_old = ShouldPaintAsActive();
243   inactive_rendering_disabled_ = disable;
244 
245   // The widget schedules a paint when the activation changes.
246   if (should_paint_as_active_old != ShouldPaintAsActive())
247     SchedulePaint();
248 }
249 
ShouldPaintAsActive() const250 bool NonClientFrameView::ShouldPaintAsActive() const {
251   return inactive_rendering_disabled_ || GetWidget()->IsActive();
252 }
253 
GetHTComponentForFrame(const gfx::Point & point,int top_resize_border_height,int resize_border_thickness,int top_resize_corner_height,int resize_corner_width,bool can_resize)254 int NonClientFrameView::GetHTComponentForFrame(const gfx::Point& point,
255                                                int top_resize_border_height,
256                                                int resize_border_thickness,
257                                                int top_resize_corner_height,
258                                                int resize_corner_width,
259                                                bool can_resize) {
260   // Tricky: In XP, native behavior is to return HTTOPLEFT and HTTOPRIGHT for
261   // a |resize_corner_size|-length strip of both the side and top borders, but
262   // only to return HTBOTTOMLEFT/HTBOTTOMRIGHT along the bottom border + corner
263   // (not the side border).  Vista goes further and doesn't return these on any
264   // of the side borders.  We allow callers to match either behavior.
265   int component;
266   if (point.x() < resize_border_thickness) {
267     if (point.y() < top_resize_corner_height)
268       component = HTTOPLEFT;
269     else if (point.y() >= (height() - resize_border_thickness))
270       component = HTBOTTOMLEFT;
271     else
272       component = HTLEFT;
273   } else if (point.x() >= (width() - resize_border_thickness)) {
274     if (point.y() < top_resize_corner_height)
275       component = HTTOPRIGHT;
276     else if (point.y() >= (height() - resize_border_thickness))
277       component = HTBOTTOMRIGHT;
278     else
279       component = HTRIGHT;
280   } else if (point.y() < top_resize_border_height) {
281     if (point.x() < resize_corner_width)
282       component = HTTOPLEFT;
283     else if (point.x() >= (width() - resize_corner_width))
284       component = HTTOPRIGHT;
285     else
286       component = HTTOP;
287   } else if (point.y() >= (height() - resize_border_thickness)) {
288     if (point.x() < resize_corner_width)
289       component = HTBOTTOMLEFT;
290     else if (point.x() >= (width() - resize_corner_width))
291       component = HTBOTTOMRIGHT;
292     else
293       component = HTBOTTOM;
294   } else {
295     return HTNOWHERE;
296   }
297 
298   // If the window can't be resized, there are no resize boundaries, just
299   // window borders.
300   return can_resize ? component : HTBORDER;
301 }
302 
303 ////////////////////////////////////////////////////////////////////////////////
304 // NonClientFrameView, View overrides:
305 
HitTestRect(const gfx::Rect & rect) const306 bool NonClientFrameView::HitTestRect(const gfx::Rect& rect) const {
307   // For the default case, we assume the non-client frame view never overlaps
308   // the client view.
309   return !GetWidget()->client_view()->bounds().Intersects(rect);
310 }
311 
312 ////////////////////////////////////////////////////////////////////////////////
313 // NonClientFrameView, protected:
314 
GetAccessibleState(ui::AXViewState * state)315 void NonClientFrameView::GetAccessibleState(ui::AXViewState* state) {
316   state->role = ui::AX_ROLE_CLIENT;
317 }
318 
GetClassName() const319 const char* NonClientFrameView::GetClassName() const {
320   return kViewClassName;
321 }
322 
OnBoundsChanged(const gfx::Rect & previous_bounds)323 void NonClientFrameView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
324   // Overridden to do nothing. The NonClientView manually calls Layout on the
325   // FrameView when it is itself laid out, see comment in NonClientView::Layout.
326 }
327 
NonClientFrameView()328 NonClientFrameView::NonClientFrameView() : inactive_rendering_disabled_(false) {
329 }
330 
331 }  // namespace views
332