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