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