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.
5 #include "ui/views/examples/radio_button_example.h"
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"
17 RadioButtonExample::RadioButtonExample()
18 : ExampleBase("Radio Button"),
22 RadioButtonExample::~RadioButtonExample() {
25 void RadioButtonExample::CreateExampleView(View
* container
) {
26 select_
= new LabelButton(this, base::ASCIIToUTF16("Select"));
27 status_
= new LabelButton(this, base::ASCIIToUTF16("Show Status"));
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
)),
35 radio_buttons_
[i
]->set_listener(this);
38 GridLayout
* layout
= new GridLayout(container
);
39 container
->SetLayoutManager(layout
);
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
]);
48 layout
->StartRow(0, 0);
49 layout
->AddView(select_
);
50 layout
->StartRow(0, 0);
51 layout
->AddView(status_
);
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()));
64 PrintStatus("Pressed! count:%d", ++count_
);
68 } // namespace examples