OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/view.h" | 5 #include "ui/views/view.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
9 | 9 |
10 #include "ui/base/dragdrop/drag_drop_types.h" | 10 #include "ui/base/dragdrop/drag_drop_types.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 parent_(NULL), | 57 parent_(NULL), |
58 focusable_(false), | 58 focusable_(false), |
59 next_focusable_view_(NULL), | 59 next_focusable_view_(NULL), |
60 prev_focusable_view_(NULL), | 60 prev_focusable_view_(NULL), |
61 context_menu_controller_(NULL), | 61 context_menu_controller_(NULL), |
62 drag_controller_(NULL) { | 62 drag_controller_(NULL) { |
63 } | 63 } |
64 | 64 |
65 View::~View() { | 65 View::~View() { |
66 if (parent_) | 66 if (parent_) |
67 parent_->RemoveChildView(this); | 67 parent_->RemoveChildView(this, false); |
68 | 68 |
69 ViewVector::const_iterator it = children_.begin(); | 69 for (Views::const_iterator i(children_begin()); i != children_.end(); ++i) { |
70 for (; it != children_.end(); ++it) { | 70 (*i)->parent_ = NULL; |
71 (*it)->parent_ = NULL; | 71 if ((*i)->parent_owned()) |
72 if ((*it)->parent_owned()) | 72 delete *i; |
73 delete *it; | |
74 } | 73 } |
75 } | 74 } |
76 | 75 |
77 // Size and disposition -------------------------------------------------------- | 76 // Size and disposition -------------------------------------------------------- |
78 | 77 |
79 void View::SetBounds(int x, int y, int width, int height) { | 78 gfx::Rect View::GetVisibleBounds() const { |
80 SetBoundsRect(gfx::Rect(x, y, std::max(0, width), std::max(0, height))); | 79 // TODO(beng): |
| 80 return bounds(); |
81 } | 81 } |
82 | 82 |
83 void View::SetBoundsRect(const gfx::Rect& bounds) { | 83 gfx::Rect View::GetContentsBounds() const { |
| 84 gfx::Rect rect(gfx::Point(), size()); |
| 85 if (border_.get()) |
| 86 rect.Inset(border_->insets()); |
| 87 return rect; |
| 88 } |
| 89 |
| 90 void View::SetBounds(const gfx::Rect& bounds) { |
84 gfx::Rect old_bounds = bounds_; | 91 gfx::Rect old_bounds = bounds_; |
85 bounds_ = bounds; | 92 bounds_ = bounds; |
86 // TODO(beng): investigate usage of needs_layout_ in old View code. | 93 // TODO(beng): investigate usage of needs_layout_ in old View code. |
87 if (old_bounds != bounds_) { | 94 if (old_bounds != bounds_) { |
88 OnBoundsChanged(); | 95 OnBoundsChanged(); |
89 Layout(); | 96 Layout(); |
90 } | 97 } |
91 } | 98 } |
92 | 99 |
93 gfx::Rect View::GetVisibleBounds() const { | 100 void View::SetOrigin(const gfx::Point& origin) { |
94 // TODO(beng): | 101 SetBounds(gfx::Rect(origin, size())); |
95 return bounds(); | |
96 } | 102 } |
97 | 103 |
98 void View::SetSize(const gfx::Size& size) { | 104 void View::SetSize(const gfx::Size& size) { |
99 SetBounds(x(), y(), size.width(), size.height()); | 105 SetBounds(gfx::Rect(origin(), size)); |
100 } | |
101 | |
102 void View::SetPosition(const gfx::Point& position) { | |
103 SetBounds(position.x(), position.y(), width(), height()); | |
104 } | 106 } |
105 | 107 |
106 void View::SetBorder(Border* border) { | 108 void View::SetBorder(Border* border) { |
107 border_.reset(border); | 109 border_.reset(border); |
108 } | 110 } |
109 | 111 |
110 gfx::Rect View::GetContentsBounds() const { | |
111 if (border_.get()) { | |
112 return gfx::Rect( | |
113 border_->insets().left(), border_->insets().top(), | |
114 width() - border_->insets().right() - border_->insets().left(), | |
115 height() - border_->insets().bottom() - border_->insets().top()); | |
116 } | |
117 return gfx::Rect(0, 0, width(), height()); | |
118 } | |
119 | |
120 void View::OnBoundsChanged() { | 112 void View::OnBoundsChanged() { |
121 } | 113 } |
122 | 114 |
123 gfx::Size View::GetPreferredSize() const { | 115 gfx::Size View::GetPreferredSize() const { |
124 return gfx::Size(); | 116 return gfx::Size(); |
125 } | 117 } |
126 | 118 |
127 gfx::Size View::GetMinimumSize() const { | 119 gfx::Size View::GetMinimumSize() const { |
128 return GetPreferredSize(); | 120 return GetPreferredSize(); |
129 } | 121 } |
130 | 122 |
131 void View::SetLayoutManager(LayoutManager* layout_manager) { | 123 void View::SetLayoutManager(LayoutManager* layout_manager) { |
132 layout_manager_.reset(layout_manager); | 124 layout_manager_.reset(layout_manager); |
133 } | 125 } |
134 | 126 |
135 void View::Layout() { | 127 void View::Layout() { |
| 128 // The layout manager handles all child layout if present. |
136 if (layout_manager_.get()) { | 129 if (layout_manager_.get()) { |
137 // Layout Manager handles laying out children. | |
138 layout_manager_->Layout(this); | 130 layout_manager_->Layout(this); |
139 } else { | 131 } else { |
140 // We handle laying out our own children. | 132 std::for_each(children_begin(), children_end(), |
141 ViewVector::iterator it = children_.begin(); | 133 std::mem_fun(&View::Layout)); |
142 for (; it != children_.end(); ++it) | |
143 (*it)->Layout(); | |
144 } | 134 } |
145 // TODO(beng): needs_layout_? SchedulePaint()? | 135 // TODO(beng): needs_layout_? SchedulePaint()? |
146 } | 136 } |
147 | 137 |
148 void View::SetVisible(bool visible) { | 138 void View::SetVisible(bool visible) { |
149 if (visible != visible_) { | 139 if (visible != visible_) { |
150 visible_ = visible; | 140 visible_ = visible; |
151 | |
152 // InvaldateRect() checks for view visibility before proceeding, so we need | 141 // InvaldateRect() checks for view visibility before proceeding, so we need |
153 // to ask the parent to invalidate our bounds. | 142 // to ask the parent to invalidate our bounds. |
154 if (parent_) | 143 if (parent_) |
155 parent_->InvalidateRect(bounds_); | 144 parent_->InvalidateRect(bounds_); |
156 } | 145 } |
157 } | 146 } |
158 | 147 |
159 void View::SetEnabled(bool enabled) { | 148 void View::SetEnabled(bool enabled) { |
160 if (enabled != enabled_) { | 149 if (enabled != enabled_) { |
161 enabled_ = enabled; | 150 enabled_ = enabled; |
162 Invalidate(); | 151 Invalidate(); |
163 } | 152 } |
164 } | 153 } |
165 | 154 |
166 // Attributes ------------------------------------------------------------------ | 155 // Attributes ------------------------------------------------------------------ |
167 | 156 |
168 View* View::GetViewById(int id) const { | 157 View* View::GetViewByID(int id) { |
169 if (id_ == id) | 158 if (id_ == id) |
170 return const_cast<View*>(this); | 159 return this; |
171 ViewVector::const_iterator it = children_.begin(); | 160 for (Views::const_iterator i(children_begin()); i != children_end(); ++i) { |
172 for (; it != children_.end(); ++it) { | 161 View* view = (*i)->GetViewByID(id); |
173 View* view = (*it)->GetViewById(id); | |
174 if (view) | 162 if (view) |
175 return view; | 163 return view; |
176 } | 164 } |
177 return NULL; | 165 return NULL; |
178 } | 166 } |
179 | 167 |
180 void View::GetViewsWithGroup(int group, ViewVector* vec) const { | 168 void View::GetViewsInGroup(int group, Views* vec) { |
181 if (group_ == group) | 169 if (group_ == group) |
182 vec->push_back(const_cast<View*>(this)); | 170 vec->push_back(const_cast<View*>(this)); |
183 ViewVector::const_iterator it = children_.begin(); | 171 for (Views::const_iterator i(children_begin()); i != children_end(); ++i) |
184 for (; it != children_.end(); ++it) | 172 (*i)->GetViewsInGroup(group, vec); |
185 (*it)->GetViewsWithGroup(group, vec); | |
186 } | 173 } |
187 | 174 |
188 View* View::GetSelectedViewForGroup(int group_id) { | 175 View* View::GetSelectedViewForGroup(int group_id) { |
189 // TODO(beng): implementme | 176 // TODO(beng): implementme |
190 return NULL; | 177 return NULL; |
191 } | 178 } |
192 | 179 |
193 // Coordinate conversion ------------------------------------------------------- | 180 // Coordinate conversion ------------------------------------------------------- |
194 | 181 |
195 // static | 182 // static |
196 void View::ConvertPointToView(View* source, View* target, gfx::Point* point) { | 183 void View::ConvertPointToView(const View& source, |
197 View* inner = NULL; | 184 const View& target, |
198 View* outer = NULL; | 185 gfx::Point* point) { |
199 if (source->Contains(target)) { | 186 const View* inner = NULL; |
200 inner = target; | 187 const View* outer = NULL; |
201 outer = source; | 188 if (source.Contains(target)) { |
202 } else if (target->Contains(source)) { | 189 inner = ⌖ |
203 inner = source; | 190 outer = &source; |
204 outer = target; | 191 } else if (target.Contains(source)) { |
205 } // Note that we cannot do a plain "else" here since |source| and |target| | 192 inner = &source; |
206 // may be in different hierarchies with no relation. | 193 outer = ⌖ |
| 194 } // Note that we cannot do a plain "else" here since |source| and |target| |
| 195 // may be in different hierarchies with no relation. |
| 196 if (!inner) |
| 197 return; |
207 | 198 |
208 if (inner && outer) { | 199 gfx::Point offset; |
209 gfx::Point offset; | 200 for (const View* v = inner; v != outer; v = v->parent()) |
210 View* temp = inner; | 201 offset.Offset(v->x(), v->y()); |
211 while (temp != outer) { | 202 // When target is contained by source, we need to subtract the offset. |
212 offset.Offset(temp->x(), temp->y()); | 203 // When source is contained by target, we need to add the offset. |
213 temp = temp->parent(); | 204 int multiplier = (inner == &target) ? -1 : 1; |
214 } | 205 point->Offset(multiplier * offset.x(), multiplier * offset.y()); |
215 // When target is contained by source, we need to subtract the offset. | 206 } |
216 // When source is contained by target, we need to add the fofset. | 207 |
217 int multiplier = inner == target ? -1 : 1; | 208 // static |
218 point->Offset(multiplier * offset.x(), multiplier * offset.y()); | 209 void View::ConvertPointToScreen(const View& source, gfx::Point* point) { |
| 210 const Widget* widget = source.GetWidget(); |
| 211 if (widget) { |
| 212 ConvertPointToWidget(source, point); |
| 213 gfx::Point client_origin(widget->GetClientAreaScreenBounds().origin()); |
| 214 point->Offset(client_origin.x(), client_origin.y()); |
219 } | 215 } |
220 } | 216 } |
221 | 217 |
222 // static | 218 // static |
223 void View::ConvertPointToScreen(View* source, gfx::Point* point) { | 219 void View::ConvertPointToWidget(const View& source, gfx::Point* point) { |
224 Widget* widget = source->GetWidget(); | 220 for (const View* v = &source; v; v = v->parent()) |
225 if (widget) { | |
226 ConvertPointToWidget(source, point); | |
227 gfx::Rect r = widget->GetClientAreaScreenBounds(); | |
228 point->Offset(r.x(), r.y()); | |
229 } | |
230 } | |
231 | |
232 // static | |
233 void View::ConvertPointToWidget(View* source, gfx::Point* point) { | |
234 for (View* v = source; v; v = v->parent()) | |
235 point->Offset(v->x(), v->y()); | 221 point->Offset(v->x(), v->y()); |
236 } | 222 } |
237 | 223 |
238 // Tree operations ------------------------------------------------------------- | 224 // Tree operations ------------------------------------------------------------- |
239 | 225 |
240 Widget* View::GetWidget() const { | 226 const Widget* View::GetWidget() const { |
241 return parent_ ? parent_->GetWidget() : NULL; | 227 return parent() ? parent()->GetWidget() : NULL; |
242 } | 228 } |
243 | 229 |
244 void View::AddChildView(View* view) { | 230 void View::AddChildView(View* view) { |
245 AddChildViewAt(view, children_.size()); | 231 AddChildViewAt(view, children_size()); |
246 } | 232 } |
247 | 233 |
248 void View::AddChildViewAt(View* view, size_t index) { | 234 void View::AddChildViewAt(View* view, size_t index) { |
249 CHECK(view != this) << "A view cannot be its own child."; | 235 CHECK_NE(this, view) << "A view cannot be its own child."; |
250 | 236 |
251 // Remove the child from its current parent if any. | 237 // Remove the child from its current parent if any. |
252 if (view->parent()) | 238 if (view->parent()) |
253 view->parent()->RemoveChildView(view); | 239 view->parent()->RemoveChildView(view, false); |
254 | 240 |
255 // TODO(beng): Move focus initialization to FocusManager. | 241 // TODO(beng): Move focus initialization to FocusManager. |
256 InitFocusSiblings(view, index); | 242 InitFocusSiblings(view, index); |
257 | 243 |
258 children_.insert(children_.begin() + index, view); | 244 children_.insert(children_.begin() + index, view); |
259 view->parent_ = this; | 245 view->parent_ = this; |
260 | 246 |
261 // Notify the hierarchy. | 247 // Notify the hierarchy. |
262 NotifyHierarchyChanged(this, view, true); | 248 NotifyHierarchyChanged(view, true); |
263 | 249 |
264 // TODO(beng): Notify other objects like tooltip, layout manager, etc. | 250 // TODO(beng): Notify other objects like tooltip, layout manager, etc. |
265 // Figure out RegisterChildrenForVisibleBoundsNotification. | 251 // Figure out RegisterChildrenForVisibleBoundsNotification. |
266 } | 252 } |
267 | 253 |
268 View* View::RemoveChildView(View* view) { | 254 void View::RemoveChildView(View* view, bool delete_child) { |
269 ViewVector::iterator it = find(children_.begin(), children_.end(), view); | 255 Views::const_iterator i(std::find(children_begin(), children_end(), view)); |
270 if (it != children_.end()) { | 256 DCHECK(i != children_end()); |
271 View* old_parent = view->parent_; | 257 view->parent_ = NULL; |
272 view->parent_ = NULL; | 258 children_.erase(i); |
273 children_.erase(it); | 259 NotifyHierarchyChanged(view, false); |
274 NotifyHierarchyChanged(old_parent, view, false); | |
275 } | |
276 | |
277 // TODO(beng): Notify other objects like tooltip, layout manager, etc. | 260 // TODO(beng): Notify other objects like tooltip, layout manager, etc. |
278 return view; | 261 if (delete_child) |
| 262 delete view; |
279 } | 263 } |
280 | 264 |
281 void View::RemoveAllChildViews(bool delete_children) { | 265 void View::RemoveAllChildViews(bool delete_children) { |
282 // TODO(beng): use for_each. | 266 while (!children_.empty()) { |
283 ViewVector::iterator it = children_.begin(); | 267 RemoveChildView(children_.front(), delete_children); |
284 while (it != children_.end()) { | |
285 View* v = RemoveChildView(*it); | |
286 if (delete_children) | |
287 delete v; | |
288 // TODO(beng): view deletion is actually more complicated in the old view.cc | 268 // TODO(beng): view deletion is actually more complicated in the old view.cc |
289 // figure out why. (it uses a ScopedVector to accumulate a list | 269 // figure out why. (it uses a ScopedVector to accumulate a list |
290 // of views to delete). | 270 // of views to delete). |
291 } | 271 } |
292 } | 272 } |
293 | 273 |
294 View* View::GetChildViewAt(size_t index) { | 274 bool View::Contains(const View& child) const { |
295 CHECK(index < child_count()); | 275 for (const View* v = &child; v; v = v->parent()) { |
296 return children_[index]; | 276 if (v == this) |
297 } | |
298 | |
299 bool View::Contains(View* child) { | |
300 while (child) { | |
301 if (child == this) | |
302 return true; | 277 return true; |
303 child = child->parent(); | |
304 } | 278 } |
305 return false; | 279 return false; |
306 } | 280 } |
307 | 281 |
308 // Painting -------------------------------------------------------------------- | 282 // Painting -------------------------------------------------------------------- |
309 | 283 |
310 void View::Invalidate() { | 284 void View::Invalidate() { |
311 InvalidateRect(gfx::Rect(0, 0, width(), height())); | 285 InvalidateRect(gfx::Rect(gfx::Point(), size())); |
312 } | 286 } |
313 | 287 |
314 void View::InvalidateRect(const gfx::Rect& invalid_rect) { | 288 void View::InvalidateRect(const gfx::Rect& invalid_rect) { |
315 if (!visible_) | 289 if (visible_ && parent_) { |
316 return; | 290 gfx::Rect r(invalid_rect); |
317 | |
318 if (parent_) { | |
319 gfx::Rect r = invalid_rect; | |
320 r.Offset(bounds_.origin()); | 291 r.Offset(bounds_.origin()); |
321 parent_->InvalidateRect(r); | 292 parent_->InvalidateRect(r); |
322 } | 293 } |
323 } | 294 } |
324 | 295 |
325 // Input ----------------------------------------------------------------------- | 296 // Input ----------------------------------------------------------------------- |
326 | 297 |
327 bool View::HitTest(const gfx::Point& point) const { | 298 bool View::HitTest(const gfx::Point& point) const { |
328 // TODO(beng): Hit test mask support. | 299 // TODO(beng): Hit test mask support. |
329 return gfx::Rect(0, 0, width(), height()).Contains(point); | 300 return gfx::Rect(gfx::Point(), size()).Contains(point); |
330 } | 301 } |
331 | 302 |
332 // Accelerators ---------------------------------------------------------------- | 303 // Accelerators ---------------------------------------------------------------- |
333 | 304 |
334 void View::AddAccelerator(const Accelerator& accelerator) { | 305 void View::AddAccelerator(const Accelerator& accelerator) { |
335 } | 306 } |
336 | 307 |
337 void View::RemoveAccelerator(const Accelerator& accelerator) { | 308 void View::RemoveAccelerator(const Accelerator& accelerator) { |
338 } | 309 } |
339 | 310 |
340 void View::RemoveAllAccelerators() { | 311 void View::RemoveAllAccelerators() { |
341 } | 312 } |
342 | 313 |
343 // Focus ----------------------------------------------------------------------- | 314 // Focus ----------------------------------------------------------------------- |
344 | 315 |
345 FocusManager* View::GetFocusManager() const { | 316 FocusManager* View::GetFocusManager() { |
346 Widget* widget = GetWidget(); | 317 return const_cast<FocusManager*>(static_cast<const View*>(this)-> |
| 318 GetFocusManager()); |
| 319 } |
| 320 |
| 321 const FocusManager* View::GetFocusManager() const { |
| 322 const Widget* widget = GetWidget(); |
347 return widget ? widget->GetFocusManager() : NULL; | 323 return widget ? widget->GetFocusManager() : NULL; |
348 } | 324 } |
349 | 325 |
350 FocusTraversable* View::GetFocusTraversable() const { | 326 FocusTraversable* View::GetFocusTraversable() { |
351 return NULL; | 327 return NULL; |
352 } | 328 } |
353 | 329 |
354 View* View::GetNextFocusableView() const { | 330 View* View::GetNextFocusableView() { |
355 return NULL; | 331 return NULL; |
356 } | 332 } |
357 | 333 |
358 View* View::GetPreviousFocusableView() const { | 334 View* View::GetPreviousFocusableView() { |
359 return NULL; | 335 return NULL; |
360 } | 336 } |
361 | 337 |
362 bool View::IsFocusable() const { | 338 bool View::IsFocusable() const { |
363 return focusable_ && enabled_ && visible_; | 339 return focusable_ && enabled_ && visible_; |
364 } | 340 } |
365 | 341 |
366 bool View::HasFocus() const { | 342 bool View::HasFocus() const { |
367 FocusManager* focus_manager = GetFocusManager(); | 343 const FocusManager* focus_manager = GetFocusManager(); |
368 return focus_manager ? focus_manager->focused_view() == this : false; | 344 return focus_manager ? (focus_manager->focused_view() == this) : false; |
369 } | 345 } |
370 | 346 |
371 void View::RequestFocus() { | 347 void View::RequestFocus() { |
372 FocusManager* focus_manager = GetFocusManager(); | 348 FocusManager* focus_manager = GetFocusManager(); |
373 if (focus_manager && focus_manager->focused_view() != this) | 349 if (focus_manager && (focus_manager->focused_view() != this)) |
374 focus_manager->SetFocusedView(this); | 350 focus_manager->SetFocusedView(this); |
375 } | 351 } |
376 | 352 |
377 // Resources ------------------------------------------------------------------- | 353 // Resources ------------------------------------------------------------------- |
378 | 354 |
379 ThemeProvider* View::GetThemeProvider() const { | 355 ThemeProvider* View::GetThemeProvider() { |
380 Widget* widget = GetWidget(); | 356 Widget* widget = GetWidget(); |
381 return widget ? widget->GetThemeProvider() : NULL; | 357 return widget ? widget->GetThemeProvider() : NULL; |
382 } | 358 } |
383 | 359 |
384 //////////////////////////////////////////////////////////////////////////////// | 360 //////////////////////////////////////////////////////////////////////////////// |
385 // View, protected: | 361 // View, protected: |
386 | 362 |
387 // Tree operations ------------------------------------------------------------- | 363 // Tree operations ------------------------------------------------------------- |
388 | 364 |
389 void View::OnViewAdded(View* parent, View* child) { | 365 void View::OnViewAdded(const View& parent, const View& child) { |
390 } | 366 } |
391 | 367 |
392 void View::OnViewRemoved(View* parent, View* child) { | 368 void View::OnViewRemoved(const View& parent, const View& child) { |
393 } | 369 } |
394 | 370 |
395 void View::OnViewAddedToWidget() { | 371 void View::OnViewAddedToWidget() { |
396 } | 372 } |
397 | 373 |
398 void View::OnViewRemovedFromWidget() { | 374 void View::OnViewRemovedFromWidget() { |
399 } | 375 } |
400 | 376 |
401 // Painting -------------------------------------------------------------------- | 377 // Painting -------------------------------------------------------------------- |
402 | 378 |
403 void View::PaintChildren(gfx::Canvas* canvas) { | 379 void View::PaintChildren(gfx::Canvas* canvas) { |
404 // TODO(beng): use for_each. | 380 std::for_each(children_begin(), children_end(), |
405 // std::for_each(children_.begin(), children_.end(), | 381 std::bind2nd(std::mem_fun(&View::Paint), canvas)); |
406 // std::bind2nd(std::mem_fun_ref(&View::Paint), canvas)); | |
407 ViewVector::iterator it = children_.begin(); | |
408 for (; it != children_.end(); ++it) | |
409 (*it)->Paint(canvas); | |
410 } | 382 } |
411 | 383 |
412 void View::OnPaint(gfx::Canvas* canvas) { | 384 void View::OnPaint(gfx::Canvas* canvas) { |
413 // TODO(beng): investigate moving these function calls to Paint(). | 385 // TODO(beng): investigate moving these function calls to Paint(). |
414 OnPaintBackground(canvas); | 386 OnPaintBackground(canvas); |
415 OnPaintFocusBorder(canvas); | 387 OnPaintFocusBorder(canvas); |
416 OnPaintBorder(canvas); | 388 OnPaintBorder(canvas); |
417 } | 389 } |
418 | 390 |
419 void View::OnPaintBackground(gfx::Canvas* canvas) { | 391 void View::OnPaintBackground(gfx::Canvas* canvas) { |
420 } | 392 } |
421 | 393 |
422 void View::OnPaintBorder(gfx::Canvas* canvas) { | 394 void View::OnPaintBorder(gfx::Canvas* canvas) { |
423 if (border_.get()) | 395 if (border_.get()) |
424 border_->Paint(const_cast<const View*>(this), canvas); | 396 border_->Paint(this, canvas); |
425 } | 397 } |
426 | 398 |
427 void View::OnPaintFocusBorder(gfx::Canvas* canvas) { | 399 void View::OnPaintFocusBorder(gfx::Canvas* canvas) { |
428 } | 400 } |
429 | 401 |
430 // Input ----------------------------------------------------------------------- | 402 // Input ----------------------------------------------------------------------- |
431 | 403 |
432 View* View::GetEventHandlerForPoint(const gfx::Point& point) const { | 404 View* View::GetEventHandlerForPoint(const gfx::Point& point) { |
433 ViewVector::const_reverse_iterator it = children_.rbegin(); | 405 for (Views::const_reverse_iterator i(children_rbegin()); i != children_rend(); |
434 for (; it != children_.rend(); ++it) { | 406 ++i) { |
435 View* child = *it; | 407 View* child = *i; |
436 if (!child->visible()) | 408 if (child->visible()) { |
437 continue; | 409 gfx::Point point_in_child_coords(point); |
438 | 410 View::ConvertPointToView(*this, *child, &point_in_child_coords); |
439 gfx::Point point_in_child_coords(point); | 411 if (child->HitTest(point_in_child_coords)) |
440 View::ConvertPointToView(const_cast<View*>(this), child, | 412 return child->GetEventHandlerForPoint(point_in_child_coords); |
441 &point_in_child_coords); | 413 } |
442 if (child->HitTest(point_in_child_coords)) | |
443 return child->GetEventHandlerForPoint(point_in_child_coords); | |
444 } | 414 } |
445 return const_cast<View*>(this); | 415 return this; |
446 } | 416 } |
447 | 417 |
448 gfx::NativeCursor View::GetCursorForPoint(const gfx::Point& point) { | 418 gfx::NativeCursor View::GetCursorForPoint(const gfx::Point& point) const { |
449 return NULL; | 419 return NULL; |
450 } | 420 } |
451 | 421 |
452 bool View::OnKeyPressed(const KeyEvent& event) { | 422 bool View::OnKeyPressed(const KeyEvent& event) { |
453 return true; | 423 return true; |
454 } | 424 } |
455 | 425 |
456 bool View::OnKeyReleased(const KeyEvent& event) { | 426 bool View::OnKeyReleased(const KeyEvent& event) { |
457 return true; | 427 return true; |
458 } | 428 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 bool View::IsFocusableInRootView() const { | 478 bool View::IsFocusableInRootView() const { |
509 // TODO(beng): kill this, replace with direct check in focus manager. | 479 // TODO(beng): kill this, replace with direct check in focus manager. |
510 return IsFocusable(); | 480 return IsFocusable(); |
511 } | 481 } |
512 | 482 |
513 bool View::IsAccessibilityFocusableInRootView() const { | 483 bool View::IsAccessibilityFocusableInRootView() const { |
514 // TODO(beng): kill this, replace with direct check in focus manager. | 484 // TODO(beng): kill this, replace with direct check in focus manager. |
515 return false; | 485 return false; |
516 } | 486 } |
517 | 487 |
518 FocusTraversable* View::GetPaneFocusTraversable() const { | 488 FocusTraversable* View::GetPaneFocusTraversable() { |
519 // TODO(beng): figure out what to do about this. | 489 // TODO(beng): figure out what to do about this. |
520 return NULL; | 490 return NULL; |
521 } | 491 } |
522 | 492 |
523 void View::OnFocus(const FocusEvent& event) { | 493 void View::OnFocus(const FocusEvent& event) { |
524 } | 494 } |
525 | 495 |
526 void View::OnBlur(const FocusEvent& event) { | 496 void View::OnBlur(const FocusEvent& event) { |
527 } | 497 } |
528 | 498 |
529 //////////////////////////////////////////////////////////////////////////////// | 499 //////////////////////////////////////////////////////////////////////////////// |
530 // View, private: | 500 // View, private: |
531 | 501 |
532 void View::DragInfo::Reset() { | 502 void View::DragInfo::Reset() { |
533 possible_drag = false; | 503 possible_drag = false; |
534 press_point = gfx::Point(); | 504 press_point = gfx::Point(); |
535 } | 505 } |
536 | 506 |
537 void View::DragInfo::PossibleDrag(const gfx::Point& point) { | 507 void View::DragInfo::PossibleDrag(const gfx::Point& point) { |
538 possible_drag = true; | 508 possible_drag = true; |
539 press_point = point; | 509 press_point = point; |
540 } | 510 } |
541 | 511 |
542 // Tree operations ------------------------------------------------------------- | 512 // Tree operations ------------------------------------------------------------- |
543 | 513 |
544 void View::NotifyHierarchyChanged(View* parent, View* child, bool is_add) { | 514 void View::NotifyHierarchyChanged(View* child, bool is_add) { |
545 // Notify the child. Note that we call GetWidget() on the parent, not the | |
546 // child, since this method is called after the child is already removed from | |
547 // the hierarchy when |is_add| is false and so child->GetWidget() will always | |
548 // return NULL. | |
549 bool has_widget = parent->GetWidget() != NULL; | |
550 CallViewNotification(child, parent, child, is_add, has_widget); | |
551 | |
552 // Notify the hierarchy up. | 515 // Notify the hierarchy up. |
553 NotifyHierarchyChangedUp(parent, child, is_add); | 516 for (View* v = parent(); v; v = v->parent()) |
| 517 CallViewNotification(v, *child, is_add, false); |
554 | 518 |
555 // Notify the hierarchy down. | 519 // Notify the hierarchy down. |
| 520 bool has_widget = GetWidget() != NULL; |
556 if (!is_add) { | 521 if (!is_add) { |
557 // Because |child| has already been removed from |parent|'s child list, we | 522 // Because |child| has already been removed from |parent|'s child list, we |
558 // need to notify its hierarchy manually. | 523 // need to notify its hierarchy manually. |
559 child->NotifyHierarchyChangedDown(parent, child, is_add, has_widget); | 524 child->NotifyHierarchyChangedDown(*child, is_add, has_widget); |
560 } | 525 } |
561 NotifyHierarchyChangedDown(parent, child, is_add, has_widget); | 526 NotifyHierarchyChangedDown(*child, is_add, has_widget); |
562 } | 527 } |
563 | 528 |
564 void View::NotifyHierarchyChangedUp(View* parent, View* child, bool is_add) { | 529 void View::NotifyHierarchyChangedDown(const View& child, |
565 for (View* v = parent; v; v = v->parent()) { | 530 bool is_add, |
566 if (is_add) | 531 bool has_widget) { |
567 v->OnViewAdded(parent, child); | 532 CallViewNotification(this, child, is_add, has_widget); |
568 else | 533 for (Views::const_iterator i(children_begin()); i != children_end(); ++i) |
569 v->OnViewRemoved(parent, child); | 534 (*i)->NotifyHierarchyChangedDown(child, is_add, has_widget); |
570 } | |
571 } | |
572 | |
573 void View::NotifyHierarchyChangedDown(View* parent, View* child, bool is_add, | |
574 bool has_widget) { | |
575 ViewVector::iterator it = children_.begin(); | |
576 for (; it != children_.end(); ++it) { | |
577 CallViewNotification(*it, parent, child, is_add, has_widget); | |
578 (*it)->NotifyHierarchyChangedDown(parent, child, is_add, has_widget); | |
579 } | |
580 } | 535 } |
581 | 536 |
582 void View::CallViewNotification(View* target, | 537 void View::CallViewNotification(View* target, |
583 View* parent, | 538 const View& child, |
584 View* child, | |
585 bool is_add, | 539 bool is_add, |
586 bool has_widget) { | 540 bool has_widget) { |
587 if (is_add) { | 541 if (is_add) { |
588 target->OnViewAdded(parent, child); | 542 target->OnViewAdded(*this, child); |
589 if (has_widget) | 543 if (has_widget) |
590 target->OnViewAddedToWidget(); | 544 target->OnViewAddedToWidget(); |
591 } else { | 545 } else { |
592 target->OnViewRemoved(parent, child); | 546 target->OnViewRemoved(*this, child); |
593 if (has_widget) | 547 if (has_widget) |
594 target->OnViewRemovedFromWidget(); | 548 target->OnViewRemovedFromWidget(); |
595 } | 549 } |
596 } | 550 } |
597 | 551 |
598 // Painting -------------------------------------------------------------------- | 552 // Painting -------------------------------------------------------------------- |
599 | 553 |
600 void View::Paint(gfx::Canvas* canvas) { | 554 void View::Paint(gfx::Canvas* canvas) { |
601 // Invisible views are not painted. | 555 // Invisible views are not painted. |
602 if (!visible_) | 556 if (!visible_) |
(...skipping 10 matching lines...) Expand all Loading... |
613 } | 567 } |
614 | 568 |
615 // Input ----------------------------------------------------------------------- | 569 // Input ----------------------------------------------------------------------- |
616 | 570 |
617 bool View::MousePressed(const MouseEvent& event, DragInfo* drag_info) { | 571 bool View::MousePressed(const MouseEvent& event, DragInfo* drag_info) { |
618 bool handled = OnMousePressed(event); | 572 bool handled = OnMousePressed(event); |
619 // TODO(beng): deal with view deletion, see ProcessMousePressed() in old code. | 573 // TODO(beng): deal with view deletion, see ProcessMousePressed() in old code. |
620 if (!enabled_) | 574 if (!enabled_) |
621 return handled; | 575 return handled; |
622 | 576 |
623 int drag_operations = | 577 if (!event.IsOnlyLeftMouseButton() || !HitTest(event.location()) || |
624 enabled_ && event.IsOnlyLeftMouseButton() && HitTest(event.location()) ? | 578 (GetDragOperations(event.location()) == DragDropTypes::DRAG_NONE)) |
625 GetDragOperations(event.location()) : DragDropTypes::DRAG_NONE; | 579 return handled || (event.IsRightMouseButton() && context_menu_controller_); |
626 if (drag_operations != DragDropTypes::DRAG_NONE) { | 580 |
627 drag_info->PossibleDrag(event.location()); | 581 drag_info->PossibleDrag(event.location()); |
628 return true; | 582 return true; |
629 } | |
630 bool has_context_menu = event.IsRightMouseButton() ? | |
631 !!context_menu_controller_ : NULL; | |
632 return has_context_menu || handled; | |
633 } | 583 } |
634 | 584 |
635 bool View::MouseDragged(const MouseEvent& event, DragInfo* drag_info) { | 585 bool View::MouseDragged(const MouseEvent& event, DragInfo* drag_info) { |
636 if (drag_info->possible_drag && | 586 if (drag_info->possible_drag && |
637 ExceededDragThreshold(drag_info->press_point, event.location())) { | 587 ExceededDragThreshold(drag_info->press_point, event.location())) { |
638 if (!drag_controller_ || | 588 if (!drag_controller_ || |
639 drag_controller_->CanStartDrag(this, drag_info->press_point, | 589 drag_controller_->CanStartDrag(this, drag_info->press_point, |
640 event.location())) { | 590 event.location())) |
641 StartShellDrag(event, drag_info->press_point); | 591 StartShellDrag(event, drag_info->press_point); |
642 } | 592 } else if (OnMouseDragged(event)) { |
643 } else { | 593 return true; |
644 if (OnMouseDragged(event)) | |
645 return true; | |
646 } | 594 } |
647 // TODO(beng): Handle view deletion from OnMouseDragged(). | 595 // TODO(beng): Handle view deletion from OnMouseDragged(). |
648 return !!context_menu_controller_ || drag_info->possible_drag; | 596 return context_menu_controller_ || drag_info->possible_drag; |
649 } | 597 } |
650 | 598 |
651 void View::MouseReleased(const MouseEvent& event) { | 599 void View::MouseReleased(const MouseEvent& event) { |
652 OnMouseReleased(event); | 600 OnMouseReleased(event); |
653 // TODO(beng): Handle view deletion from OnMouseReleased(). | 601 // TODO(beng): Handle view deletion from OnMouseReleased(). |
654 if (context_menu_controller_ && event.IsOnlyRightMouseButton()) { | 602 if (context_menu_controller_ && event.IsOnlyRightMouseButton()) { |
655 gfx::Point location(event.location()); | 603 gfx::Point location(event.location()); |
656 if (HitTest(location)) { | 604 if (HitTest(location)) { |
657 ConvertPointToScreen(this, &location); | 605 ConvertPointToScreen(*this, &location); |
658 context_menu_controller_->ShowContextMenu(this, location, true); | 606 context_menu_controller_->ShowContextMenu(this, location, true); |
659 } | 607 } |
660 } | 608 } |
661 } | 609 } |
662 | 610 |
663 // Focus ----------------------------------------------------------------------- | 611 // Focus ----------------------------------------------------------------------- |
664 | 612 |
665 // TODO(beng): Move to FocusManager. | 613 // TODO(beng): Move to FocusManager. |
666 void View::InitFocusSiblings(View* view, size_t index) { | 614 void View::InitFocusSiblings(View* view, size_t index) { |
667 if (child_count() == 0) { | 615 if (children_empty()) { |
668 view->next_focusable_view_ = NULL; | 616 view->next_focusable_view_ = NULL; |
669 view->prev_focusable_view_ = NULL; | 617 view->prev_focusable_view_ = NULL; |
670 } else { | 618 return; |
671 if (index == child_count()) { | 619 } |
672 // We are inserting at the end, but the end of the child list may not be | 620 |
673 // the last focusable element. Let's try to find an element with no next | 621 if (index != children_size()) { |
674 // focusable element to link to. | 622 View* prev = children_[index]->GetPreviousFocusableView(); |
675 View* last_focusable_view = NULL; | 623 view->prev_focusable_view_ = prev; |
676 for (std::vector<View*>::iterator iter = children_.begin(); | 624 view->next_focusable_view_ = children_[index]; |
677 iter != children_.end(); ++iter) { | 625 if (prev) |
678 if (!(*iter)->next_focusable_view_) { | 626 prev->next_focusable_view_ = view; |
679 last_focusable_view = *iter; | 627 children_[index]->prev_focusable_view_ = view; |
680 break; | 628 return; |
681 } | 629 } |
682 } | 630 |
683 if (last_focusable_view == NULL) { | 631 // We are inserting at the end, but the end of the child list may not be |
684 // Hum... there is a cycle in the focus list. Let's just insert ourself | 632 // the last focusable element. Let's try to find an element with no next |
685 // after the last child. | 633 // focusable element to link to. |
686 View* prev = children_[index - 1]; | 634 for (Views::const_iterator i(children_begin()); i != children_end(); ++i) { |
687 view->prev_focusable_view_ = prev; | 635 if (!(*i)->next_focusable_view_) { |
688 view->next_focusable_view_ = prev->next_focusable_view_; | 636 (*i)->next_focusable_view_ = view; |
689 prev->next_focusable_view_->prev_focusable_view_ = view; | 637 view->next_focusable_view_ = NULL; |
690 prev->next_focusable_view_ = view; | 638 view->prev_focusable_view_ = *i; |
691 } else { | 639 return; |
692 last_focusable_view->next_focusable_view_ = view; | |
693 view->next_focusable_view_ = NULL; | |
694 view->prev_focusable_view_ = last_focusable_view; | |
695 } | |
696 } else { | |
697 View* prev = children_[index]->GetPreviousFocusableView(); | |
698 view->prev_focusable_view_ = prev; | |
699 view->next_focusable_view_ = children_[index]; | |
700 if (prev) | |
701 prev->next_focusable_view_ = view; | |
702 children_[index]->prev_focusable_view_ = view; | |
703 } | 640 } |
704 } | 641 } |
| 642 |
| 643 // Hum... there is a cycle in the focus list. Let's just insert ourself |
| 644 // after the last child. |
| 645 View* prev = children_[index - 1]; |
| 646 view->prev_focusable_view_ = prev; |
| 647 view->next_focusable_view_ = prev->next_focusable_view_; |
| 648 prev->next_focusable_view_->prev_focusable_view_ = view; |
| 649 prev->next_focusable_view_ = view; |
705 } | 650 } |
706 | 651 |
707 // Drag & Drop ----------------------------------------------------------------- | 652 // Drag & Drop ----------------------------------------------------------------- |
708 | 653 |
709 int View::GetDragOperations(const gfx::Point& point) { | 654 int View::GetDragOperations(const gfx::Point& point) { |
710 return drag_controller_ ? | 655 return drag_controller_ ? |
711 drag_controller_->GetDragOperations(const_cast<View*>(this), point) : | 656 drag_controller_->GetDragOperations(this, point) : |
712 DragDropTypes::DRAG_NONE; | 657 DragDropTypes::DRAG_NONE; |
713 } | 658 } |
714 | 659 |
715 void View::WriteDragData(const gfx::Point& point, OSExchangeData* data) { | 660 void View::WriteDragData(const gfx::Point& point, OSExchangeData* data) { |
716 drag_controller_->WriteDragData(this, point, data); | 661 drag_controller_->WriteDragData(this, point, data); |
717 } | 662 } |
718 | 663 |
719 void View::StartShellDrag(const MouseEvent& event, | 664 void View::StartShellDrag(const MouseEvent& event, |
720 const gfx::Point& press_point) { | 665 const gfx::Point& press_point) { |
721 // TODO(beng): system stuff. | 666 // TODO(beng): system stuff. |
722 } | 667 } |
723 | 668 |
724 } // namespace ui | 669 } // namespace ui |
OLD | NEW |