Chromium Code Reviews| Index: views/controls/textfield/textfield_view_model.h |
| diff --git a/views/controls/textfield/textfield_view_model.h b/views/controls/textfield/textfield_view_model.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4113619ffdfd14303c956896ea9695bb27f87855 |
| --- /dev/null |
| +++ b/views/controls/textfield/textfield_view_model.h |
| @@ -0,0 +1,160 @@ |
| +// Copyright (c) 2010 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_VIEW_MODEL_H_ |
| +#define VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEW_MODEL_H_ |
| +#pragma once |
| + |
| +#include <vector> |
| + |
| +#include "base/string16.h" |
| +#include "gfx/rect.h" |
| +#include "third_party/skia/include/core/SkColor.h" |
| + |
| +namespace gfx { |
| +class Font; |
| +} // namespace gfx |
| + |
| +namespace views { |
| + |
| +class TextfieldViewModel { |
|
sky
2010/12/15 20:31:27
Description?
oshima
2010/12/16 01:15:19
Done.
|
| + public: |
| + TextfieldViewModel(); |
| + virtual ~TextfieldViewModel(); |
|
sky
2010/12/15 20:31:27
Why virtual?
oshima
2010/12/16 01:15:19
it's just my habit to always use virtual destructo
|
| + |
| + // Text fragment info. Used to draw selected text. |
| + // We may replace this with TextAttribute class |
| + // in the future to supportmulti-color text |
|
sky
2010/12/15 20:31:27
support multi
oshima
2010/12/16 01:15:19
Done.
|
| + // for omnibox. |
| + struct TextFragment { |
| + TextFragment(size_t b, size_t e, bool s) |
| + : begin(b), end(e), selected(s) { |
| + } |
| + // The begin and end position of text fragment. |
| + size_t begin, end; |
| + // True if the text is selected. |
| + bool selected; |
| + }; |
| + typedef std::vector<TextFragment> TextFragments; |
| + |
| + // Populate the text element info. |
| + void PopulateFragments(TextFragments* elements) const; |
|
sky
2010/12/15 20:31:27
How about GetFragments.
oshima
2010/12/16 01:15:19
Done.
|
| + |
| + void set_is_password(bool is_password) { |
| + is_password_ = is_password; |
| + } |
| + const string16& text() const { return text_; } |
| + |
| + // Edit related methods. |
| + |
| + // Set the text. Returns true if the text has been modified. |
| + bool SetText(const string16& text); |
| + |
| + // Inserts a character at the current cursor position. |
| + void Insert(char16 c); |
| + |
| + // Replaces the char at the current position with given character. |
| + void Replace(char16 c); |
| + |
| + // Appends the text. |
| + 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. |
| + 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. |
| + bool Backspace(); |
| + |
| + // Cursor related methods. |
| + |
| + // Returns the current cursor position. |
| + int cursor_pos() const { return cursor_pos_; } |
|
sky
2010/12/15 20:31:27
size_t
oshima
2010/12/16 01:15:19
good catch. Done
|
| + |
| + // 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. |
| + 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. |
| + 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. |
| + 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. |
| + void MoveCursorToNextWord(bool select); |
| + |
| + // Moves the cursor to start of the textfield contents. |
| + // If |select| is true, it updates the selection accordingly. |
| + void MoveCursorToStart(bool select); |
| + |
| + // Moves the cursor to end of the textfield contents. |
| + // If |select| is true, it updates the selection accordingly. |
| + void MoveCursorToEnd(bool select); |
| + |
| + // Moves the cursor to the specified |position|. |
| + // If |select| is true, it updates the selection accordingly. |
| + bool MoveCursorTo(size_t position, bool select); |
| + |
| + // Returns the bounds of character at the current cursor. |
| + gfx::Rect GetCursorBounds(const gfx::Font& font) const; |
| + |
| + // Selection related method |
| + |
| + // Returns the selected text. |
| + string16 GetSelectedText() const; |
|
sky
2010/12/15 20:31:27
Does this intentionally not return the password ch
oshima
2010/12/16 01:15:19
I believe this is now gtk version works and expect
|
| + |
| + // Selects all text. |
| + void SelectAll(); |
| + |
| + // Clears selection. |
| + void ClearSelection(); |
| + |
| + // Returns visible text. If the field is password, it returns the |
| + // sequence of "*". |
| + string16 GetVisibleText() const { |
| + return GetVisibleText(0U, text_.length()); |
| + } |
| + |
| + private: |
| + friend class NativeTextfieldView; |
| + |
| + // Selects the text. |old| is the old cursor position and |
| + // end is the new corsor position. |
|
sky
2010/12/15 20:31:27
corsor->cursor
oshima
2010/12/16 01:15:19
Done.
|
| + void Select(size_t old, size_t end); |
| + |
| + // Tells if any text is selected. |
| + bool HasSelection() const; |
| + |
| + // Deletes the selected text. |
| + void DeleteSelection(); |
| + |
| + // Returns the visible text given |start| and |end|. |
| + string16 GetVisibleText(size_t start, size_t end) const; |
| + |
| + // The text in utf16 format. |
| + string16 text_; |
| + |
| + // Current cursor position. |
| + size_t cursor_pos_; |
| + |
| + // Selection range. |
| + size_t begin_, end_; |
|
sky
2010/12/15 20:31:27
Could you combine cursor_pos_ and begin_ end_ ? Is
oshima
2010/12/16 01:15:19
Only if something is selected. There are several o
sky
2010/12/16 17:29:43
I don't understand this con. You have to update so
|
| + |
| + // True if the text is the password. |
| + bool is_password_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TextfieldViewModel); |
| +}; |
| + |
| +} // namespace views |
| + |
| +#endif // VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEW_MODEL_H_ |