- add sources.
[platform/framework/web/crosswalk.git] / src / ui / views / controls / textfield / textfield.h
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/events/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
29 namespace gfx {
30 class Range;
31 class ImageSkia;
32 }
33
34 namespace ui {
35 class TextInputClient;
36 }  // namespace ui
37
38 namespace views {
39
40 class ImageView;
41
42 class TextfieldController;
43
44 // This class implements a View that wraps a native text (edit) field.
45 class 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   // Returns the text cursor blink time in milliseconds, or 0 for no blinking.
60   static size_t GetCaretBlinkMs();
61
62   Textfield();
63   explicit Textfield(StyleFlags style);
64   virtual ~Textfield();
65
66   // TextfieldController accessors
67   void SetController(TextfieldController* controller);
68   TextfieldController* GetController() const;
69
70   // Gets/Sets whether or not the Textfield is read-only.
71   bool read_only() const { return read_only_; }
72   void SetReadOnly(bool read_only);
73
74   // Gets/sets the STYLE_OBSCURED bit, controlling whether characters in this
75   // Textfield are displayed as asterisks/bullets.
76   bool IsObscured() const;
77   void SetObscured(bool obscured);
78
79   // Gets/sets the duration to reveal the last typed char when the obscured bit
80   // is set. A duration of zero effectively disables the feature. Other values
81   // cause the last typed char to be shown for the defined duration. Note this
82   // only works with NativeTextfieldViews.
83   const base::TimeDelta& obscured_reveal_duration() const {
84     return obscured_reveal_duration_;
85   }
86   void set_obscured_reveal_duration(const base::TimeDelta& duration) {
87     obscured_reveal_duration_ = duration;
88   }
89
90   // Gets/Sets the input type of this textfield.
91   ui::TextInputType GetTextInputType() const;
92   void SetTextInputType(ui::TextInputType type);
93
94   // Gets/Sets the text currently displayed in the Textfield.
95   const string16& text() const { return text_; }
96
97   // Sets the text currently displayed in the Textfield.  This doesn't
98   // change the cursor position if the current cursor is within the
99   // new text's range, or moves the cursor to the end if the cursor is
100   // out of the new text's range.
101   void SetText(const string16& text);
102
103   // Appends the given string to the previously-existing text in the field.
104   void AppendText(const string16& text);
105
106   // Inserts |text| at the current cursor position, replacing any selected text.
107   void InsertOrReplaceText(const string16& text);
108
109   // Returns the text direction.
110   base::i18n::TextDirection GetTextDirection() const;
111
112   // Returns the text that is currently selected.
113   string16 GetSelectedText() const;
114
115   // Select the entire text range. If |reversed| is true, the range will end at
116   // the logical beginning of the text; this generally shows the leading portion
117   // of text that overflows its display area.
118   void SelectAll(bool reversed);
119
120   // Clears the selection within the edit field and sets the caret to the end.
121   void ClearSelection() const;
122
123   // Checks if there is any selected text.
124   bool HasSelection() const;
125
126   // Accessor for |style_|.
127   StyleFlags style() const { return style_; }
128
129   // Gets/Sets the text color to be used when painting the Textfield.
130   // Call |UseDefaultTextColor| to restore the default system color.
131   SkColor GetTextColor() const;
132   void SetTextColor(SkColor color);
133   void UseDefaultTextColor();
134
135   // Gets/Sets the background color to be used when painting the Textfield.
136   // Call |UseDefaultBackgroundColor| to restore the default system color.
137   SkColor GetBackgroundColor() const;
138   void SetBackgroundColor(SkColor color);
139   void UseDefaultBackgroundColor();
140
141   // Gets/Sets whether or not the cursor is enabled.
142   bool GetCursorEnabled() const;
143   void SetCursorEnabled(bool enabled);
144
145   // Gets/Sets the fonts used when rendering the text within the Textfield.
146   const gfx::FontList& font_list() const { return font_list_; }
147   void SetFontList(const gfx::FontList& font_list);
148   const gfx::Font& GetPrimaryFont() const;
149   void SetFont(const gfx::Font& font);
150
151   // Sets the left and right margin (in pixels) within the text box. On Windows
152   // this is accomplished by packing the left and right margin into a single
153   // 32 bit number, so the left and right margins are effectively 16 bits.
154   void SetHorizontalMargins(int left, int right);
155
156   // Sets the top and bottom margins (in pixels) within the textfield.
157   // NOTE: in most cases height could be changed instead.
158   void SetVerticalMargins(int top, int bottom);
159
160   // Sets the default width of the text control. See default_width_in_chars_.
161   void set_default_width_in_chars(int default_width) {
162     default_width_in_chars_ = default_width;
163   }
164
165   // Removes the border from the edit box, giving it a 2D look.
166   bool draw_border() const { return draw_border_; }
167   void RemoveBorder();
168
169   // Sets the text to display when empty.
170   void set_placeholder_text(const string16& text) {
171     placeholder_text_ = text;
172   }
173   virtual base::string16 GetPlaceholderText() const;
174
175   SkColor placeholder_text_color() const { return placeholder_text_color_; }
176   void set_placeholder_text_color(SkColor color) {
177     placeholder_text_color_ = color;
178   }
179
180   // Getter for the horizontal margins that were set. Returns false if
181   // horizontal margins weren't set.
182   bool GetHorizontalMargins(int* left, int* right);
183
184   // Getter for the vertical margins that were set. Returns false if vertical
185   // margins weren't set.
186   bool GetVerticalMargins(int* top, int* bottom);
187
188   // Updates all properties on the textfield. This is invoked internally.
189   // Users of Textfield never need to invoke this directly.
190   void UpdateAllProperties();
191
192   // Invoked by the edit control when the value changes. This method set
193   // the text_ member variable to the value contained in edit control.
194   // This is important because the edit control can be replaced if it has
195   // been deleted during a window close.
196   void SyncText();
197
198   // Returns whether or not an IME is composing text.
199   bool IsIMEComposing() const;
200
201   // Gets the selected range. This is views-implementation only and
202   // has to be called after the wrapper is created.
203   // TODO(msw): Return a const reference when NativeTextfieldWin is gone.
204   gfx::Range GetSelectedRange() const;
205
206   // Selects the text given by |range|. This is views-implementation only and
207   // has to be called after the wrapper is created.
208   void SelectRange(const gfx::Range& range);
209
210   // Gets the selection model. This is views-implementation only and
211   // has to be called after the wrapper is created.
212   // TODO(msw): Return a const reference when NativeTextfieldWin is gone.
213   gfx::SelectionModel GetSelectionModel() const;
214
215   // Selects the text given by |sel|. This is views-implementation only and
216   // has to be called after the wrapper is created.
217   void SelectSelectionModel(const gfx::SelectionModel& sel);
218
219   // Returns the current cursor position. This is views-implementation
220   // only and has to be called after the wrapper is created.
221   size_t GetCursorPosition() const;
222
223   // Set the text color over the entire text or a logical character range.
224   // Empty and invalid ranges are ignored. This is views-implementation only and
225   // has to be called after the wrapper is created.
226   void SetColor(SkColor value);
227   void ApplyColor(SkColor value, const gfx::Range& range);
228
229   // Set various text styles over the entire text or a logical character range.
230   // The respective |style| is applied if |value| is true, or removed if false.
231   // Empty and invalid ranges are ignored. This is views-implementation only and
232   // has to be called after the wrapper is created.
233   void SetStyle(gfx::TextStyle style, bool value);
234   void ApplyStyle(gfx::TextStyle style, bool value, const gfx::Range& range);
235
236   // Clears Edit history.
237   void ClearEditHistory();
238
239   // Set the accessible name of the text field.
240   void SetAccessibleName(const string16& name);
241
242   // Performs the action associated with the specified command id.
243   void ExecuteCommand(int command_id);
244
245   // Provided only for testing:
246   gfx::NativeView GetTestingHandle() const {
247     return native_wrapper_ ? native_wrapper_->GetTestingHandle() : NULL;
248   }
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 OnPaintFocusBorder(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   DISALLOW_COPY_AND_ASSIGN(Textfield);
350 };
351
352 }  // namespace views
353
354 #endif  // UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_