Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(283)

Unified Diff: ui/views/controls/button/label_button.cc

Issue 11068012: Add new views::LabelButton and LabelButtonBorder. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rename LabelButtonBorder, remove GetLabel, etc. Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/views/controls/button/label_button.cc
diff --git a/ui/views/controls/button/label_button.cc b/ui/views/controls/button/label_button.cc
new file mode 100644
index 0000000000000000000000000000000000000000..af22acf536a0c41b351ea86d82e19168dd9b516a
--- /dev/null
+++ b/ui/views/controls/button/label_button.cc
@@ -0,0 +1,291 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/views/controls/button/label_button.h"
+
+#include "base/logging.h"
+#include "grit/ui_resources.h"
+#include "ui/base/animation/throb_animation.h"
+#include "ui/base/resource/resource_bundle.h"
+#include "ui/gfx/canvas.h"
+#include "ui/gfx/image/image.h"
+#include "ui/views/controls/button/label_button_border.h"
+#include "ui/views/focus_border.h"
+#include "ui/views/layout/grid_layout.h"
+
+#if defined(OS_WIN)
+#include "ui/gfx/color_utils.h"
+#include "ui/base/native_theme/native_theme_win.h"
+#endif
+
+namespace views {
+
+namespace {
+
+// The spacing between the icon and text.
+static const int kSpacing = 5;
+
+// The length of the hover fade animation.
+static const int kHoverAnimationDurationMs = 170;
+
+} // namespace
+
+LabelButton::LabelButton(ButtonListener* listener, const string16& text)
+ : CustomButton(listener),
+ image_(new ImageView()),
+ label_(new Label(text)),
+ default_button_(false),
+ native_theme_(false) {
+ set_border(new LabelButtonBorder(this));
+ // Inset the button focus rect from the actual border; match Windows roughly.
+ set_focus_border(FocusBorder::CreateDashedFocusBorder(3, 3, 3, 3));
+ SetAnimationDuration(kHoverAnimationDurationMs);
+
+ image_->set_owned_by_client();
+ image_->set_interactive(false);
+
+ label_->set_owned_by_client();
+ label_->SetAutoColorReadabilityEnabled(false);
+ label_->SetHorizontalAlignment(Label::ALIGN_LEFT);
+ label_->SetElideBehavior(Label::ELIDE_AT_END);
+
+ // Initialize the colors and layout for a Views-themed button.
+ SetNativeTheme(false);
+}
+
+LabelButton::~LabelButton() {}
+
+const gfx::ImageSkia& LabelButton::GetImage(ButtonState for_state) {
+ if (for_state != BS_NORMAL && button_state_images_[for_state].isNull())
+ return button_state_images_[BS_NORMAL];
+ return button_state_images_[for_state];
+}
+
+void LabelButton::SetImage(ButtonState for_state, const gfx::ImageSkia& image) {
+ const bool was_empty = GetImage(state()).isNull();
+ button_state_images_[for_state] = image;
+ if (was_empty != GetImage(state()).isNull())
+ UpdateLayout();
+}
+
+const string16& LabelButton::GetText() const {
+ return label_->text();
+}
+
+void LabelButton::SetText(const string16& text) {
+ const bool was_empty = GetText().empty();
+ label_->SetText(text);
+ if (was_empty != GetText().empty())
+ UpdateLayout();
+}
+
+void LabelButton::SetTextColor(ButtonState for_state, SkColor color) {
+ button_state_colors_[for_state] = color;
+ if (for_state == BS_DISABLED)
+ label_->SetDisabledColor(color);
+ else if (for_state == state())
+ label_->SetEnabledColor(color);
+}
+
+bool LabelButton::GetTextMultiLine() const {
+ return label_->is_multi_line();
+}
+
+void LabelButton::SetTextMultiLine(bool text_multi_line) {
+ label_->SetMultiLine(text_multi_line);
+}
+
+Label::Alignment LabelButton::GetHorizontalAlignment() const {
+ return label_->horizontal_alignment();
+}
+
+void LabelButton::SetHorizontalAlignment(Label::Alignment alignment) {
+ label_->SetHorizontalAlignment(alignment);
+ UpdateLayout();
+}
+
+void LabelButton::SetDefaultButton(bool default_button) {
+ if (default_button == default_button_)
+ return;
+ default_button_ = default_button;
+ ui::Accelerator accel(ui::VKEY_RETURN, ui::EF_NONE);
+ default_button_ ? AddAccelerator(accel) : RemoveAccelerator(accel);
+}
+
+void LabelButton::SetNativeTheme(bool native_theme) {
+ native_theme_ = native_theme;
+ static_cast<LabelButtonBorder*>(border())->set_native_theme(native_theme);
+
+ const ui::NativeTheme* theme = ui::NativeTheme::instance();
+ SkColor colors[BS_COUNT] = {
+ theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonEnabledColor),
+ theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor),
+ theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor),
+ theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonDisabledColor),
+ };
+#if defined(OS_WIN)
+ if (native_theme) {
+ colors[BS_NORMAL] = color_utils::GetSysSkColor(COLOR_BTNTEXT);
+ colors[BS_HOT] = color_utils::GetSysSkColor(COLOR_BTNTEXT);
+ colors[BS_PUSHED] = color_utils::GetSysSkColor(COLOR_BTNTEXT);
+ colors[BS_DISABLED] = color_utils::GetSysSkColor(COLOR_GRAYTEXT);
+ }
+#endif
+ for (size_t state = BS_NORMAL; state < BS_COUNT; state++)
+ SetTextColor(static_cast<ButtonState>(state), colors[state]);
+
+ UpdateLayout();
+}
+
+void LabelButton::StateChanged() {
+ const bool was_empty = image_->GetImage().isNull();
+ image_->SetImage(GetImage(state()));
+ if (was_empty != image_->GetImage().isNull())
+ UpdateLayout();
+ const SkColor color = button_state_colors_[state()];
+ if (state() != BS_DISABLED && label_->enabled_color() != color)
+ label_->SetEnabledColor(color);
+}
+
+gfx::Size LabelButton::GetPreferredSize() {
+ gfx::Size size = label_->GetPreferredSize();
sky 2012/10/11 23:01:56 Can't you call to the layoutmanager then enlarge a
msw 2012/10/15 16:48:34 This seems necessary for the new custom Layout() a
+ size.set_height(std::max(size.height(), GetImage(state()).height()));
+ size.Enlarge(GetInsets().width() + GetImage(state()).width() + kSpacing,
+ GetInsets().height());
+
+ // Increase the minimum size monotonically with the preferred size.
+ size.SetSize(std::max(min_size_.width(), size.width()),
+ std::max(min_size_.height(), size.height()));
+ min_size_ = size;
+
+ // Return the largest known size clamped to the maximum size (if valid).
+ if (max_size_.width() > 0)
+ size.set_width(std::min(max_size_.width(), size.width()));
+ if (max_size_.height() > 0)
+ size.set_height(std::min(max_size_.height(), size.height()));
+ return size;
+}
+
+std::string LabelButton::GetClassName() const {
+ return "views/LabelButton";
+}
+
+void LabelButton::ChildPreferredSizeChanged(View* child) {
+ PreferredSizeChanged();
+}
+
+ui::NativeTheme::Part LabelButton::GetThemePart() const {
+ return ui::NativeTheme::kPushButton;
+}
+
+gfx::Rect LabelButton::GetThemePaintRect() const {
+ return GetLocalBounds();
+}
+
+ui::NativeTheme::State LabelButton::GetThemeState(
+ ui::NativeTheme::ExtraParams* params) const {
+ GetExtraParams(params);
+ switch(state()) {
+ case BS_NORMAL:
+ return ui::NativeTheme::kNormal;
+ case BS_HOT:
+ return ui::NativeTheme::kHovered;
+ case BS_PUSHED:
+ return ui::NativeTheme::kPressed;
+ case BS_DISABLED:
+ return ui::NativeTheme::kDisabled;
+ case BS_COUNT:
+ NOTREACHED() << "Unknown state: " << state();
+ }
+ return ui::NativeTheme::kNormal;
+}
+
+const ui::Animation* LabelButton::GetThemeAnimation() const {
+#if defined(OS_WIN) && !defined(USE_AURA)
+ if (!ui::NativeThemeWin::instance()->IsThemingActive())
+ return NULL;
+#endif
+ return hover_animation_.get();
+}
+
+ui::NativeTheme::State LabelButton::GetBackgroundThemeState(
+ ui::NativeTheme::ExtraParams* params) const {
+ GetExtraParams(params);
+ return ui::NativeTheme::kNormal;
+}
+
+ui::NativeTheme::State LabelButton::GetForegroundThemeState(
+ ui::NativeTheme::ExtraParams* params) const {
+ GetExtraParams(params);
+ return ui::NativeTheme::kHovered;
+}
+
+void LabelButton::GetExtraParams(ui::NativeTheme::ExtraParams* params) const {
+ params->button.checked = false;
+ params->button.indeterminate = false;
+ params->button.is_default = default_button_;
+ params->button.is_focused = HasFocus() && IsAccessibilityFocusable();
+ params->button.has_border = native_theme();
+ params->button.classic_state = 0;
+ params->button.background_color = ui::NativeTheme::instance()->GetSystemColor(
+ ui::NativeTheme::kColorId_TextButtonBackgroundColor);
+}
+
+void LabelButton::UpdateLayout() {
+ // Temporarily disregard state changes while updating layout.
+ const ButtonState saved_state = state();
+ SetState(BS_DISABLED);
sky 2012/10/11 23:01:56 As long as someone can subclass StateChanged this
msw 2012/10/15 16:48:34 This is moot with the new custom Layout() approach
+
+ RemoveAllChildViews(false);
+ GridLayout* layout = GridLayout::CreatePanel(this);
sky 2012/10/11 23:01:56 Why are you using CreatePanel here? Since you blow
msw 2012/10/15 16:48:34 This is moot with the new custom Layout() approach
+ ColumnSet* columns = layout->AddColumnSet(0);
+ layout->SetInsets(GetInsets());
+ SetLayoutManager(layout);
+
+ const bool image = !image_->GetImage().isNull();
+ // Layout with the label unless it is empty and the alignment is centered.
+ const bool label = !label_->text().empty() ||
+ (GetHorizontalAlignment() != Label::ALIGN_CENTER);
+
+ if (image) {
+ if (GetHorizontalAlignment() == Label::ALIGN_CENTER)
+ columns->AddPaddingColumn(1, 0);
+ if (GetHorizontalAlignment() != Label::ALIGN_RIGHT)
+ columns->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0,
+ GridLayout::USE_PREF, 0, 0);
+ }
+ if (label) {
+ if (image && GetHorizontalAlignment() != Label::ALIGN_RIGHT)
+ columns->AddPaddingColumn(0, kSpacing);
+ // Caution, centering a wide image and label pair is not well supported.
+ // Either view may appear truncated, and the label will not properly elide.
+ if (image && GetHorizontalAlignment() == Label::ALIGN_CENTER)
+ columns->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0,
+ GridLayout::USE_PREF, 0, 0);
+ else
+ columns->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1,
+ GridLayout::FIXED, 0, 0);
+ if (image && GetHorizontalAlignment() == Label::ALIGN_RIGHT)
+ columns->AddPaddingColumn(0, kSpacing);
+ }
+ if (image) {
+ if (GetHorizontalAlignment() == Label::ALIGN_RIGHT)
+ columns->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0,
+ GridLayout::USE_PREF, 0, 0);
+ else if (GetHorizontalAlignment() == Label::ALIGN_CENTER)
+ columns->AddPaddingColumn(1, 0);
+ }
+
+ layout->StartRow(1, 0);
+ if (image && GetHorizontalAlignment() != Label::ALIGN_RIGHT)
+ layout->AddView(image_.get());
+ if (label)
+ layout->AddView(label_.get());
+ if (image && GetHorizontalAlignment() == Label::ALIGN_RIGHT)
+ layout->AddView(image_.get());
+
+ SetState(saved_state);
+}
+
+} // namespace views

Powered by Google App Engine
This is Rietveld 408576698