Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ui / views / controls / label.h
index 40534ff..8b2081e 100644 (file)
 #include "base/strings/string16.h"
 #include "third_party/skia/include/core/SkColor.h"
 #include "ui/gfx/font_list.h"
+#include "ui/gfx/shadow_value.h"
 #include "ui/gfx/text_constants.h"
 #include "ui/views/view.h"
 
 namespace views {
 
-/////////////////////////////////////////////////////////////////////////////
-//
-// Label class
-//
-// A label is a view subclass that can display a string.
-//
-/////////////////////////////////////////////////////////////////////////////
+// A view subclass that can display a string.
 class VIEWS_EXPORT Label : public View {
  public:
   // Internal class name.
   static const char kViewClassName[];
 
-  // The following enum is used to indicate whether using the Chrome UI's
-  // directionality as the label's directionality, or auto-detecting the label's
-  // directionality.
-  //
-  // If the label text originates from the Chrome UI, we should use the Chrome
-  // UI's directionality as the label's directionality.
-  //
-  // If the text originates from a web page, its directionality is determined
-  // based on its first character with strong directionality, disregarding what
-  // directionality the Chrome UI is.
-  enum DirectionalityMode {
-    USE_UI_DIRECTIONALITY = 0,
-    AUTO_DETECT_DIRECTIONALITY
-  };
-
-  enum ElideBehavior {
-    NO_ELIDE,         // Do not elide the label text; truncate as needed.
-    ELIDE_IN_MIDDLE,  // Add ellipsis in the middle of the string as needed.
-    ELIDE_AT_END,     // Add ellipsis at the end of the string as needed.
-    ELIDE_AS_EMAIL,   // Elide while retaining username/domain chars as needed.
-  };
+  // The padding for the focus border when rendering focused text.
+  static const int kFocusBorderPadding;
 
   Label();
-  explicit Label(const string16& text);
-  Label(const string16& text, const gfx::FontList& font_list);
-  Label(const string16& text, const gfx::Font& font);  // OBSOLETE
+  explicit Label(const base::string16& text);
+  Label(const base::string16& text, const gfx::FontList& font_list);
   virtual ~Label();
 
   // Gets or sets the fonts used by this label.
   const gfx::FontList& font_list() const { return font_list_; }
   virtual void SetFontList(const gfx::FontList& font_list);
-  // Obsolete gfx::Font version.  Should use gfx::FontList version instead.
-  const gfx::Font& font() const;  // OBSOLETE
-  virtual void SetFont(const gfx::Font& font);  // OBSOLETE
 
   // Get or set the label text.
-  const string16& text() const { return text_; }
-  void SetText(const string16& text);
+  const base::string16& text() const { return text_; }
+  virtual void SetText(const base::string16& text);
 
   // Enables or disables auto-color-readability (enabled by default).  If this
   // is enabled, then calls to set any foreground or background color will
@@ -76,7 +48,7 @@ class VIEWS_EXPORT Label : public View {
   void SetAutoColorReadabilityEnabled(bool enabled);
 
   // Sets the color.  This will automatically force the color to be readable
-  // over the current background color.
+  // over the current background color, if auto color readability is enabled.
   virtual void SetEnabledColor(SkColor color);
   void SetDisabledColor(SkColor color);
 
@@ -87,39 +59,17 @@ class VIEWS_EXPORT Label : public View {
   void SetBackgroundColor(SkColor color);
   SkColor background_color() const { return background_color_; }
 
-  // Enables a drop shadow underneath the text.
-  void SetShadowColors(SkColor enabled_color, SkColor disabled_color);
+  // Set drop shadows underneath the text.
+  void SetShadows(const gfx::ShadowValues& shadows);
+  const gfx::ShadowValues& shadows() const { return shadows_; }
 
-  // Sets the drop shadow's offset from the text.
-  void SetShadowOffset(int x, int y);
+  // Sets whether subpixel rendering is used; the default is true, but this
+  // feature also requires an opaque background color.
+  void SetSubpixelRenderingEnabled(bool subpixel_rendering_enabled);
 
-  // Disables shadows.
-  void ClearEmbellishing();
-
-  // Sets horizontal alignment. If the locale is RTL, and the directionality
-  // mode is USE_UI_DIRECTIONALITY, the alignment is flipped around.
-  //
-  // Caveat: for labels originating from a web page, the directionality mode
-  // should be reset to AUTO_DETECT_DIRECTIONALITY before the horizontal
-  // alignment is set. Otherwise, the label's alignment specified as a parameter
-  // will be flipped in RTL locales.
+  // Sets the horizontal alignment; the argument value is mirrored in RTL UI.
   void SetHorizontalAlignment(gfx::HorizontalAlignment alignment);
-
-  gfx::HorizontalAlignment horizontal_alignment() const {
-    return horizontal_alignment_;
-  }
-
-  // Sets the directionality mode. The directionality mode is initialized to
-  // USE_UI_DIRECTIONALITY when the label is constructed. USE_UI_DIRECTIONALITY
-  // applies to every label that originates from the Chrome UI. However, if the
-  // label originates from a web page, its directionality is auto-detected.
-  void set_directionality_mode(DirectionalityMode mode) {
-    directionality_mode_ = mode;
-  }
-
-  DirectionalityMode directionality_mode() const {
-    return directionality_mode_;
-  }
+  gfx::HorizontalAlignment GetHorizontalAlignment() const;
 
   // Get or set the distance in pixels between baselines of multi-line text.
   // Default is 0, indicating the distance between lines should be the standard
@@ -128,23 +78,26 @@ class VIEWS_EXPORT Label : public View {
   void SetLineHeight(int height);
 
   // Get or set if the label text can wrap on multiple lines; default is false.
-  bool is_multi_line() const { return is_multi_line_; }
+  bool multi_line() const { return multi_line_; }
   void SetMultiLine(bool multi_line);
 
-  // Sets whether the label text can be split on words.
-  // Default is false. This only works when is_multi_line is true.
+  // Get or set if the label text should be obscured before rendering (e.g.
+  // should "Password!" display as "*********"); default is false.
+  bool obscured() const { return obscured_; }
+  void SetObscured(bool obscured);
+
+  // Sets whether multi-line text can wrap mid-word; the default is false.
   void SetAllowCharacterBreak(bool allow_character_break);
 
-  // Sets whether the label text should be elided in the middle or end (if
-  // necessary). The default is to elide at the end.
-  // NOTE: Eliding in the middle is not supported for multi-line strings.
-  void SetElideBehavior(ElideBehavior elide_behavior);
+  // Sets the eliding or fading behavior, applied as necessary. The default is
+  // to elide at the end. Eliding is not well supported for multi-line labels.
+  void SetElideBehavior(gfx::ElideBehavior elide_behavior);
 
   // Sets the tooltip text.  Default behavior for a label (single-line) is to
   // show the full text if it is wider than its bounds.  Calling this overrides
   // the default behavior and lets you set a custom tooltip.  To revert to
   // default behavior, call this with an empty string.
-  void SetTooltipText(const string16& tooltip_text);
+  void SetTooltipText(const base::string16& tooltip_text);
 
   // Resizes the label so its width is set to the width of the longest line and
   // its height deduced accordingly.
@@ -154,48 +107,37 @@ class VIEWS_EXPORT Label : public View {
   // wrapped).  If 0, no maximum width is enforced.
   void SizeToFit(int max_width);
 
-  // Gets/sets the flag to determine whether the label should be collapsed when
-  // it's hidden (not visible). If this flag is true, the label will return a
-  // preferred size of (0, 0) when it's not visible.
+  // Sets whether the preferred size is empty when the label is not visible.
   void set_collapse_when_hidden(bool value) { collapse_when_hidden_ = value; }
-  bool collapse_when_hidden() const { return collapse_when_hidden_; }
 
-  void SetHasFocusBorder(bool has_focus_border);
+  // Get the text as displayed to the user, respecting the obscured flag.
+  const base::string16& GetLayoutTextForTesting() const;
 
-  // Overridden from View:
+  // View:
   virtual gfx::Insets GetInsets() const OVERRIDE;
   virtual int GetBaseline() const OVERRIDE;
-  // Overridden to compute the size required to display this label.
-  virtual gfx::Size GetPreferredSize() OVERRIDE;
-  // Returns the height necessary to display this label with the provided width.
-  // This method is used to layout multi-line labels. It is equivalent to
-  // GetPreferredSize().height() if the receiver is not multi-line.
-  virtual int GetHeightForWidth(int w) OVERRIDE;
+  virtual gfx::Size GetPreferredSize() const OVERRIDE;
+  virtual gfx::Size GetMinimumSize() const OVERRIDE;
+  virtual int GetHeightForWidth(int w) const OVERRIDE;
   virtual const char* GetClassName() const OVERRIDE;
   virtual View* GetTooltipHandlerForPoint(const gfx::Point& point) OVERRIDE;
-  virtual bool HitTestRect(const gfx::Rect& rect) const OVERRIDE;
-  virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
-  // Gets the tooltip text for labels that are wider than their bounds, except
-  // when the label is multiline, in which case it just returns false (no
-  // tooltip).  If a custom tooltip has been specified with SetTooltipText()
-  // it is returned instead.
+  virtual bool CanProcessEventsWithinSubtree() const OVERRIDE;
+  virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
   virtual bool GetTooltipText(const gfx::Point& p,
-                              string16* tooltip) const OVERRIDE;
+                              base::string16* tooltip) const OVERRIDE;
 
  protected:
-  // Called by Paint to paint the text.  Override this to change how
-  // text is painted.
-  virtual void PaintText(gfx::Canvas* canvas,
-                         const string16& text,
-                         const gfx::Rect& text_bounds,
-                         int flags);
+  // Called by Paint to paint the text.
+  void PaintText(gfx::Canvas* canvas,
+                 const base::string16& text,
+                 const gfx::Rect& text_bounds,
+                 int flags);
 
   virtual gfx::Size GetTextSize() const;
 
   SkColor disabled_color() const { return actual_disabled_color_; }
 
-  // Overridden from View:
-  // Overridden to dirty our text bounds if we're multi-line.
+  // View:
   virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
   virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE;
@@ -207,12 +149,14 @@ class VIEWS_EXPORT Label : public View {
   FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineString);
   FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineStringInRTL);
   FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineStringInRTL);
-  FRIEND_TEST_ALL_PREFIXES(LabelTest, AutoDetectDirectionality);
-
-  // Calls ComputeDrawStringFlags().
+  FRIEND_TEST_ALL_PREFIXES(LabelTest, DirectionalityFromText);
   FRIEND_TEST_ALL_PREFIXES(LabelTest, DisableSubpixelRendering);
 
-  void Init(const string16& text, const gfx::FontList& font_list);
+  // Sets both |text_| and |layout_text_| to appropriate values, taking
+  // the label's 'obscured' status into account.
+  void SetTextInternal(const base::string16& text);
+
+  void Init(const base::string16& text, const gfx::FontList& font_list);
 
   void RecalculateColors();
 
@@ -224,7 +168,7 @@ class VIEWS_EXPORT Label : public View {
   gfx::Rect GetAvailableRect() const;
 
   // Returns parameters to be used for the DrawString call.
-  void CalculateDrawStringParams(string16* paint_text,
+  void CalculateDrawStringParams(base::string16* paint_text,
                                  gfx::Rect* text_bounds,
                                  int* flags) const;
 
@@ -237,7 +181,8 @@ class VIEWS_EXPORT Label : public View {
 
   bool ShouldShowDefaultTooltip() const;
 
-  string16 text_;
+  base::string16 text_;
+  base::string16 layout_text_;
   gfx::FontList font_list_;
   SkColor requested_enabled_color_;
   SkColor actual_enabled_color_;
@@ -250,39 +195,24 @@ class VIEWS_EXPORT Label : public View {
   bool disabled_color_set_;
   bool background_color_set_;
 
+  bool subpixel_rendering_enabled_;
   bool auto_color_readability_;
   mutable gfx::Size text_size_;
   mutable bool text_size_valid_;
   int line_height_;
-  bool is_multi_line_;
+  bool multi_line_;
+  bool obscured_;
   bool allow_character_break_;
-  ElideBehavior elide_behavior_;
+  gfx::ElideBehavior elide_behavior_;
   gfx::HorizontalAlignment horizontal_alignment_;
-  string16 tooltip_text_;
+  base::string16 tooltip_text_;
   // Whether to collapse the label when it's not visible.
   bool collapse_when_hidden_;
-  // The following member variable is used to control whether the
-  // directionality is auto-detected based on first strong directionality
-  // character or is determined by chrome UI's locale.
-  DirectionalityMode directionality_mode_;
-  // When embedded in a larger control that is focusable, setting this flag
-  // 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_;
-
-  // Colors for shadow.
-  SkColor enabled_shadow_color_;
-  SkColor disabled_shadow_color_;
-
-  // Space between text and shadow.
-  gfx::Point shadow_offset_;
-
-  // Should a shadow be drawn behind the text?
-  bool has_shadow_;
+  gfx::ShadowValues shadows_;
 
   // The cached heights to avoid recalculation in GetHeightForWidth().
-  std::vector<gfx::Size> cached_heights_;
-  int cached_heights_cursor_;
+  mutable std::vector<gfx::Size> cached_heights_;
+  mutable int cached_heights_cursor_;
 
   DISALLOW_COPY_AND_ASSIGN(Label);
 };