1// Copyright 2014 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_EXPANDING_TEXTFIELD_H_
6#define CHROME_BROWSER_UI_VIEWS_AUTOFILL_EXPANDING_TEXTFIELD_H_
7
8#include <list>
9
10#include "base/strings/string16.h"
11#include "chrome/browser/ui/views/autofill/decorated_textfield.h"
12#include "ui/views/controls/textfield/textfield_controller.h"
13#include "ui/views/view.h"
14
15namespace gfx {
16class Image;
17}
18
19namespace autofill {
20
21// A view that houses a stack of textfields. The stack grows as needed.
22class ExpandingTextfield : public views::View,
23                           public views::TextfieldController {
24 public:
25  static const char kViewClassName[];
26
27  // When |multiline| is false, the view acts pretty much like a normal
28  // DecoratedTextfield.
29  ExpandingTextfield(const base::string16& default_value,
30                     const base::string16& placeholder,
31                     bool multiline,
32                     views::TextfieldController* controller);
33  virtual ~ExpandingTextfield();
34
35  // Sets the contents of the textfields. Textfield n is set to the nth line
36  // of |text|, as separated by line returns.
37  void SetText(const base::string16& text);
38  // Concatenates text contents of all textfields (with line returns as the
39  // joining character) and returns it.
40  base::string16 GetText();
41
42  // Sets whether to indicate the first textfield has invalid content. Latter
43  // textfields are always valid.
44  void SetInvalid(bool invalid);
45  bool invalid() {
46    return textfields_.front()->invalid();
47  }
48
49  // Like validity, this only cares about the first textfield.
50  void SetEditable(bool editable);
51  bool editable() {
52    return textfields_.front()->editable();
53  }
54
55  // DecoratedTextfield pass-throughs.
56  void SetDefaultWidthInCharacters(int chars);
57  void SetPlaceholderText(const base::string16& placeholder);
58  void SetIcon(const gfx::Image& icon);
59  void SetTooltipIcon(const base::string16& text);
60
61  // View implementation.
62  virtual const char* GetClassName() const OVERRIDE;
63  using views::View::needs_layout;
64
65  // TextfieldController implementation.
66  virtual void ContentsChanged(views::Textfield* sender,
67                               const base::string16& new_contents) OVERRIDE;
68  virtual bool HandleKeyEvent(views::Textfield* sender,
69                              const ui::KeyEvent& key_event) OVERRIDE;
70  virtual bool HandleMouseEvent(views::Textfield* sender,
71                                const ui::MouseEvent& mouse_event) OVERRIDE;
72
73 private:
74  // Calls a given function on every textfield.
75  template <typename BaseType, typename Param>
76  void ForEachTextfield(void (BaseType::* f)(Param), Param p) const;
77
78  // The list of textfields. Owned as child views.
79  std::list<DecoratedTextfield*> textfields_;
80
81  TextfieldController* controller_;
82
83  DISALLOW_COPY_AND_ASSIGN(ExpandingTextfield);
84};
85
86}  // namespace autofill
87
88#endif  // CHROME_BROWSER_UI_VIEWS_AUTOFILL_EXPANDING_TEXTFIELD_H_
89