initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame^] | 1 | // Copyright 2008, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #ifndef CHROME_VIEWS_VIEW_H__ |
| 31 | #define CHROME_VIEWS_VIEW_H__ |
| 32 | |
| 33 | #include <atlbase.h> |
| 34 | #include <atlapp.h> |
| 35 | #include <atlmisc.h> |
| 36 | #include <map> |
| 37 | #include <vector> |
| 38 | |
| 39 | #include "base/basictypes.h" |
| 40 | #include "base/gfx/rect.h" |
| 41 | #include "base/scoped_ptr.h" |
| 42 | #include "chrome/common/drag_drop_types.h" |
| 43 | #include "chrome/common/l10n_util.h" |
| 44 | #include "chrome/common/gfx/insets.h" |
| 45 | #include "chrome/views/accessibility/accessible_wrapper.h" |
| 46 | #include "chrome/views/accelerator.h" |
| 47 | #include "chrome/views/event.h" |
| 48 | #include "chrome/views/view_container.h" |
| 49 | |
| 50 | class ChromeCanvas; |
| 51 | class OSExchangeData; |
| 52 | class SkBitmap; |
| 53 | |
| 54 | namespace ChromeViews { |
| 55 | |
| 56 | class Background; |
| 57 | class Border; |
| 58 | class FocusManager; |
| 59 | class FocusTraversable; |
| 60 | class LayoutManager; |
| 61 | class RestoreFocusTask; |
| 62 | class RootView; |
| 63 | class ScrollView; |
| 64 | class ViewContainer; |
| 65 | |
| 66 | // ContextMenuController is responsible for showing the context menu for a |
| 67 | // View. To use a ContextMenuController invoke SetContextMenuController on a |
| 68 | // View. When the appropriate user gesture occurs ShowContextMenu is invoked |
| 69 | // on the ContextMenuController. |
| 70 | // |
| 71 | // Setting a ContextMenuController on a view makes the view process mouse |
| 72 | // events. |
| 73 | // |
| 74 | // It is up to subclasses that do their own mouse processing to invoke |
| 75 | // the appropriate ContextMenuController method, typically by invoking super's |
| 76 | // implementation for mouse processing. |
| 77 | // |
| 78 | class ContextMenuController { |
| 79 | public: |
| 80 | // Invoked to show the context menu for the source view. If is_mouse_gesture |
| 81 | // is true, the x/y coordinate are the location of the mouse. If |
| 82 | // is_mouse_gesture is false, this method was not invoked by a mouse gesture |
| 83 | // and x/y is the recommended location to show the menu at. |
| 84 | // |
| 85 | // x/y is in screen coordinates. |
| 86 | virtual void ShowContextMenu(View* source, |
| 87 | int x, |
| 88 | int y, |
| 89 | bool is_mouse_gesture) = 0; |
| 90 | }; |
| 91 | |
| 92 | // DragController is responsible for writing drag data for a view, as well as |
| 93 | // supplying the supported drag operations. Use DragController if you don't |
| 94 | // want to subclass. |
| 95 | |
| 96 | class DragController { |
| 97 | public: |
| 98 | // Writes the data for the drag. |
| 99 | virtual void WriteDragData(View* sender, |
| 100 | int press_x, |
| 101 | int press_y, |
| 102 | OSExchangeData* data) = 0; |
| 103 | |
| 104 | // Returns the supported drag operations (see DragDropTypes for possible |
| 105 | // values). A drag is only started if this returns a non-zero value. |
| 106 | virtual int GetDragOperations(View* sender, int x, int y) = 0; |
| 107 | }; |
| 108 | |
| 109 | |
| 110 | ///////////////////////////////////////////////////////////////////////////// |
| 111 | // |
| 112 | // View class |
| 113 | // |
| 114 | // A View is a rectangle within the ChromeViews View hierarchy. It is the |
| 115 | // base class for all Views. |
| 116 | // |
| 117 | // A View is a container of other Views (there is no such thing as a Leaf |
| 118 | // View - makes code simpler, reduces type conversion headaches, design |
| 119 | // mistakes etc) |
| 120 | // |
| 121 | // The View contains basic properties for sizing (bounds), layout (flex, |
| 122 | // orientation, etc), painting of children and event dispatch. |
| 123 | // |
| 124 | // The View also uses a simple Box Layout Manager similar to XUL's |
| 125 | // SprocketLayout system. Alternative Layout Managers implementing the |
| 126 | // LayoutManager interface can be used to lay out children if required. |
| 127 | // |
| 128 | // It is up to the subclass to implement Painting and storage of subclass - |
| 129 | // specific properties and functionality. |
| 130 | // |
| 131 | ///////////////////////////////////////////////////////////////////////////// |
| 132 | class View : public AcceleratorTarget { |
| 133 | public: |
| 134 | |
| 135 | // Used in EnumerateFloatingViews() to specify which floating view to |
| 136 | // retrieve. |
| 137 | enum FloatingViewPosition { |
| 138 | FIRST = 0, |
| 139 | NEXT, |
| 140 | PREVIOUS, |
| 141 | LAST, |
| 142 | CURRENT |
| 143 | }; |
| 144 | |
| 145 | // Used in the versions of GetBounds() and GetX() that take a transformation |
| 146 | // parameter in order to determine whether or not to take into account the |
| 147 | // mirroring setting of the View when returning bounds positions. |
| 148 | enum PositionMirroringSettings { |
| 149 | IGNORE_MIRRORING_TRANSFORMATION = 0, |
| 150 | APPLY_MIRRORING_TRANSFORMATION |
| 151 | }; |
| 152 | |
| 153 | // The view class name. |
| 154 | static char kViewClassName[]; |
| 155 | |
| 156 | View(); |
| 157 | virtual ~View(); |
| 158 | |
| 159 | // Sizing functions |
| 160 | |
| 161 | // Get the bounds of the View, relative to the parent. Essentially, this |
| 162 | // function returns the bounds_ rectangle. |
| 163 | // |
| 164 | // This is the function subclasses should use whenever they need to obtain |
| 165 | // the bounds of one of their child views (for example, when implementing |
| 166 | // View::Layout()). |
| 167 | void GetBounds(CRect *out) const { |
| 168 | GetBounds(out, IGNORE_MIRRORING_TRANSFORMATION); |
| 169 | }; |
| 170 | |
| 171 | // Return the bounds of the View, relative to the parent. If |
| 172 | // |settings| is IGNORE_MIRRORING_TRANSFORMATION, the function returns the |
| 173 | // bounds_ rectangle. If |settings| is APPLY_MIRRORING_SETTINGS AND the |
| 174 | // parent View is using a right-to-left UI layout, then the function returns |
| 175 | // a shifted version of the bounds_ rectangle that represents the mirrored |
| 176 | // View bounds. |
| 177 | // |
| 178 | // NOTE: in the vast majority of the cases, the mirroring implementation is |
| 179 | // transparent to the View subclasses and therefore you should use the |
| 180 | // version of GetBounds() which does not take a transformation settings |
| 181 | // parameter. |
| 182 | void GetBounds(CRect *out, PositionMirroringSettings settings) const; |
| 183 | |
| 184 | // Set the bounds in the parent's coordinate system. |
| 185 | void SetBounds(const CRect& bounds); |
| 186 | void SetBounds(int x, int y, int width, int height); |
| 187 | void SetX(int x) { SetBounds(x, GetY(), GetWidth(), GetHeight()); } |
| 188 | void SetY(int y) { SetBounds(GetX(), y, GetWidth(), GetHeight()); } |
| 189 | |
| 190 | // Returns the left coordinate of the View, relative to the parent View, |
| 191 | // which is essentially the value of bounds_.left. |
| 192 | // |
| 193 | // This is the function subclasses should use whenever they need to obtain |
| 194 | // the left position of one of their child views (for example, when |
| 195 | // implementing View::Layout()). |
| 196 | inline int GetX() const { |
| 197 | return GetX(IGNORE_MIRRORING_TRANSFORMATION); |
| 198 | }; |
| 199 | |
| 200 | // Return the left coordinate of the View, relative to the parent. If |
| 201 | // |settings| is IGNORE_MIRRORING_SETTINGS, the function returns the value of |
| 202 | // bounds_.left. If |settings| is APPLY_MIRRORING_SETTINGS AND the parent |
| 203 | // View is using a right-to-left UI layout, then the function returns the |
| 204 | // mirrored value of bounds_.left. |
| 205 | // |
| 206 | // NOTE: in the vast majority of the cases, the mirroring implementation is |
| 207 | // transparent to the View subclasses and therefore you should use the |
| 208 | // paremeterless version of GetX() when you need to get the X |
| 209 | // coordinate of a child View. |
| 210 | int GetX(PositionMirroringSettings settings) const; |
| 211 | |
| 212 | inline int GetY() const { |
| 213 | return bounds_.top; |
| 214 | }; |
| 215 | inline int GetWidth() const { |
| 216 | return bounds_.Width(); |
| 217 | }; |
| 218 | inline int GetHeight() const { |
| 219 | return bounds_.Height(); |
| 220 | }; |
| 221 | |
| 222 | // Return this control local bounds. If include_border is true, local bounds |
| 223 | // is the rectangle {0, 0, GetWidth(), GetHeight()}, otherwise, it does not |
| 224 | // include the area where the border (if any) is painted. |
| 225 | void GetLocalBounds(CRect* out, bool include_border) const; |
| 226 | |
| 227 | // Get the size of the View |
| 228 | void GetSize(CSize* out) const; |
| 229 | |
| 230 | // Get the position of the View, relative to the parent. |
| 231 | // |
| 232 | // Note that if the parent uses right-to-left UI layout, then the mirrored |
| 233 | // position of this View is returned. Use GetX()/GetY() if you want to ignore |
| 234 | // mirroring. |
| 235 | void GetPosition(CPoint* out) const; |
| 236 | |
| 237 | // Get the size the View would like to be, if enough space were available. |
| 238 | virtual void GetPreferredSize(CSize* out); |
| 239 | |
| 240 | // Convenience method that sizes this view to its preferred size. |
| 241 | void SizeToPreferredSize(); |
| 242 | |
| 243 | // Gets the minimum size of the view. View's implementation invokes |
| 244 | // GetPreferredSize. |
| 245 | virtual void GetMinimumSize(CSize* out); |
| 246 | |
| 247 | // Return the height necessary to display this view with the provided width. |
| 248 | // View's implementation returns the value from getPreferredSize.cy. |
| 249 | // Override if your View's preferred height depends upon the width (such |
| 250 | // as with Labels). |
| 251 | virtual int GetHeightForWidth(int w); |
| 252 | |
| 253 | // This method is invoked when this object size or position changes. |
| 254 | // The default implementation does nothing. |
| 255 | virtual void DidChangeBounds(const CRect& previous, const CRect& current); |
| 256 | |
| 257 | // Set whether the receiving view is visible. Painting is scheduled as needed |
| 258 | virtual void SetVisible(bool flag); |
| 259 | |
| 260 | // Return whether a view is visible |
| 261 | virtual bool IsVisible() const { return is_visible_; } |
| 262 | |
| 263 | // Return whether a view and its ancestors are visible. Returns true if the |
| 264 | // path from this view to the root view is visible. |
| 265 | virtual bool IsVisibleInRootView() const; |
| 266 | |
| 267 | // Set whether this view is enabled. A disabled view does not receive keyboard |
| 268 | // or mouse inputs. If flag differs from the current value, SchedulePaint is |
| 269 | // invoked. |
| 270 | virtual void SetEnabled(bool flag); |
| 271 | |
| 272 | // Returns whether the view is enabled. |
| 273 | virtual bool IsEnabled() const; |
| 274 | |
| 275 | // Set whether this view is hottracked. A disabled view cannot be hottracked. |
| 276 | // If flag differs from the current value, SchedulePaint is invoked. |
| 277 | virtual void SetHotTracked(bool flag); |
| 278 | |
| 279 | // Returns whether the view is hot-tracked. |
| 280 | virtual bool IsHotTracked() const { return false; } |
| 281 | |
| 282 | // Returns whether the view is pushed. |
| 283 | virtual bool IsPushed() const { return false; } |
| 284 | |
| 285 | // Scrolls the specified region, in this View's coordinate system, to be |
| 286 | // visible. View's implementation passes the call onto the parent View (after |
| 287 | // adjusting the coordinates). It is up to views that only show a portion of |
| 288 | // the child view, such as Viewport, to override appropriately. |
| 289 | virtual void ScrollRectToVisible(int x, int y, int width, int height); |
| 290 | |
| 291 | // Layout functions |
| 292 | |
| 293 | // Lay out the child Views (set their bounds based on sizing heuristics |
| 294 | // specific to the current Layout Manager) |
| 295 | virtual void Layout(); |
| 296 | |
| 297 | // Gets/Sets the Layout Manager used by this view to size and place its |
| 298 | // children. |
| 299 | // The LayoutManager is owned by the View and is deleted when the view is |
| 300 | // deleted, or when a new LayoutManager is installed. |
| 301 | LayoutManager* GetLayoutManager() const; |
| 302 | void SetLayoutManager(LayoutManager* layout); |
| 303 | |
| 304 | // Right-to-left UI layout functions |
| 305 | |
| 306 | // Indicates whether the UI layout for this view is right-to-left. The view |
| 307 | // has an RTL UI layout if RTL hasn't been disabled for the view and if the |
| 308 | // locale's language is an RTL language. |
| 309 | bool UILayoutIsRightToLeft() const { |
| 310 | return (ui_mirroring_is_enabled_for_rtl_languages_ && |
| 311 | l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT); |
| 312 | } |
| 313 | |
| 314 | // Enables or disables the right-to-left layout for the view. If |enable| is |
| 315 | // true, the layout will become right-to-left only if the locale's language |
| 316 | // is right-to-left. |
| 317 | // |
| 318 | // By default, right-to-left UI layout is enabled for the view and therefore |
| 319 | // this function must be called (with false as the |enable| parameter) in |
| 320 | // order to disable the right-to-left layout property for a specific instance |
| 321 | // of the view. Disabling the right-to-left UI layout is necessary in case a |
| 322 | // UI element will not appear correctly when mirrored. |
| 323 | void EnableUIMirroringForRTLLanguages(bool enable) { |
| 324 | ui_mirroring_is_enabled_for_rtl_languages_ = enable; |
| 325 | } |
| 326 | |
| 327 | // This method determines whether the ChromeCanvas object passed to |
| 328 | // View::Paint() needs to be transformed such that anything drawn on the |
| 329 | // canvas object during View::Paint() is flipped horizontally. |
| 330 | // |
| 331 | // By default, this function returns false (which is the initial value of |
| 332 | // |flip_canvas_on_paint_for_rtl_ui_|). View subclasses that need to paint on |
| 333 | // a flipped ChromeCanvas when the UI layout is right-to-left need to call |
| 334 | // EnableCanvasFlippingForRTLUI(). |
| 335 | bool FlipCanvasOnPaintForRTLUI() const { |
| 336 | return flip_canvas_on_paint_for_rtl_ui_ ? UILayoutIsRightToLeft() : false; |
| 337 | } |
| 338 | |
| 339 | // Enables or disables flipping of the ChromeCanvas during View::Paint(). |
| 340 | // Note that if canvas flipping is enabled, the canvas will be flipped only |
| 341 | // if the UI layout is right-to-left; that is, the canvas will be flipped |
| 342 | // only if UILayoutIsRightToLeft() returns true. |
| 343 | // |
| 344 | // Enabling canvas flipping is useful for leaf views that draw a bitmap that |
| 345 | // needs to be flipped horizontally when the UI layout is right-to-left |
| 346 | // (ChromeViews::Button, for example). This method is helpful for such |
| 347 | // classes because their drawing logic stays the same and they can become |
| 348 | // agnostic to the UI directionality. |
| 349 | void EnableCanvasFlippingForRTLUI(bool enable) { |
| 350 | flip_canvas_on_paint_for_rtl_ui_ = enable; |
| 351 | } |
| 352 | |
| 353 | // Returns the mirrored X position for the view, relative to the parent. If |
| 354 | // the parent view is not mirrored, this function returns bound_.left. |
| 355 | // |
| 356 | // UI mirroring is transparent to most View subclasses and therefore there is |
| 357 | // no need to call this routine from anywhere within your subclass |
| 358 | // implementation. |
| 359 | inline int View::MirroredX() const; |
| 360 | |
| 361 | // Given a rectangle specified in this View's coordinate system, the function |
| 362 | // computes the 'left' value for the mirrored rectangle within this View. If |
| 363 | // the View's UI layout is not right-to-left, then bounds.x() is returned. |
| 364 | // |
| 365 | // UI mirroring is transparent to most View subclasses and therefore there is |
| 366 | // no need to call this routine from anywhere within your subclass |
| 367 | // implementation. |
| 368 | int MirroredLeftPointForRect(const gfx::Rect& rect) const; |
| 369 | |
| 370 | // Given the X coordinate of a point inside the View, this function returns |
| 371 | // the mirrored X coordinate of the point if the View's UI layout is |
| 372 | // right-to-left. If the layout is left-to-right, the same X coordinate is |
| 373 | // returned. |
| 374 | // |
| 375 | // Following are a few examples of the values returned by this function for |
| 376 | // a View with the bounds {0, 0, 100, 100} and a right-to-left layout: |
| 377 | // |
| 378 | // MirroredXCoordinateInsideView(0) -> 100 |
| 379 | // MirroredXCoordinateInsideView(20) -> 80 |
| 380 | // MirroredXCoordinateInsideView(99) -> 1 |
| 381 | int MirroredXCoordinateInsideView(int x) const { |
| 382 | return UILayoutIsRightToLeft() ? GetWidth() - x : x; |
| 383 | } |
| 384 | |
| 385 | // Painting functions |
| 386 | |
| 387 | // Mark the specified rectangle as dirty (needing repaint). If |urgent| is |
| 388 | // true, the view will be repainted when the current event processing is |
| 389 | // done. Otherwise, painting will take place as soon as possible. |
| 390 | virtual void SchedulePaint(const CRect& r, bool urgent); |
| 391 | |
| 392 | // Mark the entire View's bounds as dirty. Painting will occur as soon as |
| 393 | // possible. |
| 394 | virtual void SchedulePaint(); |
| 395 | |
| 396 | // Convenience to schedule a paint given some ints. Painting will occur as |
| 397 | // soon as possible. |
| 398 | virtual void SchedulePaint(int x, int y, int w, int h); |
| 399 | |
| 400 | // Paint the receiving view. g is prepared such as it is in |
| 401 | // receiver's coordinate system. g's state is restored after this |
| 402 | // call so your implementation can change the graphics configuration |
| 403 | // |
| 404 | // Default implementation paints the background if it is defined |
| 405 | // |
| 406 | // Override this method when implementing a new control. |
| 407 | virtual void Paint(ChromeCanvas* canvas); |
| 408 | |
| 409 | // Paint the background if any. This method is called by Paint() and |
| 410 | // should rarely be invoked directly. |
| 411 | virtual void PaintBackground(ChromeCanvas* canvas); |
| 412 | |
| 413 | // Paint the border if any. This method is called by Paint() and |
| 414 | // should rarely be invoked directly. |
| 415 | virtual void PaintBorder(ChromeCanvas* canvas); |
| 416 | |
| 417 | // Paints the focus border (only if the view has the focus). |
| 418 | // This method is called by Paint() and should rarely be invoked directly. |
| 419 | // The default implementation paints a gray border around the view. Override |
| 420 | // it for custom focus effects. |
| 421 | virtual void PaintFocusBorder(ChromeCanvas* canvas); |
| 422 | |
| 423 | // Paint this View immediately. |
| 424 | virtual void PaintNow(); |
| 425 | |
| 426 | // Paint a view without attaching it to this view hierarchy. |
| 427 | // Any view can be painted that way. |
| 428 | // This method set bounds, calls layout and handles clipping properly. The |
| 429 | // provided view can be attached to a parent. The parent will be saved and |
| 430 | // restored. (x, y, width, height) define the floating view bounds |
| 431 | void PaintFloatingView(ChromeCanvas* canvas, View* view, |
| 432 | int x, int y, int w, int h); |
| 433 | |
| 434 | // Tree functions |
| 435 | |
| 436 | // Add a child View. |
| 437 | void AddChildView(View* v); |
| 438 | |
| 439 | // Adds a child View at the specified position. |
| 440 | void AddChildView(int index, View* v); |
| 441 | |
| 442 | // Get the child View at the specified index. |
| 443 | View* GetChildViewAt(int index) const; |
| 444 | |
| 445 | // Remove a child view from this view. v's parent will change to NULL |
| 446 | void RemoveChildView(View *v); |
| 447 | |
| 448 | // Remove all child view from this view. If |delete_views| is true, the views |
| 449 | // are deleted, unless marked as not parent owned. |
| 450 | void RemoveAllChildViews(bool delete_views); |
| 451 | |
| 452 | // Get the number of child Views. |
| 453 | int GetChildViewCount() const; |
| 454 | |
| 455 | // Get the child View at the specified point. |
| 456 | virtual View* GetViewForPoint(const CPoint& point); |
| 457 | |
| 458 | // Get the containing ViewContainer |
| 459 | virtual ViewContainer* GetViewContainer() const; |
| 460 | |
| 461 | // Get the containing RootView |
| 462 | virtual RootView* GetRootView(); |
| 463 | |
| 464 | // Get the parent View |
| 465 | View* GetParent() const { return parent_; } |
| 466 | |
| 467 | // Returns the index of the specified |view| in this view's children, or -1 |
| 468 | // if the specified view is not a child of this view. |
| 469 | int GetChildIndex(View* v) const; |
| 470 | |
| 471 | // Returns true if the specified view is a direct or indirect child of this |
| 472 | // view. |
| 473 | bool IsParentOf(View* v) const; |
| 474 | |
| 475 | // Recursively descends the view tree starting at this view, and returns |
| 476 | // the first child that it encounters that has the given ID. |
| 477 | // Returns NULL if no matching child view is found. |
| 478 | virtual View* GetViewByID(int id) const; |
| 479 | |
| 480 | // Sets and gets the ID for this view. ID should be unique within the subtree |
| 481 | // that you intend to search for it. 0 is the default ID for views. |
| 482 | void SetID(int id); |
| 483 | int GetID() const; |
| 484 | |
| 485 | // A group id is used to tag views which are part of the same logical group. |
| 486 | // Focus can be moved between views with the same group using the arrow keys. |
| 487 | // Groups are currently used to implement radio button mutual exclusion. |
| 488 | void SetGroup(int gid); |
| 489 | int GetGroup() const; |
| 490 | |
| 491 | // If this returns true, the views from the same group can each be focused |
| 492 | // when moving focus with the Tab/Shift-Tab key. If this returns false, |
| 493 | // only the selected view from the group (obtained with |
| 494 | // GetSelectedViewForGroup()) is focused. |
| 495 | virtual bool IsGroupFocusTraversable() const { return true; } |
| 496 | |
| 497 | // Fills the provided vector with all the available views which belong to the |
| 498 | // provided group. |
| 499 | void GetViewsWithGroup(int group_id, std::vector<View*>* out); |
| 500 | |
| 501 | // Return the View that is currently selected in the specified group. |
| 502 | // The default implementation simply returns the first View found for that |
| 503 | // group. |
| 504 | virtual View* GetSelectedViewForGroup(int group_id); |
| 505 | |
| 506 | // Focus support |
| 507 | // |
| 508 | // Returns the view that should be selected next when pressing Tab. |
| 509 | View* GetNextFocusableView(); |
| 510 | |
| 511 | // Returns the view that should be selected next when pressing Shift-Tab. |
| 512 | View* GetPreviousFocusableView(); |
| 513 | |
| 514 | // Sets the component that should be selected next when pressing Tab, and |
| 515 | // makes the current view the precedent view of the specified one. |
| 516 | // Note that by default views are linked in the order they have been added to |
| 517 | // their container. Use this method if you want to modify the order. |
| 518 | // IMPORTANT NOTE: loops in the focus hierarchy are not supported. |
| 519 | void SetNextFocusableView(View* view); |
| 520 | |
| 521 | // Return whether this view can accept the focus. |
| 522 | virtual bool IsFocusable() const; |
| 523 | |
| 524 | // Sets whether this view can accept the focus. |
| 525 | // Note that this is false by default so that a view used as a container does |
| 526 | // not get the focus. |
| 527 | virtual void SetFocusable(bool focusable); |
| 528 | |
| 529 | // Convenience method to retrieve the FocusManager associated with the |
| 530 | // container window that contains this view. This can return NULL if this |
| 531 | // view is not part of a view hierarchy with a ViewContainer. |
| 532 | virtual FocusManager* GetFocusManager(); |
| 533 | |
| 534 | // Sets a keyboard accelerator for that view. When the user presses the |
| 535 | // accelerator key combination, the AcceleratorPressed method is invoked. |
| 536 | // Note that you can set multiple accelerators for a view by invoking this |
| 537 | // method several times. |
| 538 | virtual void AddAccelerator(const Accelerator& accelerator); |
| 539 | |
| 540 | // Removes all the keyboard accelerators for this view. |
| 541 | virtual void ResetAccelerators(); |
| 542 | |
| 543 | // Called when a keyboard accelerator is pressed. |
| 544 | // Derived classes should implement desired behavior and return true if they |
| 545 | // handled the accelerator. |
| 546 | virtual bool AcceleratorPressed(const Accelerator& accelerator) { |
| 547 | return false; |
| 548 | } |
| 549 | |
| 550 | // Called on a view (if it is has focus) before an Accelerator is processed. |
| 551 | // Views that want to override an accelerator should override this method to |
| 552 | // perform the required action and return true, to indicate that the |
| 553 | // accelerator should not be processed any further. |
| 554 | virtual bool OverrideAccelerator(const Accelerator& accelerator) { |
| 555 | return false; |
| 556 | } |
| 557 | |
| 558 | // Returns whether this view currently has the focus. |
| 559 | virtual bool HasFocus(); |
| 560 | |
| 561 | // Accessibility support |
| 562 | // TODO(klink): Move all this out to a AccessibleInfo wrapper class. |
| 563 | // |
| 564 | // Returns the MSAA default action of the current view. The string returned |
| 565 | // describes the default action that will occur when executing |
| 566 | // IAccessible::DoDefaultAction. For instance, default action of a button is |
| 567 | // 'Press'. Sets the input string appropriately, and returns true if |
| 568 | // successful. |
| 569 | virtual bool GetAccessibleDefaultAction(std::wstring* action) { |
| 570 | return false; |
| 571 | } |
| 572 | |
| 573 | // Returns a string containing the mnemonic, or the keyboard shortcut, for a |
| 574 | // given control. Sets the input string appropriately, and returns true if |
| 575 | // successful. |
| 576 | virtual bool GetAccessibleKeyboardShortcut(std::wstring* shortcut) { |
| 577 | return false; |
| 578 | } |
| 579 | |
| 580 | // Returns a brief, identifying string, containing a unique, readable name of |
| 581 | // a given control. Sets the input string appropriately, and returns true if |
| 582 | // successful. |
| 583 | virtual bool GetAccessibleName(std::wstring* name) { return false; } |
| 584 | |
| 585 | // Returns the MSAA role of the current view. The role is what assistive |
| 586 | // technologies (ATs) use to determine what behavior to expect from a given |
| 587 | // control. Sets the input VARIANT appropriately, and returns true if |
| 588 | // successful. |
| 589 | virtual bool GetAccessibleRole(VARIANT* role) { return false; } |
| 590 | |
| 591 | // Returns the MSAA state of the current view. Sets the input VARIANT |
| 592 | // appropriately, and returns true if a change was performed successfully. |
| 593 | virtual bool GetAccessibleState(VARIANT* state) { return false; } |
| 594 | |
| 595 | // Assigns a keyboard shortcut string description to the given control. Needed |
| 596 | // as a View does not know which shortcut will be associated with it until it |
| 597 | // is created to be a certain type. |
| 598 | virtual void SetAccessibleKeyboardShortcut(const std::wstring& shortcut) {} |
| 599 | |
| 600 | // Assigns a string name to the given control. Needed as a View does not know |
| 601 | // which name will be associated with it until it is created to be a |
| 602 | // certain type. |
| 603 | virtual void SetAccessibleName(const std::wstring& name) {} |
| 604 | |
| 605 | // Returns an instance of a wrapper class implementing the (platform-specific) |
| 606 | // accessibility interface for a given View. If one exists, it will be |
| 607 | // re-used, otherwise a new instance will be created. |
| 608 | AccessibleWrapper* GetAccessibleWrapper(); |
| 609 | |
| 610 | // Accessor used to determine if a child view (leaf) has accessibility focus. |
| 611 | // Returns NULL if there are no children, or if none of the children has |
| 612 | // accessibility focus. |
| 613 | virtual ChromeViews::View* GetAccFocusedChildView() { return NULL; } |
| 614 | |
| 615 | // Floating views |
| 616 | // |
| 617 | // A floating view is a view that is used to paint a cell within a parent view |
| 618 | // Floating Views are painted using PaintFloatingView() above. |
| 619 | // |
| 620 | // Floating views can also be lazily created and attached to the view |
| 621 | // hierarchy to process events. To make this possible, each view is given an |
| 622 | // opportunity to create and attach a floating view right before an mouse |
| 623 | // event is processed. |
| 624 | |
| 625 | // Retrieves the id for the floating view at the specified coordinates if any. |
| 626 | // Derived classes that use floating views should implement this method and |
| 627 | // return true if a view has been found and its id set in |id|. |
| 628 | virtual bool GetFloatingViewIDForPoint(int x, int y, int* id); |
| 629 | |
| 630 | // Retrieves the ID of the floating view at the specified |position| and sets |
| 631 | // it in |id|. |
| 632 | // For positions NEXT and PREVIOUS, the specified |starting_id| is used as |
| 633 | // the origin, it is ignored for FIRST and LAST. |
| 634 | // Returns true if an ID was found, false otherwise. |
| 635 | // For CURRENT, the |starting_id| should be set in |id| and true returned if |
| 636 | // the |starting_id| is a valid floating view id. |
| 637 | // Derived classes that use floating views should implement this method and |
| 638 | // return a unique ID for each floating view. |
| 639 | // The default implementation always returns false. |
| 640 | virtual bool EnumerateFloatingViews(FloatingViewPosition position, |
| 641 | int starting_id, |
| 642 | int* id); |
| 643 | |
| 644 | // Creates and attaches the floating view with the specified |id| to this view |
| 645 | // hierarchy and returns it. |
| 646 | // Derived classes that use floating views should implement this method. |
| 647 | // |
| 648 | // NOTE: subclasses implementing this should return NULL if passed an invalid |
| 649 | // id. An invalid ID may be passed in by the focus manager when attempting |
| 650 | // to restore focus. |
| 651 | virtual View* ValidateFloatingViewForID(int id); |
| 652 | |
| 653 | // Whether the focus should automatically be restored to the last focused |
| 654 | // view. Default implementation returns true. |
| 655 | // Derived classes that want to restore focus themselves should override this |
| 656 | // method and return false. |
| 657 | virtual bool ShouldRestoreFloatingViewFocus(); |
| 658 | |
| 659 | // Attach a floating view to the receiving view. The view is inserted |
| 660 | // in the child view list and will behave like a normal view. |id| is the |
| 661 | // floating view id for that view. |
| 662 | void AttachFloatingView(View* v, int id); |
| 663 | |
| 664 | // Return whether a view already has a floating view which bounds intersects |
| 665 | // the provided point. |
| 666 | // |
| 667 | // If the View uses right-to-left UI layout, then the given point is checked |
| 668 | // against the mirrored position of each floating View. |
| 669 | bool HasFloatingViewForPoint(int x, int y); |
| 670 | |
| 671 | // Detach and delete all floating views. Call this method when your model |
| 672 | // or layout changes. |
| 673 | void DetachAllFloatingViews(); |
| 674 | |
| 675 | // Returns the view with the specified |id|, by calling |
| 676 | // ValidateFloatingViewForID if that view has not yet been attached. |
| 677 | virtual View* RetrieveFloatingViewForID(int id); |
| 678 | |
| 679 | // Restores the focus to the previously selected floating view. |
| 680 | virtual void RestoreFloatingViewFocus(); |
| 681 | |
| 682 | // Goes up the parent hierarchy of this view and returns the first floating |
| 683 | // view found. Returns NULL if none were found. |
| 684 | View* RetrieveFloatingViewParent(); |
| 685 | |
| 686 | // Utility functions |
| 687 | |
| 688 | // Note that the utility coordinate conversions functions always operate on |
| 689 | // the mirrored position of the child Views if the parent View uses a |
| 690 | // right-to-left UI layout. |
| 691 | |
| 692 | // Convert a point from source coordinate system to dst coordinate system. |
| 693 | // |
| 694 | // source is a parent or a child of dst, directly or transitively. |
| 695 | // If source and dst are not in the same View hierarchy, the result is |
| 696 | // undefined. |
| 697 | // Source can be NULL in which case it means the screen coordinate system |
| 698 | static void ConvertPointToView(View* src, |
| 699 | View* dst, |
| 700 | gfx::Point* point); |
| 701 | // WARNING: DEPRECATED. Will be removed once everything is converted to |
| 702 | // gfx::Point. Don't add code that use this overload. |
| 703 | static void ConvertPointToView(View* src, |
| 704 | View* dst, |
| 705 | CPoint* point); |
| 706 | |
| 707 | // Convert a point from the coordinate system of a View to that of the |
| 708 | // ViewContainer. This is useful for example when sizing HWND children |
| 709 | // of the ViewContainer that don't know about the View hierarchy and need |
| 710 | // to be placed relative to the ViewContainer that is their parent. |
| 711 | static void ConvertPointToViewContainer(View* src, CPoint* point); |
| 712 | |
| 713 | // Convert a point from a view ViewContainer to a View dest |
| 714 | static void ConvertPointFromViewContainer(View *dest, CPoint *p); |
| 715 | |
| 716 | // Convert a point from the coordinate system of a View to that of the |
| 717 | // screen. This is useful for example when placing popup windows. |
| 718 | static void ConvertPointToScreen(View* src, CPoint* point); |
| 719 | |
| 720 | // Event Handlers |
| 721 | |
| 722 | // This method is invoked when the user clicks on this view. |
| 723 | // The provided event is in the receiver's coordinate system. |
| 724 | // |
| 725 | // Return true if you processed the event and want to receive subsequent |
| 726 | // MouseDraggged and MouseReleased events. This also stops the event from |
| 727 | // bubbling. If you return false, the event will bubble through parent |
| 728 | // views. |
| 729 | // |
| 730 | // If you remove yourself from the tree while processing this, event bubbling |
| 731 | // stops as if you returned true, but you will not receive future events. |
| 732 | // The return value is ignored in this case. |
| 733 | // |
| 734 | // Default implementation returns true if a ContextMenuController has been |
| 735 | // set, false otherwise. Override as needed. |
| 736 | // |
| 737 | virtual bool OnMousePressed(const MouseEvent& event); |
| 738 | |
| 739 | // This method is invoked when the user clicked on this control. |
| 740 | // and is still moving the mouse with a button pressed. |
| 741 | // The provided event is in the receiver's coordinate system. |
| 742 | // |
| 743 | // Return true if you processed the event and want to receive |
| 744 | // subsequent MouseDragged and MouseReleased events. |
| 745 | // |
| 746 | // Default implementation returns true if a ContextMenuController has been |
| 747 | // set, false otherwise. Override as needed. |
| 748 | // |
| 749 | virtual bool OnMouseDragged(const MouseEvent& event); |
| 750 | |
| 751 | // This method is invoked when the user releases the mouse |
| 752 | // button. The event is in the receiver's coordinate system. |
| 753 | // |
| 754 | // If canceled is true it indicates the mouse press/drag was canceled by a |
| 755 | // system/user gesture. |
| 756 | // |
| 757 | // Default implementation notifies the ContextMenuController is appropriate. |
| 758 | // Subclasses that wish to honor the ContextMenuController should invoke |
| 759 | // super. |
| 760 | virtual void OnMouseReleased(const MouseEvent& event, bool canceled); |
| 761 | |
| 762 | // This method is invoked when the mouse is above this control |
| 763 | // The event is in the receiver's coordinate system. |
| 764 | // |
| 765 | // Default implementation does nothing. Override as needed. |
| 766 | virtual void OnMouseMoved(const MouseEvent& e); |
| 767 | |
| 768 | // This method is invoked when the mouse enters this control. |
| 769 | // |
| 770 | // Default implementation does nothing. Override as needed. |
| 771 | virtual void OnMouseEntered(const MouseEvent& event); |
| 772 | |
| 773 | // This method is invoked when the mouse exits this control |
| 774 | // The provided event location is always (0, 0) |
| 775 | // Default implementation does nothing. Override as needed. |
| 776 | virtual void OnMouseExited(const MouseEvent& event); |
| 777 | |
| 778 | // Set the MouseHandler for a drag session. |
| 779 | // |
| 780 | // A drag session is a stream of mouse events starting |
| 781 | // with a MousePressed event, followed by several MouseDragged |
| 782 | // events and finishing with a MouseReleased event. |
| 783 | // |
| 784 | // This method should be only invoked while processing a |
| 785 | // MouseDragged or MouseReleased event. |
| 786 | // |
| 787 | // All further mouse dragged and mouse up events will be sent |
| 788 | // the MouseHandler, even if it is reparented to another window. |
| 789 | // |
| 790 | // The MouseHandler is automatically cleared when the control |
| 791 | // comes back from processing the MouseReleased event. |
| 792 | // |
| 793 | // Note: if the mouse handler is no longer connected to a |
| 794 | // view hierarchy, events won't be sent. |
| 795 | // |
| 796 | virtual void SetMouseHandler(View* new_mouse_handler); |
| 797 | |
| 798 | // Request the keyboard focus. The receiving view will become the |
| 799 | // focused view. |
| 800 | virtual void RequestFocus(); |
| 801 | |
| 802 | // Invoked when a view is about to gain focus |
| 803 | virtual void WillGainFocus(); |
| 804 | |
| 805 | // Invoked when a view just gained focus. |
| 806 | virtual void DidGainFocus(); |
| 807 | |
| 808 | // Invoked when a view is about lose focus |
| 809 | virtual void WillLoseFocus(); |
| 810 | |
| 811 | // Invoked when a view is about to be requested for focus due to the focus |
| 812 | // traversal. Reverse is this request was generated going backward |
| 813 | // (Shift-Tab). |
| 814 | virtual void AboutToRequestFocusFromTabTraversal(bool reverse) { } |
| 815 | |
| 816 | // Invoked when a key is pressed or released. |
| 817 | // Subclasser should return true if the event has been processed and false |
| 818 | // otherwise. If the event has not been processed, the parent will be given a |
| 819 | // chance. |
| 820 | virtual bool OnKeyPressed(const KeyEvent& e); |
| 821 | virtual bool OnKeyReleased(const KeyEvent& e); |
| 822 | |
| 823 | // Whether the view wants to receive Tab and Shift-Tab key events. |
| 824 | // If false, Tab and Shift-Tabs key events are used for focus traversal and |
| 825 | // are not sent to the view. If true, the events are sent to the view and not |
| 826 | // used for focus traversal. |
| 827 | // This implementation returns false (so that by default views handle nicely |
| 828 | // the keyboard focus traversal). |
| 829 | virtual bool CanProcessTabKeyEvents(); |
| 830 | |
| 831 | // Invoked when the user uses the mousewheel. Implementors should return true |
| 832 | // if the event has been processed and false otherwise. This message is sent |
| 833 | // if the view is focused. If the event has not been processed, the parent |
| 834 | // will be given a chance. |
| 835 | virtual bool OnMouseWheel(const MouseWheelEvent& e); |
| 836 | |
| 837 | // Drag and drop functions. |
| 838 | |
| 839 | // Set/get the DragController. See description of DragController for more |
| 840 | // information. |
| 841 | void SetDragController(DragController* drag_controller); |
| 842 | DragController* GetDragController(); |
| 843 | |
| 844 | // During a drag and drop session when the mouse moves the view under the |
| 845 | // mouse is queried to see if it should be a target for the drag and drop |
| 846 | // session. A view indicates it is a valid target by returning true from |
| 847 | // CanDrop. If a view returns true from CanDrop, |
| 848 | // OnDragEntered is sent to the view when the mouse first enters the view, |
| 849 | // as the mouse moves around within the view OnDragUpdated is invoked. |
| 850 | // If the user releases the mouse over the view and OnDragUpdated returns a |
| 851 | // valid drop, then OnPerformDrop is invoked. If the mouse moves outside the |
| 852 | // view or over another view that wants the drag, OnDragExited is invoked. |
| 853 | // |
| 854 | // Similar to mouse events, the deepest view under the mouse is first checked |
| 855 | // if it supports the drop (Drop). If the deepest view under |
| 856 | // the mouse does not support the drop, the ancestors are walked until one |
| 857 | // is found that supports the drop. |
| 858 | |
| 859 | // A view that supports drag and drop must override this and return true if |
| 860 | // data contains a type that may be dropped on this view. |
| 861 | virtual bool CanDrop(const OSExchangeData& data); |
| 862 | |
| 863 | // OnDragEntered is invoked when the mouse enters this view during a drag and |
| 864 | // drop session and CanDrop returns true. This is immediately |
| 865 | // followed by an invocation of OnDragUpdated, and eventually one of |
| 866 | // OnDragExited or OnPerformDrop. |
| 867 | virtual void OnDragEntered(const DropTargetEvent& event); |
| 868 | |
| 869 | // Invoked during a drag and drop session while the mouse is over the view. |
| 870 | // This should return a bitmask of the DragDropTypes::DragOperation supported |
| 871 | // based on the location of the event. Return 0 to indicate the drop should |
| 872 | // not be accepted. |
| 873 | virtual int OnDragUpdated(const DropTargetEvent& event); |
| 874 | |
| 875 | // Invoked during a drag and drop session when the mouse exits the views, or |
| 876 | // when the drag session was canceled and the mouse was over the view. |
| 877 | virtual void OnDragExited(); |
| 878 | |
| 879 | // Invoked during a drag and drop session when OnDragUpdated returns a valid |
| 880 | // operation and the user release the mouse. |
| 881 | virtual int OnPerformDrop(const DropTargetEvent& event); |
| 882 | |
| 883 | // Returns true if the mouse was dragged enough to start a drag operation. |
| 884 | // delta_x and y are the distance the mouse was dragged. |
| 885 | static bool ExceededDragThreshold(int delta_x, int delta_y); |
| 886 | |
| 887 | // This method is the main entry point to process paint for this |
| 888 | // view and its children. This method is called by the painting |
| 889 | // system. You should call this only if you want to draw a sub tree |
| 890 | // inside a custom graphics. |
| 891 | // To customize painting override either the Paint or PaintChildren method, |
| 892 | // not this one. |
| 893 | virtual void ProcessPaint(ChromeCanvas* canvas); |
| 894 | |
| 895 | // Paint the View's child Views, in reverse order. |
| 896 | virtual void PaintChildren(ChromeCanvas* canvas); |
| 897 | |
| 898 | // Sets the ContextMenuController. Setting this to non-null makes the View |
| 899 | // process mouse events. |
| 900 | void SetContextMenuController(ContextMenuController* menu_controller); |
| 901 | ContextMenuController* GetContextMenuController() { |
| 902 | return context_menu_controller_; |
| 903 | } |
| 904 | |
| 905 | // Set the background. The background is owned by the view after this call. |
| 906 | virtual void SetBackground(Background* b); |
| 907 | |
| 908 | // Return the background currently in use or NULL. |
| 909 | virtual const Background* GetBackground() const; |
| 910 | |
| 911 | // Set the border. The border is owned by the view after this call. |
| 912 | virtual void SetBorder(Border* b); |
| 913 | |
| 914 | // Return the border currently in use or NULL. |
| 915 | virtual const Border* GetBorder() const; |
| 916 | |
| 917 | // Returns the insets of the current border. If there is no border an empty |
| 918 | // insets is returned. |
| 919 | gfx::Insets GetInsets() const; |
| 920 | |
| 921 | // Return the cursor that should be used for this view or NULL if |
| 922 | // the default cursor should be used. The provided point is in the |
| 923 | // receiver's coordinate system. |
| 924 | virtual HCURSOR GetCursorForPoint(Event::EventType event_type, int x, int y); |
| 925 | |
| 926 | // Convenience to test whether a point is within this view's bounds |
| 927 | virtual bool HitTest(const CPoint &l) const; |
| 928 | |
| 929 | // Gets the tooltip for this View. If the View does not have a tooltip, |
| 930 | // return false. If the View does have a tooltip, copy the tooltip into |
| 931 | // the supplied string and return true. |
| 932 | // Any time the tooltip text that a View is displaying changes, it must |
| 933 | // invoke TooltipTextChanged. |
| 934 | // The x/y provide the coordinates of the mouse (relative to this view). |
| 935 | virtual bool GetTooltipText(int x, int y, std::wstring* tooltip); |
| 936 | |
| 937 | // Returns the location (relative to this View) for the text on the tooltip |
| 938 | // to display. If false is returned (the default), the tooltip is placed at |
| 939 | // a default position. |
| 940 | virtual bool GetTooltipTextOrigin(int x, int y, CPoint* loc); |
| 941 | |
| 942 | // Set whether this view is owned by its parent. A view that is owned by its |
| 943 | // parent is automatically deleted when the parent is deleted. The default is |
| 944 | // true. Set to false if the view is owned by another object and should not |
| 945 | // be deleted by its parent. |
| 946 | void SetParentOwned(bool f); |
| 947 | |
| 948 | // Return whether a view is owned by its parent. See SetParentOwned() |
| 949 | bool IsParentOwned() const; |
| 950 | |
| 951 | // Return the receiving view's class name. A view class is a string which |
| 952 | // uniquely identifies the view class. It is intended to be used as a way to |
| 953 | // find out during run time if a view can be safely casted to a specific view |
| 954 | // subclass. The default implementation returns kViewClassName. |
| 955 | virtual std::string GetClassName() const; |
| 956 | |
| 957 | // Returns the visible bounds of the receiver in the receivers coordinate |
| 958 | // system. |
| 959 | // |
| 960 | // When traversing the View hierarchy in order to compute the bounds, the |
| 961 | // function takes into account the mirroring setting for each View and |
| 962 | // therefore it will return the mirrored version of the visible bounds if |
| 963 | // need be. |
| 964 | gfx::Rect GetVisibleBounds(); |
| 965 | |
| 966 | // Subclasses that contain traversable children that are not directly |
| 967 | // accessible through the children hierarchy should return the associated |
| 968 | // FocusTraversable for the focus traversal to work properly. |
| 969 | virtual FocusTraversable* GetFocusTraversable() { return NULL; } |
| 970 | |
| 971 | #ifndef NDEBUG |
| 972 | // Debug method that logs the view hierarchy to the output. |
| 973 | void PrintViewHierarchy(); |
| 974 | |
| 975 | // Debug method that logs the focus traversal hierarchy to the output. |
| 976 | void PrintFocusHierarchy(); |
| 977 | #endif |
| 978 | |
| 979 | // The following methods are used by ScrollView to determine the amount |
| 980 | // to scroll relative to the visible bounds of the view. For example, a |
| 981 | // return value of 10 indicates the scrollview should scroll 10 pixels in |
| 982 | // the appropriate direction. |
| 983 | // |
| 984 | // Each method takes the following parameters: |
| 985 | // |
| 986 | // is_horizontal: if true, scrolling is along the horizontal axis, otherwise |
| 987 | // the vertical axis. |
| 988 | // is_positive: if true, scrolling is by a positive amount. Along the |
| 989 | // vertical axis scrolling by a positive amount equates to |
| 990 | // scrolling down. |
| 991 | // |
| 992 | // The return value should always be positive and gives the number of pixels |
| 993 | // to scroll. ScrollView interprets a return value of 0 (or negative) |
| 994 | // to scroll by a default amount. |
| 995 | // |
| 996 | // See VariableRowHeightScrollHelper and FixedRowHeightScrollHelper for |
| 997 | // implementations of common cases. |
| 998 | virtual int GetPageScrollIncrement(ScrollView* scroll_view, |
| 999 | bool is_horizontal, bool is_positive); |
| 1000 | virtual int GetLineScrollIncrement(ScrollView* scroll_view, |
| 1001 | bool is_horizontal, bool is_positive); |
| 1002 | |
| 1003 | protected: |
| 1004 | // This View's bounds in the parent coordinate system. |
| 1005 | CRect bounds_; |
| 1006 | |
| 1007 | // The id of this View. Used to find this View. |
| 1008 | int id_; |
| 1009 | |
| 1010 | // The group of this view. Some view subclasses use this id to find other |
| 1011 | // views of the same group. For example radio button uses this information |
| 1012 | // to find other radio buttons. |
| 1013 | int group_; |
| 1014 | |
| 1015 | #ifndef NDEBUG |
| 1016 | // Returns true if the View is currently processing a paint. |
| 1017 | virtual bool IsProcessingPaint() const; |
| 1018 | #endif |
| 1019 | |
| 1020 | // This method is invoked when the tree changes. |
| 1021 | // |
| 1022 | // When a view is removed, it is invoked for all children and grand |
| 1023 | // children. For each of these views, a notification is sent to the |
| 1024 | // view and all parents. |
| 1025 | // |
| 1026 | // When a view is added, a notification is sent to the view, all its |
| 1027 | // parents, and all its children (and grand children) |
| 1028 | // |
| 1029 | // Default implementation does nothing. Override to perform operations |
| 1030 | // required when a view is added or removed from a view hierarchy |
| 1031 | // |
| 1032 | // parent is the new or old parent. Child is the view being added or |
| 1033 | // removed. |
| 1034 | // |
| 1035 | virtual void ViewHierarchyChanged(bool is_add, View* parent, View* child); |
| 1036 | |
| 1037 | // When SetVisible() changes the visibility of a view, this method is |
| 1038 | // invoked for that view as well as all the children recursively. |
| 1039 | virtual void VisibilityChanged(View* starting_from, bool is_visible); |
| 1040 | |
| 1041 | // Views must invoke this when the tooltip text they are to display changes. |
| 1042 | void TooltipTextChanged(); |
| 1043 | |
| 1044 | // Actual implementation of GetViewForPoint. |
| 1045 | virtual View* GetViewForPoint(const CPoint& point, bool can_create_floating); |
| 1046 | |
| 1047 | // Sets whether this view wants notification when its visible bounds relative |
| 1048 | // to the root view changes. If true, this view is notified any time the |
| 1049 | // origin of one its ancestors changes, or the portion of the bounds not |
| 1050 | // obscured by ancestors changes. The default is false. |
| 1051 | void SetNotifyWhenVisibleBoundsInRootChanges(bool value); |
| 1052 | bool GetNotifyWhenVisibleBoundsInRootChanges(); |
| 1053 | |
| 1054 | // Notification that this views visible bounds, relative to the RootView |
| 1055 | // has changed. The visible bounds corresponds to the region of the |
| 1056 | // view not obscured by other ancestors. |
| 1057 | virtual void VisibleBoundsInRootChanged() {} |
| 1058 | |
| 1059 | // Sets the keyboard focus to this View. The correct way to set the focus is |
| 1060 | // to call RequestFocus() on the view. This method is called when the focus is |
| 1061 | // set and gives an opportunity to subclasses to perform any extra focus steps |
| 1062 | // (for example native component set the native focus on their native |
| 1063 | // component). The default behavior is to set the native focus on the root |
| 1064 | // view container, which is what is appropriate for views that have no native |
| 1065 | // window associated with them (so the root view gets the keyboard messages). |
| 1066 | virtual void Focus(); |
| 1067 | |
| 1068 | // Heavyweight views (views that hold a native control) should return the |
| 1069 | // window for that control. |
| 1070 | virtual HWND GetNativeControlHWND() { return NULL; } |
| 1071 | |
| 1072 | // Invoked when a key is pressed before the key event is processed by the |
| 1073 | // focus manager for accelerators. This gives a chance to the view to |
| 1074 | // override an accelerator. Subclasser should return false if they want to |
| 1075 | // process the key event and not have it translated to an accelerator (if |
| 1076 | // any). In that case, OnKeyPressed will subsequently be invoked for that |
| 1077 | // event. |
| 1078 | virtual bool ShouldLookupAccelerators(const KeyEvent& e) { return true; } |
| 1079 | |
| 1080 | // A convenience method for derived classes which have floating views with IDs |
| 1081 | // that are consecutive numbers in an interval [|low_bound|, |high_bound|[. |
| 1082 | // They can call this method in their EnumerateFloatingViews implementation. |
| 1083 | // If |ascending_order| is true, the first id is |low_bound|, the next after |
| 1084 | // id n is n + 1, and so on. If |ascending_order| is false, the order is |
| 1085 | // reversed, first id is |high_bound|, the next id after id n is n -1... |
| 1086 | static bool EnumerateFloatingViewsForInterval(int low_bound, int high_bound, |
| 1087 | bool ascending_order, |
| 1088 | FloatingViewPosition position, |
| 1089 | int starting_id, |
| 1090 | int* id); |
| 1091 | |
| 1092 | // These are cover methods that invoke the method of the same name on |
| 1093 | // the DragController. Subclasses may wish to override rather than install |
| 1094 | // a DragController. |
| 1095 | // See DragController for a description of these methods. |
| 1096 | virtual int GetDragOperations(int press_x, int press_y); |
| 1097 | virtual void WriteDragData(int press_x, int press_y, OSExchangeData* data); |
| 1098 | |
| 1099 | // Invoked from DoDrag after the drag completes. This implementation does |
| 1100 | // nothing, and is intended for subclasses to do cleanup. |
| 1101 | virtual void OnDragDone(); |
| 1102 | |
| 1103 | // Returns whether we're in the middle of a drag session that was initiated |
| 1104 | // by us. |
| 1105 | bool InDrag(); |
| 1106 | |
| 1107 | // Whether this view is enabled. |
| 1108 | bool enabled_; |
| 1109 | |
| 1110 | // Whether the view can be focused. |
| 1111 | bool focusable_; |
| 1112 | |
| 1113 | private: |
| 1114 | friend class RootView; |
| 1115 | friend class FocusManager; |
| 1116 | friend class ViewStorage; |
| 1117 | |
| 1118 | // Used to track a drag. RootView passes this into |
| 1119 | // ProcessMousePressed/Dragged. |
| 1120 | struct DragInfo { |
| 1121 | // Sets possible_drag to false and start_x/y to 0. This is invoked by |
| 1122 | // RootView prior to invoke ProcessMousePressed. |
| 1123 | void Reset(); |
| 1124 | |
| 1125 | // Sets possible_drag to true and start_x/y to the specified coordinates. |
| 1126 | // This is invoked by the target view if it detects the press may generate |
| 1127 | // a drag. |
| 1128 | void PossibleDrag(int x, int y); |
| 1129 | |
| 1130 | // Whether the press may generate a drag. |
| 1131 | bool possible_drag; |
| 1132 | |
| 1133 | // Coordinates of the mouse press. |
| 1134 | int start_x; |
| 1135 | int start_y; |
| 1136 | }; |
| 1137 | |
| 1138 | // RootView invokes these. These in turn invoke the appropriate OnMouseXXX |
| 1139 | // method. If a drag is detected, DoDrag is invoked. |
| 1140 | bool ProcessMousePressed(const MouseEvent& e, DragInfo* drop_info); |
| 1141 | bool ProcessMouseDragged(const MouseEvent& e, DragInfo* drop_info); |
| 1142 | void ProcessMouseReleased(const MouseEvent& e, bool canceled); |
| 1143 | |
| 1144 | // Starts a drag and drop operation originating from this view. This invokes |
| 1145 | // WriteDragData to write the data and GetDragOperations to determine the |
| 1146 | // supported drag operations. When done, OnDragDone is invoked. |
| 1147 | void DoDrag(const ChromeViews::MouseEvent& e, int press_x, int press_y); |
| 1148 | |
| 1149 | // Adds a child View at the specified position. |floating_view| should be true |
| 1150 | // if the |v| is a floating view. |
| 1151 | void AddChildView(int index, View* v, bool floating_view); |
| 1152 | |
| 1153 | // Removes |view| from the hierarchy tree. If |update_focus_cycle| is true, |
| 1154 | // the next and previous focusable views of views pointing to this view are |
| 1155 | // updated. If |update_tool_tip| is true, the tooltip is updated. If |
| 1156 | // |delete_removed_view| is true, the view is also deleted (if it is parent |
| 1157 | // owned). |
| 1158 | void DoRemoveChildView(View* view, |
| 1159 | bool update_focus_cycle, |
| 1160 | bool update_tool_tip, |
| 1161 | bool delete_removed_view); |
| 1162 | |
| 1163 | // Sets the parent View. This is called automatically by AddChild and is |
| 1164 | // thus private. |
| 1165 | void SetParent(View *parent); |
| 1166 | |
| 1167 | // Call ViewHierarchyChanged for all child views on all parents |
| 1168 | void PropagateRemoveNotifications(View* parent); |
| 1169 | |
| 1170 | // Call ViewHierarchyChanged for all children |
| 1171 | void PropagateAddNotifications(View* parent, View* child); |
| 1172 | |
| 1173 | // Call VisibilityChanged() recursively for all children. |
| 1174 | void PropagateVisibilityNotifications(View* from, bool is_visible); |
| 1175 | |
| 1176 | // Takes care of registering/unregistering accelerators if |
| 1177 | // |register_accelerators| true and calls ViewHierarchyChanged(). |
| 1178 | void ViewHierarchyChangedImpl(bool register_accelerators, |
| 1179 | bool is_add, View *parent, View *child); |
| 1180 | |
| 1181 | // This is the actual implementation for ConvertPointToView() |
| 1182 | // Attempts a parent -> child conversion and then a |
| 1183 | // child -> parent conversion if try_other_direction is true |
| 1184 | static void ConvertPointToView(View* src, |
| 1185 | View *dst, |
| 1186 | gfx::Point* point, |
| 1187 | bool try_other_direction); |
| 1188 | |
| 1189 | // Propagates UpdateTooltip() to the TooltipManager for the ViewContainer. |
| 1190 | // This must be invoked any time the View hierarchy changes in such a way |
| 1191 | // the view under the mouse differs. For example, if the bounds of a View is |
| 1192 | // changed, this is invoked. Similarly, as Views are added/removed, this |
| 1193 | // is invoked. |
| 1194 | void UpdateTooltip(); |
| 1195 | |
| 1196 | // Recursively descends through all descendant views, |
| 1197 | // registering/unregistering all views that want visible bounds in root |
| 1198 | // view notification. |
| 1199 | static void RegisterChildrenForVisibleBoundsNotification(RootView* root, |
| 1200 | View* view); |
| 1201 | static void UnregisterChildrenForVisibleBoundsNotification(RootView* root, |
| 1202 | View* view); |
| 1203 | |
| 1204 | // Adds/removes view to the list of descendants that are notified any time |
| 1205 | // this views location and possibly size are changed. |
| 1206 | void AddDescendantToNotify(View* view); |
| 1207 | void RemoveDescendantToNotify(View* view); |
| 1208 | |
| 1209 | // Initialize the previous/next focusable views of the specified view relative |
| 1210 | // to the view at the specified index. |
| 1211 | void InitFocusSiblings(View* view, int index); |
| 1212 | |
| 1213 | // Actual implementation of PrintFocusHierarchy. |
| 1214 | void PrintViewHierarchyImp(int indent); |
| 1215 | void PrintFocusHierarchyImp(int indent); |
| 1216 | |
| 1217 | // Registers/unregister this view's keyboard accelerators with the |
| 1218 | // FocusManager. |
| 1219 | void RegisterAccelerators(); |
| 1220 | void UnregisterAccelerators(); |
| 1221 | |
| 1222 | // Returns the number of children that are actually attached floating views. |
| 1223 | int GetFloatingViewCount() const; |
| 1224 | |
| 1225 | // Returns the id for this floating view. |
| 1226 | int GetFloatingViewID(); |
| 1227 | |
| 1228 | // Returns whether this view is a floating view. |
| 1229 | bool IsFloatingView(); |
| 1230 | |
| 1231 | // Sets in |path| the path in the view hierarchy from |start| to |end| (the |
| 1232 | // path is the list of indexes in each view's children to get from |start| |
| 1233 | // to |end|). |
| 1234 | // Returns true if |start| and |view| are connected and the |path| has been |
| 1235 | // retrieved succesfully, false otherwise. |
| 1236 | static bool GetViewPath(View* start, View* end, std::vector<int>* path); |
| 1237 | |
| 1238 | // Returns the view at the end of the specified |path|, starting at the |
| 1239 | // |start| view. |
| 1240 | static View* GetViewForPath(View* start, const std::vector<int>& path); |
| 1241 | |
| 1242 | // This view's parent |
| 1243 | View *parent_; |
| 1244 | |
| 1245 | // This view's children. |
| 1246 | typedef std::vector<View*> ViewList; |
| 1247 | ViewList child_views_; |
| 1248 | |
| 1249 | // List of floating children. A floating view is always referenced by |
| 1250 | // child_views_ and will be deleted on destruction just like any other |
| 1251 | // child view. |
| 1252 | ViewList floating_views_; |
| 1253 | |
| 1254 | // Maps a floating view to its floating view id. |
| 1255 | std::map<View*, int> floating_views_ids_; |
| 1256 | |
| 1257 | // Whether we want the focus to be restored. This is used to store/restore |
| 1258 | // focus for floating views. |
| 1259 | bool should_restore_focus_; |
| 1260 | |
| 1261 | // The View's LayoutManager defines the sizing heuristics applied to child |
| 1262 | // Views. The default is absolute positioning according to bounds_. |
| 1263 | scoped_ptr<LayoutManager> layout_manager_; |
| 1264 | |
| 1265 | // Visible state |
| 1266 | bool is_visible_; |
| 1267 | |
| 1268 | // Background |
| 1269 | Background* background_; |
| 1270 | |
| 1271 | // Border. |
| 1272 | Border* border_; |
| 1273 | |
| 1274 | // Whether this view is owned by its parent. |
| 1275 | bool is_parent_owned_; |
| 1276 | |
| 1277 | // See SetNotifyWhenVisibleBoundsInRootChanges. |
| 1278 | bool notify_when_visible_bounds_in_root_changes_; |
| 1279 | |
| 1280 | // Whether or not RegisterViewForVisibleBoundsNotification on the RootView |
| 1281 | // has been invoked. |
| 1282 | bool registered_for_visible_bounds_notification_; |
| 1283 | |
| 1284 | // List of descendants wanting notification when their visible bounds change. |
| 1285 | scoped_ptr<ViewList> descendants_to_notify_; |
| 1286 | |
| 1287 | // Next view to be focused when the Tab key is pressed. |
| 1288 | View* next_focusable_view_; |
| 1289 | |
| 1290 | // Next view to be focused when the Shift-Tab key combination is pressed. |
| 1291 | View* previous_focusable_view_; |
| 1292 | |
| 1293 | // The list of accelerators. |
| 1294 | scoped_ptr<std::vector<Accelerator>> accelerators_; |
| 1295 | |
| 1296 | // The task used to restore automatically the focus to the last focused |
| 1297 | // floating view. |
| 1298 | RestoreFocusTask* restore_focus_view_task_; |
| 1299 | |
| 1300 | // The menu controller. |
| 1301 | ContextMenuController* context_menu_controller_; |
| 1302 | |
| 1303 | // The accessibility implementation for this View. |
| 1304 | scoped_ptr<AccessibleWrapper> accessibility_; |
| 1305 | |
| 1306 | DragController* drag_controller_; |
| 1307 | |
| 1308 | // Indicates whether or not the view is going to be mirrored (that is, use a |
| 1309 | // right-to-left UI layout) if the locale's language is a right-to-left |
| 1310 | // language like Arabic or Hebrew. |
| 1311 | bool ui_mirroring_is_enabled_for_rtl_languages_; |
| 1312 | |
| 1313 | // Indicates whether or not the ChromeCanvas object passed to View::Paint() |
| 1314 | // is going to be flipped horizontally (using the appropriate transform) on |
| 1315 | // right-to-left locales for this View. |
| 1316 | bool flip_canvas_on_paint_for_rtl_ui_; |
| 1317 | |
| 1318 | DISALLOW_EVIL_CONSTRUCTORS(View); |
| 1319 | }; |
| 1320 | |
| 1321 | } |
| 1322 | |
| 1323 | #endif // CHROME_VIEWS_VIEW_H__ |