Home | History | Annotate | Download | only in examples
      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 using base::ASCIIToUTF16;
     20 
     21 namespace {
     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";
     28 }  // namespace
     29 
     30 namespace views {
     31 namespace examples {
     32 
     33 ButtonExample::ButtonExample()
     34     : ExampleBase("Button"),
     35       text_button_(NULL),
     36       label_button_(NULL),
     37       image_button_(NULL),
     38       alignment_(TextButton::ALIGN_LEFT),
     39       use_native_theme_border_(false),
     40       icon_(NULL),
     41       count_(0) {
     42   ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
     43   icon_ = rb.GetImageNamed(IDR_CLOSE_H).ToImageSkia();
     44 }
     45 
     46 ButtonExample::~ButtonExample() {
     47 }
     48 
     49 void ButtonExample::CreateExampleView(View* container) {
     50   container->set_background(Background::CreateSolidBackground(SK_ColorWHITE));
     51   container->SetLayoutManager(new BoxLayout(BoxLayout::kVertical, 10, 10, 10));
     52 
     53   text_button_ = new TextButton(this, ASCIIToUTF16(kTextButton));
     54   text_button_->SetFocusable(true);
     55   container->AddChildView(text_button_);
     56 
     57   label_button_ = new LabelButton(this, ASCIIToUTF16(kLabelButton));
     58   label_button_->SetFocusable(true);
     59   container->AddChildView(label_button_);
     60 
     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);
     66 
     67   container->AddChildView(new BlueButton(this, ASCIIToUTF16("Blue Button")));
     68 
     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_);
     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_->SetBorder(scoped_ptr<views::Border>(
    129             new TextButtonNativeThemeBorder(text_button_)));
    130       } else {
    131         text_button_->SetBorder(
    132             scoped_ptr<views::Border>(new TextButtonDefaultBorder()));
    133       }
    134     }
    135   } else if (event.IsAltDown()) {
    136     text_button_->SetIsDefault(!text_button_->is_default());
    137   } else {
    138     text_button_->ClearMaxTextSize();
    139   }
    140   example_view()->GetLayoutManager()->Layout(example_view());
    141 }
    142 
    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));
    151       } else {
    152         label_button_->SetText(ASCIIToUTF16(
    153             label_button_->GetText().empty() ? kLongText :
    154                 label_button_->GetText().length() > 50 ? kLabelButton : ""));
    155       }
    156     } else if (event.IsAltDown()) {
    157       label_button_->SetImage(Button::STATE_NORMAL,
    158           label_button_->GetImage(Button::STATE_NORMAL).isNull() ?
    159           *icon_ : gfx::ImageSkia());
    160     } else {
    161       label_button_->SetHorizontalAlignment(
    162           static_cast<gfx::HorizontalAlignment>(
    163               (label_button_->GetHorizontalAlignment() + 1) % 3));
    164     }
    165   } else if (event.IsShiftDown()) {
    166     if (event.IsAltDown()) {
    167       label_button_->SetFocusable(!label_button_->IsFocusable());
    168     } else {
    169       label_button_->SetStyle(static_cast<Button::ButtonStyle>(
    170           (label_button_->style() + 1) % Button::STYLE_COUNT));
    171     }
    172   } else if (event.IsAltDown()) {
    173     label_button_->SetIsDefault(!label_button_->is_default());
    174   } else {
    175     label_button_->set_min_size(gfx::Size());
    176   }
    177   example_view()->GetLayoutManager()->Layout(example_view());
    178 }
    179 
    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);
    185   else
    186     PrintStatus("Image Button Pressed! count: %d", ++count_);
    187 }
    188 
    189 }  // namespace examples
    190 }  // namespace views
    191