diff options
author | gspencer@chromium.org <gspencer@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-30 22:45:45 +0000 |
---|---|---|
committer | gspencer@chromium.org <gspencer@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-30 22:45:45 +0000 |
commit | c766a168bb731fc8618b0a202935c75fe67494ee (patch) | |
tree | 08687e47fef17d785a3e5f9df1478973a80edee9 /views | |
parent | 971b3c5679671ae9f08190a54d7cd0ed30ec9cfb (diff) | |
download | chromium_src-c766a168bb731fc8618b0a202935c75fe67494ee.zip chromium_src-c766a168bb731fc8618b0a202935c75fe67494ee.tar.gz chromium_src-c766a168bb731fc8618b0a202935c75fe67494ee.tar.bz2 |
Adding support for ChromeOS snapshot window titles and favicons.
TEST=build and ran with chromeos-wm
BUG=chromium-os:2866, chromium-os:2867
Review URL: http://codereview.chromium.org/2857005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@51305 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views')
-rw-r--r-- | views/controls/label.cc | 133 | ||||
-rw-r--r-- | views/controls/label.h | 28 |
2 files changed, 92 insertions, 69 deletions
diff --git a/views/controls/label.cc b/views/controls/label.cc index d30a62b..c842191 100644 --- a/views/controls/label.cc +++ b/views/controls/label.cc @@ -25,7 +25,7 @@ namespace views { const char Label::kViewClassName[] = "views/Label"; SkColor Label::kEnabledColor, Label::kDisabledColor; -static const int kFocusBorderPadding = 1; +const int Label::kFocusBorderPadding = 1; Label::Label() { Init(std::wstring(), GetDefaultFont()); @@ -78,19 +78,12 @@ void Label::DidChangeBounds(const gfx::Rect& previous, void Label::Paint(gfx::Canvas* canvas) { PaintBackground(canvas); + std::wstring paint_text; gfx::Rect text_bounds; int flags = 0; CalculateDrawStringParams(&paint_text, &text_bounds, &flags); - canvas->DrawStringInt(paint_text, font_, color_, - text_bounds.x(), text_bounds.y(), - text_bounds.width(), text_bounds.height(), flags); - - if (HasFocus() || paint_as_focused_) { - text_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); - canvas->DrawFocusRect(text_bounds.x(), text_bounds.y(), - text_bounds.width(), text_bounds.height()); - } + PaintText(canvas, paint_text, text_bounds, flags); } void Label::PaintBackground(gfx::Canvas* canvas) { @@ -266,6 +259,44 @@ void Label::SetHasFocusBorder(bool has_focus_border) { text_size_valid_ &= !is_multi_line_; } +void Label::PaintText(gfx::Canvas* canvas, + const std::wstring& text, + const gfx::Rect& text_bounds, + int flags) { + canvas->DrawStringInt(text, font_, color_, + text_bounds.x(), text_bounds.y(), + text_bounds.width(), text_bounds.height(), flags); + + if (HasFocus() || paint_as_focused_) { + gfx::Rect focus_bounds = text_bounds; + focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); + canvas->DrawFocusRect(focus_bounds.x(), focus_bounds.y(), + focus_bounds.width(), focus_bounds.height()); + } +} + +gfx::Size Label::GetTextSize() const { + if (!text_size_valid_) { + // For single-line strings, we supply the largest possible width, because + // while adding NO_ELLIPSIS to the flags works on Windows for forcing + // SizeStringInt() to calculate the desired width, it doesn't seem to work + // on Linux. + int w = is_multi_line_ ? + GetAvailableRect().width() : std::numeric_limits<int>::max(); + int h = font_.height(); + // For single-line strings, ignore the available width and calculate how + // wide the text wants to be. + int flags = ComputeMultiLineFlags(); + if (!is_multi_line_) + flags |= gfx::Canvas::NO_ELLIPSIS; + gfx::CanvasSkia::SizeStringInt(text_, font_, &w, &h, flags); + text_size_.SetSize(w, h); + text_size_valid_ = true; + } + + return text_size_; +} + // static gfx::Font Label::GetDefaultFont() { return ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont); @@ -302,36 +333,6 @@ void Label::Init(const std::wstring& text, const gfx::Font& font) { has_focus_border_ = false; } -void Label::CalculateDrawStringParams(std::wstring* paint_text, - gfx::Rect* text_bounds, - int* flags) const { - DCHECK(paint_text && text_bounds && flags); - - if (url_set_) { - // TODO(jungshik) : Figure out how to get 'intl.accept_languages' - // preference and use it when calling ElideUrl. - *paint_text = gfx::ElideUrl(url_, font_, width(), std::wstring()); - - // An URLs is always treated as an LTR text and therefore we should - // explicitly mark it as such if the locale is RTL so that URLs containing - // Hebrew or Arabic characters are displayed correctly. - // - // Note that we don't check the View's UI layout setting in order to - // determine whether or not to insert the special Unicode formatting - // characters. We use the locale settings because an URL is always treated - // as an LTR string, even if its containing view does not use an RTL UI - // layout. - base::i18n::GetDisplayStringInLTRDirectionality(paint_text); - } else if (elide_in_middle_) { - *paint_text = gfx::ElideText(text_, font_, width(), true); - } else { - *paint_text = text_; - } - - *text_bounds = GetTextBounds(); - *flags = ComputeMultiLineFlags(); -} - void Label::UpdateContainsMouse(const MouseEvent& event) { SetContainsMouse(GetTextBounds().Contains(event.x(), event.y())); } @@ -374,28 +375,6 @@ gfx::Rect Label::GetTextBounds() const { return gfx::Rect(text_origin, text_size); } -gfx::Size Label::GetTextSize() const { - if (!text_size_valid_) { - // For single-line strings, we supply the largest possible width, because - // while adding NO_ELLIPSIS to the flags works on Windows for forcing - // SizeStringInt() to calculate the desired width, it doesn't seem to work - // on Linux. - int w = is_multi_line_ ? - GetAvailableRect().width() : std::numeric_limits<int>::max(); - int h = font_.height(); - // For single-line strings, ignore the available width and calculate how - // wide the text wants to be. - int flags = ComputeMultiLineFlags(); - if (!is_multi_line_) - flags |= gfx::Canvas::NO_ELLIPSIS; - gfx::CanvasSkia::SizeStringInt(text_, font_, &w, &h, flags); - text_size_.SetSize(w, h); - text_size_valid_ = true; - } - - return text_size_; -} - int Label::ComputeMultiLineFlags() const { if (!is_multi_line_) return 0; @@ -432,4 +411,34 @@ gfx::Rect Label::GetAvailableRect() const { return bounds; } +void Label::CalculateDrawStringParams(std::wstring* paint_text, + gfx::Rect* text_bounds, + int* flags) const { + DCHECK(paint_text && text_bounds && flags); + + if (url_set_) { + // TODO(jungshik) : Figure out how to get 'intl.accept_languages' + // preference and use it when calling ElideUrl. + *paint_text = gfx::ElideUrl(url_, font_, width(), std::wstring()); + + // An URLs is always treated as an LTR text and therefore we should + // explicitly mark it as such if the locale is RTL so that URLs containing + // Hebrew or Arabic characters are displayed correctly. + // + // Note that we don't check the View's UI layout setting in order to + // determine whether or not to insert the special Unicode formatting + // characters. We use the locale settings because an URL is always treated + // as an LTR string, even if its containing view does not use an RTL UI + // layout. + base::i18n::GetDisplayStringInLTRDirectionality(paint_text); + } else if (elide_in_middle_) { + *paint_text = gfx::ElideText(text_, font_, width(), true); + } else { + *paint_text = text_; + } + + *text_bounds = GetTextBounds(); + *flags = ComputeMultiLineFlags(); +} + } // namespace views diff --git a/views/controls/label.h b/views/controls/label.h index a749be8..30bd1b4 100644 --- a/views/controls/label.h +++ b/views/controls/label.h @@ -45,6 +45,9 @@ class Label : public View { // The view class name. static const char kViewClassName[]; + // The padding for the focus border when rendering focused text. + static const int kFocusBorderPadding; + Label(); explicit Label(const std::wstring& text); Label(const std::wstring& text, const gfx::Font& font); @@ -186,12 +189,25 @@ class Label : public View { void set_collapse_when_hidden(bool value) { collapse_when_hidden_ = value; } bool collapse_when_hidden() const { return collapse_when_hidden_; } + // Gets/set whether or not this label is to be painted as a focused element. void set_paint_as_focused(bool paint_as_focused) { paint_as_focused_ = paint_as_focused; } + bool paint_as_focused() const { return paint_as_focused_; } void SetHasFocusBorder(bool has_focus_border); + protected: + // Called by Paint to paint the text. Override this to change how + // text is painted. + virtual void PaintText(gfx::Canvas* canvas, + const std::wstring& text, + const gfx::Rect& text_bounds, + int flags); + + void invalidate_text_size() { text_size_valid_ = false; } + + virtual gfx::Size GetTextSize() const; private: // These tests call CalculateDrawStringParams in order to verify the // calculations done for drawing text. @@ -204,11 +220,6 @@ class Label : public View { void Init(const std::wstring& text, const gfx::Font& font); - // Returns parameters to be used for the DrawString call. - void CalculateDrawStringParams(std::wstring* paint_text, - gfx::Rect* text_bounds, - int* flags) const; - // If the mouse is over the text, SetContainsMouse(true) is invoked, otherwise // SetContainsMouse(false) is invoked. void UpdateContainsMouse(const MouseEvent& event); @@ -221,12 +232,15 @@ class Label : public View { // Returns where the text is drawn, in the receivers coordinate system. gfx::Rect GetTextBounds() const; - gfx::Size GetTextSize() const; - int ComputeMultiLineFlags() const; gfx::Rect GetAvailableRect() const; + // Returns parameters to be used for the DrawString call. + void CalculateDrawStringParams(std::wstring* paint_text, + gfx::Rect* text_bounds, + int* flags) const; + // The colors to use for enabled and disabled labels. static SkColor kEnabledColor, kDisabledColor; |