1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef UI_VIEWS_VIEW_H_ 6 #define UI_VIEWS_VIEW_H_ 7 8 #include <algorithm> 9 #include <map> 10 #include <set> 11 #include <string> 12 #include <vector> 13 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/base/accelerators/accelerator.h" 20 #include "ui/base/accessibility/accessibility_types.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/insets.h" 30 #include "ui/gfx/native_widget_types.h" 31 #include "ui/gfx/rect.h" 32 #include "ui/gfx/vector2d.h" 33 #include "ui/views/views_export.h" 34 35 #if defined(OS_WIN) 36 #include "base/win/scoped_comptr.h" 37 #endif 38 39 using ui::OSExchangeData; 40 41 namespace gfx { 42 class Canvas; 43 class Insets; 44 class Path; 45 class Transform; 46 } 47 48 namespace ui { 49 struct AccessibleViewState; 50 class Compositor; 51 class Layer; 52 class NativeTheme; 53 class TextInputClient; 54 class Texture; 55 class ThemeProvider; 56 } 57 58 namespace views { 59 60 class Background; 61 class Border; 62 class ContextMenuController; 63 class DragController; 64 class FocusManager; 65 class FocusTraversable; 66 class InputMethod; 67 class LayoutManager; 68 class NativeViewAccessibility; 69 class ScrollView; 70 class Widget; 71 72 namespace internal { 73 class PostEventDispatchHandler; 74 class RootView; 75 } 76 77 ///////////////////////////////////////////////////////////////////////////// 78 // 79 // View class 80 // 81 // A View is a rectangle within the views View hierarchy. It is the base 82 // class for all Views. 83 // 84 // A View is a container of other Views (there is no such thing as a Leaf 85 // View - makes code simpler, reduces type conversion headaches, design 86 // mistakes etc) 87 // 88 // The View contains basic properties for sizing (bounds), layout (flex, 89 // orientation, etc), painting of children and event dispatch. 90 // 91 // The View also uses a simple Box Layout Manager similar to XUL's 92 // SprocketLayout system. Alternative Layout Managers implementing the 93 // LayoutManager interface can be used to lay out children if required. 94 // 95 // It is up to the subclass to implement Painting and storage of subclass - 96 // specific properties and functionality. 97 // 98 // Unless otherwise documented, views is not thread safe and should only be 99 // accessed from the main thread. 100 // 101 ///////////////////////////////////////////////////////////////////////////// 102 class VIEWS_EXPORT View : public ui::LayerDelegate, 103 public ui::LayerOwner, 104 public ui::AcceleratorTarget, 105 public ui::EventTarget { 106 public: 107 typedef std::vector<View*> Views; 108 109 // TODO(tdanderson,sadrul): Becomes obsolete with the refactoring of the 110 // event targeting logic for views and windows. 111 // Specifies the source of the region used in a hit test. 112 // HIT_TEST_SOURCE_MOUSE indicates the hit test is being performed with a 113 // single point and HIT_TEST_SOURCE_TOUCH indicates the hit test is being 114 // performed with a rect larger than a single point. This value can be used, 115 // for example, to add extra padding or change the shape of the hit test mask. 116 enum HitTestSource { 117 HIT_TEST_SOURCE_MOUSE, 118 HIT_TEST_SOURCE_TOUCH 119 }; 120 121 struct ViewHierarchyChangedDetails { 122 ViewHierarchyChangedDetails() 123 : is_add(false), 124 parent(NULL), 125 child(NULL), 126 move_view(NULL) {} 127 128 ViewHierarchyChangedDetails(bool is_add, 129 View* parent, 130 View* child, 131 View* move_view) 132 : is_add(is_add), 133 parent(parent), 134 child(child), 135 move_view(move_view) {} 136 137 bool is_add; 138 // New parent if |is_add| is true, old parent if |is_add| is false. 139 View* parent; 140 // The view being added or removed. 141 View* child; 142 // If this is a move (reparent), meaning AddChildViewAt() is invoked with an 143 // existing parent, then a notification for the remove is sent first, 144 // followed by one for the add. This case can be distinguished by a 145 // non-NULL |move_view|. 146 // For the remove part of move, |move_view| is the new parent of the View 147 // being removed. 148 // For the add part of move, |move_view| is the old parent of the View being 149 // added. 150 View* move_view; 151 }; 152 153 // Creation and lifetime ----------------------------------------------------- 154 155 View(); 156 virtual ~View(); 157 158 // By default a View is owned by its parent unless specified otherwise here. 159 void set_owned_by_client() { owned_by_client_ = true; } 160 161 // Tree operations ----------------------------------------------------------- 162 163 // Get the Widget that hosts this View, if any. 164 virtual const Widget* GetWidget() const; 165 virtual Widget* GetWidget(); 166 167 // Adds |view| as a child of this view, optionally at |index|. 168 void AddChildView(View* view); 169 void AddChildViewAt(View* view, int index); 170 171 // Moves |view| to the specified |index|. A negative value for |index| moves 172 // the view at the end. 173 void ReorderChildView(View* view, int index); 174 175 // Removes |view| from this view. The view's parent will change to NULL. 176 void RemoveChildView(View* view); 177 178 // Removes all the children from this view. If |delete_children| is true, 179 // the views are deleted, unless marked as not parent owned. 180 void RemoveAllChildViews(bool delete_children); 181 182 int child_count() const { return static_cast<int>(children_.size()); } 183 bool has_children() const { return !children_.empty(); } 184 185 // Returns the child view at |index|. 186 const View* child_at(int index) const { 187 DCHECK_GE(index, 0); 188 DCHECK_LT(index, child_count()); 189 return children_[index]; 190 } 191 View* child_at(int index) { 192 return const_cast<View*>(const_cast<const View*>(this)->child_at(index)); 193 } 194 195 // Returns the parent view. 196 const View* parent() const { return parent_; } 197 View* parent() { return parent_; } 198 199 // Returns true if |view| is contained within this View's hierarchy, even as 200 // an indirect descendant. Will return true if child is also this view. 201 bool Contains(const View* view) const; 202 203 // Returns the index of |view|, or -1 if |view| is not a child of this view. 204 int GetIndexOf(const View* view) const; 205 206 // Size and disposition ------------------------------------------------------ 207 // Methods for obtaining and modifying the position and size of the view. 208 // Position is in the coordinate system of the view's parent. 209 // Position is NOT flipped for RTL. See "RTL positioning" for RTL-sensitive 210 // position accessors. 211 // Transformations are not applied on the size/position. For example, if 212 // bounds is (0, 0, 100, 100) and it is scaled by 0.5 along the X axis, the 213 // width will still be 100 (although when painted, it will be 50x50, painted 214 // at location (0, 0)). 215 216 void SetBounds(int x, int y, int width, int height); 217 void SetBoundsRect(const gfx::Rect& bounds); 218 void SetSize(const gfx::Size& size); 219 void SetPosition(const gfx::Point& position); 220 void SetX(int x); 221 void SetY(int y); 222 223 // No transformation is applied on the size or the locations. 224 const gfx::Rect& bounds() const { return bounds_; } 225 int x() const { return bounds_.x(); } 226 int y() const { return bounds_.y(); } 227 int width() const { return bounds_.width(); } 228 int height() const { return bounds_.height(); } 229 const gfx::Size& size() const { return bounds_.size(); } 230 231 // Returns the bounds of the content area of the view, i.e. the rectangle 232 // enclosed by the view's border. 233 gfx::Rect GetContentsBounds() const; 234 235 // Returns the bounds of the view in its own coordinates (i.e. position is 236 // 0, 0). 237 gfx::Rect GetLocalBounds() const; 238 239 // Returns the bounds of the layer in its own pixel coordinates. 240 gfx::Rect GetLayerBoundsInPixel() const; 241 242 // Returns the insets of the current border. If there is no border an empty 243 // insets is returned. 244 virtual gfx::Insets GetInsets() const; 245 246 // Returns the visible bounds of the receiver in the receivers coordinate 247 // system. 248 // 249 // When traversing the View hierarchy in order to compute the bounds, the 250 // function takes into account the mirroring setting and transformation for 251 // each View and therefore it will return the mirrored and transformed version 252 // of the visible bounds if need be. 253 gfx::Rect GetVisibleBounds() const; 254 255 // Return the bounds of the View in screen coordinate system. 256 gfx::Rect GetBoundsInScreen() const; 257 258 // Returns the baseline of this view, or -1 if this view has no baseline. The 259 // return value is relative to the preferred height. 260 virtual int GetBaseline() const; 261 262 // Get the size the View would like to be, if enough space were available. 263 virtual gfx::Size GetPreferredSize(); 264 265 // Convenience method that sizes this view to its preferred size. 266 void SizeToPreferredSize(); 267 268 // Gets the minimum size of the view. View's implementation invokes 269 // GetPreferredSize. 270 virtual gfx::Size GetMinimumSize(); 271 272 // Gets the maximum size of the view. Currently only used for sizing shell 273 // windows. 274 virtual gfx::Size GetMaximumSize(); 275 276 // Return the height necessary to display this view with the provided width. 277 // View's implementation returns the value from getPreferredSize.cy. 278 // Override if your View's preferred height depends upon the width (such 279 // as with Labels). 280 virtual int GetHeightForWidth(int w); 281 282 // Set whether this view is visible. Painting is scheduled as needed. 283 virtual void SetVisible(bool visible); 284 285 // Return whether a view is visible 286 bool visible() const { return visible_; } 287 288 // Returns true if this view is drawn on screen. 289 virtual bool IsDrawn() const; 290 291 // Set whether this view is enabled. A disabled view does not receive keyboard 292 // or mouse inputs. If |enabled| differs from the current value, SchedulePaint 293 // is invoked. 294 void SetEnabled(bool enabled); 295 296 // Returns whether the view is enabled. 297 bool enabled() const { return enabled_; } 298 299 // This indicates that the view completely fills its bounds in an opaque 300 // color. This doesn't affect compositing but is a hint to the compositor to 301 // optimize painting. 302 // Note that this method does not implicitly create a layer if one does not 303 // already exist for the View, but is a no-op in that case. 304 void SetFillsBoundsOpaquely(bool fills_bounds_opaquely); 305 306 // Transformations ----------------------------------------------------------- 307 308 // Methods for setting transformations for a view (e.g. rotation, scaling). 309 310 gfx::Transform GetTransform() const; 311 312 // Clipping parameters. Clipping is done relative to the view bounds. 313 void set_clip_insets(gfx::Insets clip_insets) { clip_insets_ = clip_insets; } 314 315 // Sets the transform to the supplied transform. 316 void SetTransform(const gfx::Transform& transform); 317 318 // Sets whether this view paints to a layer. A view paints to a layer if 319 // either of the following are true: 320 // . the view has a non-identity transform. 321 // . SetPaintToLayer(true) has been invoked. 322 // View creates the Layer only when it exists in a Widget with a non-NULL 323 // Compositor. 324 void SetPaintToLayer(bool paint_to_layer); 325 326 // Recreates a layer for the view and returns the old layer. After this call, 327 // the View no longer has a pointer to the old layer (so it won't be able to 328 // update the old layer or destroy it). The caller must free the returned 329 // layer. 330 // Returns NULL and does not recreate layer if view does not own its layer. 331 ui::Layer* RecreateLayer() WARN_UNUSED_RESULT; 332 333 // RTL positioning ----------------------------------------------------------- 334 335 // Methods for accessing the bounds and position of the view, relative to its 336 // parent. The position returned is mirrored if the parent view is using a RTL 337 // layout. 338 // 339 // NOTE: in the vast majority of the cases, the mirroring implementation is 340 // transparent to the View subclasses and therefore you should use the 341 // bounds() accessor instead. 342 gfx::Rect GetMirroredBounds() const; 343 gfx::Point GetMirroredPosition() const; 344 int GetMirroredX() const; 345 346 // Given a rectangle specified in this View's coordinate system, the function 347 // computes the 'left' value for the mirrored rectangle within this View. If 348 // the View's UI layout is not right-to-left, then bounds.x() is returned. 349 // 350 // UI mirroring is transparent to most View subclasses and therefore there is 351 // no need to call this routine from anywhere within your subclass 352 // implementation. 353 int GetMirroredXForRect(const gfx::Rect& rect) const; 354 355 // Given the X coordinate of a point inside the View, this function returns 356 // the mirrored X coordinate of the point if the View's UI layout is 357 // right-to-left. If the layout is left-to-right, the same X coordinate is 358 // returned. 359 // 360 // Following are a few examples of the values returned by this function for 361 // a View with the bounds {0, 0, 100, 100} and a right-to-left layout: 362 // 363 // GetMirroredXCoordinateInView(0) -> 100 364 // GetMirroredXCoordinateInView(20) -> 80 365 // GetMirroredXCoordinateInView(99) -> 1 366 int GetMirroredXInView(int x) const; 367 368 // Given a X coordinate and a width inside the View, this function returns 369 // the mirrored X coordinate if the View's UI layout is right-to-left. If the 370 // layout is left-to-right, the same X coordinate is returned. 371 // 372 // Following are a few examples of the values returned by this function for 373 // a View with the bounds {0, 0, 100, 100} and a right-to-left layout: 374 // 375 // GetMirroredXCoordinateInView(0, 10) -> 90 376 // GetMirroredXCoordinateInView(20, 20) -> 60 377 int GetMirroredXWithWidthInView(int x, int w) const; 378 379 // Layout -------------------------------------------------------------------- 380 381 // Lay out the child Views (set their bounds based on sizing heuristics 382 // specific to the current Layout Manager) 383 virtual void Layout(); 384 385 // TODO(beng): I think we should remove this. 386 // Mark this view and all parents to require a relayout. This ensures the 387 // next call to Layout() will propagate to this view, even if the bounds of 388 // parent views do not change. 389 void InvalidateLayout(); 390 391 // Gets/Sets the Layout Manager used by this view to size and place its 392 // children. 393 // The LayoutManager is owned by the View and is deleted when the view is 394 // deleted, or when a new LayoutManager is installed. 395 LayoutManager* GetLayoutManager() const; 396 void SetLayoutManager(LayoutManager* layout); 397 398 // Attributes ---------------------------------------------------------------- 399 400 // The view class name. 401 static const char kViewClassName[]; 402 403 // Return the receiving view's class name. A view class is a string which 404 // uniquely identifies the view class. It is intended to be used as a way to 405 // find out during run time if a view can be safely casted to a specific view 406 // subclass. The default implementation returns kViewClassName. 407 virtual const char* GetClassName() const; 408 409 // Returns the first ancestor, starting at this, whose class name is |name|. 410 // Returns null if no ancestor has the class name |name|. 411 View* GetAncestorWithClassName(const std::string& name); 412 413 // Recursively descends the view tree starting at this view, and returns 414 // the first child that it encounters that has the given ID. 415 // Returns NULL if no matching child view is found. 416 virtual const View* GetViewByID(int id) const; 417 virtual View* GetViewByID(int id); 418 419 // Gets and sets the ID for this view. ID should be unique within the subtree 420 // that you intend to search for it. 0 is the default ID for views. 421 int id() const { return id_; } 422 void set_id(int id) { id_ = id; } 423 424 // A group id is used to tag views which are part of the same logical group. 425 // Focus can be moved between views with the same group using the arrow keys. 426 // Groups are currently used to implement radio button mutual exclusion. 427 // The group id is immutable once it's set. 428 void SetGroup(int gid); 429 // Returns the group id of the view, or -1 if the id is not set yet. 430 int GetGroup() const; 431 432 // If this returns true, the views from the same group can each be focused 433 // when moving focus with the Tab/Shift-Tab key. If this returns false, 434 // only the selected view from the group (obtained with 435 // GetSelectedViewForGroup()) is focused. 436 virtual bool IsGroupFocusTraversable() const; 437 438 // Fills |views| with all the available views which belong to the provided 439 // |group|. 440 void GetViewsInGroup(int group, Views* views); 441 442 // Returns the View that is currently selected in |group|. 443 // The default implementation simply returns the first View found for that 444 // group. 445 virtual View* GetSelectedViewForGroup(int group); 446 447 // Coordinate conversion ----------------------------------------------------- 448 449 // Note that the utility coordinate conversions functions always operate on 450 // the mirrored position of the child Views if the parent View uses a 451 // right-to-left UI layout. 452 453 // Convert a point from the coordinate system of one View to another. 454 // 455 // |source| and |target| must be in the same widget, but doesn't need to be in 456 // the same view hierarchy. 457 // Neither |source| nor |target| can be NULL. 458 static void ConvertPointToTarget(const View* source, 459 const View* target, 460 gfx::Point* point); 461 462 // Convert |rect| from the coordinate system of |source| to the coordinate 463 // system of |target|. 464 // 465 // |source| and |target| must be in the same widget, but doesn't need to be in 466 // the same view hierarchy. 467 // Neither |source| nor |target| can be NULL. 468 static void ConvertRectToTarget(const View* source, 469 const View* target, 470 gfx::RectF* rect); 471 472 // Convert a point from a View's coordinate system to that of its Widget. 473 static void ConvertPointToWidget(const View* src, gfx::Point* point); 474 475 // Convert a point from the coordinate system of a View's Widget to that 476 // View's coordinate system. 477 static void ConvertPointFromWidget(const View* dest, gfx::Point* p); 478 479 // Convert a point from a View's coordinate system to that of the screen. 480 static void ConvertPointToScreen(const View* src, gfx::Point* point); 481 482 // Convert a point from a View's coordinate system to that of the screen. 483 static void ConvertPointFromScreen(const View* dst, gfx::Point* point); 484 485 // Applies transformation on the rectangle, which is in the view's coordinate 486 // system, to convert it into the parent's coordinate system. 487 gfx::Rect ConvertRectToParent(const gfx::Rect& rect) const; 488 489 // Converts a rectangle from this views coordinate system to its widget 490 // coordinate system. 491 gfx::Rect ConvertRectToWidget(const gfx::Rect& rect) const; 492 493 // Painting ------------------------------------------------------------------ 494 495 // Mark all or part of the View's bounds as dirty (needing repaint). 496 // |r| is in the View's coordinates. 497 // Rectangle |r| should be in the view's coordinate system. The 498 // transformations are applied to it to convert it into the parent coordinate 499 // system before propagating SchedulePaint up the view hierarchy. 500 // TODO(beng): Make protected. 501 virtual void SchedulePaint(); 502 virtual void SchedulePaintInRect(const gfx::Rect& r); 503 504 // Called by the framework to paint a View. Performs translation and clipping 505 // for View coordinates and language direction as required, allows the View 506 // to paint itself via the various OnPaint*() event handlers and then paints 507 // the hierarchy beneath it. 508 virtual void Paint(gfx::Canvas* canvas); 509 510 // The background object is owned by this object and may be NULL. 511 void set_background(Background* b); 512 const Background* background() const { return background_.get(); } 513 Background* background() { return background_.get(); } 514 515 // The border object is owned by this object and may be NULL. 516 void set_border(Border* b); 517 const Border* border() const { return border_.get(); } 518 Border* border() { return border_.get(); } 519 520 // Get the theme provider from the parent widget. 521 virtual ui::ThemeProvider* GetThemeProvider() const; 522 523 // Returns the NativeTheme to use for this View. This calls through to 524 // GetNativeTheme() on the Widget this View is in. If this View is not in a 525 // Widget this returns ui::NativeTheme::instance(). 526 ui::NativeTheme* GetNativeTheme() { 527 return const_cast<ui::NativeTheme*>( 528 const_cast<const View*>(this)->GetNativeTheme()); 529 } 530 const ui::NativeTheme* GetNativeTheme() const; 531 532 // RTL painting -------------------------------------------------------------- 533 534 // This method determines whether the gfx::Canvas object passed to 535 // View::Paint() needs to be transformed such that anything drawn on the 536 // canvas object during View::Paint() is flipped horizontally. 537 // 538 // By default, this function returns false (which is the initial value of 539 // |flip_canvas_on_paint_for_rtl_ui_|). View subclasses that need to paint on 540 // a flipped gfx::Canvas when the UI layout is right-to-left need to call 541 // EnableCanvasFlippingForRTLUI(). 542 bool FlipCanvasOnPaintForRTLUI() const { 543 return flip_canvas_on_paint_for_rtl_ui_ ? base::i18n::IsRTL() : false; 544 } 545 546 // Enables or disables flipping of the gfx::Canvas during View::Paint(). 547 // Note that if canvas flipping is enabled, the canvas will be flipped only 548 // if the UI layout is right-to-left; that is, the canvas will be flipped 549 // only if base::i18n::IsRTL() returns true. 550 // 551 // Enabling canvas flipping is useful for leaf views that draw an image that 552 // needs to be flipped horizontally when the UI layout is right-to-left 553 // (views::Button, for example). This method is helpful for such classes 554 // because their drawing logic stays the same and they can become agnostic to 555 // the UI directionality. 556 void EnableCanvasFlippingForRTLUI(bool enable) { 557 flip_canvas_on_paint_for_rtl_ui_ = enable; 558 } 559 560 // Accelerated painting ------------------------------------------------------ 561 562 // Enable/Disable accelerated compositing. 563 static void set_use_acceleration_when_possible(bool use); 564 static bool get_use_acceleration_when_possible(); 565 566 // Input --------------------------------------------------------------------- 567 // The points, rects, mouse locations, and touch locations in the following 568 // functions are in the view's coordinates, except for a RootView. 569 570 // Convenience functions which calls into GetEventHandler() with 571 // a 1x1 rect centered at |point|. 572 View* GetEventHandlerForPoint(const gfx::Point& point); 573 574 // If point-based targeting should be used, return the deepest visible 575 // descendant that contains the center point of |rect|. 576 // If rect-based targeting (i.e., fuzzing) should be used, return the 577 // closest visible descendant having at least kRectTargetOverlap of 578 // its area covered by |rect|. If no such descendant exists, return the 579 // deepest visible descendant that contains the center point of |rect|. 580 // See http://goo.gl/3Jp2BD for more information about rect-based targeting. 581 virtual View* GetEventHandlerForRect(const gfx::Rect& rect); 582 583 // Returns the deepest visible descendant that contains the specified point 584 // and supports tooltips. If the view does not contain the point, returns 585 // NULL. 586 virtual View* GetTooltipHandlerForPoint(const gfx::Point& point); 587 588 // Return the cursor that should be used for this view or the default cursor. 589 // The event location is in the receiver's coordinate system. The caller is 590 // responsible for managing the lifetime of the returned object, though that 591 // lifetime may vary from platform to platform. On Windows and Aura, 592 // the cursor is a shared resource. 593 virtual gfx::NativeCursor GetCursor(const ui::MouseEvent& event); 594 595 // A convenience function which calls HitTestRect() with a rect of size 596 // 1x1 and an origin of |point|. 597 bool HitTestPoint(const gfx::Point& point) const; 598 599 // Tests whether |rect| intersects this view's bounds. 600 virtual bool HitTestRect(const gfx::Rect& rect) const; 601 602 // Returns true if the mouse cursor is over |view| and mouse events are 603 // enabled. 604 bool IsMouseHovered(); 605 606 // This method is invoked when the user clicks on this view. 607 // The provided event is in the receiver's coordinate system. 608 // 609 // Return true if you processed the event and want to receive subsequent 610 // MouseDraggged and MouseReleased events. This also stops the event from 611 // bubbling. If you return false, the event will bubble through parent 612 // views. 613 // 614 // If you remove yourself from the tree while processing this, event bubbling 615 // stops as if you returned true, but you will not receive future events. 616 // The return value is ignored in this case. 617 // 618 // Default implementation returns true if a ContextMenuController has been 619 // set, false otherwise. Override as needed. 620 // 621 virtual bool OnMousePressed(const ui::MouseEvent& event); 622 623 // This method is invoked when the user clicked on this control. 624 // and is still moving the mouse with a button pressed. 625 // The provided event is in the receiver's coordinate system. 626 // 627 // Return true if you processed the event and want to receive 628 // subsequent MouseDragged and MouseReleased events. 629 // 630 // Default implementation returns true if a ContextMenuController has been 631 // set, false otherwise. Override as needed. 632 // 633 virtual bool OnMouseDragged(const ui::MouseEvent& event); 634 635 // This method is invoked when the user releases the mouse 636 // button. The event is in the receiver's coordinate system. 637 // 638 // Default implementation notifies the ContextMenuController is appropriate. 639 // Subclasses that wish to honor the ContextMenuController should invoke 640 // super. 641 virtual void OnMouseReleased(const ui::MouseEvent& event); 642 643 // This method is invoked when the mouse press/drag was canceled by a 644 // system/user gesture. 645 virtual void OnMouseCaptureLost(); 646 647 // This method is invoked when the mouse is above this control 648 // The event is in the receiver's coordinate system. 649 // 650 // Default implementation does nothing. Override as needed. 651 virtual void OnMouseMoved(const ui::MouseEvent& event); 652 653 // This method is invoked when the mouse enters this control. 654 // 655 // Default implementation does nothing. Override as needed. 656 virtual void OnMouseEntered(const ui::MouseEvent& event); 657 658 // This method is invoked when the mouse exits this control 659 // The provided event location is always (0, 0) 660 // Default implementation does nothing. Override as needed. 661 virtual void OnMouseExited(const ui::MouseEvent& event); 662 663 // Set the MouseHandler for a drag session. 664 // 665 // A drag session is a stream of mouse events starting 666 // with a MousePressed event, followed by several MouseDragged 667 // events and finishing with a MouseReleased event. 668 // 669 // This method should be only invoked while processing a 670 // MouseDragged or MousePressed event. 671 // 672 // All further mouse dragged and mouse up events will be sent 673 // the MouseHandler, even if it is reparented to another window. 674 // 675 // The MouseHandler is automatically cleared when the control 676 // comes back from processing the MouseReleased event. 677 // 678 // Note: if the mouse handler is no longer connected to a 679 // view hierarchy, events won't be sent. 680 // 681 // TODO(sky): rename this. 682 virtual void SetMouseHandler(View* new_mouse_handler); 683 684 // Invoked when a key is pressed or released. 685 // Subclasser should return true if the event has been processed and false 686 // otherwise. If the event has not been processed, the parent will be given a 687 // chance. 688 virtual bool OnKeyPressed(const ui::KeyEvent& event); 689 virtual bool OnKeyReleased(const ui::KeyEvent& event); 690 691 // Invoked when the user uses the mousewheel. Implementors should return true 692 // if the event has been processed and false otherwise. This message is sent 693 // if the view is focused. If the event has not been processed, the parent 694 // will be given a chance. 695 virtual bool OnMouseWheel(const ui::MouseWheelEvent& event); 696 697 698 // See field for description. 699 void set_notify_enter_exit_on_child(bool notify) { 700 notify_enter_exit_on_child_ = notify; 701 } 702 bool notify_enter_exit_on_child() const { 703 return notify_enter_exit_on_child_; 704 } 705 706 // Returns the View's TextInputClient instance or NULL if the View doesn't 707 // support text input. 708 virtual ui::TextInputClient* GetTextInputClient(); 709 710 // Convenience method to retrieve the InputMethod associated with the 711 // Widget that contains this view. Returns NULL if this view is not part of a 712 // view hierarchy with a Widget. 713 virtual InputMethod* GetInputMethod(); 714 virtual const InputMethod* GetInputMethod() const; 715 716 // Overridden from ui::EventTarget: 717 virtual bool CanAcceptEvent(const ui::Event& event) OVERRIDE; 718 virtual ui::EventTarget* GetParentTarget() OVERRIDE; 719 virtual scoped_ptr<ui::EventTargetIterator> GetChildIterator() const OVERRIDE; 720 virtual ui::EventTargeter* GetEventTargeter() OVERRIDE; 721 722 // Overridden from ui::EventHandler: 723 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE; 724 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE; 725 virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE; 726 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE; 727 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE; 728 729 // Accelerators -------------------------------------------------------------- 730 731 // Sets a keyboard accelerator for that view. When the user presses the 732 // accelerator key combination, the AcceleratorPressed method is invoked. 733 // Note that you can set multiple accelerators for a view by invoking this 734 // method several times. Note also that AcceleratorPressed is invoked only 735 // when CanHandleAccelerators() is true. 736 virtual void AddAccelerator(const ui::Accelerator& accelerator); 737 738 // Removes the specified accelerator for this view. 739 virtual void RemoveAccelerator(const ui::Accelerator& accelerator); 740 741 // Removes all the keyboard accelerators for this view. 742 virtual void ResetAccelerators(); 743 744 // Overridden from AcceleratorTarget: 745 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE; 746 747 // Returns whether accelerators are enabled for this view. Accelerators are 748 // enabled if the containing widget is visible and the view is enabled() and 749 // IsDrawn() 750 virtual bool CanHandleAccelerators() const OVERRIDE; 751 752 // Focus --------------------------------------------------------------------- 753 754 // Returns whether this view currently has the focus. 755 virtual bool HasFocus() const; 756 757 // Returns the view that should be selected next when pressing Tab. 758 View* GetNextFocusableView(); 759 const View* GetNextFocusableView() const; 760 761 // Returns the view that should be selected next when pressing Shift-Tab. 762 View* GetPreviousFocusableView(); 763 764 // Sets the component that should be selected next when pressing Tab, and 765 // makes the current view the precedent view of the specified one. 766 // Note that by default views are linked in the order they have been added to 767 // their container. Use this method if you want to modify the order. 768 // IMPORTANT NOTE: loops in the focus hierarchy are not supported. 769 void SetNextFocusableView(View* view); 770 771 // Sets whether this view is capable of taking focus. 772 // Note that this is false by default so that a view used as a container does 773 // not get the focus. 774 void SetFocusable(bool focusable); 775 776 // Returns true if this view is capable of taking focus. 777 bool focusable() const { return focusable_ && enabled_ && visible_; } 778 779 // Returns true if this view is |focusable_|, |enabled_| and drawn. 780 virtual bool IsFocusable() const; 781 782 // Return whether this view is focusable when the user requires full keyboard 783 // access, even though it may not be normally focusable. 784 bool IsAccessibilityFocusable() const; 785 786 // Set whether this view can be made focusable if the user requires 787 // full keyboard access, even though it's not normally focusable. 788 // Note that this is false by default. 789 void SetAccessibilityFocusable(bool accessibility_focusable); 790 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; 796 797 // Request keyboard focus. The receiving view will become the focused view. 798 virtual void RequestFocus(); 799 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 802 // (Shift-Tab). 803 virtual void AboutToRequestFocusFromTabTraversal(bool reverse) {} 804 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); 815 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(); 820 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(); 827 828 // Tooltips ------------------------------------------------------------------ 829 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, string16* tooltip) const; 837 838 // Returns the location (relative to this View) for the text on the tooltip 839 // to display. If false is returned (the default), the tooltip is placed at 840 // a default position. 841 virtual bool GetTooltipTextOrigin(const gfx::Point& p, gfx::Point* loc) const; 842 843 // Context menus ------------------------------------------------------------- 844 845 // Sets the ContextMenuController. Setting this to non-null makes the View 846 // process mouse events. 847 ContextMenuController* context_menu_controller() { 848 return context_menu_controller_; 849 } 850 void set_context_menu_controller(ContextMenuController* menu_controller) { 851 context_menu_controller_ = menu_controller; 852 } 853 854 // Provides default implementation for context menu handling. The default 855 // implementation calls the ShowContextMenu of the current 856 // ContextMenuController (if it is not NULL). Overridden in subclassed views 857 // to provide right-click menu display triggerd by the keyboard (i.e. for the 858 // Chrome toolbar Back and Forward buttons). No source needs to be specified, 859 // as it is always equal to the current View. 860 virtual void ShowContextMenu(const gfx::Point& p, 861 ui::MenuSourceType source_type); 862 863 // On some platforms, we show context menu on mouse press instead of release. 864 // This method returns true for those platforms. 865 static bool ShouldShowContextMenuOnMousePress(); 866 867 // Drag and drop ------------------------------------------------------------- 868 869 DragController* drag_controller() { return drag_controller_; } 870 void set_drag_controller(DragController* drag_controller) { 871 drag_controller_ = drag_controller; 872 } 873 874 // During a drag and drop session when the mouse moves the view under the 875 // mouse is queried for the drop types it supports by way of the 876 // GetDropFormats methods. If the view returns true and the drag site can 877 // provide data in one of the formats, the view is asked if the drop data 878 // is required before any other drop events are sent. Once the 879 // data is available the view is asked if it supports the drop (by way of 880 // the CanDrop method). If a view returns true from CanDrop, 881 // OnDragEntered is sent to the view when the mouse first enters the view, 882 // as the mouse moves around within the view OnDragUpdated is invoked. 883 // If the user releases the mouse over the view and OnDragUpdated returns a 884 // valid drop, then OnPerformDrop is invoked. If the mouse moves outside the 885 // view or over another view that wants the drag, OnDragExited is invoked. 886 // 887 // Similar to mouse events, the deepest view under the mouse is first checked 888 // if it supports the drop (Drop). If the deepest view under 889 // the mouse does not support the drop, the ancestors are walked until one 890 // is found that supports the drop. 891 892 // Override and return the set of formats that can be dropped on this view. 893 // |formats| is a bitmask of the formats defined bye OSExchangeData::Format. 894 // The default implementation returns false, which means the view doesn't 895 // support dropping. 896 virtual bool GetDropFormats( 897 int* formats, 898 std::set<OSExchangeData::CustomFormat>* custom_formats); 899 900 // Override and return true if the data must be available before any drop 901 // methods should be invoked. The default is false. 902 virtual bool AreDropTypesRequired(); 903 904 // A view that supports drag and drop must override this and return true if 905 // data contains a type that may be dropped on this view. 906 virtual bool CanDrop(const OSExchangeData& data); 907 908 // OnDragEntered is invoked when the mouse enters this view during a drag and 909 // drop session and CanDrop returns true. This is immediately 910 // followed by an invocation of OnDragUpdated, and eventually one of 911 // OnDragExited or OnPerformDrop. 912 virtual void OnDragEntered(const ui::DropTargetEvent& event); 913 914 // Invoked during a drag and drop session while the mouse is over the view. 915 // This should return a bitmask of the DragDropTypes::DragOperation supported 916 // based on the location of the event. Return 0 to indicate the drop should 917 // not be accepted. 918 virtual int OnDragUpdated(const ui::DropTargetEvent& event); 919 920 // Invoked during a drag and drop session when the mouse exits the views, or 921 // when the drag session was canceled and the mouse was over the view. 922 virtual void OnDragExited(); 923 924 // Invoked during a drag and drop session when OnDragUpdated returns a valid 925 // operation and the user release the mouse. 926 virtual int OnPerformDrop(const ui::DropTargetEvent& event); 927 928 // Invoked from DoDrag after the drag completes. This implementation does 929 // nothing, and is intended for subclasses to do cleanup. 930 virtual void OnDragDone(); 931 932 // Returns true if the mouse was dragged enough to start a drag operation. 933 // delta_x and y are the distance the mouse was dragged. 934 static bool ExceededDragThreshold(const gfx::Vector2d& delta); 935 936 // Accessibility ------------------------------------------------------------- 937 938 // Modifies |state| to reflect the current accessible state of this view. 939 virtual void GetAccessibleState(ui::AccessibleViewState* state) { } 940 941 // Returns an instance of the native accessibility interface for this view. 942 virtual gfx::NativeViewAccessible GetNativeViewAccessible(); 943 944 // Notifies assistive technology that an accessibility event has 945 // occurred on this view, such as when the view is focused or when its 946 // value changes. Pass true for |send_native_event| except for rare 947 // cases where the view is a native control that's already sending a 948 // native accessibility event and the duplicate event would cause 949 // problems. 950 void NotifyAccessibilityEvent(ui::AccessibilityTypes::Event event_type, 951 bool send_native_event); 952 953 // Scrolling ----------------------------------------------------------------- 954 // TODO(beng): Figure out if this can live somewhere other than View, i.e. 955 // closer to ScrollView. 956 957 // Scrolls the specified region, in this View's coordinate system, to be 958 // visible. View's implementation passes the call onto the parent View (after 959 // adjusting the coordinates). It is up to views that only show a portion of 960 // the child view, such as Viewport, to override appropriately. 961 virtual void ScrollRectToVisible(const gfx::Rect& rect); 962 963 // The following methods are used by ScrollView to determine the amount 964 // to scroll relative to the visible bounds of the view. For example, a 965 // return value of 10 indicates the scrollview should scroll 10 pixels in 966 // the appropriate direction. 967 // 968 // Each method takes the following parameters: 969 // 970 // is_horizontal: if true, scrolling is along the horizontal axis, otherwise 971 // the vertical axis. 972 // is_positive: if true, scrolling is by a positive amount. Along the 973 // vertical axis scrolling by a positive amount equates to 974 // scrolling down. 975 // 976 // The return value should always be positive and gives the number of pixels 977 // to scroll. ScrollView interprets a return value of 0 (or negative) 978 // to scroll by a default amount. 979 // 980 // See VariableRowHeightScrollHelper and FixedRowHeightScrollHelper for 981 // implementations of common cases. 982 virtual int GetPageScrollIncrement(ScrollView* scroll_view, 983 bool is_horizontal, bool is_positive); 984 virtual int GetLineScrollIncrement(ScrollView* scroll_view, 985 bool is_horizontal, bool is_positive); 986 987 protected: 988 // Used to track a drag. RootView passes this into 989 // ProcessMousePressed/Dragged. 990 struct DragInfo { 991 // Sets possible_drag to false and start_x/y to 0. This is invoked by 992 // RootView prior to invoke ProcessMousePressed. 993 void Reset(); 994 995 // Sets possible_drag to true and start_pt to the specified point. 996 // This is invoked by the target view if it detects the press may generate 997 // a drag. 998 void PossibleDrag(const gfx::Point& p); 999 1000 // Whether the press may generate a drag. 1001 bool possible_drag; 1002 1003 // Coordinates of the mouse press. 1004 gfx::Point start_pt; 1005 }; 1006 1007 // Size and disposition ------------------------------------------------------ 1008 1009 // Override to be notified when the bounds of the view have changed. 1010 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds); 1011 1012 // Called when the preferred size of a child view changed. This gives the 1013 // parent an opportunity to do a fresh layout if that makes sense. 1014 virtual void ChildPreferredSizeChanged(View* child) {} 1015 1016 // Called when the visibility of a child view changed. This gives the parent 1017 // an opportunity to do a fresh layout if that makes sense. 1018 virtual void ChildVisibilityChanged(View* child) {} 1019 1020 // Invalidates the layout and calls ChildPreferredSizeChanged on the parent 1021 // if there is one. Be sure to call View::PreferredSizeChanged when 1022 // overriding such that the layout is properly invalidated. 1023 virtual void PreferredSizeChanged(); 1024 1025 // Override returning true when the view needs to be notified when its visible 1026 // bounds relative to the root view may have changed. Only used by 1027 // NativeViewHost. 1028 virtual bool NeedsNotificationWhenVisibleBoundsChange() const; 1029 1030 // Notification that this View's visible bounds relative to the root view may 1031 // have changed. The visible bounds are the region of the View not clipped by 1032 // its ancestors. This is used for clipping NativeViewHost. 1033 virtual void OnVisibleBoundsChanged(); 1034 1035 // Override to be notified when the enabled state of this View has 1036 // changed. The default implementation calls SchedulePaint() on this View. 1037 virtual void OnEnabledChanged(); 1038 1039 // Tree operations ----------------------------------------------------------- 1040 1041 // This method is invoked when the tree changes. 1042 // 1043 // When a view is removed, it is invoked for all children and grand 1044 // children. For each of these views, a notification is sent to the 1045 // view and all parents. 1046 // 1047 // When a view is added, a notification is sent to the view, all its 1048 // parents, and all its children (and grand children) 1049 // 1050 // Default implementation does nothing. Override to perform operations 1051 // required when a view is added or removed from a view hierarchy 1052 // 1053 // Refer to comments in struct |ViewHierarchyChangedDetails| for |details|. 1054 virtual void ViewHierarchyChanged(const ViewHierarchyChangedDetails& details); 1055 1056 // When SetVisible() changes the visibility of a view, this method is 1057 // invoked for that view as well as all the children recursively. 1058 virtual void VisibilityChanged(View* starting_from, bool is_visible); 1059 1060 // This method is invoked when the parent NativeView of the widget that the 1061 // view is attached to has changed and the view hierarchy has not changed. 1062 // ViewHierarchyChanged() is called when the parent NativeView of the widget 1063 // that the view is attached to is changed as a result of changing the view 1064 // hierarchy. Overriding this method is useful for tracking which 1065 // FocusManager manages this view. 1066 virtual void NativeViewHierarchyChanged(); 1067 1068 // Painting ------------------------------------------------------------------ 1069 1070 // Responsible for calling Paint() on child Views. Override to control the 1071 // order child Views are painted. 1072 virtual void PaintChildren(gfx::Canvas* canvas); 1073 1074 // Override to provide rendering in any part of the View's bounds. Typically 1075 // this is the "contents" of the view. If you override this method you will 1076 // have to call the subsequent OnPaint*() methods manually. 1077 virtual void OnPaint(gfx::Canvas* canvas); 1078 1079 // Override to paint a background before any content is drawn. Typically this 1080 // is done if you are satisfied with a default OnPaint handler but wish to 1081 // supply a different background. 1082 virtual void OnPaintBackground(gfx::Canvas* canvas); 1083 1084 // Override to paint a border not specified by SetBorder(). 1085 virtual void OnPaintBorder(gfx::Canvas* canvas); 1086 1087 // Accelerated painting ------------------------------------------------------ 1088 1089 // Returns the offset from this view to the nearest ancestor with a layer. If 1090 // |layer_parent| is non-NULL it is set to the nearest ancestor with a layer. 1091 virtual gfx::Vector2d CalculateOffsetToAncestorWithLayer( 1092 ui::Layer** layer_parent); 1093 1094 // Updates the view's layer's parent. Called when a view is added to a view 1095 // hierarchy, responsible for parenting the view's layer to the enclosing 1096 // layer in the hierarchy. 1097 virtual void UpdateParentLayer(); 1098 1099 // If this view has a layer, the layer is reparented to |parent_layer| and its 1100 // bounds is set based on |point|. If this view does not have a layer, then 1101 // recurses through all children. This is used when adding a layer to an 1102 // existing view to make sure all descendants that have layers are parented to 1103 // the right layer. 1104 void MoveLayerToParent(ui::Layer* parent_layer, const gfx::Point& point); 1105 1106 // Called to update the bounds of any child layers within this View's 1107 // hierarchy when something happens to the hierarchy. 1108 void UpdateChildLayerBounds(const gfx::Vector2d& offset); 1109 1110 // Overridden from ui::LayerDelegate: 1111 virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE; 1112 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE; 1113 virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE; 1114 1115 // Finds the layer that this view paints to (it may belong to an ancestor 1116 // view), then reorders the immediate children of that layer to match the 1117 // order of the view tree. 1118 virtual void ReorderLayers(); 1119 1120 // This reorders the immediate children of |*parent_layer| to match the 1121 // order of the view tree. Child layers which are owned by a view are 1122 // reordered so that they are below any child layers not owned by a view. 1123 // Widget::ReorderNativeViews() should be called to reorder any child layers 1124 // with an associated view. Widget::ReorderNativeViews() may reorder layers 1125 // below layers owned by a view. 1126 virtual void ReorderChildLayers(ui::Layer* parent_layer); 1127 1128 // Input --------------------------------------------------------------------- 1129 1130 // Called by HitTestRect() to see if this View has a custom hit test mask. If 1131 // the return value is true, GetHitTestMask() will be called to obtain the 1132 // mask. Default value is false, in which case the View will hit-test against 1133 // its bounds. 1134 virtual bool HasHitTestMask() const; 1135 1136 // Called by HitTestRect() to retrieve a mask for hit-testing against. 1137 // Subclasses override to provide custom shaped hit test regions. 1138 virtual void GetHitTestMask(HitTestSource source, gfx::Path* mask) const; 1139 1140 virtual DragInfo* GetDragInfo(); 1141 1142 // Focus --------------------------------------------------------------------- 1143 1144 // Override to be notified when focus has changed either to or from this View. 1145 virtual void OnFocus(); 1146 virtual void OnBlur(); 1147 1148 // Handle view focus/blur events for this view. 1149 void Focus(); 1150 void Blur(); 1151 1152 // System events ------------------------------------------------------------- 1153 1154 // Called when the UI theme (not the NativeTheme) has changed, overriding 1155 // allows individual Views to do special cleanup and processing (such as 1156 // dropping resource caches). To dispatch a theme changed notification, call 1157 // Widget::ThemeChanged(). 1158 virtual void OnThemeChanged() {} 1159 1160 // Called when the locale has changed, overriding allows individual Views to 1161 // update locale-dependent strings. 1162 // To dispatch a locale changed notification, call Widget::LocaleChanged(). 1163 virtual void OnLocaleChanged() {} 1164 1165 // Tooltips ------------------------------------------------------------------ 1166 1167 // Views must invoke this when the tooltip text they are to display changes. 1168 void TooltipTextChanged(); 1169 1170 // Context menus ------------------------------------------------------------- 1171 1172 // Returns the location, in screen coordinates, to show the context menu at 1173 // when the context menu is shown from the keyboard. This implementation 1174 // returns the middle of the visible region of this view. 1175 // 1176 // This method is invoked when the context menu is shown by way of the 1177 // keyboard. 1178 virtual gfx::Point GetKeyboardContextMenuLocation(); 1179 1180 // Drag and drop ------------------------------------------------------------- 1181 1182 // These are cover methods that invoke the method of the same name on 1183 // the DragController. Subclasses may wish to override rather than install 1184 // a DragController. 1185 // See DragController for a description of these methods. 1186 virtual int GetDragOperations(const gfx::Point& press_pt); 1187 virtual void WriteDragData(const gfx::Point& press_pt, OSExchangeData* data); 1188 1189 // Returns whether we're in the middle of a drag session that was initiated 1190 // by us. 1191 bool InDrag(); 1192 1193 // Returns how much the mouse needs to move in one direction to start a 1194 // drag. These methods cache in a platform-appropriate way. These values are 1195 // used by the public static method ExceededDragThreshold(). 1196 static int GetHorizontalDragThreshold(); 1197 static int GetVerticalDragThreshold(); 1198 1199 // NativeTheme --------------------------------------------------------------- 1200 1201 // Invoked when the NativeTheme associated with this View changes. 1202 virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) {} 1203 1204 // Debugging ----------------------------------------------------------------- 1205 1206 #if !defined(NDEBUG) 1207 // Returns string containing a graph of the views hierarchy in graphViz DOT 1208 // language (http://graphviz.org/). Can be called within debugger and save 1209 // to a file to compile/view. 1210 // Note: Assumes initial call made with first = true. 1211 virtual std::string PrintViewGraph(bool first); 1212 1213 // Some classes may own an object which contains the children to displayed in 1214 // the views hierarchy. The above function gives the class the flexibility to 1215 // decide which object should be used to obtain the children, but this 1216 // function makes the decision explicit. 1217 std::string DoPrintViewGraph(bool first, View* view_with_children); 1218 #endif 1219 1220 private: 1221 friend class internal::PostEventDispatchHandler; 1222 friend class internal::RootView; 1223 friend class FocusManager; 1224 friend class Widget; 1225 1226 // Painting ----------------------------------------------------------------- 1227 1228 enum SchedulePaintType { 1229 // Indicates the size is the same (only the origin changed). 1230 SCHEDULE_PAINT_SIZE_SAME, 1231 1232 // Indicates the size changed (and possibly the origin). 1233 SCHEDULE_PAINT_SIZE_CHANGED 1234 }; 1235 1236 // Invoked before and after the bounds change to schedule painting the old and 1237 // new bounds. 1238 void SchedulePaintBoundsChanged(SchedulePaintType type); 1239 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); 1243 1244 // Tree operations ----------------------------------------------------------- 1245 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); 1258 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); 1264 1265 // Call ViewHierarchyChanged() for all children. 1266 void PropagateAddNotifications(const ViewHierarchyChangedDetails& details); 1267 1268 // Propagates NativeViewHierarchyChanged() notification through all the 1269 // children. 1270 void PropagateNativeViewHierarchyChanged(); 1271 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); 1276 1277 // Invokes OnNativeThemeChanged() on this and all descendants. 1278 void PropagateNativeThemeChanged(const ui::NativeTheme* theme); 1279 1280 // Size and disposition ------------------------------------------------------ 1281 1282 // Call VisibilityChanged() recursively for all children. 1283 void PropagateVisibilityNotifications(View* from, bool is_visible); 1284 1285 // Registers/unregisters accelerators as necessary and calls 1286 // VisibilityChanged(). 1287 void VisibilityChangedImpl(View* starting_from, bool is_visible); 1288 1289 // Responsible for propagating bounds change notifications to relevant 1290 // views. 1291 void BoundsChanged(const gfx::Rect& previous_bounds); 1292 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(); 1302 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); 1307 1308 // Sets the layer's bounds given in DIP coordinates. 1309 void SetLayerBounds(const gfx::Rect& bounds_in_dip); 1310 1311 // Transformations ----------------------------------------------------------- 1312 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; 1318 1319 // Coordinate conversion ----------------------------------------------------- 1320 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; 1325 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; 1331 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; 1336 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; 1342 1343 // Accelerated painting ------------------------------------------------------ 1344 1345 // Creates the layer and related fields for this view. 1346 void CreateLayer(); 1347 1348 // Parents all un-parented layers within this view's hierarchy to this view's 1349 // layer. 1350 void UpdateParentLayers(); 1351 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); 1356 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); 1362 1363 // Orphans the layers in this subtree that are parented to layers outside of 1364 // this subtree. 1365 void OrphanLayers(); 1366 1367 // Destroys the layer associated with this view, and reparents any descendants 1368 // to the destroyed layer's parent. 1369 void DestroyLayer(); 1370 1371 // Input --------------------------------------------------------------------- 1372 1373 bool ProcessMousePressed(const ui::MouseEvent& event); 1374 bool ProcessMouseDragged(const ui::MouseEvent& event); 1375 void ProcessMouseReleased(const ui::MouseEvent& event); 1376 1377 // Accelerators -------------------------------------------------------------- 1378 1379 // Registers this view's keyboard accelerators that are not registered to 1380 // FocusManager yet, if possible. 1381 void RegisterPendingAccelerators(); 1382 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); 1387 1388 // Focus --------------------------------------------------------------------- 1389 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); 1393 1394 // System events ------------------------------------------------------------- 1395 1396 // Used to propagate theme changed notifications from the root view to all 1397 // views in the hierarchy. 1398 virtual void PropagateThemeChanged(); 1399 1400 // Used to propagate locale changed notifications from the root view to all 1401 // views in the hierarchy. 1402 virtual void PropagateLocaleChanged(); 1403 1404 // Tooltips ------------------------------------------------------------------ 1405 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(); 1412 1413 // Drag and drop ------------------------------------------------------------- 1414 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); 1423 1424 ////////////////////////////////////////////////////////////////////////////// 1425 1426 // Creation and lifetime ----------------------------------------------------- 1427 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_; 1431 1432 // Attributes ---------------------------------------------------------------- 1433 1434 // The id of this View. Used to find this View. 1435 int id_; 1436 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_; 1441 1442 // Tree operations ----------------------------------------------------------- 1443 1444 // This view's parent. 1445 View* parent_; 1446 1447 // This view's children. 1448 Views children_; 1449 1450 // Size and disposition ------------------------------------------------------ 1451 1452 // This View's bounds in the parent coordinate system. 1453 gfx::Rect bounds_; 1454 1455 // Whether this view is visible. 1456 bool visible_; 1457 1458 // Whether this view is enabled. 1459 bool enabled_; 1460 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_; 1475 1476 // Whether or not RegisterViewForVisibleBoundsNotification on the RootView 1477 // has been invoked. 1478 bool registered_for_visible_bounds_notification_; 1479 1480 // List of descendants wanting notification when their visible bounds change. 1481 scoped_ptr<Views> descendants_to_notify_; 1482 1483 // Transformations ----------------------------------------------------------- 1484 1485 // Clipping parameters. skia transformation matrix does not give us clipping. 1486 // So we do it ourselves. 1487 gfx::Insets clip_insets_; 1488 1489 // Layout -------------------------------------------------------------------- 1490 1491 // Whether the view needs to be laid out. 1492 bool needs_layout_; 1493 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_; 1497 1498 // Painting ------------------------------------------------------------------ 1499 1500 // Background 1501 scoped_ptr<Background> background_; 1502 1503 // Border. 1504 scoped_ptr<Border> border_; 1505 1506 // RTL painting -------------------------------------------------------------- 1507 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_; 1512 1513 // Accelerated painting ------------------------------------------------------ 1514 1515 bool paint_to_layer_; 1516 1517 // Accelerators -------------------------------------------------------------- 1518 1519 // Focus manager accelerators registered on. 1520 FocusManager* accelerator_focus_manager_; 1521 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_; 1527 1528 // Focus --------------------------------------------------------------------- 1529 1530 // Next view to be focused when the Tab key is pressed. 1531 View* next_focusable_view_; 1532 1533 // Next view to be focused when the Shift-Tab key combination is pressed. 1534 View* previous_focusable_view_; 1535 1536 // Whether this view can be focused. 1537 bool focusable_; 1538 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_; 1542 1543 // Context menus ------------------------------------------------------------- 1544 1545 // The menu controller. 1546 ContextMenuController* context_menu_controller_; 1547 1548 // Drag and drop ------------------------------------------------------------- 1549 1550 DragController* drag_controller_; 1551 1552 // Input -------------------------------------------------------------------- 1553 1554 scoped_ptr<internal::PostEventDispatchHandler> post_dispatch_handler_; 1555 1556 // Accessibility ------------------------------------------------------------- 1557 1558 // Belongs to this view, but it's reference-counted on some platforms 1559 // so we can't use a scoped_ptr. It's dereferenced in the destructor. 1560 NativeViewAccessibility* native_view_accessibility_; 1561 1562 DISALLOW_COPY_AND_ASSIGN(View); 1563 }; 1564 1565 } // namespace views 1566 1567 #endif // UI_VIEWS_VIEW_H_ 1568