summaryrefslogtreecommitdiffstats
path: root/views/controls/textfield/textfield_views_model.h
blob: 21cbc2c295f845765ee451e1806566ed8f91dbf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_
#define VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_
#pragma once

#include <list>
#include <vector>

#include "base/gtest_prod_util.h"
#include "base/string16.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/ime/composition_text.h"
#include "ui/gfx/rect.h"

namespace gfx {
class Font;
}  // namespace gfx

namespace ui {
class Range;
}  // namespace ui

namespace views {

class TextStyle;
typedef std::vector<TextStyle*> TextStyles;

namespace internal {
// Internal Edit class that keeps track of edits for undo/redo.
class Edit;

struct TextStyleRange;

// C++ doesn't allow forward decl enum, so let's define here.
enum MergeType {
  // The edit should not be merged with next edit. It still may
  // be merged with an edit with MERGE_WITH_PREVIOUS.
  DO_NOT_MERGE,
  // The edit can be merged with next edit when possible.
  MERGEABLE,
  // Does the edit have to be merged with previous edit?
  // This forces the merge even if the previous edit is marked
  // as DO_NOT_MERGE.
  MERGE_WITH_PREVIOUS,
};

}  // namespace internal

typedef std::vector<internal::TextStyleRange*> TextStyleRanges;

// A model that represents a text content for TextfieldViews.
// It supports editing, selection and cursor manipulation.
class TextfieldViewsModel {
 public:

  // Delegate interface implemented by the textfield view class to provided
  // additional functionalities required by the model.
  class Delegate {
   public:
    // Called when the current composition text is confirmed or cleared.
    virtual void OnCompositionTextConfirmedOrCleared() = 0;

   protected:
    virtual ~Delegate();
  };

  explicit TextfieldViewsModel(Delegate* delegate);
  virtual ~TextfieldViewsModel();

  // Text fragment info. Used to draw selected text.
  // We may replace this with TextAttribute class
  // in the future to support multi-color text
  // for omnibox.
  struct TextFragment {
    TextFragment(size_t start, size_t end, const views::TextStyle* s)
        : range(start, end), style(s) {
    }
    // The start and end position of text fragment.
    ui::Range range;
    const TextStyle* style;
  };
  typedef std::vector<TextFragment> TextFragments;

  // Gets the text element info.
  void GetFragments(TextFragments* elements);

  void set_is_password(bool is_password) {
    is_password_ = is_password;
  }
  const string16& text() const { return text_; }

  // Edit related methods.

  // Sest the text. Returns true if the text has been modified.  The
  // current composition text will be confirmed first.  Setting
  // the same text will not add edit history because it's not user
  // visible change nor user-initiated change. This allow a client
  // code to set the same text multiple times without worrying about
  // messing edit history.
  bool SetText(const string16& text);

  // Inserts given |text| at the current cursor position.
  // The current composition text will be cleared.
  void InsertText(const string16& text) {
    InsertTextInternal(text, false);
  }

  // Inserts a character at the current cursor position.
  void InsertChar(char16 c) {
    InsertTextInternal(string16(&c, 1), true);
  }

  // Replaces characters at the current position with characters in given text.
  // The current composition text will be cleared.
  void ReplaceText(const string16& text) {
    ReplaceTextInternal(text, false);
  }

  // Replaces the char at the current position with given character.
  void ReplaceChar(char16 c) {
    ReplaceTextInternal(string16(&c, 1), true);
  }

  // Appends the text.
  // The current composition text will be confirmed.
  void Append(const string16& text);

  // Deletes the first character after the current cursor position (as if, the
  // the user has pressed delete key in the textfield). Returns true if
  // the deletion is successful.
  // If there is composition text, it'll be deleted instead.
  bool Delete();

  // Deletes the first character before the current cursor position (as if, the
  // the user has pressed backspace key in the textfield). Returns true if
  // the removal is successful.
  // If there is composition text, it'll be deleted instead.
  bool Backspace();

  // Cursor related methods.

  // Returns the current cursor position.
  size_t cursor_pos() const { return cursor_pos_; }

  // Moves the cursor left by one position (as if, the user has pressed the left
  // arrow key). If |select| is true, it updates the selection accordingly.
  // The current composition text will be confirmed.
  void MoveCursorLeft(bool select);

  // Moves the cursor right by one position (as if, the user has pressed the
  // right arrow key). If |select| is true, it updates the selection
  // accordingly.
  // The current composition text will be confirmed.
  void MoveCursorRight(bool select);

  // Moves the cursor left by one word (word boundry is defined by space).
  // If |select| is true, it updates the selection accordingly.
  // The current composition text will be confirmed.
  void MoveCursorToPreviousWord(bool select);

  // Moves the cursor right by one word (word boundry is defined by space).
  // If |select| is true, it updates the selection accordingly.
  // The current composition text will be confirmed.
  void MoveCursorToNextWord(bool select);

  // Moves the cursor to start of the textfield contents.
  // If |select| is true, it updates the selection accordingly.
  // The current composition text will be confirmed.
  void MoveCursorToHome(bool select);

  // Moves the cursor to end of the textfield contents.
  // If |select| is true, it updates the selection accordingly.
  // The current composition text will be confirmed.
  void MoveCursorToEnd(bool select);

  // Moves the cursor to the specified |position|.
  // If |select| is true, it updates the selection accordingly.
  // The current composition text will be confirmed.
  bool MoveCursorTo(size_t position, bool select);

  // Returns the bounds of character at the current cursor.
  gfx::Rect GetCursorBounds(const gfx::Font& font) const;

  // Returns the bounds of selected text.
  gfx::Rect GetSelectionBounds(const gfx::Font& font) const;

  // Selection related method

  // Returns the selected text.
  string16 GetSelectedText() const;

  void GetSelectedRange(ui::Range* range) const;

  // The current composition text will be confirmed. The
  // selection starts with the range's start position,
  // and ends with the range's end position, therefore
  // the cursor position becomes the end position.
  void SelectRange(const ui::Range& range);

  // Selects all text.
  // The current composition text will be confirmed.
  void SelectAll();

  // Selects the word at which the cursor is currently positioned.
  // The current composition text will be confirmed.
  void SelectWord();

  // Clears selection.
  // The current composition text will be confirmed.
  void ClearSelection();

  // Returns true if there is an undoable edit.
  bool CanUndo();

  // Returns true if there is an redoable edit.
  bool CanRedo();

  // Undo edit. Returns true if undo changed the text.
  bool Undo();

  // Redo edit. Returns true if redo changed the text.
  bool Redo();

  // Returns visible text. If the field is password, it returns the
  // sequence of "*".
  string16 GetVisibleText() const {
    return GetVisibleText(0U, text_.length());
  }

  // Cuts the currently selected text and puts it to clipboard. Returns true
  // if text has changed after cutting.
  bool Cut();

  // Copies the currently selected text and puts it to clipboard.
  void Copy();

  // Pastes text from the clipboard at current cursor position. Returns true
  // if text has changed after pasting.
  bool Paste();

  // Tells if any text is selected, even if the selection is in composition
  // text.
  bool HasSelection() const;

  // Deletes the selected text. This method shouldn't be called with
  // composition text.
  void DeleteSelection();

  // Deletes the selected text (if any) and insert text at given
  // position.
  void DeleteSelectionAndInsertTextAt(
      const string16& text, size_t position);

  // Retrieves the text content in a given range.
  string16 GetTextFromRange(const ui::Range& range) const;

  // Retrieves the range containing all text in the model.
  void GetTextRange(ui::Range* range) const;

  // Sets composition text and attributes. If there is composition text already,
  // it’ll be replaced by the new one. Otherwise, current selection will be
  // replaced. If there is no selection, the composition text will be inserted
  // at the insertion point.
  // Any changes to the model except text insertion will confirm the current
  // composition text.
  void SetCompositionText(const ui::CompositionText& composition);

  // Converts current composition text into final content.
  void ConfirmCompositionText();

  // Removes current composition text.
  void CancelCompositionText();

  // Retrieves the range of current composition text.
  void GetCompositionTextRange(ui::Range* range) const;

  // Returns true if there is composition text.
  bool HasCompositionText() const;

  TextStyle* CreateTextStyle();

  void ClearAllTextStyles();

 private:
  friend class NativeTextfieldViews;
  friend class NativeTextfieldViewsTest;
  friend class TextfieldViewsModelTest;
  friend class TextStyle;
  friend class UndoRedo_BasicTest;
  friend class UndoRedo_CutCopyPasteTest;
  friend class UndoRedo_ReplaceTest;
  friend class internal::Edit;

  FRIEND_TEST_ALL_PREFIXES(TextfieldViewsModelTest, UndoRedo_BasicTest);
  FRIEND_TEST_ALL_PREFIXES(TextfieldViewsModelTest, UndoRedo_CutCopyPasteTest);
  FRIEND_TEST_ALL_PREFIXES(TextfieldViewsModelTest, UndoRedo_ReplaceTest);
  FRIEND_TEST_ALL_PREFIXES(TextfieldViewsModelTest, TextStyleTest);

  // Returns the visible text given |start| and |end|.
  string16 GetVisibleText(size_t start, size_t end) const;

  // Utility for SelectWord(). Checks whether position pos is at word boundary.
  bool IsPositionAtWordSelectionBoundary(size_t pos);

  // Returns the normalized cursor position that does not exceed the
  // text length.
  size_t GetSafePosition(size_t position) const;

  // Insert the given |text|. |mergeable| indicates if this insert
  // operation can be merged to previous edit in the edit history.
  void InsertTextInternal(const string16& text, bool mergeable);

  // Replace the current text with the given |text|. |mergeable|
  // indicates if this replace operation can be merged to previous
  // edit in the edit history.
  void ReplaceTextInternal(const string16& text, bool mergeable);

  // Clears all edit history.
  void ClearEditHistory();

  // Clears redo history.
  void ClearRedoHistory();

  // Executes and records edit operations.
  void ExecuteAndRecordDelete(size_t from, size_t to, bool mergeable);
  void ExecuteAndRecordReplaceSelection(internal::MergeType merge_type,
                                        const string16& text);
  void ExecuteAndRecordReplace(internal::MergeType merge_type,
                               size_t old_cursor_pos,
                               size_t new_cursor_pos,
                               const string16& text,
                               size_t new_text_start);
  void ExecuteAndRecordInsert(const string16& text, bool mergeable);

  // Adds or merge |edit| into edit history. Return true if the edit
  // has been merged and must be deleted after redo.
  bool AddOrMergeEditHistory(internal::Edit* edit);

  // Modify the text buffer in following way:
  // 1) Delete the string from |delete_from| to |delte_to|.
  // 2) Insert the |new_text| at the index |new_text_insert_at|.
  //    Note that the index is after deletion.
  // 3) Move the cursor to |new_cursor_pos|.
  void ModifyText(size_t delete_from,
                  size_t delete_to,
                  const string16& new_text,
                  size_t new_text_insert_at,
                  size_t new_cursor_pos);

  void ClearComposition();

  void ApplyTextStyle(const TextStyle* style, const ui::Range& range);

  static TextStyle* CreateUnderlineStyle();

  // Pointer to a TextfieldViewsModel::Delegate instance, should be provided by
  // the View object.
  Delegate* delegate_;

  // The text in utf16 format.
  string16 text_;

  // Current cursor position.
  size_t cursor_pos_;

  // Selection range.
  size_t selection_start_;

  // Composition text range.
  size_t composition_start_;
  size_t composition_end_;

  // True if the text is the password.
  bool is_password_;

  typedef std::list<internal::Edit*> EditHistory;
  EditHistory edit_history_;

  // An iterator that points to the current edit that can be undone.
  // This iterator moves from the |end()|, meaining no edit to undo,
  // to the last element (one before |end()|), meaning no edit to redo.
  //  There is no edit to undo (== end()) when:
  // 1) in initial state. (nothing to undo)
  // 2) very 1st edit is undone.
  // 3) all edit history is removed.
  //  There is no edit to redo (== last element or no element) when:
  // 1) in initial state. (nothing to redo)
  // 2) new edit is added. (redo history is cleared)
  // 3) redone all undone edits.
  EditHistory::iterator current_edit_;

  // This manages all styles objects.
  TextStyles text_styles_;

  // List of style ranges. Elements in the list never overlap each other.
  // Elements are not sorted at the time of insertion, and gets sorted
  // when it's painted (if necessary).
  TextStyleRanges style_ranges_;
  // True if the style_ranges_ needs to be sorted.
  bool sort_style_ranges_;

  // List of style ranges for composition text.
  TextStyleRanges composition_style_ranges_;

  DISALLOW_COPY_AND_ASSIGN(TextfieldViewsModel);
};

}  // namespace views

#endif  // VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_