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

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: Remove stray semicolon. 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
« no previous file with comments | « ui/views/controls/button/label_button.h ('k') | ui/views/controls/button/label_button_border.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..75e6c4fcf4a53561b97690dbb3f8ea16e22ee40f
--- /dev/null
+++ b/ui/views/controls/button/label_button.cc
@@ -0,0 +1,267 @@
+// 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/views/controls/button/label_button_border.h"
+#include "ui/views/focus_border.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; roughly match Windows.
+ set_focus_border(FocusBorder::CreateDashedFocusBorder(3, 3, 3, 3));
+ SetAnimationDuration(kHoverAnimationDurationMs);
+
+ AddChildView(image_);
+ image_->set_interactive(false);
+
+ AddChildView(label_);
+ label_->SetAutoColorReadabilityEnabled(false);
+ label_->SetHorizontalAlignment(Label::ALIGN_LEFT);
+ label_->SetElideBehavior(Label::ELIDE_AT_END);
+
+ // Initialize the colors, border, 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) {
+ button_state_images_[for_state] = image;
+ image_->SetImage(GetImage(state()));
+}
+
+const string16& LabelButton::GetText() const {
+ return label_->text();
+}
+
+void LabelButton::SetText(const string16& text) {
+ label_->SetText(text);
+}
+
+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);
+ InvalidateLayout();
+}
+
+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]);
+
+ // Invalidate the layout to pickup the new insets from the border.
+ InvalidateLayout();
+}
+
+void LabelButton::StateChanged() {
+ const gfx::Size previous_image_size(image_->GetPreferredSize());
+ image_->SetImage(GetImage(state()));
+ const SkColor color = button_state_colors_[state()];
+ if (state() != BS_DISABLED && label_->enabled_color() != color)
+ label_->SetEnabledColor(color);
+ if (image_->GetPreferredSize() != previous_image_size)
+ Layout();
+}
+
+gfx::Size LabelButton::GetPreferredSize() {
+ gfx::Size size(label_->GetPreferredSize());
+ // TODO(msw): Multi-line labels are cut off at their preferred bounds.
+ // This apparent bug in CanvasSkia requires investigation.
+ size.set_width(size.width() * (GetTextMultiLine() ? 2 : 1));
+ const gfx::Size image_size(image_->GetPreferredSize());
+ if (image_size.width() > 0 && size.width() > 0)
+ size.Enlarge(kSpacing, 0);
+ size.set_height(std::max(size.height(), image_size.height()));
+ size.Enlarge(image_size.width() + GetInsets().width(), 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;
+}
+
+void LabelButton::Layout() {
+ gfx::Rect child_area(GetLocalBounds());
+ child_area.Inset(GetInsets());
+
+ gfx::Size image_size(image_->GetPreferredSize());
+ image_size.set_width(std::min(image_size.width(), child_area.width()));
+ image_size.set_height(std::min(image_size.height(), child_area.height()));
+
+ // The label takes any remaining width after sizing the image.
+ gfx::Size label_size(child_area.size());
+ if (!image_size.IsEmpty() && !label_size.IsEmpty()) {
+ label_size.set_width(child_area.width() - image_size.width() - kSpacing);
+ if (GetHorizontalAlignment() == Label::ALIGN_CENTER) {
+ gfx::Size preferred(label_->GetPreferredSize());
+ // TODO(msw): Multi-line labels are cut off at their preferred bounds.
+ // This apparent bug in CanvasSkia requires investigation.
+ preferred.set_width(preferred.width() * (GetTextMultiLine() ? 2 : 1));
+ label_size.set_width(std::min(label_size.width(), preferred.width()));
+ }
+ }
+
+ gfx::Point image_origin(child_area.origin());
+ image_origin.Offset(0, (child_area.height() - image_size.height()) / 2);
+ if (GetHorizontalAlignment() == Label::ALIGN_CENTER) {
+ const int total_width = image_size.width() + label_size.width() +
+ ((image_size.width() > 0 && label_size.width() > 0) ? kSpacing : 0);
+ image_origin.Offset((child_area.width() - total_width) / 2, 0);
+ } else if (GetHorizontalAlignment() == Label::ALIGN_RIGHT) {
+ image_origin.Offset(child_area.width() - image_size.width(), 0);
+ }
+
+ gfx::Point label_origin(child_area.origin());
+ if (!image_size.IsEmpty() && GetHorizontalAlignment() != Label::ALIGN_RIGHT)
+ label_origin.set_x(image_origin.x() + image_size.width() + kSpacing);
+
+ image_->SetBoundsRect(gfx::Rect(image_origin, image_size));
+ label_->SetBoundsRect(gfx::Rect(label_origin, label_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);
+}
+
+} // namespace views
« no previous file with comments | « ui/views/controls/button/label_button.h ('k') | ui/views/controls/button/label_button_border.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698