| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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_NATIVE_TEXTFIELD_VIEWS_H_ | |
| 6 #define UI_VIEWS_CONTROLS_TEXTFIELD_NATIVE_TEXTFIELD_VIEWS_H_ | |
| 7 | |
| 8 #include "base/memory/weak_ptr.h" | |
| 9 #include "base/strings/string16.h" | |
| 10 #include "base/timer/timer.h" | |
| 11 #include "ui/base/ime/text_input_client.h" | |
| 12 #include "ui/base/models/simple_menu_model.h" | |
| 13 #include "ui/base/touch/touch_editing_controller.h" | |
| 14 #include "ui/events/event_constants.h" | |
| 15 #include "ui/gfx/selection_model.h" | |
| 16 #include "ui/views/border.h" | |
| 17 #include "ui/views/context_menu_controller.h" | |
| 18 #include "ui/views/controls/textfield/textfield_views_model.h" | |
| 19 #include "ui/views/drag_controller.h" | |
| 20 #include "ui/views/view.h" | |
| 21 | |
| 22 namespace base { | |
| 23 class Time; | |
| 24 } | |
| 25 | |
| 26 namespace gfx { | |
| 27 class Canvas; | |
| 28 } | |
| 29 | |
| 30 namespace views { | |
| 31 | |
| 32 class FocusableBorder; | |
| 33 class MenuModelAdapter; | |
| 34 class MenuRunner; | |
| 35 class Textfield; | |
| 36 | |
| 37 // A views/skia textfield implementation. No platform-specific code is used. | |
| 38 // TODO(msw): Merge views::NativeTextfieldViews and views::Textfield classes. | |
| 39 class VIEWS_EXPORT NativeTextfieldViews : public View, | |
| 40 public ui::TouchEditable, | |
| 41 public ContextMenuController, | |
| 42 public DragController, | |
| 43 public ui::TextInputClient, | |
| 44 public TextfieldViewsModel::Delegate { | |
| 45 public: | |
| 46 explicit NativeTextfieldViews(Textfield* parent); | |
| 47 virtual ~NativeTextfieldViews(); | |
| 48 | |
| 49 // View overrides: | |
| 50 virtual gfx::NativeCursor GetCursor(const ui::MouseEvent& event) OVERRIDE; | |
| 51 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE; | |
| 52 virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE; | |
| 53 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE; | |
| 54 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE; | |
| 55 virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE; | |
| 56 virtual bool GetDropFormats( | |
| 57 int* formats, | |
| 58 std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE; | |
| 59 virtual bool CanDrop(const ui::OSExchangeData& data) OVERRIDE; | |
| 60 virtual int OnDragUpdated(const ui::DropTargetEvent& event) OVERRIDE; | |
| 61 virtual void OnDragExited() OVERRIDE; | |
| 62 virtual int OnPerformDrop(const ui::DropTargetEvent& event) OVERRIDE; | |
| 63 virtual void OnDragDone() OVERRIDE; | |
| 64 virtual bool OnKeyReleased(const ui::KeyEvent& event) OVERRIDE; | |
| 65 virtual ui::TextInputClient* GetTextInputClient() OVERRIDE; | |
| 66 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; | |
| 67 virtual void OnFocus() OVERRIDE; | |
| 68 virtual void OnBlur() OVERRIDE; | |
| 69 virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE; | |
| 70 | |
| 71 // ui::TouchEditable overrides: | |
| 72 virtual void SelectRect(const gfx::Point& start, | |
| 73 const gfx::Point& end) OVERRIDE; | |
| 74 virtual void MoveCaretTo(const gfx::Point& point) OVERRIDE; | |
| 75 virtual void GetSelectionEndPoints(gfx::Rect* p1, gfx::Rect* p2) OVERRIDE; | |
| 76 virtual gfx::Rect GetBounds() OVERRIDE; | |
| 77 virtual gfx::NativeView GetNativeView() OVERRIDE; | |
| 78 virtual void ConvertPointToScreen(gfx::Point* point) OVERRIDE; | |
| 79 virtual void ConvertPointFromScreen(gfx::Point* point) OVERRIDE; | |
| 80 virtual bool DrawsHandles() OVERRIDE; | |
| 81 virtual void OpenContextMenu(const gfx::Point& anchor) OVERRIDE; | |
| 82 | |
| 83 // ContextMenuController overrides: | |
| 84 virtual void ShowContextMenuForView(View* source, | |
| 85 const gfx::Point& point, | |
| 86 ui::MenuSourceType source_type) OVERRIDE; | |
| 87 | |
| 88 // Overridden from DragController: | |
| 89 virtual void WriteDragDataForView(View* sender, | |
| 90 const gfx::Point& press_pt, | |
| 91 ui::OSExchangeData* data) OVERRIDE; | |
| 92 virtual int GetDragOperationsForView(View* sender, | |
| 93 const gfx::Point& p) OVERRIDE; | |
| 94 virtual bool CanStartDragForView(View* sender, | |
| 95 const gfx::Point& press_pt, | |
| 96 const gfx::Point& p) OVERRIDE; | |
| 97 | |
| 98 // Gets the currently displayed text. | |
| 99 base::string16 GetText() const; | |
| 100 | |
| 101 // Updates the text displayed to the text held by the parent Textfield. | |
| 102 void UpdateText(); | |
| 103 | |
| 104 // Adds the specified text to the text already displayed. | |
| 105 void AppendText(const base::string16& text); | |
| 106 | |
| 107 // Inserts |text| at the current cursor position, replacing any selected text. | |
| 108 void InsertOrReplaceText(const base::string16& text); | |
| 109 | |
| 110 // Returns the text direction. | |
| 111 base::i18n::TextDirection GetTextDirection() const; | |
| 112 | |
| 113 // Returns the currently selected text. | |
| 114 base::string16 GetSelectedText() const; | |
| 115 | |
| 116 // Select the entire text range. If |reversed| is true, the range will end at | |
| 117 // the logical beginning of the text; this generally shows the leading portion | |
| 118 // of text that overflows its display area. | |
| 119 void SelectAll(bool reversed); | |
| 120 | |
| 121 // Clears the selection within the textfield and sets the caret to the end. | |
| 122 void ClearSelection(); | |
| 123 | |
| 124 // Updates whether there is a visible border. | |
| 125 void UpdateBorder(); | |
| 126 | |
| 127 // Updates the painted text color. | |
| 128 void UpdateTextColor(); | |
| 129 | |
| 130 // Updates the painted background color. | |
| 131 void UpdateBackgroundColor(); | |
| 132 | |
| 133 // Updates the read-only state. | |
| 134 void UpdateReadOnly(); | |
| 135 | |
| 136 // Updates the font used to render the text. | |
| 137 void UpdateFont(); | |
| 138 | |
| 139 // Updates the obscured state of the text for passwords, etc. | |
| 140 void UpdateIsObscured(); | |
| 141 | |
| 142 // Updates the enabled state. | |
| 143 void UpdateEnabled(); | |
| 144 | |
| 145 // Updates the horizontal and vertical margins. | |
| 146 void UpdateHorizontalMargins(); | |
| 147 void UpdateVerticalMargins(); | |
| 148 | |
| 149 // Returns whether or not an IME is composing text. | |
| 150 bool IsIMEComposing() const; | |
| 151 | |
| 152 // Gets the selected logical text range. | |
| 153 const gfx::Range& GetSelectedRange() const; | |
| 154 | |
| 155 // Selects the specified logical text range. | |
| 156 void SelectRange(const gfx::Range& range); | |
| 157 | |
| 158 // Gets the text selection model. | |
| 159 const gfx::SelectionModel& GetSelectionModel() const; | |
| 160 | |
| 161 // Sets the specified text selection model. | |
| 162 void SelectSelectionModel(const gfx::SelectionModel& sel); | |
| 163 | |
| 164 // Returns the current cursor position. | |
| 165 size_t GetCursorPosition() const; | |
| 166 | |
| 167 // Get or set whether or not the cursor is enabled. | |
| 168 bool GetCursorEnabled() const; | |
| 169 void SetCursorEnabled(bool enabled); | |
| 170 | |
| 171 // Invoked when the parent views::Textfield receives key events. | |
| 172 // returns true if the event was processed. | |
| 173 bool HandleKeyPressed(const ui::KeyEvent& e); | |
| 174 bool HandleKeyReleased(const ui::KeyEvent& e); | |
| 175 | |
| 176 // Invoked when the parent views:Textfield gains or loses focus. | |
| 177 void HandleFocus(); | |
| 178 void HandleBlur(); | |
| 179 | |
| 180 // Set the text colors; see views::Textfield for details. | |
| 181 void SetColor(SkColor value); | |
| 182 void ApplyColor(SkColor value, const gfx::Range& range); | |
| 183 | |
| 184 // Set the text styles; see the corresponding Textfield functions for details. | |
| 185 void SetStyle(gfx::TextStyle style, bool value); | |
| 186 void ApplyStyle(gfx::TextStyle style, bool value, const gfx::Range& range); | |
| 187 | |
| 188 // Clears the Edit history. | |
| 189 void ClearEditHistory(); | |
| 190 | |
| 191 // Get the height in pixels of the fonts used. | |
| 192 int GetFontHeight(); | |
| 193 | |
| 194 // Returns the text baseline; this value does not include any insets. | |
| 195 int GetTextfieldBaseline() const; | |
| 196 | |
| 197 // Returns the width necessary to display the current text, including any | |
| 198 // necessary space for the cursor or border/margin. | |
| 199 int GetWidthNeededForText() const; | |
| 200 | |
| 201 // Returns whether this view is the origin of an ongoing drag operation. | |
| 202 bool HasTextBeingDragged(); | |
| 203 | |
| 204 // Returns the location for keyboard-triggered context menus. | |
| 205 gfx::Point GetContextMenuLocation(); | |
| 206 | |
| 207 // ui::SimpleMenuModel::Delegate overrides | |
| 208 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; | |
| 209 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; | |
| 210 virtual bool GetAcceleratorForCommandId( | |
| 211 int command_id, | |
| 212 ui::Accelerator* accelerator) OVERRIDE; | |
| 213 virtual bool IsItemForCommandIdDynamic(int command_id) const OVERRIDE; | |
| 214 virtual base::string16 GetLabelForCommandId(int command_id) const OVERRIDE; | |
| 215 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE; | |
| 216 | |
| 217 // class name of internal | |
| 218 static const char kViewClassName[]; | |
| 219 | |
| 220 protected: | |
| 221 // View override. | |
| 222 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE; | |
| 223 | |
| 224 private: | |
| 225 friend class NativeTextfieldViewsTest; | |
| 226 friend class TouchSelectionControllerImplTest; | |
| 227 | |
| 228 // Overridden from ui::TextInputClient: | |
| 229 virtual void SetCompositionText( | |
| 230 const ui::CompositionText& composition) OVERRIDE; | |
| 231 virtual void ConfirmCompositionText() OVERRIDE; | |
| 232 virtual void ClearCompositionText() OVERRIDE; | |
| 233 virtual void InsertText(const base::string16& text) OVERRIDE; | |
| 234 virtual void InsertChar(base::char16 ch, int flags) OVERRIDE; | |
| 235 virtual gfx::NativeWindow GetAttachedWindow() const OVERRIDE; | |
| 236 virtual ui::TextInputType GetTextInputType() const OVERRIDE; | |
| 237 virtual ui::TextInputMode GetTextInputMode() const OVERRIDE; | |
| 238 virtual bool CanComposeInline() const OVERRIDE; | |
| 239 virtual gfx::Rect GetCaretBounds() const OVERRIDE; | |
| 240 virtual bool GetCompositionCharacterBounds(uint32 index, | |
| 241 gfx::Rect* rect) const OVERRIDE; | |
| 242 virtual bool HasCompositionText() const OVERRIDE; | |
| 243 virtual bool GetTextRange(gfx::Range* range) const OVERRIDE; | |
| 244 virtual bool GetCompositionTextRange(gfx::Range* range) const OVERRIDE; | |
| 245 virtual bool GetSelectionRange(gfx::Range* range) const OVERRIDE; | |
| 246 virtual bool SetSelectionRange(const gfx::Range& range) OVERRIDE; | |
| 247 virtual bool DeleteRange(const gfx::Range& range) OVERRIDE; | |
| 248 virtual bool GetTextFromRange(const gfx::Range& range, | |
| 249 base::string16* text) const OVERRIDE; | |
| 250 virtual void OnInputMethodChanged() OVERRIDE; | |
| 251 virtual bool ChangeTextDirectionAndLayoutAlignment( | |
| 252 base::i18n::TextDirection direction) OVERRIDE; | |
| 253 virtual void ExtendSelectionAndDelete(size_t before, size_t after) OVERRIDE; | |
| 254 virtual void EnsureCaretInRect(const gfx::Rect& rect) OVERRIDE; | |
| 255 virtual void OnCandidateWindowShown() OVERRIDE; | |
| 256 virtual void OnCandidateWindowUpdated() OVERRIDE; | |
| 257 virtual void OnCandidateWindowHidden() OVERRIDE; | |
| 258 | |
| 259 // Overridden from TextfieldViewsModel::Delegate: | |
| 260 virtual void OnCompositionTextConfirmedOrCleared() OVERRIDE; | |
| 261 | |
| 262 // Returns the TextfieldViewsModel's text/cursor/selection rendering model. | |
| 263 gfx::RenderText* GetRenderText() const; | |
| 264 | |
| 265 // Converts |text| according to the current style, e.g. STYLE_LOWERCASE. | |
| 266 base::string16 GetTextForDisplay(const base::string16& text); | |
| 267 | |
| 268 // Updates any colors that have not been explicitly set from the theme. | |
| 269 void UpdateColorsFromTheme(const ui::NativeTheme* theme); | |
| 270 | |
| 271 // A callback function to periodically update the cursor state. | |
| 272 void UpdateCursor(); | |
| 273 | |
| 274 // Repaint the cursor. | |
| 275 void RepaintCursor(); | |
| 276 | |
| 277 // Update the cursor_bounds and text_offset. | |
| 278 void UpdateCursorBoundsAndTextOffset(size_t cursor_pos, bool insert_mode); | |
| 279 | |
| 280 void PaintTextAndCursor(gfx::Canvas* canvas); | |
| 281 | |
| 282 // Handle the keyevent. | |
| 283 bool HandleKeyEvent(const ui::KeyEvent& key_event); | |
| 284 | |
| 285 // Helper function to call MoveCursorTo on the TextfieldViewsModel. | |
| 286 bool MoveCursorTo(const gfx::Point& point, bool select); | |
| 287 | |
| 288 // Utility function to inform the parent views::Textfield (and any controller) | |
| 289 // that the text in the textfield has changed. | |
| 290 void PropagateTextChange(); | |
| 291 | |
| 292 // Does necessary updates when the text and/or cursor position changes. | |
| 293 void UpdateAfterChange(bool text_changed, bool cursor_changed); | |
| 294 | |
| 295 // Utility function to prepare the context menu. | |
| 296 void UpdateContextMenu(); | |
| 297 | |
| 298 // Convenience method to call InputMethod::OnTextInputTypeChanged(); | |
| 299 void OnTextInputTypeChanged(); | |
| 300 | |
| 301 // Convenience method to call InputMethod::OnCaretBoundsChanged(); | |
| 302 void OnCaretBoundsChanged(); | |
| 303 | |
| 304 // Convenience method to call TextfieldController::OnBeforeUserAction(); | |
| 305 void OnBeforeUserAction(); | |
| 306 | |
| 307 // Convenience method to call TextfieldController::OnAfterUserAction(); | |
| 308 void OnAfterUserAction(); | |
| 309 | |
| 310 // Calls |model_->Cut()| and notifies TextfieldController on success. | |
| 311 bool Cut(); | |
| 312 | |
| 313 // Calls |model_->Copy()| and notifies TextfieldController on success. | |
| 314 bool Copy(); | |
| 315 | |
| 316 // Calls |model_->Paste()| and calls TextfieldController::ContentsChanged() | |
| 317 // explicitly if paste succeeded. | |
| 318 bool Paste(); | |
| 319 | |
| 320 // Tracks the mouse clicks for single/double/triple clicks. | |
| 321 void TrackMouseClicks(const ui::MouseEvent& event); | |
| 322 | |
| 323 // Handles mouse press events. | |
| 324 void HandleMousePressEvent(const ui::MouseEvent& event); | |
| 325 | |
| 326 // Returns true if the current text input type allows access by the IME. | |
| 327 bool ImeEditingAllowed() const; | |
| 328 | |
| 329 // Returns true if distance between |event| and |last_click_location_| | |
| 330 // exceeds the drag threshold. | |
| 331 bool ExceededDragThresholdFromLastClickLocation(const ui::MouseEvent& event); | |
| 332 | |
| 333 // Checks if a char is ok to be inserted into the textfield. The |ch| is a | |
| 334 // modified character, i.e., modifiers took effect when generating this char. | |
| 335 static bool ShouldInsertChar(base::char16 ch, int flags); | |
| 336 | |
| 337 void CreateTouchSelectionControllerAndNotifyIt(); | |
| 338 | |
| 339 // Platform specific gesture event handling. | |
| 340 void PlatformGestureEventHandling(const ui::GestureEvent* event); | |
| 341 | |
| 342 // Reveals the obscured char at |index| for the given |duration|. If |index| | |
| 343 // is -1, existing revealed index will be cleared. | |
| 344 void RevealObscuredChar(int index, const base::TimeDelta& duration); | |
| 345 | |
| 346 // The parent views::Textfield, the owner of this object. | |
| 347 Textfield* textfield_; | |
| 348 | |
| 349 // The text model. | |
| 350 scoped_ptr<TextfieldViewsModel> model_; | |
| 351 | |
| 352 // The focusable border. This is always non-NULL, but may not actually be | |
| 353 // drawn. If it is not drawn, then by default it's also zero-sized unless the | |
| 354 // Textfield has explicitly-set margins. | |
| 355 FocusableBorder* text_border_; | |
| 356 | |
| 357 // The text editing cursor visibility. | |
| 358 bool is_cursor_visible_; | |
| 359 | |
| 360 // The drop cursor is a visual cue for where dragged text will be dropped. | |
| 361 bool is_drop_cursor_visible_; | |
| 362 // Position of the drop cursor, if it is visible. | |
| 363 gfx::SelectionModel drop_cursor_position_; | |
| 364 | |
| 365 // True if InputMethod::CancelComposition() should not be called. | |
| 366 bool skip_input_method_cancel_composition_; | |
| 367 | |
| 368 // Is the user potentially dragging and dropping from this view? | |
| 369 bool initiating_drag_; | |
| 370 | |
| 371 // A runnable method factory for callback to update the cursor. | |
| 372 base::WeakPtrFactory<NativeTextfieldViews> cursor_timer_; | |
| 373 | |
| 374 // State variables used to track double and triple clicks. | |
| 375 size_t aggregated_clicks_; | |
| 376 base::TimeDelta last_click_time_; | |
| 377 gfx::Point last_click_location_; | |
| 378 gfx::Range double_click_word_; | |
| 379 | |
| 380 // Context menu related members. | |
| 381 scoped_ptr<ui::SimpleMenuModel> context_menu_contents_; | |
| 382 scoped_ptr<views::MenuModelAdapter> context_menu_delegate_; | |
| 383 scoped_ptr<views::MenuRunner> context_menu_runner_; | |
| 384 | |
| 385 scoped_ptr<ui::TouchSelectionController> touch_selection_controller_; | |
| 386 | |
| 387 // A timer to control the duration of showing the last typed char in | |
| 388 // obscured text. When the timer is running, the last typed char is shown | |
| 389 // and when the time expires, the last typed char is obscured. | |
| 390 base::OneShotTimer<NativeTextfieldViews> obscured_reveal_timer_; | |
| 391 | |
| 392 DISALLOW_COPY_AND_ASSIGN(NativeTextfieldViews); | |
| 393 }; | |
| 394 | |
| 395 } // namespace views | |
| 396 | |
| 397 #endif // UI_VIEWS_CONTROLS_TEXTFIELD_NATIVE_TEXTFIELD_VIEWS_H_ | |
| OLD | NEW |