- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / omnibox / touch_omnibox_popup_contents_view.cc
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/omnibox/touch_omnibox_popup_contents_view.h"
6
7 #include "chrome/browser/ui/omnibox/omnibox_view.h"
8 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
9 #include "third_party/skia/include/core/SkPaint.h"
10 #include "ui/gfx/canvas.h"
11 #include "ui/gfx/font_list.h"
12 #include "ui/gfx/path.h"
13 #include "ui/gfx/rect.h"
14 #include "ui/gfx/size.h"
15 #include "ui/views/view.h"
16
17 // TouchOmniboxResultView ------------------------------------------------
18
19 TouchOmniboxResultView::TouchOmniboxResultView(
20     OmniboxResultViewModel* model,
21     int model_index,
22     LocationBarView* location_bar_view,
23     const gfx::FontList& font_list)
24     : OmniboxResultView(model, model_index, location_bar_view, font_list) {
25   set_edge_item_padding(8);
26   set_item_padding(8);
27   set_minimum_text_vertical_padding(10);
28 }
29
30 TouchOmniboxResultView::~TouchOmniboxResultView() {
31 }
32
33 void TouchOmniboxResultView::PaintMatch(gfx::Canvas* canvas,
34                                         const AutocompleteMatch& match,
35                                         int x) {
36   int y = text_bounds().y();
37
38   if (!match.description.empty()) {
39     // We use our base class's GetTextHeight below because we need the height
40     // of a single line of text.
41     DrawString(canvas, match.description, match.description_class, true, x, y);
42     y += OmniboxResultView::GetTextHeight();
43   } else {
44     // When we have only one line of content (no description), we center the
45     // single line vertically on our two-lines-tall results box.
46     y += OmniboxResultView::GetTextHeight() / 2;
47   }
48
49   DrawString(canvas, match.contents, match.contents_class, false, x, y);
50 }
51
52 int TouchOmniboxResultView::GetTextHeight() const {
53   return OmniboxResultView::GetTextHeight() * 2;
54 }
55
56 // TouchOmniboxPopupContentsView -----------------------------------------
57
58 TouchOmniboxPopupContentsView::TouchOmniboxPopupContentsView(
59     const gfx::FontList& font_list,
60     OmniboxView* omnibox_view,
61     OmniboxEditModel* edit_model,
62     LocationBarView* location_bar_view)
63     : OmniboxPopupContentsView(font_list, omnibox_view, edit_model,
64                                location_bar_view) {
65 }
66
67 TouchOmniboxPopupContentsView::~TouchOmniboxPopupContentsView() {
68 }
69
70 void TouchOmniboxPopupContentsView::UpdatePopupAppearance() {
71   OmniboxPopupContentsView::UpdatePopupAppearance();
72   Layout();
73 }
74
75 void TouchOmniboxPopupContentsView::PaintResultViews(gfx::Canvas* canvas) {
76   OmniboxPopupContentsView::PaintResultViews(canvas);
77
78   // Draw divider lines.
79   std::vector<View*> visible_children(GetVisibleChildren());
80   if (visible_children.size() < 2)
81     return;
82   gfx::Rect bounds(GetContentsBounds());
83
84   // Draw a line at the bottom of each child except the last.  The
85   // color of the line is determined to blend appropriately with the
86   // most dominant of the two surrounding cells, in precedence order,
87   // i.e. selected > hovered > normal.
88   for (std::vector<View*>::const_iterator i(visible_children.begin());
89        i + 1 != visible_children.end(); ++i) {
90     TouchOmniboxResultView* child = static_cast<TouchOmniboxResultView*>(*i);
91     TouchOmniboxResultView* next_child =
92         static_cast<TouchOmniboxResultView*>(*(i + 1));
93     SkColor divider_color = child->GetColor(
94         std::max(child->GetState(), next_child->GetState()),
95         OmniboxResultView::DIVIDER);
96     int line_y = child->y() + child->height() - 1;
97     canvas->DrawLine(gfx::Point(bounds.x(), line_y),
98                      gfx::Point(bounds.right(), line_y), divider_color);
99   }
100 }
101
102 OmniboxResultView* TouchOmniboxPopupContentsView::CreateResultView(
103     OmniboxResultViewModel* model,
104     int model_index,
105     const gfx::FontList& font_list) {
106   return new TouchOmniboxResultView(model, model_index, location_bar_view(),
107                                     font_list);
108 }
109
110 std::vector<views::View*> TouchOmniboxPopupContentsView::GetVisibleChildren() {
111   std::vector<View*> visible_children;
112   for (int i = 0; i < child_count(); ++i) {
113     View* v = child_at(i);
114     if (child_at(i)->visible())
115       visible_children.push_back(v);
116   }
117   return visible_children;
118 }