1// Copyright (c) 2012 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 UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_
6#define UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/compiler_specific.h"
12#include "base/memory/weak_ptr.h"
13#include "base/strings/string16.h"
14#include "base/time/time.h"
15#include "build/build_config.h"
16#include "third_party/skia/include/core/SkColor.h"
17#include "ui/base/ime/text_input_type.h"
18#include "ui/base/keycodes/keyboard_codes.h"
19#include "ui/gfx/font_list.h"
20#include "ui/gfx/native_widget_types.h"
21#include "ui/gfx/text_constants.h"
22#include "ui/views/controls/textfield/native_textfield_wrapper.h"
23#include "ui/views/view.h"
24
25#if !defined(OS_LINUX)
26#include "base/logging.h"
27#endif
28
29namespace gfx {
30class ImageSkia;
31}
32
33namespace ui {
34class Range;
35class TextInputClient;
36}  // namespace ui
37
38namespace views {
39
40class ImageView;
41
42class TextfieldController;
43
44// This class implements a View that wraps a native text (edit) field.
45class VIEWS_EXPORT Textfield : public View {
46 public:
47  // The textfield's class name.
48  static const char kViewClassName[];
49
50  enum StyleFlags {
51    STYLE_DEFAULT   = 0,
52    STYLE_OBSCURED  = 1 << 0,
53    STYLE_LOWERCASE = 1 << 1
54  };
55
56  // Returns true if the build or commandline dictates NativeTextfieldViews use.
57  static bool IsViewsTextfieldEnabled();
58
59  Textfield();
60  explicit Textfield(StyleFlags style);
61  virtual ~Textfield();
62
63  // TextfieldController accessors
64  void SetController(TextfieldController* controller);
65  TextfieldController* GetController() const;
66
67  // Gets/Sets whether or not the Textfield is read-only.
68  bool read_only() const { return read_only_; }
69  void SetReadOnly(bool read_only);
70
71  // Gets/sets the STYLE_OBSCURED bit, controlling whether characters in this
72  // Textfield are displayed as asterisks/bullets.
73  bool IsObscured() const;
74  void SetObscured(bool obscured);
75
76  // Gets/sets the duration to reveal the last typed char when the obscured bit
77  // is set. A duration of zero effectively disables the feature. Other values
78  // cause the last typed char to be shown for the defined duration. Note this
79  // only works with NativeTextfieldViews.
80  const base::TimeDelta& obscured_reveal_duration() const {
81    return obscured_reveal_duration_;
82  }
83  void set_obscured_reveal_duration(const base::TimeDelta& duration) {
84    obscured_reveal_duration_ = duration;
85  }
86
87  // Gets/Sets the input type of this textfield.
88  ui::TextInputType GetTextInputType() const;
89  void SetTextInputType(ui::TextInputType type);
90
91  // Gets/Sets the text currently displayed in the Textfield.
92  const string16& text() const { return text_; }
93
94  // Sets the text currently displayed in the Textfield.  This doesn't
95  // change the cursor position if the current cursor is within the
96  // new text's range, or moves the cursor to the end if the cursor is
97  // out of the new text's range.
98  void SetText(const string16& text);
99
100  // Appends the given string to the previously-existing text in the field.
101  void AppendText(const string16& text);
102
103  // Inserts |text| at the current cursor position, replacing any selected text.
104  void InsertOrReplaceText(const string16& text);
105
106  // Returns the text direction.
107  base::i18n::TextDirection GetTextDirection() const;
108
109  // Returns the text that is currently selected.
110  string16 GetSelectedText() const;
111
112  // Select the entire text range. If |reversed| is true, the range will end at
113  // the logical beginning of the text; this generally shows the leading portion
114  // of text that overflows its display area.
115  void SelectAll(bool reversed);
116
117  // Clears the selection within the edit field and sets the caret to the end.
118  void ClearSelection() const;
119
120  // Checks if there is any selected text.
121  bool HasSelection() const;
122
123  // Accessor for |style_|.
124  StyleFlags style() const { return style_; }
125
126  // Gets/Sets the text color to be used when painting the Textfield.
127  // Call |UseDefaultTextColor| to restore the default system color.
128  SkColor GetTextColor() const;
129  void SetTextColor(SkColor color);
130  void UseDefaultTextColor();
131
132  // Gets/Sets the background color to be used when painting the Textfield.
133  // Call |UseDefaultBackgroundColor| to restore the default system color.
134  SkColor GetBackgroundColor() const;
135  void SetBackgroundColor(SkColor color);
136  void UseDefaultBackgroundColor();
137
138  // Gets/Sets whether or not the cursor is enabled.
139  bool GetCursorEnabled() const;
140  void SetCursorEnabled(bool enabled);
141
142  // Gets/Sets the fonts used when rendering the text within the Textfield.
143  const gfx::FontList& font_list() const { return font_list_; }
144  void SetFontList(const gfx::FontList& font_list);
145  const gfx::Font& GetPrimaryFont() const;
146  void SetFont(const gfx::Font& font);
147
148  // Sets the left and right margin (in pixels) within the text box. On Windows
149  // this is accomplished by packing the left and right margin into a single
150  // 32 bit number, so the left and right margins are effectively 16 bits.
151  void SetHorizontalMargins(int left, int right);
152
153  // Sets the top and bottom margins (in pixels) within the textfield.
154  // NOTE: in most cases height could be changed instead.
155  void SetVerticalMargins(int top, int bottom);
156
157  // Set the text vertical alignment.  Text is vertically centered by default.
158  gfx::VerticalAlignment vertical_alignment() const {
159    return vertical_alignment_;
160  }
161  void SetVerticalAlignment(gfx::VerticalAlignment alignment);
162
163  // Sets the default width of the text control. See default_width_in_chars_.
164  void set_default_width_in_chars(int default_width) {
165    default_width_in_chars_ = default_width;
166  }
167
168  // Removes the border from the edit box, giving it a 2D look.
169  bool draw_border() const { return draw_border_; }
170  void RemoveBorder();
171
172  // Sets the text to display when empty.
173  void set_placeholder_text(const string16& text) {
174    placeholder_text_ = text;
175#if !defined(OS_LINUX)
176    NOTIMPLEMENTED();
177#endif
178  }
179  const string16& placeholder_text() const {
180    return placeholder_text_;
181  }
182
183  SkColor placeholder_text_color() const { return placeholder_text_color_; }
184  void set_placeholder_text_color(SkColor color) {
185    placeholder_text_color_ = color;
186  }
187
188  // Getter for the horizontal margins that were set. Returns false if
189  // horizontal margins weren't set.
190  bool GetHorizontalMargins(int* left, int* right);
191
192  // Getter for the vertical margins that were set. Returns false if vertical
193  // margins weren't set.
194  bool GetVerticalMargins(int* top, int* bottom);
195
196  // Updates all properties on the textfield. This is invoked internally.
197  // Users of Textfield never need to invoke this directly.
198  void UpdateAllProperties();
199
200  // Invoked by the edit control when the value changes. This method set
201  // the text_ member variable to the value contained in edit control.
202  // This is important because the edit control can be replaced if it has
203  // been deleted during a window close.
204  void SyncText();
205
206  // Returns whether or not an IME is composing text.
207  bool IsIMEComposing() const;
208
209  // Gets the selected range. This is views-implementation only and
210  // has to be called after the wrapper is created.
211  // TODO(msw): Return a const reference when NativeTextfieldWin is gone.
212  ui::Range GetSelectedRange() const;
213
214  // Selects the text given by |range|. This is views-implementation only and
215  // has to be called after the wrapper is created.
216  void SelectRange(const ui::Range& range);
217
218  // Gets the selection model. This is views-implementation only and
219  // has to be called after the wrapper is created.
220  // TODO(msw): Return a const reference when NativeTextfieldWin is gone.
221  gfx::SelectionModel GetSelectionModel() const;
222
223  // Selects the text given by |sel|. This is views-implementation only and
224  // has to be called after the wrapper is created.
225  void SelectSelectionModel(const gfx::SelectionModel& sel);
226
227  // Returns the current cursor position. This is views-implementation
228  // only and has to be called after the wrapper is created.
229  size_t GetCursorPosition() const;
230
231  // Set the text color over the entire text or a logical character range.
232  // Empty and invalid ranges are ignored. This is views-implementation only and
233  // has to be called after the wrapper is created.
234  void SetColor(SkColor value);
235  void ApplyColor(SkColor value, const ui::Range& range);
236
237  // Set various text styles over the entire text or a logical character range.
238  // The respective |style| is applied if |value| is true, or removed if false.
239  // Empty and invalid ranges are ignored. This is views-implementation only and
240  // has to be called after the wrapper is created.
241  void SetStyle(gfx::TextStyle style, bool value);
242  void ApplyStyle(gfx::TextStyle style, bool value, const ui::Range& range);
243
244  // Clears Edit history.
245  void ClearEditHistory();
246
247  // Set the accessible name of the text field.
248  void SetAccessibleName(const string16& name);
249
250  // Performs the action associated with the specified command id.
251  void ExecuteCommand(int command_id);
252
253  // Provided only for testing:
254  gfx::NativeView GetTestingHandle() const {
255    return native_wrapper_ ? native_wrapper_->GetTestingHandle() : NULL;
256  }
257  NativeTextfieldWrapper* GetNativeWrapperForTesting() const {
258    return native_wrapper_;
259  }
260
261  // Returns whether there is a drag operation originating from the textfield.
262  bool HasTextBeingDragged();
263
264  // Overridden from View:
265  virtual void Layout() OVERRIDE;
266  virtual int GetBaseline() const OVERRIDE;
267  virtual gfx::Size GetPreferredSize() OVERRIDE;
268  virtual void AboutToRequestFocusFromTabTraversal(bool reverse) OVERRIDE;
269  virtual bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) OVERRIDE;
270  virtual void OnEnabledChanged() OVERRIDE;
271  virtual void OnPaintFocusBorder(gfx::Canvas* canvas) OVERRIDE;
272  virtual bool OnKeyPressed(const ui::KeyEvent& e) OVERRIDE;
273  virtual bool OnKeyReleased(const ui::KeyEvent& e) OVERRIDE;
274  virtual bool OnMouseDragged(const ui::MouseEvent& e) OVERRIDE;
275  virtual void OnFocus() OVERRIDE;
276  virtual void OnBlur() OVERRIDE;
277  virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
278  virtual ui::TextInputClient* GetTextInputClient() OVERRIDE;
279
280 protected:
281  virtual void ViewHierarchyChanged(
282      const ViewHierarchyChangedDetails& details) OVERRIDE;
283  virtual const char* GetClassName() const OVERRIDE;
284
285  // The object that actually implements the native text field.
286  NativeTextfieldWrapper* native_wrapper_;
287
288 private:
289  // Returns the insets to the rectangle where text is actually painted.
290  gfx::Insets GetTextInsets() const;
291
292  // Handles a request to change the value of this text field from software
293  // using an accessibility API (typically automation software, screen readers
294  // don't normally use this). Sets the value and clears the selection.
295  void AccessibilitySetValue(const string16& new_value);
296
297  // This is the current listener for events from this Textfield.
298  TextfieldController* controller_;
299
300  // The mask of style options for this Textfield.
301  StyleFlags style_;
302
303  // The fonts used to render the text in the Textfield.
304  gfx::FontList font_list_;
305
306  // The text displayed in the Textfield.
307  string16 text_;
308
309  // True if this Textfield cannot accept input and is read-only.
310  bool read_only_;
311
312  // The default number of average characters for the width of this text field.
313  // This will be reported as the "desired size". Defaults to 0.
314  int default_width_in_chars_;
315
316  // Whether the border is drawn.
317  bool draw_border_;
318
319  // Text color.  Only used if |use_default_text_color_| is false.
320  SkColor text_color_;
321
322  // Should we use the system text color instead of |text_color_|?
323  bool use_default_text_color_;
324
325  // Background color.  Only used if |use_default_background_color_| is false.
326  SkColor background_color_;
327
328  // Should we use the system background color instead of |background_color_|?
329  bool use_default_background_color_;
330
331  // Holds inner textfield margins.
332  gfx::Insets margins_;
333
334  // Holds whether margins were set.
335  bool horizontal_margins_were_set_;
336  bool vertical_margins_were_set_;
337
338  // The vertical alignment of text in the Textfield.
339  gfx::VerticalAlignment vertical_alignment_;
340
341  // Text to display when empty.
342  string16 placeholder_text_;
343
344  // Placeholder text color.
345  SkColor placeholder_text_color_;
346
347  // The accessible name of the text field.
348  string16 accessible_name_;
349
350  // The input type of this text field.
351  ui::TextInputType text_input_type_;
352
353  // The duration to reveal the last typed char for obscured textfields.
354  base::TimeDelta obscured_reveal_duration_;
355
356  // Used to bind callback functions to this object.
357  base::WeakPtrFactory<Textfield> weak_ptr_factory_;
358
359  DISALLOW_COPY_AND_ASSIGN(Textfield);
360};
361
362}  // namespace views
363
364#endif  // UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_
365