OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_ |
| 6 #define VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <vector> |
| 10 |
| 11 #include "base/string16.h" |
| 12 #include "gfx/rect.h" |
| 13 #include "third_party/skia/include/core/SkColor.h" |
| 14 |
| 15 namespace gfx { |
| 16 class Font; |
| 17 } // namespace gfx |
| 18 |
| 19 namespace views { |
| 20 |
| 21 // A model that represents a text content for TextfieldViews. |
| 22 // It supports editing, selection and cursor manipulation. |
| 23 class TextfieldViewsModel { |
| 24 public: |
| 25 TextfieldViewsModel(); |
| 26 virtual ~TextfieldViewsModel(); |
| 27 |
| 28 // Text fragment info. Used to draw selected text. |
| 29 // We may replace this with TextAttribute class |
| 30 // in the future to support multi-color text |
| 31 // for omnibox. |
| 32 struct TextFragment { |
| 33 TextFragment(size_t b, size_t e, bool s) |
| 34 : begin(b), end(e), selected(s) { |
| 35 } |
| 36 // The begin and end position of text fragment. |
| 37 size_t begin, end; |
| 38 // True if the text is selected. |
| 39 bool selected; |
| 40 }; |
| 41 typedef std::vector<TextFragment> TextFragments; |
| 42 |
| 43 // Gets the text element info. |
| 44 void GetFragments(TextFragments* elements) const; |
| 45 |
| 46 void set_is_password(bool is_password) { |
| 47 is_password_ = is_password; |
| 48 } |
| 49 const string16& text() const { return text_; } |
| 50 |
| 51 // Edit related methods. |
| 52 |
| 53 // Sest the text. Returns true if the text has been modified. |
| 54 bool SetText(const string16& text); |
| 55 |
| 56 // Inserts a character at the current cursor position. |
| 57 void Insert(char16 c); |
| 58 |
| 59 // Replaces the char at the current position with given character. |
| 60 void Replace(char16 c); |
| 61 |
| 62 // Appends the text. |
| 63 void Append(const string16& text); |
| 64 |
| 65 // Deletes the first character after the current cursor position (as if, the |
| 66 // the user has pressed delete key in the textfield). Returns true if |
| 67 // the deletion is successful. |
| 68 bool Delete(); |
| 69 |
| 70 // Deletes the first character before the current cursor position (as if, the |
| 71 // the user has pressed backspace key in the textfield). Returns true if |
| 72 // the removal is successful. |
| 73 bool Backspace(); |
| 74 |
| 75 // Cursor related methods. |
| 76 |
| 77 // Returns the current cursor position. |
| 78 size_t cursor_pos() const { return cursor_pos_; } |
| 79 |
| 80 // Moves the cursor left by one position (as if, the user has pressed the left |
| 81 // arrow key). If |select| is true, it updates the selection accordingly. |
| 82 void MoveCursorLeft(bool select); |
| 83 |
| 84 // Moves the cursor right by one position (as if, the user has pressed the |
| 85 // right arrow key). If |select| is true, it updates the selection |
| 86 // accordingly. |
| 87 void MoveCursorRight(bool select); |
| 88 |
| 89 // Moves the cursor left by one word (word boundry is defined by space). |
| 90 // If |select| is true, it updates the selection accordingly. |
| 91 void MoveCursorToPreviousWord(bool select); |
| 92 |
| 93 // Moves the cursor right by one word (word boundry is defined by space). |
| 94 // If |select| is true, it updates the selection accordingly. |
| 95 void MoveCursorToNextWord(bool select); |
| 96 |
| 97 // Moves the cursor to start of the textfield contents. |
| 98 // If |select| is true, it updates the selection accordingly. |
| 99 void MoveCursorToStart(bool select); |
| 100 |
| 101 // Moves the cursor to end of the textfield contents. |
| 102 // If |select| is true, it updates the selection accordingly. |
| 103 void MoveCursorToEnd(bool select); |
| 104 |
| 105 // Moves the cursor to the specified |position|. |
| 106 // If |select| is true, it updates the selection accordingly. |
| 107 bool MoveCursorTo(size_t position, bool select); |
| 108 |
| 109 // Returns the bounds of character at the current cursor. |
| 110 gfx::Rect GetCursorBounds(const gfx::Font& font) const; |
| 111 |
| 112 // Selection related method |
| 113 |
| 114 // Returns the selected text. |
| 115 string16 GetSelectedText() const; |
| 116 |
| 117 // Selects all text. |
| 118 void SelectAll(); |
| 119 |
| 120 // Clears selection. |
| 121 void ClearSelection(); |
| 122 |
| 123 // Returns visible text. If the field is password, it returns the |
| 124 // sequence of "*". |
| 125 string16 GetVisibleText() const { |
| 126 return GetVisibleText(0U, text_.length()); |
| 127 } |
| 128 |
| 129 private: |
| 130 friend class NativeTextfieldViews; |
| 131 |
| 132 // Tells if any text is selected. |
| 133 bool HasSelection() const; |
| 134 |
| 135 // Deletes the selected text. |
| 136 void DeleteSelection(); |
| 137 |
| 138 // Returns the visible text given |start| and |end|. |
| 139 string16 GetVisibleText(size_t start, size_t end) const; |
| 140 |
| 141 // The text in utf16 format. |
| 142 string16 text_; |
| 143 |
| 144 // Current cursor position. |
| 145 size_t cursor_pos_; |
| 146 |
| 147 // Selection range. |
| 148 size_t selection_begin_; |
| 149 |
| 150 // True if the text is the password. |
| 151 bool is_password_; |
| 152 |
| 153 DISALLOW_COPY_AND_ASSIGN(TextfieldViewsModel); |
| 154 }; |
| 155 |
| 156 } // namespace views |
| 157 |
| 158 #endif // VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_ |
OLD | NEW |