1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "touch_autocomplete_popup_contents_view.h"
6
7#include "chrome/browser/autocomplete/autocomplete_edit_view.h"
8#include "chrome/browser/profiles/profile.h"
9#include "ui/gfx/canvas.h"
10#include "ui/gfx/canvas_skia.h"
11#include "ui/gfx/font.h"
12#include "ui/gfx/path.h"
13#include "ui/gfx/rect.h"
14#include "ui/gfx/size.h"
15#include "third_party/skia/include/core/SkPaint.h"
16#include "views/view.h"
17
18
19// TouchAutocompleteResultView ------------------------------------------------
20
21TouchAutocompleteResultView::TouchAutocompleteResultView(
22    AutocompleteResultViewModel* model,
23    int model_index,
24    const gfx::Font& font,
25    const gfx::Font& bold_font)
26    : AutocompleteResultView(model, model_index, font, bold_font) {
27}
28
29TouchAutocompleteResultView::~TouchAutocompleteResultView() {
30}
31
32void TouchAutocompleteResultView::PaintMatch(gfx::Canvas* canvas,
33                                             const AutocompleteMatch& match,
34                                             int x) {
35  DrawString(canvas, match.contents, match.contents_class, false, x,
36             text_bounds().y());
37
38  if (!match.description.empty()) {
39    DrawString(canvas, match.description, match.description_class, true, x,
40        text_bounds().y() + GetFontHeight());
41  }
42}
43
44int TouchAutocompleteResultView::GetFontHeight() const {
45  // In touch version of autocomplete popup, the text is displayed in two lines:
46  // First line is the title of the suggestion and second is the description.
47  // Hence, the total text height is 2 times the height of one line.
48  return AutocompleteResultView::GetFontHeight() * 2;
49}
50
51
52// TouchAutocompletePopupContentsView -----------------------------------------
53
54TouchAutocompletePopupContentsView::TouchAutocompletePopupContentsView(
55    const gfx::Font& font,
56    AutocompleteEditView* edit_view,
57    AutocompleteEditModel* edit_model,
58    Profile* profile,
59    const views::View* location_bar)
60    : AutocompletePopupContentsView(font, edit_view, edit_model, profile,
61                                    location_bar) {
62}
63
64TouchAutocompletePopupContentsView::~TouchAutocompletePopupContentsView() {
65}
66
67void TouchAutocompletePopupContentsView::UpdatePopupAppearance() {
68  AutocompletePopupContentsView::UpdatePopupAppearance();
69  Layout();
70}
71
72void TouchAutocompletePopupContentsView::LayoutChildren() {
73  std::vector<View*> visible_children(GetVisibleChildren());
74  gfx::Rect bounds(GetContentsBounds());
75  double child_width =
76      static_cast<double>(bounds.width()) / visible_children.size();
77  int x = bounds.x();
78  for (size_t i = 0; i < visible_children.size(); ++i) {
79    int next_x = bounds.x() + static_cast<int>(((i + 1) * child_width) + 0.5);
80    visible_children[i]->SetBounds(x, bounds.y(), next_x - x, bounds.height());
81    x = next_x;
82  }
83}
84
85void TouchAutocompletePopupContentsView::PaintResultViews(
86    gfx::CanvasSkia* canvas) {
87  AutocompletePopupContentsView::PaintResultViews(canvas);
88
89  // Draw divider lines.
90  std::vector<View*> visible_children(GetVisibleChildren());
91  if (visible_children.size() < 2)
92    return;
93  SkColor color = AutocompleteResultView::GetColor(
94      AutocompleteResultView::NORMAL, AutocompleteResultView::DIMMED_TEXT);
95  gfx::Rect bounds(GetContentsBounds());
96  for (std::vector<View*>::const_iterator i(visible_children.begin() + 1);
97       i != visible_children.end(); ++i) {
98    canvas->DrawLineInt(color, (*i)->x(), bounds.y(), (*i)->x(),
99                        bounds.bottom());
100  }
101}
102
103int TouchAutocompletePopupContentsView::CalculatePopupHeight() {
104  DCHECK_GE(static_cast<size_t>(child_count()), model_->result().size());
105  int popup_height = 0;
106  for (size_t i = 0; i < model_->result().size(); ++i) {
107    popup_height = std::max(popup_height,
108                            GetChildViewAt(i)->GetPreferredSize().height());
109  }
110  popup_height = std::max(popup_height, opt_in_view_ ?
111      opt_in_view_->GetPreferredSize().height() : 0);
112  return popup_height;
113}
114
115AutocompleteResultView* TouchAutocompletePopupContentsView::CreateResultView(
116    AutocompleteResultViewModel* model,
117    int model_index,
118    const gfx::Font& font,
119    const gfx::Font& bold_font) {
120  return new TouchAutocompleteResultView(model, model_index, font, bold_font);
121}
122
123std::vector<views::View*>
124    TouchAutocompletePopupContentsView::GetVisibleChildren() {
125  std::vector<View*> visible_children;
126  for (int i = 0; i < child_count(); ++i) {
127    View* v = GetChildViewAt(i);
128    if (GetChildViewAt(i)->IsVisible())
129      visible_children.push_back(v);
130  }
131  return visible_children;
132}
133