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.
5 #include "ui/views/examples/button_example.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "grit/ui_resources.h"
9 #include "ui/base/resource/resource_bundle.h"
10 #include "ui/gfx/image/image.h"
11 #include "ui/views/background.h"
12 #include "ui/views/controls/button/blue_button.h"
13 #include "ui/views/controls/button/image_button.h"
14 #include "ui/views/controls/button/label_button.h"
15 #include "ui/views/controls/button/text_button.h"
16 #include "ui/views/layout/box_layout.h"
17 #include "ui/views/view.h"
19 using base::ASCIIToUTF16;
22 const char kLabelButton[] = "Label Button";
23 const char kTextButton[] = "Text Button";
24 const char kMultiLineText[] = "Multi-Line\nButton Text Is Here To Stay!\n123";
25 const char kLongText[] = "Start of Really Really Really Really Really Really "
26 "Really Really Really Really Really Really Really "
27 "Really Really Really Really Really Long Button Text";
33 ButtonExample::ButtonExample()
34 : ExampleBase("Button"),
38 alignment_(TextButton::ALIGN_LEFT),
39 use_native_theme_border_(false),
42 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
43 icon_ = rb.GetImageNamed(IDR_CLOSE_H).ToImageSkia();
46 ButtonExample::~ButtonExample() {
49 void ButtonExample::CreateExampleView(View* container) {
50 container->set_background(Background::CreateSolidBackground(SK_ColorWHITE));
51 container->SetLayoutManager(new BoxLayout(BoxLayout::kVertical, 10, 10, 10));
53 text_button_ = new TextButton(this, ASCIIToUTF16(kTextButton));
54 text_button_->SetFocusable(true);
55 container->AddChildView(text_button_);
57 label_button_ = new LabelButton(this, ASCIIToUTF16(kLabelButton));
58 label_button_->SetFocusable(true);
59 container->AddChildView(label_button_);
61 LabelButton* disabled_button =
62 new LabelButton(this, ASCIIToUTF16("Disabled Button"));
63 disabled_button->SetStyle(Button::STYLE_BUTTON);
64 disabled_button->SetState(Button::STATE_DISABLED);
65 container->AddChildView(disabled_button);
67 container->AddChildView(new BlueButton(this, ASCIIToUTF16("Blue Button")));
69 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
70 image_button_ = new ImageButton(this);
71 image_button_->SetFocusable(true);
72 image_button_->SetImage(ImageButton::STATE_NORMAL,
73 rb.GetImageNamed(IDR_CLOSE).ToImageSkia());
74 image_button_->SetImage(ImageButton::STATE_HOVERED,
75 rb.GetImageNamed(IDR_CLOSE_H).ToImageSkia());
76 image_button_->SetImage(ImageButton::STATE_PRESSED,
77 rb.GetImageNamed(IDR_CLOSE_P).ToImageSkia());
78 container->AddChildView(image_button_);
81 void ButtonExample::TextButtonPressed(const ui::Event& event) {
82 PrintStatus("Text Button Pressed! count: %d", ++count_);
83 if (event.IsControlDown()) {
84 if (event.IsShiftDown()) {
85 if (event.IsAltDown()) {
86 text_button_->SetMultiLine(!text_button_->multi_line());
87 text_button_->SetText(ASCIIToUTF16(
88 text_button_->multi_line() ? kMultiLineText : kTextButton));
90 switch (text_button_->icon_placement()) {
91 case TextButton::ICON_ON_LEFT:
92 text_button_->set_icon_placement(TextButton::ICON_ON_RIGHT);
94 case TextButton::ICON_ON_RIGHT:
95 text_button_->set_icon_placement(TextButton::ICON_ON_LEFT);
97 case TextButton::ICON_CENTERED:
102 } else if (event.IsAltDown()) {
103 if (text_button_->HasIcon())
104 text_button_->SetIcon(gfx::ImageSkia());
106 text_button_->SetIcon(*icon_);
108 switch (alignment_) {
109 case TextButton::ALIGN_LEFT:
110 alignment_ = TextButton::ALIGN_CENTER;
112 case TextButton::ALIGN_CENTER:
113 alignment_ = TextButton::ALIGN_RIGHT;
115 case TextButton::ALIGN_RIGHT:
116 alignment_ = TextButton::ALIGN_LEFT;
119 text_button_->set_alignment(alignment_);
121 } else if (event.IsShiftDown()) {
122 if (event.IsAltDown()) {
123 text_button_->SetText(ASCIIToUTF16(
124 text_button_->text().length() < 50 ? kLongText : kTextButton));
126 use_native_theme_border_ = !use_native_theme_border_;
127 if (use_native_theme_border_) {
128 text_button_->SetBorder(scoped_ptr<views::Border>(
129 new TextButtonNativeThemeBorder(text_button_)));
131 text_button_->SetBorder(
132 scoped_ptr<views::Border>(new TextButtonDefaultBorder()));
135 } else if (event.IsAltDown()) {
136 text_button_->SetIsDefault(!text_button_->is_default());
138 text_button_->ClearMaxTextSize();
140 example_view()->GetLayoutManager()->Layout(example_view());
143 void ButtonExample::LabelButtonPressed(const ui::Event& event) {
144 PrintStatus("Label Button Pressed! count: %d", ++count_);
145 if (event.IsControlDown()) {
146 if (event.IsShiftDown()) {
147 if (event.IsAltDown()) {
148 label_button_->SetTextMultiLine(!label_button_->GetTextMultiLine());
149 label_button_->SetText(ASCIIToUTF16(
150 label_button_->GetTextMultiLine() ? kMultiLineText : kLabelButton));
152 label_button_->SetText(ASCIIToUTF16(
153 label_button_->GetText().empty() ? kLongText :
154 label_button_->GetText().length() > 50 ? kLabelButton : ""));
156 } else if (event.IsAltDown()) {
157 label_button_->SetImage(Button::STATE_NORMAL,
158 label_button_->GetImage(Button::STATE_NORMAL).isNull() ?
159 *icon_ : gfx::ImageSkia());
161 label_button_->SetHorizontalAlignment(
162 static_cast<gfx::HorizontalAlignment>(
163 (label_button_->GetHorizontalAlignment() + 1) % 3));
165 } else if (event.IsShiftDown()) {
166 if (event.IsAltDown()) {
167 label_button_->SetFocusable(!label_button_->IsFocusable());
169 label_button_->SetStyle(static_cast<Button::ButtonStyle>(
170 (label_button_->style() + 1) % Button::STYLE_COUNT));
172 } else if (event.IsAltDown()) {
173 label_button_->SetIsDefault(!label_button_->is_default());
175 label_button_->set_min_size(gfx::Size());
177 example_view()->GetLayoutManager()->Layout(example_view());
180 void ButtonExample::ButtonPressed(Button* sender, const ui::Event& event) {
181 if (sender == text_button_)
182 TextButtonPressed(event);
183 else if (sender == label_button_)
184 LabelButtonPressed(event);
186 PrintStatus("Image Button Pressed! count: %d", ++count_);
189 } // namespace examples