OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include <gdk/gdkkeysyms.h> | 5 #include <gdk/gdkkeysyms.h> |
6 #include <gtk/gtk.h> | 6 #include <gtk/gtk.h> |
7 | 7 |
8 #include "views/controls/textfield/native_textfield_gtk.h" | 8 #include "views/controls/textfield/native_textfield_gtk.h" |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 // Border width for GtkTextView. | 28 // Border width for GtkTextView. |
29 const int kTextViewBorderWidth = 4; | 29 const int kTextViewBorderWidth = 4; |
30 | 30 |
31 //////////////////////////////////////////////////////////////////////////////// | 31 //////////////////////////////////////////////////////////////////////////////// |
32 // NativeTextfieldGtk, public: | 32 // NativeTextfieldGtk, public: |
33 | 33 |
34 NativeTextfieldGtk::NativeTextfieldGtk(Textfield* textfield) | 34 NativeTextfieldGtk::NativeTextfieldGtk(Textfield* textfield) |
35 : textfield_(textfield), | 35 : textfield_(textfield), |
36 paste_clipboard_requested_(false) { | 36 paste_clipboard_requested_(false) { |
37 if (textfield_->IsMultiLine() && textfield_->IsPassword()) | |
38 NOTIMPLEMENTED(); // We don't support multiline password yet. | |
39 // Make |textfield| the focused view, so that when we get focused the focus | 37 // Make |textfield| the focused view, so that when we get focused the focus |
40 // manager sees |textfield| as the focused view (since we are just a wrapper | 38 // manager sees |textfield| as the focused view (since we are just a wrapper |
41 // view). | 39 // view). |
42 set_focus_view(textfield); | 40 set_focus_view(textfield); |
43 } | 41 } |
44 | 42 |
45 NativeTextfieldGtk::~NativeTextfieldGtk() { | 43 NativeTextfieldGtk::~NativeTextfieldGtk() { |
46 } | 44 } |
47 | 45 |
48 // Returns the inner border of an entry. | 46 // Returns the inner border of an entry. |
(...skipping 18 matching lines...) Expand all Loading... |
67 | 65 |
68 gfx::Insets NativeTextfieldGtk::GetTextViewInnerBorder(GtkTextView* text_view) { | 66 gfx::Insets NativeTextfieldGtk::GetTextViewInnerBorder(GtkTextView* text_view) { |
69 return gfx::Insets(kTextViewBorderWidth / 2, kTextViewBorderWidth / 2, | 67 return gfx::Insets(kTextViewBorderWidth / 2, kTextViewBorderWidth / 2, |
70 kTextViewBorderWidth / 2, kTextViewBorderWidth / 2); | 68 kTextViewBorderWidth / 2, kTextViewBorderWidth / 2); |
71 } | 69 } |
72 | 70 |
73 //////////////////////////////////////////////////////////////////////////////// | 71 //////////////////////////////////////////////////////////////////////////////// |
74 // NativeTextfieldGtk, NativeTextfieldWrapper implementation: | 72 // NativeTextfieldGtk, NativeTextfieldWrapper implementation: |
75 | 73 |
76 string16 NativeTextfieldGtk::GetText() const { | 74 string16 NativeTextfieldGtk::GetText() const { |
77 if (textfield_->IsMultiLine()) { | 75 return UTF8ToUTF16(gtk_entry_get_text(GTK_ENTRY(native_view()))); |
78 GtkTextBuffer* text_buffer = gtk_text_view_get_buffer( | |
79 GTK_TEXT_VIEW(native_view())); | |
80 | |
81 GtkTextIter start; | |
82 GtkTextIter end; | |
83 gtk_text_buffer_get_bounds(text_buffer, &start, &end); | |
84 | |
85 return UTF8ToUTF16(gtk_text_iter_get_visible_text(&start, &end)); | |
86 } else { | |
87 return UTF8ToUTF16(gtk_entry_get_text(GTK_ENTRY(native_view()))); | |
88 } | |
89 } | 76 } |
90 | 77 |
91 void NativeTextfieldGtk::UpdateText() { | 78 void NativeTextfieldGtk::UpdateText() { |
92 if (!native_view()) | 79 if (!native_view()) |
93 return; | 80 return; |
94 if (textfield_->IsMultiLine()) { | 81 gtk_entry_set_text(GTK_ENTRY(native_view()), |
95 GtkTextBuffer* text_buffer = gtk_text_view_get_buffer( | |
96 GTK_TEXT_VIEW(native_view())); | |
97 | |
98 std::string utf8 = UTF16ToUTF8(textfield_->text()); | |
99 gtk_text_buffer_set_text(text_buffer, utf8.c_str(), utf8.length()); | |
100 } else { | |
101 gtk_entry_set_text(GTK_ENTRY(native_view()), | |
102 UTF16ToUTF8(textfield_->text()).c_str()); | 82 UTF16ToUTF8(textfield_->text()).c_str()); |
103 } | |
104 } | 83 } |
105 | 84 |
106 void NativeTextfieldGtk::AppendText(const string16& text) { | 85 void NativeTextfieldGtk::AppendText(const string16& text) { |
107 if (!native_view()) | 86 if (!native_view()) |
108 return; | 87 return; |
109 if (textfield_->IsMultiLine()) { | 88 gtk_entry_append_text(GTK_ENTRY(native_view()), UTF16ToUTF8(text).c_str()); |
110 GtkTextBuffer* text_buffer = gtk_text_view_get_buffer( | |
111 GTK_TEXT_VIEW(native_view())); | |
112 | |
113 GtkTextIter end; | |
114 gtk_text_buffer_get_end_iter(text_buffer, &end); | |
115 | |
116 std::string utf8 = UTF16ToUTF8(text); | |
117 gtk_text_buffer_insert(text_buffer, &end, utf8.c_str(), utf8.length()); | |
118 } else { | |
119 gtk_entry_append_text(GTK_ENTRY(native_view()), UTF16ToUTF8(text).c_str()); | |
120 } | |
121 } | 89 } |
122 | 90 |
123 string16 NativeTextfieldGtk::GetSelectedText() const { | 91 string16 NativeTextfieldGtk::GetSelectedText() const { |
124 if (!native_view()) | 92 if (!native_view()) |
125 return string16(); | 93 return string16(); |
126 | 94 |
127 string16 result; | 95 string16 result; |
128 | 96 |
129 if (textfield_->IsMultiLine()) { | 97 gint start_pos; |
130 GtkTextBuffer* text_buffer = gtk_text_view_get_buffer( | 98 gint end_pos; |
131 GTK_TEXT_VIEW(native_view())); | 99 if (!gtk_editable_get_selection_bounds(GTK_EDITABLE(native_view()), |
| 100 &start_pos, &end_pos)) |
| 101 return result; // No selection. |
132 | 102 |
133 GtkTextIter start; | 103 UTF8ToUTF16(gtk_editable_get_chars(GTK_EDITABLE(native_view()), |
134 GtkTextIter end; | 104 start_pos, end_pos), |
135 if (gtk_text_buffer_get_selection_bounds(text_buffer, &start, &end)) { | 105 end_pos - start_pos, &result); |
136 gchar* selected_text = gtk_text_iter_get_visible_text(&start, &end); | |
137 if (selected_text) | |
138 UTF8ToUTF16(selected_text, strlen(selected_text), &result); | |
139 } | |
140 } else { | |
141 gint start_pos; | |
142 gint end_pos; | |
143 if (!gtk_editable_get_selection_bounds(GTK_EDITABLE(native_view()), | |
144 &start_pos, &end_pos)) | |
145 return result; // No selection. | |
146 | |
147 UTF8ToUTF16(gtk_editable_get_chars(GTK_EDITABLE(native_view()), | |
148 start_pos, end_pos), | |
149 end_pos - start_pos, &result); | |
150 } | |
151 | 106 |
152 return result; | 107 return result; |
153 } | 108 } |
154 | 109 |
155 void NativeTextfieldGtk::SelectAll() { | 110 void NativeTextfieldGtk::SelectAll() { |
156 if (!native_view()) | 111 if (!native_view()) |
157 return; | 112 return; |
158 if (textfield_->IsMultiLine()) { | 113 // -1 as the end position selects to the end of the text. |
159 GtkTextBuffer* text_buffer = gtk_text_view_get_buffer( | 114 gtk_editable_select_region(GTK_EDITABLE(native_view()), 0, -1); |
160 GTK_TEXT_VIEW(native_view())); | |
161 | |
162 GtkTextIter start; | |
163 GtkTextIter end; | |
164 gtk_text_buffer_get_bounds(text_buffer, &start, &end); | |
165 gtk_text_buffer_select_range(text_buffer, &start, &end); | |
166 } else { | |
167 // -1 as the end position selects to the end of the text. | |
168 gtk_editable_select_region(GTK_EDITABLE(native_view()), 0, -1); | |
169 } | |
170 } | 115 } |
171 | 116 |
172 void NativeTextfieldGtk::ClearSelection() { | 117 void NativeTextfieldGtk::ClearSelection() { |
173 if (!native_view()) | 118 if (!native_view()) |
174 return; | 119 return; |
175 if (textfield_->IsMultiLine()) { | 120 gtk_editable_select_region(GTK_EDITABLE(native_view()), 0, 0); |
176 GtkTextBuffer* text_buffer = gtk_text_view_get_buffer( | |
177 GTK_TEXT_VIEW(native_view())); | |
178 | |
179 GtkTextMark* insert_mark = gtk_text_buffer_get_insert(text_buffer); | |
180 GtkTextIter insert; | |
181 gtk_text_buffer_get_iter_at_mark(text_buffer, &insert, insert_mark); | |
182 gtk_text_buffer_select_range(text_buffer, &insert, &insert); | |
183 } else { | |
184 gtk_editable_select_region(GTK_EDITABLE(native_view()), 0, 0); | |
185 } | |
186 } | 121 } |
187 | 122 |
188 void NativeTextfieldGtk::UpdateBorder() { | 123 void NativeTextfieldGtk::UpdateBorder() { |
189 if (!native_view()) | 124 if (!native_view()) |
190 return; | 125 return; |
191 | 126 |
192 if (textfield_->IsMultiLine()) { | 127 if (!textfield_->draw_border()) |
193 if (!textfield_->draw_border()) { | 128 gtk_entry_set_has_frame(GTK_ENTRY(native_view()), false); |
194 gtk_container_set_border_width(GTK_CONTAINER(native_view()), 0); | |
195 | |
196 // Use margin to match entry with no border | |
197 textfield_->SetHorizontalMargins(kTextViewBorderWidth / 2 + 1, | |
198 kTextViewBorderWidth / 2 + 1); | |
199 } | |
200 } else { | |
201 if (!textfield_->draw_border()) | |
202 gtk_entry_set_has_frame(GTK_ENTRY(native_view()), false); | |
203 } | |
204 } | 129 } |
205 | 130 |
206 void NativeTextfieldGtk::UpdateTextColor() { | 131 void NativeTextfieldGtk::UpdateTextColor() { |
207 if (textfield_->use_default_text_color()) { | 132 if (textfield_->use_default_text_color()) { |
208 // Passing NULL as the color undoes the effect of previous calls to | 133 // Passing NULL as the color undoes the effect of previous calls to |
209 // gtk_widget_modify_text. | 134 // gtk_widget_modify_text. |
210 gtk_widget_modify_text(native_view(), GTK_STATE_NORMAL, NULL); | 135 gtk_widget_modify_text(native_view(), GTK_STATE_NORMAL, NULL); |
211 return; | 136 return; |
212 } | 137 } |
213 GdkColor gdk_color = gfx::SkColorToGdkColor(textfield_->text_color()); | 138 GdkColor gdk_color = gfx::SkColorToGdkColor(textfield_->text_color()); |
214 gtk_widget_modify_text(native_view(), GTK_STATE_NORMAL, &gdk_color); | 139 gtk_widget_modify_text(native_view(), GTK_STATE_NORMAL, &gdk_color); |
215 } | 140 } |
216 | 141 |
217 void NativeTextfieldGtk::UpdateBackgroundColor() { | 142 void NativeTextfieldGtk::UpdateBackgroundColor() { |
218 if (textfield_->use_default_background_color()) { | 143 if (textfield_->use_default_background_color()) { |
219 // Passing NULL as the color undoes the effect of previous calls to | 144 // Passing NULL as the color undoes the effect of previous calls to |
220 // gtk_widget_modify_base. | 145 // gtk_widget_modify_base. |
221 gtk_widget_modify_base(native_view(), GTK_STATE_NORMAL, NULL); | 146 gtk_widget_modify_base(native_view(), GTK_STATE_NORMAL, NULL); |
222 return; | 147 return; |
223 } | 148 } |
224 GdkColor gdk_color = gfx::SkColorToGdkColor(textfield_->background_color()); | 149 GdkColor gdk_color = gfx::SkColorToGdkColor(textfield_->background_color()); |
225 gtk_widget_modify_base(native_view(), GTK_STATE_NORMAL, &gdk_color); | 150 gtk_widget_modify_base(native_view(), GTK_STATE_NORMAL, &gdk_color); |
226 } | 151 } |
227 | 152 |
228 void NativeTextfieldGtk::UpdateReadOnly() { | 153 void NativeTextfieldGtk::UpdateReadOnly() { |
229 if (!native_view()) | 154 if (!native_view()) |
230 return; | 155 return; |
231 | 156 gtk_editable_set_editable(GTK_EDITABLE(native_view()), |
232 if (textfield_->IsMultiLine()) { | 157 !textfield_->read_only()); |
233 gtk_text_view_set_editable(GTK_TEXT_VIEW(native_view()), | |
234 !textfield_->read_only()); | |
235 } else { | |
236 gtk_editable_set_editable(GTK_EDITABLE(native_view()), | |
237 !textfield_->read_only()); | |
238 } | |
239 } | 158 } |
240 | 159 |
241 void NativeTextfieldGtk::UpdateFont() { | 160 void NativeTextfieldGtk::UpdateFont() { |
242 if (!native_view()) | 161 if (!native_view()) |
243 return; | 162 return; |
244 PangoFontDescription* pfd = textfield_->font().GetNativeFont(); | 163 PangoFontDescription* pfd = textfield_->font().GetNativeFont(); |
245 gtk_widget_modify_font(native_view(), pfd); | 164 gtk_widget_modify_font(native_view(), pfd); |
246 pango_font_description_free(pfd); | 165 pango_font_description_free(pfd); |
247 } | 166 } |
248 | 167 |
249 void NativeTextfieldGtk::UpdateIsPassword() { | 168 void NativeTextfieldGtk::UpdateIsPassword() { |
250 if (!native_view()) | 169 if (!native_view()) |
251 return; | 170 return; |
252 if (!textfield_->IsMultiLine()) { | 171 gtk_entry_set_visibility(GTK_ENTRY(native_view()), !textfield_->IsPassword()); |
253 gtk_entry_set_visibility(GTK_ENTRY(native_view()), | |
254 !textfield_->IsPassword()); | |
255 } | |
256 } | 172 } |
257 | 173 |
258 void NativeTextfieldGtk::UpdateEnabled() { | 174 void NativeTextfieldGtk::UpdateEnabled() { |
259 if (!native_view()) | 175 if (!native_view()) |
260 return; | 176 return; |
261 SetEnabled(textfield_->IsEnabled()); | 177 SetEnabled(textfield_->IsEnabled()); |
262 } | 178 } |
263 | 179 |
264 gfx::Insets NativeTextfieldGtk::CalculateInsets() { | 180 gfx::Insets NativeTextfieldGtk::CalculateInsets() { |
265 if (!native_view()) | 181 if (!native_view()) |
266 return gfx::Insets(); | 182 return gfx::Insets(); |
267 | 183 |
268 GtkWidget* widget = native_view(); | 184 GtkWidget* widget = native_view(); |
269 gfx::Insets insets; | 185 gfx::Insets insets; |
270 | 186 |
271 if (textfield_->IsMultiLine()) { | 187 GtkEntry* entry = GTK_ENTRY(widget); |
272 insets += GetTextViewInnerBorder(GTK_TEXT_VIEW(widget)); | 188 insets += GetEntryInnerBorder(entry); |
273 } else { | 189 if (entry->has_frame) { |
274 GtkEntry* entry = GTK_ENTRY(widget); | 190 insets += gfx::Insets(widget->style->ythickness, |
275 insets += GetEntryInnerBorder(entry); | |
276 if (entry->has_frame) { | |
277 insets += gfx::Insets(widget->style->ythickness, | |
278 widget->style->xthickness, | 191 widget->style->xthickness, |
279 widget->style->ythickness, | 192 widget->style->ythickness, |
280 widget->style->xthickness); | 193 widget->style->xthickness); |
281 } | |
282 } | 194 } |
283 | 195 |
284 gboolean interior_focus; | 196 gboolean interior_focus; |
285 gint focus_width; | 197 gint focus_width; |
286 gtk_widget_style_get(widget, | 198 gtk_widget_style_get(widget, |
287 "focus-line-width", &focus_width, | 199 "focus-line-width", &focus_width, |
288 "interior-focus", &interior_focus, | 200 "interior-focus", &interior_focus, |
289 NULL); | 201 NULL); |
290 if (!interior_focus) | 202 if (!interior_focus) |
291 insets += gfx::Insets(focus_width, focus_width, focus_width, focus_width); | 203 insets += gfx::Insets(focus_width, focus_width, focus_width, focus_width); |
292 | 204 |
293 return insets; | 205 return insets; |
294 } | 206 } |
295 | 207 |
296 void NativeTextfieldGtk::UpdateHorizontalMargins() { | 208 void NativeTextfieldGtk::UpdateHorizontalMargins() { |
297 if (!native_view()) | 209 if (!native_view()) |
298 return; | 210 return; |
299 | 211 |
300 int left, right; | 212 int left, right; |
301 if (!textfield_->GetHorizontalMargins(&left, &right)) | 213 if (!textfield_->GetHorizontalMargins(&left, &right)) |
302 return; | 214 return; |
303 | 215 |
304 if (textfield_->IsMultiLine()) { | 216 gfx::Insets insets = GetEntryInnerBorder(GTK_ENTRY(native_view())); |
305 GtkTextView* text_view = GTK_TEXT_VIEW(native_view()); | 217 GtkBorder border = {left, right, insets.top(), insets.bottom()}; |
306 gtk_text_view_set_left_margin(text_view, left); | 218 gtk_entry_set_inner_border(GTK_ENTRY(native_view()), &border); |
307 gtk_text_view_set_right_margin(text_view, right); | |
308 } else { | |
309 gfx::Insets insets = GetEntryInnerBorder(GTK_ENTRY(native_view())); | |
310 GtkBorder border = {left, right, insets.top(), insets.bottom()}; | |
311 gtk_entry_set_inner_border(GTK_ENTRY(native_view()), &border); | |
312 } | |
313 } | 219 } |
314 | 220 |
315 void NativeTextfieldGtk::UpdateVerticalMargins() { | 221 void NativeTextfieldGtk::UpdateVerticalMargins() { |
316 if (!native_view()) | 222 if (!native_view()) |
317 return; | 223 return; |
318 | 224 |
319 int top, bottom; | 225 int top, bottom; |
320 if (!textfield_->GetVerticalMargins(&top, &bottom)) | 226 if (!textfield_->GetVerticalMargins(&top, &bottom)) |
321 return; | 227 return; |
322 | 228 |
323 if (!textfield_->IsMultiLine()) { | 229 gfx::Insets insets = GetEntryInnerBorder(GTK_ENTRY(native_view())); |
324 gfx::Insets insets = GetEntryInnerBorder(GTK_ENTRY(native_view())); | 230 GtkBorder border = {insets.left(), insets.right(), top, bottom}; |
325 GtkBorder border = {insets.left(), insets.right(), top, bottom}; | 231 gtk_entry_set_inner_border(GTK_ENTRY(native_view()), &border); |
326 gtk_entry_set_inner_border(GTK_ENTRY(native_view()), &border); | |
327 } else { | |
328 NOTIMPLEMENTED(); | |
329 } | |
330 } | 232 } |
331 | 233 |
332 bool NativeTextfieldGtk::SetFocus() { | 234 bool NativeTextfieldGtk::SetFocus() { |
333 OnFocus(); | 235 OnFocus(); |
334 return true; | 236 return true; |
335 } | 237 } |
336 | 238 |
337 View* NativeTextfieldGtk::GetView() { | 239 View* NativeTextfieldGtk::GetView() { |
338 return this; | 240 return this; |
339 } | 241 } |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 | 381 |
480 void NativeTextfieldGtk::OnPasteClipboard(GtkWidget* widget) { | 382 void NativeTextfieldGtk::OnPasteClipboard(GtkWidget* widget) { |
481 if (!textfield_->read_only()) | 383 if (!textfield_->read_only()) |
482 paste_clipboard_requested_ = true; | 384 paste_clipboard_requested_ = true; |
483 } | 385 } |
484 | 386 |
485 //////////////////////////////////////////////////////////////////////////////// | 387 //////////////////////////////////////////////////////////////////////////////// |
486 // NativeTextfieldGtk, NativeControlGtk overrides: | 388 // NativeTextfieldGtk, NativeControlGtk overrides: |
487 | 389 |
488 void NativeTextfieldGtk::CreateNativeControl() { | 390 void NativeTextfieldGtk::CreateNativeControl() { |
489 if (textfield_->IsMultiLine()) { | 391 NativeControlCreated(gtk_views_entry_new(this)); |
490 NativeControlCreated(gtk_views_textview_new(this)); | 392 gtk_entry_set_invisible_char(GTK_ENTRY(native_view()), |
491 if (textfield_->draw_border()) | |
492 gtk_container_set_border_width(GTK_CONTAINER(native_view()), | |
493 kTextViewBorderWidth); | |
494 | |
495 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(native_view()), | |
496 GTK_WRAP_WORD_CHAR); | |
497 } else { | |
498 NativeControlCreated(gtk_views_entry_new(this)); | |
499 gtk_entry_set_invisible_char(GTK_ENTRY(native_view()), | |
500 static_cast<gunichar>(kPasswordChar)); | 393 static_cast<gunichar>(kPasswordChar)); |
501 } | |
502 textfield_->UpdateAllProperties(); | 394 textfield_->UpdateAllProperties(); |
503 } | 395 } |
504 | 396 |
505 void NativeTextfieldGtk::NativeControlCreated(GtkWidget* widget) { | 397 void NativeTextfieldGtk::NativeControlCreated(GtkWidget* widget) { |
506 NativeControlGtk::NativeControlCreated(widget); | 398 NativeControlGtk::NativeControlCreated(widget); |
507 | 399 |
508 if (GTK_IS_TEXT_VIEW(widget)) { | 400 if (GTK_IS_TEXT_VIEW(widget)) { |
509 GtkTextBuffer* text_buffer = gtk_text_view_get_buffer( | 401 GtkTextBuffer* text_buffer = gtk_text_view_get_buffer( |
510 GTK_TEXT_VIEW(widget)); | 402 GTK_TEXT_VIEW(widget)); |
511 g_signal_connect(text_buffer, "changed", G_CALLBACK(OnChangedThunk), this); | 403 g_signal_connect(text_buffer, "changed", G_CALLBACK(OnChangedThunk), this); |
(...skipping 27 matching lines...) Expand all Loading... |
539 // static | 431 // static |
540 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( | 432 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( |
541 Textfield* field) { | 433 Textfield* field) { |
542 if (NativeTextfieldViews::IsTextfieldViewsEnabled()) { | 434 if (NativeTextfieldViews::IsTextfieldViewsEnabled()) { |
543 return new NativeTextfieldViews(field); | 435 return new NativeTextfieldViews(field); |
544 } | 436 } |
545 return new NativeTextfieldGtk(field); | 437 return new NativeTextfieldGtk(field); |
546 } | 438 } |
547 | 439 |
548 } // namespace views | 440 } // namespace views |
OLD | NEW |