| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "app/gfx/font.h" | 10 #include "app/gfx/font.h" |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "views/view.h" | 12 #include "views/view.h" |
| 13 #include "third_party/skia/include/core/SkColor.h" | 13 #include "third_party/skia/include/core/SkColor.h" |
| 14 | 14 |
| 15 #ifdef UNIT_TEST |
| 16 #include "base/gfx/native_widget_types.h" |
| 17 #include "views/controls/textfield/native_textfield_wrapper.h" |
| 18 #endif |
| 19 |
| 15 namespace views { | 20 namespace views { |
| 16 | 21 |
| 17 class HWNDView; | 22 class NativeTextfieldWrapper; |
| 18 | 23 |
| 19 // This class implements a ChromeView that wraps a native text (edit) field. | 24 // This class implements a ChromeView that wraps a native text (edit) field. |
| 20 class Textfield : public View { | 25 class Textfield : public View { |
| 21 public: | 26 public: |
| 27 // The button's class name. |
| 28 static const char kViewClassName[]; |
| 29 |
| 22 // Keystroke provides a platform-dependent way to send keystroke events. | 30 // Keystroke provides a platform-dependent way to send keystroke events. |
| 23 // Cross-platform code can use IsKeystrokeEnter/Escape to check for these | 31 // Cross-platform code can use IsKeystrokeEnter/Escape to check for these |
| 24 // two common key events. | 32 // two common key events. |
| 25 // TODO(brettw) this should be cleaned up to be more cross-platform. | 33 // TODO(brettw) this should be cleaned up to be more cross-platform. |
| 26 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
| 27 struct Keystroke { | 35 struct Keystroke { |
| 28 Keystroke(unsigned int m, | 36 Keystroke(unsigned int m, |
| 29 wchar_t k, | 37 wchar_t k, |
| 30 int r, | 38 int r, |
| 31 unsigned int f) | 39 unsigned int f) |
| (...skipping 29 matching lines...) Expand all Loading... |
| 61 const Textfield::Keystroke& keystroke) = 0; | 69 const Textfield::Keystroke& keystroke) = 0; |
| 62 }; | 70 }; |
| 63 | 71 |
| 64 enum StyleFlags { | 72 enum StyleFlags { |
| 65 STYLE_DEFAULT = 0, | 73 STYLE_DEFAULT = 0, |
| 66 STYLE_PASSWORD = 1<<0, | 74 STYLE_PASSWORD = 1<<0, |
| 67 STYLE_MULTILINE = 1<<1, | 75 STYLE_MULTILINE = 1<<1, |
| 68 STYLE_LOWERCASE = 1<<2 | 76 STYLE_LOWERCASE = 1<<2 |
| 69 }; | 77 }; |
| 70 | 78 |
| 71 Textfield() | 79 Textfield(); |
| 72 : | 80 explicit Textfield(StyleFlags style); |
| 73 #if defined(OS_WIN) | |
| 74 native_view_(NULL), | |
| 75 #endif | |
| 76 edit_(NULL), | |
| 77 controller_(NULL), | |
| 78 style_(STYLE_DEFAULT), | |
| 79 read_only_(false), | |
| 80 default_width_in_chars_(0), | |
| 81 draw_border_(true), | |
| 82 use_default_background_color_(true), | |
| 83 num_lines_(1) { | |
| 84 SetFocusable(true); | |
| 85 } | |
| 86 explicit Textfield(StyleFlags style) | |
| 87 : | |
| 88 #if defined(OS_WIN) | |
| 89 native_view_(NULL), | |
| 90 #endif | |
| 91 edit_(NULL), | |
| 92 controller_(NULL), | |
| 93 style_(style), | |
| 94 read_only_(false), | |
| 95 default_width_in_chars_(0), | |
| 96 draw_border_(true), | |
| 97 use_default_background_color_(true), | |
| 98 num_lines_(1) { | |
| 99 SetFocusable(true); | |
| 100 } | |
| 101 virtual ~Textfield(); | 81 virtual ~Textfield(); |
| 102 | 82 |
| 103 void ViewHierarchyChanged(bool is_add, View* parent, View* child); | |
| 104 | |
| 105 // Overridden for layout purposes | |
| 106 virtual void Layout(); | |
| 107 virtual gfx::Size GetPreferredSize(); | |
| 108 | 83 |
| 109 // Controller accessors | 84 // Controller accessors |
| 110 void SetController(Controller* controller); | 85 void SetController(Controller* controller); |
| 111 Controller* GetController() const; | 86 Controller* GetController() const; |
| 112 | 87 |
| 88 // Gets/Sets whether or not the Textfield is read-only. |
| 89 bool read_only() const { return read_only_; } |
| 113 void SetReadOnly(bool read_only); | 90 void SetReadOnly(bool read_only); |
| 114 bool IsReadOnly() const; | |
| 115 | 91 |
| 92 // Returns true if the Textfield is a password field. |
| 116 bool IsPassword() const; | 93 bool IsPassword() const; |
| 117 | 94 |
| 118 // Whether the text field is multi-line or not, must be set when the text | 95 // Whether the text field is multi-line or not, must be set when the text |
| 119 // field is created, using StyleFlags. | 96 // field is created, using StyleFlags. |
| 120 bool IsMultiLine() const; | 97 bool IsMultiLine() const; |
| 121 | 98 |
| 122 virtual bool IsFocusable() const; | 99 // Gets/Sets the text currently displayed in the Textfield. |
| 123 virtual void AboutToRequestFocusFromTabTraversal(bool reverse); | 100 const std::wstring& text() const { return text_; } |
| 124 | |
| 125 // Overridden from Chrome::View. | |
| 126 virtual bool SkipDefaultKeyEventProcessing(const KeyEvent& e); | |
| 127 | |
| 128 #if defined(OS_WIN) | |
| 129 virtual HWND GetNativeComponent(); | |
| 130 #endif | |
| 131 | |
| 132 // Returns the text currently displayed in the text field. | |
| 133 std::wstring GetText() const; | |
| 134 | |
| 135 // Sets the text currently displayed in the text field. | |
| 136 void SetText(const std::wstring& text); | 101 void SetText(const std::wstring& text); |
| 137 | 102 |
| 138 // Appends the given string to the previously-existing text in the field. | 103 // Appends the given string to the previously-existing text in the field. |
| 139 void AppendText(const std::wstring& text); | 104 void AppendText(const std::wstring& text); |
| 140 | 105 |
| 141 virtual void Focus(); | |
| 142 | |
| 143 // Causes the edit field to be fully selected. | 106 // Causes the edit field to be fully selected. |
| 144 void SelectAll(); | 107 void SelectAll(); |
| 145 | 108 |
| 146 // Clears the selection within the edit field and sets the caret to the end. | 109 // Clears the selection within the edit field and sets the caret to the end. |
| 147 void ClearSelection() const; | 110 void ClearSelection() const; |
| 148 | 111 |
| 149 StyleFlags GetStyle() const { return style_; } | 112 // Accessor for |style_|. |
| 113 StyleFlags style() const { return style_; } |
| 150 | 114 |
| 115 // Gets/Sets the background color to be used when painting the Textfield. |
| 116 // Call |UseDefaultBackgroundColor| to return to the system default colors. |
| 117 SkColor background_color() const { return background_color_; } |
| 151 void SetBackgroundColor(SkColor color); | 118 void SetBackgroundColor(SkColor color); |
| 152 void SetDefaultBackgroundColor(); | |
| 153 | 119 |
| 154 // Set the font. | 120 // Gets/Sets whether the default background color should be used when painting |
| 121 // the Textfield. |
| 122 bool use_default_background_color() const { |
| 123 return use_default_background_color_; |
| 124 } |
| 125 void UseDefaultBackgroundColor(); |
| 126 |
| 127 // Gets/Sets the font used when rendering the text within the Textfield. |
| 128 gfx::Font font() const { return font_; } |
| 155 void SetFont(const gfx::Font& font); | 129 void SetFont(const gfx::Font& font); |
| 156 | 130 |
| 157 // Return the font used by this Textfield. | |
| 158 gfx::Font GetFont() const; | |
| 159 | |
| 160 // Sets the left and right margin (in pixels) within the text box. On Windows | 131 // Sets the left and right margin (in pixels) within the text box. On Windows |
| 161 // this is accomplished by packing the left and right margin into a single | 132 // this is accomplished by packing the left and right margin into a single |
| 162 // 32 bit number, so the left and right margins are effectively 16 bits. | 133 // 32 bit number, so the left and right margins are effectively 16 bits. |
| 163 bool SetHorizontalMargins(int left, int right); | 134 void SetHorizontalMargins(int left, int right); |
| 164 | 135 |
| 165 // Should only be called on a multi-line text field. Sets how many lines of | 136 // Should only be called on a multi-line text field. Sets how many lines of |
| 166 // text can be displayed at once by this text field. | 137 // text can be displayed at once by this text field. |
| 167 void SetHeightInLines(int num_lines); | 138 void SetHeightInLines(int num_lines); |
| 168 | 139 |
| 169 // Sets the default width of the text control. See default_width_in_chars_. | 140 // Sets the default width of the text control. See default_width_in_chars_. |
| 170 void set_default_width_in_chars(int default_width) { | 141 void set_default_width_in_chars(int default_width) { |
| 171 default_width_in_chars_ = default_width; | 142 default_width_in_chars_ = default_width; |
| 172 } | 143 } |
| 173 | 144 |
| 174 // Removes the border from the edit box, giving it a 2D look. | 145 // Removes the border from the edit box, giving it a 2D look. |
| 146 bool draw_border() const { return draw_border_; } |
| 175 void RemoveBorder(); | 147 void RemoveBorder(); |
| 176 | 148 |
| 177 // Disable the edit control. | 149 // Calculates the insets for the text field. |
| 178 // NOTE: this does NOT change the read only property. | 150 void CalculateInsets(gfx::Insets* insets); |
| 179 void SetEnabled(bool enabled); | 151 |
| 152 // Invoked by the edit control when the value changes. This method set |
| 153 // the text_ member variable to the value contained in edit control. |
| 154 // This is important because the edit control can be replaced if it has |
| 155 // been deleted during a window close. |
| 156 void SyncText(); |
| 180 | 157 |
| 181 // Provides a cross-platform way of checking whether a keystroke is one of | 158 // Provides a cross-platform way of checking whether a keystroke is one of |
| 182 // these common keys. Most code only checks keystrokes against these two keys, | 159 // these common keys. Most code only checks keystrokes against these two keys, |
| 183 // so the caller can be cross-platform by implementing the platform-specific | 160 // so the caller can be cross-platform by implementing the platform-specific |
| 184 // parts in here. | 161 // parts in here. |
| 185 // TODO(brettw) we should use a more cross-platform representation of | 162 // TODO(brettw) we should use a more cross-platform representation of |
| 186 // keyboard events so these are not necessary. | 163 // keyboard events so these are not necessary. |
| 187 static bool IsKeystrokeEnter(const Keystroke& key); | 164 static bool IsKeystrokeEnter(const Keystroke& key); |
| 188 static bool IsKeystrokeEscape(const Keystroke& key); | 165 static bool IsKeystrokeEscape(const Keystroke& key); |
| 189 | 166 |
| 190 private: | 167 #ifdef UNIT_TEST |
| 191 class Edit; | 168 gfx::NativeView GetTestingHandle() const { |
| 192 | 169 return native_wrapper_ ? native_wrapper_->GetTestingHandle() : NULL; |
| 193 // Invoked by the edit control when the value changes. This method set | 170 } |
| 194 // the text_ member variable to the value contained in edit control. | |
| 195 // This is important because the edit control can be replaced if it has | |
| 196 // been deleted during a window close. | |
| 197 void SyncText(); | |
| 198 | |
| 199 // Reset the text field native control. | |
| 200 void ResetNativeControl(); | |
| 201 | |
| 202 // Resets the background color of the edit. | |
| 203 void UpdateEditBackgroundColor(); | |
| 204 | |
| 205 #if defined(OS_WIN) | |
| 206 // This encapsulates the HWND of the native text field. | |
| 207 HWNDView* native_view_; | |
| 208 #endif | 171 #endif |
| 209 | 172 |
| 210 // This inherits from the native text field. | 173 // Overridden from View: |
| 211 Edit* edit_; | 174 virtual void Layout(); |
| 175 virtual gfx::Size GetPreferredSize(); |
| 176 virtual bool IsFocusable() const; |
| 177 virtual void AboutToRequestFocusFromTabTraversal(bool reverse); |
| 178 virtual bool SkipDefaultKeyEventProcessing(const KeyEvent& e); |
| 179 virtual void SetEnabled(bool enabled); |
| 212 | 180 |
| 213 // This is the current listener for events from this control. | 181 protected: |
| 182 virtual void Focus(); |
| 183 virtual void ViewHierarchyChanged(bool is_add, View* parent, View* child); |
| 184 virtual std::string GetClassName() const; |
| 185 |
| 186 private: |
| 187 // The object that actually implements the native text field. |
| 188 NativeTextfieldWrapper* native_wrapper_; |
| 189 |
| 190 // This is the current listener for events from this Textfield. |
| 214 Controller* controller_; | 191 Controller* controller_; |
| 215 | 192 |
| 193 // The mask of style options for this Textfield. |
| 216 StyleFlags style_; | 194 StyleFlags style_; |
| 217 | 195 |
| 196 // The font used to render the text in the Textfield. |
| 218 gfx::Font font_; | 197 gfx::Font font_; |
| 219 | 198 |
| 220 // NOTE: this is temporary until we rewrite Textfield to always work whether | 199 // The text displayed in the Textfield. |
| 221 // there is an HWND or not. | |
| 222 // Used if the HWND hasn't been created yet. | |
| 223 std::wstring text_; | 200 std::wstring text_; |
| 224 | 201 |
| 202 // True if this Textfield cannot accept input and is read-only. |
| 225 bool read_only_; | 203 bool read_only_; |
| 226 | 204 |
| 227 // The default number of average characters for the width of this text field. | 205 // The default number of average characters for the width of this text field. |
| 228 // This will be reported as the "desired size". Defaults to 0. | 206 // This will be reported as the "desired size". Defaults to 0. |
| 229 int default_width_in_chars_; | 207 int default_width_in_chars_; |
| 230 | 208 |
| 231 // Whether the border is drawn. | 209 // Whether the border is drawn. |
| 232 bool draw_border_; | 210 bool draw_border_; |
| 233 | 211 |
| 212 // The background color to be used when painting the Textfield, provided |
| 213 // |use_default_background_color_| is set to false. |
| 234 SkColor background_color_; | 214 SkColor background_color_; |
| 235 | 215 |
| 216 // When true, the system colors for Textfields are used when painting this |
| 217 // Textfield. When false, the value of |background_color_| determines the |
| 218 // Textfield's background color. |
| 236 bool use_default_background_color_; | 219 bool use_default_background_color_; |
| 237 | 220 |
| 238 // The number of lines of text this Textfield displays at once. | 221 // The number of lines of text this Textfield displays at once. |
| 239 int num_lines_; | 222 int num_lines_; |
| 240 | 223 |
| 241 protected: | 224 // TODO(beng): remove this once NativeTextfieldWin subclasses |
| 242 // Calculates the insets for the text field. | 225 // NativeControlWin. |
| 243 void CalculateInsets(gfx::Insets* insets); | 226 bool initialized_; |
| 244 | 227 |
| 245 DISALLOW_COPY_AND_ASSIGN(Textfield); | 228 DISALLOW_COPY_AND_ASSIGN(Textfield); |
| 246 }; | 229 }; |
| 247 | 230 |
| 248 } // namespace views | 231 } // namespace views |
| 249 | 232 |
| 250 #endif // VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_ | 233 #endif // VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_ |
| OLD | NEW |