Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / app_list / views / contents_switcher_view.cc
1 // Copyright 2014 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 "ui/app_list/views/contents_switcher_view.h"
6
7 #include "grit/ui_resources.h"
8 #include "ui/app_list/app_list_constants.h"
9 #include "ui/app_list/views/contents_view.h"
10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/views/controls/button/custom_button.h"
12 #include "ui/views/controls/button/image_button.h"
13 #include "ui/views/layout/box_layout.h"
14
15 namespace app_list {
16
17 namespace {
18
19 const int kPreferredHeight = 32;
20 const int kButtonSpacing = 4;
21
22 }  // namespace
23
24 ContentsSwitcherView::ContentsSwitcherView(ContentsView* contents_view)
25     : contents_view_(contents_view), buttons_(new views::View) {
26   AddChildView(buttons_);
27
28   buttons_->SetLayoutManager(new views::BoxLayout(
29       views::BoxLayout::kHorizontal, 0, 0, kButtonSpacing));
30   AddSwitcherButton(IDR_APP_LIST_SEARCH_ICON,
31                     ContentsView::SHOW_SEARCH_RESULTS);
32   AddSwitcherButton(IDR_APP_LIST_APPS_ICON, ContentsView::SHOW_APPS);
33 }
34
35 ContentsSwitcherView::~ContentsSwitcherView() {}
36
37 void ContentsSwitcherView::AddSwitcherButton(int resource_id, int tag) {
38   views::ImageButton* button = new views::ImageButton(this);
39   button->SetImage(
40       views::CustomButton::STATE_NORMAL,
41       ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id));
42   button->set_tag(tag);
43   buttons_->AddChildView(button);
44 }
45
46 gfx::Size ContentsSwitcherView::GetPreferredSize() {
47   return gfx::Size(buttons_->GetPreferredSize().width(), kPreferredHeight);
48 }
49
50 void ContentsSwitcherView::Layout() {
51   gfx::Rect rect(GetContentsBounds());
52
53   // Makes |buttons_| horizontally center and vertically fill.
54   gfx::Size buttons_size(buttons_->GetPreferredSize());
55   gfx::Rect buttons_bounds(rect.CenterPoint().x() - buttons_size.width() / 2,
56                            rect.y(),
57                            buttons_size.width(),
58                            rect.height());
59   buttons_->SetBoundsRect(gfx::IntersectRects(rect, buttons_bounds));
60 }
61
62 void ContentsSwitcherView::ButtonPressed(views::Button* sender,
63                                          const ui::Event& event) {
64   contents_view_->SetShowState(
65       static_cast<ContentsView::ShowState>(sender->tag()));
66 }
67
68 }  // namespace app_list