summaryrefslogtreecommitdiffstats
path: root/views
diff options
context:
space:
mode:
authorerikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-15 22:32:41 +0000
committererikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-15 22:32:41 +0000
commit7f8bd32d382cc3f1cd70670d40f5f96d69e5d6ea (patch)
treed5d5b1722da43f55212e738d1333d2654923b75b /views
parent1b0a2055ebe972b871ec3e5062ae33db72f6bf80 (diff)
downloadchromium_src-7f8bd32d382cc3f1cd70670d40f5f96d69e5d6ea.zip
chromium_src-7f8bd32d382cc3f1cd70670d40f5f96d69e5d6ea.tar.gz
chromium_src-7f8bd32d382cc3f1cd70670d40f5f96d69e5d6ea.tar.bz2
Add highlight color support to label text.
BUG=none TEST=--gtest_filter=LabelTest.* Review URL: http://codereview.chromium.org/113466 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16202 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views')
-rw-r--r--views/controls/label.cc26
-rw-r--r--views/controls/label.h13
-rw-r--r--views/controls/label_unittest.cc10
3 files changed, 46 insertions, 3 deletions
diff --git a/views/controls/label.cc b/views/controls/label.cc
index bf09438..5dec56b8 100644
--- a/views/controls/label.cc
+++ b/views/controls/label.cc
@@ -43,6 +43,7 @@ void Label::Init(const std::wstring& text, const gfx::Font& font) {
SetText(text);
url_set_ = false;
color_ = kEnabledColor;
+ highlight_color_ = kEnabledColor;
horiz_alignment_ = ALIGN_CENTER;
is_multi_line_ = false;
allow_character_break_ = false;
@@ -50,6 +51,7 @@ void Label::Init(const std::wstring& text, const gfx::Font& font) {
rtl_alignment_mode_ = USE_UI_ALIGNMENT;
paint_as_focused_ = false;
has_focus_border_ = false;
+ highlighted_ = false;
}
Label::~Label() {
@@ -69,6 +71,8 @@ gfx::Size Label::GetPreferredSize() {
if (is_multi_line_) {
int w = width(), h = 0;
gfx::Canvas::SizeStringInt(text_, font_, &w, &h, ComputeMultiLineFlags());
+ // TODO(erikkay) With highlighted_ enabled, should we adjust the size
+ // in the multi-line case?
prefsize.SetSize(w, h);
} else {
prefsize = GetTextSize();
@@ -140,6 +144,17 @@ void Label::Paint(gfx::Canvas* canvas) {
gfx::Rect text_bounds;
int flags = 0;
CalculateDrawStringParams(&paint_text, &text_bounds, &flags);
+ if (highlighted_) {
+ // Draw a second version of the string underneath the main one, but down
+ // and to the right by a pixel to create a highlight.
+ canvas->DrawStringInt(paint_text,
+ font_,
+ highlight_color_,
+ text_bounds.x() + 1,
+ text_bounds.y() + 1,
+ text_bounds.width(),
+ text_bounds.height());
+ }
canvas->DrawStringInt(paint_text,
font_,
color_,
@@ -226,12 +241,12 @@ const GURL Label::GetURL() const {
gfx::Size Label::GetTextSize() {
if (!text_size_valid_) {
text_size_.SetSize(font_.GetStringWidth(text_), font_.height());
+ if (highlighted_)
+ text_size_.Enlarge(1, 1);
text_size_valid_ = true;
}
- if (text_size_valid_)
- return text_size_;
- return gfx::Size();
+ return text_size_;
}
int Label::GetHeightForWidth(int w) {
@@ -259,6 +274,11 @@ const SkColor Label::GetColor() const {
return color_;
}
+void Label::SetDrawHighlighted(bool h) {
+ highlighted_ = h;
+ text_size_valid_ = false;
+}
+
void Label::SetHorizontalAlignment(Alignment a) {
// If the View's UI layout is right-to-left and rtl_alignment_mode_ is
// USE_UI_ALIGNMENT, we need to flip the alignment so that the alignment
diff --git a/views/controls/label.h b/views/controls/label.h
index 97b5bc2..9a5526a 100644
--- a/views/controls/label.h
+++ b/views/controls/label.h
@@ -97,6 +97,16 @@ class Label : public View {
// Return a reference to the currently used color
virtual const SkColor GetColor() const;
+ // Set and Get the highlight color
+ virtual void SetHighlightColor(const SkColor& color) {
+ highlight_color_ = color;
+ }
+ virtual const SkColor GetHighlightColor() const { return highlight_color_; }
+
+ // Whether to draw highlighted text.
+ virtual bool DrawHighlighted() const { return highlighted_; }
+ virtual void SetDrawHighlighted(bool h);
+
// Set horizontal alignment. If the locale is RTL, and the RTL alignment
// setting is set as USE_UI_ALIGNMENT, the alignment is flipped around.
//
@@ -218,6 +228,7 @@ class Label : public View {
GURL url_;
gfx::Font font_;
SkColor color_;
+ SkColor highlight_color_;
gfx::Size text_size_;
bool text_size_valid_;
bool is_multi_line_;
@@ -241,6 +252,8 @@ class Label : public View {
// allows this view to reserve space for a focus border that it otherwise
// might not have because it is not itself focusable.
bool has_focus_border_;
+ // Whether the text is drawn with an inset highlight.
+ bool highlighted_;
DISALLOW_COPY_AND_ASSIGN(Label);
};
diff --git a/views/controls/label_unittest.cc b/views/controls/label_unittest.cc
index f9bb484..7f1ca60 100644
--- a/views/controls/label_unittest.cc
+++ b/views/controls/label_unittest.cc
@@ -45,6 +45,9 @@ TEST(LabelTest, ColorProperty) {
SkColor color = SkColorSetARGB(20, 40, 10, 5);
label.SetColor(color);
EXPECT_EQ(color, label.GetColor());
+ SkColor h_color = SkColorSetARGB(40, 80, 20, 10);
+ label.SetHighlightColor(h_color);
+ EXPECT_EQ(h_color, label.GetHighlightColor());
}
TEST(LabelTest, AlignmentProperty) {
@@ -169,6 +172,13 @@ TEST(LabelTest, SingleLineSizing) {
EXPECT_GT(required_size.height(), kMinTextDimension);
EXPECT_GT(required_size.width(), kMinTextDimension);
+ // Test with highlights.
+ label.SetDrawHighlighted(true);
+ gfx::Size highlighted_size = label.GetPreferredSize();
+ EXPECT_GT(highlighted_size.height(), required_size.height());
+ EXPECT_GT(highlighted_size.width(), required_size.width());
+ label.SetDrawHighlighted(false);
+
// Test everything with borders.
gfx::Insets border(10, 20, 30, 40);
label.set_border(Border::CreateEmptyBorder(border.top(),