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 #ifndef UI_VIEWS_VIEW_H_
6 #define UI_VIEWS_VIEW_H_
14 #include "base/compiler_specific.h"
15 #include "base/i18n/rtl.h"
16 #include "base/logging.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "build/build_config.h"
19 #include "ui/accessibility/ax_enums.h"
20 #include "ui/base/accelerators/accelerator.h"
21 #include "ui/base/dragdrop/drag_drop_types.h"
22 #include "ui/base/dragdrop/drop_target_event.h"
23 #include "ui/base/dragdrop/os_exchange_data.h"
24 #include "ui/base/ui_base_types.h"
25 #include "ui/compositor/layer_delegate.h"
26 #include "ui/compositor/layer_owner.h"
27 #include "ui/events/event.h"
28 #include "ui/events/event_target.h"
29 #include "ui/gfx/geometry/insets.h"
30 #include "ui/gfx/geometry/rect.h"
31 #include "ui/gfx/geometry/vector2d.h"
32 #include "ui/gfx/native_widget_types.h"
33 #include "ui/views/view_targeter.h"
34 #include "ui/views/views_export.h"
37 #include "base/win/scoped_comptr.h"
40 using ui::OSExchangeData
;
55 class TextInputClient
;
64 class ContextMenuController
;
67 class FocusTraversable
;
70 class NativeViewAccessibility
;
75 class PreEventDispatchHandler
;
76 class PostEventDispatchHandler
;
80 /////////////////////////////////////////////////////////////////////////////
84 // A View is a rectangle within the views View hierarchy. It is the base
85 // class for all Views.
87 // A View is a container of other Views (there is no such thing as a Leaf
88 // View - makes code simpler, reduces type conversion headaches, design
91 // The View contains basic properties for sizing (bounds), layout (flex,
92 // orientation, etc), painting of children and event dispatch.
94 // The View also uses a simple Box Layout Manager similar to XUL's
95 // SprocketLayout system. Alternative Layout Managers implementing the
96 // LayoutManager interface can be used to lay out children if required.
98 // It is up to the subclass to implement Painting and storage of subclass -
99 // specific properties and functionality.
101 // Unless otherwise documented, views is not thread safe and should only be
102 // accessed from the main thread.
104 /////////////////////////////////////////////////////////////////////////////
105 class VIEWS_EXPORT View
: public ui::LayerDelegate
,
106 public ui::LayerOwner
,
107 public ui::AcceleratorTarget
,
108 public ui::EventTarget
{
110 typedef std::vector
<View
*> Views
;
112 struct ViewHierarchyChangedDetails
{
113 ViewHierarchyChangedDetails()
119 ViewHierarchyChangedDetails(bool is_add
,
126 move_view(move_view
) {}
129 // New parent if |is_add| is true, old parent if |is_add| is false.
131 // The view being added or removed.
133 // If this is a move (reparent), meaning AddChildViewAt() is invoked with an
134 // existing parent, then a notification for the remove is sent first,
135 // followed by one for the add. This case can be distinguished by a
136 // non-NULL |move_view|.
137 // For the remove part of move, |move_view| is the new parent of the View
139 // For the add part of move, |move_view| is the old parent of the View being
144 // Creation and lifetime -----------------------------------------------------
149 // By default a View is owned by its parent unless specified otherwise here.
150 void set_owned_by_client() { owned_by_client_
= true; }
152 // Tree operations -----------------------------------------------------------
154 // Get the Widget that hosts this View, if any.
155 virtual const Widget
* GetWidget() const;
156 virtual Widget
* GetWidget();
158 // Adds |view| as a child of this view, optionally at |index|.
159 void AddChildView(View
* view
);
160 void AddChildViewAt(View
* view
, int index
);
162 // Moves |view| to the specified |index|. A negative value for |index| moves
163 // the view at the end.
164 void ReorderChildView(View
* view
, int index
);
166 // Removes |view| from this view. The view's parent will change to NULL.
167 void RemoveChildView(View
* view
);
169 // Removes all the children from this view. If |delete_children| is true,
170 // the views are deleted, unless marked as not parent owned.
171 void RemoveAllChildViews(bool delete_children
);
173 int child_count() const { return static_cast<int>(children_
.size()); }
174 bool has_children() const { return !children_
.empty(); }
176 // Returns the child view at |index|.
177 const View
* child_at(int index
) const {
179 DCHECK_LT(index
, child_count());
180 return children_
[index
];
182 View
* child_at(int index
) {
183 return const_cast<View
*>(const_cast<const View
*>(this)->child_at(index
));
186 // Returns the parent view.
187 const View
* parent() const { return parent_
; }
188 View
* parent() { return parent_
; }
190 // Returns true if |view| is contained within this View's hierarchy, even as
191 // an indirect descendant. Will return true if child is also this view.
192 bool Contains(const View
* view
) const;
194 // Returns the index of |view|, or -1 if |view| is not a child of this view.
195 int GetIndexOf(const View
* view
) const;
197 // Size and disposition ------------------------------------------------------
198 // Methods for obtaining and modifying the position and size of the view.
199 // Position is in the coordinate system of the view's parent.
200 // Position is NOT flipped for RTL. See "RTL positioning" for RTL-sensitive
201 // position accessors.
202 // Transformations are not applied on the size/position. For example, if
203 // bounds is (0, 0, 100, 100) and it is scaled by 0.5 along the X axis, the
204 // width will still be 100 (although when painted, it will be 50x50, painted
205 // at location (0, 0)).
207 void SetBounds(int x
, int y
, int width
, int height
);
208 void SetBoundsRect(const gfx::Rect
& bounds
);
209 void SetSize(const gfx::Size
& size
);
210 void SetPosition(const gfx::Point
& position
);
214 // No transformation is applied on the size or the locations.
215 const gfx::Rect
& bounds() const { return bounds_
; }
216 int x() const { return bounds_
.x(); }
217 int y() const { return bounds_
.y(); }
218 int width() const { return bounds_
.width(); }
219 int height() const { return bounds_
.height(); }
220 const gfx::Size
& size() const { return bounds_
.size(); }
222 // Returns the bounds of the content area of the view, i.e. the rectangle
223 // enclosed by the view's border.
224 gfx::Rect
GetContentsBounds() const;
226 // Returns the bounds of the view in its own coordinates (i.e. position is
228 gfx::Rect
GetLocalBounds() const;
230 // Returns the bounds of the layer in its own pixel coordinates.
231 gfx::Rect
GetLayerBoundsInPixel() const;
233 // Returns the insets of the current border. If there is no border an empty
234 // insets is returned.
235 virtual gfx::Insets
GetInsets() const;
237 // Returns the visible bounds of the receiver in the receivers coordinate
240 // When traversing the View hierarchy in order to compute the bounds, the
241 // function takes into account the mirroring setting and transformation for
242 // each View and therefore it will return the mirrored and transformed version
243 // of the visible bounds if need be.
244 gfx::Rect
GetVisibleBounds() const;
246 // Return the bounds of the View in screen coordinate system.
247 gfx::Rect
GetBoundsInScreen() const;
249 // Returns the baseline of this view, or -1 if this view has no baseline. The
250 // return value is relative to the preferred height.
251 virtual int GetBaseline() const;
253 // Get the size the View would like to be, if enough space were available.
254 virtual gfx::Size
GetPreferredSize() const;
256 // Convenience method that sizes this view to its preferred size.
257 void SizeToPreferredSize();
259 // Gets the minimum size of the view. View's implementation invokes
261 virtual gfx::Size
GetMinimumSize() const;
263 // Gets the maximum size of the view. Currently only used for sizing shell
265 virtual gfx::Size
GetMaximumSize() const;
267 // Return the height necessary to display this view with the provided width.
268 // View's implementation returns the value from getPreferredSize.cy.
269 // Override if your View's preferred height depends upon the width (such
271 virtual int GetHeightForWidth(int w
) const;
273 // Sets whether this view is visible. Painting is scheduled as needed. Also,
274 // clears focus if the focused view or one of its ancestors is set to be
276 virtual void SetVisible(bool visible
);
278 // Return whether a view is visible
279 bool visible() const { return visible_
; }
281 // Returns true if this view is drawn on screen.
282 virtual bool IsDrawn() const;
284 // Set whether this view is enabled. A disabled view does not receive keyboard
285 // or mouse inputs. If |enabled| differs from the current value, SchedulePaint
286 // is invoked. Also, clears focus if the focused view is disabled.
287 void SetEnabled(bool enabled
);
289 // Returns whether the view is enabled.
290 bool enabled() const { return enabled_
; }
292 // This indicates that the view completely fills its bounds in an opaque
293 // color. This doesn't affect compositing but is a hint to the compositor to
294 // optimize painting.
295 // Note that this method does not implicitly create a layer if one does not
296 // already exist for the View, but is a no-op in that case.
297 void SetFillsBoundsOpaquely(bool fills_bounds_opaquely
);
299 // Transformations -----------------------------------------------------------
301 // Methods for setting transformations for a view (e.g. rotation, scaling).
303 gfx::Transform
GetTransform() const;
305 // Clipping parameters. Clipping is done relative to the view bounds.
306 void set_clip_insets(gfx::Insets clip_insets
) { clip_insets_
= clip_insets
; }
308 // Sets the transform to the supplied transform.
309 void SetTransform(const gfx::Transform
& transform
);
311 // Sets whether this view paints to a layer. A view paints to a layer if
312 // either of the following are true:
313 // . the view has a non-identity transform.
314 // . SetPaintToLayer(true) has been invoked.
315 // View creates the Layer only when it exists in a Widget with a non-NULL
317 void SetPaintToLayer(bool paint_to_layer
);
319 // RTL positioning -----------------------------------------------------------
321 // Methods for accessing the bounds and position of the view, relative to its
322 // parent. The position returned is mirrored if the parent view is using a RTL
325 // NOTE: in the vast majority of the cases, the mirroring implementation is
326 // transparent to the View subclasses and therefore you should use the
327 // bounds() accessor instead.
328 gfx::Rect
GetMirroredBounds() const;
329 gfx::Point
GetMirroredPosition() const;
330 int GetMirroredX() const;
332 // Given a rectangle specified in this View's coordinate system, the function
333 // computes the 'left' value for the mirrored rectangle within this View. If
334 // the View's UI layout is not right-to-left, then bounds.x() is returned.
336 // UI mirroring is transparent to most View subclasses and therefore there is
337 // no need to call this routine from anywhere within your subclass
339 int GetMirroredXForRect(const gfx::Rect
& rect
) const;
341 // Given the X coordinate of a point inside the View, this function returns
342 // the mirrored X coordinate of the point if the View's UI layout is
343 // right-to-left. If the layout is left-to-right, the same X coordinate is
346 // Following are a few examples of the values returned by this function for
347 // a View with the bounds {0, 0, 100, 100} and a right-to-left layout:
349 // GetMirroredXCoordinateInView(0) -> 100
350 // GetMirroredXCoordinateInView(20) -> 80
351 // GetMirroredXCoordinateInView(99) -> 1
352 int GetMirroredXInView(int x
) const;
354 // Given a X coordinate and a width inside the View, this function returns
355 // the mirrored X coordinate if the View's UI layout is right-to-left. If the
356 // layout is left-to-right, the same X coordinate is returned.
358 // Following are a few examples of the values returned by this function for
359 // a View with the bounds {0, 0, 100, 100} and a right-to-left layout:
361 // GetMirroredXCoordinateInView(0, 10) -> 90
362 // GetMirroredXCoordinateInView(20, 20) -> 60
363 int GetMirroredXWithWidthInView(int x
, int w
) const;
365 // Layout --------------------------------------------------------------------
367 // Lay out the child Views (set their bounds based on sizing heuristics
368 // specific to the current Layout Manager)
369 virtual void Layout();
371 // TODO(beng): I think we should remove this.
372 // Mark this view and all parents to require a relayout. This ensures the
373 // next call to Layout() will propagate to this view, even if the bounds of
374 // parent views do not change.
375 void InvalidateLayout();
377 // Gets/Sets the Layout Manager used by this view to size and place its
379 // The LayoutManager is owned by the View and is deleted when the view is
380 // deleted, or when a new LayoutManager is installed.
381 LayoutManager
* GetLayoutManager() const;
382 void SetLayoutManager(LayoutManager
* layout
);
384 // Adjust the layer's offset so that it snaps to the physical pixel boundary.
385 // This has no effect if the view does not have an associated layer.
386 void SnapLayerToPixelBoundary();
388 // Attributes ----------------------------------------------------------------
390 // The view class name.
391 static const char kViewClassName
[];
393 // Return the receiving view's class name. A view class is a string which
394 // uniquely identifies the view class. It is intended to be used as a way to
395 // find out during run time if a view can be safely casted to a specific view
396 // subclass. The default implementation returns kViewClassName.
397 virtual const char* GetClassName() const;
399 // Returns the first ancestor, starting at this, whose class name is |name|.
400 // Returns null if no ancestor has the class name |name|.
401 const View
* GetAncestorWithClassName(const std::string
& name
) const;
402 View
* GetAncestorWithClassName(const std::string
& name
);
404 // Recursively descends the view tree starting at this view, and returns
405 // the first child that it encounters that has the given ID.
406 // Returns NULL if no matching child view is found.
407 virtual const View
* GetViewByID(int id
) const;
408 virtual View
* GetViewByID(int id
);
410 // Gets and sets the ID for this view. ID should be unique within the subtree
411 // that you intend to search for it. 0 is the default ID for views.
412 int id() const { return id_
; }
413 void set_id(int id
) { id_
= id
; }
415 // A group id is used to tag views which are part of the same logical group.
416 // Focus can be moved between views with the same group using the arrow keys.
417 // Groups are currently used to implement radio button mutual exclusion.
418 // The group id is immutable once it's set.
419 void SetGroup(int gid
);
420 // Returns the group id of the view, or -1 if the id is not set yet.
421 int GetGroup() const;
423 // If this returns true, the views from the same group can each be focused
424 // when moving focus with the Tab/Shift-Tab key. If this returns false,
425 // only the selected view from the group (obtained with
426 // GetSelectedViewForGroup()) is focused.
427 virtual bool IsGroupFocusTraversable() const;
429 // Fills |views| with all the available views which belong to the provided
431 void GetViewsInGroup(int group
, Views
* views
);
433 // Returns the View that is currently selected in |group|.
434 // The default implementation simply returns the first View found for that
436 virtual View
* GetSelectedViewForGroup(int group
);
438 // Coordinate conversion -----------------------------------------------------
440 // Note that the utility coordinate conversions functions always operate on
441 // the mirrored position of the child Views if the parent View uses a
442 // right-to-left UI layout.
444 // Convert a point from the coordinate system of one View to another.
446 // |source| and |target| must be in the same widget, but doesn't need to be in
447 // the same view hierarchy.
448 // Neither |source| nor |target| can be NULL.
449 static void ConvertPointToTarget(const View
* source
,
453 // Convert |rect| from the coordinate system of |source| to the coordinate
454 // system of |target|.
456 // |source| and |target| must be in the same widget, but doesn't need to be in
457 // the same view hierarchy.
458 // Neither |source| nor |target| can be NULL.
459 static void ConvertRectToTarget(const View
* source
,
463 // Convert a point from a View's coordinate system to that of its Widget.
464 static void ConvertPointToWidget(const View
* src
, gfx::Point
* point
);
466 // Convert a point from the coordinate system of a View's Widget to that
467 // View's coordinate system.
468 static void ConvertPointFromWidget(const View
* dest
, gfx::Point
* p
);
470 // Convert a point from a View's coordinate system to that of the screen.
471 static void ConvertPointToScreen(const View
* src
, gfx::Point
* point
);
473 // Convert a point from a View's coordinate system to that of the screen.
474 static void ConvertPointFromScreen(const View
* dst
, gfx::Point
* point
);
476 // Applies transformation on the rectangle, which is in the view's coordinate
477 // system, to convert it into the parent's coordinate system.
478 gfx::Rect
ConvertRectToParent(const gfx::Rect
& rect
) const;
480 // Converts a rectangle from this views coordinate system to its widget
481 // coordinate system.
482 gfx::Rect
ConvertRectToWidget(const gfx::Rect
& rect
) const;
484 // Painting ------------------------------------------------------------------
486 // Mark all or part of the View's bounds as dirty (needing repaint).
487 // |r| is in the View's coordinates.
488 // Rectangle |r| should be in the view's coordinate system. The
489 // transformations are applied to it to convert it into the parent coordinate
490 // system before propagating SchedulePaint up the view hierarchy.
491 // TODO(beng): Make protected.
492 virtual void SchedulePaint();
493 virtual void SchedulePaintInRect(const gfx::Rect
& r
);
495 // Called by the framework to paint a View. Performs translation and clipping
496 // for View coordinates and language direction as required, allows the View
497 // to paint itself via the various OnPaint*() event handlers and then paints
498 // the hierarchy beneath it.
499 void Paint(const ui::PaintContext
& parent_context
);
501 // The background object is owned by this object and may be NULL.
502 void set_background(Background
* b
);
503 const Background
* background() const { return background_
.get(); }
504 Background
* background() { return background_
.get(); }
506 // The border object is owned by this object and may be NULL.
507 virtual void SetBorder(scoped_ptr
<Border
> b
);
508 const Border
* border() const { return border_
.get(); }
509 Border
* border() { return border_
.get(); }
511 // Get the theme provider from the parent widget.
512 ui::ThemeProvider
* GetThemeProvider() const;
514 // Returns the NativeTheme to use for this View. This calls through to
515 // GetNativeTheme() on the Widget this View is in. If this View is not in a
516 // Widget this returns ui::NativeTheme::instance().
517 ui::NativeTheme
* GetNativeTheme() {
518 return const_cast<ui::NativeTheme
*>(
519 const_cast<const View
*>(this)->GetNativeTheme());
521 const ui::NativeTheme
* GetNativeTheme() const;
523 // RTL painting --------------------------------------------------------------
525 // This method determines whether the gfx::Canvas object passed to
526 // View::Paint() needs to be transformed such that anything drawn on the
527 // canvas object during View::Paint() is flipped horizontally.
529 // By default, this function returns false (which is the initial value of
530 // |flip_canvas_on_paint_for_rtl_ui_|). View subclasses that need to paint on
531 // a flipped gfx::Canvas when the UI layout is right-to-left need to call
532 // EnableCanvasFlippingForRTLUI().
533 bool FlipCanvasOnPaintForRTLUI() const {
534 return flip_canvas_on_paint_for_rtl_ui_
? base::i18n::IsRTL() : false;
537 // Enables or disables flipping of the gfx::Canvas during View::Paint().
538 // Note that if canvas flipping is enabled, the canvas will be flipped only
539 // if the UI layout is right-to-left; that is, the canvas will be flipped
540 // only if base::i18n::IsRTL() returns true.
542 // Enabling canvas flipping is useful for leaf views that draw an image that
543 // needs to be flipped horizontally when the UI layout is right-to-left
544 // (views::Button, for example). This method is helpful for such classes
545 // because their drawing logic stays the same and they can become agnostic to
546 // the UI directionality.
547 void EnableCanvasFlippingForRTLUI(bool enable
) {
548 flip_canvas_on_paint_for_rtl_ui_
= enable
;
551 // Input ---------------------------------------------------------------------
552 // The points, rects, mouse locations, and touch locations in the following
553 // functions are in the view's coordinates, except for a RootView.
555 // A convenience function which calls into GetEventHandlerForRect() with
556 // a 1x1 rect centered at |point|. |point| is in the local coordinate
558 View
* GetEventHandlerForPoint(const gfx::Point
& point
);
560 // Returns the View that should be the target of an event having |rect| as
561 // its location, or NULL if no such target exists. |rect| is in the local
562 // coordinate space of |this|.
563 View
* GetEventHandlerForRect(const gfx::Rect
& rect
);
565 // Returns the deepest visible descendant that contains the specified point
566 // and supports tooltips. If the view does not contain the point, returns
568 virtual View
* GetTooltipHandlerForPoint(const gfx::Point
& point
);
570 // Return the cursor that should be used for this view or the default cursor.
571 // The event location is in the receiver's coordinate system. The caller is
572 // responsible for managing the lifetime of the returned object, though that
573 // lifetime may vary from platform to platform. On Windows and Aura,
574 // the cursor is a shared resource.
575 virtual gfx::NativeCursor
GetCursor(const ui::MouseEvent
& event
);
577 // A convenience function which calls HitTestRect() with a rect of size
578 // 1x1 and an origin of |point|. |point| is in the local coordinate space
580 bool HitTestPoint(const gfx::Point
& point
) const;
582 // Returns true if |rect| intersects this view's bounds. |rect| is in the
583 // local coordinate space of |this|.
584 bool HitTestRect(const gfx::Rect
& rect
) const;
586 // Returns true if this view or any of its descendants are permitted to
587 // be the target of an event.
588 virtual bool CanProcessEventsWithinSubtree() const;
590 // Returns true if the mouse cursor is over |view| and mouse events are
592 bool IsMouseHovered();
594 // This method is invoked when the user clicks on this view.
595 // The provided event is in the receiver's coordinate system.
597 // Return true if you processed the event and want to receive subsequent
598 // MouseDraggged and MouseReleased events. This also stops the event from
599 // bubbling. If you return false, the event will bubble through parent
602 // If you remove yourself from the tree while processing this, event bubbling
603 // stops as if you returned true, but you will not receive future events.
604 // The return value is ignored in this case.
606 // Default implementation returns true if a ContextMenuController has been
607 // set, false otherwise. Override as needed.
609 virtual bool OnMousePressed(const ui::MouseEvent
& event
);
611 // This method is invoked when the user clicked on this control.
612 // and is still moving the mouse with a button pressed.
613 // The provided event is in the receiver's coordinate system.
615 // Return true if you processed the event and want to receive
616 // subsequent MouseDragged and MouseReleased events.
618 // Default implementation returns true if a ContextMenuController has been
619 // set, false otherwise. Override as needed.
621 virtual bool OnMouseDragged(const ui::MouseEvent
& event
);
623 // This method is invoked when the user releases the mouse
624 // button. The event is in the receiver's coordinate system.
626 // Default implementation notifies the ContextMenuController is appropriate.
627 // Subclasses that wish to honor the ContextMenuController should invoke
629 virtual void OnMouseReleased(const ui::MouseEvent
& event
);
631 // This method is invoked when the mouse press/drag was canceled by a
632 // system/user gesture.
633 virtual void OnMouseCaptureLost();
635 // This method is invoked when the mouse is above this control
636 // The event is in the receiver's coordinate system.
638 // Default implementation does nothing. Override as needed.
639 virtual void OnMouseMoved(const ui::MouseEvent
& event
);
641 // This method is invoked when the mouse enters this control.
643 // Default implementation does nothing. Override as needed.
644 virtual void OnMouseEntered(const ui::MouseEvent
& event
);
646 // This method is invoked when the mouse exits this control
647 // The provided event location is always (0, 0)
648 // Default implementation does nothing. Override as needed.
649 virtual void OnMouseExited(const ui::MouseEvent
& event
);
651 // Set the MouseHandler for a drag session.
653 // A drag session is a stream of mouse events starting
654 // with a MousePressed event, followed by several MouseDragged
655 // events and finishing with a MouseReleased event.
657 // This method should be only invoked while processing a
658 // MouseDragged or MousePressed event.
660 // All further mouse dragged and mouse up events will be sent
661 // the MouseHandler, even if it is reparented to another window.
663 // The MouseHandler is automatically cleared when the control
664 // comes back from processing the MouseReleased event.
666 // Note: if the mouse handler is no longer connected to a
667 // view hierarchy, events won't be sent.
669 // TODO(sky): rename this.
670 virtual void SetMouseHandler(View
* new_mouse_handler
);
672 // Invoked when a key is pressed or released.
673 // Subclasser should return true if the event has been processed and false
674 // otherwise. If the event has not been processed, the parent will be given a
676 virtual bool OnKeyPressed(const ui::KeyEvent
& event
);
677 virtual bool OnKeyReleased(const ui::KeyEvent
& event
);
679 // Invoked when the user uses the mousewheel. Implementors should return true
680 // if the event has been processed and false otherwise. This message is sent
681 // if the view is focused. If the event has not been processed, the parent
682 // will be given a chance.
683 virtual bool OnMouseWheel(const ui::MouseWheelEvent
& event
);
686 // See field for description.
687 void set_notify_enter_exit_on_child(bool notify
) {
688 notify_enter_exit_on_child_
= notify
;
690 bool notify_enter_exit_on_child() const {
691 return notify_enter_exit_on_child_
;
694 // Returns the View's TextInputClient instance or NULL if the View doesn't
695 // support text input.
696 virtual ui::TextInputClient
* GetTextInputClient();
698 // Convenience method to retrieve the InputMethod associated with the
699 // Widget that contains this view. Returns NULL if this view is not part of a
700 // view hierarchy with a Widget.
701 virtual InputMethod
* GetInputMethod();
702 virtual const InputMethod
* GetInputMethod() const;
704 // Sets a new ViewTargeter for the view, and returns the previous
706 scoped_ptr
<ViewTargeter
> SetEventTargeter(scoped_ptr
<ViewTargeter
> targeter
);
708 // Returns the ViewTargeter installed on |this| if one exists,
709 // otherwise returns the ViewTargeter installed on our root view.
710 // The return value is guaranteed to be non-null.
711 ViewTargeter
* GetEffectiveViewTargeter() const;
713 ViewTargeter
* targeter() const { return targeter_
.get(); }
715 // Overridden from ui::EventTarget:
716 bool CanAcceptEvent(const ui::Event
& event
) override
;
717 ui::EventTarget
* GetParentTarget() override
;
718 scoped_ptr
<ui::EventTargetIterator
> GetChildIterator() const override
;
719 ui::EventTargeter
* GetEventTargeter() override
;
720 void ConvertEventToTarget(ui::EventTarget
* target
,
721 ui::LocatedEvent
* event
) override
;
723 // Overridden from ui::EventHandler:
724 void OnKeyEvent(ui::KeyEvent
* event
) override
;
725 void OnMouseEvent(ui::MouseEvent
* event
) override
;
726 void OnScrollEvent(ui::ScrollEvent
* event
) override
;
727 void OnTouchEvent(ui::TouchEvent
* event
) final
;
728 void OnGestureEvent(ui::GestureEvent
* event
) override
;
730 // Accelerators --------------------------------------------------------------
732 // Sets a keyboard accelerator for that view. When the user presses the
733 // accelerator key combination, the AcceleratorPressed method is invoked.
734 // Note that you can set multiple accelerators for a view by invoking this
735 // method several times. Note also that AcceleratorPressed is invoked only
736 // when CanHandleAccelerators() is true.
737 virtual void AddAccelerator(const ui::Accelerator
& accelerator
);
739 // Removes the specified accelerator for this view.
740 virtual void RemoveAccelerator(const ui::Accelerator
& accelerator
);
742 // Removes all the keyboard accelerators for this view.
743 virtual void ResetAccelerators();
745 // Overridden from AcceleratorTarget:
746 bool AcceleratorPressed(const ui::Accelerator
& accelerator
) override
;
748 // Returns whether accelerators are enabled for this view. Accelerators are
749 // enabled if the containing widget is visible and the view is enabled() and
751 bool CanHandleAccelerators() const override
;
753 // Focus ---------------------------------------------------------------------
755 // Returns whether this view currently has the focus.
756 virtual bool HasFocus() const;
758 // Returns the view that should be selected next when pressing Tab.
759 View
* GetNextFocusableView();
760 const View
* GetNextFocusableView() const;
762 // Returns the view that should be selected next when pressing Shift-Tab.
763 View
* GetPreviousFocusableView();
765 // Sets the component that should be selected next when pressing Tab, and
766 // makes the current view the precedent view of the specified one.
767 // Note that by default views are linked in the order they have been added to
768 // their container. Use this method if you want to modify the order.
769 // IMPORTANT NOTE: loops in the focus hierarchy are not supported.
770 void SetNextFocusableView(View
* view
);
772 // Sets whether this view is capable of taking focus. It will clear focus if
773 // the focused view is set to be non-focusable.
774 // Note that this is false by default so that a view used as a container does
775 // not get the focus.
776 void SetFocusable(bool focusable
);
778 // Returns true if this view is |focusable_|, |enabled_| and drawn.
779 bool IsFocusable() const;
781 // Return whether this view is focusable when the user requires full keyboard
782 // access, even though it may not be normally focusable.
783 bool IsAccessibilityFocusable() const;
785 // Set whether this view can be made focusable if the user requires
786 // full keyboard access, even though it's not normally focusable. It will
787 // clear focus if the focused view is set to be non-focusable.
788 // Note that this is false by default.
789 void SetAccessibilityFocusable(bool accessibility_focusable
);
791 // Convenience method to retrieve the FocusManager associated with the
792 // Widget that contains this view. This can return NULL if this view is not
793 // part of a view hierarchy with a Widget.
794 virtual FocusManager
* GetFocusManager();
795 virtual const FocusManager
* GetFocusManager() const;
797 // Request keyboard focus. The receiving view will become the focused view.
798 virtual void RequestFocus();
800 // Invoked when a view is about to be requested for focus due to the focus
801 // traversal. Reverse is this request was generated going backward
803 virtual void AboutToRequestFocusFromTabTraversal(bool reverse
) {}
805 // Invoked when a key is pressed before the key event is processed (and
806 // potentially eaten) by the focus manager for tab traversal, accelerators and
807 // other focus related actions.
808 // The default implementation returns false, ensuring that tab traversal and
809 // accelerators processing is performed.
810 // Subclasses should return true if they want to process the key event and not
811 // have it processed as an accelerator (if any) or as a tab traversal (if the
812 // key event is for the TAB key). In that case, OnKeyPressed will
813 // subsequently be invoked for that event.
814 virtual bool SkipDefaultKeyEventProcessing(const ui::KeyEvent
& event
);
816 // Subclasses that contain traversable children that are not directly
817 // accessible through the children hierarchy should return the associated
818 // FocusTraversable for the focus traversal to work properly.
819 virtual FocusTraversable
* GetFocusTraversable();
821 // Subclasses that can act as a "pane" must implement their own
822 // FocusTraversable to keep the focus trapped within the pane.
823 // If this method returns an object, any view that's a direct or
824 // indirect child of this view will always use this FocusTraversable
825 // rather than the one from the widget.
826 virtual FocusTraversable
* GetPaneFocusTraversable();
828 // Tooltips ------------------------------------------------------------------
830 // Gets the tooltip for this View. If the View does not have a tooltip,
831 // return false. If the View does have a tooltip, copy the tooltip into
832 // the supplied string and return true.
833 // Any time the tooltip text that a View is displaying changes, it must
834 // invoke TooltipTextChanged.
835 // |p| provides the coordinates of the mouse (relative to this view).
836 virtual bool GetTooltipText(const gfx::Point
& p
,
837 base::string16
* tooltip
) const;
839 // Returns the location (relative to this View) for the text on the tooltip
840 // to display. If false is returned (the default), the tooltip is placed at
841 // a default position.
842 virtual bool GetTooltipTextOrigin(const gfx::Point
& p
, gfx::Point
* loc
) const;
844 // Context menus -------------------------------------------------------------
846 // Sets the ContextMenuController. Setting this to non-null makes the View
847 // process mouse events.
848 ContextMenuController
* context_menu_controller() {
849 return context_menu_controller_
;
851 void set_context_menu_controller(ContextMenuController
* menu_controller
) {
852 context_menu_controller_
= menu_controller
;
855 // Provides default implementation for context menu handling. The default
856 // implementation calls the ShowContextMenu of the current
857 // ContextMenuController (if it is not NULL). Overridden in subclassed views
858 // to provide right-click menu display triggerd by the keyboard (i.e. for the
859 // Chrome toolbar Back and Forward buttons). No source needs to be specified,
860 // as it is always equal to the current View.
861 virtual void ShowContextMenu(const gfx::Point
& p
,
862 ui::MenuSourceType source_type
);
864 // On some platforms, we show context menu on mouse press instead of release.
865 // This method returns true for those platforms.
866 static bool ShouldShowContextMenuOnMousePress();
868 // Drag and drop -------------------------------------------------------------
870 DragController
* drag_controller() { return drag_controller_
; }
871 void set_drag_controller(DragController
* drag_controller
) {
872 drag_controller_
= drag_controller
;
875 // During a drag and drop session when the mouse moves the view under the
876 // mouse is queried for the drop types it supports by way of the
877 // GetDropFormats methods. If the view returns true and the drag site can
878 // provide data in one of the formats, the view is asked if the drop data
879 // is required before any other drop events are sent. Once the
880 // data is available the view is asked if it supports the drop (by way of
881 // the CanDrop method). If a view returns true from CanDrop,
882 // OnDragEntered is sent to the view when the mouse first enters the view,
883 // as the mouse moves around within the view OnDragUpdated is invoked.
884 // If the user releases the mouse over the view and OnDragUpdated returns a
885 // valid drop, then OnPerformDrop is invoked. If the mouse moves outside the
886 // view or over another view that wants the drag, OnDragExited is invoked.
888 // Similar to mouse events, the deepest view under the mouse is first checked
889 // if it supports the drop (Drop). If the deepest view under
890 // the mouse does not support the drop, the ancestors are walked until one
891 // is found that supports the drop.
893 // Override and return the set of formats that can be dropped on this view.
894 // |formats| is a bitmask of the formats defined bye OSExchangeData::Format.
895 // The default implementation returns false, which means the view doesn't
897 virtual bool GetDropFormats(
899 std::set
<OSExchangeData::CustomFormat
>* custom_formats
);
901 // Override and return true if the data must be available before any drop
902 // methods should be invoked. The default is false.
903 virtual bool AreDropTypesRequired();
905 // A view that supports drag and drop must override this and return true if
906 // data contains a type that may be dropped on this view.
907 virtual bool CanDrop(const OSExchangeData
& data
);
909 // OnDragEntered is invoked when the mouse enters this view during a drag and
910 // drop session and CanDrop returns true. This is immediately
911 // followed by an invocation of OnDragUpdated, and eventually one of
912 // OnDragExited or OnPerformDrop.
913 virtual void OnDragEntered(const ui::DropTargetEvent
& event
);
915 // Invoked during a drag and drop session while the mouse is over the view.
916 // This should return a bitmask of the DragDropTypes::DragOperation supported
917 // based on the location of the event. Return 0 to indicate the drop should
919 virtual int OnDragUpdated(const ui::DropTargetEvent
& event
);
921 // Invoked during a drag and drop session when the mouse exits the views, or
922 // when the drag session was canceled and the mouse was over the view.
923 virtual void OnDragExited();
925 // Invoked during a drag and drop session when OnDragUpdated returns a valid
926 // operation and the user release the mouse.
927 virtual int OnPerformDrop(const ui::DropTargetEvent
& event
);
929 // Invoked from DoDrag after the drag completes. This implementation does
930 // nothing, and is intended for subclasses to do cleanup.
931 virtual void OnDragDone();
933 // Returns true if the mouse was dragged enough to start a drag operation.
934 // delta_x and y are the distance the mouse was dragged.
935 static bool ExceededDragThreshold(const gfx::Vector2d
& delta
);
937 // Accessibility -------------------------------------------------------------
939 // Modifies |state| to reflect the current accessible state of this view.
940 virtual void GetAccessibleState(ui::AXViewState
* state
) { }
942 // Returns an instance of the native accessibility interface for this view.
943 virtual gfx::NativeViewAccessible
GetNativeViewAccessible();
945 // Notifies assistive technology that an accessibility event has
946 // occurred on this view, such as when the view is focused or when its
947 // value changes. Pass true for |send_native_event| except for rare
948 // cases where the view is a native control that's already sending a
949 // native accessibility event and the duplicate event would cause
951 void NotifyAccessibilityEvent(ui::AXEvent event_type
,
952 bool send_native_event
);
954 // Scrolling -----------------------------------------------------------------
955 // TODO(beng): Figure out if this can live somewhere other than View, i.e.
956 // closer to ScrollView.
958 // Scrolls the specified region, in this View's coordinate system, to be
959 // visible. View's implementation passes the call onto the parent View (after
960 // adjusting the coordinates). It is up to views that only show a portion of
961 // the child view, such as Viewport, to override appropriately.
962 virtual void ScrollRectToVisible(const gfx::Rect
& rect
);
964 // The following methods are used by ScrollView to determine the amount
965 // to scroll relative to the visible bounds of the view. For example, a
966 // return value of 10 indicates the scrollview should scroll 10 pixels in
967 // the appropriate direction.
969 // Each method takes the following parameters:
971 // is_horizontal: if true, scrolling is along the horizontal axis, otherwise
972 // the vertical axis.
973 // is_positive: if true, scrolling is by a positive amount. Along the
974 // vertical axis scrolling by a positive amount equates to
977 // The return value should always be positive and gives the number of pixels
978 // to scroll. ScrollView interprets a return value of 0 (or negative)
979 // to scroll by a default amount.
981 // See VariableRowHeightScrollHelper and FixedRowHeightScrollHelper for
982 // implementations of common cases.
983 virtual int GetPageScrollIncrement(ScrollView
* scroll_view
,
984 bool is_horizontal
, bool is_positive
);
985 virtual int GetLineScrollIncrement(ScrollView
* scroll_view
,
986 bool is_horizontal
, bool is_positive
);
989 // Used to track a drag. RootView passes this into
990 // ProcessMousePressed/Dragged.
992 // Sets possible_drag to false and start_x/y to 0. This is invoked by
993 // RootView prior to invoke ProcessMousePressed.
996 // Sets possible_drag to true and start_pt to the specified point.
997 // This is invoked by the target view if it detects the press may generate
999 void PossibleDrag(const gfx::Point
& p
);
1001 // Whether the press may generate a drag.
1004 // Coordinates of the mouse press.
1005 gfx::Point start_pt
;
1008 // Size and disposition ------------------------------------------------------
1010 // Override to be notified when the bounds of the view have changed.
1011 virtual void OnBoundsChanged(const gfx::Rect
& previous_bounds
);
1013 // Called when the preferred size of a child view changed. This gives the
1014 // parent an opportunity to do a fresh layout if that makes sense.
1015 virtual void ChildPreferredSizeChanged(View
* child
) {}
1017 // Called when the visibility of a child view changed. This gives the parent
1018 // an opportunity to do a fresh layout if that makes sense.
1019 virtual void ChildVisibilityChanged(View
* child
) {}
1021 // Invalidates the layout and calls ChildPreferredSizeChanged on the parent
1022 // if there is one. Be sure to call View::PreferredSizeChanged when
1023 // overriding such that the layout is properly invalidated.
1024 virtual void PreferredSizeChanged();
1026 // Override returning true when the view needs to be notified when its visible
1027 // bounds relative to the root view may have changed. Only used by
1029 virtual bool GetNeedsNotificationWhenVisibleBoundsChange() const;
1031 // Notification that this View's visible bounds relative to the root view may
1032 // have changed. The visible bounds are the region of the View not clipped by
1033 // its ancestors. This is used for clipping NativeViewHost.
1034 virtual void OnVisibleBoundsChanged();
1036 // Override to be notified when the enabled state of this View has
1037 // changed. The default implementation calls SchedulePaint() on this View.
1038 virtual void OnEnabledChanged();
1040 bool needs_layout() const { return needs_layout_
; }
1042 // Tree operations -----------------------------------------------------------
1044 // This method is invoked when the tree changes.
1046 // When a view is removed, it is invoked for all children and grand
1047 // children. For each of these views, a notification is sent to the
1048 // view and all parents.
1050 // When a view is added, a notification is sent to the view, all its
1051 // parents, and all its children (and grand children)
1053 // Default implementation does nothing. Override to perform operations
1054 // required when a view is added or removed from a view hierarchy
1056 // Refer to comments in struct |ViewHierarchyChangedDetails| for |details|.
1057 virtual void ViewHierarchyChanged(const ViewHierarchyChangedDetails
& details
);
1059 // When SetVisible() changes the visibility of a view, this method is
1060 // invoked for that view as well as all the children recursively.
1061 virtual void VisibilityChanged(View
* starting_from
, bool is_visible
);
1063 // This method is invoked when the parent NativeView of the widget that the
1064 // view is attached to has changed and the view hierarchy has not changed.
1065 // ViewHierarchyChanged() is called when the parent NativeView of the widget
1066 // that the view is attached to is changed as a result of changing the view
1067 // hierarchy. Overriding this method is useful for tracking which
1068 // FocusManager manages this view.
1069 virtual void NativeViewHierarchyChanged();
1071 // Painting ------------------------------------------------------------------
1073 // Responsible for calling Paint() on child Views. Override to control the
1074 // order child Views are painted.
1075 virtual void PaintChildren(const ui::PaintContext
& context
);
1077 // Override to provide rendering in any part of the View's bounds. Typically
1078 // this is the "contents" of the view. If you override this method you will
1079 // have to call the subsequent OnPaint*() methods manually.
1080 virtual void OnPaint(gfx::Canvas
* canvas
);
1082 // Override to paint a background before any content is drawn. Typically this
1083 // is done if you are satisfied with a default OnPaint handler but wish to
1084 // supply a different background.
1085 virtual void OnPaintBackground(gfx::Canvas
* canvas
);
1087 // Override to paint a border not specified by SetBorder().
1088 virtual void OnPaintBorder(gfx::Canvas
* canvas
);
1090 // Accelerated painting ------------------------------------------------------
1092 // Returns the offset from this view to the nearest ancestor with a layer. If
1093 // |layer_parent| is non-NULL it is set to the nearest ancestor with a layer.
1094 virtual gfx::Vector2d
CalculateOffsetToAncestorWithLayer(
1095 ui::Layer
** layer_parent
);
1097 // Updates the view's layer's parent. Called when a view is added to a view
1098 // hierarchy, responsible for parenting the view's layer to the enclosing
1099 // layer in the hierarchy.
1100 virtual void UpdateParentLayer();
1102 // If this view has a layer, the layer is reparented to |parent_layer| and its
1103 // bounds is set based on |point|. If this view does not have a layer, then
1104 // recurses through all children. This is used when adding a layer to an
1105 // existing view to make sure all descendants that have layers are parented to
1107 void MoveLayerToParent(ui::Layer
* parent_layer
, const gfx::Point
& point
);
1109 // Called to update the bounds of any child layers within this View's
1110 // hierarchy when something happens to the hierarchy.
1111 void UpdateChildLayerBounds(const gfx::Vector2d
& offset
);
1113 // Overridden from ui::LayerDelegate:
1114 void OnPaintLayer(const ui::PaintContext
& context
) override
;
1115 void OnDelegatedFrameDamage(const gfx::Rect
& damage_rect_in_dip
) override
;
1116 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
;
1117 base::Closure
PrepareForLayerBoundsChange() override
;
1119 // Finds the layer that this view paints to (it may belong to an ancestor
1120 // view), then reorders the immediate children of that layer to match the
1121 // order of the view tree.
1122 virtual void ReorderLayers();
1124 // This reorders the immediate children of |*parent_layer| to match the
1125 // order of the view tree. Child layers which are owned by a view are
1126 // reordered so that they are below any child layers not owned by a view.
1127 // Widget::ReorderNativeViews() should be called to reorder any child layers
1128 // with an associated view. Widget::ReorderNativeViews() may reorder layers
1129 // below layers owned by a view.
1130 virtual void ReorderChildLayers(ui::Layer
* parent_layer
);
1132 // Input ---------------------------------------------------------------------
1134 virtual DragInfo
* GetDragInfo();
1136 // Focus ---------------------------------------------------------------------
1138 // Returns last value passed to SetFocusable(). Use IsFocusable() to determine
1139 // if a view can take focus right now.
1140 bool focusable() const { return focusable_
; }
1142 // Override to be notified when focus has changed either to or from this View.
1143 virtual void OnFocus();
1144 virtual void OnBlur();
1146 // Handle view focus/blur events for this view.
1150 // System events -------------------------------------------------------------
1152 // Called when the UI theme (not the NativeTheme) has changed, overriding
1153 // allows individual Views to do special cleanup and processing (such as
1154 // dropping resource caches). To dispatch a theme changed notification, call
1155 // Widget::ThemeChanged().
1156 virtual void OnThemeChanged() {}
1158 // Called when the locale has changed, overriding allows individual Views to
1159 // update locale-dependent strings.
1160 // To dispatch a locale changed notification, call Widget::LocaleChanged().
1161 virtual void OnLocaleChanged() {}
1163 // Tooltips ------------------------------------------------------------------
1165 // Views must invoke this when the tooltip text they are to display changes.
1166 void TooltipTextChanged();
1168 // Context menus -------------------------------------------------------------
1170 // Returns the location, in screen coordinates, to show the context menu at
1171 // when the context menu is shown from the keyboard. This implementation
1172 // returns the middle of the visible region of this view.
1174 // This method is invoked when the context menu is shown by way of the
1176 virtual gfx::Point
GetKeyboardContextMenuLocation();
1178 // Drag and drop -------------------------------------------------------------
1180 // These are cover methods that invoke the method of the same name on
1181 // the DragController. Subclasses may wish to override rather than install
1182 // a DragController.
1183 // See DragController for a description of these methods.
1184 virtual int GetDragOperations(const gfx::Point
& press_pt
);
1185 virtual void WriteDragData(const gfx::Point
& press_pt
, OSExchangeData
* data
);
1187 // Returns whether we're in the middle of a drag session that was initiated
1191 // Returns how much the mouse needs to move in one direction to start a
1192 // drag. These methods cache in a platform-appropriate way. These values are
1193 // used by the public static method ExceededDragThreshold().
1194 static int GetHorizontalDragThreshold();
1195 static int GetVerticalDragThreshold();
1197 // NativeTheme ---------------------------------------------------------------
1199 // Invoked when the NativeTheme associated with this View changes.
1200 virtual void OnNativeThemeChanged(const ui::NativeTheme
* theme
) {}
1202 // Debugging -----------------------------------------------------------------
1204 #if !defined(NDEBUG)
1205 // Returns string containing a graph of the views hierarchy in graphViz DOT
1206 // language (http://graphviz.org/). Can be called within debugger and save
1207 // to a file to compile/view.
1208 // Note: Assumes initial call made with first = true.
1209 virtual std::string
PrintViewGraph(bool first
);
1211 // Some classes may own an object which contains the children to displayed in
1212 // the views hierarchy. The above function gives the class the flexibility to
1213 // decide which object should be used to obtain the children, but this
1214 // function makes the decision explicit.
1215 std::string
DoPrintViewGraph(bool first
, View
* view_with_children
);
1219 friend class internal::PreEventDispatchHandler
;
1220 friend class internal::PostEventDispatchHandler
;
1221 friend class internal::RootView
;
1222 friend class FocusManager
;
1223 friend class Widget
;
1225 // Painting -----------------------------------------------------------------
1227 enum SchedulePaintType
{
1228 // Indicates the size is the same (only the origin changed).
1229 SCHEDULE_PAINT_SIZE_SAME
,
1231 // Indicates the size changed (and possibly the origin).
1232 SCHEDULE_PAINT_SIZE_CHANGED
1235 // Invoked before and after the bounds change to schedule painting the old and
1237 void SchedulePaintBoundsChanged(SchedulePaintType type
);
1239 // Tree operations -----------------------------------------------------------
1241 // Removes |view| from the hierarchy tree. If |update_focus_cycle| is true,
1242 // the next and previous focusable views of views pointing to this view are
1243 // updated. If |update_tool_tip| is true, the tooltip is updated. If
1244 // |delete_removed_view| is true, the view is also deleted (if it is parent
1245 // owned). If |new_parent| is not NULL, the remove is the result of
1246 // AddChildView() to a new parent. For this case, |new_parent| is the View
1247 // that |view| is going to be added to after the remove completes.
1248 void DoRemoveChildView(View
* view
,
1249 bool update_focus_cycle
,
1250 bool update_tool_tip
,
1251 bool delete_removed_view
,
1254 // Call ViewHierarchyChanged() for all child views and all parents.
1255 // |old_parent| is the original parent of the View that was removed.
1256 // If |new_parent| is not NULL, the View that was removed will be reparented
1257 // to |new_parent| after the remove operation.
1258 void PropagateRemoveNotifications(View
* old_parent
, View
* new_parent
);
1260 // Call ViewHierarchyChanged() for all children.
1261 void PropagateAddNotifications(const ViewHierarchyChangedDetails
& details
);
1263 // Propagates NativeViewHierarchyChanged() notification through all the
1265 void PropagateNativeViewHierarchyChanged();
1267 // Takes care of registering/unregistering accelerators if
1268 // |register_accelerators| true and calls ViewHierarchyChanged().
1269 void ViewHierarchyChangedImpl(bool register_accelerators
,
1270 const ViewHierarchyChangedDetails
& details
);
1272 // Invokes OnNativeThemeChanged() on this and all descendants.
1273 void PropagateNativeThemeChanged(const ui::NativeTheme
* theme
);
1275 // Size and disposition ------------------------------------------------------
1277 // Call VisibilityChanged() recursively for all children.
1278 void PropagateVisibilityNotifications(View
* from
, bool is_visible
);
1280 // Registers/unregisters accelerators as necessary and calls
1281 // VisibilityChanged().
1282 void VisibilityChangedImpl(View
* starting_from
, bool is_visible
);
1284 // Responsible for propagating bounds change notifications to relevant
1286 void BoundsChanged(const gfx::Rect
& previous_bounds
);
1288 // Visible bounds notification registration.
1289 // When a view is added to a hierarchy, it and all its children are asked if
1290 // they need to be registered for "visible bounds within root" notifications
1291 // (see comment on OnVisibleBoundsChanged()). If they do, they are registered
1292 // with every ancestor between them and the root of the hierarchy.
1293 static void RegisterChildrenForVisibleBoundsNotification(View
* view
);
1294 static void UnregisterChildrenForVisibleBoundsNotification(View
* view
);
1295 void RegisterForVisibleBoundsNotification();
1296 void UnregisterForVisibleBoundsNotification();
1298 // Adds/removes view to the list of descendants that are notified any time
1299 // this views location and possibly size are changed.
1300 void AddDescendantToNotify(View
* view
);
1301 void RemoveDescendantToNotify(View
* view
);
1303 // Sets the layer's bounds given in DIP coordinates.
1304 void SetLayerBounds(const gfx::Rect
& bounds_in_dip
);
1306 // Transformations -----------------------------------------------------------
1308 // Returns in |transform| the transform to get from coordinates of |ancestor|
1309 // to this. Returns true if |ancestor| is found. If |ancestor| is not found,
1310 // or NULL, |transform| is set to convert from root view coordinates to this.
1311 bool GetTransformRelativeTo(const View
* ancestor
,
1312 gfx::Transform
* transform
) const;
1314 // Coordinate conversion -----------------------------------------------------
1316 // Convert a point in the view's coordinate to an ancestor view's coordinate
1317 // system using necessary transformations. Returns whether the point was
1318 // successfully converted to the ancestor's coordinate system.
1319 bool ConvertPointForAncestor(const View
* ancestor
, gfx::Point
* point
) const;
1321 // Convert a point in the ancestor's coordinate system to the view's
1322 // coordinate system using necessary transformations. Returns whether the
1323 // point was successfully converted from the ancestor's coordinate system
1324 // to the view's coordinate system.
1325 bool ConvertPointFromAncestor(const View
* ancestor
, gfx::Point
* point
) const;
1327 // Convert a rect in the view's coordinate to an ancestor view's coordinate
1328 // system using necessary transformations. Returns whether the rect was
1329 // successfully converted to the ancestor's coordinate system.
1330 bool ConvertRectForAncestor(const View
* ancestor
, gfx::RectF
* rect
) const;
1332 // Convert a rect in the ancestor's coordinate system to the view's
1333 // coordinate system using necessary transformations. Returns whether the
1334 // rect was successfully converted from the ancestor's coordinate system
1335 // to the view's coordinate system.
1336 bool ConvertRectFromAncestor(const View
* ancestor
, gfx::RectF
* rect
) const;
1338 // Accelerated painting ------------------------------------------------------
1340 // Creates the layer and related fields for this view.
1343 // Parents all un-parented layers within this view's hierarchy to this view's
1345 void UpdateParentLayers();
1347 // Parents this view's layer to |parent_layer|, and sets its bounds and other
1348 // properties in accordance to |offset|, the view's offset from the
1350 void ReparentLayer(const gfx::Vector2d
& offset
, ui::Layer
* parent_layer
);
1352 // Called to update the layer visibility. The layer will be visible if the
1353 // View itself, and all its parent Views are visible. This also updates
1354 // visibility of the child layers.
1355 void UpdateLayerVisibility();
1356 void UpdateChildLayerVisibility(bool visible
);
1358 // Orphans the layers in this subtree that are parented to layers outside of
1360 void OrphanLayers();
1362 // Destroys the layer associated with this view, and reparents any descendants
1363 // to the destroyed layer's parent.
1364 void DestroyLayer();
1366 // Input ---------------------------------------------------------------------
1368 bool ProcessMousePressed(const ui::MouseEvent
& event
);
1369 bool ProcessMouseDragged(const ui::MouseEvent
& event
);
1370 void ProcessMouseReleased(const ui::MouseEvent
& event
);
1372 // Accelerators --------------------------------------------------------------
1374 // Registers this view's keyboard accelerators that are not registered to
1375 // FocusManager yet, if possible.
1376 void RegisterPendingAccelerators();
1378 // Unregisters all the keyboard accelerators associated with this view.
1379 // |leave_data_intact| if true does not remove data from accelerators_ array,
1380 // so it could be re-registered with other focus manager
1381 void UnregisterAccelerators(bool leave_data_intact
);
1383 // Focus ---------------------------------------------------------------------
1385 // Initialize the previous/next focusable views of the specified view relative
1386 // to the view at the specified index.
1387 void InitFocusSiblings(View
* view
, int index
);
1389 // Helper function to advance focus, in case the currently focused view has
1390 // become unfocusable.
1391 void AdvanceFocusIfNecessary();
1393 // System events -------------------------------------------------------------
1395 // Used to propagate theme changed notifications from the root view to all
1396 // views in the hierarchy.
1397 void PropagateThemeChanged();
1399 // Used to propagate locale changed notifications from the root view to all
1400 // views in the hierarchy.
1401 void PropagateLocaleChanged();
1403 // Used to propagate device scale factor changed notifications from the root
1404 // view to all views in the hierarchy.
1405 void PropagateDeviceScaleFactorChanged(float device_scale_factor
);
1407 // Tooltips ------------------------------------------------------------------
1409 // Propagates UpdateTooltip() to the TooltipManager for the Widget.
1410 // This must be invoked any time the View hierarchy changes in such a way
1411 // the view under the mouse differs. For example, if the bounds of a View is
1412 // changed, this is invoked. Similarly, as Views are added/removed, this
1414 void UpdateTooltip();
1416 // Drag and drop -------------------------------------------------------------
1418 // Starts a drag and drop operation originating from this view. This invokes
1419 // WriteDragData to write the data and GetDragOperations to determine the
1420 // supported drag operations. When done, OnDragDone is invoked. |press_pt| is
1421 // in the view's coordinate system.
1422 // Returns true if a drag was started.
1423 bool DoDrag(const ui::LocatedEvent
& event
,
1424 const gfx::Point
& press_pt
,
1425 ui::DragDropTypes::DragEventSource source
);
1427 //////////////////////////////////////////////////////////////////////////////
1429 // Creation and lifetime -----------------------------------------------------
1431 // False if this View is owned by its parent - i.e. it will be deleted by its
1432 // parent during its parents destruction. False is the default.
1433 bool owned_by_client_
;
1435 // Attributes ----------------------------------------------------------------
1437 // The id of this View. Used to find this View.
1440 // The group of this view. Some view subclasses use this id to find other
1441 // views of the same group. For example radio button uses this information
1442 // to find other radio buttons.
1445 // Tree operations -----------------------------------------------------------
1447 // This view's parent.
1450 // This view's children.
1453 // Size and disposition ------------------------------------------------------
1455 // This View's bounds in the parent coordinate system.
1458 // Whether this view is visible.
1461 // Whether this view is enabled.
1464 // When this flag is on, a View receives a mouse-enter and mouse-leave event
1465 // even if a descendant View is the event-recipient for the real mouse
1466 // events. When this flag is turned on, and mouse moves from outside of the
1467 // view into a child view, both the child view and this view receives
1468 // mouse-enter event. Similarly, if the mouse moves from inside a child view
1469 // and out of this view, then both views receive a mouse-leave event.
1470 // When this flag is turned off, if the mouse moves from inside this view into
1471 // a child view, then this view receives a mouse-leave event. When this flag
1472 // is turned on, it does not receive the mouse-leave event in this case.
1473 // When the mouse moves from inside the child view out of the child view but
1474 // still into this view, this view receives a mouse-enter event if this flag
1475 // is turned off, but doesn't if this flag is turned on.
1476 // This flag is initialized to false.
1477 bool notify_enter_exit_on_child_
;
1479 // Whether or not RegisterViewForVisibleBoundsNotification on the RootView
1480 // has been invoked.
1481 bool registered_for_visible_bounds_notification_
;
1483 // List of descendants wanting notification when their visible bounds change.
1484 scoped_ptr
<Views
> descendants_to_notify_
;
1486 // Transformations -----------------------------------------------------------
1488 // Clipping parameters. skia transformation matrix does not give us clipping.
1489 // So we do it ourselves.
1490 gfx::Insets clip_insets_
;
1492 // Layout --------------------------------------------------------------------
1494 // Whether the view needs to be laid out.
1497 // The View's LayoutManager defines the sizing heuristics applied to child
1498 // Views. The default is absolute positioning according to bounds_.
1499 scoped_ptr
<LayoutManager
> layout_manager_
;
1501 // Whether this View's layer should be snapped to the pixel boundary.
1502 bool snap_layer_to_pixel_boundary_
;
1504 // Painting ------------------------------------------------------------------
1507 scoped_ptr
<Background
> background_
;
1510 scoped_ptr
<Border
> border_
;
1512 // RTL painting --------------------------------------------------------------
1514 // Indicates whether or not the gfx::Canvas object passed to View::Paint()
1515 // is going to be flipped horizontally (using the appropriate transform) on
1516 // right-to-left locales for this View.
1517 bool flip_canvas_on_paint_for_rtl_ui_
;
1519 // Accelerated painting ------------------------------------------------------
1521 bool paint_to_layer_
;
1523 // Accelerators --------------------------------------------------------------
1525 // Focus manager accelerators registered on.
1526 FocusManager
* accelerator_focus_manager_
;
1528 // The list of accelerators. List elements in the range
1529 // [0, registered_accelerator_count_) are already registered to FocusManager,
1530 // and the rest are not yet.
1531 scoped_ptr
<std::vector
<ui::Accelerator
> > accelerators_
;
1532 size_t registered_accelerator_count_
;
1534 // Focus ---------------------------------------------------------------------
1536 // Next view to be focused when the Tab key is pressed.
1537 View
* next_focusable_view_
;
1539 // Next view to be focused when the Shift-Tab key combination is pressed.
1540 View
* previous_focusable_view_
;
1542 // Whether this view can be focused.
1545 // Whether this view is focusable if the user requires full keyboard access,
1546 // even though it may not be normally focusable.
1547 bool accessibility_focusable_
;
1549 // Context menus -------------------------------------------------------------
1551 // The menu controller.
1552 ContextMenuController
* context_menu_controller_
;
1554 // Drag and drop -------------------------------------------------------------
1556 DragController
* drag_controller_
;
1558 // Input --------------------------------------------------------------------
1560 scoped_ptr
<ViewTargeter
> targeter_
;
1562 // Accessibility -------------------------------------------------------------
1564 // Belongs to this view, but it's reference-counted on some platforms
1565 // so we can't use a scoped_ptr. It's dereferenced in the destructor.
1566 NativeViewAccessibility
* native_view_accessibility_
;
1568 DISALLOW_COPY_AND_ASSIGN(View
);
1571 } // namespace views
1573 #endif // UI_VIEWS_VIEW_H_