Chromium Code Reviews| 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/checkbox.h" | 11 #include "ui/views/controls/button/image_button.h" |
| 12 #include "ui/views/layout/fill_layout.h" | 12 #include "ui/views/controls/button/text_button.h" |
| 13 #include "ui/views/layout/box_layout.h" | |
| 13 #include "ui/views/view.h" | 14 #include "ui/views/view.h" |
| 14 | 15 |
| 16 namespace { | |
| 17 const int kLayoutSpacing = 10; // pixels | |
| 18 } // namespace | |
| 19 | |
| 15 namespace views { | 20 namespace views { |
| 16 namespace examples { | 21 namespace examples { |
| 17 | 22 |
| 18 ButtonExample::ButtonExample() | 23 ButtonExample::ButtonExample() |
| 19 : ExampleBase("Text Button"), | 24 : ExampleBase("Button"), |
| 25 text_button_(NULL), | |
| 26 image_button_(NULL), | |
| 20 alignment_(TextButton::ALIGN_LEFT), | 27 alignment_(TextButton::ALIGN_LEFT), |
| 21 use_native_theme_border_(false), | 28 use_native_theme_border_(false), |
| 22 icon_(NULL), | 29 icon_(NULL), |
| 23 count_(0) { | 30 count_(0) { |
| 24 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 31 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 25 icon_ = rb.GetImageNamed(IDR_CLOSE_SA_H).ToSkBitmap(); | 32 icon_ = rb.GetImageNamed(IDR_CLOSE_SA_H).ToSkBitmap(); |
| 26 } | 33 } |
| 27 | 34 |
| 28 ButtonExample::~ButtonExample() { | 35 ButtonExample::~ButtonExample() { |
| 29 } | 36 } |
| 30 | 37 |
| 31 void ButtonExample::CreateExampleView(View* container) { | 38 void ButtonExample::CreateExampleView(View* container) { |
| 32 TextButton* tb = new TextButton(this, ASCIIToUTF16("Button")); | 39 container->SetLayoutManager( |
| 33 button_ = tb; | 40 new BoxLayout(BoxLayout::kVertical, 0, 0, kLayoutSpacing)); |
| 34 container->SetLayoutManager(new FillLayout); | 41 |
| 35 container->AddChildView(button_); | 42 text_button_ = new TextButton(this, ASCIIToUTF16("Text Button")); |
| 43 container->AddChildView(text_button_); | |
| 44 | |
| 45 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 46 image_button_ = new ImageButton(this); | |
| 47 image_button_->SetImage(ImageButton::BS_NORMAL, | |
| 48 rb.GetImageNamed(IDR_CLOSE).ToSkBitmap()); | |
| 49 image_button_->SetImage(ImageButton::BS_HOT, | |
| 50 rb.GetImageNamed(IDR_CLOSE_H).ToSkBitmap()); | |
| 51 image_button_->SetImage(ImageButton::BS_PUSHED, | |
| 52 rb.GetImageNamed(IDR_CLOSE_P).ToSkBitmap()); | |
| 53 image_button_->SetOverlayImage(rb.GetImageNamed(IDR_MENU_CHECK).ToSkBitmap()); | |
| 54 container->AddChildView(image_button_); | |
| 36 } | 55 } |
| 37 | 56 |
| 38 void ButtonExample::ButtonPressed(Button* sender, const Event& event) { | 57 void ButtonExample::ButtonPressed(Button* sender, const Event& event) { |
| 39 PrintStatus("Pressed! count: %d", ++count_); | 58 PrintStatus("Pressed! count: %d", ++count_); |
| 40 | 59 |
| 41 if (event.IsControlDown()) { | 60 if (event.IsControlDown()) { |
| 42 if (event.IsShiftDown()) { | 61 if (event.IsShiftDown()) { |
| 43 if (event.IsAltDown()) { | 62 if (!event.IsAltDown()) { |
| 44 button_->SetMultiLine(!button_->multi_line()); | 63 text_button_->SetMultiLine(!text_button_->multi_line()); |
| 45 if (button_->multi_line()) { | 64 if (text_button_->multi_line()) { |
| 46 button_->SetText(ASCIIToUTF16("Multi-line text\n") + | 65 text_button_->SetText(ASCIIToUTF16("Multi-line text\n") + |
| 47 ASCIIToUTF16("is here to stay all the way!\n") + | 66 ASCIIToUTF16("is here to stay all the way!\n") + |
| 48 ASCIIToUTF16("123")); | 67 ASCIIToUTF16("123")); |
| 49 } else { | 68 } else { |
| 50 button_->SetText(ASCIIToUTF16("Button")); | 69 text_button_->SetText(ASCIIToUTF16("Button")); |
| 51 } | 70 } |
| 52 } else { | 71 } else { |
| 53 switch(button_->icon_placement()) { | 72 switch(text_button_->icon_placement()) { |
| 54 case TextButton::ICON_ON_LEFT: | 73 case TextButton::ICON_ON_LEFT: |
| 55 button_->set_icon_placement(TextButton::ICON_ON_RIGHT); | 74 text_button_->set_icon_placement(TextButton::ICON_ON_RIGHT); |
| 56 break; | 75 break; |
| 57 case TextButton::ICON_ON_RIGHT: | 76 case TextButton::ICON_ON_RIGHT: |
| 58 button_->set_icon_placement(TextButton::ICON_ON_LEFT); | 77 text_button_->set_icon_placement(TextButton::ICON_ON_LEFT); |
| 59 break; | 78 break; |
| 60 } | 79 } |
| 61 } | 80 } |
| 62 } else if (event.IsAltDown()) { | 81 } else if (event.IsAltDown()) { |
| 63 if (button_->HasIcon()) | 82 if (text_button_->HasIcon()) |
| 64 button_->SetIcon(SkBitmap()); | 83 text_button_->SetIcon(SkBitmap()); |
| 65 else | 84 else |
| 66 button_->SetIcon(*icon_); | 85 text_button_->SetIcon(*icon_); |
| 67 } else { | 86 } else { |
| 68 switch(alignment_) { | 87 switch(alignment_) { |
| 69 case TextButton::ALIGN_LEFT: | 88 case TextButton::ALIGN_LEFT: |
| 70 alignment_ = TextButton::ALIGN_CENTER; | 89 alignment_ = TextButton::ALIGN_CENTER; |
| 71 break; | 90 break; |
| 72 case TextButton::ALIGN_CENTER: | 91 case TextButton::ALIGN_CENTER: |
| 73 alignment_ = TextButton::ALIGN_RIGHT; | 92 alignment_ = TextButton::ALIGN_RIGHT; |
| 74 break; | 93 break; |
| 75 case TextButton::ALIGN_RIGHT: | 94 case TextButton::ALIGN_RIGHT: |
| 76 alignment_ = TextButton::ALIGN_LEFT; | 95 alignment_ = TextButton::ALIGN_LEFT; |
| 77 break; | 96 break; |
| 78 } | 97 } |
| 79 button_->set_alignment(alignment_); | 98 text_button_->set_alignment(alignment_); |
| 80 } | 99 } |
| 81 } else if (event.IsShiftDown()) { | 100 } else if (event.IsShiftDown()) { |
| 82 if (event.IsAltDown()) { | 101 if (event.IsAltDown()) { |
| 83 if (button_->text().length() < 10) { | 102 if (text_button_->text().length() < 10) { |
| 84 button_->SetText( | 103 text_button_->SetText( |
| 85 ASCIIToUTF16("Startof") + | 104 ASCIIToUTF16("Startof") + |
| 86 ASCIIToUTF16("ReallyReallyReallyReallyReallyReallyReally") + | 105 ASCIIToUTF16("ReallyReallyReallyReallyReallyReallyReally") + |
| 87 ASCIIToUTF16("ReallyReallyReallyReallyReallyReallyReally") + | 106 ASCIIToUTF16("ReallyReallyReallyReallyReallyReallyReally") + |
| 88 ASCIIToUTF16("ReallyReallyReallyReallyReallyReallyReally") + | 107 ASCIIToUTF16("ReallyReallyReallyReallyReallyReallyReally") + |
| 89 ASCIIToUTF16("LongButtonText")); | 108 ASCIIToUTF16("LongButtonText")); |
| 90 } else { | 109 } else { |
| 91 button_->SetText(ASCIIToUTF16("Button")); | 110 text_button_->SetText(ASCIIToUTF16("Button")); |
| 92 } | 111 } |
| 93 } else { | 112 } else { |
| 94 use_native_theme_border_ = !use_native_theme_border_; | 113 use_native_theme_border_ = !use_native_theme_border_; |
| 95 if (use_native_theme_border_) | 114 if (use_native_theme_border_) |
| 96 button_->set_border(new TextButtonNativeThemeBorder(button_)); | 115 text_button_->set_border(new TextButtonNativeThemeBorder(text_button_)); |
| 97 else | 116 else |
| 98 button_->set_border(new TextButtonBorder()); | 117 text_button_->set_border(new TextButtonBorder()); |
| 99 } | 118 } |
| 100 } else if (event.IsAltDown()) { | 119 } else if (event.IsAltDown()) { |
| 101 button_->SetIsDefault(!button_->is_default()); | 120 text_button_->SetIsDefault(!text_button_->is_default()); |
| 102 } | 121 } |
| 122 example_view()->GetLayoutManager()->Layout(example_view()); | |
|
sadrul
2012/04/23 21:06:26
Is this necessary?
James Cook
2012/04/23 22:11:25
Yeah, since I changed the layout manager to box la
| |
| 103 } | 123 } |
| 104 | 124 |
| 105 } // namespace examples | 125 } // namespace examples |
| 106 } // namespace views | 126 } // namespace views |
| OLD | NEW |