Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / views / examples / table_example.cc
blobabdfebbeebd1f869365dba0f7aaa84081ff8756d
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_.allocN32Pixels(16, 16);
75 SkCanvas canvas1(icon1_);
76 canvas1.drawColor(SK_ColorRED);
78 icon2_.allocN32Pixels(16, 16);
79 SkCanvas canvas2(icon2_);
80 canvas2.drawColor(SK_ColorBLUE);
82 ColumnSet* column_set = layout->AddColumnSet(0);
83 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1,
84 GridLayout::USE_PREF, 0, 0);
85 layout->StartRow(1 /* expand */, 0);
86 layout->AddView(table_->CreateParentIfNecessary());
88 column_set = layout->AddColumnSet(1);
89 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL,
90 0.5f, GridLayout::USE_PREF, 0, 0);
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);
98 layout->StartRow(0 /* no expand */, 1);
100 layout->AddView(column1_visible_checkbox_);
101 layout->AddView(column2_visible_checkbox_);
102 layout->AddView(column3_visible_checkbox_);
103 layout->AddView(column4_visible_checkbox_);
106 int TableExample::RowCount() {
107 return 10;
110 base::string16 TableExample::GetText(int row, int column_id) {
111 if (row == -1)
112 return base::string16();
114 const char* const cells[5][4] = {
115 { "Orange", "Orange", "South america", "$5" },
116 { "Apple", "Green", "Canada", "$3" },
117 { "Blue berries", "Blue", "Mexico", "$10.3" },
118 { "Strawberries", "Red", "California", "$7" },
119 { "Cantaloupe", "Orange", "South america", "$5" },
121 return ASCIIToUTF16(cells[row % 5][column_id]);
124 gfx::ImageSkia TableExample::GetIcon(int row) {
125 SkBitmap row_icon = row % 2 ? icon1_ : icon2_;
126 return gfx::ImageSkia::CreateFrom1xBitmap(row_icon);
129 void TableExample::SetObserver(ui::TableModelObserver* observer) {}
131 void TableExample::GetGroupRange(int model_index, GroupRange* range) {
132 if (model_index < 2) {
133 range->start = 0;
134 range->length = 2;
135 } else if (model_index > 6) {
136 range->start = 7;
137 range->length = 3;
138 } else {
139 range->start = model_index;
140 range->length = 1;
144 void TableExample::OnSelectionChanged() {
145 PrintStatus("Selected: %s",
146 base::UTF16ToASCII(GetText(table_->selection_model().active(),
147 0)).c_str());
150 void TableExample::OnDoubleClick() {
151 PrintStatus("Double Click: %s",
152 base::UTF16ToASCII(GetText(table_->selection_model().active(),
153 0)).c_str());
156 void TableExample::OnMiddleClick() {}
158 void TableExample::OnKeyDown(ui::KeyboardCode virtual_keycode) {}
160 void TableExample::ButtonPressed(Button* sender, const ui::Event& event) {
161 int index = 0;
162 bool show = true;
163 if (sender == column1_visible_checkbox_) {
164 index = 0;
165 show = column1_visible_checkbox_->checked();
166 } else if (sender == column2_visible_checkbox_) {
167 index = 1;
168 show = column2_visible_checkbox_->checked();
169 } else if (sender == column3_visible_checkbox_) {
170 index = 2;
171 show = column3_visible_checkbox_->checked();
172 } else if (sender == column4_visible_checkbox_) {
173 index = 3;
174 show = column4_visible_checkbox_->checked();
176 table_->SetColumnVisibility(index, show);
179 } // namespace examples
180 } // namespace views