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