autofill_popup_view_views.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/ui/views/autofill/autofill_popup_view_views.h"
6
7#include "chrome/browser/ui/autofill/autofill_popup_controller.h"
8#include "grit/ui_resources.h"
9#include "third_party/WebKit/public/web/WebAutofillClient.h"
10#include "ui/base/resource/resource_bundle.h"
11#include "ui/events/keycodes/keyboard_codes.h"
12#include "ui/gfx/canvas.h"
13#include "ui/gfx/image/image.h"
14#include "ui/gfx/native_widget_types.h"
15#include "ui/gfx/point.h"
16#include "ui/gfx/rect.h"
17#include "ui/gfx/text_utils.h"
18#include "ui/views/border.h"
19#include "ui/views/widget/widget.h"
20
21using blink::WebAutofillClient;
22
23namespace autofill {
24
25AutofillPopupViewViews::AutofillPopupViewViews(
26    AutofillPopupController* controller, views::Widget* observing_widget)
27    : AutofillPopupBaseView(controller, observing_widget),
28      controller_(controller) {}
29
30AutofillPopupViewViews::~AutofillPopupViewViews() {}
31
32void AutofillPopupViewViews::Show() {
33  DoShow();
34}
35
36void AutofillPopupViewViews::Hide() {
37  // The controller is no longer valid after it hides us.
38  controller_ = NULL;
39
40  DoHide();
41}
42
43void AutofillPopupViewViews::UpdateBoundsAndRedrawPopup() {
44  DoUpdateBoundsAndRedrawPopup();
45}
46
47void AutofillPopupViewViews::OnPaint(gfx::Canvas* canvas) {
48  if (!controller_)
49    return;
50
51  canvas->DrawColor(kPopupBackground);
52  OnPaintBorder(canvas);
53
54  for (size_t i = 0; i < controller_->names().size(); ++i) {
55    gfx::Rect line_rect = controller_->GetRowBounds(i);
56
57    if (controller_->identifiers()[i] ==
58            WebAutofillClient::MenuItemIDSeparator) {
59      canvas->DrawRect(line_rect, kItemTextColor);
60    } else {
61      DrawAutofillEntry(canvas, i, line_rect);
62    }
63  }
64}
65
66void AutofillPopupViewViews::InvalidateRow(size_t row) {
67  SchedulePaintInRect(controller_->GetRowBounds(row));
68}
69
70void AutofillPopupViewViews::DrawAutofillEntry(gfx::Canvas* canvas,
71                                               int index,
72                                               const gfx::Rect& entry_rect) {
73  if (controller_->selected_line() == index)
74    canvas->FillRect(entry_rect, kHoveredBackgroundColor);
75
76  const bool is_rtl = controller_->IsRTL();
77  const int value_text_width =
78      gfx::GetStringWidth(controller_->names()[index],
79                          controller_->GetNameFontListForRow(index));
80  const int value_content_x = is_rtl ?
81      entry_rect.width() - value_text_width - kEndPadding : kEndPadding;
82
83  canvas->DrawStringRectWithFlags(
84      controller_->names()[index],
85      controller_->GetNameFontListForRow(index),
86      controller_->IsWarning(index) ? kWarningTextColor : kValueTextColor,
87      gfx::Rect(value_content_x,
88                entry_rect.y(),
89                value_text_width,
90                entry_rect.height()),
91      gfx::Canvas::TEXT_ALIGN_CENTER);
92
93  // Use this to figure out where all the other Autofill items should be placed.
94  int x_align_left = is_rtl ? kEndPadding : entry_rect.width() - kEndPadding;
95
96  // Draw the Autofill icon, if one exists
97  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
98  int row_height = controller_->GetRowBounds(index).height();
99  if (!controller_->icons()[index].empty()) {
100    int icon = controller_->GetIconResourceID(controller_->icons()[index]);
101    DCHECK_NE(-1, icon);
102    const gfx::ImageSkia* image = rb.GetImageSkiaNamed(icon);
103    int icon_y = entry_rect.y() + (row_height - image->height()) / 2;
104
105    x_align_left += is_rtl ? 0 : -image->width();
106
107    canvas->DrawImageInt(*image, x_align_left, icon_y);
108
109    x_align_left += is_rtl ? image->width() + kIconPadding : -kIconPadding;
110  }
111
112  // Draw the name text.
113  const int subtext_width =
114      gfx::GetStringWidth(controller_->subtexts()[index],
115                          controller_->subtext_font_list());
116  if (!is_rtl)
117    x_align_left -= subtext_width;
118
119  canvas->DrawStringRectWithFlags(
120      controller_->subtexts()[index],
121      controller_->subtext_font_list(),
122      kItemTextColor,
123      gfx::Rect(x_align_left,
124                entry_rect.y(),
125                subtext_width,
126                entry_rect.height()),
127      gfx::Canvas::TEXT_ALIGN_CENTER);
128}
129
130AutofillPopupView* AutofillPopupView::Create(
131    AutofillPopupController* controller) {
132  views::Widget* observing_widget =
133      views::Widget::GetTopLevelWidgetForNativeView(
134          controller->container_view());
135
136  // If the top level widget can't be found, cancel the popup since we can't
137  // fully set it up.
138  if (!observing_widget)
139    return NULL;
140
141  return new AutofillPopupViewViews(controller, observing_widget);
142}
143
144}  // namespace autofill
145