Blink roll 174234:174238
[chromium-blink-merge.git] / ui / views / examples / table_example.cc
blob5c9eebfd2ed2a05ae7feaa656bda1b6051fbb444
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.
5 #include "ui/views/examples/table_example.h"
7 #include <vector>
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "third_party/skia/include/core/SkBitmap.h"
12 #include "third_party/skia/include/core/SkCanvas.h"
13 #include "ui/gfx/image/image_skia.h"
14 #include "ui/views/controls/button/checkbox.h"
15 #include "ui/views/layout/grid_layout.h"
17 using base::ASCIIToUTF16;
19 namespace views {
20 namespace examples {
22 namespace {
24 ui::TableColumn TestTableColumn(int id, const std::string& title) {
25 ui::TableColumn column;
26 column.id = id;
27 column.title = ASCIIToUTF16(title.c_str());
28 column.sortable = true;
29 return column;
32 } // namespace
34 TableExample::TableExample() : ExampleBase("Table") , table_(NULL) {
37 TableExample::~TableExample() {
38 // Delete the view before the model.
39 delete table_;
40 table_ = NULL;
43 void TableExample::CreateExampleView(View* container) {
44 column1_visible_checkbox_ = new Checkbox(
45 ASCIIToUTF16("Fruit column visible"));
46 column1_visible_checkbox_->SetChecked(true);
47 column1_visible_checkbox_->set_listener(this);
48 column2_visible_checkbox_ = new Checkbox(
49 ASCIIToUTF16("Color column visible"));
50 column2_visible_checkbox_->SetChecked(true);
51 column2_visible_checkbox_->set_listener(this);
52 column3_visible_checkbox_ = new Checkbox(
53 ASCIIToUTF16("Origin column visible"));
54 column3_visible_checkbox_->SetChecked(true);
55 column3_visible_checkbox_->set_listener(this);
56 column4_visible_checkbox_ = new Checkbox(
57 ASCIIToUTF16("Price column visible"));
58 column4_visible_checkbox_->SetChecked(true);
59 column4_visible_checkbox_->set_listener(this);
61 GridLayout* layout = new GridLayout(container);
62 container->SetLayoutManager(layout);
64 std::vector<ui::TableColumn> columns;
65 columns.push_back(TestTableColumn(0, "Fruit"));
66 columns[0].percent = 1;
67 columns.push_back(TestTableColumn(1, "Color"));
68 columns.push_back(TestTableColumn(2, "Origin"));
69 columns.push_back(TestTableColumn(3, "Price"));
70 columns.back().alignment = ui::TableColumn::RIGHT;
71 table_ = new TableView(this, columns, ICON_AND_TEXT, true);
72 table_->SetGrouper(this);
73 table_->SetObserver(this);
74 icon1_.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
75 icon1_.allocPixels();
76 SkCanvas canvas1(icon1_);
77 canvas1.drawColor(SK_ColorRED);
79 icon2_.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
80 icon2_.allocPixels();
81 SkCanvas canvas2(icon2_);
82 canvas2.drawColor(SK_ColorBLUE);
84 ColumnSet* column_set = layout->AddColumnSet(0);
85 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1,
86 GridLayout::USE_PREF, 0, 0);
87 layout->StartRow(1 /* expand */, 0);
88 layout->AddView(table_->CreateParentIfNecessary());
90 column_set = layout->AddColumnSet(1);
91 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL,
92 0.5f, GridLayout::USE_PREF, 0, 0);
93 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL,
94 0.5f, GridLayout::USE_PREF, 0, 0);
95 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL,
96 0.5f, GridLayout::USE_PREF, 0, 0);
97 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL,
98 0.5f, GridLayout::USE_PREF, 0, 0);
100 layout->StartRow(0 /* no expand */, 1);
102 layout->AddView(column1_visible_checkbox_);
103 layout->AddView(column2_visible_checkbox_);
104 layout->AddView(column3_visible_checkbox_);
105 layout->AddView(column4_visible_checkbox_);
108 int TableExample::RowCount() {
109 return 10;
112 base::string16 TableExample::GetText(int row, int column_id) {
113 if (row == -1)
114 return base::string16();
116 const char* const cells[5][4] = {
117 { "Orange", "Orange", "South america", "$5" },
118 { "Apple", "Green", "Canada", "$3" },
119 { "Blue berries", "Blue", "Mexico", "$10.3" },
120 { "Strawberries", "Red", "California", "$7" },
121 { "Cantaloupe", "Orange", "South america", "$5" },
123 return ASCIIToUTF16(cells[row % 5][column_id]);
126 gfx::ImageSkia TableExample::GetIcon(int row) {
127 SkBitmap row_icon = row % 2 ? icon1_ : icon2_;
128 return gfx::ImageSkia::CreateFrom1xBitmap(row_icon);
131 void TableExample::SetObserver(ui::TableModelObserver* observer) {}
133 void TableExample::GetGroupRange(int model_index, GroupRange* range) {
134 if (model_index < 2) {
135 range->start = 0;
136 range->length = 2;
137 } else if (model_index > 6) {
138 range->start = 7;
139 range->length = 3;
140 } else {
141 range->start = model_index;
142 range->length = 1;
146 void TableExample::OnSelectionChanged() {
147 PrintStatus("Selected: %s",
148 base::UTF16ToASCII(GetText(table_->selection_model().active(),
149 0)).c_str());
152 void TableExample::OnDoubleClick() {
153 PrintStatus("Double Click: %s",
154 base::UTF16ToASCII(GetText(table_->selection_model().active(),
155 0)).c_str());
158 void TableExample::OnMiddleClick() {}
160 void TableExample::OnKeyDown(ui::KeyboardCode virtual_keycode) {}
162 void TableExample::ButtonPressed(Button* sender, const ui::Event& event) {
163 int index = 0;
164 bool show = true;
165 if (sender == column1_visible_checkbox_) {
166 index = 0;
167 show = column1_visible_checkbox_->checked();
168 } else if (sender == column2_visible_checkbox_) {
169 index = 1;
170 show = column2_visible_checkbox_->checked();
171 } else if (sender == column3_visible_checkbox_) {
172 index = 2;
173 show = column3_visible_checkbox_->checked();
174 } else if (sender == column4_visible_checkbox_) {
175 index = 3;
176 show = column4_visible_checkbox_->checked();
178 table_->SetColumnVisibility(index, show);
181 } // namespace examples
182 } // namespace views