decorated_textfield.h revision 8bcbed890bc3ce4d7a057a8f32cab53fa534672e
1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_UI_VIEWS_AUTOFILL_DECORATED_TEXTFIELD_H_
6#define CHROME_BROWSER_UI_VIEWS_AUTOFILL_DECORATED_TEXTFIELD_H_
7
8#include "base/memory/scoped_ptr.h"
9#include "base/strings/string16.h"
10#include "ui/gfx/image/image.h"
11#include "ui/views/controls/textfield/textfield.h"
12#include "ui/views/controls/textfield/textfield.h"
13
14namespace views {
15class FocusableBorder;
16class TextfieldController;
17}
18
19namespace autofill {
20
21// A class which holds a textfield and draws extra stuff on top, like
22// invalid content indications.
23class DecoratedTextfield : public views::Textfield {
24 public:
25  static const char kViewClassName[];
26
27  DecoratedTextfield(const base::string16& default_value,
28                     const base::string16& placeholder,
29                     views::TextfieldController* controller);
30  virtual ~DecoratedTextfield();
31
32  // Sets whether to indicate the textfield has invalid content.
33  void SetInvalid(bool invalid);
34  bool invalid() const { return invalid_; }
35
36  // See docs for |editable_|.
37  void SetEditable(bool editable);
38  bool editable() const { return editable_; }
39
40  // Sets the icon to be displayed inside the textfield at the end of the
41  // text.
42  void SetIcon(const gfx::Image& icon);
43
44  // Sets a tooltip for this field. This will override the icon set with
45  // SetIcon(), if any, and will be overridden by future calls to SetIcon().
46  void SetTooltipIcon(const base::string16& text);
47
48  // views::Textfield implementation.
49  virtual base::string16 GetPlaceholderText() const OVERRIDE;
50
51  // views::View implementation.
52  virtual const char* GetClassName() const OVERRIDE;
53  virtual gfx::Size GetPreferredSize() OVERRIDE;
54  virtual void Layout() OVERRIDE;
55  virtual void OnFocus() OVERRIDE;
56  virtual void OnBlur() OVERRIDE;
57
58 private:
59  FRIEND_TEST_ALL_PREFIXES(DecoratedTextfieldTest, HeightMatchesButton);
60
61  // Updates the background of |this| after it may have changed. This is
62  // necessary for the sake of the padding around the native textfield.
63  void UpdateBackground();
64
65  // Called to update the layout after SetIcon or SetTooltipIcon has been
66  // called.
67  void IconChanged();
68
69  // This number corresponds to the number of pixels in the images that
70  // are used to draw a views button which are above or below the actual border.
71  // This number is encoded in the button assets themselves, so there's no other
72  // way to get it than to hardcode it here.
73  static const int kMagicInsetNumber;
74
75  // We draw the border.
76  views::FocusableBorder* border_;  // Weak.
77
78  // The view that holds the icon at the end of the textfield.
79  scoped_ptr<views::ImageView> icon_view_;
80
81  // Whether the text contents are "invalid" (i.e. should special markers be
82  // shown to indicate invalidness).
83  bool invalid_;
84
85  // Whether the user can edit the field. When not editable, many of the
86  // pieces of the textfield disappear (border, background, icon, placeholder
87  // text) and it can't receive focus.
88  bool editable_;
89
90  DISALLOW_COPY_AND_ASSIGN(DecoratedTextfield);
91};
92
93}  // namespace autofill
94
95#endif  // CHROME_BROWSER_UI_VIEWS_AUTOFILL_DECORATED_TEXTFIELD_H_
96