decorated_textfield.h revision a36e5920737c6adbddd3e43b760e5de8431db6e0
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/strings/string16.h"
9#include "ui/gfx/image/image.h"
10#include "ui/views/controls/textfield/textfield.h"
11
12namespace views {
13class FocusableBorder;
14class TextfieldController;
15}
16
17namespace autofill {
18
19// A class which holds a textfield and draws extra stuff on top, like
20// invalid content indications.
21class DecoratedTextfield : public views::Textfield {
22 public:
23  static const char kViewClassName[];
24
25  DecoratedTextfield(const base::string16& default_value,
26                     const base::string16& placeholder,
27                     views::TextfieldController* controller);
28  virtual ~DecoratedTextfield();
29
30  // Sets whether to indicate the textfield has invalid content.
31  void SetInvalid(bool invalid);
32  bool invalid() const { return invalid_; }
33
34  // Sets the icon to be displayed inside the textfield at the end of the
35  // text.
36  void SetIcon(const gfx::Image& icon);
37
38  // views::View implementation.
39  virtual const char* GetClassName() const OVERRIDE;
40  virtual gfx::Size GetPreferredSize() OVERRIDE;
41  virtual void PaintChildren(gfx::Canvas* canvas) OVERRIDE;
42  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
43
44 private:
45  FRIEND_TEST_ALL_PREFIXES(DecoratedTextfieldTest, HeightMatchesButton);
46
47  // This number corresponds to the number of pixels in the images that
48  // are used to draw a views button which are above or below the actual border.
49  // This number is encoded in the button assets themselves, so there's no other
50  // way to get it than to hardcode it here.
51  static const int kMagicInsetNumber;
52
53  // We draw the border.
54  views::FocusableBorder* border_;  // Weak.
55
56  // The icon that goes at the right side of the textfield.
57  gfx::Image icon_;
58
59  // Whether the text contents are "invalid" (i.e. should special markers be
60  // shown to indicate invalidness).
61  bool invalid_;
62
63  DISALLOW_COPY_AND_ASSIGN(DecoratedTextfield);
64};
65
66}  // namespace autofill
67
68#endif  // CHROME_BROWSER_UI_VIEWS_AUTOFILL_DECORATED_TEXTFIELD_H_
69