| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_ | 5 #ifndef VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_ |
| 6 #define VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_ | 6 #define VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 | 10 |
| 11 #if defined(OS_LINUX) | 11 #if defined(OS_LINUX) |
| 12 #include <gdk/gdk.h> | 12 #include <gdk/gdk.h> |
| 13 #endif | 13 #endif |
| 14 | 14 |
| 15 #include <string> | 15 #include <string> |
| 16 | 16 |
| 17 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
| 18 #include "base/compiler_specific.h" |
| 18 #include "base/string16.h" | 19 #include "base/string16.h" |
| 19 #include "third_party/skia/include/core/SkColor.h" | 20 #include "third_party/skia/include/core/SkColor.h" |
| 20 #include "ui/base/keycodes/keyboard_codes.h" | 21 #include "ui/base/keycodes/keyboard_codes.h" |
| 21 #include "ui/gfx/font.h" | 22 #include "ui/gfx/font.h" |
| 22 #include "views/view.h" | 23 #include "views/view.h" |
| 23 | 24 |
| 24 #if !defined(OS_LINUX) | 25 #if !defined(OS_LINUX) |
| 25 #include "base/logging.h" | 26 #include "base/logging.h" |
| 26 #endif | 27 #endif |
| 27 #ifdef UNIT_TEST | 28 #ifdef UNIT_TEST |
| 28 #include "ui/gfx/native_widget_types.h" | 29 #include "ui/gfx/native_widget_types.h" |
| 29 #include "views/controls/textfield/native_textfield_wrapper.h" | 30 #include "views/controls/textfield/native_textfield_wrapper.h" |
| 30 #endif | 31 #endif |
| 31 | 32 |
| 32 namespace views { | 33 namespace views { |
| 33 | 34 |
| 34 class KeyEvent; | 35 class KeyEvent; |
| 35 class NativeTextfieldWrapper; | 36 class NativeTextfieldWrapper; |
| 37 class TextfieldController; |
| 38 class TextRange; |
| 36 | 39 |
| 37 // TextRange specifies the range of text in the Textfield. This is | 40 // This class implements a View that wraps a native text (edit) field. |
| 38 // used to specify selected text and will be used to change the | |
| 39 // attributes of characters in the textfield. When this is used for | |
| 40 // selection, the end is caret position, and the start is where | |
| 41 // selection started. The range preserves the direction, and | |
| 42 // selecting from the end to the begining is considered "reverse" | |
| 43 // order. (that is, start > end is reverse) | |
| 44 class TextRange { | |
| 45 public: | |
| 46 TextRange() : start_(0), end_(0) {} | |
| 47 TextRange(size_t start, size_t end); | |
| 48 | |
| 49 // Allow copy so that the omnibox can save the view state | |
| 50 // for each tabs. | |
| 51 explicit TextRange(const TextRange& range) | |
| 52 : start_(range.start_), end_(range.end_) {} | |
| 53 | |
| 54 // Returns the start position; | |
| 55 size_t start() const { return start_; } | |
| 56 | |
| 57 // Returns the end position. | |
| 58 size_t end() const { return end_; } | |
| 59 | |
| 60 // Returns true if the selected text is empty. | |
| 61 bool is_empty() const { return start_ == end_; } | |
| 62 | |
| 63 // Returns true if the selection is made in reverse order. | |
| 64 bool is_reverse() const { return start_ > end_; } | |
| 65 | |
| 66 // Returns the min of selected range. | |
| 67 size_t GetMin() const; | |
| 68 | |
| 69 // Returns the max of selected range. | |
| 70 size_t GetMax() const; | |
| 71 | |
| 72 // Returns true if the the selection range is same ignoring the direction. | |
| 73 bool EqualsIgnoringDirection(const TextRange& range) const { | |
| 74 return GetMin() == range.GetMin() && GetMax() == range.GetMax(); | |
| 75 } | |
| 76 | |
| 77 // Set the range with |start| and |end|. | |
| 78 void SetRange(size_t start, size_t end); | |
| 79 | |
| 80 private: | |
| 81 size_t start_; | |
| 82 size_t end_; | |
| 83 | |
| 84 // No assign. | |
| 85 void operator=(const TextRange&); | |
| 86 }; | |
| 87 | |
| 88 // This class implements a ChromeView that wraps a native text (edit) field. | |
| 89 class Textfield : public View { | 41 class Textfield : public View { |
| 90 public: | 42 public: |
| 91 // The button's class name. | 43 // The button's class name. |
| 92 static const char kViewClassName[]; | 44 static const char kViewClassName[]; |
| 93 | 45 |
| 94 // This defines the callback interface for other code to be notified of | |
| 95 // changes in the state of a text field. | |
| 96 class Controller { | |
| 97 public: | |
| 98 // This method is called whenever the text in the field changes. | |
| 99 virtual void ContentsChanged(Textfield* sender, | |
| 100 const string16& new_contents) = 0; | |
| 101 | |
| 102 // This method is called to get notified about keystrokes in the edit. | |
| 103 // This method returns true if the message was handled and should not be | |
| 104 // processed further. If it returns false the processing continues. | |
| 105 virtual bool HandleKeyEvent(Textfield* sender, | |
| 106 const KeyEvent& key_event) = 0; | |
| 107 }; | |
| 108 | |
| 109 enum StyleFlags { | 46 enum StyleFlags { |
| 110 STYLE_DEFAULT = 0, | 47 STYLE_DEFAULT = 0, |
| 111 STYLE_PASSWORD = 1<<0, | 48 STYLE_PASSWORD = 1 << 0, |
| 112 STYLE_MULTILINE = 1<<1, | 49 STYLE_MULTILINE = 1 << 1, |
| 113 STYLE_LOWERCASE = 1<<2 | 50 STYLE_LOWERCASE = 1 << 2 |
| 114 }; | 51 }; |
| 115 | 52 |
| 116 Textfield(); | 53 Textfield(); |
| 117 explicit Textfield(StyleFlags style); | 54 explicit Textfield(StyleFlags style); |
| 118 virtual ~Textfield(); | 55 virtual ~Textfield(); |
| 119 | 56 |
| 120 | 57 // TextfieldController accessors |
| 121 // Controller accessors | 58 void SetController(TextfieldController* controller); |
| 122 void SetController(Controller* controller); | 59 TextfieldController* GetController() const; |
| 123 Controller* GetController() const; | |
| 124 | 60 |
| 125 // Gets/Sets whether or not the Textfield is read-only. | 61 // Gets/Sets whether or not the Textfield is read-only. |
| 126 bool read_only() const { return read_only_; } | 62 bool read_only() const { return read_only_; } |
| 127 void SetReadOnly(bool read_only); | 63 void SetReadOnly(bool read_only); |
| 128 | 64 |
| 129 // Gets/Sets whether or not this Textfield is a password field. | 65 // Gets/Sets whether or not this Textfield is a password field. |
| 130 bool IsPassword() const; | 66 bool IsPassword() const; |
| 131 void SetPassword(bool password); | 67 void SetPassword(bool password); |
| 132 | 68 |
| 133 // Whether the text field is multi-line or not, must be set when the text | 69 // Whether the text field is multi-line or not, must be set when the text |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 protected: | 212 protected: |
| 277 virtual void ViewHierarchyChanged(bool is_add, View* parent, | 213 virtual void ViewHierarchyChanged(bool is_add, View* parent, |
| 278 View* child) OVERRIDE; | 214 View* child) OVERRIDE; |
| 279 virtual std::string GetClassName() const OVERRIDE; | 215 virtual std::string GetClassName() const OVERRIDE; |
| 280 | 216 |
| 281 // The object that actually implements the native text field. | 217 // The object that actually implements the native text field. |
| 282 NativeTextfieldWrapper* native_wrapper_; | 218 NativeTextfieldWrapper* native_wrapper_; |
| 283 | 219 |
| 284 private: | 220 private: |
| 285 // This is the current listener for events from this Textfield. | 221 // This is the current listener for events from this Textfield. |
| 286 Controller* controller_; | 222 TextfieldController* controller_; |
| 287 | 223 |
| 288 // The mask of style options for this Textfield. | 224 // The mask of style options for this Textfield. |
| 289 StyleFlags style_; | 225 StyleFlags style_; |
| 290 | 226 |
| 291 // The font used to render the text in the Textfield. | 227 // The font used to render the text in the Textfield. |
| 292 gfx::Font font_; | 228 gfx::Font font_; |
| 293 | 229 |
| 294 // The text displayed in the Textfield. | 230 // The text displayed in the Textfield. |
| 295 string16 text_; | 231 string16 text_; |
| 296 | 232 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 | 274 |
| 339 // Text to display when empty. | 275 // Text to display when empty. |
| 340 string16 text_to_display_when_empty_; | 276 string16 text_to_display_when_empty_; |
| 341 | 277 |
| 342 DISALLOW_COPY_AND_ASSIGN(Textfield); | 278 DISALLOW_COPY_AND_ASSIGN(Textfield); |
| 343 }; | 279 }; |
| 344 | 280 |
| 345 } // namespace views | 281 } // namespace views |
| 346 | 282 |
| 347 #endif // VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_ | 283 #endif // VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_ |
| OLD | NEW |