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. 4 5 #include "ui/views/examples/button_example.h" 6 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" 18 19 namespace { 20 const char kLabelButton[] = "Label Button"; 21 const char kTextButton[] = "Text 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"; 26 } // namespace 27 28 namespace views { 29 namespace examples { 30 31 ButtonExample::ButtonExample() 32 : ExampleBase("Button"), 33 text_button_(NULL), 34 label_button_(NULL), 35 image_button_(NULL), 36 alignment_(TextButton::ALIGN_LEFT), 37 use_native_theme_border_(false), 38 icon_(NULL), 39 count_(0) { 40 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 41 icon_ = rb.GetImageNamed(IDR_CLOSE_H).ToImageSkia(); 42 } 43 44 ButtonExample::~ButtonExample() { 45 } 46 47 void ButtonExample::CreateExampleView(View* container) { 48 container->set_background(Background::CreateSolidBackground(SK_ColorWHITE)); 49 container->SetLayoutManager(new BoxLayout(BoxLayout::kVertical, 10, 10, 10)); 50 51 text_button_ = new TextButton(this, ASCIIToUTF16(kTextButton)); 52 text_button_->SetFocusable(true); 53 container->AddChildView(text_button_); 54 55 label_button_ = new LabelButton(this, ASCIIToUTF16(kLabelButton)); 56 label_button_->SetFocusable(true); 57 container->AddChildView(label_button_); 58 59 LabelButton* disabled_button = 60 new LabelButton(this, ASCIIToUTF16("Disabled Button")); 61 disabled_button->SetStyle(Button::STYLE_BUTTON); 62 disabled_button->SetState(Button::STATE_DISABLED); 63 container->AddChildView(disabled_button); 64 65 container->AddChildView(new BlueButton(this, ASCIIToUTF16("Blue Button"))); 66 67 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 68 image_button_ = new ImageButton(this); 69 image_button_->SetFocusable(true); 70 image_button_->SetImage(ImageButton::STATE_NORMAL, 71 rb.GetImageNamed(IDR_CLOSE).ToImageSkia()); 72 image_button_->SetImage(ImageButton::STATE_HOVERED, 73 rb.GetImageNamed(IDR_CLOSE_H).ToImageSkia()); 74 image_button_->SetImage(ImageButton::STATE_PRESSED, 75 rb.GetImageNamed(IDR_CLOSE_P).ToImageSkia()); 76 image_button_->SetOverlayImage(rb.GetImageNamed( 77 IDR_MENU_CHECK).ToImageSkia()); 78 container->AddChildView(image_button_); 79 } 80 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)); 89 } else { 90 switch (text_button_->icon_placement()) { 91 case TextButton::ICON_ON_LEFT: 92 text_button_->set_icon_placement(TextButton::ICON_ON_RIGHT); 93 break; 94 case TextButton::ICON_ON_RIGHT: 95 text_button_->set_icon_placement(TextButton::ICON_ON_LEFT); 96 break; 97 case TextButton::ICON_CENTERED: 98 // Do nothing. 99 break; 100 } 101 } 102 } else if (event.IsAltDown()) { 103 if (text_button_->HasIcon()) 104 text_button_->SetIcon(gfx::ImageSkia()); 105 else 106 text_button_->SetIcon(*icon_); 107 } else { 108 switch (alignment_) { 109 case TextButton::ALIGN_LEFT: 110 alignment_ = TextButton::ALIGN_CENTER; 111 break; 112 case TextButton::ALIGN_CENTER: 113 alignment_ = TextButton::ALIGN_RIGHT; 114 break; 115 case TextButton::ALIGN_RIGHT: 116 alignment_ = TextButton::ALIGN_LEFT; 117 break; 118 } 119 text_button_->set_alignment(alignment_); 120 } 121 } else if (event.IsShiftDown()) { 122 if (event.IsAltDown()) { 123 text_button_->SetText(ASCIIToUTF16( 124 text_button_->text().length() < 50 ? kLongText : kTextButton)); 125 } else { 126 use_native_theme_border_ = !use_native_theme_border_; 127 if (use_native_theme_border_) 128 text_button_->set_border(new TextButtonNativeThemeBorder(text_button_)); 129 else 130 text_button_->set_border(new TextButtonDefaultBorder()); 131 } 132 } else if (event.IsAltDown()) { 133 text_button_->SetIsDefault(!text_button_->is_default()); 134 } else { 135 text_button_->ClearMaxTextSize(); 136 } 137 example_view()->GetLayoutManager()->Layout(example_view()); 138 } 139 140 void ButtonExample::LabelButtonPressed(const ui::Event& event) { 141 PrintStatus("Label Button Pressed! count: %d", ++count_); 142 if (event.IsControlDown()) { 143 if (event.IsShiftDown()) { 144 if (event.IsAltDown()) { 145 label_button_->SetTextMultiLine(!label_button_->GetTextMultiLine()); 146 label_button_->SetText(ASCIIToUTF16( 147 label_button_->GetTextMultiLine() ? kMultiLineText : kLabelButton)); 148 } else { 149 label_button_->SetText(ASCIIToUTF16( 150 label_button_->GetText().empty() ? kLongText : 151 label_button_->GetText().length() > 50 ? kLabelButton : "")); 152 } 153 } else if (event.IsAltDown()) { 154 label_button_->SetImage(Button::STATE_NORMAL, 155 label_button_->GetImage(Button::STATE_NORMAL).isNull() ? 156 *icon_ : gfx::ImageSkia()); 157 } else { 158 label_button_->SetHorizontalAlignment( 159 static_cast<gfx::HorizontalAlignment>( 160 (label_button_->GetHorizontalAlignment() + 1) % 3)); 161 } 162 } else if (event.IsShiftDown()) { 163 if (event.IsAltDown()) { 164 label_button_->SetFocusable(!label_button_->focusable()); 165 } else { 166 label_button_->SetStyle(static_cast<Button::ButtonStyle>( 167 (label_button_->style() + 1) % Button::STYLE_COUNT)); 168 } 169 } else if (event.IsAltDown()) { 170 label_button_->SetIsDefault(!label_button_->is_default()); 171 } else { 172 label_button_->set_min_size(gfx::Size()); 173 } 174 example_view()->GetLayoutManager()->Layout(example_view()); 175 } 176 177 void ButtonExample::ButtonPressed(Button* sender, const ui::Event& event) { 178 if (sender == text_button_) 179 TextButtonPressed(event); 180 else if (sender == label_button_) 181 LabelButtonPressed(event); 182 else 183 PrintStatus("Image Button Pressed! count: %d", ++count_); 184 } 185 186 } // namespace examples 187 } // namespace views 188