blob: 83247e6f375b303484528d69e0e207a2727e894d [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_
initial.commit09911bf2008-07-26 23:55:297
[email protected]8c117712009-01-13 12:26:468#include "build/build_config.h"
9
[email protected]1bc83062009-02-06 00:16:3710#include <algorithm>
initial.commit09911bf2008-07-26 23:55:2911#include <map>
[email protected]134c47b92009-08-19 03:33:4412#include <set>
[email protected]1bc83062009-02-06 00:16:3713#include <string>
initial.commit09911bf2008-07-26 23:55:2914#include <vector>
15
[email protected]134c47b92009-08-19 03:33:4416#include "app/os_exchange_data.h"
[email protected]c2f4bdb72010-05-10 23:15:2117#include "base/i18n/rtl.h"
initial.commit09911bf2008-07-26 23:55:2918#include "base/scoped_ptr.h"
[email protected]5c7293a2010-03-17 06:40:5719#include "gfx/native_widget_types.h"
[email protected]e0fc2f12010-03-14 23:30:5920#include "gfx/rect.h"
[email protected]2362e4f2009-05-08 00:34:0521#include "views/accelerator.h"
[email protected]91e81ae2009-05-08 22:14:3822#include "views/accessibility/accessibility_types.h"
[email protected]2362e4f2009-05-08 00:34:0523#include "views/background.h"
24#include "views/border.h"
initial.commit09911bf2008-07-26 23:55:2925
[email protected]1eb89e82008-08-15 12:27:0326namespace gfx {
[email protected]82522512009-05-15 07:37:2927class Canvas;
[email protected]1eb89e82008-08-15 12:27:0328class Insets;
[email protected]82739cf2008-09-16 00:37:5629class Path;
[email protected]1eb89e82008-08-15 12:27:0330}
31
[email protected]fef10642009-03-17 21:17:0432class ViewAccessibilityWrapper;
[email protected]4a190632009-05-09 01:07:4233class ThemeProvider;
initial.commit09911bf2008-07-26 23:55:2934
[email protected]c2dacc92008-10-16 23:51:3835namespace views {
initial.commit09911bf2008-07-26 23:55:2936
37class Background;
38class Border;
39class FocusManager;
40class FocusTraversable;
41class LayoutManager;
42class RestoreFocusTask;
43class RootView;
44class ScrollView;
[email protected]a0dde122008-11-21 20:51:2045class Widget;
[email protected]cd8c47902009-04-30 20:55:3546class Window;
initial.commit09911bf2008-07-26 23:55:2947
48// ContextMenuController is responsible for showing the context menu for a
49// View. To use a ContextMenuController invoke SetContextMenuController on a
50// View. When the appropriate user gesture occurs ShowContextMenu is invoked
51// on the ContextMenuController.
52//
53// Setting a ContextMenuController on a view makes the view process mouse
54// events.
55//
56// It is up to subclasses that do their own mouse processing to invoke
57// the appropriate ContextMenuController method, typically by invoking super's
58// implementation for mouse processing.
59//
60class ContextMenuController {
61 public:
[email protected]e9adf0702010-03-08 23:34:0762 // Invoked to show the context menu for the source view. If |is_mouse_gesture|
63 // is true, |p| is the location of the mouse. If |is_mouse_gesture| is false,
64 // this method was not invoked by a mouse gesture and |p| is the recommended
65 // location to show the menu at.
initial.commit09911bf2008-07-26 23:55:2966 //
[email protected]e9adf0702010-03-08 23:34:0767 // |p| is in screen coordinates.
initial.commit09911bf2008-07-26 23:55:2968 virtual void ShowContextMenu(View* source,
[email protected]e9adf0702010-03-08 23:34:0769 const gfx::Point& p,
initial.commit09911bf2008-07-26 23:55:2970 bool is_mouse_gesture) = 0;
[email protected]20cb5f482009-12-16 01:01:2571
72 protected:
73 virtual ~ContextMenuController() {}
initial.commit09911bf2008-07-26 23:55:2974};
75
76// DragController is responsible for writing drag data for a view, as well as
77// supplying the supported drag operations. Use DragController if you don't
78// want to subclass.
79
80class DragController {
81 public:
82 // Writes the data for the drag.
83 virtual void WriteDragData(View* sender,
[email protected]e9adf0702010-03-08 23:34:0784 const gfx::Point& press_pt,
initial.commit09911bf2008-07-26 23:55:2985 OSExchangeData* data) = 0;
86
87 // Returns the supported drag operations (see DragDropTypes for possible
88 // values). A drag is only started if this returns a non-zero value.
[email protected]e9adf0702010-03-08 23:34:0789 virtual int GetDragOperations(View* sender, const gfx::Point& p) = 0;
initial.commit09911bf2008-07-26 23:55:2990
[email protected]b5f94de2009-12-04 07:59:0091 // Returns true if a drag operation can be started.
[email protected]e9adf0702010-03-08 23:34:0792 // |press_pt| represents the coordinates where the mouse was initially
93 // pressed down. |p| is the current mouse coordinates.
[email protected]b5f94de2009-12-04 07:59:0094 virtual bool CanStartDrag(View* sender,
[email protected]e9adf0702010-03-08 23:34:0795 const gfx::Point& press_pt,
96 const gfx::Point& p) = 0;
[email protected]20cb5f482009-12-16 01:01:2597
98 protected:
99 virtual ~DragController() {}
[email protected]b5f94de2009-12-04 07:59:00100};
initial.commit09911bf2008-07-26 23:55:29101
102/////////////////////////////////////////////////////////////////////////////
103//
104// View class
105//
[email protected]c2dacc92008-10-16 23:51:38106// A View is a rectangle within the views View hierarchy. It is the base
[email protected]1bc83062009-02-06 00:16:37107// class for all Views.
initial.commit09911bf2008-07-26 23:55:29108//
109// A View is a container of other Views (there is no such thing as a Leaf
110// View - makes code simpler, reduces type conversion headaches, design
111// mistakes etc)
112//
113// The View contains basic properties for sizing (bounds), layout (flex,
114// orientation, etc), painting of children and event dispatch.
115//
116// The View also uses a simple Box Layout Manager similar to XUL's
117// SprocketLayout system. Alternative Layout Managers implementing the
118// LayoutManager interface can be used to lay out children if required.
119//
120// It is up to the subclass to implement Painting and storage of subclass -
121// specific properties and functionality.
122//
123/////////////////////////////////////////////////////////////////////////////
124class View : public AcceleratorTarget {
125 public:
[email protected]6f3bb6c2008-09-17 22:25:33126 // Used in the versions of GetBounds() and x() that take a transformation
initial.commit09911bf2008-07-26 23:55:29127 // parameter in order to determine whether or not to take into account the
128 // mirroring setting of the View when returning bounds positions.
129 enum PositionMirroringSettings {
130 IGNORE_MIRRORING_TRANSFORMATION = 0,
131 APPLY_MIRRORING_TRANSFORMATION
132 };
133
134 // The view class name.
135 static char kViewClassName[];
136
137 View();
138 virtual ~View();
139
[email protected]aadcd1d92009-09-22 18:11:31140 // Returns the amount of time between double clicks, in milliseconds.
141 static int GetDoubleClickTimeMS();
142
[email protected]8af4c1992010-02-04 21:38:07143 // Returns the amount of time to wait from hovering over a menu button until
144 // showing the menu.
145 static int GetMenuShowDelay();
146
initial.commit09911bf2008-07-26 23:55:29147 // Sizing functions
148
149 // Get the bounds of the View, relative to the parent. Essentially, this
150 // function returns the bounds_ rectangle.
151 //
152 // This is the function subclasses should use whenever they need to obtain
153 // the bounds of one of their child views (for example, when implementing
154 // View::Layout()).
[email protected]24db2eb2009-07-17 17:54:16155 const gfx::Rect& bounds() const { return bounds_; }
[email protected]80f8b9f2008-10-16 18:17:47156
157 // Get the size of the View.
[email protected]24db2eb2009-07-17 17:54:16158 const gfx::Size& size() const { return bounds_.size(); }
initial.commit09911bf2008-07-26 23:55:29159
160 // Return the bounds of the View, relative to the parent. If
161 // |settings| is IGNORE_MIRRORING_TRANSFORMATION, the function returns the
[email protected]8f763a302009-11-11 00:47:32162 // bounds_ rectangle. If |settings| is APPLY_MIRRORING_TRANSFORMATION AND the
initial.commit09911bf2008-07-26 23:55:29163 // parent View is using a right-to-left UI layout, then the function returns
164 // a shifted version of the bounds_ rectangle that represents the mirrored
165 // View bounds.
166 //
167 // NOTE: in the vast majority of the cases, the mirroring implementation is
168 // transparent to the View subclasses and therefore you should use the
169 // version of GetBounds() which does not take a transformation settings
170 // parameter.
[email protected]0d8ea702008-10-14 17:03:07171 gfx::Rect GetBounds(PositionMirroringSettings settings) const;
initial.commit09911bf2008-07-26 23:55:29172
173 // Set the bounds in the parent's coordinate system.
[email protected]80f8b9f2008-10-16 18:17:47174 void SetBounds(const gfx::Rect& bounds);
175 void SetBounds(int x, int y, int width, int height) {
176 SetBounds(gfx::Rect(x, y, std::max(0, width), std::max(0, height)));
177 }
[email protected]6f3bb6c2008-09-17 22:25:33178 void SetX(int x) { SetBounds(x, y(), width(), height()); }
179 void SetY(int y) { SetBounds(x(), y, width(), height()); }
initial.commit09911bf2008-07-26 23:55:29180
181 // Returns the left coordinate of the View, relative to the parent View,
[email protected]80f8b9f2008-10-16 18:17:47182 // which is the value of bounds_.x().
initial.commit09911bf2008-07-26 23:55:29183 //
184 // This is the function subclasses should use whenever they need to obtain
185 // the left position of one of their child views (for example, when
186 // implementing View::Layout()).
[email protected]0a1d36b22008-10-17 19:33:09187 // This is equivalent to GetX(IGNORE_MIRRORING_TRANSFORMATION), but
188 // inlinable.
189 int x() const { return bounds_.x(); }
190 int y() const { return bounds_.y(); }
191 int width() const { return bounds_.width(); }
192 int height() const { return bounds_.height(); }
initial.commit09911bf2008-07-26 23:55:29193
194 // Return the left coordinate of the View, relative to the parent. If
195 // |settings| is IGNORE_MIRRORING_SETTINGS, the function returns the value of
[email protected]80f8b9f2008-10-16 18:17:47196 // bounds_.x(). If |settings| is APPLY_MIRRORING_SETTINGS AND the parent
initial.commit09911bf2008-07-26 23:55:29197 // View is using a right-to-left UI layout, then the function returns the
[email protected]80f8b9f2008-10-16 18:17:47198 // mirrored value of bounds_.x().
initial.commit09911bf2008-07-26 23:55:29199 //
200 // NOTE: in the vast majority of the cases, the mirroring implementation is
201 // transparent to the View subclasses and therefore you should use the
[email protected]6f3bb6c2008-09-17 22:25:33202 // paremeterless version of x() when you need to get the X
initial.commit09911bf2008-07-26 23:55:29203 // coordinate of a child View.
204 int GetX(PositionMirroringSettings settings) const;
205
initial.commit09911bf2008-07-26 23:55:29206 // Return this control local bounds. If include_border is true, local bounds
[email protected]6f3bb6c2008-09-17 22:25:33207 // is the rectangle {0, 0, width(), height()}, otherwise, it does not
initial.commit09911bf2008-07-26 23:55:29208 // include the area where the border (if any) is painted.
[email protected]80f8b9f2008-10-16 18:17:47209 gfx::Rect GetLocalBounds(bool include_border) const;
initial.commit09911bf2008-07-26 23:55:29210
211 // Get the position of the View, relative to the parent.
212 //
213 // Note that if the parent uses right-to-left UI layout, then the mirrored
[email protected]6f3bb6c2008-09-17 22:25:33214 // position of this View is returned. Use x()/y() if you want to ignore
initial.commit09911bf2008-07-26 23:55:29215 // mirroring.
[email protected]0a1d36b22008-10-17 19:33:09216 gfx::Point GetPosition() const;
initial.commit09911bf2008-07-26 23:55:29217
218 // Get the size the View would like to be, if enough space were available.
[email protected]154f8bc2008-10-15 18:02:30219 virtual gfx::Size GetPreferredSize();
initial.commit09911bf2008-07-26 23:55:29220
[email protected]a1360162009-11-30 21:19:07221 // Returns the baseline of this view, or -1 if this view has no baseline. The
222 // return value is relative to the preferred height.
223 virtual int GetBaseline();
224
initial.commit09911bf2008-07-26 23:55:29225 // Convenience method that sizes this view to its preferred size.
226 void SizeToPreferredSize();
227
228 // Gets the minimum size of the view. View's implementation invokes
229 // GetPreferredSize.
[email protected]154f8bc2008-10-15 18:02:30230 virtual gfx::Size GetMinimumSize();
initial.commit09911bf2008-07-26 23:55:29231
232 // Return the height necessary to display this view with the provided width.
233 // View's implementation returns the value from getPreferredSize.cy.
234 // Override if your View's preferred height depends upon the width (such
235 // as with Labels).
236 virtual int GetHeightForWidth(int w);
237
238 // This method is invoked when this object size or position changes.
239 // The default implementation does nothing.
[email protected]80f8b9f2008-10-16 18:17:47240 virtual void DidChangeBounds(const gfx::Rect& previous,
241 const gfx::Rect& current);
initial.commit09911bf2008-07-26 23:55:29242
243 // Set whether the receiving view is visible. Painting is scheduled as needed
244 virtual void SetVisible(bool flag);
245
246 // Return whether a view is visible
247 virtual bool IsVisible() const { return is_visible_; }
248
249 // Return whether a view and its ancestors are visible. Returns true if the
250 // path from this view to the root view is visible.
251 virtual bool IsVisibleInRootView() const;
252
253 // Set whether this view is enabled. A disabled view does not receive keyboard
254 // or mouse inputs. If flag differs from the current value, SchedulePaint is
255 // invoked.
256 virtual void SetEnabled(bool flag);
257
258 // Returns whether the view is enabled.
259 virtual bool IsEnabled() const;
260
261 // Set whether this view is hottracked. A disabled view cannot be hottracked.
262 // If flag differs from the current value, SchedulePaint is invoked.
263 virtual void SetHotTracked(bool flag);
264
265 // Returns whether the view is hot-tracked.
266 virtual bool IsHotTracked() const { return false; }
267
268 // Returns whether the view is pushed.
269 virtual bool IsPushed() const { return false; }
270
271 // Scrolls the specified region, in this View's coordinate system, to be
272 // visible. View's implementation passes the call onto the parent View (after
273 // adjusting the coordinates). It is up to views that only show a portion of
274 // the child view, such as Viewport, to override appropriately.
[email protected]e9adf0702010-03-08 23:34:07275 virtual void ScrollRectToVisible(const gfx::Rect& rect);
initial.commit09911bf2008-07-26 23:55:29276
277 // Layout functions
278
279 // Lay out the child Views (set their bounds based on sizing heuristics
280 // specific to the current Layout Manager)
281 virtual void Layout();
282
283 // Gets/Sets the Layout Manager used by this view to size and place its
284 // children.
285 // The LayoutManager is owned by the View and is deleted when the view is
286 // deleted, or when a new LayoutManager is installed.
287 LayoutManager* GetLayoutManager() const;
288 void SetLayoutManager(LayoutManager* layout);
289
290 // Right-to-left UI layout functions
291
[email protected]82522512009-05-15 07:37:29292 // This method determines whether the gfx::Canvas object passed to
initial.commit09911bf2008-07-26 23:55:29293 // View::Paint() needs to be transformed such that anything drawn on the
294 // canvas object during View::Paint() is flipped horizontally.
295 //
296 // By default, this function returns false (which is the initial value of
297 // |flip_canvas_on_paint_for_rtl_ui_|). View subclasses that need to paint on
[email protected]82522512009-05-15 07:37:29298 // a flipped gfx::Canvas when the UI layout is right-to-left need to call
initial.commit09911bf2008-07-26 23:55:29299 // EnableCanvasFlippingForRTLUI().
300 bool FlipCanvasOnPaintForRTLUI() const {
[email protected]c2f4bdb72010-05-10 23:15:21301 return flip_canvas_on_paint_for_rtl_ui_ ? base::i18n::IsRTL() : false;
initial.commit09911bf2008-07-26 23:55:29302 }
303
[email protected]82522512009-05-15 07:37:29304 // Enables or disables flipping of the gfx::Canvas during View::Paint().
initial.commit09911bf2008-07-26 23:55:29305 // Note that if canvas flipping is enabled, the canvas will be flipped only
306 // if the UI layout is right-to-left; that is, the canvas will be flipped
[email protected]c2f4bdb72010-05-10 23:15:21307 // only if base::i18n::IsRTL() returns true.
initial.commit09911bf2008-07-26 23:55:29308 //
309 // Enabling canvas flipping is useful for leaf views that draw a bitmap that
310 // needs to be flipped horizontally when the UI layout is right-to-left
[email protected]c2dacc92008-10-16 23:51:38311 // (views::Button, for example). This method is helpful for such classes
312 // because their drawing logic stays the same and they can become agnostic to
313 // the UI directionality.
initial.commit09911bf2008-07-26 23:55:29314 void EnableCanvasFlippingForRTLUI(bool enable) {
315 flip_canvas_on_paint_for_rtl_ui_ = enable;
316 }
317
318 // Returns the mirrored X position for the view, relative to the parent. If
319 // the parent view is not mirrored, this function returns bound_.left.
320 //
321 // UI mirroring is transparent to most View subclasses and therefore there is
322 // no need to call this routine from anywhere within your subclass
323 // implementation.
[email protected]63329982008-10-10 21:56:57324 int MirroredX() const;
initial.commit09911bf2008-07-26 23:55:29325
326 // Given a rectangle specified in this View's coordinate system, the function
327 // computes the 'left' value for the mirrored rectangle within this View. If
328 // the View's UI layout is not right-to-left, then bounds.x() is returned.
329 //
330 // UI mirroring is transparent to most View subclasses and therefore there is
331 // no need to call this routine from anywhere within your subclass
332 // implementation.
333 int MirroredLeftPointForRect(const gfx::Rect& rect) const;
334
335 // Given the X coordinate of a point inside the View, this function returns
336 // the mirrored X coordinate of the point if the View's UI layout is
337 // right-to-left. If the layout is left-to-right, the same X coordinate is
338 // returned.
339 //
340 // Following are a few examples of the values returned by this function for
341 // a View with the bounds {0, 0, 100, 100} and a right-to-left layout:
342 //
343 // MirroredXCoordinateInsideView(0) -> 100
344 // MirroredXCoordinateInsideView(20) -> 80
345 // MirroredXCoordinateInsideView(99) -> 1
346 int MirroredXCoordinateInsideView(int x) const {
[email protected]c2f4bdb72010-05-10 23:15:21347 return base::i18n::IsRTL() ? width() - x : x;
initial.commit09911bf2008-07-26 23:55:29348 }
349
[email protected]14da3dff2009-06-12 18:01:47350 // Given a X coordinate and a width inside the View, this function returns
351 // the mirrored X coordinate if the View's UI layout is right-to-left. If the
352 // layout is left-to-right, the same X coordinate is returned.
353 //
354 // Following are a few examples of the values returned by this function for
355 // a View with the bounds {0, 0, 100, 100} and a right-to-left layout:
356 //
357 // MirroredXCoordinateInsideView(0, 10) -> 90
358 // MirroredXCoordinateInsideView(20, 20) -> 60
359 int MirroredXWithWidthInsideView(int x, int w) const {
[email protected]c2f4bdb72010-05-10 23:15:21360 return base::i18n::IsRTL() ? width() - x - w : x;
[email protected]14da3dff2009-06-12 18:01:47361 }
362
initial.commit09911bf2008-07-26 23:55:29363 // Painting functions
364
365 // Mark the specified rectangle as dirty (needing repaint). If |urgent| is
366 // true, the view will be repainted when the current event processing is
367 // done. Otherwise, painting will take place as soon as possible.
[email protected]0a1d36b22008-10-17 19:33:09368 virtual void SchedulePaint(const gfx::Rect& r, bool urgent);
initial.commit09911bf2008-07-26 23:55:29369
370 // Mark the entire View's bounds as dirty. Painting will occur as soon as
371 // possible.
372 virtual void SchedulePaint();
373
initial.commit09911bf2008-07-26 23:55:29374 // Paint the receiving view. g is prepared such as it is in
375 // receiver's coordinate system. g's state is restored after this
376 // call so your implementation can change the graphics configuration
377 //
378 // Default implementation paints the background if it is defined
379 //
380 // Override this method when implementing a new control.
[email protected]82522512009-05-15 07:37:29381 virtual void Paint(gfx::Canvas* canvas);
initial.commit09911bf2008-07-26 23:55:29382
383 // Paint the background if any. This method is called by Paint() and
384 // should rarely be invoked directly.
[email protected]82522512009-05-15 07:37:29385 virtual void PaintBackground(gfx::Canvas* canvas);
initial.commit09911bf2008-07-26 23:55:29386
387 // Paint the border if any. This method is called by Paint() and
388 // should rarely be invoked directly.
[email protected]82522512009-05-15 07:37:29389 virtual void PaintBorder(gfx::Canvas* canvas);
initial.commit09911bf2008-07-26 23:55:29390
391 // Paints the focus border (only if the view has the focus).
392 // This method is called by Paint() and should rarely be invoked directly.
393 // The default implementation paints a gray border around the view. Override
394 // it for custom focus effects.
[email protected]82522512009-05-15 07:37:29395 virtual void PaintFocusBorder(gfx::Canvas* canvas);
initial.commit09911bf2008-07-26 23:55:29396
397 // Paint this View immediately.
398 virtual void PaintNow();
399
initial.commit09911bf2008-07-26 23:55:29400 // Tree functions
401
402 // Add a child View.
403 void AddChildView(View* v);
404
405 // Adds a child View at the specified position.
406 void AddChildView(int index, View* v);
407
408 // Get the child View at the specified index.
409 View* GetChildViewAt(int index) const;
410
411 // Remove a child view from this view. v's parent will change to NULL
412 void RemoveChildView(View *v);
413
414 // Remove all child view from this view. If |delete_views| is true, the views
415 // are deleted, unless marked as not parent owned.
416 void RemoveAllChildViews(bool delete_views);
417
418 // Get the number of child Views.
419 int GetChildViewCount() const;
420
[email protected]3a9c26f2010-03-12 21:04:39421 // Tests if this view has a given view as direct child.
422 bool HasChildView(View* a_view);
423
[email protected]24db2eb2009-07-17 17:54:16424 // Returns the deepest descendant that contains the specified point.
[email protected]613b8062008-10-14 23:45:09425 virtual View* GetViewForPoint(const gfx::Point& point);
initial.commit09911bf2008-07-26 23:55:29426
[email protected]a0dde122008-11-21 20:51:20427 // Get the Widget that hosts this View, if any.
428 virtual Widget* GetWidget() const;
initial.commit09911bf2008-07-26 23:55:29429
[email protected]cd8c47902009-04-30 20:55:35430 // Gets the Widget that most closely contains this View, if any.
[email protected]08a3b712009-10-14 18:03:47431 // NOTE: almost all views displayed on screen have a Widget, but not
432 // necessarily a Window. This is due to widgets being able to create top
433 // level windows (as is done for popups, bubbles and menus).
[email protected]cd8c47902009-04-30 20:55:35434 virtual Window* GetWindow() const;
435
[email protected]2db27be2010-02-10 22:46:47436 // Returns true if the native view |native_view| is contained in the view
437 // hierarchy beneath this view.
438 virtual bool ContainsNativeView(gfx::NativeView native_view) const;
439
initial.commit09911bf2008-07-26 23:55:29440 // Get the containing RootView
441 virtual RootView* GetRootView();
442
443 // Get the parent View
444 View* GetParent() const { return parent_; }
445
446 // Returns the index of the specified |view| in this view's children, or -1
447 // if the specified view is not a child of this view.
[email protected]5e26d9d42010-05-13 22:11:03448 int GetChildIndex(const View* v) const;
initial.commit09911bf2008-07-26 23:55:29449
450 // Returns true if the specified view is a direct or indirect child of this
451 // view.
452 bool IsParentOf(View* v) const;
453
454 // Recursively descends the view tree starting at this view, and returns
455 // the first child that it encounters that has the given ID.
456 // Returns NULL if no matching child view is found.
457 virtual View* GetViewByID(int id) const;
458
459 // Sets and gets the ID for this view. ID should be unique within the subtree
460 // that you intend to search for it. 0 is the default ID for views.
461 void SetID(int id);
462 int GetID() const;
463
464 // A group id is used to tag views which are part of the same logical group.
465 // Focus can be moved between views with the same group using the arrow keys.
466 // Groups are currently used to implement radio button mutual exclusion.
[email protected]96f960d2009-09-14 18:45:30467 // The group id is immutable once it's set.
initial.commit09911bf2008-07-26 23:55:29468 void SetGroup(int gid);
[email protected]96f960d2009-09-14 18:45:30469 // Returns the group id of the view, or -1 if the id is not set yet.
initial.commit09911bf2008-07-26 23:55:29470 int GetGroup() const;
471
472 // If this returns true, the views from the same group can each be focused
473 // when moving focus with the Tab/Shift-Tab key. If this returns false,
474 // only the selected view from the group (obtained with
475 // GetSelectedViewForGroup()) is focused.
476 virtual bool IsGroupFocusTraversable() const { return true; }
477
478 // Fills the provided vector with all the available views which belong to the
479 // provided group.
480 void GetViewsWithGroup(int group_id, std::vector<View*>* out);
481
482 // Return the View that is currently selected in the specified group.
483 // The default implementation simply returns the first View found for that
484 // group.
485 virtual View* GetSelectedViewForGroup(int group_id);
486
487 // Focus support
488 //
489 // Returns the view that should be selected next when pressing Tab.
490 View* GetNextFocusableView();
491
492 // Returns the view that should be selected next when pressing Shift-Tab.
493 View* GetPreviousFocusableView();
494
495 // Sets the component that should be selected next when pressing Tab, and
496 // makes the current view the precedent view of the specified one.
497 // Note that by default views are linked in the order they have been added to
498 // their container. Use this method if you want to modify the order.
499 // IMPORTANT NOTE: loops in the focus hierarchy are not supported.
500 void SetNextFocusableView(View* view);
501
502 // Return whether this view can accept the focus.
503 virtual bool IsFocusable() const;
504
505 // Sets whether this view can accept the focus.
506 // Note that this is false by default so that a view used as a container does
507 // not get the focus.
508 virtual void SetFocusable(bool focusable);
509
510 // Convenience method to retrieve the FocusManager associated with the
[email protected]a0dde122008-11-21 20:51:20511 // Widget that contains this view. This can return NULL if this view is not
512 // part of a view hierarchy with a Widget.
initial.commit09911bf2008-07-26 23:55:29513 virtual FocusManager* GetFocusManager();
514
515 // Sets a keyboard accelerator for that view. When the user presses the
516 // accelerator key combination, the AcceleratorPressed method is invoked.
517 // Note that you can set multiple accelerators for a view by invoking this
518 // method several times.
519 virtual void AddAccelerator(const Accelerator& accelerator);
520
[email protected]e8e0f362008-11-08 01:13:25521 // Removes the specified accelerator for this view.
522 virtual void RemoveAccelerator(const Accelerator& accelerator);
523
initial.commit09911bf2008-07-26 23:55:29524 // Removes all the keyboard accelerators for this view.
525 virtual void ResetAccelerators();
526
527 // Called when a keyboard accelerator is pressed.
528 // Derived classes should implement desired behavior and return true if they
529 // handled the accelerator.
530 virtual bool AcceleratorPressed(const Accelerator& accelerator) {
531 return false;
532 }
533
initial.commit09911bf2008-07-26 23:55:29534 // Returns whether this view currently has the focus.
535 virtual bool HasFocus();
536
537 // Accessibility support
538 // TODO(klink): Move all this out to a AccessibleInfo wrapper class.
539 //
540 // Returns the MSAA default action of the current view. The string returned
541 // describes the default action that will occur when executing
542 // IAccessible::DoDefaultAction. For instance, default action of a button is
543 // 'Press'. Sets the input string appropriately, and returns true if
544 // successful.
545 virtual bool GetAccessibleDefaultAction(std::wstring* action) {
546 return false;
547 }
548
549 // Returns a string containing the mnemonic, or the keyboard shortcut, for a
550 // given control. Sets the input string appropriately, and returns true if
551 // successful.
552 virtual bool GetAccessibleKeyboardShortcut(std::wstring* shortcut) {
553 return false;
554 }
555
556 // Returns a brief, identifying string, containing a unique, readable name of
557 // a given control. Sets the input string appropriately, and returns true if
558 // successful.
[email protected]d5c81012010-04-03 00:56:12559 bool GetAccessibleName(std::wstring* name);
initial.commit09911bf2008-07-26 23:55:29560
[email protected]e92070ac2009-04-28 00:12:01561 // Returns the accessibility role of the current view. The role is what
562 // assistive technologies (ATs) use to determine what behavior to expect from
563 // a given control. Sets the input Role appropriately, and returns true if
initial.commit09911bf2008-07-26 23:55:29564 // successful.
[email protected]e92070ac2009-04-28 00:12:01565 virtual bool GetAccessibleRole(AccessibilityTypes::Role* role) {
566 return false;
567 }
initial.commit09911bf2008-07-26 23:55:29568
[email protected]e92070ac2009-04-28 00:12:01569 // Returns the accessibility state of the current view. Sets the input State
570 // appropriately, and returns true if successful.
571 virtual bool GetAccessibleState(AccessibilityTypes::State* state) {
572 return false;
573 }
initial.commit09911bf2008-07-26 23:55:29574
[email protected]a9d59462010-03-26 21:51:04575 // Returns the current value associated with a view. Sets the input string
576 // appropriately, and returns true if successful.
577 virtual bool GetAccessibleValue(std::wstring* value) { return false; }
578
initial.commit09911bf2008-07-26 23:55:29579 // Assigns a string name to the given control. Needed as a View does not know
580 // which name will be associated with it until it is created to be a
581 // certain type.
[email protected]d5c81012010-04-03 00:56:12582 void SetAccessibleName(const std::wstring& name);
[email protected]a9d59462010-03-26 21:51:04583
initial.commit09911bf2008-07-26 23:55:29584 // Returns an instance of a wrapper class implementing the (platform-specific)
585 // accessibility interface for a given View. If one exists, it will be
586 // re-used, otherwise a new instance will be created.
[email protected]fef10642009-03-17 21:17:04587 ViewAccessibilityWrapper* GetViewAccessibilityWrapper();
initial.commit09911bf2008-07-26 23:55:29588
589 // Accessor used to determine if a child view (leaf) has accessibility focus.
590 // Returns NULL if there are no children, or if none of the children has
591 // accessibility focus.
[email protected]c2dacc92008-10-16 23:51:38592 virtual View* GetAccFocusedChildView() { return NULL; }
initial.commit09911bf2008-07-26 23:55:29593
[email protected]cc824372010-03-31 15:33:01594 // Try to give accessibility focus to a given child view. Returns true on
595 // success. Returns false if this view isn't already focused, if it doesn't
596 // support accessibility focus for children, or if the given view isn't a
597 // valid child view that can receive accessibility focus.
598 virtual bool SetAccFocusedChildView(View* child_view) { return false; }
599
initial.commit09911bf2008-07-26 23:55:29600 // Utility functions
601
602 // Note that the utility coordinate conversions functions always operate on
603 // the mirrored position of the child Views if the parent View uses a
604 // right-to-left UI layout.
605
606 // Convert a point from source coordinate system to dst coordinate system.
607 //
608 // source is a parent or a child of dst, directly or transitively.
609 // If source and dst are not in the same View hierarchy, the result is
610 // undefined.
611 // Source can be NULL in which case it means the screen coordinate system
[email protected]bb515ed2009-01-15 00:53:43612 static void ConvertPointToView(const View* src,
613 const View* dst,
initial.commit09911bf2008-07-26 23:55:29614 gfx::Point* point);
initial.commit09911bf2008-07-26 23:55:29615
616 // Convert a point from the coordinate system of a View to that of the
[email protected]a0dde122008-11-21 20:51:20617 // Widget. This is useful for example when sizing HWND children of the
618 // Widget that don't know about the View hierarchy and need to be placed
619 // relative to the Widget that is their parent.
[email protected]2fb6d462009-02-13 18:40:10620 static void ConvertPointToWidget(const View* src, gfx::Point* point);
initial.commit09911bf2008-07-26 23:55:29621
[email protected]a0dde122008-11-21 20:51:20622 // Convert a point from a view Widget to a View dest
[email protected]2fb6d462009-02-13 18:40:10623 static void ConvertPointFromWidget(const View* dest, gfx::Point* p);
initial.commit09911bf2008-07-26 23:55:29624
625 // Convert a point from the coordinate system of a View to that of the
626 // screen. This is useful for example when placing popup windows.
[email protected]2fb6d462009-02-13 18:40:10627 static void ConvertPointToScreen(const View* src, gfx::Point* point);
initial.commit09911bf2008-07-26 23:55:29628
629 // Event Handlers
630
631 // This method is invoked when the user clicks on this view.
632 // The provided event is in the receiver's coordinate system.
633 //
634 // Return true if you processed the event and want to receive subsequent
635 // MouseDraggged and MouseReleased events. This also stops the event from
636 // bubbling. If you return false, the event will bubble through parent
637 // views.
638 //
639 // If you remove yourself from the tree while processing this, event bubbling
640 // stops as if you returned true, but you will not receive future events.
641 // The return value is ignored in this case.
642 //
643 // Default implementation returns true if a ContextMenuController has been
644 // set, false otherwise. Override as needed.
645 //
646 virtual bool OnMousePressed(const MouseEvent& event);
647
648 // This method is invoked when the user clicked on this control.
649 // and is still moving the mouse with a button pressed.
650 // The provided event is in the receiver's coordinate system.
651 //
652 // Return true if you processed the event and want to receive
653 // subsequent MouseDragged and MouseReleased events.
654 //
655 // Default implementation returns true if a ContextMenuController has been
656 // set, false otherwise. Override as needed.
657 //
658 virtual bool OnMouseDragged(const MouseEvent& event);
659
660 // This method is invoked when the user releases the mouse
661 // button. The event is in the receiver's coordinate system.
662 //
663 // If canceled is true it indicates the mouse press/drag was canceled by a
664 // system/user gesture.
665 //
666 // Default implementation notifies the ContextMenuController is appropriate.
667 // Subclasses that wish to honor the ContextMenuController should invoke
668 // super.
669 virtual void OnMouseReleased(const MouseEvent& event, bool canceled);
670
671 // This method is invoked when the mouse is above this control
672 // The event is in the receiver's coordinate system.
673 //
674 // Default implementation does nothing. Override as needed.
675 virtual void OnMouseMoved(const MouseEvent& e);
676
677 // This method is invoked when the mouse enters this control.
678 //
679 // Default implementation does nothing. Override as needed.
680 virtual void OnMouseEntered(const MouseEvent& event);
681
682 // This method is invoked when the mouse exits this control
683 // The provided event location is always (0, 0)
684 // Default implementation does nothing. Override as needed.
685 virtual void OnMouseExited(const MouseEvent& event);
686
687 // Set the MouseHandler for a drag session.
688 //
689 // A drag session is a stream of mouse events starting
690 // with a MousePressed event, followed by several MouseDragged
691 // events and finishing with a MouseReleased event.
692 //
693 // This method should be only invoked while processing a
694 // MouseDragged or MouseReleased event.
695 //
696 // All further mouse dragged and mouse up events will be sent
697 // the MouseHandler, even if it is reparented to another window.
698 //
699 // The MouseHandler is automatically cleared when the control
700 // comes back from processing the MouseReleased event.
701 //
702 // Note: if the mouse handler is no longer connected to a
703 // view hierarchy, events won't be sent.
704 //
705 virtual void SetMouseHandler(View* new_mouse_handler);
706
707 // Request the keyboard focus. The receiving view will become the
708 // focused view.
709 virtual void RequestFocus();
710
711 // Invoked when a view is about to gain focus
712 virtual void WillGainFocus();
713
714 // Invoked when a view just gained focus.
715 virtual void DidGainFocus();
716
717 // Invoked when a view is about lose focus
718 virtual void WillLoseFocus();
719
720 // Invoked when a view is about to be requested for focus due to the focus
721 // traversal. Reverse is this request was generated going backward
722 // (Shift-Tab).
723 virtual void AboutToRequestFocusFromTabTraversal(bool reverse) { }
724
[email protected]ca13d804c2009-05-14 04:28:07725 // Invoked when a key is pressed before the key event is processed (and
726 // potentially eaten) by the focus manager for tab traversal, accelerators and
727 // other focus related actions.
728 // The default implementation returns false, ensuring that tab traversal and
729 // accelerators processing is performed.
730 // Subclasses should return true if they want to process the key event and not
731 // have it processed as an accelerator (if any) or as a tab traversal (if the
732 // key event is for the TAB key). In that case, OnKeyPressed will
733 // subsequently be invoked for that event.
734 virtual bool SkipDefaultKeyEventProcessing(const KeyEvent& e) {
735 return false;
736 }
737
initial.commit09911bf2008-07-26 23:55:29738 // Invoked when a key is pressed or released.
739 // Subclasser should return true if the event has been processed and false
740 // otherwise. If the event has not been processed, the parent will be given a
741 // chance.
742 virtual bool OnKeyPressed(const KeyEvent& e);
743 virtual bool OnKeyReleased(const KeyEvent& e);
744
initial.commit09911bf2008-07-26 23:55:29745 // Invoked when the user uses the mousewheel. Implementors should return true
746 // if the event has been processed and false otherwise. This message is sent
747 // if the view is focused. If the event has not been processed, the parent
748 // will be given a chance.
749 virtual bool OnMouseWheel(const MouseWheelEvent& e);
750
751 // Drag and drop functions.
752
753 // Set/get the DragController. See description of DragController for more
754 // information.
755 void SetDragController(DragController* drag_controller);
756 DragController* GetDragController();
757
758 // During a drag and drop session when the mouse moves the view under the
[email protected]134c47b92009-08-19 03:33:44759 // mouse is queried for the drop types it supports by way of the
760 // GetDropFormats methods. If the view returns true and the drag site can
761 // provide data in one of the formats, the view is asked if the drop data
762 // is required before any other drop events are sent. Once the
763 // data is available the view is asked if it supports the drop (by way of
764 // the CanDrop method). If a view returns true from CanDrop,
initial.commit09911bf2008-07-26 23:55:29765 // OnDragEntered is sent to the view when the mouse first enters the view,
766 // as the mouse moves around within the view OnDragUpdated is invoked.
767 // If the user releases the mouse over the view and OnDragUpdated returns a
768 // valid drop, then OnPerformDrop is invoked. If the mouse moves outside the
769 // view or over another view that wants the drag, OnDragExited is invoked.
770 //
771 // Similar to mouse events, the deepest view under the mouse is first checked
772 // if it supports the drop (Drop). If the deepest view under
773 // the mouse does not support the drop, the ancestors are walked until one
774 // is found that supports the drop.
775
[email protected]134c47b92009-08-19 03:33:44776 // Override and return the set of formats that can be dropped on this view.
777 // |formats| is a bitmask of the formats defined bye OSExchangeData::Format.
778 // The default implementation returns false, which means the view doesn't
779 // support dropping.
780 virtual bool GetDropFormats(
781 int* formats,
782 std::set<OSExchangeData::CustomFormat>* custom_formats);
783
784 // Override and return true if the data must be available before any drop
785 // methods should be invoked. The default is false.
786 virtual bool AreDropTypesRequired();
787
initial.commit09911bf2008-07-26 23:55:29788 // A view that supports drag and drop must override this and return true if
789 // data contains a type that may be dropped on this view.
790 virtual bool CanDrop(const OSExchangeData& data);
791
792 // OnDragEntered is invoked when the mouse enters this view during a drag and
793 // drop session and CanDrop returns true. This is immediately
794 // followed by an invocation of OnDragUpdated, and eventually one of
795 // OnDragExited or OnPerformDrop.
796 virtual void OnDragEntered(const DropTargetEvent& event);
797
798 // Invoked during a drag and drop session while the mouse is over the view.
799 // This should return a bitmask of the DragDropTypes::DragOperation supported
800 // based on the location of the event. Return 0 to indicate the drop should
801 // not be accepted.
802 virtual int OnDragUpdated(const DropTargetEvent& event);
803
804 // Invoked during a drag and drop session when the mouse exits the views, or
805 // when the drag session was canceled and the mouse was over the view.
806 virtual void OnDragExited();
807
808 // Invoked during a drag and drop session when OnDragUpdated returns a valid
809 // operation and the user release the mouse.
810 virtual int OnPerformDrop(const DropTargetEvent& event);
811
812 // Returns true if the mouse was dragged enough to start a drag operation.
813 // delta_x and y are the distance the mouse was dragged.
814 static bool ExceededDragThreshold(int delta_x, int delta_y);
815
816 // This method is the main entry point to process paint for this
817 // view and its children. This method is called by the painting
818 // system. You should call this only if you want to draw a sub tree
819 // inside a custom graphics.
820 // To customize painting override either the Paint or PaintChildren method,
821 // not this one.
[email protected]82522512009-05-15 07:37:29822 virtual void ProcessPaint(gfx::Canvas* canvas);
initial.commit09911bf2008-07-26 23:55:29823
824 // Paint the View's child Views, in reverse order.
[email protected]82522512009-05-15 07:37:29825 virtual void PaintChildren(gfx::Canvas* canvas);
initial.commit09911bf2008-07-26 23:55:29826
827 // Sets the ContextMenuController. Setting this to non-null makes the View
828 // process mouse events.
829 void SetContextMenuController(ContextMenuController* menu_controller);
830 ContextMenuController* GetContextMenuController() {
831 return context_menu_controller_;
832 }
833
[email protected]042811c2008-10-31 21:31:34834 // Provides default implementation for context menu handling. The default
835 // implementation calls the ShowContextMenu of the current
836 // ContextMenuController (if it is not NULL). Overridden in subclassed views
837 // to provide right-click menu display triggerd by the keyboard (i.e. for the
838 // Chrome toolbar Back and Forward buttons). No source needs to be specified,
839 // as it is always equal to the current View.
[email protected]e9adf0702010-03-08 23:34:07840 virtual void ShowContextMenu(const gfx::Point& p,
[email protected]042811c2008-10-31 21:31:34841 bool is_mouse_gesture);
842
[email protected]9a3f0ac22008-11-14 03:24:02843 // The background object is owned by this object and may be NULL.
844 void set_background(Background* b) { background_.reset(b); }
845 const Background* background() const { return background_.get(); }
initial.commit09911bf2008-07-26 23:55:29846
[email protected]9a3f0ac22008-11-14 03:24:02847 // The border object is owned by this object and may be NULL.
848 void set_border(Border* b) { border_.reset(b); }
849 const Border* border() const { return border_.get(); }
initial.commit09911bf2008-07-26 23:55:29850
851 // Returns the insets of the current border. If there is no border an empty
852 // insets is returned.
[email protected]a37bea82009-04-22 23:02:15853 virtual gfx::Insets GetInsets() const;
initial.commit09911bf2008-07-26 23:55:29854
855 // Return the cursor that should be used for this view or NULL if
856 // the default cursor should be used. The provided point is in the
[email protected]9abf8dd62009-06-04 06:40:42857 // receiver's coordinate system. The caller is responsible for managing the
858 // lifetime of the returned object, though that lifetime may vary from
859 // platform to platform. On Windows, the cursor is a shared resource but in
860 // Gtk, the framework destroys the returned cursor after setting it.
861 virtual gfx::NativeCursor GetCursorForPoint(Event::EventType event_type,
[email protected]e9adf0702010-03-08 23:34:07862 const gfx::Point& p);
initial.commit09911bf2008-07-26 23:55:29863
864 // Convenience to test whether a point is within this view's bounds
[email protected]613b8062008-10-14 23:45:09865 virtual bool HitTest(const gfx::Point& l) const;
initial.commit09911bf2008-07-26 23:55:29866
867 // Gets the tooltip for this View. If the View does not have a tooltip,
868 // return false. If the View does have a tooltip, copy the tooltip into
869 // the supplied string and return true.
870 // Any time the tooltip text that a View is displaying changes, it must
871 // invoke TooltipTextChanged.
[email protected]e9adf0702010-03-08 23:34:07872 // |p| provides the coordinates of the mouse (relative to this view).
873 virtual bool GetTooltipText(const gfx::Point& p, std::wstring* tooltip);
initial.commit09911bf2008-07-26 23:55:29874
875 // Returns the location (relative to this View) for the text on the tooltip
876 // to display. If false is returned (the default), the tooltip is placed at
877 // a default position.
[email protected]e9adf0702010-03-08 23:34:07878 virtual bool GetTooltipTextOrigin(const gfx::Point& p, gfx::Point* loc);
initial.commit09911bf2008-07-26 23:55:29879
880 // Set whether this view is owned by its parent. A view that is owned by its
881 // parent is automatically deleted when the parent is deleted. The default is
882 // true. Set to false if the view is owned by another object and should not
883 // be deleted by its parent.
[email protected]09fe9492009-11-07 02:23:06884 void set_parent_owned(bool is_parent_owned) {
885 is_parent_owned_ = is_parent_owned;
886 }
initial.commit09911bf2008-07-26 23:55:29887
[email protected]09fe9492009-11-07 02:23:06888 // Return whether a view is owned by its parent.
889 bool IsParentOwned() const { return is_parent_owned_; }
initial.commit09911bf2008-07-26 23:55:29890
891 // Return the receiving view's class name. A view class is a string which
892 // uniquely identifies the view class. It is intended to be used as a way to
893 // find out during run time if a view can be safely casted to a specific view
894 // subclass. The default implementation returns kViewClassName.
895 virtual std::string GetClassName() const;
896
[email protected]5c2b98b2009-03-09 20:55:54897 // Returns the first ancestor, starting at this, whose class name is |name|.
898 // Returns null if no ancestor has the class name |name|.
899 View* GetAncestorWithClassName(const std::string& name);
900
initial.commit09911bf2008-07-26 23:55:29901 // Returns the visible bounds of the receiver in the receivers coordinate
902 // system.
903 //
904 // When traversing the View hierarchy in order to compute the bounds, the
905 // function takes into account the mirroring setting for each View and
906 // therefore it will return the mirrored version of the visible bounds if
907 // need be.
908 gfx::Rect GetVisibleBounds();
909
910 // Subclasses that contain traversable children that are not directly
911 // accessible through the children hierarchy should return the associated
912 // FocusTraversable for the focus traversal to work properly.
913 virtual FocusTraversable* GetFocusTraversable() { return NULL; }
914
915#ifndef NDEBUG
916 // Debug method that logs the view hierarchy to the output.
917 void PrintViewHierarchy();
918
919 // Debug method that logs the focus traversal hierarchy to the output.
920 void PrintFocusHierarchy();
921#endif
922
923 // The following methods are used by ScrollView to determine the amount
924 // to scroll relative to the visible bounds of the view. For example, a
925 // return value of 10 indicates the scrollview should scroll 10 pixels in
926 // the appropriate direction.
927 //
928 // Each method takes the following parameters:
929 //
930 // is_horizontal: if true, scrolling is along the horizontal axis, otherwise
931 // the vertical axis.
932 // is_positive: if true, scrolling is by a positive amount. Along the
933 // vertical axis scrolling by a positive amount equates to
934 // scrolling down.
935 //
936 // The return value should always be positive and gives the number of pixels
937 // to scroll. ScrollView interprets a return value of 0 (or negative)
938 // to scroll by a default amount.
939 //
940 // See VariableRowHeightScrollHelper and FixedRowHeightScrollHelper for
941 // implementations of common cases.
942 virtual int GetPageScrollIncrement(ScrollView* scroll_view,
943 bool is_horizontal, bool is_positive);
944 virtual int GetLineScrollIncrement(ScrollView* scroll_view,
945 bool is_horizontal, bool is_positive);
946
[email protected]4a190632009-05-09 01:07:42947 // Get the theme provider from the parent widget.
[email protected]45da6c72009-10-28 20:45:42948 ThemeProvider* GetThemeProvider() const;
[email protected]4a190632009-05-09 01:07:42949
initial.commit09911bf2008-07-26 23:55:29950 protected:
initial.commit09911bf2008-07-26 23:55:29951 // The id of this View. Used to find this View.
952 int id_;
953
954 // The group of this view. Some view subclasses use this id to find other
955 // views of the same group. For example radio button uses this information
956 // to find other radio buttons.
957 int group_;
958
[email protected]32670b02009-03-03 00:28:00959 // Called when the UI theme has changed, overriding allows individual Views to
960 // do special cleanup and processing (such as dropping resource caches).
961 // Subclasses that override this method must call the base class
962 // implementation to ensure child views are processed.
963 // Can only be called by subclasses. To dispatch a theme changed notification,
964 // call this method on the RootView.
965 virtual void ThemeChanged();
966
[email protected]b2b718012010-03-25 15:09:06967 // Called when the locale has changed, overriding allows individual Views to
[email protected]7ceeba72010-04-20 18:22:12968 // update locale-dependent strings.
969 virtual void LocaleChanged() { }
[email protected]b2b718012010-03-25 15:09:06970
initial.commit09911bf2008-07-26 23:55:29971#ifndef NDEBUG
972 // Returns true if the View is currently processing a paint.
973 virtual bool IsProcessingPaint() const;
974#endif
975
[email protected]f704ee72008-11-10 21:31:59976 // Returns the location, in screen coordinates, to show the context menu at
977 // when the context menu is shown from the keyboard. This implementation
978 // returns the middle of the visible region of this view.
979 //
980 // This method is invoked when the context menu is shown by way of the
981 // keyboard.
982 virtual gfx::Point GetKeyboardContextMenuLocation();
983
[email protected]82739cf2008-09-16 00:37:56984 // Called by HitTest to see if this View has a custom hit test mask. If the
985 // return value is true, GetHitTestMask will be called to obtain the mask.
986 // Default value is false, in which case the View will hit-test against its
987 // bounds.
988 virtual bool HasHitTestMask() const;
989
990 // Called by HitTest to retrieve a mask for hit-testing against. Subclasses
991 // override to provide custom shaped hit test regions.
992 virtual void GetHitTestMask(gfx::Path* mask) const;
993
initial.commit09911bf2008-07-26 23:55:29994 // This method is invoked when the tree changes.
995 //
996 // When a view is removed, it is invoked for all children and grand
997 // children. For each of these views, a notification is sent to the
998 // view and all parents.
999 //
1000 // When a view is added, a notification is sent to the view, all its
1001 // parents, and all its children (and grand children)
1002 //
1003 // Default implementation does nothing. Override to perform operations
1004 // required when a view is added or removed from a view hierarchy
1005 //
1006 // parent is the new or old parent. Child is the view being added or
1007 // removed.
1008 //
1009 virtual void ViewHierarchyChanged(bool is_add, View* parent, View* child);
1010
1011 // When SetVisible() changes the visibility of a view, this method is
1012 // invoked for that view as well as all the children recursively.
1013 virtual void VisibilityChanged(View* starting_from, bool is_visible);
1014
[email protected]bda9556c2010-01-07 00:55:161015 // Called when the native view hierarchy changed.
1016 // |attached| is true if that view has been attached to a new NativeView
1017 // hierarchy, false if it has been detached.
1018 // |native_view| is the NativeView this view was attached/detached from, and
1019 // |root_view| is the root view associated with the NativeView.
1020 // Views created without a native view parent don't have a focus manager.
1021 // When this function is called they could do the processing that requires
1022 // it - like registering accelerators, for example.
1023 virtual void NativeViewHierarchyChanged(bool attached,
1024 gfx::NativeView native_view,
1025 RootView* root_view);
1026
[email protected]7ccc52b72009-05-08 21:09:111027 // Called when the preferred size of a child view changed. This gives the
1028 // parent an opportunity to do a fresh layout if that makes sense.
1029 virtual void ChildPreferredSizeChanged(View* child) {}
1030
1031 // Simply calls ChildPreferredSizeChanged on the parent if there is one.
1032 virtual void PreferredSizeChanged();
1033
initial.commit09911bf2008-07-26 23:55:291034 // Views must invoke this when the tooltip text they are to display changes.
1035 void TooltipTextChanged();
1036
initial.commit09911bf2008-07-26 23:55:291037 // Sets whether this view wants notification when its visible bounds relative
1038 // to the root view changes. If true, this view is notified any time the
1039 // origin of one its ancestors changes, or the portion of the bounds not
1040 // obscured by ancestors changes. The default is false.
1041 void SetNotifyWhenVisibleBoundsInRootChanges(bool value);
1042 bool GetNotifyWhenVisibleBoundsInRootChanges();
1043
1044 // Notification that this views visible bounds, relative to the RootView
1045 // has changed. The visible bounds corresponds to the region of the
1046 // view not obscured by other ancestors.
1047 virtual void VisibleBoundsInRootChanged() {}
1048
1049 // Sets the keyboard focus to this View. The correct way to set the focus is
1050 // to call RequestFocus() on the view. This method is called when the focus is
1051 // set and gives an opportunity to subclasses to perform any extra focus steps
1052 // (for example native component set the native focus on their native
1053 // component). The default behavior is to set the native focus on the root
[email protected]a0dde122008-11-21 20:51:201054 // Widget, which is what is appropriate for views that have no native window
1055 // associated with them (so the root view gets the keyboard messages).
initial.commit09911bf2008-07-26 23:55:291056 virtual void Focus();
1057
initial.commit09911bf2008-07-26 23:55:291058 // These are cover methods that invoke the method of the same name on
1059 // the DragController. Subclasses may wish to override rather than install
1060 // a DragController.
1061 // See DragController for a description of these methods.
[email protected]e9adf0702010-03-08 23:34:071062 virtual int GetDragOperations(const gfx::Point& press_pt);
1063 virtual void WriteDragData(const gfx::Point& press_pt, OSExchangeData* data);
initial.commit09911bf2008-07-26 23:55:291064
1065 // Invoked from DoDrag after the drag completes. This implementation does
1066 // nothing, and is intended for subclasses to do cleanup.
1067 virtual void OnDragDone();
1068
1069 // Returns whether we're in the middle of a drag session that was initiated
1070 // by us.
1071 bool InDrag();
1072
[email protected]253a39a2009-05-29 20:45:131073 // Returns how much the mouse needs to move in one direction to start a
1074 // drag. These methods cache in a platform-appropriate way. These values are
1075 // used by the public static method ExceededDragThreshold().
1076 static int GetHorizontalDragThreshold();
1077 static int GetVerticalDragThreshold();
1078
initial.commit09911bf2008-07-26 23:55:291079 // Whether this view is enabled.
1080 bool enabled_;
1081
1082 // Whether the view can be focused.
1083 bool focusable_;
1084
1085 private:
1086 friend class RootView;
1087 friend class FocusManager;
1088 friend class ViewStorage;
1089
1090 // Used to track a drag. RootView passes this into
1091 // ProcessMousePressed/Dragged.
1092 struct DragInfo {
1093 // Sets possible_drag to false and start_x/y to 0. This is invoked by
1094 // RootView prior to invoke ProcessMousePressed.
1095 void Reset();
1096
[email protected]e9adf0702010-03-08 23:34:071097 // Sets possible_drag to true and start_pt to the specified point.
initial.commit09911bf2008-07-26 23:55:291098 // This is invoked by the target view if it detects the press may generate
1099 // a drag.
[email protected]e9adf0702010-03-08 23:34:071100 void PossibleDrag(const gfx::Point& p);
initial.commit09911bf2008-07-26 23:55:291101
1102 // Whether the press may generate a drag.
1103 bool possible_drag;
1104
1105 // Coordinates of the mouse press.
[email protected]e9adf0702010-03-08 23:34:071106 gfx::Point start_pt;
initial.commit09911bf2008-07-26 23:55:291107 };
1108
[email protected]7ceeba72010-04-20 18:22:121109 // Propagates locale changed notification from the root view downside.
1110 // Invokes LocaleChanged() for every view in the hierarchy.
1111 virtual void NotifyLocaleChanged();
1112
initial.commit09911bf2008-07-26 23:55:291113 // RootView invokes these. These in turn invoke the appropriate OnMouseXXX
1114 // method. If a drag is detected, DoDrag is invoked.
1115 bool ProcessMousePressed(const MouseEvent& e, DragInfo* drop_info);
1116 bool ProcessMouseDragged(const MouseEvent& e, DragInfo* drop_info);
1117 void ProcessMouseReleased(const MouseEvent& e, bool canceled);
1118
1119 // Starts a drag and drop operation originating from this view. This invokes
1120 // WriteDragData to write the data and GetDragOperations to determine the
1121 // supported drag operations. When done, OnDragDone is invoked.
[email protected]e9adf0702010-03-08 23:34:071122 void DoDrag(const MouseEvent& e, const gfx::Point& press_pt);
initial.commit09911bf2008-07-26 23:55:291123
initial.commit09911bf2008-07-26 23:55:291124 // Removes |view| from the hierarchy tree. If |update_focus_cycle| is true,
1125 // the next and previous focusable views of views pointing to this view are
1126 // updated. If |update_tool_tip| is true, the tooltip is updated. If
1127 // |delete_removed_view| is true, the view is also deleted (if it is parent
1128 // owned).
1129 void DoRemoveChildView(View* view,
1130 bool update_focus_cycle,
1131 bool update_tool_tip,
1132 bool delete_removed_view);
1133
1134 // Sets the parent View. This is called automatically by AddChild and is
1135 // thus private.
[email protected]054e3fd2009-09-14 19:48:051136 void SetParent(View* parent);
initial.commit09911bf2008-07-26 23:55:291137
1138 // Call ViewHierarchyChanged for all child views on all parents
1139 void PropagateRemoveNotifications(View* parent);
1140
1141 // Call ViewHierarchyChanged for all children
1142 void PropagateAddNotifications(View* parent, View* child);
1143
1144 // Call VisibilityChanged() recursively for all children.
1145 void PropagateVisibilityNotifications(View* from, bool is_visible);
1146
[email protected]bda9556c2010-01-07 00:55:161147 // Propagates NativeViewHierarchyChanged() notification through all the
1148 // children.
1149 void PropagateNativeViewHierarchyChanged(bool attached,
1150 gfx::NativeView native_view,
1151 RootView* root_view);
1152
initial.commit09911bf2008-07-26 23:55:291153 // Takes care of registering/unregistering accelerators if
1154 // |register_accelerators| true and calls ViewHierarchyChanged().
1155 void ViewHierarchyChangedImpl(bool register_accelerators,
[email protected]bb515ed2009-01-15 00:53:431156 bool is_add,
1157 View* parent,
1158 View* child);
initial.commit09911bf2008-07-26 23:55:291159
1160 // This is the actual implementation for ConvertPointToView()
1161 // Attempts a parent -> child conversion and then a
1162 // child -> parent conversion if try_other_direction is true
[email protected]bb515ed2009-01-15 00:53:431163 static void ConvertPointToView(const View* src,
1164 const View* dst,
initial.commit09911bf2008-07-26 23:55:291165 gfx::Point* point,
1166 bool try_other_direction);
1167
[email protected]a0dde122008-11-21 20:51:201168 // Propagates UpdateTooltip() to the TooltipManager for the Widget.
initial.commit09911bf2008-07-26 23:55:291169 // This must be invoked any time the View hierarchy changes in such a way
1170 // the view under the mouse differs. For example, if the bounds of a View is
1171 // changed, this is invoked. Similarly, as Views are added/removed, this
1172 // is invoked.
1173 void UpdateTooltip();
1174
1175 // Recursively descends through all descendant views,
1176 // registering/unregistering all views that want visible bounds in root
1177 // view notification.
1178 static void RegisterChildrenForVisibleBoundsNotification(RootView* root,
1179 View* view);
1180 static void UnregisterChildrenForVisibleBoundsNotification(RootView* root,
1181 View* view);
1182
1183 // Adds/removes view to the list of descendants that are notified any time
1184 // this views location and possibly size are changed.
1185 void AddDescendantToNotify(View* view);
1186 void RemoveDescendantToNotify(View* view);
1187
1188 // Initialize the previous/next focusable views of the specified view relative
1189 // to the view at the specified index.
1190 void InitFocusSiblings(View* view, int index);
1191
1192 // Actual implementation of PrintFocusHierarchy.
1193 void PrintViewHierarchyImp(int indent);
1194 void PrintFocusHierarchyImp(int indent);
1195
[email protected]71421c3f2009-06-06 00:41:441196 // Registers this view's keyboard accelerators that are not registered to
1197 // FocusManager yet, if possible.
1198 void RegisterPendingAccelerators();
1199
1200 // Unregisters all the keyboard accelerators associated with this view.
[email protected]bda9556c2010-01-07 00:55:161201 // |leave_data_intact| if true does not remove data from accelerators_ array,
1202 // so it could be re-registered with other focus manager
1203 void UnregisterAccelerators(bool leave_data_intact);
initial.commit09911bf2008-07-26 23:55:291204
[email protected]80f8b9f2008-10-16 18:17:471205 // This View's bounds in the parent coordinate system.
1206 gfx::Rect bounds_;
1207
initial.commit09911bf2008-07-26 23:55:291208 // This view's parent
[email protected]054e3fd2009-09-14 19:48:051209 View* parent_;
initial.commit09911bf2008-07-26 23:55:291210
1211 // This view's children.
1212 typedef std::vector<View*> ViewList;
1213 ViewList child_views_;
1214
initial.commit09911bf2008-07-26 23:55:291215 // The View's LayoutManager defines the sizing heuristics applied to child
1216 // Views. The default is absolute positioning according to bounds_.
1217 scoped_ptr<LayoutManager> layout_manager_;
1218
1219 // Visible state
1220 bool is_visible_;
1221
1222 // Background
[email protected]9a3f0ac22008-11-14 03:24:021223 scoped_ptr<Background> background_;
initial.commit09911bf2008-07-26 23:55:291224
1225 // Border.
[email protected]9a3f0ac22008-11-14 03:24:021226 scoped_ptr<Border> border_;
initial.commit09911bf2008-07-26 23:55:291227
1228 // Whether this view is owned by its parent.
1229 bool is_parent_owned_;
1230
1231 // See SetNotifyWhenVisibleBoundsInRootChanges.
1232 bool notify_when_visible_bounds_in_root_changes_;
1233
1234 // Whether or not RegisterViewForVisibleBoundsNotification on the RootView
1235 // has been invoked.
1236 bool registered_for_visible_bounds_notification_;
1237
[email protected]bda9556c2010-01-07 00:55:161238 // true if when we were added to hierarchy we were without focus manager
1239 // attempt addition when ancestor chain changed.
1240 bool accelerator_registration_delayed_;
1241
initial.commit09911bf2008-07-26 23:55:291242 // List of descendants wanting notification when their visible bounds change.
1243 scoped_ptr<ViewList> descendants_to_notify_;
1244
[email protected]d5c81012010-04-03 00:56:121245 // Name for this view, which can be retrieved by accessibility APIs.
1246 std::wstring accessible_name_;
1247
initial.commit09911bf2008-07-26 23:55:291248 // Next view to be focused when the Tab key is pressed.
1249 View* next_focusable_view_;
1250
1251 // Next view to be focused when the Shift-Tab key combination is pressed.
1252 View* previous_focusable_view_;
1253
[email protected]bda9556c2010-01-07 00:55:161254 // Focus manager accelerators registered on.
1255 FocusManager* accelerator_focus_manager_;
1256
[email protected]71421c3f2009-06-06 00:41:441257 // The list of accelerators. List elements in the range
1258 // [0, registered_accelerator_count_) are already registered to FocusManager,
1259 // and the rest are not yet.
[email protected]1eb89e82008-08-15 12:27:031260 scoped_ptr<std::vector<Accelerator> > accelerators_;
[email protected]4bd23f32009-06-08 20:59:191261 size_t registered_accelerator_count_;
initial.commit09911bf2008-07-26 23:55:291262
initial.commit09911bf2008-07-26 23:55:291263 // The menu controller.
1264 ContextMenuController* context_menu_controller_;
1265
[email protected]6ff244f2009-01-20 20:38:081266#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291267 // The accessibility implementation for this View.
[email protected]fef10642009-03-17 21:17:041268 scoped_ptr<ViewAccessibilityWrapper> accessibility_;
[email protected]6ff244f2009-01-20 20:38:081269#endif
initial.commit09911bf2008-07-26 23:55:291270
1271 DragController* drag_controller_;
1272
[email protected]82522512009-05-15 07:37:291273 // Indicates whether or not the gfx::Canvas object passed to View::Paint()
initial.commit09911bf2008-07-26 23:55:291274 // is going to be flipped horizontally (using the appropriate transform) on
1275 // right-to-left locales for this View.
1276 bool flip_canvas_on_paint_for_rtl_ui_;
1277
[email protected]8af4c1992010-02-04 21:38:071278 // The default value for how long to wait (in ms) before showing a menu
1279 // button on hover. This value is used if the OS doesn't supply one.
1280 static const int kShowFolderDropMenuDelay;
1281
[email protected]1eb89e82008-08-15 12:27:031282 DISALLOW_COPY_AND_ASSIGN(View);
initial.commit09911bf2008-07-26 23:55:291283};
1284
[email protected]c2dacc92008-10-16 23:51:381285} // namespace views
initial.commit09911bf2008-07-26 23:55:291286
[email protected]2362e4f2009-05-08 00:34:051287#endif // VIEWS_VIEW_H_