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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
// Copyright (c) 2011 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 <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include "views/controls/textfield/native_textfield_gtk.h"
#include "base/logging.h"
#include "base/utf_string_conversions.h"
#include "ui/base/range/range.h"
#include "ui/gfx/gtk_util.h"
#include "ui/gfx/insets.h"
#include "ui/gfx/skia_utils_gtk.h"
#include "views/controls/textfield/gtk_views_entry.h"
#include "views/controls/textfield/gtk_views_textview.h"
#include "views/controls/textfield/native_textfield_views.h"
#include "views/controls/textfield/textfield.h"
#include "views/controls/textfield/textfield_controller.h"
#include "views/widget/widget_gtk.h"
namespace views {
// A character used to hide a text in password mode.
static const char kPasswordChar = '*';
// Border width for GtkTextView.
const int kTextViewBorderWidth = 4;
////////////////////////////////////////////////////////////////////////////////
// NativeTextfieldGtk, public:
NativeTextfieldGtk::NativeTextfieldGtk(Textfield* textfield)
: textfield_(textfield) {
if (textfield_->IsMultiLine() && textfield_->IsPassword())
NOTIMPLEMENTED(); // We don't support multiline password yet.
// Make |textfield| the focused view, so that when we get focused the focus
// manager sees |textfield| as the focused view (since we are just a wrapper
// view).
set_focus_view(textfield);
}
NativeTextfieldGtk::~NativeTextfieldGtk() {
}
// Returns the inner border of an entry.
// static
gfx::Insets NativeTextfieldGtk::GetEntryInnerBorder(GtkEntry* entry) {
const GtkBorder* inner_border = gtk_entry_get_inner_border(entry);
if (inner_border)
return gfx::Insets(*inner_border);
// No explicit border set, try the style.
GtkBorder* style_border;
gtk_widget_style_get(GTK_WIDGET(entry), "inner-border", &style_border, NULL);
if (style_border) {
gfx::Insets insets = gfx::Insets(*style_border);
gtk_border_free(style_border);
return insets;
}
// If border is null, Gtk uses 2 on all sides.
return gfx::Insets(2, 2, 2, 2);
}
gfx::Insets NativeTextfieldGtk::GetTextViewInnerBorder(GtkTextView* text_view) {
return gfx::Insets(kTextViewBorderWidth / 2, kTextViewBorderWidth / 2,
kTextViewBorderWidth / 2, kTextViewBorderWidth / 2);
}
////////////////////////////////////////////////////////////////////////////////
// NativeTextfieldGtk, NativeTextfieldWrapper implementation:
string16 NativeTextfieldGtk::GetText() const {
if (textfield_->IsMultiLine()) {
GtkTextBuffer* text_buffer = gtk_text_view_get_buffer(
GTK_TEXT_VIEW(native_view()));
GtkTextIter start;
GtkTextIter end;
gtk_text_buffer_get_bounds(text_buffer, &start, &end);
return UTF8ToUTF16(gtk_text_iter_get_visible_text(&start, &end));
} else {
return UTF8ToUTF16(gtk_entry_get_text(GTK_ENTRY(native_view())));
}
}
void NativeTextfieldGtk::UpdateText() {
if (!native_view())
return;
if (textfield_->IsMultiLine()) {
GtkTextBuffer* text_buffer = gtk_text_view_get_buffer(
GTK_TEXT_VIEW(native_view()));
std::string utf8 = UTF16ToUTF8(textfield_->text());
gtk_text_buffer_set_text(text_buffer, utf8.c_str(), utf8.length());
} else {
gtk_entry_set_text(GTK_ENTRY(native_view()),
UTF16ToUTF8(textfield_->text()).c_str());
}
}
void NativeTextfieldGtk::AppendText(const string16& text) {
if (!native_view())
return;
if (textfield_->IsMultiLine()) {
GtkTextBuffer* text_buffer = gtk_text_view_get_buffer(
GTK_TEXT_VIEW(native_view()));
GtkTextIter end;
gtk_text_buffer_get_end_iter(text_buffer, &end);
std::string utf8 = UTF16ToUTF8(text);
gtk_text_buffer_insert(text_buffer, &end, utf8.c_str(), utf8.length());
} else {
gtk_entry_append_text(GTK_ENTRY(native_view()), UTF16ToUTF8(text).c_str());
}
}
string16 NativeTextfieldGtk::GetSelectedText() const {
if (!native_view())
return string16();
string16 result;
if (textfield_->IsMultiLine()) {
GtkTextBuffer* text_buffer = gtk_text_view_get_buffer(
GTK_TEXT_VIEW(native_view()));
GtkTextIter start;
GtkTextIter end;
if (gtk_text_buffer_get_selection_bounds(text_buffer, &start, &end)) {
gchar* selected_text = gtk_text_iter_get_visible_text(&start, &end);
if (selected_text)
UTF8ToUTF16(selected_text, strlen(selected_text), &result);
}
} else {
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;
if (textfield_->IsMultiLine()) {
GtkTextBuffer* text_buffer = gtk_text_view_get_buffer(
GTK_TEXT_VIEW(native_view()));
GtkTextIter start;
GtkTextIter end;
gtk_text_buffer_get_bounds(text_buffer, &start, &end);
gtk_text_buffer_select_range(text_buffer, &start, &end);
} else {
// -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;
if (textfield_->IsMultiLine()) {
GtkTextBuffer* text_buffer = gtk_text_view_get_buffer(
GTK_TEXT_VIEW(native_view()));
GtkTextMark* insert_mark = gtk_text_buffer_get_insert(text_buffer);
GtkTextIter insert;
gtk_text_buffer_get_iter_at_mark(text_buffer, &insert, insert_mark);
gtk_text_buffer_select_range(text_buffer, &insert, &insert);
} else {
gtk_editable_select_region(GTK_EDITABLE(native_view()), 0, 0);
}
}
void NativeTextfieldGtk::UpdateBorder() {
if (!native_view())
return;
if (textfield_->IsMultiLine()) {
if (!textfield_->draw_border()) {
gtk_container_set_border_width(GTK_CONTAINER(native_view()), 0);
// Use margin to match entry with no border
textfield_->SetHorizontalMargins(kTextViewBorderWidth / 2 + 1,
kTextViewBorderWidth / 2 + 1);
}
} else {
if (!textfield_->draw_border())
gtk_entry_set_has_frame(GTK_ENTRY(native_view()), false);
}
}
void NativeTextfieldGtk::UpdateTextColor() {
if (textfield_->use_default_text_color()) {
// Passing NULL as the color undoes the effect of previous calls to
// gtk_widget_modify_text.
gtk_widget_modify_text(native_view(), GTK_STATE_NORMAL, NULL);
return;
}
GdkColor gdk_color = gfx::SkColorToGdkColor(textfield_->text_color());
gtk_widget_modify_text(native_view(), GTK_STATE_NORMAL, &gdk_color);
}
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 = gfx::SkColorToGdkColor(textfield_->background_color());
gtk_widget_modify_base(native_view(), GTK_STATE_NORMAL, &gdk_color);
}
void NativeTextfieldGtk::UpdateReadOnly() {
if (!native_view())
return;
if (textfield_->IsMultiLine()) {
gtk_text_view_set_editable(GTK_TEXT_VIEW(native_view()),
!textfield_->read_only());
} else {
gtk_editable_set_editable(GTK_EDITABLE(native_view()),
!textfield_->read_only());
}
}
void NativeTextfieldGtk::UpdateFont() {
if (!native_view())
return;
PangoFontDescription* pfd = textfield_->font().GetNativeFont();
gtk_widget_modify_font(native_view(), pfd);
pango_font_description_free(pfd);
}
void NativeTextfieldGtk::UpdateIsPassword() {
if (!native_view())
return;
if (!textfield_->IsMultiLine()) {
gtk_entry_set_visibility(GTK_ENTRY(native_view()),
!textfield_->IsPassword());
}
}
void NativeTextfieldGtk::UpdateEnabled() {
if (!native_view())
return;
SetEnabled(textfield_->IsEnabled());
}
gfx::Insets NativeTextfieldGtk::CalculateInsets() {
if (!native_view())
return gfx::Insets();
GtkWidget* widget = native_view();
gfx::Insets insets;
if (textfield_->IsMultiLine()) {
insets += GetTextViewInnerBorder(GTK_TEXT_VIEW(widget));
} else {
GtkEntry* entry = GTK_ENTRY(widget);
insets += GetEntryInnerBorder(entry);
if (entry->has_frame) {
insets += gfx::Insets(widget->style->ythickness,
widget->style->xthickness,
widget->style->ythickness,
widget->style->xthickness);
}
}
gboolean interior_focus;
gint focus_width;
gtk_widget_style_get(widget,
"focus-line-width", &focus_width,
"interior-focus", &interior_focus,
NULL);
if (!interior_focus)
insets += gfx::Insets(focus_width, focus_width, focus_width, focus_width);
return insets;
}
void NativeTextfieldGtk::UpdateHorizontalMargins() {
if (!native_view())
return;
int left, right;
if (!textfield_->GetHorizontalMargins(&left, &right))
return;
if (textfield_->IsMultiLine()) {
GtkTextView* text_view = GTK_TEXT_VIEW(native_view());
gtk_text_view_set_left_margin(text_view, left);
gtk_text_view_set_right_margin(text_view, right);
} else {
gfx::Insets insets = GetEntryInnerBorder(GTK_ENTRY(native_view()));
GtkBorder border = {left, right, insets.top(), insets.bottom()};
gtk_entry_set_inner_border(GTK_ENTRY(native_view()), &border);
}
}
void NativeTextfieldGtk::UpdateVerticalMargins() {
if (!native_view())
return;
int top, bottom;
if (!textfield_->GetVerticalMargins(&top, &bottom))
return;
if (!textfield_->IsMultiLine()) {
gfx::Insets insets = GetEntryInnerBorder(GTK_ENTRY(native_view()));
GtkBorder border = {insets.left(), insets.right(), top, bottom};
gtk_entry_set_inner_border(GTK_ENTRY(native_view()), &border);
} else {
NOTIMPLEMENTED();
}
}
bool NativeTextfieldGtk::SetFocus() {
OnFocus();
return true;
}
View* NativeTextfieldGtk::GetView() {
return this;
}
gfx::NativeView NativeTextfieldGtk::GetTestingHandle() const {
return native_view();
}
bool NativeTextfieldGtk::IsIMEComposing() const {
return false;
}
void NativeTextfieldGtk::GetSelectedRange(ui::Range* range) const {
gint start_pos;
gint end_pos;
gtk_editable_get_selection_bounds(
GTK_EDITABLE(native_view()), &start_pos, &end_pos);
*range = ui::Range(start_pos, end_pos);
}
void NativeTextfieldGtk::SelectRange(const ui::Range& range) {
NOTREACHED();
}
size_t NativeTextfieldGtk::GetCursorPosition() const {
NOTREACHED();
return 0U;
}
bool NativeTextfieldGtk::HandleKeyPressed(const views::KeyEvent& e) {
return false;
}
bool NativeTextfieldGtk::HandleKeyReleased(const views::KeyEvent& e) {
return false;
}
void NativeTextfieldGtk::HandleFocus() {
}
void NativeTextfieldGtk::HandleBlur() {
}
// static
gboolean NativeTextfieldGtk::OnKeyPressEventHandler(
GtkWidget* widget,
GdkEventKey* event,
NativeTextfieldGtk* textfield) {
return textfield->OnKeyPressEvent(event);
}
gboolean NativeTextfieldGtk::OnKeyPressEvent(GdkEventKey* event) {
TextfieldController* controller = textfield_->GetController();
if (controller) {
KeyEvent key_event(reinterpret_cast<GdkEvent*>(event));
return controller->HandleKeyEvent(textfield_, key_event);
}
return false;
}
// static
gboolean NativeTextfieldGtk::OnActivateHandler(
GtkWidget* widget,
NativeTextfieldGtk* textfield) {
return textfield->OnActivate();
}
gboolean NativeTextfieldGtk::OnActivate() {
GdkEvent* event = gtk_get_current_event();
if (!event || event->type != GDK_KEY_PRESS)
return false;
GdkEventKey* key_event = reinterpret_cast<GdkEventKey*>(event);
gboolean handled = false;
TextfieldController* controller = textfield_->GetController();
if (controller) {
KeyEvent views_key_event(event);
handled = controller->HandleKeyEvent(textfield_, views_key_event);
}
WidgetGtk* widget = static_cast<WidgetGtk*>(GetWidget());
if (!handled && widget)
handled = widget->HandleKeyboardEvent(key_event);
return handled;
}
// static
gboolean NativeTextfieldGtk::OnChangedHandler(
GtkWidget* widget,
NativeTextfieldGtk* textfield) {
return textfield->OnChanged();
}
gboolean NativeTextfieldGtk::OnChanged() {
textfield_->SyncText();
TextfieldController* controller = textfield_->GetController();
if (controller)
controller->ContentsChanged(textfield_, GetText());
textfield_->GetWidget()->NotifyAccessibilityEvent(
textfield_, ui::AccessibilityTypes::EVENT_TEXT_CHANGED, true);
return false;
}
// static
gboolean NativeTextfieldGtk::OnMoveCursorHandler(
GtkWidget* widget,
GtkMovementStep step,
gint count,
gboolean extend_selection,
NativeTextfieldGtk* textfield) {
return textfield->OnMoveCursor();
}
gboolean NativeTextfieldGtk::OnMoveCursor() {
textfield_->GetWidget()->NotifyAccessibilityEvent(
textfield_, ui::AccessibilityTypes::EVENT_TEXT_CHANGED, true);
return false;
}
// static
gboolean NativeTextfieldGtk::OnMouseUpHandler(
GtkWidget* widget,
GdkEvent* event,
NativeTextfieldGtk* textfield) {
return textfield->OnMouseUp();
}
gboolean NativeTextfieldGtk::OnMouseUp() {
textfield_->GetWidget()->NotifyAccessibilityEvent(
textfield_, ui::AccessibilityTypes::EVENT_TEXT_CHANGED, true);
return false;
}
////////////////////////////////////////////////////////////////////////////////
// NativeTextfieldGtk, NativeControlGtk overrides:
void NativeTextfieldGtk::CreateNativeControl() {
if (textfield_->IsMultiLine()) {
NativeControlCreated(gtk_views_textview_new(this));
if (textfield_->draw_border())
gtk_container_set_border_width(GTK_CONTAINER(native_view()),
kTextViewBorderWidth);
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(native_view()),
GTK_WRAP_WORD_CHAR);
} else {
NativeControlCreated(gtk_views_entry_new(this));
gtk_entry_set_invisible_char(GTK_ENTRY(native_view()),
static_cast<gunichar>(kPasswordChar));
}
textfield_->UpdateAllProperties();
}
void NativeTextfieldGtk::NativeControlCreated(GtkWidget* widget) {
NativeControlGtk::NativeControlCreated(widget);
if (GTK_IS_TEXT_VIEW(widget)) {
GtkTextBuffer* text_buffer = gtk_text_view_get_buffer(
GTK_TEXT_VIEW(widget));
g_signal_connect(text_buffer, "changed",
G_CALLBACK(OnChangedHandler), this);
} else {
g_signal_connect(widget, "changed",
G_CALLBACK(OnChangedHandler), this);
}
g_signal_connect_after(widget, "move-cursor",
G_CALLBACK(OnMoveCursorHandler), this);
g_signal_connect_after(widget, "button-release-event",
G_CALLBACK(OnMouseUpHandler), this);
g_signal_connect_after(widget, "key-press-event",
G_CALLBACK(OnKeyPressEventHandler), this);
// In order to properly trigger Accelerators bound to VKEY_RETURN, we need to
// send an event when the widget gets the activate signal.
g_signal_connect(widget, "activate", G_CALLBACK(OnActivateHandler), this);
}
bool NativeTextfieldGtk::IsPassword() {
return textfield_->IsPassword();
}
///////////////////////////////////////////////////////////////////////////////
// NativeTextfieldWrapper:
// static
NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper(
Textfield* field) {
if (NativeTextfieldViews::IsTextfieldViewsEnabled()) {
return new NativeTextfieldViews(field);
}
return new NativeTextfieldGtk(field);
}
} // namespace views
|