Upstream version 5.34.104.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   // Displays a virtual keyboard or alternate input view if enabled.
135   void ShowImeIfNeeded();
136
137   // Returns whether or not an IME is composing text.
138   bool IsIMEComposing() const;
139
140   // Gets the selected logical text range.
141   const gfx::Range& GetSelectedRange() const;
142
143   // Selects the specified logical text range.
144   void SelectRange(const gfx::Range& range);
145
146   // Gets the text selection model.
147   const gfx::SelectionModel& GetSelectionModel() const;
148
149   // Sets the specified text selection model.
150   void SelectSelectionModel(const gfx::SelectionModel& sel);
151
152   // Returns the current cursor position.
153   size_t GetCursorPosition() const;
154
155   // Set the text color over the entire text or a logical character range.
156   // Empty and invalid ranges are ignored.
157   void SetColor(SkColor value);
158   void ApplyColor(SkColor value, const gfx::Range& range);
159
160   // Set various text styles over the entire text or a logical character range.
161   // The respective |style| is applied if |value| is true, or removed if false.
162   // Empty and invalid ranges are ignored.
163   void SetStyle(gfx::TextStyle style, bool value);
164   void ApplyStyle(gfx::TextStyle style, bool value, const gfx::Range& range);
165
166   // Clears Edit history.
167   void ClearEditHistory();
168
169   // Set the accessible name of the text field.
170   void SetAccessibleName(const base::string16& name);
171
172   // Performs the action associated with the specified command id.
173   void ExecuteCommand(int command_id);
174
175   void SetFocusPainter(scoped_ptr<Painter> focus_painter);
176
177   // Returns whether there is a drag operation originating from the textfield.
178   bool HasTextBeingDragged();
179
180   // View overrides:
181   // TODO(msw): Match declaration and definition order to View.
182   virtual int GetBaseline() const OVERRIDE;
183   virtual gfx::Size GetPreferredSize() OVERRIDE;
184   virtual void AboutToRequestFocusFromTabTraversal(bool reverse) OVERRIDE;
185   virtual bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) OVERRIDE;
186   virtual void OnEnabledChanged() OVERRIDE;
187   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
188   virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE;
189   virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
190   virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
191   virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
192   virtual void OnFocus() OVERRIDE;
193   virtual void OnBlur() OVERRIDE;
194   virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
195   virtual ui::TextInputClient* GetTextInputClient() OVERRIDE;
196   virtual gfx::Point GetKeyboardContextMenuLocation() OVERRIDE;
197   virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE;
198   virtual const char* GetClassName() const OVERRIDE;
199   virtual gfx::NativeCursor GetCursor(const ui::MouseEvent& event) OVERRIDE;
200   virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
201   virtual bool GetDropFormats(
202       int* formats,
203       std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE;
204   virtual bool CanDrop(const ui::OSExchangeData& data) OVERRIDE;
205   virtual int OnDragUpdated(const ui::DropTargetEvent& event) OVERRIDE;
206   virtual void OnDragExited() OVERRIDE;
207   virtual int OnPerformDrop(const ui::DropTargetEvent& event) OVERRIDE;
208   virtual void OnDragDone() OVERRIDE;
209   virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
210   virtual void ViewHierarchyChanged(
211       const ViewHierarchyChangedDetails& details) OVERRIDE;
212
213   // TextfieldModel::Delegate overrides:
214   virtual void OnCompositionTextConfirmedOrCleared() OVERRIDE;
215
216   // ContextMenuController overrides:
217   virtual void ShowContextMenuForView(View* source,
218                                       const gfx::Point& point,
219                                       ui::MenuSourceType source_type) OVERRIDE;
220
221   // DragController overrides:
222   virtual void WriteDragDataForView(View* sender,
223                                     const gfx::Point& press_pt,
224                                     ui::OSExchangeData* data) OVERRIDE;
225   virtual int GetDragOperationsForView(View* sender,
226                                        const gfx::Point& p) OVERRIDE;
227   virtual bool CanStartDragForView(View* sender,
228                                    const gfx::Point& press_pt,
229                                    const gfx::Point& p) OVERRIDE;
230
231   // ui::TouchEditable overrides:
232   virtual void SelectRect(const gfx::Point& start,
233                           const gfx::Point& end) OVERRIDE;
234   virtual void MoveCaretTo(const gfx::Point& point) OVERRIDE;
235   virtual void GetSelectionEndPoints(gfx::Rect* p1, gfx::Rect* p2) OVERRIDE;
236   virtual gfx::Rect GetBounds() OVERRIDE;
237   virtual gfx::NativeView GetNativeView() const OVERRIDE;
238   virtual void ConvertPointToScreen(gfx::Point* point) OVERRIDE;
239   virtual void ConvertPointFromScreen(gfx::Point* point) OVERRIDE;
240   virtual bool DrawsHandles() OVERRIDE;
241   virtual void OpenContextMenu(const gfx::Point& anchor) OVERRIDE;
242
243   // ui::SimpleMenuModel::Delegate overrides:
244   virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
245   virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
246   virtual bool GetAcceleratorForCommandId(
247       int command_id,
248       ui::Accelerator* accelerator) OVERRIDE;
249   virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
250
251   // ui::TextInputClient overrides:
252   virtual void SetCompositionText(
253       const ui::CompositionText& composition) OVERRIDE;
254   virtual void ConfirmCompositionText() OVERRIDE;
255   virtual void ClearCompositionText() OVERRIDE;
256   virtual void InsertText(const base::string16& text) OVERRIDE;
257   virtual void InsertChar(base::char16 ch, int flags) OVERRIDE;
258   virtual gfx::NativeWindow GetAttachedWindow() const OVERRIDE;
259   virtual ui::TextInputType GetTextInputType() const OVERRIDE;
260   virtual ui::TextInputMode GetTextInputMode() const OVERRIDE;
261   virtual bool CanComposeInline() const OVERRIDE;
262   virtual gfx::Rect GetCaretBounds() const OVERRIDE;
263   virtual bool GetCompositionCharacterBounds(uint32 index,
264                                              gfx::Rect* rect) const OVERRIDE;
265   virtual bool HasCompositionText() const OVERRIDE;
266   virtual bool GetTextRange(gfx::Range* range) const OVERRIDE;
267   virtual bool GetCompositionTextRange(gfx::Range* range) const OVERRIDE;
268   virtual bool GetSelectionRange(gfx::Range* range) const OVERRIDE;
269   virtual bool SetSelectionRange(const gfx::Range& range) OVERRIDE;
270   virtual bool DeleteRange(const gfx::Range& range) OVERRIDE;
271   virtual bool GetTextFromRange(const gfx::Range& range,
272                                 base::string16* text) const OVERRIDE;
273   virtual void OnInputMethodChanged() OVERRIDE;
274   virtual bool ChangeTextDirectionAndLayoutAlignment(
275       base::i18n::TextDirection direction) OVERRIDE;
276   virtual void ExtendSelectionAndDelete(size_t before, size_t after) OVERRIDE;
277   virtual void EnsureCaretInRect(const gfx::Rect& rect) OVERRIDE;
278   virtual void OnCandidateWindowShown() OVERRIDE;
279   virtual void OnCandidateWindowUpdated() OVERRIDE;
280   virtual void OnCandidateWindowHidden() OVERRIDE;
281
282  protected:
283   // Returns the TextfieldModel's text/cursor/selection rendering model.
284   gfx::RenderText* GetRenderText() const;
285
286   gfx::Point last_click_location() const { return last_click_location_; }
287
288   // Get the text from the selection clipboard.
289   virtual base::string16 GetSelectionClipboardText() const;
290
291  private:
292   friend class TextfieldTest;
293   friend class TouchSelectionControllerImplTest;
294
295   // Handles a request to change the value of this text field from software
296   // using an accessibility API (typically automation software, screen readers
297   // don't normally use this). Sets the value and clears the selection.
298   void AccessibilitySetValue(const base::string16& new_value);
299
300   // Updates the painted background color.
301   void UpdateBackgroundColor();
302
303   // Updates any colors that have not been explicitly set from the theme.
304   void UpdateColorsFromTheme(const ui::NativeTheme* theme);
305
306   // Does necessary updates when the text and/or cursor position changes.
307   void UpdateAfterChange(bool text_changed, bool cursor_changed);
308
309   // A callback function to periodically update the cursor state.
310   void UpdateCursor();
311
312   // Repaint the cursor.
313   void RepaintCursor();
314
315   void PaintTextAndCursor(gfx::Canvas* canvas);
316
317   // Helper function to call MoveCursorTo on the TextfieldModel.
318   void MoveCursorTo(const gfx::Point& point, bool select);
319
320   // Convenience method to notify the InputMethod and TouchSelectionController.
321   void OnCaretBoundsChanged();
322
323   // Convenience method to call TextfieldController::OnBeforeUserAction();
324   void OnBeforeUserAction();
325
326   // Convenience method to call TextfieldController::OnAfterUserAction();
327   void OnAfterUserAction();
328
329   // Calls |model_->Cut()| and notifies TextfieldController on success.
330   bool Cut();
331
332   // Calls |model_->Copy()| and notifies TextfieldController on success.
333   bool Copy();
334
335   // Calls |model_->Paste()| and calls TextfieldController::ContentsChanged()
336   // explicitly if paste succeeded.
337   bool Paste();
338
339   // Utility function to prepare the context menu.
340   void UpdateContextMenu();
341
342   // Tracks the mouse clicks for single/double/triple clicks.
343   void TrackMouseClicks(const ui::MouseEvent& event);
344
345   // Returns true if the current text input type allows access by the IME.
346   bool ImeEditingAllowed() const;
347
348   // Reveals the password character at |index| for a set duration.
349   // If |index| is -1, the existing revealed character will be reset.
350   void RevealPasswordChar(int index);
351
352   void CreateTouchSelectionControllerAndNotifyIt();
353
354   // Updates the selection clipboard to any non-empty text selection.
355   void UpdateSelectionClipboard() const;
356
357   // Pastes the selection clipboard for the specified mouse event.
358   void PasteSelectionClipboard(const ui::MouseEvent& event);
359
360   // The text model.
361   scoped_ptr<TextfieldModel> model_;
362
363   // This is the current listener for events from this Textfield.
364   TextfieldController* controller_;
365
366   // True if this Textfield cannot accept input and is read-only.
367   bool read_only_;
368
369   // The default number of average characters for the width of this text field.
370   // This will be reported as the "desired size". Defaults to 0.
371   int default_width_in_chars_;
372
373   scoped_ptr<Painter> focus_painter_;
374
375   // Text color.  Only used if |use_default_text_color_| is false.
376   SkColor text_color_;
377
378   // Should we use the system text color instead of |text_color_|?
379   bool use_default_text_color_;
380
381   // Background color.  Only used if |use_default_background_color_| is false.
382   SkColor background_color_;
383
384   // Should we use the system background color instead of |background_color_|?
385   bool use_default_background_color_;
386
387   // Text to display when empty.
388   base::string16 placeholder_text_;
389
390   // Placeholder text color.
391   SkColor placeholder_text_color_;
392
393   // The accessible name of the text field.
394   base::string16 accessible_name_;
395
396   // The input type of this text field.
397   ui::TextInputType text_input_type_;
398
399   // The duration and timer to reveal the last typed password character.
400   base::TimeDelta password_reveal_duration_;
401   base::OneShotTimer<Textfield> password_reveal_timer_;
402
403   // True if InputMethod::CancelComposition() should not be called.
404   bool skip_input_method_cancel_composition_;
405
406   // The text editing cursor repaint timer and visibility.
407   base::RepeatingTimer<Textfield> cursor_repaint_timer_;
408   bool cursor_visible_;
409
410   // The drop cursor is a visual cue for where dragged text will be dropped.
411   bool drop_cursor_visible_;
412   gfx::SelectionModel drop_cursor_position_;
413
414   // Is the user potentially dragging and dropping from this view?
415   bool initiating_drag_;
416
417   // State variables used to track double and triple clicks.
418   size_t aggregated_clicks_;
419   base::TimeDelta last_click_time_;
420   gfx::Point last_click_location_;
421   gfx::Range double_click_word_;
422
423   scoped_ptr<ui::TouchSelectionController> touch_selection_controller_;
424
425   // Context menu related members.
426   scoped_ptr<ui::SimpleMenuModel> context_menu_contents_;
427   scoped_ptr<views::MenuRunner> context_menu_runner_;
428
429   // Used to bind callback functions to this object.
430   base::WeakPtrFactory<Textfield> weak_ptr_factory_;
431
432   DISALLOW_COPY_AND_ASSIGN(Textfield);
433 };
434
435 }  // namespace views
436
437 #endif  // UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_