Home | History | Annotate | Download | only in examples
      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
      3 // found in the LICENSE file.
      4 
      5 #include "ui/views/examples/radio_button_example.h"
      6 
      7 #include "base/strings/stringprintf.h"
      8 #include "base/strings/utf_string_conversions.h"
      9 #include "ui/views/controls/button/label_button.h"
     10 #include "ui/views/controls/button/radio_button.h"
     11 #include "ui/views/layout/grid_layout.h"
     12 #include "ui/views/view.h"
     13 
     14 namespace views {
     15 namespace examples {
     16 
     17 RadioButtonExample::RadioButtonExample()
     18     : ExampleBase("Radio Button"),
     19       count_(0) {
     20 }
     21 
     22 RadioButtonExample::~RadioButtonExample() {
     23 }
     24 
     25 void RadioButtonExample::CreateExampleView(View* container) {
     26   select_ = new LabelButton(this, base::ASCIIToUTF16("Select"));
     27   status_ = new LabelButton(this, base::ASCIIToUTF16("Show Status"));
     28 
     29   int group = 1;
     30   for (size_t i = 0; i < arraysize(radio_buttons_); ++i) {
     31     radio_buttons_[i] = new RadioButton(
     32         base::UTF8ToUTF16(base::StringPrintf(
     33             "Radio %d in group %d", static_cast<int>(i) + 1, group)),
     34         group);
     35     radio_buttons_[i]->set_listener(this);
     36   }
     37 
     38   GridLayout* layout = new GridLayout(container);
     39   container->SetLayoutManager(layout);
     40 
     41   ColumnSet* column_set = layout->AddColumnSet(0);
     42   column_set->AddColumn(GridLayout::FILL, GridLayout::FILL,
     43                         1.0f, GridLayout::USE_PREF, 0, 0);
     44   for (size_t i = 0; i < arraysize(radio_buttons_); ++i) {
     45     layout->StartRow(0, 0);
     46     layout->AddView(radio_buttons_[i]);
     47   }
     48   layout->StartRow(0, 0);
     49   layout->AddView(select_);
     50   layout->StartRow(0, 0);
     51   layout->AddView(status_);
     52 }
     53 
     54 void RadioButtonExample::ButtonPressed(Button* sender, const ui::Event& event) {
     55   if (sender == select_) {
     56     radio_buttons_[2]->SetChecked(true);
     57   } else if (sender == status_) {
     58     // Show the state of radio buttons.
     59     PrintStatus("Group: 1:%s, 2:%s, 3:%s",
     60                 BoolToOnOff(radio_buttons_[0]->checked()),
     61                 BoolToOnOff(radio_buttons_[1]->checked()),
     62                 BoolToOnOff(radio_buttons_[2]->checked()));
     63   } else {
     64     PrintStatus("Pressed! count:%d", ++count_);
     65   }
     66 }
     67 
     68 }  // namespace examples
     69 }  // namespace views
     70