| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/examples/button_example.h" | 5 #include "ui/views/examples/button_example.h" |
| 6 | 6 |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "grit/ui_resources.h" | 8 #include "grit/ui_resources.h" |
| 9 #include "ui/base/resource/resource_bundle.h" | 9 #include "ui/base/resource/resource_bundle.h" |
| 10 #include "ui/gfx/image/image.h" | 10 #include "ui/gfx/image/image.h" |
| 11 #include "ui/views/controls/button/image_button.h" | 11 #include "ui/views/controls/button/image_button.h" |
| 12 #include "ui/views/controls/button/text_button.h" | 12 #include "ui/views/controls/button/text_button.h" |
| 13 #include "ui/views/layout/box_layout.h" | 13 #include "ui/views/layout/box_layout.h" |
| 14 #include "ui/views/view.h" | 14 #include "ui/views/view.h" |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 const int kLayoutSpacing = 10; // pixels | 17 const int kLayoutSpacing = 10; // pixels |
| 18 } // namespace | 18 } // namespace |
| 19 | 19 |
| 20 namespace views { | 20 namespace views { |
| 21 namespace examples { | 21 namespace examples { |
| 22 | 22 |
| 23 ButtonExample::ButtonExample() | 23 ButtonExample::ButtonExample() |
| 24 : ExampleBase("Button"), | 24 : ExampleBase("Button"), |
| 25 text_button_(NULL), | 25 text_button_(NULL), |
| 26 label_button_(NULL), |
| 26 image_button_(NULL), | 27 image_button_(NULL), |
| 27 alignment_(TextButton::ALIGN_LEFT), | 28 alignment_(TextButton::ALIGN_LEFT), |
| 28 use_native_theme_border_(false), | 29 use_native_theme_border_(false), |
| 29 icon_(NULL), | 30 icon_(NULL), |
| 30 count_(0) { | 31 count_(0) { |
| 31 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 32 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 32 icon_ = rb.GetImageNamed(IDR_CLOSE_SA_H).ToImageSkia(); | 33 icon_ = rb.GetImageNamed(IDR_CLOSE_SA_H).ToImageSkia(); |
| 33 } | 34 } |
| 34 | 35 |
| 35 ButtonExample::~ButtonExample() { | 36 ButtonExample::~ButtonExample() { |
| 36 } | 37 } |
| 37 | 38 |
| 38 void ButtonExample::CreateExampleView(View* container) { | 39 void ButtonExample::CreateExampleView(View* container) { |
| 39 container->SetLayoutManager( | 40 container->SetLayoutManager( |
| 40 new BoxLayout(BoxLayout::kVertical, 0, 0, kLayoutSpacing)); | 41 new BoxLayout(BoxLayout::kVertical, 0, 0, kLayoutSpacing)); |
| 41 | 42 |
| 42 text_button_ = new TextButton(this, ASCIIToUTF16("Text Button")); | 43 text_button_ = new TextButton(this, ASCIIToUTF16("Text Button")); |
| 44 text_button_->set_focusable(true); |
| 45 text_button_->SetHoverColor(SK_ColorRED); |
| 43 container->AddChildView(text_button_); | 46 container->AddChildView(text_button_); |
| 44 | 47 |
| 48 label_button_ = new LabelButton(this, ASCIIToUTF16("Label Button")); |
| 49 label_button_->set_focusable(true); |
| 50 label_button_->SetTextColor(CustomButton::BS_HOT, SK_ColorRED); |
| 51 container->AddChildView(label_button_); |
| 52 |
| 45 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 53 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 46 image_button_ = new ImageButton(this); | 54 image_button_ = new ImageButton(this); |
| 55 image_button_->set_focusable(true); |
| 47 image_button_->SetImage(ImageButton::BS_NORMAL, | 56 image_button_->SetImage(ImageButton::BS_NORMAL, |
| 48 rb.GetImageNamed(IDR_CLOSE).ToImageSkia()); | 57 rb.GetImageNamed(IDR_CLOSE).ToImageSkia()); |
| 49 image_button_->SetImage(ImageButton::BS_HOT, | 58 image_button_->SetImage(ImageButton::BS_HOT, |
| 50 rb.GetImageNamed(IDR_CLOSE_H).ToImageSkia()); | 59 rb.GetImageNamed(IDR_CLOSE_H).ToImageSkia()); |
| 51 image_button_->SetImage(ImageButton::BS_PUSHED, | 60 image_button_->SetImage(ImageButton::BS_PUSHED, |
| 52 rb.GetImageNamed(IDR_CLOSE_P).ToImageSkia()); | 61 rb.GetImageNamed(IDR_CLOSE_P).ToImageSkia()); |
| 53 image_button_->SetOverlayImage(rb.GetImageNamed( | 62 image_button_->SetOverlayImage(rb.GetImageNamed( |
| 54 IDR_MENU_CHECK).ToImageSkia()); | 63 IDR_MENU_CHECK).ToImageSkia()); |
| 55 container->AddChildView(image_button_); | 64 container->AddChildView(image_button_); |
| 56 } | 65 } |
| 57 | 66 |
| 58 void ButtonExample::ButtonPressed(Button* sender, const ui::Event& event) { | 67 void ButtonExample::TextButtonPressed(const ui::Event& event) { |
| 59 PrintStatus("Pressed! count: %d", ++count_); | 68 PrintStatus("Text Button Pressed! count: %d", ++count_); |
| 60 | |
| 61 if (event.IsControlDown()) { | 69 if (event.IsControlDown()) { |
| 62 if (event.IsShiftDown()) { | 70 if (event.IsShiftDown()) { |
| 63 if (event.IsAltDown()) { | 71 if (event.IsAltDown()) { |
| 64 text_button_->SetMultiLine(!text_button_->multi_line()); | 72 text_button_->SetMultiLine(!text_button_->multi_line()); |
| 65 if (text_button_->multi_line()) { | 73 if (text_button_->multi_line()) { |
| 66 text_button_->SetText(ASCIIToUTF16("Multi-line text\n") + | 74 text_button_->SetText(ASCIIToUTF16("Multi-line text\n") + |
| 67 ASCIIToUTF16("is here to stay all the way!\n") + | 75 ASCIIToUTF16("is here to stay all the way!\n") + |
| 68 ASCIIToUTF16("123")); | 76 ASCIIToUTF16("123")); |
| 69 } else { | 77 } else { |
| 70 text_button_->SetText(ASCIIToUTF16("Button")); | 78 text_button_->SetText(ASCIIToUTF16("Text Button")); |
| 71 } | 79 } |
| 72 } else { | 80 } else { |
| 73 switch (text_button_->icon_placement()) { | 81 switch (text_button_->icon_placement()) { |
| 74 case TextButton::ICON_ON_LEFT: | 82 case TextButton::ICON_ON_LEFT: |
| 75 text_button_->set_icon_placement(TextButton::ICON_ON_RIGHT); | 83 text_button_->set_icon_placement(TextButton::ICON_ON_RIGHT); |
| 76 break; | 84 break; |
| 77 case TextButton::ICON_ON_RIGHT: | 85 case TextButton::ICON_ON_RIGHT: |
| 78 text_button_->set_icon_placement(TextButton::ICON_ON_LEFT); | 86 text_button_->set_icon_placement(TextButton::ICON_ON_LEFT); |
| 79 break; | 87 break; |
| 80 case TextButton::ICON_CENTERED: | 88 case TextButton::ICON_CENTERED: |
| (...skipping 15 matching lines...) Expand all Loading... |
| 96 alignment_ = TextButton::ALIGN_RIGHT; | 104 alignment_ = TextButton::ALIGN_RIGHT; |
| 97 break; | 105 break; |
| 98 case TextButton::ALIGN_RIGHT: | 106 case TextButton::ALIGN_RIGHT: |
| 99 alignment_ = TextButton::ALIGN_LEFT; | 107 alignment_ = TextButton::ALIGN_LEFT; |
| 100 break; | 108 break; |
| 101 } | 109 } |
| 102 text_button_->set_alignment(alignment_); | 110 text_button_->set_alignment(alignment_); |
| 103 } | 111 } |
| 104 } else if (event.IsShiftDown()) { | 112 } else if (event.IsShiftDown()) { |
| 105 if (event.IsAltDown()) { | 113 if (event.IsAltDown()) { |
| 106 if (text_button_->text().length() < 10) { | 114 if (text_button_->text().length() < 20) { |
| 107 text_button_->SetText( | 115 text_button_->SetText( |
| 108 ASCIIToUTF16("Startof") + | 116 ASCIIToUTF16("Startof") + |
| 109 ASCIIToUTF16("ReallyReallyReallyReallyReallyReallyReally") + | 117 ASCIIToUTF16("ReallyReallyReallyReallyReallyReallyReally") + |
| 110 ASCIIToUTF16("ReallyReallyReallyReallyReallyReallyReally") + | 118 ASCIIToUTF16("ReallyReallyReallyReallyReallyReallyReally") + |
| 111 ASCIIToUTF16("ReallyReallyReallyReallyReallyReallyReally") + | 119 ASCIIToUTF16("ReallyReallyReallyReallyReallyReallyReally") + |
| 112 ASCIIToUTF16("LongButtonText")); | 120 ASCIIToUTF16("LongButtonText")); |
| 113 } else { | 121 } else { |
| 114 text_button_->SetText(ASCIIToUTF16("Button")); | 122 text_button_->SetText(ASCIIToUTF16("Text Button")); |
| 115 } | 123 } |
| 116 } else { | 124 } else { |
| 117 use_native_theme_border_ = !use_native_theme_border_; | 125 use_native_theme_border_ = !use_native_theme_border_; |
| 118 if (use_native_theme_border_) | 126 if (use_native_theme_border_) |
| 119 text_button_->set_border(new TextButtonNativeThemeBorder(text_button_)); | 127 text_button_->set_border(new TextButtonNativeThemeBorder(text_button_)); |
| 120 else | 128 else |
| 121 text_button_->set_border(new TextButtonBorder()); | 129 text_button_->set_border(new TextButtonBorder()); |
| 122 } | 130 } |
| 123 } else if (event.IsAltDown()) { | 131 } else if (event.IsAltDown()) { |
| 124 text_button_->SetIsDefault(!text_button_->is_default()); | 132 text_button_->SetIsDefault(!text_button_->is_default()); |
| 133 } else { |
| 134 text_button_->ClearMaxTextSize(); |
| 125 } | 135 } |
| 126 example_view()->GetLayoutManager()->Layout(example_view()); | 136 example_view()->GetLayoutManager()->Layout(example_view()); |
| 127 } | 137 } |
| 128 | 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(label_button_->GetTextMultiLine() ? |
| 146 "Multi-line text\nis here to stay all the way!\n123" : |
| 147 "Label Button")); |
| 148 } else { |
| 149 label_button_->SetText(!label_button_->GetText().empty() ? |
| 150 string16() : ASCIIToUTF16("Label Button")); |
| 151 } |
| 152 } else if (event.IsAltDown()) { |
| 153 label_button_->SetImage(CustomButton::BS_NORMAL, |
| 154 label_button_->GetImage(CustomButton::BS_NORMAL).isNull() ? |
| 155 *icon_ : gfx::ImageSkia()); |
| 156 } else { |
| 157 label_button_->SetHorizontalAlignment(static_cast<Label::Alignment>( |
| 158 (label_button_->GetHorizontalAlignment() + 1) % 3)); |
| 159 } |
| 160 } else if (event.IsShiftDown()) { |
| 161 if (event.IsAltDown()) { |
| 162 label_button_->SetText(ASCIIToUTF16( |
| 163 label_button_->GetText().length() > 20 ? "Label Button" : |
| 164 "StartofReallyReallyReallyReallyReallyReallyReally" |
| 165 "ReallyReallyReallyReallyReallyReallyReally" |
| 166 "ReallyReallyReallyReallyReallyReallyReallyLongButtonText")); |
| 167 } else { |
| 168 label_button_->SetNativeTheme(!label_button_->native_theme()); |
| 169 } |
| 170 } else if (event.IsAltDown()) { |
| 171 label_button_->SetDefaultButton(!label_button_->default_button()); |
| 172 } else { |
| 173 label_button_->set_min_size(gfx::Size()); |
| 174 } |
| 175 example_view()->GetLayoutManager()->Layout(example_view()); |
| 176 } |
| 177 |
| 178 void ButtonExample::ButtonPressed(Button* sender, const ui::Event& event) { |
| 179 if (sender == text_button_) |
| 180 TextButtonPressed(event); |
| 181 else if (sender == label_button_) |
| 182 LabelButtonPressed(event); |
| 183 else |
| 184 PrintStatus("Image Button Pressed! count: %d", ++count_); |
| 185 } |
| 186 |
| 129 } // namespace examples | 187 } // namespace examples |
| 130 } // namespace views | 188 } // namespace views |
| OLD | NEW |