decorated_textfield.h revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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  // Sets the icon to be displayed inside the textfield at the end of the
37  // text.
38  void SetIcon(const gfx::Image& icon);
39
40  // Sets a tooltip for this field. This will override the icon set with
41  // SetIcon(), if any, and will be overridden by future calls to SetIcon().
42  void SetTooltipIcon(const base::string16& text);
43
44  // views::View implementation.
45  virtual const char* GetClassName() const OVERRIDE;
46  virtual gfx::Size GetPreferredSize() OVERRIDE;
47  virtual void Layout() OVERRIDE;
48  virtual void OnFocus() OVERRIDE;
49  virtual void OnBlur() OVERRIDE;
50
51 private:
52  FRIEND_TEST_ALL_PREFIXES(DecoratedTextfieldTest, HeightMatchesButton);
53
54  // Called to update the layout after SetIcon or SetTooltipIcon has been
55  // called.
56  void IconChanged();
57
58  // This number corresponds to the number of pixels in the images that
59  // are used to draw a views button which are above or below the actual border.
60  // This number is encoded in the button assets themselves, so there's no other
61  // way to get it than to hardcode it here.
62  static const int kMagicInsetNumber;
63
64  // We draw the border.
65  views::FocusableBorder* border_;  // Weak.
66
67  // The view that holds the icon at the end of the textfield.
68  scoped_ptr<views::ImageView> icon_view_;
69
70  // Whether the text contents are "invalid" (i.e. should special markers be
71  // shown to indicate invalidness).
72  bool invalid_;
73
74  DISALLOW_COPY_AND_ASSIGN(DecoratedTextfield);
75};
76
77}  // namespace autofill
78
79#endif  // CHROME_BROWSER_UI_VIEWS_AUTOFILL_DECORATED_TEXTFIELD_H_
80