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 "ui/base/resource/resource_bundle.h"
9 #include "ui/gfx/image/image.h"
10 #include "ui/resources/grit/ui_resources.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/layout/box_layout.h"
16 #include "ui/views/resources/grit/views_resources.h"
17 #include "ui/views/view.h"
19 using base::ASCIIToUTF16
;
22 const char kLabelButton
[] = "Label Button";
23 const char kMultiLineText
[] = "Multi-Line\nButton Text Is Here To Stay!\n123";
24 const char kLongText
[] = "Start of Really Really Really Really Really Really "
25 "Really Really Really Really Really Really Really "
26 "Really Really Really Really Really Long Button Text";
32 ButtonExample::ButtonExample()
33 : ExampleBase("Button"),
38 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
39 icon_
= rb
.GetImageNamed(IDR_CLOSE_H
).ToImageSkia();
42 ButtonExample::~ButtonExample() {
45 void ButtonExample::CreateExampleView(View
* container
) {
46 container
->set_background(Background::CreateSolidBackground(SK_ColorWHITE
));
47 container
->SetLayoutManager(new BoxLayout(BoxLayout::kVertical
, 10, 10, 10));
49 label_button_
= new LabelButton(this, ASCIIToUTF16(kLabelButton
));
50 label_button_
->SetFocusable(true);
51 container
->AddChildView(label_button_
);
53 LabelButton
* styled_button
=
54 new LabelButton(this, ASCIIToUTF16("Styled Button"));
55 styled_button
->SetStyle(Button::STYLE_BUTTON
);
56 container
->AddChildView(styled_button
);
58 LabelButton
* disabled_button
=
59 new LabelButton(this, ASCIIToUTF16("Disabled Button"));
60 disabled_button
->SetStyle(Button::STYLE_BUTTON
);
61 disabled_button
->SetState(Button::STATE_DISABLED
);
62 container
->AddChildView(disabled_button
);
64 container
->AddChildView(new BlueButton(this, ASCIIToUTF16("Blue Button")));
66 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
67 image_button_
= new ImageButton(this);
68 image_button_
->SetFocusable(true);
69 image_button_
->SetImage(ImageButton::STATE_NORMAL
,
70 rb
.GetImageNamed(IDR_CLOSE
).ToImageSkia());
71 image_button_
->SetImage(ImageButton::STATE_HOVERED
,
72 rb
.GetImageNamed(IDR_CLOSE_H
).ToImageSkia());
73 image_button_
->SetImage(ImageButton::STATE_PRESSED
,
74 rb
.GetImageNamed(IDR_CLOSE_P
).ToImageSkia());
75 container
->AddChildView(image_button_
);
78 void ButtonExample::LabelButtonPressed(const ui::Event
& event
) {
79 PrintStatus("Label Button Pressed! count: %d", ++count_
);
80 if (event
.IsControlDown()) {
81 if (event
.IsShiftDown()) {
82 if (event
.IsAltDown()) {
83 label_button_
->SetTextMultiLine(!label_button_
->GetTextMultiLine());
84 label_button_
->SetText(ASCIIToUTF16(
85 label_button_
->GetTextMultiLine() ? kMultiLineText
: kLabelButton
));
87 label_button_
->SetText(ASCIIToUTF16(
88 label_button_
->GetText().empty() ? kLongText
:
89 label_button_
->GetText().length() > 50 ? kLabelButton
: ""));
91 } else if (event
.IsAltDown()) {
92 label_button_
->SetImage(Button::STATE_NORMAL
,
93 label_button_
->GetImage(Button::STATE_NORMAL
).isNull() ?
94 *icon_
: gfx::ImageSkia());
96 label_button_
->SetHorizontalAlignment(
97 static_cast<gfx::HorizontalAlignment
>(
98 (label_button_
->GetHorizontalAlignment() + 1) % 3));
100 } else if (event
.IsShiftDown()) {
101 if (event
.IsAltDown()) {
102 label_button_
->SetFocusable(!label_button_
->IsFocusable());
104 label_button_
->SetStyle(static_cast<Button::ButtonStyle
>(
105 (label_button_
->style() + 1) % Button::STYLE_COUNT
));
107 } else if (event
.IsAltDown()) {
108 label_button_
->SetIsDefault(!label_button_
->is_default());
110 label_button_
->SetMinSize(gfx::Size());
112 example_view()->GetLayoutManager()->Layout(example_view());
115 void ButtonExample::ButtonPressed(Button
* sender
, const ui::Event
& event
) {
116 if (sender
== label_button_
)
117 LabelButtonPressed(event
);
119 PrintStatus("Image Button Pressed! count: %d", ++count_
);
122 } // namespace examples