 Chromium Code Reviews
 Chromium Code Reviews| OLD | NEW | 
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef UI_VIEWS_VIEW_H_ | |
| 6 #define UI_VIEWS_VIEW_H_ | |
| 7 | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "base/scoped_ptr.h" | |
| 12 #include "gfx/rect.h" | |
| 13 #include "ui/views/events/event.h" | |
| 14 | |
| 15 namespace gfx { | |
| 16 class Canvas; | |
| 17 class Point; | |
| 18 class Rect; | |
| 19 class Size; | |
| 20 } | |
| 21 | |
| 22 namespace ui { | |
| 23 namespace internal { | |
| 24 class RootView; | |
| 25 class ViewLayer; | |
| 
sky
2011/02/01 18:56:22
Nuke ViewLayer.
 | |
| 26 } | |
| 27 class Accelerator; | |
| 28 class Border; | |
| 29 class ContextMenuController; | |
| 30 class DragController; | |
| 31 class FocusManager; | |
| 32 class FocusTraversable; | |
| 33 class LayoutManager; | |
| 34 class ThemeProvider; | |
| 35 class Widget; | |
| 36 | |
| 37 //////////////////////////////////////////////////////////////////////////////// | |
| 38 // View class | |
| 39 // | |
| 40 // View encapsulates rendering, layout and event handling for rectangles within | |
| 41 // a view hierarchy. | |
| 42 // | |
| 43 // Client code typically subclasses View and overrides virtual methods to | |
| 44 // handle painting, child view positioning and handle certain types of events. | |
| 45 // | |
| 46 // Views are owned by their parent View unless specified otherwise. This means | |
| 47 // that in most cases Views are automatically destroyed when the window that | |
| 48 // contains them is destroyed. | |
| 49 // | |
| 50 // TODO(beng): consider the visibility of many of these methods. | |
| 51 // consider making RootView a friend and making many private or | |
| 52 // protected. | |
| 53 class View { | |
| 54 public: | |
| 55 typedef std::vector<View*> ViewVector; | |
| 56 | |
| 57 // Creation and lifetime ----------------------------------------------------- | |
| 58 View(); | |
| 59 virtual ~View(); | |
| 60 | |
| 61 // By default a View is owned by its parent unless specified otherwise here. | |
| 62 bool parent_owned() const { return parent_owned_; } | |
| 63 void set_parent_owned(bool parent_owned) { parent_owned_ = parent_owned; } | |
| 64 | |
| 65 void set_drag_controller(DragController* drag_controller) { | |
| 66 drag_controller_ = drag_controller; | |
| 67 } | |
| 68 | |
| 69 // Size and disposition ------------------------------------------------------ | |
| 70 | |
| 71 void SetBounds(int x, int y, int width, int height); | |
| 72 void SetBoundsRect(const gfx::Rect& bounds); | |
| 73 void SetSize(const gfx::Size& size); | |
| 74 void SetPosition(const gfx::Point& position); | |
| 75 gfx::Rect bounds() const { return bounds_; } | |
| 76 gfx::Rect GetVisibleBounds() const; | |
| 77 int x() const { return bounds_.x(); } | |
| 78 int y() const { return bounds_.y(); } | |
| 79 int width() const { return bounds_.width(); } | |
| 80 int height() const { return bounds_.height(); } | |
| 81 | |
| 82 // Owned by the view. | |
| 83 void SetBorder(Border* border); | |
| 84 Border* border() { return border_.get(); } | |
| 85 | |
| 86 // Returns the bounds of the content area of the view, i.e. the rectangle | |
| 87 // enclosed by the view's border. | |
| 88 gfx::Rect GetContentsBounds() const; | |
| 89 | |
| 90 virtual gfx::Size GetPreferredSize() const; | |
| 91 virtual gfx::Size GetMinimumSize() const; | |
| 92 | |
| 93 void SetLayoutManager(LayoutManager* layout_manager); | |
| 94 virtual void Layout(); | |
| 95 | |
| 96 // If a View is not visible, it will not be rendered, focused, etc. | |
| 97 bool visible() const { return visible_; } | |
| 98 void SetVisible(bool visible); | |
| 99 | |
| 100 // Disabled Views will not receive mouse press/release events, nor can they be | |
| 101 // focused. | |
| 102 bool enabled() const { return enabled_; } | |
| 103 void SetEnabled(bool enabled); | |
| 104 | |
| 105 virtual void OnBoundsChanged(); | |
| 106 | |
| 107 // Coordinate conversion ----------------------------------------------------- | |
| 108 | |
| 109 // Converts a point from the coordinate system of |source| to |target|. | |
| 110 static void ConvertPointToView(View* source, View* target, gfx::Point* point); | |
| 111 | |
| 112 // Converts a point from the coordinate system of |source| to the screen. | |
| 113 // If |source| is not attached to a Widget that is in screen space, |point| is | |
| 114 // not modified. | |
| 115 static void ConvertPointToScreen(View* source, gfx::Point* point); | |
| 116 | |
| 117 // Converts a point from the coordinate system of |source| to the Widget that | |
| 118 // most closely contains it. | |
| 119 static void ConvertPointToWidget(View* source, gfx::Point* point); | |
| 120 | |
| 121 // Tree operations ----------------------------------------------------------- | |
| 122 | |
| 123 // Returns the Widget that contains this View, or NULL if it is not contained | |
| 124 // within a Widget. | |
| 125 virtual Widget* GetWidget() const; | |
| 126 | |
| 127 // Adds a View as a child of this one, optionally at |index|. | |
| 128 void AddChildView(View* view); | |
| 129 void AddChildViewAt(View* view, size_t index); | |
| 130 | |
| 131 // Removes a View as a child of this View. Does not delete the child. | |
| 132 View* RemoveChildView(View* view); | |
| 133 | |
| 134 // Removes all View children of this View. Deletes the children if | |
| 135 // |delete_children| is true. | |
| 136 void RemoveAllChildViews(bool delete_children); | |
| 137 | |
| 138 // Returns the View at the specified |index|. | |
| 139 View* GetChildViewAt(size_t index); | |
| 140 | |
| 141 // Returns the number of child views. | |
| 142 size_t child_count() const { return children_.size(); } | |
| 143 | |
| 144 // Returns the parent View, or NULL if this View has no parent. | |
| 145 View* parent() const { return parent_; } | |
| 146 | |
| 147 // Returns true if |child| is contained within this View's hierarchy, even as | |
| 148 // an indirect descendant. Will return true if child is also this View. | |
| 149 bool Contains(View* child); | |
| 150 | |
| 151 // Returns the visible View that most closely contains the specified point. | |
| 152 // |point| is in this View's coordinates. | |
| 153 // This function is used by the event processing system in the Widget to | |
| 154 // locate views for event targeting. Override this function if you wish to | |
| 155 // specify a view other than the one most closely enclosing |point| to receive | |
| 156 // notifications for events within it. | |
| 157 // TODO(beng): This is [ab]used primarily for event handling. Should be | |
| 158 // renamed to something like GetViewForEvent(). | |
| 159 virtual View* GetViewForPoint(const gfx::Point& point) const; | |
| 160 | |
| 161 // Returns true if the specified point is contained within this View or its | |
| 162 // hit test mask. |point| is in this View's coordinates. | |
| 163 bool HitTest(const gfx::Point& point) const; | |
| 164 | |
| 165 int id() const { return id_; } | |
| 166 void set_id(int id) { id_ = id; } | |
| 167 int group() const { return group_; } | |
| 168 void set_group(int group) { group_ = group; } | |
| 169 | |
| 170 // Returns the View within this View's hierarchy whose id matches that | |
| 171 // specified. | |
| 172 View* GetViewById(int id) const; | |
| 173 | |
| 174 // Populates a ViewVector with the Views within this View's hierarchy that | |
| 175 // match the specified group id. | |
| 176 void GetViewsWithGroup(int group, ViewVector* vec) const; | |
| 177 | |
| 178 // Called on every view in the hierarchy when a view is added or removed. | |
| 179 virtual void OnViewAdded(View* parent, View* child); | |
| 180 virtual void OnViewRemoved(View* parent, View* child); | |
| 181 | |
| 182 // Called on a View when it is added or removed from a Widget. | |
| 183 virtual void OnViewAddedToWidget(); | |
| 184 virtual void OnViewRemovedFromWidget(); | |
| 185 | |
| 186 // Accelerators -------------------------------------------------------------- | |
| 187 | |
| 188 // Accelerator Registration. | |
| 189 void AddAccelerator(const Accelerator& accelerator); | |
| 190 void RemoveAccelerator(const Accelerator& accelerator); | |
| 191 void RemoveAllAccelerators(); | |
| 192 | |
| 193 virtual bool OnAcceleratorPressed(const Accelerator& accelerator); | |
| 194 | |
| 195 // Focus --------------------------------------------------------------------- | |
| 196 | |
| 197 // Manager. | |
| 198 FocusManager* GetFocusManager() const; | |
| 199 | |
| 200 // Traversal. | |
| 201 virtual FocusTraversable* GetFocusTraversable() const; | |
| 202 View* GetNextFocusableView() const; | |
| 203 View* GetPreviousFocusableView() const; | |
| 204 | |
| 205 // Attributes. | |
| 206 virtual bool SkipDefaultKeyEventProcessing(const KeyEvent& event) const; | |
| 207 void set_focusable(bool focusable) { focusable_ = focusable; } | |
| 208 bool IsFocusable() const; | |
| 209 | |
| 210 bool HasFocus() const; | |
| 211 void RequestFocus(); | |
| 212 | |
| 213 virtual void OnFocus(/* const FocusEvent& event */); | |
| 214 virtual void OnBlur(); | |
| 215 | |
| 216 // Input --------------------------------------------------------------------- | |
| 217 | |
| 218 virtual bool OnKeyPressed(const KeyEvent& event); | |
| 219 virtual bool OnKeyReleased(const KeyEvent& event); | |
| 220 virtual bool OnMouseWheel(const MouseWheelEvent& event); | |
| 221 // To receive OnMouseDragged() or OnMouseReleased() events, overriding classes | |
| 222 // must return true from this function. | |
| 223 virtual bool OnMousePressed(const MouseEvent& event); | |
| 224 virtual bool OnMouseDragged(const MouseEvent& event); | |
| 225 virtual void OnMouseReleased(const MouseEvent& event); | |
| 226 virtual void OnMouseCaptureLost(); | |
| 227 virtual void OnMouseMoved(const MouseEvent& event); | |
| 228 virtual void OnMouseEntered(const MouseEvent& event); | |
| 229 virtual void OnMouseExited(const MouseEvent& event); | |
| 230 | |
| 231 virtual gfx::NativeCursor GetCursorForPoint(const gfx::Point& point); | |
| 232 | |
| 233 // Painting ------------------------------------------------------------------ | |
| 234 | |
| 235 void Invalidate(); | |
| 236 virtual void InvalidateRect(const gfx::Rect& invalid_rect); | |
| 237 virtual void Paint(gfx::Canvas* canvas); | |
| 238 virtual void PaintChildren(gfx::Canvas* canvas); | |
| 239 | |
| 240 virtual void OnPaint(gfx::Canvas* canvas); | |
| 
sky
2011/02/01 18:56:22
I think it's worth documenting these so that folks
 | |
| 241 virtual void OnPaintBackground(gfx::Canvas* canvas); | |
| 242 virtual void OnPaintBorder(gfx::Canvas* canvas); | |
| 243 virtual void OnPaintFocusBorder(gfx::Canvas* canvas); | |
| 244 | |
| 245 // Context menus ------------------------------------------------------------- | |
| 246 | |
| 247 void set_context_menu_controller( | |
| 248 ContextMenuController* context_menu_controller) { | |
| 
sky
2011/02/01 18:56:22
indent this two more spaces and bring in line 249
 | |
| 249 context_menu_controller_ = context_menu_controller; | |
| 250 } | |
| 251 | |
| 252 // Resources ----------------------------------------------------------------- | |
| 253 | |
| 254 ThemeProvider* GetThemeProvider() const; | |
| 255 | |
| 256 private: | |
| 257 friend internal::RootView; | |
| 258 | |
| 259 // State collected during a MousePressed event to detect possible drag | |
| 260 // operations. | |
| 261 struct DragInfo { | |
| 262 // Sets possible_drag to false and start_x/y to 0. This is invoked by | |
| 263 // RootView prior to invoke MousePressed(). | |
| 264 void Reset(); | |
| 265 | |
| 266 // Sets possible_drag to true and start_pt to the specified point. | |
| 267 // This is invoked by the target view if it detects the press may generate | |
| 268 // a drag. | |
| 269 void PossibleDrag(const gfx::Point& point); | |
| 270 | |
| 271 // Whether the press may generate a drag. | |
| 272 bool possible_drag; | |
| 273 | |
| 274 // Position of the mouse press in screen coordinates. | |
| 275 gfx::Point press_point; | |
| 276 }; | |
| 277 | |
| 278 // Drag & Drop --------------------------------------------------------------- | |
| 279 int GetDragOperations(const gfx::Point& point); | |
| 280 void WriteDragData(const gfx::Point& point, OSExchangeData* data); | |
| 281 void StartShellDrag(const MouseEvent& event, const gfx::Point& press_point); | |
| 282 | |
| 283 // RootView API -------------------------------------------------------------- | |
| 284 // These methods are designed to be called by the RootView. The RootView | |
| 285 // should limit its interaction with the View to these methods and the public | |
| 286 // API. | |
| 287 bool MousePressed(const MouseEvent& event, DragInfo* drag_info); | |
| 288 bool MouseDragged(const MouseEvent& event, DragInfo* drag_info); | |
| 289 void MouseReleased(const MouseEvent& event); | |
| 290 | |
| 291 // Tree operations ----------------------------------------------------------- | |
| 292 void NotifyHierarchyChanged(View* parent, View* child, bool is_add); | |
| 293 void NotifyHierarchyChangedUp(View* parent, View* child, bool is_add); | |
| 294 void NotifyHierarchyChangedDown(View* parent, View* child, bool is_add, | |
| 295 bool has_widget); | |
| 296 void CallViewNotification(View* target, | |
| 297 View* parent, | |
| 298 View* child, | |
| 299 bool is_add, | |
| 300 bool has_widget); | |
| 301 | |
| 302 // The View's parent view. This is set and reset when the View is added and | |
| 303 // removed from a hierarchy. | |
| 304 View* parent_; | |
| 305 | |
| 306 // The View's children. | |
| 307 ViewVector children_; | |
| 308 | |
| 309 // True if the hierarchy (i.e. the parent View) is responsible for deleting | |
| 310 // this View. Default is true. | |
| 311 bool parent_owned_; | |
| 312 | |
| 313 // The bounds of the View, in its parent's coordinates. | |
| 314 gfx::Rect bounds_; | |
| 315 | |
| 316 scoped_ptr<Border> border_; | |
| 317 | |
| 318 // Whether or not this View is visible. The view still participates in layout | |
| 319 // but will not be painted. | |
| 320 bool visible_; | |
| 321 | |
| 322 // Whether or not this View is enabled. When disabled, the event system will | |
| 323 // not propagate un-handled events beyond the View in the hierarchy. | |
| 324 bool enabled_; | |
| 325 | |
| 326 // An identifier for this View. Caller must guarantee uniqueness. | |
| 327 int id_; | |
| 328 | |
| 329 // An identifier for a group of potentially related Views. | |
| 330 int group_; | |
| 331 | |
| 332 // True if this View is focusable by the FocusManager. | |
| 333 bool focusable_; | |
| 334 | |
| 335 // An optional helper that handles layout for child views. | |
| 336 scoped_ptr<LayoutManager> layout_manager_; | |
| 337 | |
| 338 // Shows the context menu. | |
| 339 ContextMenuController* context_menu_controller_; | |
| 340 | |
| 341 // Delegate for drag and drop related functionality. | |
| 342 DragController* drag_controller_; | |
| 343 | |
| 344 DISALLOW_COPY_AND_ASSIGN(View); | |
| 345 }; | |
| 346 | |
| 347 } // namespace ui | |
| 348 | |
| 349 #endif // UI_VIEWS_VIEW_H_ | |
| 350 | |
| 351 /* | |
| 352 | |
| 353 TODO(beng): | |
| 354 - focus | |
| 355 - accessibility | |
| 356 - scrolling | |
| 357 - cursors | |
| 358 - tooltips | |
| 359 - rtl | |
| 360 - l10n | |
| 361 - mousewheel | |
| 362 - more on painting | |
| 363 - layer stuff | |
| 364 - investigate why assorted notifications are necessary | |
| 365 - native_widget_views | |
| 366 - native_widget_gtk | |
| 367 - pick a name | |
| 368 | |
| 369 */ | |
| OLD | NEW |