| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef VIEWS_EXAMPLES_TABLE2_EXAMPLE_H_ | 5 #ifndef VIEWS_EXAMPLES_TABLE2_EXAMPLE_H_ |
| 6 #define VIEWS_EXAMPLES_TABLE2_EXAMPLE_H_ | 6 #define VIEWS_EXAMPLES_TABLE2_EXAMPLE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | |
| 11 | 10 |
| 12 #include "base/utf_string_conversions.h" | 11 #include "base/basictypes.h" |
| 13 #include "third_party/skia/include/core/SkCanvas.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "third_party/skia/include/core/SkBitmap.h" |
| 14 #include "ui/base/models/table_model.h" | 14 #include "ui/base/models/table_model.h" |
| 15 #include "views/controls/button/checkbox.h" | 15 #include "views/controls/button/button.h" |
| 16 #include "views/controls/table/table_view_observer.h" | 16 #include "views/controls/table/table_view_observer.h" |
| 17 #include "views/controls/table/table_view2.h" | |
| 18 #include "views/examples/example_base.h" | 17 #include "views/examples/example_base.h" |
| 19 | 18 |
| 20 using ui::TableModel; | 19 namespace views { |
| 21 using ui::TableModelObserver; // TODO(beng): remove these. | 20 class Checkbox; |
| 21 class Event; |
| 22 class TableView2; |
| 23 } |
| 22 | 24 |
| 23 namespace examples { | 25 namespace examples { |
| 24 | 26 |
| 25 class Table2Example | 27 class Table2Example : public ExampleBase, |
| 26 : public ExampleBase, | 28 public ui::TableModel, |
| 27 public TableModel, | 29 public views::ButtonListener, |
| 28 public views::ButtonListener, | 30 public views::TableViewObserver { |
| 29 public views::TableViewObserver { | |
| 30 public: | 31 public: |
| 31 explicit Table2Example(ExamplesMain* main) : ExampleBase(main) { | 32 explicit Table2Example(ExamplesMain* main); |
| 32 } | 33 virtual ~Table2Example(); |
| 33 | 34 |
| 34 virtual ~Table2Example() {} | 35 // Overridden from ExampleBase: |
| 36 virtual std::wstring GetExampleTitle() OVERRIDE; |
| 37 virtual void CreateExampleView(views::View* container) OVERRIDE; |
| 35 | 38 |
| 36 virtual std::wstring GetExampleTitle() { | 39 // Overridden from TableModel: |
| 37 return L"Table2"; | 40 virtual int RowCount() OVERRIDE; |
| 38 } | 41 virtual string16 GetText(int row, int column_id) OVERRIDE; |
| 42 virtual SkBitmap GetIcon(int row) OVERRIDE; |
| 43 virtual void SetObserver(ui::TableModelObserver* observer) OVERRIDE; |
| 39 | 44 |
| 40 virtual void CreateExampleView(views::View* container) { | 45 // Overridden from views::ButtonListener: |
| 41 column1_visible_checkbox_ = new views::Checkbox(L"Fruit column visible"); | 46 virtual void ButtonPressed(views::Button* sender, |
| 42 column1_visible_checkbox_->SetChecked(true); | 47 const views::Event& event) OVERRIDE; |
| 43 column1_visible_checkbox_->set_listener(this); | |
| 44 column2_visible_checkbox_ = new views::Checkbox(L"Color column visible"); | |
| 45 column2_visible_checkbox_->SetChecked(true); | |
| 46 column2_visible_checkbox_->set_listener(this); | |
| 47 column3_visible_checkbox_ = new views::Checkbox(L"Origin column visible"); | |
| 48 column3_visible_checkbox_->SetChecked(true); | |
| 49 column3_visible_checkbox_->set_listener(this); | |
| 50 column4_visible_checkbox_ = new views::Checkbox(L"Price column visible"); | |
| 51 column4_visible_checkbox_->SetChecked(true); | |
| 52 column4_visible_checkbox_->set_listener(this); | |
| 53 | 48 |
| 54 views::GridLayout* layout = new views::GridLayout(container); | 49 // Overridden from views::TableViewObserver: |
| 55 container->SetLayoutManager(layout); | 50 virtual void OnSelectionChanged() OVERRIDE; |
| 56 | 51 virtual void OnDoubleClick() OVERRIDE; |
| 57 std::vector<TableColumn> columns; | 52 virtual void OnMiddleClick() OVERRIDE; |
| 58 columns.push_back(TableColumn(0, ASCIIToUTF16("Fruit"), TableColumn::LEFT, | 53 virtual void OnKeyDown(ui::KeyboardCode virtual_keycode) OVERRIDE; |
| 59 100)); | 54 virtual void OnTableViewDelete(views::TableView* table_view) OVERRIDE; |
| 60 columns.push_back(TableColumn(1, ASCIIToUTF16("Color"), TableColumn::LEFT, | 55 virtual void OnTableView2Delete(views::TableView2* table_view) OVERRIDE; |
| 61 100)); | |
| 62 columns.push_back(TableColumn(2, ASCIIToUTF16("Origin"), TableColumn::LEFT, | |
| 63 100)); | |
| 64 columns.push_back(TableColumn(3, ASCIIToUTF16("Price"), TableColumn::LEFT, | |
| 65 100)); | |
| 66 const int options = (views::TableView2::SINGLE_SELECTION | | |
| 67 views::TableView2::RESIZABLE_COLUMNS | | |
| 68 views::TableView2::AUTOSIZE_COLUMNS | | |
| 69 views::TableView2::HORIZONTAL_LINES | | |
| 70 views::TableView2::VERTICAL_LINES); | |
| 71 table_ = new views::TableView2(this, columns, views::ICON_AND_TEXT, | |
| 72 options); | |
| 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); | |
| 78 | |
| 79 icon2.setConfig(SkBitmap::kARGB_8888_Config, 16, 16); | |
| 80 icon2.allocPixels(); | |
| 81 SkCanvas canvas2(icon2); | |
| 82 canvas2.drawColor(SK_ColorBLUE); | |
| 83 | |
| 84 views::ColumnSet* column_set = layout->AddColumnSet(0); | |
| 85 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, | |
| 86 views::GridLayout::USE_PREF, 0, 0); | |
| 87 layout->StartRow(1 /* expand */, 0); | |
| 88 layout->AddView(table_); | |
| 89 | |
| 90 column_set = layout->AddColumnSet(1); | |
| 91 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, | |
| 92 0.5f, views::GridLayout::USE_PREF, 0, 0); | |
| 93 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, | |
| 94 0.5f, views::GridLayout::USE_PREF, 0, 0); | |
| 95 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, | |
| 96 0.5f, views::GridLayout::USE_PREF, 0, 0); | |
| 97 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, | |
| 98 0.5f, views::GridLayout::USE_PREF, 0, 0); | |
| 99 | |
| 100 layout->StartRow(0 /* no expand */, 1); | |
| 101 | |
| 102 layout->AddView(column1_visible_checkbox_); | |
| 103 layout->AddView(column2_visible_checkbox_); | |
| 104 layout->AddView(column3_visible_checkbox_); | |
| 105 layout->AddView(column4_visible_checkbox_); | |
| 106 } | |
| 107 | |
| 108 // TableModel implementation: | |
| 109 virtual int RowCount() { | |
| 110 return 10; | |
| 111 } | |
| 112 | |
| 113 virtual string16 GetText(int row, int column_id) { | |
| 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 virtual SkBitmap GetIcon(int row) { | |
| 125 return row % 2 ? icon1 : icon2; | |
| 126 } | |
| 127 | |
| 128 virtual void SetObserver(TableModelObserver* observer) { | |
| 129 } | |
| 130 | |
| 131 // TableViewObserver implementation: | |
| 132 virtual void OnSelectionChanged() { | |
| 133 PrintStatus(L"Selection changed: %d", | |
| 134 table_->GetFirstSelectedRow()); | |
| 135 } | |
| 136 | |
| 137 virtual void OnDoubleClick() {} | |
| 138 | |
| 139 virtual void OnMiddleClick() {} | |
| 140 | |
| 141 virtual void OnKeyDown(ui::KeyboardCode virtual_keycode) {} | |
| 142 | |
| 143 virtual void OnTableViewDelete(views::TableView* table_view) {} | |
| 144 | |
| 145 virtual void OnTableView2Delete(views::TableView2* table_view) {} | |
| 146 | |
| 147 // ButtonListener implementation: | |
| 148 virtual void ButtonPressed(views::Button* sender, const views::Event& event) { | |
| 149 int index = 0; | |
| 150 bool show = true; | |
| 151 if (sender == column1_visible_checkbox_) { | |
| 152 index = 0; | |
| 153 show = column1_visible_checkbox_->checked(); | |
| 154 } else if (sender == column2_visible_checkbox_) { | |
| 155 index = 1; | |
| 156 show = column2_visible_checkbox_->checked(); | |
| 157 } else if (sender == column3_visible_checkbox_) { | |
| 158 index = 2; | |
| 159 show = column3_visible_checkbox_->checked(); | |
| 160 } else if (sender == column4_visible_checkbox_) { | |
| 161 index = 3; | |
| 162 show = column4_visible_checkbox_->checked(); | |
| 163 } | |
| 164 table_->SetColumnVisibility(index, show); | |
| 165 } | |
| 166 | 56 |
| 167 private: | 57 private: |
| 168 // The table to be tested. | 58 // The table to be tested. |
| 169 views::TableView2* table_; | 59 views::TableView2* table_; |
| 170 | 60 |
| 171 views::Checkbox* column1_visible_checkbox_; | 61 views::Checkbox* column1_visible_checkbox_; |
| 172 views::Checkbox* column2_visible_checkbox_; | 62 views::Checkbox* column2_visible_checkbox_; |
| 173 views::Checkbox* column3_visible_checkbox_; | 63 views::Checkbox* column3_visible_checkbox_; |
| 174 views::Checkbox* column4_visible_checkbox_; | 64 views::Checkbox* column4_visible_checkbox_; |
| 175 | 65 |
| 176 SkBitmap icon1; | 66 SkBitmap icon1_; |
| 177 SkBitmap icon2; | 67 SkBitmap icon2_; |
| 178 | 68 |
| 179 DISALLOW_COPY_AND_ASSIGN(Table2Example); | 69 DISALLOW_COPY_AND_ASSIGN(Table2Example); |
| 180 }; | 70 }; |
| 181 | 71 |
| 182 } // namespace examples | 72 } // namespace examples |
| 183 | 73 |
| 184 #endif // VIEWS_EXAMPLES_TABLE2_EXAMPLE_H_ | 74 #endif // VIEWS_EXAMPLES_TABLE2_EXAMPLE_H_ |
| OLD | NEW |