Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ui / views / examples / table_example.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 "ui/views/examples/table_example.h"
6
7 #include <vector>
8
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"
16
17 using base::ASCIIToUTF16;
18
19 namespace views {
20 namespace examples {
21
22 namespace {
23
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;
30 }
31
32 }  // namespace
33
34 TableExample::TableExample() : ExampleBase("Table") , table_(NULL) {
35 }
36
37 TableExample::~TableExample() {
38   // Delete the view before the model.
39   delete table_;
40   table_ = NULL;
41 }
42
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);
60
61   GridLayout* layout = new GridLayout(container);
62   container->SetLayoutManager(layout);
63
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);
77
78   icon2_.allocN32Pixels(16, 16);
79   SkCanvas canvas2(icon2_);
80   canvas2.drawColor(SK_ColorBLUE);
81
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());
87
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);
97
98   layout->StartRow(0 /* no expand */, 1);
99
100   layout->AddView(column1_visible_checkbox_);
101   layout->AddView(column2_visible_checkbox_);
102   layout->AddView(column3_visible_checkbox_);
103   layout->AddView(column4_visible_checkbox_);
104 }
105
106 int TableExample::RowCount() {
107   return 10;
108 }
109
110 base::string16 TableExample::GetText(int row, int column_id) {
111   if (row == -1)
112     return base::string16();
113
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" },
120   };
121   return ASCIIToUTF16(cells[row % 5][column_id]);
122 }
123
124 gfx::ImageSkia TableExample::GetIcon(int row) {
125   SkBitmap row_icon = row % 2 ? icon1_ : icon2_;
126   return gfx::ImageSkia::CreateFrom1xBitmap(row_icon);
127 }
128
129 void TableExample::SetObserver(ui::TableModelObserver* observer) {}
130
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;
141   }
142 }
143
144 void TableExample::OnSelectionChanged() {
145   PrintStatus("Selected: %s",
146               base::UTF16ToASCII(GetText(table_->selection_model().active(),
147                                          0)).c_str());
148 }
149
150 void TableExample::OnDoubleClick() {
151   PrintStatus("Double Click: %s",
152               base::UTF16ToASCII(GetText(table_->selection_model().active(),
153                                          0)).c_str());
154 }
155
156 void TableExample::OnMiddleClick() {}
157
158 void TableExample::OnKeyDown(ui::KeyboardCode virtual_keycode) {}
159
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();
175   }
176   table_->SetColumnVisibility(index, show);
177 }
178
179 }  // namespace examples
180 }  // namespace views