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 "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" 17 18using base::ASCIIToUTF16; 19 20namespace { 21const char kLabelButton[] = "Label Button"; 22const char kMultiLineText[] = "Multi-Line\nButton Text Is Here To Stay!\n123"; 23const 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 28namespace views { 29namespace examples { 30 31ButtonExample::ButtonExample() 32 : ExampleBase("Button"), 33 label_button_(NULL), 34 image_button_(NULL), 35 icon_(NULL), 36 count_(0) { 37 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 38 icon_ = rb.GetImageNamed(IDR_CLOSE_H).ToImageSkia(); 39} 40 41ButtonExample::~ButtonExample() { 42} 43 44void ButtonExample::CreateExampleView(View* container) { 45 container->set_background(Background::CreateSolidBackground(SK_ColorWHITE)); 46 container->SetLayoutManager(new BoxLayout(BoxLayout::kVertical, 10, 10, 10)); 47 48 label_button_ = new LabelButton(this, ASCIIToUTF16(kLabelButton)); 49 label_button_->SetFocusable(true); 50 container->AddChildView(label_button_); 51 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); 57 58 container->AddChildView(new BlueButton(this, ASCIIToUTF16("Blue Button"))); 59 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_); 70} 71 72void 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)); 80 } else { 81 label_button_->SetText(ASCIIToUTF16( 82 label_button_->GetText().empty() ? kLongText : 83 label_button_->GetText().length() > 50 ? kLabelButton : "")); 84 } 85 } else if (event.IsAltDown()) { 86 label_button_->SetImage(Button::STATE_NORMAL, 87 label_button_->GetImage(Button::STATE_NORMAL).isNull() ? 88 *icon_ : gfx::ImageSkia()); 89 } else { 90 label_button_->SetHorizontalAlignment( 91 static_cast<gfx::HorizontalAlignment>( 92 (label_button_->GetHorizontalAlignment() + 1) % 3)); 93 } 94 } else if (event.IsShiftDown()) { 95 if (event.IsAltDown()) { 96 label_button_->SetFocusable(!label_button_->IsFocusable()); 97 } else { 98 label_button_->SetStyle(static_cast<Button::ButtonStyle>( 99 (label_button_->style() + 1) % Button::STYLE_COUNT)); 100 } 101 } else if (event.IsAltDown()) { 102 label_button_->SetIsDefault(!label_button_->is_default()); 103 } else { 104 label_button_->SetMinSize(gfx::Size()); 105 } 106 example_view()->GetLayoutManager()->Layout(example_view()); 107} 108 109void ButtonExample::ButtonPressed(Button* sender, const ui::Event& event) { 110 if (sender == label_button_) 111 LabelButtonPressed(event); 112 else 113 PrintStatus("Image Button Pressed! count: %d", ++count_); 114} 115 116} // namespace examples 117} // namespace views 118