Home | History | Annotate | Download | only in examples
      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 namespace views {
     18 namespace examples {
     19 
     20 namespace {
     21 
     22 ui::TableColumn TestTableColumn(int id, const std::string& title) {
     23   ui::TableColumn column;
     24   column.id = id;
     25   column.title = ASCIIToUTF16(title.c_str());
     26   column.sortable = true;
     27   return column;
     28 }
     29 
     30 }  // namespace
     31 
     32 TableExample::TableExample() : ExampleBase("Table") , table_(NULL) {
     33 }
     34 
     35 TableExample::~TableExample() {
     36   // Delete the view before the model.
     37   delete table_;
     38   table_ = NULL;
     39 }
     40 
     41 void TableExample::CreateExampleView(View* container) {
     42   column1_visible_checkbox_ = new Checkbox(
     43       ASCIIToUTF16("Fruit column visible"));
     44   column1_visible_checkbox_->SetChecked(true);
     45   column1_visible_checkbox_->set_listener(this);
     46   column2_visible_checkbox_ = new Checkbox(
     47       ASCIIToUTF16("Color column visible"));
     48   column2_visible_checkbox_->SetChecked(true);
     49   column2_visible_checkbox_->set_listener(this);
     50   column3_visible_checkbox_ = new Checkbox(
     51       ASCIIToUTF16("Origin column visible"));
     52   column3_visible_checkbox_->SetChecked(true);
     53   column3_visible_checkbox_->set_listener(this);
     54   column4_visible_checkbox_ = new Checkbox(
     55       ASCIIToUTF16("Price column visible"));
     56   column4_visible_checkbox_->SetChecked(true);
     57   column4_visible_checkbox_->set_listener(this);
     58 
     59   GridLayout* layout = new GridLayout(container);
     60   container->SetLayoutManager(layout);
     61 
     62   std::vector<ui::TableColumn> columns;
     63   columns.push_back(TestTableColumn(0, "Fruit"));
     64   columns[0].percent = 1;
     65   columns.push_back(TestTableColumn(1, "Color"));
     66   columns.push_back(TestTableColumn(2, "Origin"));
     67   columns.push_back(TestTableColumn(3, "Price"));
     68   columns.back().alignment = ui::TableColumn::RIGHT;
     69   table_ = new TableView(this, columns, ICON_AND_TEXT, true);
     70   table_->SetGrouper(this);
     71   table_->SetObserver(this);
     72   icon1_.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
     73   icon1_.allocPixels();
     74   SkCanvas canvas1(icon1_);
     75   canvas1.drawColor(SK_ColorRED);
     76 
     77   icon2_.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
     78   icon2_.allocPixels();
     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 string16 TableExample::GetText(int row, int column_id) {
    111   if (row == -1)
    112     return 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               UTF16ToASCII(GetText(table_->selection_model().active(),
    147                                    0)).c_str());
    148 }
    149 
    150 void TableExample::OnDoubleClick() {
    151   PrintStatus("Double Click: %s",
    152               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
    181