OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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_RADIO_BUTTON_EXAMPLE_H_ | 5 #ifndef VIEWS_EXAMPLES_RADIO_BUTTON_EXAMPLE_H_ |
6 #define VIEWS_EXAMPLES_RADIO_BUTTON_EXAMPLE_H_ | 6 #define VIEWS_EXAMPLES_RADIO_BUTTON_EXAMPLE_H_ |
7 | 7 |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "views/controls/button/radio_button.h" | 10 #include "views/controls/button/radio_button.h" |
11 #include "views/controls/button/text_button.h" | 11 #include "views/controls/button/text_button.h" |
12 #include "views/examples/example_base.h" | 12 #include "views/examples/example_base.h" |
13 | 13 |
14 namespace examples { | 14 namespace examples { |
15 | 15 |
16 class RadioButtonExample : protected ExampleBase, | 16 class RadioButtonExample : public ExampleBase, |
17 private views::ButtonListener { | 17 public views::ButtonListener { |
18 public: | 18 public: |
19 explicit RadioButtonExample(ExamplesMain* main) : ExampleBase(main) { | 19 explicit RadioButtonExample(ExamplesMain* main): ExampleBase(main) {} |
| 20 |
| 21 virtual ~RadioButtonExample() {} |
| 22 |
| 23 virtual std::wstring GetExampleTitle() { |
| 24 return L"Radio Button"; |
| 25 } |
| 26 |
| 27 virtual void CreateExampleView(views::View* container) { |
20 select_ = new views::TextButton(this, L"Select"); | 28 select_ = new views::TextButton(this, L"Select"); |
21 status_ = new views::TextButton(this, L"Show Status"); | 29 status_ = new views::TextButton(this, L"Show Status"); |
22 | 30 |
23 int all = arraysize(radio_buttons_); | 31 int all = arraysize(radio_buttons_); |
24 | 32 |
25 // divide buttons into 2 groups | 33 // divide buttons into 2 groups |
26 int group_count = all / 2; | 34 int group_count = all / 2; |
27 for (int i = 0; i < all; i++) { | 35 for (int i = 0; i < all; i++) { |
28 int group = i / group_count; | 36 int group = i / group_count; |
29 radio_buttons_[i] = new views::RadioButton( | 37 radio_buttons_[i] = new views::RadioButton( |
30 StringPrintf(L"Radio %d in group %d", (i % group_count + 1), group), | 38 StringPrintf(L"Radio %d in group %d", (i % group_count + 1), group), |
31 group); | 39 group); |
32 } | 40 } |
33 | 41 |
34 container_ = new views::View(); | 42 views::GridLayout* layout = new views::GridLayout(container); |
35 views::GridLayout* layout = new views::GridLayout(container_); | 43 container->SetLayoutManager(layout); |
36 container_->SetLayoutManager(layout); | |
37 | 44 |
38 views::ColumnSet* column_set = layout->AddColumnSet(0); | 45 views::ColumnSet* column_set = layout->AddColumnSet(0); |
39 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, | 46 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, |
40 1.0f, views::GridLayout::USE_PREF, 0, 0); | 47 1.0f, views::GridLayout::USE_PREF, 0, 0); |
41 for (int i = 0; i < all; i++) { | 48 for (int i = 0; i < all; i++) { |
42 layout->StartRow(0, 0); | 49 layout->StartRow(0, 0); |
43 layout->AddView(radio_buttons_[i]); | 50 layout->AddView(radio_buttons_[i]); |
44 } | 51 } |
45 layout->StartRow(0, 0); | 52 layout->StartRow(0, 0); |
46 layout->AddView(select_); | 53 layout->AddView(select_); |
47 layout->StartRow(0, 0); | 54 layout->StartRow(0, 0); |
48 layout->AddView(status_); | 55 layout->AddView(status_); |
49 } | 56 } |
50 | 57 |
51 virtual ~RadioButtonExample() {} | |
52 | |
53 virtual std::wstring GetExampleTitle() { | |
54 return L"Radio Button"; | |
55 } | |
56 | |
57 virtual views::View* GetExampleView() { | |
58 return container_; | |
59 } | |
60 | |
61 private: | 58 private: |
62 // Override from ButtonListener | 59 // Override from ButtonListener |
63 virtual void ButtonPressed(views::Button* sender, const views::Event& event) { | 60 virtual void ButtonPressed(views::Button* sender, const views::Event& event) { |
64 if (sender == select_) { | 61 if (sender == select_) { |
65 radio_buttons_[0]->SetChecked(true); | 62 radio_buttons_[0]->SetChecked(true); |
66 radio_buttons_[5]->SetChecked(true); | 63 radio_buttons_[5]->SetChecked(true); |
67 } else if (sender == status_) { | 64 } else if (sender == status_) { |
68 // Show the state of radio buttons. | 65 // Show the state of radio buttons. |
69 PrintStatus(L"Group1: 1:%ls, 2:%ls, 3:%ls Group2: 1:%ls, 2:%ls, 3:%ls", | 66 PrintStatus(L"Group1: 1:%ls, 2:%ls, 3:%ls Group2: 1:%ls, 2:%ls, 3:%ls", |
70 IntToOnOff(radio_buttons_[0]->checked()), | 67 IntToOnOff(radio_buttons_[0]->checked()), |
71 IntToOnOff(radio_buttons_[1]->checked()), | 68 IntToOnOff(radio_buttons_[1]->checked()), |
72 IntToOnOff(radio_buttons_[2]->checked()), | 69 IntToOnOff(radio_buttons_[2]->checked()), |
73 IntToOnOff(radio_buttons_[3]->checked()), | 70 IntToOnOff(radio_buttons_[3]->checked()), |
74 IntToOnOff(radio_buttons_[4]->checked()), | 71 IntToOnOff(radio_buttons_[4]->checked()), |
75 IntToOnOff(radio_buttons_[5]->checked())); | 72 IntToOnOff(radio_buttons_[5]->checked())); |
76 } | 73 } |
77 } | 74 } |
78 | 75 |
79 // The view containing this test's controls. | |
80 views::View* container_; | |
81 | |
82 // 6 radio buttons, 0-2 consists 1st group, and 3-5 consists | 76 // 6 radio buttons, 0-2 consists 1st group, and 3-5 consists |
83 // 2nd group. | 77 // 2nd group. |
84 views::RadioButton* radio_buttons_[6]; | 78 views::RadioButton* radio_buttons_[6]; |
85 | 79 |
86 // Control button to select radio buttons, and show the status of buttons. | 80 // Control button to select radio buttons, and show the status of buttons. |
87 views::TextButton* select_, *status_; | 81 views::TextButton* select_, *status_; |
88 | 82 |
89 DISALLOW_COPY_AND_ASSIGN(RadioButtonExample); | 83 DISALLOW_COPY_AND_ASSIGN(RadioButtonExample); |
90 }; | 84 }; |
91 | 85 |
92 } // namespace examples | 86 } // namespace examples |
93 | 87 |
94 #endif // VIEWS_EXAMPLES_RADIO_BUTTON_EXAMPLE_H_ | 88 #endif // VIEWS_EXAMPLES_RADIO_BUTTON_EXAMPLE_H_ |
95 | 89 |
OLD | NEW |