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
|
// Copyright (c) 2009 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.
#include <gtk/gtk.h>
#include "views/controls/textfield/native_textfield_gtk.h"
#include "base/gfx/gtk_util.h"
#include "base/string_util.h"
#include "skia/ext/skia_utils_gtk.h"
#include "views/controls/textfield/textfield.h"
namespace views {
////////////////////////////////////////////////////////////////////////////////
// NativeTextfieldGtk, public:
NativeTextfieldGtk::NativeTextfieldGtk(Textfield* textfield)
: textfield_(textfield) {
if (textfield_->style() & Textfield::STYLE_MULTILINE)
NOTIMPLEMENTED(); // We don't support multiline yet.
}
NativeTextfieldGtk::~NativeTextfieldGtk() {
}
////////////////////////////////////////////////////////////////////////////////
// NativeTextfieldGtk, NativeTextfieldWrapper implementation:
string16 NativeTextfieldGtk::GetText() const {
return UTF8ToUTF16(gtk_entry_get_text(GTK_ENTRY(native_view())));
}
void NativeTextfieldGtk::UpdateText() {
if (!native_view())
return;
gtk_entry_set_text(GTK_ENTRY(native_view()),
UTF16ToUTF8(textfield_->text()).c_str());
}
void NativeTextfieldGtk::AppendText(const string16& text) {
gint position = -1;
gtk_editable_insert_text(GTK_EDITABLE(native_view()),
UTF16ToUTF8(text).c_str(),
text.size(), &position);
if (!native_view())
return;
gtk_entry_append_text(GTK_ENTRY(native_view()), UTF16ToUTF8(text).c_str());
}
string16 NativeTextfieldGtk::GetSelectedText() const {
if (!native_view())
return string16();
string16 result;
gint start_pos;
gint end_pos;
if (!gtk_editable_get_selection_bounds(GTK_EDITABLE(native_view()),
&start_pos, &end_pos))
return result; // No selection.
UTF8ToUTF16(gtk_editable_get_chars(GTK_EDITABLE(native_view()),
start_pos, end_pos),
end_pos - start_pos, &result);
return result;
}
void NativeTextfieldGtk::SelectAll() {
if (!native_view())
return;
// -1 as the end position selects to the end of the text.
gtk_editable_select_region(GTK_EDITABLE(native_view()), 0, -1);
}
void NativeTextfieldGtk::ClearSelection() {
if (!native_view())
return;
gtk_editable_select_region(GTK_EDITABLE(native_view()), 0, 0);
}
void NativeTextfieldGtk::UpdateBorder() {
if (!native_view())
return;
}
void NativeTextfieldGtk::UpdateBackgroundColor() {
if (textfield_->use_default_background_color()) {
// Passing NULL as the color undoes the effect of previous calls to
// gtk_widget_modify_base.
gtk_widget_modify_base(native_view(), GTK_STATE_NORMAL, NULL);
return;
}
GdkColor gdk_color = skia::SkColorToGdkColor(textfield_->background_color());
gtk_widget_modify_base(native_view(), GTK_STATE_NORMAL, &gdk_color);
}
void NativeTextfieldGtk::UpdateReadOnly() {
if (!native_view())
return;
gtk_editable_set_editable(GTK_EDITABLE(native_view()),
!textfield_->read_only());
}
void NativeTextfieldGtk::UpdateFont() {
if (!native_view())
return;
gtk_widget_modify_font(native_view(),
gfx::Font::PangoFontFromGfxFont(textfield_->font()));
}
void NativeTextfieldGtk::UpdateEnabled() {
if (!native_view())
return;
SetEnabled(textfield_->IsEnabled());
}
void NativeTextfieldGtk::SetHorizontalMargins(int left, int right) {
if (!native_view())
return;
GtkBorder border = { left, right, 0, 0 };
gtk_entry_set_inner_border(GTK_ENTRY(native_view()), &border);
}
void NativeTextfieldGtk::SetFocus() {
Focus();
}
View* NativeTextfieldGtk::GetView() {
return this;
}
gfx::NativeView NativeTextfieldGtk::GetTestingHandle() const {
return native_view();
}
// static
gboolean NativeTextfieldGtk::OnKeyPressEventHandler(
GtkWidget* entry,
GdkEventKey* event,
NativeTextfieldGtk* textfield) {
return textfield->OnKeyPressEvent(event);
}
gboolean NativeTextfieldGtk::OnKeyPressEvent(GdkEventKey* event) {
Textfield::Controller* controller = textfield_->GetController();
if (controller) {
Textfield::Keystroke ks(event);
return controller->HandleKeystroke(textfield_, ks);
}
return false;
}
// static
gboolean NativeTextfieldGtk::OnChangedHandler(
GtkWidget* entry,
NativeTextfieldGtk* textfield) {
return textfield->OnChanged();
}
gboolean NativeTextfieldGtk::OnChanged() {
Textfield::Controller* controller = textfield_->GetController();
if (controller)
controller->ContentsChanged(textfield_, GetText());
return false;
}
////////////////////////////////////////////////////////////////////////////////
// NativeTextfieldGtk, NativeControlGtk overrides:
void NativeTextfieldGtk::CreateNativeControl() {
NativeControlCreated(gtk_entry_new());
}
void NativeTextfieldGtk::NativeControlCreated(GtkWidget* widget) {
NativeControlGtk::NativeControlCreated(widget);
g_signal_connect(widget, "changed",
G_CALLBACK(OnChangedHandler), this);
g_signal_connect(widget, "key-press-event",
G_CALLBACK(OnKeyPressEventHandler), this);
}
////////////////////////////////////////////////////////////////////////////////
// NativeTextfieldWrapper, public:
// static
NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper(
Textfield* field) {
return new NativeTextfieldGtk(field);
}
} // namespace views
|