Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / ui / views / view.h
blob314ebb7ddeaef6756bfa2450148a5c6badcb4062
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_
8 #include <algorithm>
9 #include <map>
10 #include <set>
11 #include <string>
12 #include <vector>
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/events/event_targeter.h"
30 #include "ui/gfx/insets.h"
31 #include "ui/gfx/native_widget_types.h"
32 #include "ui/gfx/rect.h"
33 #include "ui/gfx/vector2d.h"
34 #include "ui/views/views_export.h"
36 #if defined(OS_WIN)
37 #include "base/win/scoped_comptr.h"
38 #endif
40 using ui::OSExchangeData;
42 namespace gfx {
43 class Canvas;
44 class Insets;
45 class Path;
46 class Transform;
49 namespace ui {
50 struct AXViewState;
51 class Compositor;
52 class Layer;
53 class NativeTheme;
54 class TextInputClient;
55 class Texture;
56 class ThemeProvider;
59 namespace views {
61 class Background;
62 class Border;
63 class ContextMenuController;
64 class DragController;
65 class FocusManager;
66 class FocusTraversable;
67 class InputMethod;
68 class LayoutManager;
69 class NativeViewAccessibility;
70 class ScrollView;
71 class Widget;
73 namespace internal {
74 class PreEventDispatchHandler;
75 class PostEventDispatchHandler;
76 class RootView;
79 /////////////////////////////////////////////////////////////////////////////
81 // View class
83 // A View is a rectangle within the views View hierarchy. It is the base
84 // class for all Views.
86 // A View is a container of other Views (there is no such thing as a Leaf
87 // View - makes code simpler, reduces type conversion headaches, design
88 // mistakes etc)
90 // The View contains basic properties for sizing (bounds), layout (flex,
91 // orientation, etc), painting of children and event dispatch.
93 // The View also uses a simple Box Layout Manager similar to XUL's
94 // SprocketLayout system. Alternative Layout Managers implementing the
95 // LayoutManager interface can be used to lay out children if required.
97 // It is up to the subclass to implement Painting and storage of subclass -
98 // specific properties and functionality.
100 // Unless otherwise documented, views is not thread safe and should only be
101 // accessed from the main thread.
103 /////////////////////////////////////////////////////////////////////////////
104 class VIEWS_EXPORT View : public ui::LayerDelegate,
105 public ui::LayerOwner,
106 public ui::AcceleratorTarget,
107 public ui::EventTarget {
108 public:
109 typedef std::vector<View*> Views;
111 // TODO(tdanderson,sadrul): Becomes obsolete with the refactoring of the
112 // event targeting logic for views and windows.
113 // Specifies the source of the region used in a hit test.
114 // HIT_TEST_SOURCE_MOUSE indicates the hit test is being performed with a
115 // single point and HIT_TEST_SOURCE_TOUCH indicates the hit test is being
116 // performed with a rect larger than a single point. This value can be used,
117 // for example, to add extra padding or change the shape of the hit test mask.
118 enum HitTestSource {
119 HIT_TEST_SOURCE_MOUSE,
120 HIT_TEST_SOURCE_TOUCH
123 struct ViewHierarchyChangedDetails {
124 ViewHierarchyChangedDetails()
125 : is_add(false),
126 parent(NULL),
127 child(NULL),
128 move_view(NULL) {}
130 ViewHierarchyChangedDetails(bool is_add,
131 View* parent,
132 View* child,
133 View* move_view)
134 : is_add(is_add),
135 parent(parent),
136 child(child),
137 move_view(move_view) {}
139 bool is_add;
140 // New parent if |is_add| is true, old parent if |is_add| is false.
141 View* parent;
142 // The view being added or removed.
143 View* child;
144 // If this is a move (reparent), meaning AddChildViewAt() is invoked with an
145 // existing parent, then a notification for the remove is sent first,
146 // followed by one for the add. This case can be distinguished by a
147 // non-NULL |move_view|.
148 // For the remove part of move, |move_view| is the new parent of the View
149 // being removed.
150 // For the add part of move, |move_view| is the old parent of the View being
151 // added.
152 View* move_view;
155 // Creation and lifetime -----------------------------------------------------
157 View();
158 virtual ~View();
160 // By default a View is owned by its parent unless specified otherwise here.
161 void set_owned_by_client() { owned_by_client_ = true; }
163 // Tree operations -----------------------------------------------------------
165 // Get the Widget that hosts this View, if any.
166 virtual const Widget* GetWidget() const;
167 virtual Widget* GetWidget();
169 // Adds |view| as a child of this view, optionally at |index|.
170 void AddChildView(View* view);
171 void AddChildViewAt(View* view, int index);
173 // Moves |view| to the specified |index|. A negative value for |index| moves
174 // the view at the end.
175 void ReorderChildView(View* view, int index);
177 // Removes |view| from this view. The view's parent will change to NULL.
178 void RemoveChildView(View* view);
180 // Removes all the children from this view. If |delete_children| is true,
181 // the views are deleted, unless marked as not parent owned.
182 void RemoveAllChildViews(bool delete_children);
184 int child_count() const { return static_cast<int>(children_.size()); }
185 bool has_children() const { return !children_.empty(); }
187 // Returns the child view at |index|.
188 const View* child_at(int index) const {
189 DCHECK_GE(index, 0);
190 DCHECK_LT(index, child_count());
191 return children_[index];
193 View* child_at(int index) {
194 return const_cast<View*>(const_cast<const View*>(this)->child_at(index));
197 // Returns the parent view.
198 const View* parent() const { return parent_; }
199 View* parent() { return parent_; }
201 // Returns true if |view| is contained within this View's hierarchy, even as
202 // an indirect descendant. Will return true if child is also this view.
203 bool Contains(const View* view) const;
205 // Returns the index of |view|, or -1 if |view| is not a child of this view.
206 int GetIndexOf(const View* view) const;
208 // Size and disposition ------------------------------------------------------
209 // Methods for obtaining and modifying the position and size of the view.
210 // Position is in the coordinate system of the view's parent.
211 // Position is NOT flipped for RTL. See "RTL positioning" for RTL-sensitive
212 // position accessors.
213 // Transformations are not applied on the size/position. For example, if
214 // bounds is (0, 0, 100, 100) and it is scaled by 0.5 along the X axis, the
215 // width will still be 100 (although when painted, it will be 50x50, painted
216 // at location (0, 0)).
218 void SetBounds(int x, int y, int width, int height);
219 void SetBoundsRect(const gfx::Rect& bounds);
220 void SetSize(const gfx::Size& size);
221 void SetPosition(const gfx::Point& position);
222 void SetX(int x);
223 void SetY(int y);
225 // No transformation is applied on the size or the locations.
226 const gfx::Rect& bounds() const { return bounds_; }
227 int x() const { return bounds_.x(); }
228 int y() const { return bounds_.y(); }
229 int width() const { return bounds_.width(); }
230 int height() const { return bounds_.height(); }
231 const gfx::Size& size() const { return bounds_.size(); }
233 // Returns the bounds of the content area of the view, i.e. the rectangle
234 // enclosed by the view's border.
235 gfx::Rect GetContentsBounds() const;
237 // Returns the bounds of the view in its own coordinates (i.e. position is
238 // 0, 0).
239 gfx::Rect GetLocalBounds() const;
241 // Returns the bounds of the layer in its own pixel coordinates.
242 gfx::Rect GetLayerBoundsInPixel() const;
244 // Returns the insets of the current border. If there is no border an empty
245 // insets is returned.
246 virtual gfx::Insets GetInsets() const;
248 // Returns the visible bounds of the receiver in the receivers coordinate
249 // system.
251 // When traversing the View hierarchy in order to compute the bounds, the
252 // function takes into account the mirroring setting and transformation for
253 // each View and therefore it will return the mirrored and transformed version
254 // of the visible bounds if need be.
255 gfx::Rect GetVisibleBounds() const;
257 // Return the bounds of the View in screen coordinate system.
258 gfx::Rect GetBoundsInScreen() const;
260 // Returns the baseline of this view, or -1 if this view has no baseline. The
261 // return value is relative to the preferred height.
262 virtual int GetBaseline() const;
264 // Get the size the View would like to be, if enough space were available.
265 virtual gfx::Size GetPreferredSize();
267 // Convenience method that sizes this view to its preferred size.
268 void SizeToPreferredSize();
270 // Gets the minimum size of the view. View's implementation invokes
271 // GetPreferredSize.
272 virtual gfx::Size GetMinimumSize();
274 // Gets the maximum size of the view. Currently only used for sizing shell
275 // windows.
276 virtual gfx::Size GetMaximumSize();
278 // Return the height necessary to display this view with the provided width.
279 // View's implementation returns the value from getPreferredSize.cy.
280 // Override if your View's preferred height depends upon the width (such
281 // as with Labels).
282 virtual int GetHeightForWidth(int w);
284 // Set whether this view is visible. Painting is scheduled as needed.
285 virtual void SetVisible(bool visible);
287 // Return whether a view is visible
288 bool visible() const { return visible_; }
290 // Returns true if this view is drawn on screen.
291 virtual bool IsDrawn() const;
293 // Set whether this view is enabled. A disabled view does not receive keyboard
294 // or mouse inputs. If |enabled| differs from the current value, SchedulePaint
295 // is invoked.
296 void SetEnabled(bool enabled);
298 // Returns whether the view is enabled.
299 bool enabled() const { return enabled_; }
301 // This indicates that the view completely fills its bounds in an opaque
302 // color. This doesn't affect compositing but is a hint to the compositor to
303 // optimize painting.
304 // Note that this method does not implicitly create a layer if one does not
305 // already exist for the View, but is a no-op in that case.
306 void SetFillsBoundsOpaquely(bool fills_bounds_opaquely);
308 // Transformations -----------------------------------------------------------
310 // Methods for setting transformations for a view (e.g. rotation, scaling).
312 gfx::Transform GetTransform() const;
314 // Clipping parameters. Clipping is done relative to the view bounds.
315 void set_clip_insets(gfx::Insets clip_insets) { clip_insets_ = clip_insets; }
317 // Sets the transform to the supplied transform.
318 void SetTransform(const gfx::Transform& transform);
320 // Sets whether this view paints to a layer. A view paints to a layer if
321 // either of the following are true:
322 // . the view has a non-identity transform.
323 // . SetPaintToLayer(true) has been invoked.
324 // View creates the Layer only when it exists in a Widget with a non-NULL
325 // Compositor.
326 void SetPaintToLayer(bool paint_to_layer);
328 // RTL positioning -----------------------------------------------------------
330 // Methods for accessing the bounds and position of the view, relative to its
331 // parent. The position returned is mirrored if the parent view is using a RTL
332 // layout.
334 // NOTE: in the vast majority of the cases, the mirroring implementation is
335 // transparent to the View subclasses and therefore you should use the
336 // bounds() accessor instead.
337 gfx::Rect GetMirroredBounds() const;
338 gfx::Point GetMirroredPosition() const;
339 int GetMirroredX() const;
341 // Given a rectangle specified in this View's coordinate system, the function
342 // computes the 'left' value for the mirrored rectangle within this View. If
343 // the View's UI layout is not right-to-left, then bounds.x() is returned.
345 // UI mirroring is transparent to most View subclasses and therefore there is
346 // no need to call this routine from anywhere within your subclass
347 // implementation.
348 int GetMirroredXForRect(const gfx::Rect& rect) const;
350 // Given the X coordinate of a point inside the View, this function returns
351 // the mirrored X coordinate of the point if the View's UI layout is
352 // right-to-left. If the layout is left-to-right, the same X coordinate is
353 // returned.
355 // Following are a few examples of the values returned by this function for
356 // a View with the bounds {0, 0, 100, 100} and a right-to-left layout:
358 // GetMirroredXCoordinateInView(0) -> 100
359 // GetMirroredXCoordinateInView(20) -> 80
360 // GetMirroredXCoordinateInView(99) -> 1
361 int GetMirroredXInView(int x) const;
363 // Given a X coordinate and a width inside the View, this function returns
364 // the mirrored X coordinate if the View's UI layout is right-to-left. If the
365 // layout is left-to-right, the same X coordinate is returned.
367 // Following are a few examples of the values returned by this function for
368 // a View with the bounds {0, 0, 100, 100} and a right-to-left layout:
370 // GetMirroredXCoordinateInView(0, 10) -> 90
371 // GetMirroredXCoordinateInView(20, 20) -> 60
372 int GetMirroredXWithWidthInView(int x, int w) const;
374 // Layout --------------------------------------------------------------------
376 // Lay out the child Views (set their bounds based on sizing heuristics
377 // specific to the current Layout Manager)
378 virtual void Layout();
380 // TODO(beng): I think we should remove this.
381 // Mark this view and all parents to require a relayout. This ensures the
382 // next call to Layout() will propagate to this view, even if the bounds of
383 // parent views do not change.
384 void InvalidateLayout();
386 // Gets/Sets the Layout Manager used by this view to size and place its
387 // children.
388 // The LayoutManager is owned by the View and is deleted when the view is
389 // deleted, or when a new LayoutManager is installed.
390 LayoutManager* GetLayoutManager() const;
391 void SetLayoutManager(LayoutManager* layout);
393 // Attributes ----------------------------------------------------------------
395 // The view class name.
396 static const char kViewClassName[];
398 // Return the receiving view's class name. A view class is a string which
399 // uniquely identifies the view class. It is intended to be used as a way to
400 // find out during run time if a view can be safely casted to a specific view
401 // subclass. The default implementation returns kViewClassName.
402 virtual const char* GetClassName() const;
404 // Returns the first ancestor, starting at this, whose class name is |name|.
405 // Returns null if no ancestor has the class name |name|.
406 const View* GetAncestorWithClassName(const std::string& name) const;
407 View* GetAncestorWithClassName(const std::string& name);
409 // Recursively descends the view tree starting at this view, and returns
410 // the first child that it encounters that has the given ID.
411 // Returns NULL if no matching child view is found.
412 virtual const View* GetViewByID(int id) const;
413 virtual View* GetViewByID(int id);
415 // Gets and sets the ID for this view. ID should be unique within the subtree
416 // that you intend to search for it. 0 is the default ID for views.
417 int id() const { return id_; }
418 void set_id(int id) { id_ = id; }
420 // A group id is used to tag views which are part of the same logical group.
421 // Focus can be moved between views with the same group using the arrow keys.
422 // Groups are currently used to implement radio button mutual exclusion.
423 // The group id is immutable once it's set.
424 void SetGroup(int gid);
425 // Returns the group id of the view, or -1 if the id is not set yet.
426 int GetGroup() const;
428 // If this returns true, the views from the same group can each be focused
429 // when moving focus with the Tab/Shift-Tab key. If this returns false,
430 // only the selected view from the group (obtained with
431 // GetSelectedViewForGroup()) is focused.
432 virtual bool IsGroupFocusTraversable() const;
434 // Fills |views| with all the available views which belong to the provided
435 // |group|.
436 void GetViewsInGroup(int group, Views* views);
438 // Returns the View that is currently selected in |group|.
439 // The default implementation simply returns the first View found for that
440 // group.
441 virtual View* GetSelectedViewForGroup(int group);
443 // Coordinate conversion -----------------------------------------------------
445 // Note that the utility coordinate conversions functions always operate on
446 // the mirrored position of the child Views if the parent View uses a
447 // right-to-left UI layout.
449 // Convert a point from the coordinate system of one View to another.
451 // |source| and |target| must be in the same widget, but doesn't need to be in
452 // the same view hierarchy.
453 // Neither |source| nor |target| can be NULL.
454 static void ConvertPointToTarget(const View* source,
455 const View* target,
456 gfx::Point* point);
458 // Convert |rect| from the coordinate system of |source| to the coordinate
459 // system of |target|.
461 // |source| and |target| must be in the same widget, but doesn't need to be in
462 // the same view hierarchy.
463 // Neither |source| nor |target| can be NULL.
464 static void ConvertRectToTarget(const View* source,
465 const View* target,
466 gfx::RectF* rect);
468 // Convert a point from a View's coordinate system to that of its Widget.
469 static void ConvertPointToWidget(const View* src, gfx::Point* point);
471 // Convert a point from the coordinate system of a View's Widget to that
472 // View's coordinate system.
473 static void ConvertPointFromWidget(const View* dest, gfx::Point* p);
475 // Convert a point from a View's coordinate system to that of the screen.
476 static void ConvertPointToScreen(const View* src, gfx::Point* point);
478 // Convert a point from a View's coordinate system to that of the screen.
479 static void ConvertPointFromScreen(const View* dst, gfx::Point* point);
481 // Applies transformation on the rectangle, which is in the view's coordinate
482 // system, to convert it into the parent's coordinate system.
483 gfx::Rect ConvertRectToParent(const gfx::Rect& rect) const;
485 // Converts a rectangle from this views coordinate system to its widget
486 // coordinate system.
487 gfx::Rect ConvertRectToWidget(const gfx::Rect& rect) const;
489 // Painting ------------------------------------------------------------------
491 // Mark all or part of the View's bounds as dirty (needing repaint).
492 // |r| is in the View's coordinates.
493 // Rectangle |r| should be in the view's coordinate system. The
494 // transformations are applied to it to convert it into the parent coordinate
495 // system before propagating SchedulePaint up the view hierarchy.
496 // TODO(beng): Make protected.
497 virtual void SchedulePaint();
498 virtual void SchedulePaintInRect(const gfx::Rect& r);
500 // Called by the framework to paint a View. Performs translation and clipping
501 // for View coordinates and language direction as required, allows the View
502 // to paint itself via the various OnPaint*() event handlers and then paints
503 // the hierarchy beneath it.
504 virtual void Paint(gfx::Canvas* canvas);
506 // The background object is owned by this object and may be NULL.
507 void set_background(Background* b);
508 const Background* background() const { return background_.get(); }
509 Background* background() { return background_.get(); }
511 // The border object is owned by this object and may be NULL.
512 void SetBorder(scoped_ptr<Border> b);
513 const Border* border() const { return border_.get(); }
514 Border* border() { return border_.get(); }
516 // Get the theme provider from the parent widget.
517 ui::ThemeProvider* GetThemeProvider() const;
519 // Returns the NativeTheme to use for this View. This calls through to
520 // GetNativeTheme() on the Widget this View is in. If this View is not in a
521 // Widget this returns ui::NativeTheme::instance().
522 ui::NativeTheme* GetNativeTheme() {
523 return const_cast<ui::NativeTheme*>(
524 const_cast<const View*>(this)->GetNativeTheme());
526 const ui::NativeTheme* GetNativeTheme() const;
528 // RTL painting --------------------------------------------------------------
530 // This method determines whether the gfx::Canvas object passed to
531 // View::Paint() needs to be transformed such that anything drawn on the
532 // canvas object during View::Paint() is flipped horizontally.
534 // By default, this function returns false (which is the initial value of
535 // |flip_canvas_on_paint_for_rtl_ui_|). View subclasses that need to paint on
536 // a flipped gfx::Canvas when the UI layout is right-to-left need to call
537 // EnableCanvasFlippingForRTLUI().
538 bool FlipCanvasOnPaintForRTLUI() const {
539 return flip_canvas_on_paint_for_rtl_ui_ ? base::i18n::IsRTL() : false;
542 // Enables or disables flipping of the gfx::Canvas during View::Paint().
543 // Note that if canvas flipping is enabled, the canvas will be flipped only
544 // if the UI layout is right-to-left; that is, the canvas will be flipped
545 // only if base::i18n::IsRTL() returns true.
547 // Enabling canvas flipping is useful for leaf views that draw an image that
548 // needs to be flipped horizontally when the UI layout is right-to-left
549 // (views::Button, for example). This method is helpful for such classes
550 // because their drawing logic stays the same and they can become agnostic to
551 // the UI directionality.
552 void EnableCanvasFlippingForRTLUI(bool enable) {
553 flip_canvas_on_paint_for_rtl_ui_ = enable;
556 // Input ---------------------------------------------------------------------
557 // The points, rects, mouse locations, and touch locations in the following
558 // functions are in the view's coordinates, except for a RootView.
560 // Convenience functions which calls into GetEventHandler() with
561 // a 1x1 rect centered at |point|.
562 View* GetEventHandlerForPoint(const gfx::Point& point);
564 // If point-based targeting should be used, return the deepest visible
565 // descendant that contains the center point of |rect|.
566 // If rect-based targeting (i.e., fuzzing) should be used, return the
567 // closest visible descendant having at least kRectTargetOverlap of
568 // its area covered by |rect|. If no such descendant exists, return the
569 // deepest visible descendant that contains the center point of |rect|.
570 // See http://goo.gl/3Jp2BD for more information about rect-based targeting.
571 virtual View* GetEventHandlerForRect(const gfx::Rect& rect);
573 // Returns the deepest visible descendant that contains the specified point
574 // and supports tooltips. If the view does not contain the point, returns
575 // NULL.
576 virtual View* GetTooltipHandlerForPoint(const gfx::Point& point);
578 // Return the cursor that should be used for this view or the default cursor.
579 // The event location is in the receiver's coordinate system. The caller is
580 // responsible for managing the lifetime of the returned object, though that
581 // lifetime may vary from platform to platform. On Windows and Aura,
582 // the cursor is a shared resource.
583 virtual gfx::NativeCursor GetCursor(const ui::MouseEvent& event);
585 // A convenience function which calls HitTestRect() with a rect of size
586 // 1x1 and an origin of |point|.
587 bool HitTestPoint(const gfx::Point& point) const;
589 // Tests whether |rect| intersects this view's bounds.
590 virtual bool HitTestRect(const gfx::Rect& rect) const;
592 // Returns true if the mouse cursor is over |view| and mouse events are
593 // enabled.
594 bool IsMouseHovered();
596 // This method is invoked when the user clicks on this view.
597 // The provided event is in the receiver's coordinate system.
599 // Return true if you processed the event and want to receive subsequent
600 // MouseDraggged and MouseReleased events. This also stops the event from
601 // bubbling. If you return false, the event will bubble through parent
602 // views.
604 // If you remove yourself from the tree while processing this, event bubbling
605 // stops as if you returned true, but you will not receive future events.
606 // The return value is ignored in this case.
608 // Default implementation returns true if a ContextMenuController has been
609 // set, false otherwise. Override as needed.
611 virtual bool OnMousePressed(const ui::MouseEvent& event);
613 // This method is invoked when the user clicked on this control.
614 // and is still moving the mouse with a button pressed.
615 // The provided event is in the receiver's coordinate system.
617 // Return true if you processed the event and want to receive
618 // subsequent MouseDragged and MouseReleased events.
620 // Default implementation returns true if a ContextMenuController has been
621 // set, false otherwise. Override as needed.
623 virtual bool OnMouseDragged(const ui::MouseEvent& event);
625 // This method is invoked when the user releases the mouse
626 // button. The event is in the receiver's coordinate system.
628 // Default implementation notifies the ContextMenuController is appropriate.
629 // Subclasses that wish to honor the ContextMenuController should invoke
630 // super.
631 virtual void OnMouseReleased(const ui::MouseEvent& event);
633 // This method is invoked when the mouse press/drag was canceled by a
634 // system/user gesture.
635 virtual void OnMouseCaptureLost();
637 // This method is invoked when the mouse is above this control
638 // The event is in the receiver's coordinate system.
640 // Default implementation does nothing. Override as needed.
641 virtual void OnMouseMoved(const ui::MouseEvent& event);
643 // This method is invoked when the mouse enters this control.
645 // Default implementation does nothing. Override as needed.
646 virtual void OnMouseEntered(const ui::MouseEvent& event);
648 // This method is invoked when the mouse exits this control
649 // The provided event location is always (0, 0)
650 // Default implementation does nothing. Override as needed.
651 virtual void OnMouseExited(const ui::MouseEvent& event);
653 // Set the MouseHandler for a drag session.
655 // A drag session is a stream of mouse events starting
656 // with a MousePressed event, followed by several MouseDragged
657 // events and finishing with a MouseReleased event.
659 // This method should be only invoked while processing a
660 // MouseDragged or MousePressed event.
662 // All further mouse dragged and mouse up events will be sent
663 // the MouseHandler, even if it is reparented to another window.
665 // The MouseHandler is automatically cleared when the control
666 // comes back from processing the MouseReleased event.
668 // Note: if the mouse handler is no longer connected to a
669 // view hierarchy, events won't be sent.
671 // TODO(sky): rename this.
672 virtual void SetMouseHandler(View* new_mouse_handler);
674 // Invoked when a key is pressed or released.
675 // Subclasser should return true if the event has been processed and false
676 // otherwise. If the event has not been processed, the parent will be given a
677 // chance.
678 virtual bool OnKeyPressed(const ui::KeyEvent& event);
679 virtual bool OnKeyReleased(const ui::KeyEvent& event);
681 // Invoked when the user uses the mousewheel. Implementors should return true
682 // if the event has been processed and false otherwise. This message is sent
683 // if the view is focused. If the event has not been processed, the parent
684 // will be given a chance.
685 virtual bool OnMouseWheel(const ui::MouseWheelEvent& event);
688 // See field for description.
689 void set_notify_enter_exit_on_child(bool notify) {
690 notify_enter_exit_on_child_ = notify;
692 bool notify_enter_exit_on_child() const {
693 return notify_enter_exit_on_child_;
696 // Returns the View's TextInputClient instance or NULL if the View doesn't
697 // support text input.
698 virtual ui::TextInputClient* GetTextInputClient();
700 // Convenience method to retrieve the InputMethod associated with the
701 // Widget that contains this view. Returns NULL if this view is not part of a
702 // view hierarchy with a Widget.
703 virtual InputMethod* GetInputMethod();
704 virtual const InputMethod* GetInputMethod() const;
706 // Sets a new event-targeter for the view, and returns the previous
707 // event-targeter.
708 scoped_ptr<ui::EventTargeter> SetEventTargeter(
709 scoped_ptr<ui::EventTargeter> targeter);
711 // Overridden from ui::EventTarget:
712 virtual bool CanAcceptEvent(const ui::Event& event) OVERRIDE;
713 virtual ui::EventTarget* GetParentTarget() OVERRIDE;
714 virtual scoped_ptr<ui::EventTargetIterator> GetChildIterator() const OVERRIDE;
715 virtual ui::EventTargeter* GetEventTargeter() OVERRIDE;
717 // Overridden from ui::EventHandler:
718 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE;
719 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE;
720 virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE;
721 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE FINAL;
722 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
724 // Accelerators --------------------------------------------------------------
726 // Sets a keyboard accelerator for that view. When the user presses the
727 // accelerator key combination, the AcceleratorPressed method is invoked.
728 // Note that you can set multiple accelerators for a view by invoking this
729 // method several times. Note also that AcceleratorPressed is invoked only
730 // when CanHandleAccelerators() is true.
731 virtual void AddAccelerator(const ui::Accelerator& accelerator);
733 // Removes the specified accelerator for this view.
734 virtual void RemoveAccelerator(const ui::Accelerator& accelerator);
736 // Removes all the keyboard accelerators for this view.
737 virtual void ResetAccelerators();
739 // Overridden from AcceleratorTarget:
740 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
742 // Returns whether accelerators are enabled for this view. Accelerators are
743 // enabled if the containing widget is visible and the view is enabled() and
744 // IsDrawn()
745 virtual bool CanHandleAccelerators() const OVERRIDE;
747 // Focus ---------------------------------------------------------------------
749 // Returns whether this view currently has the focus.
750 virtual bool HasFocus() const;
752 // Returns the view that should be selected next when pressing Tab.
753 View* GetNextFocusableView();
754 const View* GetNextFocusableView() const;
756 // Returns the view that should be selected next when pressing Shift-Tab.
757 View* GetPreviousFocusableView();
759 // Sets the component that should be selected next when pressing Tab, and
760 // makes the current view the precedent view of the specified one.
761 // Note that by default views are linked in the order they have been added to
762 // their container. Use this method if you want to modify the order.
763 // IMPORTANT NOTE: loops in the focus hierarchy are not supported.
764 void SetNextFocusableView(View* view);
766 // Sets whether this view is capable of taking focus.
767 // Note that this is false by default so that a view used as a container does
768 // not get the focus.
769 void SetFocusable(bool focusable);
771 // Returns true if this view is |focusable_|, |enabled_| and drawn.
772 virtual bool IsFocusable() const;
774 // Return whether this view is focusable when the user requires full keyboard
775 // access, even though it may not be normally focusable.
776 bool IsAccessibilityFocusable() const;
778 // Set whether this view can be made focusable if the user requires
779 // full keyboard access, even though it's not normally focusable.
780 // Note that this is false by default.
781 void SetAccessibilityFocusable(bool accessibility_focusable);
783 // Convenience method to retrieve the FocusManager associated with the
784 // Widget that contains this view. This can return NULL if this view is not
785 // part of a view hierarchy with a Widget.
786 virtual FocusManager* GetFocusManager();
787 virtual const FocusManager* GetFocusManager() const;
789 // Request keyboard focus. The receiving view will become the focused view.
790 virtual void RequestFocus();
792 // Invoked when a view is about to be requested for focus due to the focus
793 // traversal. Reverse is this request was generated going backward
794 // (Shift-Tab).
795 virtual void AboutToRequestFocusFromTabTraversal(bool reverse) {}
797 // Invoked when a key is pressed before the key event is processed (and
798 // potentially eaten) by the focus manager for tab traversal, accelerators and
799 // other focus related actions.
800 // The default implementation returns false, ensuring that tab traversal and
801 // accelerators processing is performed.
802 // Subclasses should return true if they want to process the key event and not
803 // have it processed as an accelerator (if any) or as a tab traversal (if the
804 // key event is for the TAB key). In that case, OnKeyPressed will
805 // subsequently be invoked for that event.
806 virtual bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& event);
808 // Subclasses that contain traversable children that are not directly
809 // accessible through the children hierarchy should return the associated
810 // FocusTraversable for the focus traversal to work properly.
811 virtual FocusTraversable* GetFocusTraversable();
813 // Subclasses that can act as a "pane" must implement their own
814 // FocusTraversable to keep the focus trapped within the pane.
815 // If this method returns an object, any view that's a direct or
816 // indirect child of this view will always use this FocusTraversable
817 // rather than the one from the widget.
818 virtual FocusTraversable* GetPaneFocusTraversable();
820 // Tooltips ------------------------------------------------------------------
822 // Gets the tooltip for this View. If the View does not have a tooltip,
823 // return false. If the View does have a tooltip, copy the tooltip into
824 // the supplied string and return true.
825 // Any time the tooltip text that a View is displaying changes, it must
826 // invoke TooltipTextChanged.
827 // |p| provides the coordinates of the mouse (relative to this view).
828 virtual bool GetTooltipText(const gfx::Point& p,
829 base::string16* tooltip) const;
831 // Returns the location (relative to this View) for the text on the tooltip
832 // to display. If false is returned (the default), the tooltip is placed at
833 // a default position.
834 virtual bool GetTooltipTextOrigin(const gfx::Point& p, gfx::Point* loc) const;
836 // Context menus -------------------------------------------------------------
838 // Sets the ContextMenuController. Setting this to non-null makes the View
839 // process mouse events.
840 ContextMenuController* context_menu_controller() {
841 return context_menu_controller_;
843 void set_context_menu_controller(ContextMenuController* menu_controller) {
844 context_menu_controller_ = menu_controller;
847 // Provides default implementation for context menu handling. The default
848 // implementation calls the ShowContextMenu of the current
849 // ContextMenuController (if it is not NULL). Overridden in subclassed views
850 // to provide right-click menu display triggerd by the keyboard (i.e. for the
851 // Chrome toolbar Back and Forward buttons). No source needs to be specified,
852 // as it is always equal to the current View.
853 virtual void ShowContextMenu(const gfx::Point& p,
854 ui::MenuSourceType source_type);
856 // On some platforms, we show context menu on mouse press instead of release.
857 // This method returns true for those platforms.
858 static bool ShouldShowContextMenuOnMousePress();
860 // Drag and drop -------------------------------------------------------------
862 DragController* drag_controller() { return drag_controller_; }
863 void set_drag_controller(DragController* drag_controller) {
864 drag_controller_ = drag_controller;
867 // During a drag and drop session when the mouse moves the view under the
868 // mouse is queried for the drop types it supports by way of the
869 // GetDropFormats methods. If the view returns true and the drag site can
870 // provide data in one of the formats, the view is asked if the drop data
871 // is required before any other drop events are sent. Once the
872 // data is available the view is asked if it supports the drop (by way of
873 // the CanDrop method). If a view returns true from CanDrop,
874 // OnDragEntered is sent to the view when the mouse first enters the view,
875 // as the mouse moves around within the view OnDragUpdated is invoked.
876 // If the user releases the mouse over the view and OnDragUpdated returns a
877 // valid drop, then OnPerformDrop is invoked. If the mouse moves outside the
878 // view or over another view that wants the drag, OnDragExited is invoked.
880 // Similar to mouse events, the deepest view under the mouse is first checked
881 // if it supports the drop (Drop). If the deepest view under
882 // the mouse does not support the drop, the ancestors are walked until one
883 // is found that supports the drop.
885 // Override and return the set of formats that can be dropped on this view.
886 // |formats| is a bitmask of the formats defined bye OSExchangeData::Format.
887 // The default implementation returns false, which means the view doesn't
888 // support dropping.
889 virtual bool GetDropFormats(
890 int* formats,
891 std::set<OSExchangeData::CustomFormat>* custom_formats);
893 // Override and return true if the data must be available before any drop
894 // methods should be invoked. The default is false.
895 virtual bool AreDropTypesRequired();
897 // A view that supports drag and drop must override this and return true if
898 // data contains a type that may be dropped on this view.
899 virtual bool CanDrop(const OSExchangeData& data);
901 // OnDragEntered is invoked when the mouse enters this view during a drag and
902 // drop session and CanDrop returns true. This is immediately
903 // followed by an invocation of OnDragUpdated, and eventually one of
904 // OnDragExited or OnPerformDrop.
905 virtual void OnDragEntered(const ui::DropTargetEvent& event);
907 // Invoked during a drag and drop session while the mouse is over the view.
908 // This should return a bitmask of the DragDropTypes::DragOperation supported
909 // based on the location of the event. Return 0 to indicate the drop should
910 // not be accepted.
911 virtual int OnDragUpdated(const ui::DropTargetEvent& event);
913 // Invoked during a drag and drop session when the mouse exits the views, or
914 // when the drag session was canceled and the mouse was over the view.
915 virtual void OnDragExited();
917 // Invoked during a drag and drop session when OnDragUpdated returns a valid
918 // operation and the user release the mouse.
919 virtual int OnPerformDrop(const ui::DropTargetEvent& event);
921 // Invoked from DoDrag after the drag completes. This implementation does
922 // nothing, and is intended for subclasses to do cleanup.
923 virtual void OnDragDone();
925 // Returns true if the mouse was dragged enough to start a drag operation.
926 // delta_x and y are the distance the mouse was dragged.
927 static bool ExceededDragThreshold(const gfx::Vector2d& delta);
929 // Accessibility -------------------------------------------------------------
931 // Modifies |state| to reflect the current accessible state of this view.
932 virtual void GetAccessibleState(ui::AXViewState* state) { }
934 // Returns an instance of the native accessibility interface for this view.
935 virtual gfx::NativeViewAccessible GetNativeViewAccessible();
937 // Notifies assistive technology that an accessibility event has
938 // occurred on this view, such as when the view is focused or when its
939 // value changes. Pass true for |send_native_event| except for rare
940 // cases where the view is a native control that's already sending a
941 // native accessibility event and the duplicate event would cause
942 // problems.
943 void NotifyAccessibilityEvent(ui::AXEvent event_type,
944 bool send_native_event);
946 // Scrolling -----------------------------------------------------------------
947 // TODO(beng): Figure out if this can live somewhere other than View, i.e.
948 // closer to ScrollView.
950 // Scrolls the specified region, in this View's coordinate system, to be
951 // visible. View's implementation passes the call onto the parent View (after
952 // adjusting the coordinates). It is up to views that only show a portion of
953 // the child view, such as Viewport, to override appropriately.
954 virtual void ScrollRectToVisible(const gfx::Rect& rect);
956 // The following methods are used by ScrollView to determine the amount
957 // to scroll relative to the visible bounds of the view. For example, a
958 // return value of 10 indicates the scrollview should scroll 10 pixels in
959 // the appropriate direction.
961 // Each method takes the following parameters:
963 // is_horizontal: if true, scrolling is along the horizontal axis, otherwise
964 // the vertical axis.
965 // is_positive: if true, scrolling is by a positive amount. Along the
966 // vertical axis scrolling by a positive amount equates to
967 // scrolling down.
969 // The return value should always be positive and gives the number of pixels
970 // to scroll. ScrollView interprets a return value of 0 (or negative)
971 // to scroll by a default amount.
973 // See VariableRowHeightScrollHelper and FixedRowHeightScrollHelper for
974 // implementations of common cases.
975 virtual int GetPageScrollIncrement(ScrollView* scroll_view,
976 bool is_horizontal, bool is_positive);
977 virtual int GetLineScrollIncrement(ScrollView* scroll_view,
978 bool is_horizontal, bool is_positive);
980 protected:
981 // Used to track a drag. RootView passes this into
982 // ProcessMousePressed/Dragged.
983 struct DragInfo {
984 // Sets possible_drag to false and start_x/y to 0. This is invoked by
985 // RootView prior to invoke ProcessMousePressed.
986 void Reset();
988 // Sets possible_drag to true and start_pt to the specified point.
989 // This is invoked by the target view if it detects the press may generate
990 // a drag.
991 void PossibleDrag(const gfx::Point& p);
993 // Whether the press may generate a drag.
994 bool possible_drag;
996 // Coordinates of the mouse press.
997 gfx::Point start_pt;
1000 // Size and disposition ------------------------------------------------------
1002 // Override to be notified when the bounds of the view have changed.
1003 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds);
1005 // Called when the preferred size of a child view changed. This gives the
1006 // parent an opportunity to do a fresh layout if that makes sense.
1007 virtual void ChildPreferredSizeChanged(View* child) {}
1009 // Called when the visibility of a child view changed. This gives the parent
1010 // an opportunity to do a fresh layout if that makes sense.
1011 virtual void ChildVisibilityChanged(View* child) {}
1013 // Invalidates the layout and calls ChildPreferredSizeChanged on the parent
1014 // if there is one. Be sure to call View::PreferredSizeChanged when
1015 // overriding such that the layout is properly invalidated.
1016 virtual void PreferredSizeChanged();
1018 // Override returning true when the view needs to be notified when its visible
1019 // bounds relative to the root view may have changed. Only used by
1020 // NativeViewHost.
1021 virtual bool NeedsNotificationWhenVisibleBoundsChange() const;
1023 // Notification that this View's visible bounds relative to the root view may
1024 // have changed. The visible bounds are the region of the View not clipped by
1025 // its ancestors. This is used for clipping NativeViewHost.
1026 virtual void OnVisibleBoundsChanged();
1028 // Override to be notified when the enabled state of this View has
1029 // changed. The default implementation calls SchedulePaint() on this View.
1030 virtual void OnEnabledChanged();
1032 bool needs_layout() const { return needs_layout_; }
1034 // Tree operations -----------------------------------------------------------
1036 // This method is invoked when the tree changes.
1038 // When a view is removed, it is invoked for all children and grand
1039 // children. For each of these views, a notification is sent to the
1040 // view and all parents.
1042 // When a view is added, a notification is sent to the view, all its
1043 // parents, and all its children (and grand children)
1045 // Default implementation does nothing. Override to perform operations
1046 // required when a view is added or removed from a view hierarchy
1048 // Refer to comments in struct |ViewHierarchyChangedDetails| for |details|.
1049 virtual void ViewHierarchyChanged(const ViewHierarchyChangedDetails& details);
1051 // When SetVisible() changes the visibility of a view, this method is
1052 // invoked for that view as well as all the children recursively.
1053 virtual void VisibilityChanged(View* starting_from, bool is_visible);
1055 // This method is invoked when the parent NativeView of the widget that the
1056 // view is attached to has changed and the view hierarchy has not changed.
1057 // ViewHierarchyChanged() is called when the parent NativeView of the widget
1058 // that the view is attached to is changed as a result of changing the view
1059 // hierarchy. Overriding this method is useful for tracking which
1060 // FocusManager manages this view.
1061 virtual void NativeViewHierarchyChanged();
1063 // Painting ------------------------------------------------------------------
1065 // Responsible for calling Paint() on child Views. Override to control the
1066 // order child Views are painted.
1067 virtual void PaintChildren(gfx::Canvas* canvas);
1069 // Override to provide rendering in any part of the View's bounds. Typically
1070 // this is the "contents" of the view. If you override this method you will
1071 // have to call the subsequent OnPaint*() methods manually.
1072 virtual void OnPaint(gfx::Canvas* canvas);
1074 // Override to paint a background before any content is drawn. Typically this
1075 // is done if you are satisfied with a default OnPaint handler but wish to
1076 // supply a different background.
1077 virtual void OnPaintBackground(gfx::Canvas* canvas);
1079 // Override to paint a border not specified by SetBorder().
1080 virtual void OnPaintBorder(gfx::Canvas* canvas);
1082 // Accelerated painting ------------------------------------------------------
1084 // Returns the offset from this view to the nearest ancestor with a layer. If
1085 // |layer_parent| is non-NULL it is set to the nearest ancestor with a layer.
1086 virtual gfx::Vector2d CalculateOffsetToAncestorWithLayer(
1087 ui::Layer** layer_parent);
1089 // Updates the view's layer's parent. Called when a view is added to a view
1090 // hierarchy, responsible for parenting the view's layer to the enclosing
1091 // layer in the hierarchy.
1092 virtual void UpdateParentLayer();
1094 // If this view has a layer, the layer is reparented to |parent_layer| and its
1095 // bounds is set based on |point|. If this view does not have a layer, then
1096 // recurses through all children. This is used when adding a layer to an
1097 // existing view to make sure all descendants that have layers are parented to
1098 // the right layer.
1099 void MoveLayerToParent(ui::Layer* parent_layer, const gfx::Point& point);
1101 // Called to update the bounds of any child layers within this View's
1102 // hierarchy when something happens to the hierarchy.
1103 void UpdateChildLayerBounds(const gfx::Vector2d& offset);
1105 // Overridden from ui::LayerDelegate:
1106 virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE;
1107 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE;
1108 virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE;
1110 // Finds the layer that this view paints to (it may belong to an ancestor
1111 // view), then reorders the immediate children of that layer to match the
1112 // order of the view tree.
1113 virtual void ReorderLayers();
1115 // This reorders the immediate children of |*parent_layer| to match the
1116 // order of the view tree. Child layers which are owned by a view are
1117 // reordered so that they are below any child layers not owned by a view.
1118 // Widget::ReorderNativeViews() should be called to reorder any child layers
1119 // with an associated view. Widget::ReorderNativeViews() may reorder layers
1120 // below layers owned by a view.
1121 virtual void ReorderChildLayers(ui::Layer* parent_layer);
1123 // Input ---------------------------------------------------------------------
1125 // Called by HitTestRect() to see if this View has a custom hit test mask. If
1126 // the return value is true, GetHitTestMask() will be called to obtain the
1127 // mask. Default value is false, in which case the View will hit-test against
1128 // its bounds.
1129 virtual bool HasHitTestMask() const;
1131 // Called by HitTestRect() to retrieve a mask for hit-testing against.
1132 // Subclasses override to provide custom shaped hit test regions.
1133 virtual void GetHitTestMask(HitTestSource source, gfx::Path* mask) const;
1135 virtual DragInfo* GetDragInfo();
1137 // Focus ---------------------------------------------------------------------
1139 // Returns last value passed to SetFocusable(). Use IsFocusable() to determine
1140 // if a view can take focus right now.
1141 bool focusable() const { return focusable_; }
1143 // Override to be notified when focus has changed either to or from this View.
1144 virtual void OnFocus();
1145 virtual void OnBlur();
1147 // Handle view focus/blur events for this view.
1148 void Focus();
1149 void Blur();
1151 // System events -------------------------------------------------------------
1153 // Called when the UI theme (not the NativeTheme) has changed, overriding
1154 // allows individual Views to do special cleanup and processing (such as
1155 // dropping resource caches). To dispatch a theme changed notification, call
1156 // Widget::ThemeChanged().
1157 virtual void OnThemeChanged() {}
1159 // Called when the locale has changed, overriding allows individual Views to
1160 // update locale-dependent strings.
1161 // To dispatch a locale changed notification, call Widget::LocaleChanged().
1162 virtual void OnLocaleChanged() {}
1164 // Tooltips ------------------------------------------------------------------
1166 // Views must invoke this when the tooltip text they are to display changes.
1167 void TooltipTextChanged();
1169 // Context menus -------------------------------------------------------------
1171 // Returns the location, in screen coordinates, to show the context menu at
1172 // when the context menu is shown from the keyboard. This implementation
1173 // returns the middle of the visible region of this view.
1175 // This method is invoked when the context menu is shown by way of the
1176 // keyboard.
1177 virtual gfx::Point GetKeyboardContextMenuLocation();
1179 // Drag and drop -------------------------------------------------------------
1181 // These are cover methods that invoke the method of the same name on
1182 // the DragController. Subclasses may wish to override rather than install
1183 // a DragController.
1184 // See DragController for a description of these methods.
1185 virtual int GetDragOperations(const gfx::Point& press_pt);
1186 virtual void WriteDragData(const gfx::Point& press_pt, OSExchangeData* data);
1188 // Returns whether we're in the middle of a drag session that was initiated
1189 // by us.
1190 bool InDrag();
1192 // Returns how much the mouse needs to move in one direction to start a
1193 // drag. These methods cache in a platform-appropriate way. These values are
1194 // used by the public static method ExceededDragThreshold().
1195 static int GetHorizontalDragThreshold();
1196 static int GetVerticalDragThreshold();
1198 // NativeTheme ---------------------------------------------------------------
1200 // Invoked when the NativeTheme associated with this View changes.
1201 virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) {}
1203 // Debugging -----------------------------------------------------------------
1205 #if !defined(NDEBUG)
1206 // Returns string containing a graph of the views hierarchy in graphViz DOT
1207 // language (http://graphviz.org/). Can be called within debugger and save
1208 // to a file to compile/view.
1209 // Note: Assumes initial call made with first = true.
1210 virtual std::string PrintViewGraph(bool first);
1212 // Some classes may own an object which contains the children to displayed in
1213 // the views hierarchy. The above function gives the class the flexibility to
1214 // decide which object should be used to obtain the children, but this
1215 // function makes the decision explicit.
1216 std::string DoPrintViewGraph(bool first, View* view_with_children);
1217 #endif
1219 private:
1220 friend class internal::PreEventDispatchHandler;
1221 friend class internal::PostEventDispatchHandler;
1222 friend class internal::RootView;
1223 friend class FocusManager;
1224 friend class Widget;
1226 // Painting -----------------------------------------------------------------
1228 enum SchedulePaintType {
1229 // Indicates the size is the same (only the origin changed).
1230 SCHEDULE_PAINT_SIZE_SAME,
1232 // Indicates the size changed (and possibly the origin).
1233 SCHEDULE_PAINT_SIZE_CHANGED
1236 // Invoked before and after the bounds change to schedule painting the old and
1237 // new bounds.
1238 void SchedulePaintBoundsChanged(SchedulePaintType type);
1240 // Common Paint() code shared by accelerated and non-accelerated code paths to
1241 // invoke OnPaint() on the View.
1242 void PaintCommon(gfx::Canvas* canvas);
1244 // Tree operations -----------------------------------------------------------
1246 // Removes |view| from the hierarchy tree. If |update_focus_cycle| is true,
1247 // the next and previous focusable views of views pointing to this view are
1248 // updated. If |update_tool_tip| is true, the tooltip is updated. If
1249 // |delete_removed_view| is true, the view is also deleted (if it is parent
1250 // owned). If |new_parent| is not NULL, the remove is the result of
1251 // AddChildView() to a new parent. For this case, |new_parent| is the View
1252 // that |view| is going to be added to after the remove completes.
1253 void DoRemoveChildView(View* view,
1254 bool update_focus_cycle,
1255 bool update_tool_tip,
1256 bool delete_removed_view,
1257 View* new_parent);
1259 // Call ViewHierarchyChanged() for all child views and all parents.
1260 // |old_parent| is the original parent of the View that was removed.
1261 // If |new_parent| is not NULL, the View that was removed will be reparented
1262 // to |new_parent| after the remove operation.
1263 void PropagateRemoveNotifications(View* old_parent, View* new_parent);
1265 // Call ViewHierarchyChanged() for all children.
1266 void PropagateAddNotifications(const ViewHierarchyChangedDetails& details);
1268 // Propagates NativeViewHierarchyChanged() notification through all the
1269 // children.
1270 void PropagateNativeViewHierarchyChanged();
1272 // Takes care of registering/unregistering accelerators if
1273 // |register_accelerators| true and calls ViewHierarchyChanged().
1274 void ViewHierarchyChangedImpl(bool register_accelerators,
1275 const ViewHierarchyChangedDetails& details);
1277 // Invokes OnNativeThemeChanged() on this and all descendants.
1278 void PropagateNativeThemeChanged(const ui::NativeTheme* theme);
1280 // Size and disposition ------------------------------------------------------
1282 // Call VisibilityChanged() recursively for all children.
1283 void PropagateVisibilityNotifications(View* from, bool is_visible);
1285 // Registers/unregisters accelerators as necessary and calls
1286 // VisibilityChanged().
1287 void VisibilityChangedImpl(View* starting_from, bool is_visible);
1289 // Responsible for propagating bounds change notifications to relevant
1290 // views.
1291 void BoundsChanged(const gfx::Rect& previous_bounds);
1293 // Visible bounds notification registration.
1294 // When a view is added to a hierarchy, it and all its children are asked if
1295 // they need to be registered for "visible bounds within root" notifications
1296 // (see comment on OnVisibleBoundsChanged()). If they do, they are registered
1297 // with every ancestor between them and the root of the hierarchy.
1298 static void RegisterChildrenForVisibleBoundsNotification(View* view);
1299 static void UnregisterChildrenForVisibleBoundsNotification(View* view);
1300 void RegisterForVisibleBoundsNotification();
1301 void UnregisterForVisibleBoundsNotification();
1303 // Adds/removes view to the list of descendants that are notified any time
1304 // this views location and possibly size are changed.
1305 void AddDescendantToNotify(View* view);
1306 void RemoveDescendantToNotify(View* view);
1308 // Sets the layer's bounds given in DIP coordinates.
1309 void SetLayerBounds(const gfx::Rect& bounds_in_dip);
1311 // Transformations -----------------------------------------------------------
1313 // Returns in |transform| the transform to get from coordinates of |ancestor|
1314 // to this. Returns true if |ancestor| is found. If |ancestor| is not found,
1315 // or NULL, |transform| is set to convert from root view coordinates to this.
1316 bool GetTransformRelativeTo(const View* ancestor,
1317 gfx::Transform* transform) const;
1319 // Coordinate conversion -----------------------------------------------------
1321 // Convert a point in the view's coordinate to an ancestor view's coordinate
1322 // system using necessary transformations. Returns whether the point was
1323 // successfully converted to the ancestor's coordinate system.
1324 bool ConvertPointForAncestor(const View* ancestor, gfx::Point* point) const;
1326 // Convert a point in the ancestor's coordinate system to the view's
1327 // coordinate system using necessary transformations. Returns whether the
1328 // point was successfully converted from the ancestor's coordinate system
1329 // to the view's coordinate system.
1330 bool ConvertPointFromAncestor(const View* ancestor, gfx::Point* point) const;
1332 // Convert a rect in the view's coordinate to an ancestor view's coordinate
1333 // system using necessary transformations. Returns whether the rect was
1334 // successfully converted to the ancestor's coordinate system.
1335 bool ConvertRectForAncestor(const View* ancestor, gfx::RectF* rect) const;
1337 // Convert a rect in the ancestor's coordinate system to the view's
1338 // coordinate system using necessary transformations. Returns whether the
1339 // rect was successfully converted from the ancestor's coordinate system
1340 // to the view's coordinate system.
1341 bool ConvertRectFromAncestor(const View* ancestor, gfx::RectF* rect) const;
1343 // Accelerated painting ------------------------------------------------------
1345 // Creates the layer and related fields for this view.
1346 void CreateLayer();
1348 // Parents all un-parented layers within this view's hierarchy to this view's
1349 // layer.
1350 void UpdateParentLayers();
1352 // Parents this view's layer to |parent_layer|, and sets its bounds and other
1353 // properties in accordance to |offset|, the view's offset from the
1354 // |parent_layer|.
1355 void ReparentLayer(const gfx::Vector2d& offset, ui::Layer* parent_layer);
1357 // Called to update the layer visibility. The layer will be visible if the
1358 // View itself, and all its parent Views are visible. This also updates
1359 // visibility of the child layers.
1360 void UpdateLayerVisibility();
1361 void UpdateChildLayerVisibility(bool visible);
1363 // Orphans the layers in this subtree that are parented to layers outside of
1364 // this subtree.
1365 void OrphanLayers();
1367 // Destroys the layer associated with this view, and reparents any descendants
1368 // to the destroyed layer's parent.
1369 void DestroyLayer();
1371 // Input ---------------------------------------------------------------------
1373 bool ProcessMousePressed(const ui::MouseEvent& event);
1374 bool ProcessMouseDragged(const ui::MouseEvent& event);
1375 void ProcessMouseReleased(const ui::MouseEvent& event);
1377 // Accelerators --------------------------------------------------------------
1379 // Registers this view's keyboard accelerators that are not registered to
1380 // FocusManager yet, if possible.
1381 void RegisterPendingAccelerators();
1383 // Unregisters all the keyboard accelerators associated with this view.
1384 // |leave_data_intact| if true does not remove data from accelerators_ array,
1385 // so it could be re-registered with other focus manager
1386 void UnregisterAccelerators(bool leave_data_intact);
1388 // Focus ---------------------------------------------------------------------
1390 // Initialize the previous/next focusable views of the specified view relative
1391 // to the view at the specified index.
1392 void InitFocusSiblings(View* view, int index);
1394 // System events -------------------------------------------------------------
1396 // Used to propagate theme changed notifications from the root view to all
1397 // views in the hierarchy.
1398 virtual void PropagateThemeChanged();
1400 // Used to propagate locale changed notifications from the root view to all
1401 // views in the hierarchy.
1402 virtual void PropagateLocaleChanged();
1404 // Tooltips ------------------------------------------------------------------
1406 // Propagates UpdateTooltip() to the TooltipManager for the Widget.
1407 // This must be invoked any time the View hierarchy changes in such a way
1408 // the view under the mouse differs. For example, if the bounds of a View is
1409 // changed, this is invoked. Similarly, as Views are added/removed, this
1410 // is invoked.
1411 void UpdateTooltip();
1413 // Drag and drop -------------------------------------------------------------
1415 // Starts a drag and drop operation originating from this view. This invokes
1416 // WriteDragData to write the data and GetDragOperations to determine the
1417 // supported drag operations. When done, OnDragDone is invoked. |press_pt| is
1418 // in the view's coordinate system.
1419 // Returns true if a drag was started.
1420 bool DoDrag(const ui::LocatedEvent& event,
1421 const gfx::Point& press_pt,
1422 ui::DragDropTypes::DragEventSource source);
1424 //////////////////////////////////////////////////////////////////////////////
1426 // Creation and lifetime -----------------------------------------------------
1428 // False if this View is owned by its parent - i.e. it will be deleted by its
1429 // parent during its parents destruction. False is the default.
1430 bool owned_by_client_;
1432 // Attributes ----------------------------------------------------------------
1434 // The id of this View. Used to find this View.
1435 int id_;
1437 // The group of this view. Some view subclasses use this id to find other
1438 // views of the same group. For example radio button uses this information
1439 // to find other radio buttons.
1440 int group_;
1442 // Tree operations -----------------------------------------------------------
1444 // This view's parent.
1445 View* parent_;
1447 // This view's children.
1448 Views children_;
1450 // Size and disposition ------------------------------------------------------
1452 // This View's bounds in the parent coordinate system.
1453 gfx::Rect bounds_;
1455 // Whether this view is visible.
1456 bool visible_;
1458 // Whether this view is enabled.
1459 bool enabled_;
1461 // When this flag is on, a View receives a mouse-enter and mouse-leave event
1462 // even if a descendant View is the event-recipient for the real mouse
1463 // events. When this flag is turned on, and mouse moves from outside of the
1464 // view into a child view, both the child view and this view receives
1465 // mouse-enter event. Similarly, if the mouse moves from inside a child view
1466 // and out of this view, then both views receive a mouse-leave event.
1467 // When this flag is turned off, if the mouse moves from inside this view into
1468 // a child view, then this view receives a mouse-leave event. When this flag
1469 // is turned on, it does not receive the mouse-leave event in this case.
1470 // When the mouse moves from inside the child view out of the child view but
1471 // still into this view, this view receives a mouse-enter event if this flag
1472 // is turned off, but doesn't if this flag is turned on.
1473 // This flag is initialized to false.
1474 bool notify_enter_exit_on_child_;
1476 // Whether or not RegisterViewForVisibleBoundsNotification on the RootView
1477 // has been invoked.
1478 bool registered_for_visible_bounds_notification_;
1480 // List of descendants wanting notification when their visible bounds change.
1481 scoped_ptr<Views> descendants_to_notify_;
1483 // Transformations -----------------------------------------------------------
1485 // Clipping parameters. skia transformation matrix does not give us clipping.
1486 // So we do it ourselves.
1487 gfx::Insets clip_insets_;
1489 // Layout --------------------------------------------------------------------
1491 // Whether the view needs to be laid out.
1492 bool needs_layout_;
1494 // The View's LayoutManager defines the sizing heuristics applied to child
1495 // Views. The default is absolute positioning according to bounds_.
1496 scoped_ptr<LayoutManager> layout_manager_;
1498 // Painting ------------------------------------------------------------------
1500 // Background
1501 scoped_ptr<Background> background_;
1503 // Border.
1504 scoped_ptr<Border> border_;
1506 // RTL painting --------------------------------------------------------------
1508 // Indicates whether or not the gfx::Canvas object passed to View::Paint()
1509 // is going to be flipped horizontally (using the appropriate transform) on
1510 // right-to-left locales for this View.
1511 bool flip_canvas_on_paint_for_rtl_ui_;
1513 // Accelerated painting ------------------------------------------------------
1515 bool paint_to_layer_;
1517 // Accelerators --------------------------------------------------------------
1519 // Focus manager accelerators registered on.
1520 FocusManager* accelerator_focus_manager_;
1522 // The list of accelerators. List elements in the range
1523 // [0, registered_accelerator_count_) are already registered to FocusManager,
1524 // and the rest are not yet.
1525 scoped_ptr<std::vector<ui::Accelerator> > accelerators_;
1526 size_t registered_accelerator_count_;
1528 // Focus ---------------------------------------------------------------------
1530 // Next view to be focused when the Tab key is pressed.
1531 View* next_focusable_view_;
1533 // Next view to be focused when the Shift-Tab key combination is pressed.
1534 View* previous_focusable_view_;
1536 // Whether this view can be focused.
1537 bool focusable_;
1539 // Whether this view is focusable if the user requires full keyboard access,
1540 // even though it may not be normally focusable.
1541 bool accessibility_focusable_;
1543 // Context menus -------------------------------------------------------------
1545 // The menu controller.
1546 ContextMenuController* context_menu_controller_;
1548 // Drag and drop -------------------------------------------------------------
1550 DragController* drag_controller_;
1552 // Input --------------------------------------------------------------------
1554 scoped_ptr<internal::PostEventDispatchHandler> post_dispatch_handler_;
1555 scoped_ptr<ui::EventTargeter> targeter_;
1557 // Accessibility -------------------------------------------------------------
1559 // Belongs to this view, but it's reference-counted on some platforms
1560 // so we can't use a scoped_ptr. It's dereferenced in the destructor.
1561 NativeViewAccessibility* native_view_accessibility_;
1563 DISALLOW_COPY_AND_ASSIGN(View);
1566 } // namespace views
1568 #endif // UI_VIEWS_VIEW_H_