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
|
// Copyright (c) 2006-2008 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.
// These classes define a text field widget that can be used in the views UI
// toolkit.
#ifndef CHROME_VIEWS_TEXT_FIELD_H_
#define CHROME_VIEWS_TEXT_FIELD_H_
#include <string>
#include "base/basictypes.h"
#include "chrome/common/gfx/chrome_font.h"
#include "chrome/views/view.h"
#include "skia/include/SkColor.h"
namespace views {
class HWNDView;
// This class implements a ChromeView that wraps a native text (edit) field.
class TextField : public View {
public:
// This defines the callback interface for other code to be notified of
// changes in the state of a text field.
class Controller {
public:
// This method is called whenever the text in the field changes.
virtual void ContentsChanged(TextField* sender,
const std::wstring& new_contents) = 0;
// This method is called to get notified about keystrokes in the edit.
virtual void HandleKeystroke(TextField* sender,
UINT message, TCHAR key, UINT repeat_count,
UINT flags) = 0;
};
enum StyleFlags {
STYLE_DEFAULT = 0,
STYLE_PASSWORD = 1<<0,
STYLE_MULTILINE = 1<<1
};
TextField::TextField()
: native_view_(NULL),
edit_(NULL),
controller_(NULL),
style_(STYLE_DEFAULT),
read_only_(false),
default_width_in_chars_(0),
draw_border_(true),
use_default_background_color_(true),
num_lines_(1) {
SetFocusable(true);
}
explicit TextField::TextField(StyleFlags style)
: native_view_(NULL),
edit_(NULL),
controller_(NULL),
style_(style),
read_only_(false),
default_width_in_chars_(0),
draw_border_(true),
use_default_background_color_(true),
num_lines_(1) {
SetFocusable(true);
}
virtual ~TextField();
void ViewHierarchyChanged(bool is_add, View* parent, View* child);
// Overridden for layout purposes
virtual void Layout();
virtual gfx::Size GetPreferredSize();
// Controller accessors
void SetController(Controller* controller);
Controller* GetController() const;
void SetReadOnly(bool read_only);
bool IsReadOnly() const;
// Whether the text field is multi-line or not, must be set when the text
// field is created, using StyleFlags.
bool IsMultiLine() const;
virtual bool IsFocusable() const;
virtual void AboutToRequestFocusFromTabTraversal(bool reverse);
// Overridden from Chrome::View.
virtual bool ShouldLookupAccelerators(const KeyEvent& e);
virtual HWND GetNativeComponent();
// Returns the text currently displayed in the text field.
std::wstring GetText() const;
// Set the text currently displayed in the text field.
void SetText(const std::wstring& text);
virtual void Focus();
// Causes the edit field to be fully selected.
void SelectAll();
// Clears the selection within the edit field and sets the caret to the end.
void ClearSelection() const;
StyleFlags GetStyle() const { return style_; }
void SetBackgroundColor(SkColor color);
void SetDefaultBackgroundColor();
// Set the font.
void SetFont(const ChromeFont& font);
// Return the font used by this TextField.
ChromeFont GetFont() const;
// Sets the left and right margin (in pixels) within the text box. On Windows
// this is accomplished by packing the left and right margin into a single
// 32 bit number, so the left and right margins are effectively 16 bits.
bool SetHorizontalMargins(int left, int right);
// Should only be called on a multi-line text field. Sets how many lines of
// text can be displayed at once by this text field.
void SetHeightInLines(int num_lines);
// Sets the default width of the text control. See default_width_in_chars_.
void set_default_width_in_chars(int default_width) {
default_width_in_chars_ = default_width;
}
// Removes the border from the edit box, giving it a 2D look.
void RemoveBorder();
// Disable the edit control.
// NOTE: this does NOT change the read only property.
void SetEnabled(bool enabled);
private:
class Edit;
// Invoked by the edit control when the value changes. This method set
// the text_ member variable to the value contained in edit control.
// This is important because the edit control can be replaced if it has
// been deleted during a window close.
void SyncText();
// Reset the text field native control.
void ResetNativeControl();
// Resets the background color of the edit.
void UpdateEditBackgroundColor();
// This encapsulates the HWND of the native text field.
HWNDView* native_view_;
// This inherits from the native text field.
Edit* edit_;
// This is the current listener for events from this control.
Controller* controller_;
StyleFlags style_;
ChromeFont font_;
// NOTE: this is temporary until we rewrite TextField to always work whether
// there is an HWND or not.
// Used if the HWND hasn't been created yet.
std::wstring text_;
bool read_only_;
// The default number of average characters for the width of this text field.
// This will be reported as the "desired size". Defaults to 0.
int default_width_in_chars_;
// Whether the border is drawn.
bool draw_border_;
SkColor background_color_;
bool use_default_background_color_;
// The number of lines of text this textfield displays at once.
int num_lines_;
protected:
// Calculates the insets for the text field.
void CalculateInsets(gfx::Insets* insets);
DISALLOW_COPY_AND_ASSIGN(TextField);
};
} // namespace views
#endif // CHROME_VIEWS_TEXT_FIELD_H_
|