blob: 285aed7565ab033fd3202e751b98c7a30a66ca90 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/views/view.h"
6
7#include <algorithm>
[email protected]1eb89e82008-08-15 12:27:038
9#ifndef NDEBUG
initial.commit09911bf2008-07-26 23:55:2910#include <iostream>
[email protected]1eb89e82008-08-15 12:27:0311#endif
initial.commit09911bf2008-07-26 23:55:2912
13#include "base/logging.h"
14#include "base/message_loop.h"
[email protected]82739cf2008-09-16 00:37:5615#include "base/scoped_handle.h"
initial.commit09911bf2008-07-26 23:55:2916#include "base/string_util.h"
[email protected]1eb89e82008-08-15 12:27:0317#include "chrome/common/drag_drop_types.h"
initial.commit09911bf2008-07-26 23:55:2918#include "chrome/common/gfx/chrome_canvas.h"
[email protected]82739cf2008-09-16 00:37:5619#include "chrome/common/gfx/path.h"
[email protected]1eb89e82008-08-15 12:27:0320#include "chrome/common/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2921#include "chrome/common/os_exchange_data.h"
[email protected]1eb89e82008-08-15 12:27:0322#include "chrome/views/accessibility/accessible_wrapper.h"
initial.commit09911bf2008-07-26 23:55:2923#include "chrome/views/background.h"
24#include "chrome/views/border.h"
[email protected]4d0bd102008-10-16 00:26:3025#include "chrome/views/container.h"
initial.commit09911bf2008-07-26 23:55:2926#include "chrome/views/layout_manager.h"
27#include "chrome/views/root_view.h"
28#include "chrome/views/tooltip_manager.h"
initial.commit09911bf2008-07-26 23:55:2929#include "SkShader.h"
30
[email protected]c2dacc92008-10-16 23:51:3831namespace views {
initial.commit09911bf2008-07-26 23:55:2932
33// static
34char View::kViewClassName[] = "chrome/views/View";
35
36////////////////////////////////////////////////////////////////////////////////
37//
38// A task used to automatically restore focus on the last focused floating view
39//
40////////////////////////////////////////////////////////////////////////////////
41
42class RestoreFocusTask : public Task {
43 public:
44 explicit RestoreFocusTask(View* target) : view_(target) {
45 }
46
47 ~RestoreFocusTask() {}
48
49 void Cancel() {
50 view_ = NULL;
51 }
52
53 void Run() {
54 if (view_)
55 view_->RestoreFloatingViewFocus();
56 }
57 private:
58 // The target view.
59 View* view_;
60
61 DISALLOW_EVIL_CONSTRUCTORS(RestoreFocusTask);
62};
63
64/////////////////////////////////////////////////////////////////////////////
65//
66// View - constructors, destructors, initialization
67//
68/////////////////////////////////////////////////////////////////////////////
69
70View::View()
71 : id_(0),
72 group_(-1),
73 bounds_(0,0,0,0),
74 parent_(NULL),
75 enabled_(true),
76 is_visible_(true),
77 focusable_(false),
78 background_(NULL),
79 accessibility_(NULL),
80 border_(NULL),
81 is_parent_owned_(true),
82 notify_when_visible_bounds_in_root_changes_(false),
83 registered_for_visible_bounds_notification_(false),
84 next_focusable_view_(NULL),
85 previous_focusable_view_(NULL),
86 should_restore_focus_(false),
87 restore_focus_view_task_(NULL),
88 context_menu_controller_(NULL),
89 drag_controller_(NULL),
90 ui_mirroring_is_enabled_for_rtl_languages_(true),
91 flip_canvas_on_paint_for_rtl_ui_(false) {
92}
93
94View::~View() {
95 if (restore_focus_view_task_)
96 restore_focus_view_task_->Cancel();
97
98 int c = static_cast<int>(child_views_.size());
99 while (--c >= 0) {
100 if (child_views_[c]->IsParentOwned())
101 delete child_views_[c];
102 else
103 child_views_[c]->SetParent(NULL);
104 }
105 if (background_)
106 delete background_;
107 if (border_)
108 delete border_;
109}
110
111/////////////////////////////////////////////////////////////////////////////
112//
113// View - sizing
114//
115/////////////////////////////////////////////////////////////////////////////
116
[email protected]0d8ea702008-10-14 17:03:07117gfx::Rect View::GetBounds(PositionMirroringSettings settings) const {
118 gfx::Rect bounds(bounds_);
initial.commit09911bf2008-07-26 23:55:29119
120 // If the parent uses an RTL UI layout and if we are asked to transform the
121 // bounds to their mirrored position if necessary, then we should shift the
122 // rectangle appropriately.
[email protected]0d8ea702008-10-14 17:03:07123 if (settings == APPLY_MIRRORING_TRANSFORMATION)
124 bounds.set_x(MirroredX());
125
126 return bounds;
initial.commit09911bf2008-07-26 23:55:29127}
128
[email protected]6f3bb6c2008-09-17 22:25:33129// y(), width() and height() are agnostic to the RTL UI layout of the
130// parent view. x(), on the other hand, is not.
initial.commit09911bf2008-07-26 23:55:29131int View::GetX(PositionMirroringSettings settings) const {
[email protected]80f8b9f2008-10-16 18:17:47132 return settings == IGNORE_MIRRORING_TRANSFORMATION ? x() : MirroredX();
initial.commit09911bf2008-07-26 23:55:29133}
134
[email protected]80f8b9f2008-10-16 18:17:47135void View::SetBounds(const gfx::Rect& bounds) {
136 if (bounds == bounds_)
initial.commit09911bf2008-07-26 23:55:29137 return;
initial.commit09911bf2008-07-26 23:55:29138
[email protected]80f8b9f2008-10-16 18:17:47139 gfx::Rect prev = bounds_;
initial.commit09911bf2008-07-26 23:55:29140 bounds_ = bounds;
initial.commit09911bf2008-07-26 23:55:29141 DidChangeBounds(prev, bounds_);
142
143 RootView* root = GetRootView();
144 if (root) {
[email protected]80f8b9f2008-10-16 18:17:47145 bool size_changed = prev.size() != bounds_.size();
146 bool position_changed = prev.origin() != bounds_.origin();
initial.commit09911bf2008-07-26 23:55:29147 if (size_changed || position_changed)
148 root->ViewBoundsChanged(this, size_changed, position_changed);
149 }
150}
151
[email protected]80f8b9f2008-10-16 18:17:47152gfx::Rect View::GetLocalBounds(bool include_border) const {
153 if (include_border || border_ == NULL)
154 return gfx::Rect(0, 0, width(), height());
initial.commit09911bf2008-07-26 23:55:29155
[email protected]80f8b9f2008-10-16 18:17:47156 gfx::Insets insets;
157 border_->GetInsets(&insets);
158 return gfx::Rect(insets.left(), insets.top(),
159 width() - insets.width(), height() - insets.height());
initial.commit09911bf2008-07-26 23:55:29160}
161
[email protected]0a1d36b22008-10-17 19:33:09162gfx::Point View::GetPosition() const {
163 return gfx::Point(GetX(APPLY_MIRRORING_TRANSFORMATION), y());
initial.commit09911bf2008-07-26 23:55:29164}
165
[email protected]154f8bc2008-10-15 18:02:30166gfx::Size View::GetPreferredSize() {
167 if (layout_manager_.get())
168 return layout_manager_->GetPreferredSize(this);
169 return gfx::Size();
initial.commit09911bf2008-07-26 23:55:29170}
171
172void View::SizeToPreferredSize() {
[email protected]154f8bc2008-10-15 18:02:30173 gfx::Size prefsize = GetPreferredSize();
174 if ((prefsize.width() != width()) || (prefsize.height() != height()))
175 SetBounds(x(), y(), prefsize.width(), prefsize.height());
initial.commit09911bf2008-07-26 23:55:29176}
177
[email protected]154f8bc2008-10-15 18:02:30178gfx::Size View::GetMinimumSize() {
179 return GetPreferredSize();
initial.commit09911bf2008-07-26 23:55:29180}
181
182int View::GetHeightForWidth(int w) {
183 if (layout_manager_.get())
184 return layout_manager_->GetPreferredHeightForWidth(this, w);
185
[email protected]154f8bc2008-10-15 18:02:30186 return GetPreferredSize().height();
initial.commit09911bf2008-07-26 23:55:29187}
188
[email protected]80f8b9f2008-10-16 18:17:47189void View::DidChangeBounds(const gfx::Rect& previous,
190 const gfx::Rect& current) {
191 Layout();
initial.commit09911bf2008-07-26 23:55:29192}
193
194void View::ScrollRectToVisible(int x, int y, int width, int height) {
195 View* parent = GetParent();
196
197 // We must take RTL UI mirroring into account when adjusting the position of
198 // the region.
199 if (parent)
200 parent->ScrollRectToVisible(
[email protected]6f3bb6c2008-09-17 22:25:33201 GetX(APPLY_MIRRORING_TRANSFORMATION) + x, View::y() + y, width, height);
initial.commit09911bf2008-07-26 23:55:29202}
203
204/////////////////////////////////////////////////////////////////////////////
205//
206// View - layout
207//
208/////////////////////////////////////////////////////////////////////////////
209
210void View::Layout() {
211 // Layout child Views
212 if (layout_manager_.get()) {
213 layout_manager_->Layout(this);
214 SchedulePaint();
215 }
216
217 // Lay out contents of child Views
218 int child_count = GetChildViewCount();
219 for (int i = 0; i < child_count; ++i) {
220 View* child = GetChildViewAt(i);
221 child->Layout();
222 }
223}
224
225LayoutManager* View::GetLayoutManager() const {
226 return layout_manager_.get();
227}
228
229void View::SetLayoutManager(LayoutManager* layout_manager) {
230 if (layout_manager_.get()) {
231 layout_manager_->Uninstalled(this);
232 }
233 layout_manager_.reset(layout_manager);
234 if (layout_manager_.get()) {
235 layout_manager_->Installed(this);
236 }
237}
238
[email protected]1eb89e82008-08-15 12:27:03239bool View::UILayoutIsRightToLeft() const {
240 return (ui_mirroring_is_enabled_for_rtl_languages_ &&
241 l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT);
242}
243
initial.commit09911bf2008-07-26 23:55:29244////////////////////////////////////////////////////////////////////////////////
245//
246// View - Right-to-left UI layout
247//
248////////////////////////////////////////////////////////////////////////////////
249
250inline int View::MirroredX() const {
[email protected]63329982008-10-10 21:56:57251 // TODO(beng): reimplement in terms of MirroredLeftPointForRect.
initial.commit09911bf2008-07-26 23:55:29252 View* parent = GetParent();
[email protected]80f8b9f2008-10-16 18:17:47253 if (parent && parent->UILayoutIsRightToLeft())
254 return parent->width() - x() - width();
255 return x();
initial.commit09911bf2008-07-26 23:55:29256}
257
258int View::MirroredLeftPointForRect(const gfx::Rect& bounds) const {
259 if (!UILayoutIsRightToLeft()) {
260 return bounds.x();
261 }
[email protected]6f3bb6c2008-09-17 22:25:33262 return width() - bounds.x() - bounds.width();
initial.commit09911bf2008-07-26 23:55:29263}
264
265////////////////////////////////////////////////////////////////////////////////
266//
267// View - states
268//
269////////////////////////////////////////////////////////////////////////////////
270
271bool View::IsEnabled() const {
272 return enabled_;
273}
274
275void View::SetEnabled(bool state) {
276 if (enabled_ != state) {
277 enabled_ = state;
278 SchedulePaint();
279 }
280}
281
282bool View::IsFocusable() const {
283 return focusable_ && enabled_ && is_visible_;
284}
285
286void View::SetFocusable(bool focusable) {
287 focusable_ = focusable;
288}
289
290FocusManager* View::GetFocusManager() {
[email protected]4d0bd102008-10-16 00:26:30291 Container* container = GetContainer();
initial.commit09911bf2008-07-26 23:55:29292 if (!container)
293 return NULL;
294
295 HWND hwnd = container->GetHWND();
296 if (!hwnd)
297 return NULL;
298
[email protected]c2dacc92008-10-16 23:51:38299 return FocusManager::GetFocusManager(hwnd);
initial.commit09911bf2008-07-26 23:55:29300}
301
302bool View::HasFocus() {
303 RootView* root_view = GetRootView();
304 FocusManager* focus_manager = GetFocusManager();
305 if (focus_manager)
306 return focus_manager->GetFocusedView() == this;
307 return false;
308}
309
310void View::SetHotTracked(bool flag) {
311}
312
313/////////////////////////////////////////////////////////////////////////////
314//
315// View - painting
316//
317/////////////////////////////////////////////////////////////////////////////
318
[email protected]0a1d36b22008-10-17 19:33:09319void View::SchedulePaint(const gfx::Rect& r, bool urgent) {
320 if (!IsVisible())
initial.commit09911bf2008-07-26 23:55:29321 return;
initial.commit09911bf2008-07-26 23:55:29322
323 if (parent_) {
324 // Translate the requested paint rect to the parent's coordinate system
325 // then pass this notification up to the parent.
[email protected]0a1d36b22008-10-17 19:33:09326 gfx::Rect paint_rect = r;
327 paint_rect.Offset(GetPosition());
initial.commit09911bf2008-07-26 23:55:29328 parent_->SchedulePaint(paint_rect, urgent);
329 }
330}
331
332void View::SchedulePaint() {
[email protected]0a1d36b22008-10-17 19:33:09333 SchedulePaint(GetLocalBounds(true), false);
initial.commit09911bf2008-07-26 23:55:29334}
335
336void View::SchedulePaint(int x, int y, int w, int h) {
[email protected]0a1d36b22008-10-17 19:33:09337 SchedulePaint(gfx::Rect(x, y, w, h), false);
initial.commit09911bf2008-07-26 23:55:29338}
339
340void View::Paint(ChromeCanvas* canvas) {
341 PaintBackground(canvas);
342 PaintFocusBorder(canvas);
343 PaintBorder(canvas);
344}
345
346void View::PaintBackground(ChromeCanvas* canvas) {
347 if (background_)
348 background_->Paint(canvas, this);
349}
350
351void View::PaintBorder(ChromeCanvas* canvas) {
352 if (border_)
353 border_->Paint(*this, canvas);
354}
355
356void View::PaintFocusBorder(ChromeCanvas* canvas) {
357 if (HasFocus() && IsFocusable())
[email protected]6f3bb6c2008-09-17 22:25:33358 canvas->DrawFocusRect(0, 0, width(), height());
initial.commit09911bf2008-07-26 23:55:29359}
360
361void View::PaintChildren(ChromeCanvas* canvas) {
362 int i, c;
363 for (i = 0, c = GetChildViewCount(); i < c; ++i) {
364 View* child = GetChildViewAt(i);
365 if (!child) {
366 NOTREACHED() << "Should not have a NULL child View for index in bounds";
367 continue;
368 }
369 child->ProcessPaint(canvas);
370 }
371}
372
373void View::ProcessPaint(ChromeCanvas* canvas) {
374 if (!IsVisible()) {
375 return;
376 }
377
378 // We're going to modify the canvas, save it's state first.
379 canvas->save();
380
381 // Paint this View and its children, setting the clip rect to the bounds
382 // of this View and translating the origin to the local bounds' top left
383 // point.
384 //
385 // Note that the X (or left) position we pass to ClipRectInt takes into
386 // consideration whether or not the view uses a right-to-left layout so that
387 // we paint our view in its mirrored position if need be.
[email protected]80f8b9f2008-10-16 18:17:47388 if (canvas->ClipRectInt(MirroredX(), y(), width(), height())) {
initial.commit09911bf2008-07-26 23:55:29389 // Non-empty clip, translate the graphics such that 0,0 corresponds to
390 // where this view is located (related to its parent).
[email protected]80f8b9f2008-10-16 18:17:47391 canvas->TranslateInt(MirroredX(), y());
initial.commit09911bf2008-07-26 23:55:29392
393 // Save the state again, so that any changes don't effect PaintChildren.
394 canvas->save();
395
396 // If the View we are about to paint requested the canvas to be flipped, we
397 // should change the transform appropriately.
398 bool flip_canvas = FlipCanvasOnPaintForRTLUI();
399 if (flip_canvas) {
[email protected]6f3bb6c2008-09-17 22:25:33400 canvas->TranslateInt(width(), 0);
initial.commit09911bf2008-07-26 23:55:29401 canvas->ScaleInt(-1, 1);
402 canvas->save();
403 }
404
405 Paint(canvas);
406
407 // We must undo the canvas mirroring once the View is done painting so that
408 // we don't pass the canvas with the mirrored transform to Views that
409 // didn't request the canvas to be flipped.
410 if (flip_canvas) {
411 canvas->restore();
412 }
413 canvas->restore();
414 PaintChildren(canvas);
415 }
416
417 // Restore the canvas's original transform.
418 canvas->restore();
419}
420
421void View::PaintNow() {
422 if (!IsVisible()) {
423 return;
424 }
425
426 View* view = GetParent();
427 if (view)
428 view->PaintNow();
429}
430
431void View::PaintFloatingView(ChromeCanvas* canvas, View* view,
432 int x, int y, int w, int h) {
433 if (should_restore_focus_ && ShouldRestoreFloatingViewFocus()) {
434 // We are painting again a floating view, this is a good time to restore the
435 // focus to the last focused floating view if any.
436 should_restore_focus_ = false;
437 restore_focus_view_task_ = new RestoreFocusTask(this);
438 MessageLoop::current()->PostTask(FROM_HERE, restore_focus_view_task_);
439 }
440 View* saved_parent = view->GetParent();
441 view->SetParent(this);
442 view->SetBounds(x, y, w, h);
443 view->Layout();
444 view->ProcessPaint(canvas);
445 view->SetParent(saved_parent);
446}
447
448void View::SetBackground(Background* b) {
449 if (background_ != b)
450 delete background_;
451 background_ = b;
452}
453
454const Background* View::GetBackground() const {
455 return background_;
456}
457
458void View::SetBorder(Border* b) {
459 if (border_ != b)
460 delete border_;
461 border_ = b;
462}
463
464const Border* View::GetBorder() const {
465 return border_;
466}
467
468gfx::Insets View::GetInsets() const {
469 const Border* border = GetBorder();
470 gfx::Insets insets;
471 if (border)
472 border->GetInsets(&insets);
473 return insets;
474}
475
476void View::SetContextMenuController(ContextMenuController* menu_controller) {
477 context_menu_controller_ = menu_controller;
478}
479
480/////////////////////////////////////////////////////////////////////////////
481//
482// View - tree
483//
484/////////////////////////////////////////////////////////////////////////////
485
486bool View::ProcessMousePressed(const MouseEvent& e, DragInfo* drag_info) {
487 const bool enabled = enabled_;
488 int drag_operations;
[email protected]613b8062008-10-14 23:45:09489 if (enabled && e.IsOnlyLeftMouseButton() && HitTest(e.location()))
[email protected]6f3bb6c2008-09-17 22:25:33490 drag_operations = GetDragOperations(e.x(), e.y());
initial.commit09911bf2008-07-26 23:55:29491 else
492 drag_operations = 0;
493 ContextMenuController* context_menu_controller = context_menu_controller_;
494
495 const bool result = OnMousePressed(e);
496 // WARNING: we may have been deleted, don't use any View variables;
497
498 if (!enabled)
499 return result;
500
501 if (drag_operations != DragDropTypes::DRAG_NONE) {
[email protected]6f3bb6c2008-09-17 22:25:33502 drag_info->PossibleDrag(e.x(), e.y());
initial.commit09911bf2008-07-26 23:55:29503 return true;
504 }
505 return !!context_menu_controller || result;
506}
507
508bool View::ProcessMouseDragged(const MouseEvent& e, DragInfo* drag_info) {
509 // Copy the field, that way if we're deleted after drag and drop no harm is
510 // done.
511 ContextMenuController* context_menu_controller = context_menu_controller_;
512 const bool possible_drag = drag_info->possible_drag;
[email protected]6f3bb6c2008-09-17 22:25:33513 if (possible_drag && ExceededDragThreshold(drag_info->start_x - e.x(),
514 drag_info->start_y - e.y())) {
initial.commit09911bf2008-07-26 23:55:29515 DoDrag(e, drag_info->start_x, drag_info->start_y);
516 } else {
517 if (OnMouseDragged(e))
518 return true;
519 // Fall through to return value based on context menu controller.
520 }
521 // WARNING: we may have been deleted.
522 return (context_menu_controller != NULL) || possible_drag;
523}
524
525void View::ProcessMouseReleased(const MouseEvent& e, bool canceled) {
526 if (!canceled && context_menu_controller_ && e.IsOnlyRightMouseButton()) {
527 // Assume that if there is a context menu controller we won't be deleted
528 // from mouse released.
[email protected]96b667d2008-10-14 20:58:44529 gfx::Point location(e.location());
initial.commit09911bf2008-07-26 23:55:29530 ConvertPointToScreen(this, &location);
531 ContextMenuController* context_menu_controller = context_menu_controller_;
532 OnMouseReleased(e, canceled);
[email protected]96b667d2008-10-14 20:58:44533 context_menu_controller_->ShowContextMenu(this, location.x(), location.y(),
initial.commit09911bf2008-07-26 23:55:29534 true);
535 } else {
536 OnMouseReleased(e, canceled);
537 }
538 // WARNING: we may have been deleted.
539}
540
541void View::DoDrag(const MouseEvent& e, int press_x, int press_y) {
542 scoped_refptr<OSExchangeData> data = new OSExchangeData;
543 WriteDragData(press_x, press_y, data.get());
544
545 // Message the RootView to do the drag and drop. That way if we're removed
546 // the RootView can detect it and avoid callins us back.
547 RootView* root_view = GetRootView();
548 root_view->StartDragForViewFromMouseEvent(
549 this, data, GetDragOperations(press_x, press_y));
550}
551
552void View::AddChildView(View* v) {
553 AddChildView(static_cast<int>(child_views_.size()), v, false);
554}
555
556void View::AddChildView(int index, View* v) {
557 AddChildView(index, v, false);
558}
559
560void View::AddChildView(int index, View* v, bool floating_view) {
561 // Remove the view from its current parent if any.
562 if (v->GetParent())
563 v->GetParent()->RemoveChildView(v);
564
565 if (!floating_view) {
566 // Sets the prev/next focus views.
567 InitFocusSiblings(v, index);
568 }
569
570 // Let's insert the view.
571 child_views_.insert(child_views_.begin() + index, v);
572 v->SetParent(this);
573
574 for (View* p = this; p; p = p->GetParent()) {
575 p->ViewHierarchyChangedImpl(false, true, this, v);
576 }
577 v->PropagateAddNotifications(this, v);
578 UpdateTooltip();
579 RootView* root = GetRootView();
580 if (root)
581 RegisterChildrenForVisibleBoundsNotification(root, v);
582
583 if (layout_manager_.get())
584 layout_manager_->ViewAdded(this, v);
585}
586
587View* View::GetChildViewAt(int index) const {
588 return index < GetChildViewCount() ? child_views_[index] : NULL;
589}
590
591int View::GetChildViewCount() const {
592 return static_cast<int>(child_views_.size());
593}
594
595void View::RemoveChildView(View* a_view) {
596 DoRemoveChildView(a_view, true, true, false);
597}
598
599void View::RemoveAllChildViews(bool delete_views) {
600 ViewList::iterator iter;
601 while ((iter = child_views_.begin()) != child_views_.end()) {
602 DoRemoveChildView(*iter, false, false, delete_views);
603 }
604 UpdateTooltip();
605}
606
607void View::DoRemoveChildView(View* a_view,
608 bool update_focus_cycle,
609 bool update_tool_tip,
610 bool delete_removed_view) {
611#ifndef NDEBUG
612 DCHECK(!IsProcessingPaint()) << "Should not be removing a child view " <<
613 "during a paint, this will seriously " <<
614 "mess things up!";
615#endif
616 DCHECK(a_view);
617 const ViewList::iterator i = find(child_views_.begin(),
618 child_views_.end(),
619 a_view);
620 if (i != child_views_.end()) {
621 if (update_focus_cycle && !a_view->IsFloatingView()) {
622 // Let's remove the view from the focus traversal.
623 View* next_focusable = a_view->next_focusable_view_;
624 View* prev_focusable = a_view->previous_focusable_view_;
625 if (prev_focusable)
626 prev_focusable->next_focusable_view_ = next_focusable;
627 if (next_focusable)
628 next_focusable->previous_focusable_view_ = prev_focusable;
629 }
630
631 RootView* root = GetRootView();
632 if (root)
633 UnregisterChildrenForVisibleBoundsNotification(root, a_view);
634 a_view->PropagateRemoveNotifications(this);
635 a_view->SetParent(NULL);
636
637 if (delete_removed_view && a_view->IsParentOwned())
638 delete a_view;
639
640 child_views_.erase(i);
641 }
642
643 if (update_tool_tip)
644 UpdateTooltip();
645
646 if (layout_manager_.get())
647 layout_manager_->ViewRemoved(this, a_view);
648}
649
650void View::PropagateRemoveNotifications(View* parent) {
651 int i, c;
652 for (i = 0, c = GetChildViewCount(); i < c; ++i) {
653 GetChildViewAt(i)->PropagateRemoveNotifications(parent);
654 }
655
656 View *t;
657 for (t = this; t; t = t->GetParent()) {
658 t->ViewHierarchyChangedImpl(true, false, parent, this);
659 }
660}
661
662void View::PropagateAddNotifications(View* parent, View* child) {
663 int i, c;
664 for (i = 0, c = GetChildViewCount(); i < c; ++i) {
665 GetChildViewAt(i)->PropagateAddNotifications(parent, child);
666 }
667 ViewHierarchyChangedImpl(true, true, parent, child);
668}
669
670#ifndef NDEBUG
671bool View::IsProcessingPaint() const {
672 return GetParent() && GetParent()->IsProcessingPaint();
673}
674#endif
675
[email protected]82739cf2008-09-16 00:37:56676bool View::HasHitTestMask() const {
677 return false;
678}
679
680void View::GetHitTestMask(gfx::Path* mask) const {
681 DCHECK(mask);
682}
683
initial.commit09911bf2008-07-26 23:55:29684void View::ViewHierarchyChanged(bool is_add, View *parent, View *child) {
685}
686
687void View::ViewHierarchyChangedImpl(bool register_accelerators,
688 bool is_add, View *parent, View *child) {
689 if (register_accelerators) {
690 if (is_add) {
691 // If you get this registration, you are part of a subtree that has been
692 // added to the view hierarchy.
693 RegisterAccelerators();
694 } else {
695 if (child == this)
696 UnregisterAccelerators();
697 }
698 }
699
700 ViewHierarchyChanged(is_add, parent, child);
701}
702
703void View::PropagateVisibilityNotifications(View* start, bool is_visible) {
704 int i, c;
705 for (i = 0, c = GetChildViewCount(); i < c; ++i) {
706 GetChildViewAt(i)->PropagateVisibilityNotifications(start, is_visible);
707 }
708 VisibilityChanged(start, is_visible);
709}
710
711void View::VisibilityChanged(View* starting_from, bool is_visible) {
712}
713
[email protected]613b8062008-10-14 23:45:09714View* View::GetViewForPoint(const gfx::Point& point) {
initial.commit09911bf2008-07-26 23:55:29715 return GetViewForPoint(point, true);
716}
717
718void View::SetNotifyWhenVisibleBoundsInRootChanges(bool value) {
719 if (notify_when_visible_bounds_in_root_changes_ == value)
720 return;
721 notify_when_visible_bounds_in_root_changes_ = value;
722 RootView* root = GetRootView();
723 if (root) {
724 if (value)
725 root->RegisterViewForVisibleBoundsNotification(this);
726 else
727 root->UnregisterViewForVisibleBoundsNotification(this);
728 }
729}
730
731bool View::GetNotifyWhenVisibleBoundsInRootChanges() {
732 return notify_when_visible_bounds_in_root_changes_;
733}
734
[email protected]613b8062008-10-14 23:45:09735View* View::GetViewForPoint(const gfx::Point& point,
736 bool can_create_floating) {
initial.commit09911bf2008-07-26 23:55:29737 // Walk the child Views recursively looking for the View that most
738 // tightly encloses the specified point.
739 for (int i = GetChildViewCount() - 1 ; i >= 0 ; --i) {
740 View* child = GetChildViewAt(i);
[email protected]82739cf2008-09-16 00:37:56741 if (!child->IsVisible())
initial.commit09911bf2008-07-26 23:55:29742 continue;
[email protected]82739cf2008-09-16 00:37:56743
[email protected]96b667d2008-10-14 20:58:44744 gfx::Point point_in_child_coords(point);
[email protected]82739cf2008-09-16 00:37:56745 View::ConvertPointToView(this, child, &point_in_child_coords);
[email protected]613b8062008-10-14 23:45:09746 if (child->HitTest(point_in_child_coords))
747 return child->GetViewForPoint(point_in_child_coords, true);
initial.commit09911bf2008-07-26 23:55:29748 }
749
750 // We haven't found a view for the point. Try to create floating views
751 // and try again if one was created.
752 // can_create_floating makes sure we don't try forever even if
753 // GetFloatingViewIDForPoint lies or if RetrieveFloatingViewForID creates a
754 // view which doesn't contain the provided point
755 int id;
[email protected]613b8062008-10-14 23:45:09756 if (can_create_floating &&
757 GetFloatingViewIDForPoint(point.x(), point.y(), &id)) {
initial.commit09911bf2008-07-26 23:55:29758 RetrieveFloatingViewForID(id); // This creates the floating view.
759 return GetViewForPoint(point, false);
760 }
761 return this;
762}
763
[email protected]4d0bd102008-10-16 00:26:30764Container* View::GetContainer() const {
initial.commit09911bf2008-07-26 23:55:29765 // The root view holds a reference to this view hierarchy's container.
[email protected]4d0bd102008-10-16 00:26:30766 return parent_ ? parent_->GetContainer() : NULL;
initial.commit09911bf2008-07-26 23:55:29767}
768
769// Get the containing RootView
770RootView* View::GetRootView() {
[email protected]4d0bd102008-10-16 00:26:30771 Container* vc = GetContainer();
initial.commit09911bf2008-07-26 23:55:29772 if (vc) {
773 return vc->GetRootView();
774 } else {
775 return NULL;
776 }
777}
778
779View* View::GetViewByID(int id) const {
780 if (id == id_)
781 return const_cast<View*>(this);
782
783 int view_count = GetChildViewCount();
784 for (int i = 0; i < view_count; ++i) {
785 View* child = GetChildViewAt(i);
786 View* view = child->GetViewByID(id);
787 if (view)
788 return view;
789 }
790 return NULL;
791}
792
793void View::GetViewsWithGroup(int group_id, std::vector<View*>* out) {
794 if (group_ == group_id)
795 out->push_back(this);
796
797 int view_count = GetChildViewCount();
798 for (int i = 0; i < view_count; ++i)
799 GetChildViewAt(i)->GetViewsWithGroup(group_id, out);
800}
801
802View* View::GetSelectedViewForGroup(int group_id) {
803 std::vector<View*> views;
804 GetRootView()->GetViewsWithGroup(group_id, &views);
805 if (views.size() > 0)
806 return views[0];
807 else
808 return NULL;
809}
810
811void View::SetID(int id) {
812 id_ = id;
813}
814
815int View::GetID() const {
816 return id_;
817}
818
819void View::SetGroup(int gid) {
820 group_ = gid;
821}
822
823int View::GetGroup() const {
824 return group_;
825}
826
827void View::SetParent(View* parent) {
828 if (parent != parent_) {
829 parent_ = parent;
830 }
831}
832
833bool View::IsParentOf(View* v) const {
834 DCHECK(v);
835 View* parent = v->GetParent();
836 while (parent) {
837 if (this == parent)
838 return true;
839 parent = parent->GetParent();
840 }
841 return false;
842}
843
844int View::GetChildIndex(View* v) const {
845 for (int i = 0; i < GetChildViewCount(); i++) {
846 if (v == GetChildViewAt(i))
847 return i;
848 }
849 return -1;
850}
851
852///////////////////////////////////////////////////////////////////////////////
853//
854// View - focus
855//
856///////////////////////////////////////////////////////////////////////////////
857
858View* View::GetNextFocusableView() {
859 return next_focusable_view_;
860}
861
862View* View::GetPreviousFocusableView() {
863 return previous_focusable_view_;
864}
865
866void View::SetNextFocusableView(View* view) {
867 view->previous_focusable_view_ = this;
868 next_focusable_view_ = view;
869}
870
871void View::InitFocusSiblings(View* v, int index) {
872 int child_count = static_cast<int>(child_views_.size());
873
874 if (child_count == 0) {
875 v->next_focusable_view_ = NULL;
876 v->previous_focusable_view_ = NULL;
877 } else {
878 if (index == child_count) {
879 // We are inserting at the end, but the end of the child list may not be
880 // the last focusable element. Let's try to find an element with no next
881 // focusable element to link to.
882 View* last_focusable_view = NULL;
883 for (std::vector<View*>::iterator iter = child_views_.begin();
884 iter != child_views_.end(); ++iter) {
885 if (!(*iter)->next_focusable_view_) {
886 last_focusable_view = *iter;
887 break;
888 }
889 }
890 if (last_focusable_view == NULL) {
891 // Hum... there is a cycle in the focus list. Let's just insert ourself
892 // after the last child.
893 View* prev = child_views_[index - 1];
894 v->previous_focusable_view_ = prev;
895 v->next_focusable_view_ = prev->next_focusable_view_;
896 prev->next_focusable_view_->previous_focusable_view_ = v;
897 prev->next_focusable_view_ = v;
898 } else {
899 last_focusable_view->next_focusable_view_ = v;
900 v->next_focusable_view_ = NULL;
901 v->previous_focusable_view_ = last_focusable_view;
902 }
903 } else {
904 View* prev = child_views_[index]->GetPreviousFocusableView();
905 v->previous_focusable_view_ = prev;
906 v->next_focusable_view_ = child_views_[index];
907 if (prev)
908 prev->next_focusable_view_ = v;
909 child_views_[index]->previous_focusable_view_ = v;
910 }
911 }
912}
913
914#ifndef NDEBUG
915void View::PrintViewHierarchy() {
916 PrintViewHierarchyImp(0);
917}
918
919void View::PrintViewHierarchyImp(int indent) {
920 std::wostringstream buf;
921 int ind = indent;
922 while (ind-- > 0)
923 buf << L' ';
924 buf << UTF8ToWide(GetClassName());
925 buf << L' ';
926 buf << GetID();
927 buf << L' ';
[email protected]80f8b9f2008-10-16 18:17:47928 buf << bounds_.x() << L"," << bounds_.y() << L",";
929 buf << bounds_.right() << L"," << bounds_.bottom();
initial.commit09911bf2008-07-26 23:55:29930 buf << L' ';
931 buf << this;
932
933 LOG(INFO) << buf.str();
934 std::cout << buf.str() << std::endl;
935
936 for (int i = 0; i < GetChildViewCount(); ++i) {
937 GetChildViewAt(i)->PrintViewHierarchyImp(indent + 2);
938 }
939}
940
941
942void View::PrintFocusHierarchy() {
943 PrintFocusHierarchyImp(0);
944}
945
946void View::PrintFocusHierarchyImp(int indent) {
947 std::wostringstream buf;
948 int ind = indent;
949 while (ind-- > 0)
950 buf << L' ';
951 buf << UTF8ToWide(GetClassName());
952 buf << L' ';
953 buf << GetID();
954 buf << L' ';
955 buf << GetClassName().c_str();
956 buf << L' ';
957 buf << this;
958
959 LOG(INFO) << buf.str();
960 std::cout << buf.str() << std::endl;
961
962 if (GetChildViewCount() > 0)
963 GetChildViewAt(0)->PrintFocusHierarchyImp(indent + 2);
964
965 View* v = GetNextFocusableView();
966 if (v)
967 v->PrintFocusHierarchyImp(indent);
968}
969#endif
970
971////////////////////////////////////////////////////////////////////////////////
972//
973// View - accelerators
974//
975////////////////////////////////////////////////////////////////////////////////
976
977void View::AddAccelerator(const Accelerator& accelerator) {
978 if (!accelerators_.get())
979 accelerators_.reset(new std::vector<Accelerator>());
980 accelerators_->push_back(accelerator);
981 RegisterAccelerators();
982}
983
984void View::ResetAccelerators() {
985 if (accelerators_.get()) {
986 UnregisterAccelerators();
987 accelerators_->clear();
988 accelerators_.reset();
989 }
990}
991
992void View::RegisterAccelerators() {
993 if (!accelerators_.get())
994 return;
995
996 RootView* root_view = GetRootView();
997 if (!root_view) {
998 // We are not yet part of a view hierarchy, we'll register ourselves once
999 // added to one.
1000 return;
1001 }
1002 FocusManager* focus_manager = GetFocusManager();
1003 if (!focus_manager) {
1004 // Some crash reports seem to show that we may get cases where we have no
1005 // focus manager (see bug #1291225). This should never be the case, just
1006 // making sure we don't crash.
1007 NOTREACHED();
1008 return;
1009 }
1010 for (std::vector<Accelerator>::const_iterator iter = accelerators_->begin();
1011 iter != accelerators_->end(); ++iter) {
1012 focus_manager->RegisterAccelerator(*iter, this);
1013 }
1014}
1015
1016void View::UnregisterAccelerators() {
1017 if (!accelerators_.get())
1018 return;
1019
1020 RootView* root_view = GetRootView();
1021 if (root_view) {
1022 FocusManager* focus_manager = GetFocusManager();
1023 if (focus_manager) {
1024 // We may not have a FocusManager if the window containing us is being
1025 // closed, in which case the FocusManager is being deleted so there is
1026 // nothing to unregister.
1027 focus_manager->UnregisterAccelerators(this);
1028 }
1029 }
1030}
1031
1032/////////////////////////////////////////////////////////////////////////////
1033//
1034// View - accessibility
1035//
1036/////////////////////////////////////////////////////////////////////////////
1037
1038AccessibleWrapper* View::GetAccessibleWrapper() {
1039 if (accessibility_.get() == NULL) {
1040 accessibility_.reset(new AccessibleWrapper(this));
1041 }
1042 return accessibility_.get();
1043}
1044
1045/////////////////////////////////////////////////////////////////////////////
1046//
1047// View - floating views
1048//
1049/////////////////////////////////////////////////////////////////////////////
1050
1051bool View::IsFloatingView() {
1052 if (!parent_)
1053 return false;
1054
1055 return parent_->floating_views_ids_.find(this) !=
1056 parent_->floating_views_ids_.end();
1057}
1058
1059// default implementation does nothing
1060bool View::GetFloatingViewIDForPoint(int x, int y, int* id) {
1061 return false;
1062}
1063
1064int View::GetFloatingViewCount() const {
1065 return static_cast<int>(floating_views_.size());
1066}
1067
1068View* View::RetrieveFloatingViewParent() {
1069 View* v = this;
1070 while (v) {
1071 if (v->IsFloatingView())
1072 return v;
1073 v = v->GetParent();
1074 }
1075 return NULL;
1076}
1077
1078bool View::EnumerateFloatingViews(FloatingViewPosition position,
1079 int starting_id, int* id) {
1080 return false;
1081}
1082
1083int View::GetDragOperations(int press_x, int press_y) {
1084 if (!drag_controller_)
1085 return DragDropTypes::DRAG_NONE;
1086 return drag_controller_->GetDragOperations(this, press_x, press_y);
1087}
1088
1089void View::WriteDragData(int press_x, int press_y, OSExchangeData* data) {
1090 DCHECK(drag_controller_);
1091 drag_controller_->WriteDragData(this, press_x, press_y, data);
1092}
1093
1094void View::OnDragDone() {
1095}
1096
1097bool View::InDrag() {
1098 RootView* root_view = GetRootView();
1099 return root_view ? (root_view->GetDragView() == this) : false;
1100}
1101
1102View* View::ValidateFloatingViewForID(int id) {
1103 return NULL;
1104}
1105
1106bool View::ShouldRestoreFloatingViewFocus() {
1107 return true;
1108}
1109
1110void View::AttachFloatingView(View* v, int id) {
1111 floating_views_.push_back(v);
1112 floating_views_ids_[v] = id;
1113 AddChildView(static_cast<int>(child_views_.size()), v, true);
1114}
1115
1116bool View::HasFloatingViewForPoint(int x, int y) {
1117 int i, c;
1118 View* v;
1119 gfx::Rect r;
1120
1121 for (i = 0, c = static_cast<int>(floating_views_.size()); i < c; ++i) {
1122 v = floating_views_[i];
[email protected]6f3bb6c2008-09-17 22:25:331123 r.SetRect(v->GetX(APPLY_MIRRORING_TRANSFORMATION), v->y(),
1124 v->width(), v->height());
initial.commit09911bf2008-07-26 23:55:291125 if (r.Contains(x, y))
1126 return true;
1127 }
1128 return false;
1129}
1130
1131void View::DetachAllFloatingViews() {
1132 RootView* root_view = GetRootView();
1133 View* focused_view = NULL;
1134 FocusManager* focus_manager = NULL;
1135 if (root_view) {
1136 // We may be called when we are not attached to a root view in which case
1137 // there is nothing to do for focus.
1138 focus_manager = GetFocusManager();
1139 if (focus_manager) {
1140 // We may not have a focus manager (if we are detached from a top window).
1141 focused_view = focus_manager->GetFocusedView();
1142 }
1143 }
1144
1145 int c = static_cast<int>(floating_views_.size());
1146 while (--c >= 0) {
1147 // If the focused view is a floating view or a floating view's children,
1148 // use the focus manager to store it.
1149 int tmp_id;
1150 if (focused_view &&
1151 ((focused_view == floating_views_[c]) ||
1152 floating_views_[c]->IsParentOf(focused_view))) {
1153 // We call EnumerateFloatingView to make sure the floating view is still
1154 // valid: the model may have changed and could not know anything about
1155 // that floating view anymore.
1156 if (EnumerateFloatingViews(CURRENT,
1157 floating_views_[c]->GetFloatingViewID(),
1158 &tmp_id)) {
1159 focus_manager->StoreFocusedView();
1160 should_restore_focus_ = true;
1161 }
1162 focused_view = NULL;
1163 }
1164
1165 RemoveChildView(floating_views_[c]);
1166 delete floating_views_[c];
1167 }
1168 floating_views_.clear();
1169 floating_views_ids_.clear();
1170}
1171
1172int View::GetFloatingViewID() {
1173 DCHECK(IsFloatingView());
1174 std::map<View*, int>::iterator iter = parent_->floating_views_ids_.find(this);
1175 DCHECK(iter != parent_->floating_views_ids_.end());
1176 return iter->second;
1177}
1178
1179View* View::RetrieveFloatingViewForID(int id) {
1180 for (ViewList::const_iterator iter = floating_views_.begin();
1181 iter != floating_views_.end(); ++iter) {
1182 if ((*iter)->GetFloatingViewID() == id)
1183 return *iter;
1184 }
1185 return ValidateFloatingViewForID(id);
1186}
1187
1188void View::RestoreFloatingViewFocus() {
1189 // Clear the reference to the task as if we have been triggered by it, it will
1190 // soon be invalid.
1191 restore_focus_view_task_ = NULL;
1192 should_restore_focus_ = false;
1193
1194 GetFocusManager()->RestoreFocusedView();
1195}
1196
1197// static
1198bool View::EnumerateFloatingViewsForInterval(int low_bound, int high_bound,
1199 bool ascending_order,
1200 FloatingViewPosition position,
1201 int starting_id,
1202 int* id) {
1203 DCHECK(low_bound <= high_bound);
1204 if (low_bound >= high_bound)
1205 return false;
1206
1207 switch (position) {
1208 case CURRENT:
1209 if ((starting_id >= low_bound) && (starting_id < high_bound)) {
1210 *id = starting_id;
1211 return true;
1212 }
1213 return false;
1214 case FIRST:
1215 *id = ascending_order ? low_bound : high_bound - 1;
1216 return true;
1217 case LAST:
1218 *id = ascending_order ? high_bound - 1 : low_bound;
1219 return true;
1220 case NEXT:
1221 case PREVIOUS:
1222 if (((position == NEXT) && ascending_order) ||
1223 ((position == PREVIOUS) && !ascending_order)) {
1224 starting_id++;
1225 if (starting_id < high_bound) {
1226 *id = starting_id;
1227 return true;
1228 }
1229 return false;
1230 }
1231 DCHECK(((position == NEXT) && !ascending_order) ||
1232 ((position == PREVIOUS) && ascending_order));
1233 starting_id--;
1234 if (starting_id >= low_bound) {
1235 *id = starting_id;
1236 return true;
1237 }
1238 return false;
1239 default:
1240 NOTREACHED();
1241 }
1242 return false;
1243}
1244
1245// static
[email protected]96b667d2008-10-14 20:58:441246void View::ConvertPointToView(View* src, View* dst, gfx::Point* point) {
initial.commit09911bf2008-07-26 23:55:291247 ConvertPointToView(src, dst, point, true);
1248}
1249
1250// static
[email protected]96b667d2008-10-14 20:58:441251void View::ConvertPointToView(View* src, View* dst, gfx::Point* point,
initial.commit09911bf2008-07-26 23:55:291252 bool try_other_direction) {
1253 // src can be NULL
1254 DCHECK(dst);
1255 DCHECK(point);
1256
1257 View* v;
1258 gfx::Point offset;
1259
1260 for (v = dst; v && v != src; v = v->GetParent()) {
1261 offset.SetPoint(offset.x() + v->GetX(APPLY_MIRRORING_TRANSFORMATION),
[email protected]6f3bb6c2008-09-17 22:25:331262 offset.y() + v->y());
initial.commit09911bf2008-07-26 23:55:291263 }
1264
1265 // The source was not found. The caller wants a conversion
1266 // from a view to a transitive parent.
1267 if (src && v == NULL && try_other_direction) {
1268 gfx::Point p;
1269 // note: try_other_direction is force to FALSE so we don't
1270 // end up in an infinite recursion should both src and dst
1271 // are not parented.
1272 ConvertPointToView(dst, src, &p, false);
1273 // since the src and dst are inverted, p should also be negated
1274 point->SetPoint(point->x() - p.x(), point->y() - p.y());
1275 } else {
1276 point->SetPoint(point->x() - offset.x(), point->y() - offset.y());
1277
1278 // If src is NULL, sp is in the screen coordinate system
1279 if (src == NULL) {
[email protected]4d0bd102008-10-16 00:26:301280 Container* vc = dst->GetContainer();
initial.commit09911bf2008-07-26 23:55:291281 if (vc) {
1282 CRect b;
1283 vc->GetBounds(&b, false);
1284 point->SetPoint(point->x() - b.left, point->y() - b.top);
1285 }
1286 }
1287 }
1288}
1289
1290// static
[email protected]4d0bd102008-10-16 00:26:301291void View::ConvertPointToContainer(View* src, gfx::Point* p) {
initial.commit09911bf2008-07-26 23:55:291292 DCHECK(src);
1293 DCHECK(p);
[email protected]96b667d2008-10-14 20:58:441294
initial.commit09911bf2008-07-26 23:55:291295 View *v;
[email protected]96b667d2008-10-14 20:58:441296 gfx::Point offset;
initial.commit09911bf2008-07-26 23:55:291297 for (v = src; v; v = v->GetParent()) {
[email protected]96b667d2008-10-14 20:58:441298 offset.set_x(offset.x() + v->GetX(APPLY_MIRRORING_TRANSFORMATION));
1299 offset.set_y(offset.y() + v->y());
initial.commit09911bf2008-07-26 23:55:291300 }
[email protected]96b667d2008-10-14 20:58:441301 p->SetPoint(p->x() + offset.x(), p->y() + offset.y());
initial.commit09911bf2008-07-26 23:55:291302}
1303
1304// static
[email protected]4d0bd102008-10-16 00:26:301305void View::ConvertPointFromContainer(View *source, gfx::Point* p) {
[email protected]96b667d2008-10-14 20:58:441306 gfx::Point t;
[email protected]4d0bd102008-10-16 00:26:301307 ConvertPointToContainer(source, &t);
[email protected]96b667d2008-10-14 20:58:441308 p->SetPoint(p->x() - t.x(), p->y() - t.y());
initial.commit09911bf2008-07-26 23:55:291309}
1310
1311// static
[email protected]96b667d2008-10-14 20:58:441312void View::ConvertPointToScreen(View* src, gfx::Point* p) {
initial.commit09911bf2008-07-26 23:55:291313 DCHECK(src);
1314 DCHECK(p);
1315
[email protected]96b667d2008-10-14 20:58:441316 // If the view is not connected to a tree, there's nothing we can do.
[email protected]4d0bd102008-10-16 00:26:301317 Container* vc = src->GetContainer();
initial.commit09911bf2008-07-26 23:55:291318 if (vc) {
[email protected]4d0bd102008-10-16 00:26:301319 ConvertPointToContainer(src, p);
initial.commit09911bf2008-07-26 23:55:291320 CRect r;
1321 vc->GetBounds(&r, false);
[email protected]96b667d2008-10-14 20:58:441322 p->SetPoint(p->x() + r.left, p->y() + r.top);
initial.commit09911bf2008-07-26 23:55:291323 }
1324}
1325
1326/////////////////////////////////////////////////////////////////////////////
1327//
1328// View - event handlers
1329//
1330/////////////////////////////////////////////////////////////////////////////
1331
1332bool View::OnMousePressed(const MouseEvent& e) {
1333 return false;
1334}
1335
1336bool View::OnMouseDragged(const MouseEvent& e) {
1337 return false;
1338}
1339
1340void View::OnMouseReleased(const MouseEvent& e, bool canceled) {
1341}
1342
1343void View::OnMouseMoved(const MouseEvent& e) {
1344}
1345
1346void View::OnMouseEntered(const MouseEvent& e) {
1347}
1348
1349void View::OnMouseExited(const MouseEvent& e) {
1350}
1351
1352void View::SetMouseHandler(View *new_mouse_handler) {
1353 // It is valid for new_mouse_handler to be NULL
1354 if (parent_) {
1355 parent_->SetMouseHandler(new_mouse_handler);
1356 }
1357}
1358
1359void View::SetVisible(bool flag) {
1360 if (flag != is_visible_) {
1361 // If the tab is currently visible, schedule paint to
1362 // refresh parent
1363 if (IsVisible()) {
1364 SchedulePaint();
1365 }
1366
1367 is_visible_ = flag;
1368
1369 // This notifies all subviews recursively.
1370 PropagateVisibilityNotifications(this, flag);
1371
1372 // If we are newly visible, schedule paint.
1373 if (IsVisible()) {
1374 SchedulePaint();
1375 }
1376 }
1377}
1378
1379bool View::IsVisibleInRootView() const {
1380 View* parent = GetParent();
1381 if (IsVisible() && parent)
1382 return parent->IsVisibleInRootView();
1383 else
1384 return false;
1385}
1386
[email protected]613b8062008-10-14 23:45:091387bool View::HitTest(const gfx::Point& l) const {
1388 if (l.x() >= 0 && l.x() < static_cast<int>(width()) &&
1389 l.y() >= 0 && l.y() < static_cast<int>(height())) {
[email protected]82739cf2008-09-16 00:37:561390 if (HasHitTestMask()) {
1391 gfx::Path mask;
1392 GetHitTestMask(&mask);
1393 ScopedHRGN rgn(mask.CreateHRGN());
[email protected]613b8062008-10-14 23:45:091394 return !!PtInRegion(rgn, l.x(), l.y());
[email protected]82739cf2008-09-16 00:37:561395 }
1396 // No mask, but inside our bounds.
initial.commit09911bf2008-07-26 23:55:291397 return true;
initial.commit09911bf2008-07-26 23:55:291398 }
[email protected]82739cf2008-09-16 00:37:561399 // Outside our bounds.
1400 return false;
initial.commit09911bf2008-07-26 23:55:291401}
1402
1403HCURSOR View::GetCursorForPoint(Event::EventType event_type, int x, int y) {
1404 return NULL;
1405}
1406
1407/////////////////////////////////////////////////////////////////////////////
1408//
1409// View - keyboard and focus
1410//
1411/////////////////////////////////////////////////////////////////////////////
1412
1413void View::RequestFocus() {
1414 RootView* rv = GetRootView();
1415 if (rv) {
1416 rv->FocusView(this);
1417 }
1418}
1419
1420void View::WillGainFocus() {
1421}
1422
1423void View::DidGainFocus() {
1424}
1425
1426void View::WillLoseFocus() {
1427}
1428
1429bool View::OnKeyPressed(const KeyEvent& e) {
1430 return false;
1431}
1432
1433bool View::OnKeyReleased(const KeyEvent& e) {
1434 return false;
1435}
1436
1437bool View::OnMouseWheel(const MouseWheelEvent& e) {
1438 return false;
1439}
1440
1441void View::SetDragController(DragController* drag_controller) {
1442 drag_controller_ = drag_controller;
1443}
1444
1445DragController* View::GetDragController() {
1446 return drag_controller_;
1447}
1448
1449bool View::CanDrop(const OSExchangeData& data) {
1450 return false;
1451}
1452
1453void View::OnDragEntered(const DropTargetEvent& event) {
1454}
1455
1456int View::OnDragUpdated(const DropTargetEvent& event) {
1457 return DragDropTypes::DRAG_NONE;
1458}
1459
1460void View::OnDragExited() {
1461}
1462
1463int View::OnPerformDrop(const DropTargetEvent& event) {
1464 return DragDropTypes::DRAG_NONE;
1465}
1466
1467static int GetHorizontalDragThreshold() {
1468 static int threshold = -1;
1469 if (threshold == -1)
1470 threshold = GetSystemMetrics(SM_CXDRAG) / 2;
1471 return threshold;
1472}
1473
1474static int GetVerticalDragThreshold() {
1475 static int threshold = -1;
1476 if (threshold == -1)
1477 threshold = GetSystemMetrics(SM_CYDRAG) / 2;
1478 return threshold;
1479}
1480
1481// static
1482bool View::ExceededDragThreshold(int delta_x, int delta_y) {
1483 return (abs(delta_x) > GetHorizontalDragThreshold() ||
1484 abs(delta_y) > GetVerticalDragThreshold());
1485}
1486
1487void View::Focus() {
1488 // Set the native focus to the root view window so it receives the keyboard
1489 // messages.
1490 FocusManager* focus_manager = GetFocusManager();
1491 if (focus_manager)
[email protected]4d0bd102008-10-16 00:26:301492 focus_manager->FocusHWND(GetRootView()->GetContainer()->GetHWND());
initial.commit09911bf2008-07-26 23:55:291493}
1494
1495bool View::CanProcessTabKeyEvents() {
1496 return false;
1497}
1498
1499// Tooltips -----------------------------------------------------------------
1500bool View::GetTooltipText(int x, int y, std::wstring* tooltip) {
1501 return false;
1502}
1503
[email protected]0a1d36b22008-10-17 19:33:091504bool View::GetTooltipTextOrigin(int x, int y, gfx::Point* loc) {
initial.commit09911bf2008-07-26 23:55:291505 return false;
1506}
1507
1508void View::TooltipTextChanged() {
[email protected]4d0bd102008-10-16 00:26:301509 Container* view_container = GetContainer();
initial.commit09911bf2008-07-26 23:55:291510 if (view_container != NULL && view_container->GetTooltipManager())
1511 view_container->GetTooltipManager()->TooltipTextChanged(this);
1512}
1513
1514void View::UpdateTooltip() {
[email protected]4d0bd102008-10-16 00:26:301515 Container* view_container = GetContainer();
initial.commit09911bf2008-07-26 23:55:291516 if (view_container != NULL && view_container->GetTooltipManager())
1517 view_container->GetTooltipManager()->UpdateTooltip();
1518}
1519
1520void View::SetParentOwned(bool f) {
1521 is_parent_owned_ = f;
1522}
1523
1524bool View::IsParentOwned() const {
1525 return is_parent_owned_;
1526}
1527
1528std::string View::GetClassName() const {
1529 return kViewClassName;
1530}
1531
1532gfx::Rect View::GetVisibleBounds() {
[email protected]6f3bb6c2008-09-17 22:25:331533 gfx::Rect vis_bounds(0, 0, width(), height());
initial.commit09911bf2008-07-26 23:55:291534 gfx::Rect ancestor_bounds;
1535 View* view = this;
1536 int root_x = 0;
1537 int root_y = 0;
1538 bool has_view_container = false;
1539 while (view != NULL && !vis_bounds.IsEmpty()) {
1540 root_x += view->GetX(APPLY_MIRRORING_TRANSFORMATION);
[email protected]6f3bb6c2008-09-17 22:25:331541 root_y += view->y();
1542 vis_bounds.Offset(view->GetX(APPLY_MIRRORING_TRANSFORMATION), view->y());
initial.commit09911bf2008-07-26 23:55:291543 View* ancestor = view->GetParent();
1544 if (ancestor != NULL) {
[email protected]6f3bb6c2008-09-17 22:25:331545 ancestor_bounds.SetRect(0, 0, ancestor->width(),
1546 ancestor->height());
initial.commit09911bf2008-07-26 23:55:291547 vis_bounds = vis_bounds.Intersect(ancestor_bounds);
[email protected]4d0bd102008-10-16 00:26:301548 } else if (!view->GetContainer()) {
1549 // If the view has no Container, we're not visible. Return an empty
initial.commit09911bf2008-07-26 23:55:291550 // rect.
1551 return gfx::Rect();
1552 }
1553 view = ancestor;
1554 }
1555 if (vis_bounds.IsEmpty())
1556 return vis_bounds;
1557 // Convert back to this views coordinate system.
1558 vis_bounds.Offset(-root_x, -root_y);
1559 return vis_bounds;
1560}
1561
1562int View::GetPageScrollIncrement(ScrollView* scroll_view,
1563 bool is_horizontal, bool is_positive) {
1564 return 0;
1565}
1566
1567int View::GetLineScrollIncrement(ScrollView* scroll_view,
1568 bool is_horizontal, bool is_positive) {
1569 return 0;
1570}
1571
1572// static
1573void View::RegisterChildrenForVisibleBoundsNotification(
1574 RootView* root, View* view) {
1575 DCHECK(root && view);
1576 if (view->GetNotifyWhenVisibleBoundsInRootChanges())
1577 root->RegisterViewForVisibleBoundsNotification(view);
1578 for (int i = 0; i < view->GetChildViewCount(); ++i)
1579 RegisterChildrenForVisibleBoundsNotification(root, view->GetChildViewAt(i));
1580}
1581
1582// static
1583void View::UnregisterChildrenForVisibleBoundsNotification(
1584 RootView* root, View* view) {
1585 DCHECK(root && view);
1586 if (view->GetNotifyWhenVisibleBoundsInRootChanges())
1587 root->UnregisterViewForVisibleBoundsNotification(view);
1588 for (int i = 0; i < view->GetChildViewCount(); ++i)
1589 UnregisterChildrenForVisibleBoundsNotification(root,
1590 view->GetChildViewAt(i));
1591}
1592
1593void View::AddDescendantToNotify(View* view) {
1594 DCHECK(view);
1595 if (!descendants_to_notify_.get())
1596 descendants_to_notify_.reset(new ViewList());
1597 descendants_to_notify_->push_back(view);
1598}
1599
1600void View::RemoveDescendantToNotify(View* view) {
1601 DCHECK(view && descendants_to_notify_.get());
1602 ViewList::iterator i = find(descendants_to_notify_->begin(),
1603 descendants_to_notify_->end(),
1604 view);
1605 DCHECK(i != descendants_to_notify_->end());
1606 descendants_to_notify_->erase(i);
1607 if (descendants_to_notify_->empty())
1608 descendants_to_notify_.reset();
1609}
1610
1611// static
1612bool View::GetViewPath(View* start, View* end, std::vector<int>* path) {
1613 while (end && (end != start)) {
1614 View* parent = end->GetParent();
1615 if (!parent)
1616 return false;
1617 path->insert(path->begin(), parent->GetChildIndex(end));
1618 end = parent;
1619 }
1620 return end == start;
1621}
1622
1623// static
1624View* View::GetViewForPath(View* start, const std::vector<int>& path) {
1625 View* v = start;
1626 for (std::vector<int>::const_iterator iter = path.begin();
1627 iter != path.end(); ++iter) {
1628 int index = *iter;
1629 if (index >= v->GetChildViewCount())
1630 return NULL;
1631 v = v->GetChildViewAt(index);
1632 }
1633 return v;
1634}
1635
1636// DropInfo --------------------------------------------------------------------
1637
1638void View::DragInfo::Reset() {
1639 possible_drag = false;
1640 start_x = start_y = 0;
1641}
1642
1643void View::DragInfo::PossibleDrag(int x, int y) {
1644 possible_drag = true;
1645 start_x = x;
1646 start_y = y;
1647}
1648
1649} // namespace
license.botbf09a502008-08-24 00:55:551650