Upstream version 7.36.149.0
[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/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/strings/string16.h"
15 #include "base/timer/timer.h"
16 #include "third_party/skia/include/core/SkColor.h"
17 #include "ui/base/ime/text_input_client.h"
18 #include "ui/base/ime/text_input_type.h"
19 #include "ui/base/models/simple_menu_model.h"
20 #include "ui/base/touch/touch_editing_controller.h"
21 #include "ui/events/keycodes/keyboard_codes.h"
22 #include "ui/gfx/font_list.h"
23 #include "ui/gfx/range/range.h"
24 #include "ui/gfx/selection_model.h"
25 #include "ui/gfx/text_constants.h"
26 #include "ui/views/context_menu_controller.h"
27 #include "ui/views/controls/textfield/textfield_model.h"
28 #include "ui/views/drag_controller.h"
29 #include "ui/views/view.h"
30
31 namespace views {
32
33 class MenuRunner;
34 class Painter;
35 class TextfieldController;
36
37 // A views/skia textfield implementation. No platform-specific code is used.
38 class VIEWS_EXPORT Textfield : public View,
39                                public TextfieldModel::Delegate,
40                                public ContextMenuController,
41                                public DragController,
42                                public ui::TouchEditable,
43                                public ui::TextInputClient {
44  public:
45   // The textfield's class name.
46   static const char kViewClassName[];
47
48   // Returns the text cursor blink time in milliseconds, or 0 for no blinking.
49   static size_t GetCaretBlinkMs();
50
51   Textfield();
52   virtual ~Textfield();
53
54   // Set the controller for this textfield.
55   void set_controller(TextfieldController* controller) {
56     controller_ = controller;
57   }
58
59   // Gets/Sets whether or not the Textfield is read-only.
60   bool read_only() const { return read_only_; }
61   void SetReadOnly(bool read_only);
62
63   // Sets the input type; displays only asterisks for TEXT_INPUT_TYPE_PASSWORD.
64   void SetTextInputType(ui::TextInputType type);
65
66   // Gets the text currently displayed in the Textfield.
67   const base::string16& text() const { return model_->text(); }
68
69   // Sets the text currently displayed in the Textfield.  This doesn't
70   // change the cursor position if the current cursor is within the
71   // new text's range, or moves the cursor to the end if the cursor is
72   // out of the new text's range.
73   void SetText(const base::string16& new_text);
74
75   // Appends the given string to the previously-existing text in the field.
76   void AppendText(const base::string16& new_text);
77
78   // Inserts |new_text| at the cursor position, replacing any selected text.
79   void InsertOrReplaceText(const base::string16& new_text);
80
81   // Returns the text direction.
82   base::i18n::TextDirection GetTextDirection() const;
83
84   // Returns the text that is currently selected.
85   base::string16 GetSelectedText() const;
86
87   // Select the entire text range. If |reversed| is true, the range will end at
88   // the logical beginning of the text; this generally shows the leading portion
89   // of text that overflows its display area.
90   void SelectAll(bool reversed);
91
92   // Clears the selection within the edit field and sets the caret to the end.
93   void ClearSelection();
94
95   // Checks if there is any selected text.
96   bool HasSelection() const;
97
98   // Gets/Sets the text color to be used when painting the Textfield.
99   // Call |UseDefaultTextColor| to restore the default system color.
100   SkColor GetTextColor() const;
101   void SetTextColor(SkColor color);
102   void UseDefaultTextColor();
103
104   // Gets/Sets the background color to be used when painting the Textfield.
105   // Call |UseDefaultBackgroundColor| to restore the default system color.
106   SkColor GetBackgroundColor() const;
107   void SetBackgroundColor(SkColor color);
108   void UseDefaultBackgroundColor();
109
110   // Gets/Sets whether or not the cursor is enabled.
111   bool GetCursorEnabled() const;
112   void SetCursorEnabled(bool enabled);
113
114   // Gets/Sets the fonts used when rendering the text within the Textfield.
115   const gfx::FontList& GetFontList() const;
116   void SetFontList(const gfx::FontList& font_list);
117
118   // Sets the default width of the text control. See default_width_in_chars_.
119   void set_default_width_in_chars(int default_width) {
120     default_width_in_chars_ = default_width;
121   }
122
123   // Sets the text to display when empty.
124   void set_placeholder_text(const base::string16& text) {
125     placeholder_text_ = text;
126   }
127   virtual base::string16 GetPlaceholderText() const;
128
129   SkColor placeholder_text_color() const { return placeholder_text_color_; }
130   void set_placeholder_text_color(SkColor color) {
131     placeholder_text_color_ = color;
132   }
133
134   // Get or set the horizontal alignment used for the button from the underlying
135   // RenderText object.
136   gfx::HorizontalAlignment GetHorizontalAlignment() const;
137   void SetHorizontalAlignment(gfx::HorizontalAlignment alignment);
138
139   // Displays a virtual keyboard or alternate input view if enabled.
140   void ShowImeIfNeeded();
141
142   // Returns whether or not an IME is composing text.
143   bool IsIMEComposing() const;
144
145   // Gets the selected logical text range.
146   const gfx::Range& GetSelectedRange() const;
147
148   // Selects the specified logical text range.
149   void SelectRange(const gfx::Range& range);
150
151   // Gets the text selection model.
152   const gfx::SelectionModel& GetSelectionModel() const;
153
154   // Sets the specified text selection model.
155   void SelectSelectionModel(const gfx::SelectionModel& sel);
156
157   // Returns the current cursor position.
158   size_t GetCursorPosition() const;
159
160   // Set the text color over the entire text or a logical character range.
161   // Empty and invalid ranges are ignored.
162   void SetColor(SkColor value);
163   void ApplyColor(SkColor value, const gfx::Range& range);
164
165   // Set various text styles over the entire text or a logical character range.
166   // The respective |style| is applied if |value| is true, or removed if false.
167   // Empty and invalid ranges are ignored.
168   void SetStyle(gfx::TextStyle style, bool value);
169   void ApplyStyle(gfx::TextStyle style, bool value, const gfx::Range& range);
170
171   // Clears Edit history.
172   void ClearEditHistory();
173
174   // Set the accessible name of the text field.
175   void SetAccessibleName(const base::string16& name);
176
177   // Performs the action associated with the specified command id.
178   void ExecuteCommand(int command_id);
179
180   void SetFocusPainter(scoped_ptr<Painter> focus_painter);
181
182   // Returns whether there is a drag operation originating from the textfield.
183   bool HasTextBeingDragged();
184
185   // View overrides:
186   virtual int GetBaseline() const OVERRIDE;
187   virtual gfx::Size GetPreferredSize() OVERRIDE;
188   virtual const char* GetClassName() const OVERRIDE;
189   virtual gfx::NativeCursor GetCursor(const ui::MouseEvent& event) OVERRIDE;
190   virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
191   virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
192   virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
193   virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE;
194   virtual ui::TextInputClient* GetTextInputClient() OVERRIDE;
195   virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
196   virtual void AboutToRequestFocusFromTabTraversal(bool reverse) OVERRIDE;
197   virtual bool SkipDefaultKeyEventProcessing(
198       const ui::KeyEvent& event) OVERRIDE;
199   virtual bool GetDropFormats(
200       int* formats,
201       std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE;
202   virtual bool CanDrop(const ui::OSExchangeData& data) OVERRIDE;
203   virtual int OnDragUpdated(const ui::DropTargetEvent& event) OVERRIDE;
204   virtual void OnDragExited() OVERRIDE;
205   virtual int OnPerformDrop(const ui::DropTargetEvent& event) OVERRIDE;
206   virtual void OnDragDone() OVERRIDE;
207   virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
208   virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
209   virtual void OnEnabledChanged() OVERRIDE;
210   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
211   virtual void OnFocus() OVERRIDE;
212   virtual void OnBlur() OVERRIDE;
213   virtual gfx::Point GetKeyboardContextMenuLocation() OVERRIDE;
214   virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE;
215
216   // TextfieldModel::Delegate overrides:
217   virtual void OnCompositionTextConfirmedOrCleared() OVERRIDE;
218
219   // ContextMenuController overrides:
220   virtual void ShowContextMenuForView(View* source,
221                                       const gfx::Point& point,
222                                       ui::MenuSourceType source_type) OVERRIDE;
223
224   // DragController overrides:
225   virtual void WriteDragDataForView(View* sender,
226                                     const gfx::Point& press_pt,
227                                     ui::OSExchangeData* data) OVERRIDE;
228   virtual int GetDragOperationsForView(View* sender,
229                                        const gfx::Point& p) OVERRIDE;
230   virtual bool CanStartDragForView(View* sender,
231                                    const gfx::Point& press_pt,
232                                    const gfx::Point& p) OVERRIDE;
233
234   // ui::TouchEditable overrides:
235   virtual void SelectRect(const gfx::Point& start,
236                           const gfx::Point& end) OVERRIDE;
237   virtual void MoveCaretTo(const gfx::Point& point) OVERRIDE;
238   virtual void GetSelectionEndPoints(gfx::Rect* p1, gfx::Rect* p2) OVERRIDE;
239   virtual gfx::Rect GetBounds() OVERRIDE;
240   virtual gfx::NativeView GetNativeView() const OVERRIDE;
241   virtual void ConvertPointToScreen(gfx::Point* point) OVERRIDE;
242   virtual void ConvertPointFromScreen(gfx::Point* point) OVERRIDE;
243   virtual bool DrawsHandles() OVERRIDE;
244   virtual void OpenContextMenu(const gfx::Point& anchor) OVERRIDE;
245   virtual void DestroyTouchSelection() OVERRIDE;
246
247   // ui::SimpleMenuModel::Delegate overrides:
248   virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
249   virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
250   virtual bool GetAcceleratorForCommandId(
251       int command_id,
252       ui::Accelerator* accelerator) OVERRIDE;
253   virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
254
255   // ui::TextInputClient overrides:
256   virtual void SetCompositionText(
257       const ui::CompositionText& composition) OVERRIDE;
258   virtual void ConfirmCompositionText() OVERRIDE;
259   virtual void ClearCompositionText() OVERRIDE;
260   virtual void InsertText(const base::string16& text) OVERRIDE;
261   virtual void InsertChar(base::char16 ch, int flags) OVERRIDE;
262   virtual gfx::NativeWindow GetAttachedWindow() const OVERRIDE;
263   virtual ui::TextInputType GetTextInputType() const OVERRIDE;
264   virtual ui::TextInputMode GetTextInputMode() const OVERRIDE;
265   virtual bool CanComposeInline() const OVERRIDE;
266   virtual gfx::Rect GetCaretBounds() const OVERRIDE;
267   virtual bool GetCompositionCharacterBounds(uint32 index,
268                                              gfx::Rect* rect) const OVERRIDE;
269   virtual bool HasCompositionText() const OVERRIDE;
270   virtual bool GetTextRange(gfx::Range* range) const OVERRIDE;
271   virtual bool GetCompositionTextRange(gfx::Range* range) const OVERRIDE;
272   virtual bool GetSelectionRange(gfx::Range* range) const OVERRIDE;
273   virtual bool SetSelectionRange(const gfx::Range& range) OVERRIDE;
274   virtual bool DeleteRange(const gfx::Range& range) OVERRIDE;
275   virtual bool GetTextFromRange(const gfx::Range& range,
276                                 base::string16* text) const OVERRIDE;
277   virtual void OnInputMethodChanged() OVERRIDE;
278   virtual bool ChangeTextDirectionAndLayoutAlignment(
279       base::i18n::TextDirection direction) OVERRIDE;
280   virtual void ExtendSelectionAndDelete(size_t before, size_t after) OVERRIDE;
281   virtual void EnsureCaretInRect(const gfx::Rect& rect) OVERRIDE;
282   virtual void OnCandidateWindowShown() OVERRIDE;
283   virtual void OnCandidateWindowUpdated() OVERRIDE;
284   virtual void OnCandidateWindowHidden() OVERRIDE;
285
286  protected:
287   // Returns the TextfieldModel's text/cursor/selection rendering model.
288   gfx::RenderText* GetRenderText() const;
289
290   gfx::Point last_click_location() const { return last_click_location_; }
291
292   // Get the text from the selection clipboard.
293   virtual base::string16 GetSelectionClipboardText() const;
294
295  private:
296   friend class TextfieldTest;
297   friend class TouchSelectionControllerImplTest;
298
299   // Handles a request to change the value of this text field from software
300   // using an accessibility API (typically automation software, screen readers
301   // don't normally use this). Sets the value and clears the selection.
302   void AccessibilitySetValue(const base::string16& new_value);
303
304   // Updates the painted background color.
305   void UpdateBackgroundColor();
306
307   // Does necessary updates when the text and/or cursor position changes.
308   void UpdateAfterChange(bool text_changed, bool cursor_changed);
309
310   // A callback function to periodically update the cursor state.
311   void UpdateCursor();
312
313   // Repaint the cursor.
314   void RepaintCursor();
315
316   void PaintTextAndCursor(gfx::Canvas* canvas);
317
318   // Helper function to call MoveCursorTo on the TextfieldModel.
319   void MoveCursorTo(const gfx::Point& point, bool select);
320
321   // Convenience method to notify the InputMethod and TouchSelectionController.
322   void OnCaretBoundsChanged();
323
324   // Convenience method to call TextfieldController::OnBeforeUserAction();
325   void OnBeforeUserAction();
326
327   // Convenience method to call TextfieldController::OnAfterUserAction();
328   void OnAfterUserAction();
329
330   // Calls |model_->Cut()| and notifies TextfieldController on success.
331   bool Cut();
332
333   // Calls |model_->Copy()| and notifies TextfieldController on success.
334   bool Copy();
335
336   // Calls |model_->Paste()| and calls TextfieldController::ContentsChanged()
337   // explicitly if paste succeeded.
338   bool Paste();
339
340   // Utility function to prepare the context menu.
341   void UpdateContextMenu();
342
343   // Tracks the mouse clicks for single/double/triple clicks.
344   void TrackMouseClicks(const ui::MouseEvent& event);
345
346   // Returns true if the current text input type allows access by the IME.
347   bool ImeEditingAllowed() const;
348
349   // Reveals the password character at |index| for a set duration.
350   // If |index| is -1, the existing revealed character will be reset.
351   void RevealPasswordChar(int index);
352
353   void CreateTouchSelectionControllerAndNotifyIt();
354
355   // Updates the selection clipboard to any non-empty text selection.
356   void UpdateSelectionClipboard() const;
357
358   // Pastes the selection clipboard for the specified mouse event.
359   void PasteSelectionClipboard(const ui::MouseEvent& event);
360
361   // The text model.
362   scoped_ptr<TextfieldModel> model_;
363
364   // This is the current listener for events from this Textfield.
365   TextfieldController* controller_;
366
367   // True if this Textfield cannot accept input and is read-only.
368   bool read_only_;
369
370   // The default number of average characters for the width of this text field.
371   // This will be reported as the "desired size". Defaults to 0.
372   int default_width_in_chars_;
373
374   scoped_ptr<Painter> focus_painter_;
375
376   // Text color.  Only used if |use_default_text_color_| is false.
377   SkColor text_color_;
378
379   // Should we use the system text color instead of |text_color_|?
380   bool use_default_text_color_;
381
382   // Background color.  Only used if |use_default_background_color_| is false.
383   SkColor background_color_;
384
385   // Should we use the system background color instead of |background_color_|?
386   bool use_default_background_color_;
387
388   // Text to display when empty.
389   base::string16 placeholder_text_;
390
391   // Placeholder text color.
392   SkColor placeholder_text_color_;
393
394   // The accessible name of the text field.
395   base::string16 accessible_name_;
396
397   // The input type of this text field.
398   ui::TextInputType text_input_type_;
399
400   // The duration and timer to reveal the last typed password character.
401   base::TimeDelta password_reveal_duration_;
402   base::OneShotTimer<Textfield> password_reveal_timer_;
403
404   // Keeps track of whether currently performing a user action (i.e. when
405   // OnBeforeUserAction() has been called, but OnAfterUserAction() is yet to be
406   // called).
407   bool performing_user_action_;
408
409   // True if InputMethod::CancelComposition() should not be called.
410   bool skip_input_method_cancel_composition_;
411
412   // The text editing cursor repaint timer and visibility.
413   base::RepeatingTimer<Textfield> cursor_repaint_timer_;
414   bool cursor_visible_;
415
416   // The drop cursor is a visual cue for where dragged text will be dropped.
417   bool drop_cursor_visible_;
418   gfx::SelectionModel drop_cursor_position_;
419
420   // Is the user potentially dragging and dropping from this view?
421   bool initiating_drag_;
422
423   // State variables used to track double and triple clicks.
424   size_t aggregated_clicks_;
425   base::TimeDelta last_click_time_;
426   gfx::Point last_click_location_;
427   gfx::Range double_click_word_;
428
429   scoped_ptr<ui::TouchSelectionController> touch_selection_controller_;
430
431   // Context menu related members.
432   scoped_ptr<ui::SimpleMenuModel> context_menu_contents_;
433   scoped_ptr<views::MenuRunner> context_menu_runner_;
434
435   // Used to bind callback functions to this object.
436   base::WeakPtrFactory<Textfield> weak_ptr_factory_;
437
438   DISALLOW_COPY_AND_ASSIGN(Textfield);
439 };
440
441 }  // namespace views
442
443 #endif  // UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_