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/view.h"
18 using base::ASCIIToUTF16
;
21 const char kLabelButton
[] = "Label Button";
22 const char kMultiLineText
[] = "Multi-Line\nButton Text Is Here To Stay!\n123";
23 const char kLongText
[] = "Start of Really Really Really Really Really Really "
24 "Really Really Really Really Really Really Really "
25 "Really Really Really Really Really Long Button Text";
31 ButtonExample::ButtonExample()
32 : ExampleBase("Button"),
37 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
38 icon_
= rb
.GetImageNamed(IDR_CLOSE_H
).ToImageSkia();
41 ButtonExample::~ButtonExample() {
44 void ButtonExample::CreateExampleView(View
* container
) {
45 container
->set_background(Background::CreateSolidBackground(SK_ColorWHITE
));
46 container
->SetLayoutManager(new BoxLayout(BoxLayout::kVertical
, 10, 10, 10));
48 label_button_
= new LabelButton(this, ASCIIToUTF16(kLabelButton
));
49 label_button_
->SetFocusable(true);
50 container
->AddChildView(label_button_
);
52 LabelButton
* disabled_button
=
53 new LabelButton(this, ASCIIToUTF16("Disabled Button"));
54 disabled_button
->SetStyle(Button::STYLE_BUTTON
);
55 disabled_button
->SetState(Button::STATE_DISABLED
);
56 container
->AddChildView(disabled_button
);
58 container
->AddChildView(new BlueButton(this, ASCIIToUTF16("Blue Button")));
60 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
61 image_button_
= new ImageButton(this);
62 image_button_
->SetFocusable(true);
63 image_button_
->SetImage(ImageButton::STATE_NORMAL
,
64 rb
.GetImageNamed(IDR_CLOSE
).ToImageSkia());
65 image_button_
->SetImage(ImageButton::STATE_HOVERED
,
66 rb
.GetImageNamed(IDR_CLOSE_H
).ToImageSkia());
67 image_button_
->SetImage(ImageButton::STATE_PRESSED
,
68 rb
.GetImageNamed(IDR_CLOSE_P
).ToImageSkia());
69 container
->AddChildView(image_button_
);
72 void ButtonExample::LabelButtonPressed(const ui::Event
& event
) {
73 PrintStatus("Label Button Pressed! count: %d", ++count_
);
74 if (event
.IsControlDown()) {
75 if (event
.IsShiftDown()) {
76 if (event
.IsAltDown()) {
77 label_button_
->SetTextMultiLine(!label_button_
->GetTextMultiLine());
78 label_button_
->SetText(ASCIIToUTF16(
79 label_button_
->GetTextMultiLine() ? kMultiLineText
: kLabelButton
));
81 label_button_
->SetText(ASCIIToUTF16(
82 label_button_
->GetText().empty() ? kLongText
:
83 label_button_
->GetText().length() > 50 ? kLabelButton
: ""));
85 } else if (event
.IsAltDown()) {
86 label_button_
->SetImage(Button::STATE_NORMAL
,
87 label_button_
->GetImage(Button::STATE_NORMAL
).isNull() ?
88 *icon_
: gfx::ImageSkia());
90 label_button_
->SetHorizontalAlignment(
91 static_cast<gfx::HorizontalAlignment
>(
92 (label_button_
->GetHorizontalAlignment() + 1) % 3));
94 } else if (event
.IsShiftDown()) {
95 if (event
.IsAltDown()) {
96 label_button_
->SetFocusable(!label_button_
->IsFocusable());
98 label_button_
->SetStyle(static_cast<Button::ButtonStyle
>(
99 (label_button_
->style() + 1) % Button::STYLE_COUNT
));
101 } else if (event
.IsAltDown()) {
102 label_button_
->SetIsDefault(!label_button_
->is_default());
104 label_button_
->SetMinSize(gfx::Size());
106 example_view()->GetLayoutManager()->Layout(example_view());
109 void ButtonExample::ButtonPressed(Button
* sender
, const ui::Event
& event
) {
110 if (sender
== label_button_
)
111 LabelButtonPressed(event
);
113 PrintStatus("Image Button Pressed! count: %d", ++count_
);
116 } // namespace examples