blob: ce9fcea2f98695c36a4c57d0cd8b870c1cf49722 [file] [log] [blame]
[email protected]8af4c1992010-02-04 21:38:071// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]2362e4f2009-05-08 00:34:055#ifndef VIEWS_VIEW_H_
6#define VIEWS_VIEW_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
initial.commit09911bf2008-07-26 23:55:298
[email protected]8c117712009-01-13 12:26:469#include "build/build_config.h"
10
[email protected]1bc83062009-02-06 00:16:3711#include <algorithm>
initial.commit09911bf2008-07-26 23:55:2912#include <map>
[email protected]134c47b92009-08-19 03:33:4413#include <set>
[email protected]1bc83062009-02-06 00:16:3714#include <string>
initial.commit09911bf2008-07-26 23:55:2915#include <vector>
16
[email protected]134c47b92009-08-19 03:33:4417#include "app/os_exchange_data.h"
[email protected]c2f4bdb72010-05-10 23:15:2118#include "base/i18n/rtl.h"
initial.commit09911bf2008-07-26 23:55:2919#include "base/scoped_ptr.h"
[email protected]5c7293a2010-03-17 06:40:5720#include "gfx/native_widget_types.h"
[email protected]e0fc2f12010-03-14 23:30:5921#include "gfx/rect.h"
[email protected]2362e4f2009-05-08 00:34:0522#include "views/accelerator.h"
[email protected]91e81ae2009-05-08 22:14:3823#include "views/accessibility/accessibility_types.h"
[email protected]2362e4f2009-05-08 00:34:0524#include "views/background.h"
25#include "views/border.h"
initial.commit09911bf2008-07-26 23:55:2926
[email protected]1eb89e82008-08-15 12:27:0327namespace gfx {
[email protected]82522512009-05-15 07:37:2928class Canvas;
[email protected]1eb89e82008-08-15 12:27:0329class Insets;
[email protected]82739cf2008-09-16 00:37:5630class Path;
[email protected]1eb89e82008-08-15 12:27:0331}
32
[email protected]4a190632009-05-09 01:07:4233class ThemeProvider;
[email protected]5fe15722010-10-22 00:03:2234class ViewAccessibility;
initial.commit09911bf2008-07-26 23:55:2935
[email protected]c2dacc92008-10-16 23:51:3836namespace views {
initial.commit09911bf2008-07-26 23:55:2937
38class Background;
39class Border;
40class FocusManager;
41class FocusTraversable;
42class LayoutManager;
43class RestoreFocusTask;
44class RootView;
45class ScrollView;
[email protected]a0dde122008-11-21 20:51:2046class Widget;
[email protected]cd8c47902009-04-30 20:55:3547class Window;
initial.commit09911bf2008-07-26 23:55:2948
49// ContextMenuController is responsible for showing the context menu for a
50// View. To use a ContextMenuController invoke SetContextMenuController on a
51// View. When the appropriate user gesture occurs ShowContextMenu is invoked
52// on the ContextMenuController.
53//
54// Setting a ContextMenuController on a view makes the view process mouse
55// events.
56//
57// It is up to subclasses that do their own mouse processing to invoke
58// the appropriate ContextMenuController method, typically by invoking super's
59// implementation for mouse processing.
60//
61class ContextMenuController {
62 public:
[email protected]e9adf0702010-03-08 23:34:0763 // Invoked to show the context menu for the source view. If |is_mouse_gesture|
64 // is true, |p| is the location of the mouse. If |is_mouse_gesture| is false,
65 // this method was not invoked by a mouse gesture and |p| is the recommended
66 // location to show the menu at.
initial.commit09911bf2008-07-26 23:55:2967 //
[email protected]e9adf0702010-03-08 23:34:0768 // |p| is in screen coordinates.
initial.commit09911bf2008-07-26 23:55:2969 virtual void ShowContextMenu(View* source,
[email protected]e9adf0702010-03-08 23:34:0770 const gfx::Point& p,
initial.commit09911bf2008-07-26 23:55:2971 bool is_mouse_gesture) = 0;
[email protected]20cb5f482009-12-16 01:01:2572
73 protected:
74 virtual ~ContextMenuController() {}
initial.commit09911bf2008-07-26 23:55:2975};
76
77// DragController is responsible for writing drag data for a view, as well as
78// supplying the supported drag operations. Use DragController if you don't
79// want to subclass.
80
81class DragController {
82 public:
83 // Writes the data for the drag.
84 virtual void WriteDragData(View* sender,
[email protected]e9adf0702010-03-08 23:34:0785 const gfx::Point& press_pt,
initial.commit09911bf2008-07-26 23:55:2986 OSExchangeData* data) = 0;
87
88 // Returns the supported drag operations (see DragDropTypes for possible
89 // values). A drag is only started if this returns a non-zero value.
[email protected]e9adf0702010-03-08 23:34:0790 virtual int GetDragOperations(View* sender, const gfx::Point& p) = 0;
initial.commit09911bf2008-07-26 23:55:2991
[email protected]b5f94de2009-12-04 07:59:0092 // Returns true if a drag operation can be started.
[email protected]e9adf0702010-03-08 23:34:0793 // |press_pt| represents the coordinates where the mouse was initially
94 // pressed down. |p| is the current mouse coordinates.
[email protected]b5f94de2009-12-04 07:59:0095 virtual bool CanStartDrag(View* sender,
[email protected]e9adf0702010-03-08 23:34:0796 const gfx::Point& press_pt,
97 const gfx::Point& p) = 0;
[email protected]20cb5f482009-12-16 01:01:2598
99 protected:
100 virtual ~DragController() {}
[email protected]b5f94de2009-12-04 07:59:00101};
initial.commit09911bf2008-07-26 23:55:29102
103/////////////////////////////////////////////////////////////////////////////
104//
105// View class
106//
[email protected]c2dacc92008-10-16 23:51:38107// A View is a rectangle within the views View hierarchy. It is the base
[email protected]1bc83062009-02-06 00:16:37108// class for all Views.
initial.commit09911bf2008-07-26 23:55:29109//
110// A View is a container of other Views (there is no such thing as a Leaf
111// View - makes code simpler, reduces type conversion headaches, design
112// mistakes etc)
113//
114// The View contains basic properties for sizing (bounds), layout (flex,
115// orientation, etc), painting of children and event dispatch.
116//
117// The View also uses a simple Box Layout Manager similar to XUL's
118// SprocketLayout system. Alternative Layout Managers implementing the
119// LayoutManager interface can be used to lay out children if required.
120//
121// It is up to the subclass to implement Painting and storage of subclass -
122// specific properties and functionality.
123//
124/////////////////////////////////////////////////////////////////////////////
125class View : public AcceleratorTarget {
126 public:
[email protected]6f3bb6c2008-09-17 22:25:33127 // Used in the versions of GetBounds() and x() that take a transformation
initial.commit09911bf2008-07-26 23:55:29128 // parameter in order to determine whether or not to take into account the
129 // mirroring setting of the View when returning bounds positions.
130 enum PositionMirroringSettings {
131 IGNORE_MIRRORING_TRANSFORMATION = 0,
132 APPLY_MIRRORING_TRANSFORMATION
133 };
134
135 // The view class name.
136 static char kViewClassName[];
137
138 View();
139 virtual ~View();
140
[email protected]aadcd1d92009-09-22 18:11:31141 // Returns the amount of time between double clicks, in milliseconds.
142 static int GetDoubleClickTimeMS();
143
[email protected]8af4c1992010-02-04 21:38:07144 // Returns the amount of time to wait from hovering over a menu button until
145 // showing the menu.
146 static int GetMenuShowDelay();
147
initial.commit09911bf2008-07-26 23:55:29148 // Sizing functions
149
150 // Get the bounds of the View, relative to the parent. Essentially, this
151 // function returns the bounds_ rectangle.
152 //
153 // This is the function subclasses should use whenever they need to obtain
154 // the bounds of one of their child views (for example, when implementing
155 // View::Layout()).
[email protected]24db2eb2009-07-17 17:54:16156 const gfx::Rect& bounds() const { return bounds_; }
[email protected]80f8b9f2008-10-16 18:17:47157
158 // Get the size of the View.
[email protected]24db2eb2009-07-17 17:54:16159 const gfx::Size& size() const { return bounds_.size(); }
initial.commit09911bf2008-07-26 23:55:29160
161 // Return the bounds of the View, relative to the parent. If
162 // |settings| is IGNORE_MIRRORING_TRANSFORMATION, the function returns the
[email protected]8f763a302009-11-11 00:47:32163 // bounds_ rectangle. If |settings| is APPLY_MIRRORING_TRANSFORMATION AND the
initial.commit09911bf2008-07-26 23:55:29164 // parent View is using a right-to-left UI layout, then the function returns
165 // a shifted version of the bounds_ rectangle that represents the mirrored
166 // View bounds.
167 //
168 // NOTE: in the vast majority of the cases, the mirroring implementation is
169 // transparent to the View subclasses and therefore you should use the
170 // version of GetBounds() which does not take a transformation settings
171 // parameter.
[email protected]0d8ea702008-10-14 17:03:07172 gfx::Rect GetBounds(PositionMirroringSettings settings) const;
initial.commit09911bf2008-07-26 23:55:29173
174 // Set the bounds in the parent's coordinate system.
[email protected]80f8b9f2008-10-16 18:17:47175 void SetBounds(const gfx::Rect& bounds);
176 void SetBounds(int x, int y, int width, int height) {
177 SetBounds(gfx::Rect(x, y, std::max(0, width), std::max(0, height)));
178 }
[email protected]6f3bb6c2008-09-17 22:25:33179 void SetX(int x) { SetBounds(x, y(), width(), height()); }
180 void SetY(int y) { SetBounds(x(), y, width(), height()); }
initial.commit09911bf2008-07-26 23:55:29181
182 // Returns the left coordinate of the View, relative to the parent View,
[email protected]80f8b9f2008-10-16 18:17:47183 // which is the value of bounds_.x().
initial.commit09911bf2008-07-26 23:55:29184 //
185 // This is the function subclasses should use whenever they need to obtain
186 // the left position of one of their child views (for example, when
187 // implementing View::Layout()).
[email protected]0a1d36b22008-10-17 19:33:09188 // This is equivalent to GetX(IGNORE_MIRRORING_TRANSFORMATION), but
189 // inlinable.
190 int x() const { return bounds_.x(); }
191 int y() const { return bounds_.y(); }
192 int width() const { return bounds_.width(); }
193 int height() const { return bounds_.height(); }
initial.commit09911bf2008-07-26 23:55:29194
195 // Return the left coordinate of the View, relative to the parent. If
196 // |settings| is IGNORE_MIRRORING_SETTINGS, the function returns the value of
[email protected]80f8b9f2008-10-16 18:17:47197 // bounds_.x(). If |settings| is APPLY_MIRRORING_SETTINGS AND the parent
initial.commit09911bf2008-07-26 23:55:29198 // View is using a right-to-left UI layout, then the function returns the
[email protected]80f8b9f2008-10-16 18:17:47199 // mirrored value of bounds_.x().
initial.commit09911bf2008-07-26 23:55:29200 //
201 // NOTE: in the vast majority of the cases, the mirroring implementation is
202 // transparent to the View subclasses and therefore you should use the
[email protected]6f3bb6c2008-09-17 22:25:33203 // paremeterless version of x() when you need to get the X
initial.commit09911bf2008-07-26 23:55:29204 // coordinate of a child View.
205 int GetX(PositionMirroringSettings settings) const;
206
initial.commit09911bf2008-07-26 23:55:29207 // Return this control local bounds. If include_border is true, local bounds
[email protected]6f3bb6c2008-09-17 22:25:33208 // is the rectangle {0, 0, width(), height()}, otherwise, it does not
initial.commit09911bf2008-07-26 23:55:29209 // include the area where the border (if any) is painted.
[email protected]80f8b9f2008-10-16 18:17:47210 gfx::Rect GetLocalBounds(bool include_border) const;
initial.commit09911bf2008-07-26 23:55:29211
212 // Get the position of the View, relative to the parent.
213 //
214 // Note that if the parent uses right-to-left UI layout, then the mirrored
[email protected]6f3bb6c2008-09-17 22:25:33215 // position of this View is returned. Use x()/y() if you want to ignore
initial.commit09911bf2008-07-26 23:55:29216 // mirroring.
[email protected]0a1d36b22008-10-17 19:33:09217 gfx::Point GetPosition() const;
initial.commit09911bf2008-07-26 23:55:29218
219 // Get the size the View would like to be, if enough space were available.
[email protected]154f8bc2008-10-15 18:02:30220 virtual gfx::Size GetPreferredSize();
initial.commit09911bf2008-07-26 23:55:29221
[email protected]a1360162009-11-30 21:19:07222 // Returns the baseline of this view, or -1 if this view has no baseline. The
223 // return value is relative to the preferred height.
224 virtual int GetBaseline();
225
initial.commit09911bf2008-07-26 23:55:29226 // Convenience method that sizes this view to its preferred size.
227 void SizeToPreferredSize();
228
229 // Gets the minimum size of the view. View's implementation invokes
230 // GetPreferredSize.
[email protected]154f8bc2008-10-15 18:02:30231 virtual gfx::Size GetMinimumSize();
initial.commit09911bf2008-07-26 23:55:29232
233 // Return the height necessary to display this view with the provided width.
234 // View's implementation returns the value from getPreferredSize.cy.
235 // Override if your View's preferred height depends upon the width (such
236 // as with Labels).
237 virtual int GetHeightForWidth(int w);
238
239 // This method is invoked when this object size or position changes.
240 // The default implementation does nothing.
[email protected]80f8b9f2008-10-16 18:17:47241 virtual void DidChangeBounds(const gfx::Rect& previous,
242 const gfx::Rect& current);
initial.commit09911bf2008-07-26 23:55:29243
244 // Set whether the receiving view is visible. Painting is scheduled as needed
245 virtual void SetVisible(bool flag);
246
247 // Return whether a view is visible
248 virtual bool IsVisible() const { return is_visible_; }
249
250 // Return whether a view and its ancestors are visible. Returns true if the
251 // path from this view to the root view is visible.
252 virtual bool IsVisibleInRootView() const;
253
254 // Set whether this view is enabled. A disabled view does not receive keyboard
255 // or mouse inputs. If flag differs from the current value, SchedulePaint is
256 // invoked.
257 virtual void SetEnabled(bool flag);
258
259 // Returns whether the view is enabled.
260 virtual bool IsEnabled() const;
261
262 // Set whether this view is hottracked. A disabled view cannot be hottracked.
263 // If flag differs from the current value, SchedulePaint is invoked.
264 virtual void SetHotTracked(bool flag);
265
266 // Returns whether the view is hot-tracked.
267 virtual bool IsHotTracked() const { return false; }
268
[email protected]b82a0492010-05-27 23:00:03269 virtual Widget* child_widget() { return NULL; }
270
initial.commit09911bf2008-07-26 23:55:29271 // Returns whether the view is pushed.
272 virtual bool IsPushed() const { return false; }
273
274 // Scrolls the specified region, in this View's coordinate system, to be
275 // visible. View's implementation passes the call onto the parent View (after
276 // adjusting the coordinates). It is up to views that only show a portion of
277 // the child view, such as Viewport, to override appropriately.
[email protected]e9adf0702010-03-08 23:34:07278 virtual void ScrollRectToVisible(const gfx::Rect& rect);
initial.commit09911bf2008-07-26 23:55:29279
280 // Layout functions
281
282 // Lay out the child Views (set their bounds based on sizing heuristics
283 // specific to the current Layout Manager)
284 virtual void Layout();
285
[email protected]9ea053e2010-07-15 08:19:05286 // Mark this view and all parents to require a relayout. This ensures the
287 // next call to Layout() will propagate to this view, even if the bounds of
288 // parent views do not change.
289 void InvalidateLayout();
290
initial.commit09911bf2008-07-26 23:55:29291 // Gets/Sets the Layout Manager used by this view to size and place its
292 // children.
293 // The LayoutManager is owned by the View and is deleted when the view is
294 // deleted, or when a new LayoutManager is installed.
295 LayoutManager* GetLayoutManager() const;
296 void SetLayoutManager(LayoutManager* layout);
297
298 // Right-to-left UI layout functions
299
[email protected]82522512009-05-15 07:37:29300 // This method determines whether the gfx::Canvas object passed to
initial.commit09911bf2008-07-26 23:55:29301 // View::Paint() needs to be transformed such that anything drawn on the
302 // canvas object during View::Paint() is flipped horizontally.
303 //
304 // By default, this function returns false (which is the initial value of
305 // |flip_canvas_on_paint_for_rtl_ui_|). View subclasses that need to paint on
[email protected]82522512009-05-15 07:37:29306 // a flipped gfx::Canvas when the UI layout is right-to-left need to call
initial.commit09911bf2008-07-26 23:55:29307 // EnableCanvasFlippingForRTLUI().
308 bool FlipCanvasOnPaintForRTLUI() const {
[email protected]c2f4bdb72010-05-10 23:15:21309 return flip_canvas_on_paint_for_rtl_ui_ ? base::i18n::IsRTL() : false;
initial.commit09911bf2008-07-26 23:55:29310 }
311
[email protected]82522512009-05-15 07:37:29312 // Enables or disables flipping of the gfx::Canvas during View::Paint().
initial.commit09911bf2008-07-26 23:55:29313 // Note that if canvas flipping is enabled, the canvas will be flipped only
314 // if the UI layout is right-to-left; that is, the canvas will be flipped
[email protected]c2f4bdb72010-05-10 23:15:21315 // only if base::i18n::IsRTL() returns true.
initial.commit09911bf2008-07-26 23:55:29316 //
317 // Enabling canvas flipping is useful for leaf views that draw a bitmap that
318 // needs to be flipped horizontally when the UI layout is right-to-left
[email protected]c2dacc92008-10-16 23:51:38319 // (views::Button, for example). This method is helpful for such classes
320 // because their drawing logic stays the same and they can become agnostic to
321 // the UI directionality.
initial.commit09911bf2008-07-26 23:55:29322 void EnableCanvasFlippingForRTLUI(bool enable) {
323 flip_canvas_on_paint_for_rtl_ui_ = enable;
324 }
325
326 // Returns the mirrored X position for the view, relative to the parent. If
327 // the parent view is not mirrored, this function returns bound_.left.
328 //
329 // UI mirroring is transparent to most View subclasses and therefore there is
330 // no need to call this routine from anywhere within your subclass
331 // implementation.
[email protected]63329982008-10-10 21:56:57332 int MirroredX() const;
initial.commit09911bf2008-07-26 23:55:29333
334 // Given a rectangle specified in this View's coordinate system, the function
335 // computes the 'left' value for the mirrored rectangle within this View. If
336 // the View's UI layout is not right-to-left, then bounds.x() is returned.
337 //
338 // UI mirroring is transparent to most View subclasses and therefore there is
339 // no need to call this routine from anywhere within your subclass
340 // implementation.
341 int MirroredLeftPointForRect(const gfx::Rect& rect) const;
342
343 // Given the X coordinate of a point inside the View, this function returns
344 // the mirrored X coordinate of the point if the View's UI layout is
345 // right-to-left. If the layout is left-to-right, the same X coordinate is
346 // returned.
347 //
348 // Following are a few examples of the values returned by this function for
349 // a View with the bounds {0, 0, 100, 100} and a right-to-left layout:
350 //
351 // MirroredXCoordinateInsideView(0) -> 100
352 // MirroredXCoordinateInsideView(20) -> 80
353 // MirroredXCoordinateInsideView(99) -> 1
354 int MirroredXCoordinateInsideView(int x) const {
[email protected]c2f4bdb72010-05-10 23:15:21355 return base::i18n::IsRTL() ? width() - x : x;
initial.commit09911bf2008-07-26 23:55:29356 }
357
[email protected]14da3dff2009-06-12 18:01:47358 // Given a X coordinate and a width inside the View, this function returns
359 // the mirrored X coordinate if the View's UI layout is right-to-left. If the
360 // layout is left-to-right, the same X coordinate is returned.
361 //
362 // Following are a few examples of the values returned by this function for
363 // a View with the bounds {0, 0, 100, 100} and a right-to-left layout:
364 //
365 // MirroredXCoordinateInsideView(0, 10) -> 90
366 // MirroredXCoordinateInsideView(20, 20) -> 60
367 int MirroredXWithWidthInsideView(int x, int w) const {
[email protected]c2f4bdb72010-05-10 23:15:21368 return base::i18n::IsRTL() ? width() - x - w : x;
[email protected]14da3dff2009-06-12 18:01:47369 }
370
initial.commit09911bf2008-07-26 23:55:29371 // Painting functions
372
373 // Mark the specified rectangle as dirty (needing repaint). If |urgent| is
374 // true, the view will be repainted when the current event processing is
375 // done. Otherwise, painting will take place as soon as possible.
[email protected]0a1d36b22008-10-17 19:33:09376 virtual void SchedulePaint(const gfx::Rect& r, bool urgent);
initial.commit09911bf2008-07-26 23:55:29377
378 // Mark the entire View's bounds as dirty. Painting will occur as soon as
379 // possible.
380 virtual void SchedulePaint();
381
initial.commit09911bf2008-07-26 23:55:29382 // Paint the receiving view. g is prepared such as it is in
383 // receiver's coordinate system. g's state is restored after this
384 // call so your implementation can change the graphics configuration
385 //
386 // Default implementation paints the background if it is defined
387 //
388 // Override this method when implementing a new control.
[email protected]82522512009-05-15 07:37:29389 virtual void Paint(gfx::Canvas* canvas);
initial.commit09911bf2008-07-26 23:55:29390
391 // Paint the background if any. This method is called by Paint() and
392 // should rarely be invoked directly.
[email protected]82522512009-05-15 07:37:29393 virtual void PaintBackground(gfx::Canvas* canvas);
initial.commit09911bf2008-07-26 23:55:29394
395 // Paint the border if any. This method is called by Paint() and
396 // should rarely be invoked directly.
[email protected]82522512009-05-15 07:37:29397 virtual void PaintBorder(gfx::Canvas* canvas);
initial.commit09911bf2008-07-26 23:55:29398
399 // Paints the focus border (only if the view has the focus).
400 // This method is called by Paint() and should rarely be invoked directly.
401 // The default implementation paints a gray border around the view. Override
402 // it for custom focus effects.
[email protected]82522512009-05-15 07:37:29403 virtual void PaintFocusBorder(gfx::Canvas* canvas);
initial.commit09911bf2008-07-26 23:55:29404
405 // Paint this View immediately.
406 virtual void PaintNow();
407
initial.commit09911bf2008-07-26 23:55:29408 // Tree functions
409
410 // Add a child View.
411 void AddChildView(View* v);
412
413 // Adds a child View at the specified position.
414 void AddChildView(int index, View* v);
415
416 // Get the child View at the specified index.
417 View* GetChildViewAt(int index) const;
418
419 // Remove a child view from this view. v's parent will change to NULL
420 void RemoveChildView(View *v);
421
422 // Remove all child view from this view. If |delete_views| is true, the views
423 // are deleted, unless marked as not parent owned.
424 void RemoveAllChildViews(bool delete_views);
425
426 // Get the number of child Views.
427 int GetChildViewCount() const;
428
[email protected]3a9c26f2010-03-12 21:04:39429 // Tests if this view has a given view as direct child.
430 bool HasChildView(View* a_view);
431
[email protected]24db2eb2009-07-17 17:54:16432 // Returns the deepest descendant that contains the specified point.
[email protected]613b8062008-10-14 23:45:09433 virtual View* GetViewForPoint(const gfx::Point& point);
initial.commit09911bf2008-07-26 23:55:29434
[email protected]a0dde122008-11-21 20:51:20435 // Get the Widget that hosts this View, if any.
436 virtual Widget* GetWidget() const;
initial.commit09911bf2008-07-26 23:55:29437
[email protected]cd8c47902009-04-30 20:55:35438 // Gets the Widget that most closely contains this View, if any.
[email protected]08a3b712009-10-14 18:03:47439 // NOTE: almost all views displayed on screen have a Widget, but not
440 // necessarily a Window. This is due to widgets being able to create top
441 // level windows (as is done for popups, bubbles and menus).
[email protected]cd8c47902009-04-30 20:55:35442 virtual Window* GetWindow() const;
443
[email protected]2db27be2010-02-10 22:46:47444 // Returns true if the native view |native_view| is contained in the view
445 // hierarchy beneath this view.
446 virtual bool ContainsNativeView(gfx::NativeView native_view) const;
447
initial.commit09911bf2008-07-26 23:55:29448 // Get the containing RootView
449 virtual RootView* GetRootView();
450
451 // Get the parent View
452 View* GetParent() const { return parent_; }
453
454 // Returns the index of the specified |view| in this view's children, or -1
455 // if the specified view is not a child of this view.
[email protected]5e26d9d42010-05-13 22:11:03456 int GetChildIndex(const View* v) const;
initial.commit09911bf2008-07-26 23:55:29457
458 // Returns true if the specified view is a direct or indirect child of this
459 // view.
460 bool IsParentOf(View* v) const;
461
462 // Recursively descends the view tree starting at this view, and returns
463 // the first child that it encounters that has the given ID.
464 // Returns NULL if no matching child view is found.
465 virtual View* GetViewByID(int id) const;
466
467 // Sets and gets the ID for this view. ID should be unique within the subtree
468 // that you intend to search for it. 0 is the default ID for views.
469 void SetID(int id);
470 int GetID() const;
471
472 // A group id is used to tag views which are part of the same logical group.
473 // Focus can be moved between views with the same group using the arrow keys.
474 // Groups are currently used to implement radio button mutual exclusion.
[email protected]96f960d2009-09-14 18:45:30475 // The group id is immutable once it's set.
initial.commit09911bf2008-07-26 23:55:29476 void SetGroup(int gid);
[email protected]96f960d2009-09-14 18:45:30477 // Returns the group id of the view, or -1 if the id is not set yet.
initial.commit09911bf2008-07-26 23:55:29478 int GetGroup() const;
479
480 // If this returns true, the views from the same group can each be focused
481 // when moving focus with the Tab/Shift-Tab key. If this returns false,
482 // only the selected view from the group (obtained with
483 // GetSelectedViewForGroup()) is focused.
484 virtual bool IsGroupFocusTraversable() const { return true; }
485
486 // Fills the provided vector with all the available views which belong to the
487 // provided group.
488 void GetViewsWithGroup(int group_id, std::vector<View*>* out);
489
490 // Return the View that is currently selected in the specified group.
491 // The default implementation simply returns the first View found for that
492 // group.
493 virtual View* GetSelectedViewForGroup(int group_id);
494
495 // Focus support
496 //
497 // Returns the view that should be selected next when pressing Tab.
498 View* GetNextFocusableView();
499
500 // Returns the view that should be selected next when pressing Shift-Tab.
501 View* GetPreviousFocusableView();
502
503 // Sets the component that should be selected next when pressing Tab, and
504 // makes the current view the precedent view of the specified one.
505 // Note that by default views are linked in the order they have been added to
506 // their container. Use this method if you want to modify the order.
507 // IMPORTANT NOTE: loops in the focus hierarchy are not supported.
508 void SetNextFocusableView(View* view);
509
initial.commit09911bf2008-07-26 23:55:29510 // Sets whether this view can accept the focus.
511 // Note that this is false by default so that a view used as a container does
512 // not get the focus.
513 virtual void SetFocusable(bool focusable);
514
[email protected]59461d22010-07-21 15:41:09515 // Returns true if the view is focusable (IsFocusable) and visible in the root
516 // view. See also IsFocusable.
517 bool IsFocusableInRootView() const;
518
519 // Return whether this view is focusable when the user requires full keyboard
520 // access, even though it may not be normally focusable.
521 bool IsAccessibilityFocusableInRootView() const;
[email protected]83548a42010-06-18 13:53:37522
523 // Set whether this view can be made focusable if the user requires
524 // full keyboard access, even though it's not normally focusable.
525 // Note that this is false by default.
526 virtual void set_accessibility_focusable(bool accessibility_focusable) {
527 accessibility_focusable_ = accessibility_focusable;
528 }
529
initial.commit09911bf2008-07-26 23:55:29530 // Convenience method to retrieve the FocusManager associated with the
[email protected]a0dde122008-11-21 20:51:20531 // Widget that contains this view. This can return NULL if this view is not
532 // part of a view hierarchy with a Widget.
initial.commit09911bf2008-07-26 23:55:29533 virtual FocusManager* GetFocusManager();
534
535 // Sets a keyboard accelerator for that view. When the user presses the
536 // accelerator key combination, the AcceleratorPressed method is invoked.
537 // Note that you can set multiple accelerators for a view by invoking this
538 // method several times.
539 virtual void AddAccelerator(const Accelerator& accelerator);
540
[email protected]e8e0f362008-11-08 01:13:25541 // Removes the specified accelerator for this view.
542 virtual void RemoveAccelerator(const Accelerator& accelerator);
543
initial.commit09911bf2008-07-26 23:55:29544 // Removes all the keyboard accelerators for this view.
545 virtual void ResetAccelerators();
546
547 // Called when a keyboard accelerator is pressed.
548 // Derived classes should implement desired behavior and return true if they
549 // handled the accelerator.
550 virtual bool AcceleratorPressed(const Accelerator& accelerator) {
551 return false;
552 }
553
initial.commit09911bf2008-07-26 23:55:29554 // Returns whether this view currently has the focus.
555 virtual bool HasFocus();
556
557 // Accessibility support
[email protected]4c671e12010-09-28 19:30:23558 // TODO(ctguil): Move all this out to a AccessibleInfo wrapper class.
[email protected]98703262010-06-18 23:53:41559
560 // Notify the platform specific accessibility client of changes in the user
[email protected]ddd919e2010-07-30 17:32:17561 // interface. This will always raise native notifications.
[email protected]98703262010-06-18 23:53:41562 virtual void NotifyAccessibilityEvent(AccessibilityTypes::Event event_type);
563
[email protected]ddd919e2010-07-30 17:32:17564 // Raise an accessibility notification with an option to also raise a native
565 // notification.
566 virtual void NotifyAccessibilityEvent(AccessibilityTypes::Event event_type,
567 bool send_native_event);
568
initial.commit09911bf2008-07-26 23:55:29569 // Returns the MSAA default action of the current view. The string returned
570 // describes the default action that will occur when executing
571 // IAccessible::DoDefaultAction. For instance, default action of a button is
[email protected]4c671e12010-09-28 19:30:23572 // 'Press'.
573 virtual std::wstring GetAccessibleDefaultAction() { return std::wstring(); }
initial.commit09911bf2008-07-26 23:55:29574
575 // Returns a string containing the mnemonic, or the keyboard shortcut, for a
[email protected]4c671e12010-09-28 19:30:23576 // given control.
577 virtual std::wstring GetAccessibleKeyboardShortcut() {
578 return std::wstring();
initial.commit09911bf2008-07-26 23:55:29579 }
580
581 // Returns a brief, identifying string, containing a unique, readable name of
582 // a given control. Sets the input string appropriately, and returns true if
583 // successful.
[email protected]d5c81012010-04-03 00:56:12584 bool GetAccessibleName(std::wstring* name);
initial.commit09911bf2008-07-26 23:55:29585
[email protected]e92070ac2009-04-28 00:12:01586 // Returns the accessibility role of the current view. The role is what
587 // assistive technologies (ATs) use to determine what behavior to expect from
[email protected]4c671e12010-09-28 19:30:23588 // a given control.
589 virtual AccessibilityTypes::Role GetAccessibleRole();
initial.commit09911bf2008-07-26 23:55:29590
[email protected]4c671e12010-09-28 19:30:23591 // Returns the accessibility state of the current view.
592 virtual AccessibilityTypes::State GetAccessibleState() {
593 return 0;
[email protected]e92070ac2009-04-28 00:12:01594 }
initial.commit09911bf2008-07-26 23:55:29595
[email protected]4c671e12010-09-28 19:30:23596 // Returns the current value associated with a view.
597 virtual std::wstring GetAccessibleValue() { return std::wstring(); }
[email protected]a9d59462010-03-26 21:51:04598
initial.commit09911bf2008-07-26 23:55:29599 // Assigns a string name to the given control. Needed as a View does not know
600 // which name will be associated with it until it is created to be a
601 // certain type.
[email protected]d5c81012010-04-03 00:56:12602 void SetAccessibleName(const std::wstring& name);
[email protected]a9d59462010-03-26 21:51:04603
[email protected]5fe15722010-10-22 00:03:22604 // Returns an instance of the (platform-specific) accessibility interface for
605 // the View.
606 ViewAccessibility* GetViewAccessibility();
initial.commit09911bf2008-07-26 23:55:29607
initial.commit09911bf2008-07-26 23:55:29608 // Utility functions
609
610 // Note that the utility coordinate conversions functions always operate on
611 // the mirrored position of the child Views if the parent View uses a
612 // right-to-left UI layout.
613
614 // Convert a point from source coordinate system to dst coordinate system.
615 //
616 // source is a parent or a child of dst, directly or transitively.
617 // If source and dst are not in the same View hierarchy, the result is
618 // undefined.
619 // Source can be NULL in which case it means the screen coordinate system
[email protected]bb515ed2009-01-15 00:53:43620 static void ConvertPointToView(const View* src,
621 const View* dst,
initial.commit09911bf2008-07-26 23:55:29622 gfx::Point* point);
initial.commit09911bf2008-07-26 23:55:29623
624 // Convert a point from the coordinate system of a View to that of the
[email protected]a0dde122008-11-21 20:51:20625 // Widget. This is useful for example when sizing HWND children of the
626 // Widget that don't know about the View hierarchy and need to be placed
627 // relative to the Widget that is their parent.
[email protected]2fb6d462009-02-13 18:40:10628 static void ConvertPointToWidget(const View* src, gfx::Point* point);
initial.commit09911bf2008-07-26 23:55:29629
[email protected]a0dde122008-11-21 20:51:20630 // Convert a point from a view Widget to a View dest
[email protected]2fb6d462009-02-13 18:40:10631 static void ConvertPointFromWidget(const View* dest, gfx::Point* p);
initial.commit09911bf2008-07-26 23:55:29632
633 // Convert a point from the coordinate system of a View to that of the
634 // screen. This is useful for example when placing popup windows.
[email protected]2fb6d462009-02-13 18:40:10635 static void ConvertPointToScreen(const View* src, gfx::Point* point);
initial.commit09911bf2008-07-26 23:55:29636
[email protected]a0949a42010-08-23 16:03:05637 // Return the bounds of the View in screen coordinate system.
638 gfx::Rect GetScreenBounds() const;
639
initial.commit09911bf2008-07-26 23:55:29640 // Event Handlers
641
642 // This method is invoked when the user clicks on this view.
643 // The provided event is in the receiver's coordinate system.
644 //
645 // Return true if you processed the event and want to receive subsequent
646 // MouseDraggged and MouseReleased events. This also stops the event from
647 // bubbling. If you return false, the event will bubble through parent
648 // views.
649 //
650 // If you remove yourself from the tree while processing this, event bubbling
651 // stops as if you returned true, but you will not receive future events.
652 // The return value is ignored in this case.
653 //
654 // Default implementation returns true if a ContextMenuController has been
655 // set, false otherwise. Override as needed.
656 //
657 virtual bool OnMousePressed(const MouseEvent& event);
658
659 // This method is invoked when the user clicked on this control.
660 // and is still moving the mouse with a button pressed.
661 // The provided event is in the receiver's coordinate system.
662 //
663 // Return true if you processed the event and want to receive
664 // subsequent MouseDragged and MouseReleased events.
665 //
666 // Default implementation returns true if a ContextMenuController has been
667 // set, false otherwise. Override as needed.
668 //
669 virtual bool OnMouseDragged(const MouseEvent& event);
670
671 // This method is invoked when the user releases the mouse
672 // button. The event is in the receiver's coordinate system.
673 //
674 // If canceled is true it indicates the mouse press/drag was canceled by a
675 // system/user gesture.
676 //
677 // Default implementation notifies the ContextMenuController is appropriate.
678 // Subclasses that wish to honor the ContextMenuController should invoke
679 // super.
680 virtual void OnMouseReleased(const MouseEvent& event, bool canceled);
681
682 // This method is invoked when the mouse is above this control
683 // The event is in the receiver's coordinate system.
684 //
685 // Default implementation does nothing. Override as needed.
686 virtual void OnMouseMoved(const MouseEvent& e);
687
688 // This method is invoked when the mouse enters this control.
689 //
690 // Default implementation does nothing. Override as needed.
691 virtual void OnMouseEntered(const MouseEvent& event);
692
693 // This method is invoked when the mouse exits this control
694 // The provided event location is always (0, 0)
695 // Default implementation does nothing. Override as needed.
696 virtual void OnMouseExited(const MouseEvent& event);
697
[email protected]ad84c8f2010-09-08 14:06:10698#if defined(TOUCH_UI)
699 // This method is invoked for each touch event. Default implementation
700 // does nothing. Override as needed.
701 virtual bool OnTouchEvent(const TouchEvent& event);
702#endif
703
initial.commit09911bf2008-07-26 23:55:29704 // Set the MouseHandler for a drag session.
705 //
706 // A drag session is a stream of mouse events starting
707 // with a MousePressed event, followed by several MouseDragged
708 // events and finishing with a MouseReleased event.
709 //
710 // This method should be only invoked while processing a
[email protected]ad84c8f2010-09-08 14:06:10711 // MouseDragged or MousePressed event.
initial.commit09911bf2008-07-26 23:55:29712 //
713 // All further mouse dragged and mouse up events will be sent
714 // the MouseHandler, even if it is reparented to another window.
715 //
716 // The MouseHandler is automatically cleared when the control
717 // comes back from processing the MouseReleased event.
718 //
719 // Note: if the mouse handler is no longer connected to a
720 // view hierarchy, events won't be sent.
721 //
722 virtual void SetMouseHandler(View* new_mouse_handler);
723
724 // Request the keyboard focus. The receiving view will become the
725 // focused view.
726 virtual void RequestFocus();
727
728 // Invoked when a view is about to gain focus
729 virtual void WillGainFocus();
730
731 // Invoked when a view just gained focus.
732 virtual void DidGainFocus();
733
734 // Invoked when a view is about lose focus
735 virtual void WillLoseFocus();
736
737 // Invoked when a view is about to be requested for focus due to the focus
738 // traversal. Reverse is this request was generated going backward
739 // (Shift-Tab).
740 virtual void AboutToRequestFocusFromTabTraversal(bool reverse) { }
741
[email protected]ca13d804c2009-05-14 04:28:07742 // Invoked when a key is pressed before the key event is processed (and
743 // potentially eaten) by the focus manager for tab traversal, accelerators and
744 // other focus related actions.
745 // The default implementation returns false, ensuring that tab traversal and
746 // accelerators processing is performed.
747 // Subclasses should return true if they want to process the key event and not
748 // have it processed as an accelerator (if any) or as a tab traversal (if the
749 // key event is for the TAB key). In that case, OnKeyPressed will
750 // subsequently be invoked for that event.
751 virtual bool SkipDefaultKeyEventProcessing(const KeyEvent& e) {
752 return false;
753 }
754
initial.commit09911bf2008-07-26 23:55:29755 // Invoked when a key is pressed or released.
756 // Subclasser should return true if the event has been processed and false
757 // otherwise. If the event has not been processed, the parent will be given a
758 // chance.
759 virtual bool OnKeyPressed(const KeyEvent& e);
760 virtual bool OnKeyReleased(const KeyEvent& e);
761
initial.commit09911bf2008-07-26 23:55:29762 // Invoked when the user uses the mousewheel. Implementors should return true
763 // if the event has been processed and false otherwise. This message is sent
764 // if the view is focused. If the event has not been processed, the parent
765 // will be given a chance.
766 virtual bool OnMouseWheel(const MouseWheelEvent& e);
767
768 // Drag and drop functions.
769
770 // Set/get the DragController. See description of DragController for more
771 // information.
772 void SetDragController(DragController* drag_controller);
773 DragController* GetDragController();
774
775 // During a drag and drop session when the mouse moves the view under the
[email protected]134c47b92009-08-19 03:33:44776 // mouse is queried for the drop types it supports by way of the
777 // GetDropFormats methods. If the view returns true and the drag site can
778 // provide data in one of the formats, the view is asked if the drop data
779 // is required before any other drop events are sent. Once the
780 // data is available the view is asked if it supports the drop (by way of
781 // the CanDrop method). If a view returns true from CanDrop,
initial.commit09911bf2008-07-26 23:55:29782 // OnDragEntered is sent to the view when the mouse first enters the view,
783 // as the mouse moves around within the view OnDragUpdated is invoked.
784 // If the user releases the mouse over the view and OnDragUpdated returns a
785 // valid drop, then OnPerformDrop is invoked. If the mouse moves outside the
786 // view or over another view that wants the drag, OnDragExited is invoked.
787 //
788 // Similar to mouse events, the deepest view under the mouse is first checked
789 // if it supports the drop (Drop). If the deepest view under
790 // the mouse does not support the drop, the ancestors are walked until one
791 // is found that supports the drop.
792
[email protected]134c47b92009-08-19 03:33:44793 // Override and return the set of formats that can be dropped on this view.
794 // |formats| is a bitmask of the formats defined bye OSExchangeData::Format.
795 // The default implementation returns false, which means the view doesn't
796 // support dropping.
797 virtual bool GetDropFormats(
798 int* formats,
799 std::set<OSExchangeData::CustomFormat>* custom_formats);
800
801 // Override and return true if the data must be available before any drop
802 // methods should be invoked. The default is false.
803 virtual bool AreDropTypesRequired();
804
initial.commit09911bf2008-07-26 23:55:29805 // A view that supports drag and drop must override this and return true if
806 // data contains a type that may be dropped on this view.
807 virtual bool CanDrop(const OSExchangeData& data);
808
809 // OnDragEntered is invoked when the mouse enters this view during a drag and
810 // drop session and CanDrop returns true. This is immediately
811 // followed by an invocation of OnDragUpdated, and eventually one of
812 // OnDragExited or OnPerformDrop.
813 virtual void OnDragEntered(const DropTargetEvent& event);
814
815 // Invoked during a drag and drop session while the mouse is over the view.
816 // This should return a bitmask of the DragDropTypes::DragOperation supported
817 // based on the location of the event. Return 0 to indicate the drop should
818 // not be accepted.
819 virtual int OnDragUpdated(const DropTargetEvent& event);
820
821 // Invoked during a drag and drop session when the mouse exits the views, or
822 // when the drag session was canceled and the mouse was over the view.
823 virtual void OnDragExited();
824
825 // Invoked during a drag and drop session when OnDragUpdated returns a valid
826 // operation and the user release the mouse.
827 virtual int OnPerformDrop(const DropTargetEvent& event);
828
829 // Returns true if the mouse was dragged enough to start a drag operation.
830 // delta_x and y are the distance the mouse was dragged.
831 static bool ExceededDragThreshold(int delta_x, int delta_y);
832
833 // This method is the main entry point to process paint for this
834 // view and its children. This method is called by the painting
835 // system. You should call this only if you want to draw a sub tree
836 // inside a custom graphics.
837 // To customize painting override either the Paint or PaintChildren method,
838 // not this one.
[email protected]82522512009-05-15 07:37:29839 virtual void ProcessPaint(gfx::Canvas* canvas);
initial.commit09911bf2008-07-26 23:55:29840
841 // Paint the View's child Views, in reverse order.
[email protected]82522512009-05-15 07:37:29842 virtual void PaintChildren(gfx::Canvas* canvas);
initial.commit09911bf2008-07-26 23:55:29843
844 // Sets the ContextMenuController. Setting this to non-null makes the View
845 // process mouse events.
846 void SetContextMenuController(ContextMenuController* menu_controller);
847 ContextMenuController* GetContextMenuController() {
848 return context_menu_controller_;
849 }
850
[email protected]042811c2008-10-31 21:31:34851 // Provides default implementation for context menu handling. The default
852 // implementation calls the ShowContextMenu of the current
853 // ContextMenuController (if it is not NULL). Overridden in subclassed views
854 // to provide right-click menu display triggerd by the keyboard (i.e. for the
855 // Chrome toolbar Back and Forward buttons). No source needs to be specified,
856 // as it is always equal to the current View.
[email protected]e9adf0702010-03-08 23:34:07857 virtual void ShowContextMenu(const gfx::Point& p,
[email protected]042811c2008-10-31 21:31:34858 bool is_mouse_gesture);
859
[email protected]9a3f0ac22008-11-14 03:24:02860 // The background object is owned by this object and may be NULL.
861 void set_background(Background* b) { background_.reset(b); }
862 const Background* background() const { return background_.get(); }
initial.commit09911bf2008-07-26 23:55:29863
[email protected]9a3f0ac22008-11-14 03:24:02864 // The border object is owned by this object and may be NULL.
865 void set_border(Border* b) { border_.reset(b); }
866 const Border* border() const { return border_.get(); }
initial.commit09911bf2008-07-26 23:55:29867
868 // Returns the insets of the current border. If there is no border an empty
869 // insets is returned.
[email protected]a37bea82009-04-22 23:02:15870 virtual gfx::Insets GetInsets() const;
initial.commit09911bf2008-07-26 23:55:29871
872 // Return the cursor that should be used for this view or NULL if
873 // the default cursor should be used. The provided point is in the
[email protected]9abf8dd62009-06-04 06:40:42874 // receiver's coordinate system. The caller is responsible for managing the
875 // lifetime of the returned object, though that lifetime may vary from
876 // platform to platform. On Windows, the cursor is a shared resource but in
877 // Gtk, the framework destroys the returned cursor after setting it.
878 virtual gfx::NativeCursor GetCursorForPoint(Event::EventType event_type,
[email protected]e9adf0702010-03-08 23:34:07879 const gfx::Point& p);
initial.commit09911bf2008-07-26 23:55:29880
881 // Convenience to test whether a point is within this view's bounds
[email protected]613b8062008-10-14 23:45:09882 virtual bool HitTest(const gfx::Point& l) const;
initial.commit09911bf2008-07-26 23:55:29883
884 // Gets the tooltip for this View. If the View does not have a tooltip,
885 // return false. If the View does have a tooltip, copy the tooltip into
886 // the supplied string and return true.
887 // Any time the tooltip text that a View is displaying changes, it must
888 // invoke TooltipTextChanged.
[email protected]e9adf0702010-03-08 23:34:07889 // |p| provides the coordinates of the mouse (relative to this view).
890 virtual bool GetTooltipText(const gfx::Point& p, std::wstring* tooltip);
initial.commit09911bf2008-07-26 23:55:29891
892 // Returns the location (relative to this View) for the text on the tooltip
893 // to display. If false is returned (the default), the tooltip is placed at
894 // a default position.
[email protected]e9adf0702010-03-08 23:34:07895 virtual bool GetTooltipTextOrigin(const gfx::Point& p, gfx::Point* loc);
initial.commit09911bf2008-07-26 23:55:29896
897 // Set whether this view is owned by its parent. A view that is owned by its
898 // parent is automatically deleted when the parent is deleted. The default is
899 // true. Set to false if the view is owned by another object and should not
900 // be deleted by its parent.
[email protected]09fe9492009-11-07 02:23:06901 void set_parent_owned(bool is_parent_owned) {
902 is_parent_owned_ = is_parent_owned;
903 }
initial.commit09911bf2008-07-26 23:55:29904
[email protected]09fe9492009-11-07 02:23:06905 // Return whether a view is owned by its parent.
906 bool IsParentOwned() const { return is_parent_owned_; }
initial.commit09911bf2008-07-26 23:55:29907
908 // Return the receiving view's class name. A view class is a string which
909 // uniquely identifies the view class. It is intended to be used as a way to
910 // find out during run time if a view can be safely casted to a specific view
911 // subclass. The default implementation returns kViewClassName.
912 virtual std::string GetClassName() const;
913
[email protected]5c2b98b2009-03-09 20:55:54914 // Returns the first ancestor, starting at this, whose class name is |name|.
915 // Returns null if no ancestor has the class name |name|.
916 View* GetAncestorWithClassName(const std::string& name);
917
initial.commit09911bf2008-07-26 23:55:29918 // Returns the visible bounds of the receiver in the receivers coordinate
919 // system.
920 //
921 // When traversing the View hierarchy in order to compute the bounds, the
922 // function takes into account the mirroring setting for each View and
923 // therefore it will return the mirrored version of the visible bounds if
924 // need be.
925 gfx::Rect GetVisibleBounds();
926
927 // Subclasses that contain traversable children that are not directly
928 // accessible through the children hierarchy should return the associated
929 // FocusTraversable for the focus traversal to work properly.
930 virtual FocusTraversable* GetFocusTraversable() { return NULL; }
931
[email protected]83548a42010-06-18 13:53:37932 // Subclasses that can act as a "pane" must implement their own
933 // FocusTraversable to keep the focus trapped within the pane.
934 // If this method returns an object, any view that's a direct or
935 // indirect child of this view will always use this FocusTraversable
936 // rather than the one from the widget.
937 virtual FocusTraversable* GetPaneFocusTraversable() { return NULL; }
938
initial.commit09911bf2008-07-26 23:55:29939#ifndef NDEBUG
940 // Debug method that logs the view hierarchy to the output.
941 void PrintViewHierarchy();
942
943 // Debug method that logs the focus traversal hierarchy to the output.
944 void PrintFocusHierarchy();
945#endif
946
947 // The following methods are used by ScrollView to determine the amount
948 // to scroll relative to the visible bounds of the view. For example, a
949 // return value of 10 indicates the scrollview should scroll 10 pixels in
950 // the appropriate direction.
951 //
952 // Each method takes the following parameters:
953 //
954 // is_horizontal: if true, scrolling is along the horizontal axis, otherwise
955 // the vertical axis.
956 // is_positive: if true, scrolling is by a positive amount. Along the
957 // vertical axis scrolling by a positive amount equates to
958 // scrolling down.
959 //
960 // The return value should always be positive and gives the number of pixels
961 // to scroll. ScrollView interprets a return value of 0 (or negative)
962 // to scroll by a default amount.
963 //
964 // See VariableRowHeightScrollHelper and FixedRowHeightScrollHelper for
965 // implementations of common cases.
966 virtual int GetPageScrollIncrement(ScrollView* scroll_view,
967 bool is_horizontal, bool is_positive);
968 virtual int GetLineScrollIncrement(ScrollView* scroll_view,
969 bool is_horizontal, bool is_positive);
970
[email protected]4a190632009-05-09 01:07:42971 // Get the theme provider from the parent widget.
[email protected]45da6c72009-10-28 20:45:42972 ThemeProvider* GetThemeProvider() const;
[email protected]4a190632009-05-09 01:07:42973
initial.commit09911bf2008-07-26 23:55:29974 protected:
[email protected]59461d22010-07-21 15:41:09975 // Returns whether this view can accept focus.
976 // A view can accept focus if it's enabled, focusable and visible.
977 // This method is intended for views to use when calculating preferred size.
978 // The FocusManager and other places use IsFocusableInRootView.
979 virtual bool IsFocusable() const;
initial.commit09911bf2008-07-26 23:55:29980
[email protected]32670b02009-03-03 00:28:00981 // Called when the UI theme has changed, overriding allows individual Views to
982 // do special cleanup and processing (such as dropping resource caches).
[email protected]8b9a8f12010-07-27 01:39:13983 // To dispatch a theme changed notification, call
984 // RootView::NotifyThemeChanged().
985 virtual void OnThemeChanged() { }
[email protected]32670b02009-03-03 00:28:00986
[email protected]b2b718012010-03-25 15:09:06987 // Called when the locale has changed, overriding allows individual Views to
[email protected]7ceeba72010-04-20 18:22:12988 // update locale-dependent strings.
[email protected]8b9a8f12010-07-27 01:39:13989 // To dispatch a locale changed notification, call
990 // RootView::NotifyLocaleChanged().
991 virtual void OnLocaleChanged() { }
[email protected]b2b718012010-03-25 15:09:06992
initial.commit09911bf2008-07-26 23:55:29993#ifndef NDEBUG
994 // Returns true if the View is currently processing a paint.
995 virtual bool IsProcessingPaint() const;
996#endif
997
[email protected]f704ee72008-11-10 21:31:59998 // Returns the location, in screen coordinates, to show the context menu at
999 // when the context menu is shown from the keyboard. This implementation
1000 // returns the middle of the visible region of this view.
1001 //
1002 // This method is invoked when the context menu is shown by way of the
1003 // keyboard.
1004 virtual gfx::Point GetKeyboardContextMenuLocation();
1005
[email protected]82739cf2008-09-16 00:37:561006 // Called by HitTest to see if this View has a custom hit test mask. If the
1007 // return value is true, GetHitTestMask will be called to obtain the mask.
1008 // Default value is false, in which case the View will hit-test against its
1009 // bounds.
1010 virtual bool HasHitTestMask() const;
1011
1012 // Called by HitTest to retrieve a mask for hit-testing against. Subclasses
1013 // override to provide custom shaped hit test regions.
1014 virtual void GetHitTestMask(gfx::Path* mask) const;
1015
initial.commit09911bf2008-07-26 23:55:291016 // This method is invoked when the tree changes.
1017 //
1018 // When a view is removed, it is invoked for all children and grand
1019 // children. For each of these views, a notification is sent to the
1020 // view and all parents.
1021 //
1022 // When a view is added, a notification is sent to the view, all its
1023 // parents, and all its children (and grand children)
1024 //
1025 // Default implementation does nothing. Override to perform operations
1026 // required when a view is added or removed from a view hierarchy
1027 //
1028 // parent is the new or old parent. Child is the view being added or
1029 // removed.
1030 //
1031 virtual void ViewHierarchyChanged(bool is_add, View* parent, View* child);
1032
1033 // When SetVisible() changes the visibility of a view, this method is
1034 // invoked for that view as well as all the children recursively.
1035 virtual void VisibilityChanged(View* starting_from, bool is_visible);
1036
[email protected]bda9556c2010-01-07 00:55:161037 // Called when the native view hierarchy changed.
1038 // |attached| is true if that view has been attached to a new NativeView
1039 // hierarchy, false if it has been detached.
1040 // |native_view| is the NativeView this view was attached/detached from, and
1041 // |root_view| is the root view associated with the NativeView.
1042 // Views created without a native view parent don't have a focus manager.
1043 // When this function is called they could do the processing that requires
1044 // it - like registering accelerators, for example.
1045 virtual void NativeViewHierarchyChanged(bool attached,
1046 gfx::NativeView native_view,
1047 RootView* root_view);
1048
[email protected]7ccc52b72009-05-08 21:09:111049 // Called when the preferred size of a child view changed. This gives the
1050 // parent an opportunity to do a fresh layout if that makes sense.
1051 virtual void ChildPreferredSizeChanged(View* child) {}
1052
[email protected]9ea053e2010-07-15 08:19:051053 // Invalidates the layout and calls ChildPreferredSizeChanged on the parent
1054 // if there is one. Be sure to call View::PreferredSizeChanged when
1055 // overriding such that the layout is properly invalidated.
[email protected]7ccc52b72009-05-08 21:09:111056 virtual void PreferredSizeChanged();
1057
initial.commit09911bf2008-07-26 23:55:291058 // Views must invoke this when the tooltip text they are to display changes.
1059 void TooltipTextChanged();
1060
initial.commit09911bf2008-07-26 23:55:291061 // Sets whether this view wants notification when its visible bounds relative
1062 // to the root view changes. If true, this view is notified any time the
1063 // origin of one its ancestors changes, or the portion of the bounds not
1064 // obscured by ancestors changes. The default is false.
1065 void SetNotifyWhenVisibleBoundsInRootChanges(bool value);
1066 bool GetNotifyWhenVisibleBoundsInRootChanges();
1067
1068 // Notification that this views visible bounds, relative to the RootView
1069 // has changed. The visible bounds corresponds to the region of the
1070 // view not obscured by other ancestors.
1071 virtual void VisibleBoundsInRootChanged() {}
1072
1073 // Sets the keyboard focus to this View. The correct way to set the focus is
1074 // to call RequestFocus() on the view. This method is called when the focus is
1075 // set and gives an opportunity to subclasses to perform any extra focus steps
1076 // (for example native component set the native focus on their native
1077 // component). The default behavior is to set the native focus on the root
[email protected]a0dde122008-11-21 20:51:201078 // Widget, which is what is appropriate for views that have no native window
1079 // associated with them (so the root view gets the keyboard messages).
initial.commit09911bf2008-07-26 23:55:291080 virtual void Focus();
1081
initial.commit09911bf2008-07-26 23:55:291082 // These are cover methods that invoke the method of the same name on
1083 // the DragController. Subclasses may wish to override rather than install
1084 // a DragController.
1085 // See DragController for a description of these methods.
[email protected]e9adf0702010-03-08 23:34:071086 virtual int GetDragOperations(const gfx::Point& press_pt);
1087 virtual void WriteDragData(const gfx::Point& press_pt, OSExchangeData* data);
initial.commit09911bf2008-07-26 23:55:291088
1089 // Invoked from DoDrag after the drag completes. This implementation does
1090 // nothing, and is intended for subclasses to do cleanup.
1091 virtual void OnDragDone();
1092
1093 // Returns whether we're in the middle of a drag session that was initiated
1094 // by us.
1095 bool InDrag();
1096
[email protected]253a39a2009-05-29 20:45:131097 // Returns how much the mouse needs to move in one direction to start a
1098 // drag. These methods cache in a platform-appropriate way. These values are
1099 // used by the public static method ExceededDragThreshold().
1100 static int GetHorizontalDragThreshold();
1101 static int GetVerticalDragThreshold();
1102
[email protected]59461d22010-07-21 15:41:091103 // The id of this View. Used to find this View.
1104 int id_;
1105
1106 // The group of this view. Some view subclasses use this id to find other
1107 // views of the same group. For example radio button uses this information
1108 // to find other radio buttons.
1109 int group_;
1110
initial.commit09911bf2008-07-26 23:55:291111 // Whether this view is enabled.
1112 bool enabled_;
1113
1114 // Whether the view can be focused.
1115 bool focusable_;
1116
[email protected]83548a42010-06-18 13:53:371117 // Whether this view is focusable if the user requires full keyboard access,
1118 // even though it may not be normally focusable.
1119 bool accessibility_focusable_;
1120
initial.commit09911bf2008-07-26 23:55:291121 private:
1122 friend class RootView;
1123 friend class FocusManager;
1124 friend class ViewStorage;
1125
1126 // Used to track a drag. RootView passes this into
1127 // ProcessMousePressed/Dragged.
1128 struct DragInfo {
1129 // Sets possible_drag to false and start_x/y to 0. This is invoked by
1130 // RootView prior to invoke ProcessMousePressed.
1131 void Reset();
1132
[email protected]e9adf0702010-03-08 23:34:071133 // Sets possible_drag to true and start_pt to the specified point.
initial.commit09911bf2008-07-26 23:55:291134 // This is invoked by the target view if it detects the press may generate
1135 // a drag.
[email protected]e9adf0702010-03-08 23:34:071136 void PossibleDrag(const gfx::Point& p);
initial.commit09911bf2008-07-26 23:55:291137
1138 // Whether the press may generate a drag.
1139 bool possible_drag;
1140
1141 // Coordinates of the mouse press.
[email protected]e9adf0702010-03-08 23:34:071142 gfx::Point start_pt;
initial.commit09911bf2008-07-26 23:55:291143 };
1144
[email protected]8b9a8f12010-07-27 01:39:131145 // Used to propagate theme changed notifications from the root view to all
1146 // views in the hierarchy.
1147 virtual void PropagateThemeChanged();
1148
1149 // Used to propagate locale changed notifications from the root view to all
1150 // views in the hierarchy.
1151 virtual void PropagateLocaleChanged();
[email protected]7ceeba72010-04-20 18:22:121152
initial.commit09911bf2008-07-26 23:55:291153 // RootView invokes these. These in turn invoke the appropriate OnMouseXXX
1154 // method. If a drag is detected, DoDrag is invoked.
1155 bool ProcessMousePressed(const MouseEvent& e, DragInfo* drop_info);
1156 bool ProcessMouseDragged(const MouseEvent& e, DragInfo* drop_info);
1157 void ProcessMouseReleased(const MouseEvent& e, bool canceled);
1158
[email protected]ad84c8f2010-09-08 14:06:101159#if defined(TOUCH_UI)
1160 // RootView will invoke this with incoming TouchEvents. Returns the
1161 // the result of OnTouchEvent: true if the event was handled by the
1162 // callee.
1163 bool ProcessTouchEvent(const TouchEvent& e);
1164#endif
1165
initial.commit09911bf2008-07-26 23:55:291166 // Starts a drag and drop operation originating from this view. This invokes
1167 // WriteDragData to write the data and GetDragOperations to determine the
1168 // supported drag operations. When done, OnDragDone is invoked.
[email protected]e9adf0702010-03-08 23:34:071169 void DoDrag(const MouseEvent& e, const gfx::Point& press_pt);
initial.commit09911bf2008-07-26 23:55:291170
initial.commit09911bf2008-07-26 23:55:291171 // Removes |view| from the hierarchy tree. If |update_focus_cycle| is true,
1172 // the next and previous focusable views of views pointing to this view are
1173 // updated. If |update_tool_tip| is true, the tooltip is updated. If
1174 // |delete_removed_view| is true, the view is also deleted (if it is parent
1175 // owned).
1176 void DoRemoveChildView(View* view,
1177 bool update_focus_cycle,
1178 bool update_tool_tip,
1179 bool delete_removed_view);
1180
1181 // Sets the parent View. This is called automatically by AddChild and is
1182 // thus private.
[email protected]054e3fd2009-09-14 19:48:051183 void SetParent(View* parent);
initial.commit09911bf2008-07-26 23:55:291184
1185 // Call ViewHierarchyChanged for all child views on all parents
1186 void PropagateRemoveNotifications(View* parent);
1187
1188 // Call ViewHierarchyChanged for all children
1189 void PropagateAddNotifications(View* parent, View* child);
1190
1191 // Call VisibilityChanged() recursively for all children.
1192 void PropagateVisibilityNotifications(View* from, bool is_visible);
1193
[email protected]bda9556c2010-01-07 00:55:161194 // Propagates NativeViewHierarchyChanged() notification through all the
1195 // children.
1196 void PropagateNativeViewHierarchyChanged(bool attached,
1197 gfx::NativeView native_view,
1198 RootView* root_view);
1199
initial.commit09911bf2008-07-26 23:55:291200 // Takes care of registering/unregistering accelerators if
1201 // |register_accelerators| true and calls ViewHierarchyChanged().
1202 void ViewHierarchyChangedImpl(bool register_accelerators,
[email protected]bb515ed2009-01-15 00:53:431203 bool is_add,
1204 View* parent,
1205 View* child);
initial.commit09911bf2008-07-26 23:55:291206
1207 // This is the actual implementation for ConvertPointToView()
1208 // Attempts a parent -> child conversion and then a
1209 // child -> parent conversion if try_other_direction is true
[email protected]bb515ed2009-01-15 00:53:431210 static void ConvertPointToView(const View* src,
1211 const View* dst,
initial.commit09911bf2008-07-26 23:55:291212 gfx::Point* point,
1213 bool try_other_direction);
1214
[email protected]a0dde122008-11-21 20:51:201215 // Propagates UpdateTooltip() to the TooltipManager for the Widget.
initial.commit09911bf2008-07-26 23:55:291216 // This must be invoked any time the View hierarchy changes in such a way
1217 // the view under the mouse differs. For example, if the bounds of a View is
1218 // changed, this is invoked. Similarly, as Views are added/removed, this
1219 // is invoked.
1220 void UpdateTooltip();
1221
1222 // Recursively descends through all descendant views,
1223 // registering/unregistering all views that want visible bounds in root
[email protected]9fc4f202010-07-07 15:38:191224 // view notification.
1225 static void RegisterChildrenForVisibleBoundsNotification(RootView* root,
1226 View* view);
1227 static void UnregisterChildrenForVisibleBoundsNotification(RootView* root,
1228 View* view);
initial.commit09911bf2008-07-26 23:55:291229
1230 // Adds/removes view to the list of descendants that are notified any time
1231 // this views location and possibly size are changed.
1232 void AddDescendantToNotify(View* view);
1233 void RemoveDescendantToNotify(View* view);
1234
1235 // Initialize the previous/next focusable views of the specified view relative
1236 // to the view at the specified index.
1237 void InitFocusSiblings(View* view, int index);
1238
1239 // Actual implementation of PrintFocusHierarchy.
1240 void PrintViewHierarchyImp(int indent);
1241 void PrintFocusHierarchyImp(int indent);
1242
[email protected]71421c3f2009-06-06 00:41:441243 // Registers this view's keyboard accelerators that are not registered to
1244 // FocusManager yet, if possible.
1245 void RegisterPendingAccelerators();
1246
1247 // Unregisters all the keyboard accelerators associated with this view.
[email protected]bda9556c2010-01-07 00:55:161248 // |leave_data_intact| if true does not remove data from accelerators_ array,
1249 // so it could be re-registered with other focus manager
1250 void UnregisterAccelerators(bool leave_data_intact);
initial.commit09911bf2008-07-26 23:55:291251
[email protected]80f8b9f2008-10-16 18:17:471252 // This View's bounds in the parent coordinate system.
1253 gfx::Rect bounds_;
1254
[email protected]9ea053e2010-07-15 08:19:051255 // Whether the view needs to be laid out.
1256 bool needs_layout_;
1257
initial.commit09911bf2008-07-26 23:55:291258 // This view's parent
[email protected]054e3fd2009-09-14 19:48:051259 View* parent_;
initial.commit09911bf2008-07-26 23:55:291260
1261 // This view's children.
1262 typedef std::vector<View*> ViewList;
1263 ViewList child_views_;
1264
initial.commit09911bf2008-07-26 23:55:291265 // The View's LayoutManager defines the sizing heuristics applied to child
1266 // Views. The default is absolute positioning according to bounds_.
1267 scoped_ptr<LayoutManager> layout_manager_;
1268
1269 // Visible state
1270 bool is_visible_;
1271
1272 // Background
[email protected]9a3f0ac22008-11-14 03:24:021273 scoped_ptr<Background> background_;
initial.commit09911bf2008-07-26 23:55:291274
1275 // Border.
[email protected]9a3f0ac22008-11-14 03:24:021276 scoped_ptr<Border> border_;
initial.commit09911bf2008-07-26 23:55:291277
1278 // Whether this view is owned by its parent.
1279 bool is_parent_owned_;
1280
1281 // See SetNotifyWhenVisibleBoundsInRootChanges.
1282 bool notify_when_visible_bounds_in_root_changes_;
1283
1284 // Whether or not RegisterViewForVisibleBoundsNotification on the RootView
1285 // has been invoked.
1286 bool registered_for_visible_bounds_notification_;
1287
[email protected]bda9556c2010-01-07 00:55:161288 // true if when we were added to hierarchy we were without focus manager
1289 // attempt addition when ancestor chain changed.
1290 bool accelerator_registration_delayed_;
1291
initial.commit09911bf2008-07-26 23:55:291292 // List of descendants wanting notification when their visible bounds change.
1293 scoped_ptr<ViewList> descendants_to_notify_;
1294
[email protected]d5c81012010-04-03 00:56:121295 // Name for this view, which can be retrieved by accessibility APIs.
1296 std::wstring accessible_name_;
1297
initial.commit09911bf2008-07-26 23:55:291298 // Next view to be focused when the Tab key is pressed.
1299 View* next_focusable_view_;
1300
1301 // Next view to be focused when the Shift-Tab key combination is pressed.
1302 View* previous_focusable_view_;
1303
[email protected]bda9556c2010-01-07 00:55:161304 // Focus manager accelerators registered on.
1305 FocusManager* accelerator_focus_manager_;
1306
[email protected]71421c3f2009-06-06 00:41:441307 // The list of accelerators. List elements in the range
1308 // [0, registered_accelerator_count_) are already registered to FocusManager,
1309 // and the rest are not yet.
[email protected]1eb89e82008-08-15 12:27:031310 scoped_ptr<std::vector<Accelerator> > accelerators_;
[email protected]4bd23f32009-06-08 20:59:191311 size_t registered_accelerator_count_;
initial.commit09911bf2008-07-26 23:55:291312
initial.commit09911bf2008-07-26 23:55:291313 // The menu controller.
1314 ContextMenuController* context_menu_controller_;
1315
[email protected]6ff244f2009-01-20 20:38:081316#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291317 // The accessibility implementation for this View.
[email protected]5fe15722010-10-22 00:03:221318 scoped_refptr<ViewAccessibility> view_accessibility_;
[email protected]6ff244f2009-01-20 20:38:081319#endif
initial.commit09911bf2008-07-26 23:55:291320
1321 DragController* drag_controller_;
1322
[email protected]82522512009-05-15 07:37:291323 // Indicates whether or not the gfx::Canvas object passed to View::Paint()
initial.commit09911bf2008-07-26 23:55:291324 // is going to be flipped horizontally (using the appropriate transform) on
1325 // right-to-left locales for this View.
1326 bool flip_canvas_on_paint_for_rtl_ui_;
1327
[email protected]8af4c1992010-02-04 21:38:071328 // The default value for how long to wait (in ms) before showing a menu
1329 // button on hover. This value is used if the OS doesn't supply one.
1330 static const int kShowFolderDropMenuDelay;
1331
[email protected]1eb89e82008-08-15 12:27:031332 DISALLOW_COPY_AND_ASSIGN(View);
initial.commit09911bf2008-07-26 23:55:291333};
1334
[email protected]c2dacc92008-10-16 23:51:381335} // namespace views
initial.commit09911bf2008-07-26 23:55:291336
[email protected]2362e4f2009-05-08 00:34:051337#endif // VIEWS_VIEW_H_