Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(444)

Side by Side Diff: chrome/browser/autocomplete/autocomplete_edit_view_gtk.cc

Issue 194056: Fix text and selection's save/restore issue of omnibox when displaying tempor... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 11 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 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 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 "chrome/browser/autocomplete/autocomplete_edit_view_gtk.h" 5 #include "chrome/browser/autocomplete/autocomplete_edit_view_gtk.h"
6 6
7 #include <gtk/gtk.h> 7 #include <gtk/gtk.h>
8 #include <gdk/gdkkeysyms.h> 8 #include <gdk/gdkkeysyms.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 UpdatePopup(); 331 UpdatePopup();
332 TextChanged(); 332 TextChanged();
333 } 333 }
334 334
335 void AutocompleteEditViewGtk::SetWindowTextAndCaretPos(const std::wstring& text, 335 void AutocompleteEditViewGtk::SetWindowTextAndCaretPos(const std::wstring& text,
336 size_t caret_pos) { 336 size_t caret_pos) {
337 CharRange range(static_cast<int>(caret_pos), static_cast<int>(caret_pos)); 337 CharRange range(static_cast<int>(caret_pos), static_cast<int>(caret_pos));
338 SetTextAndSelectedRange(text, range); 338 SetTextAndSelectedRange(text, range);
339 } 339 }
340 340
341 void AutocompleteEditViewGtk::SetTextAndSelectedRange(const std::wstring& text,
342 const CharRange& range) {
343 std::string utf8 = WideToUTF8(text);
344 gtk_text_buffer_set_text(text_buffer_, utf8.data(), utf8.length());
345
346 GtkTextIter insert, bound;
347 ItersFromCharRange(range, &insert, &bound);
348 gtk_text_buffer_select_range(text_buffer_, &insert, &bound);
349 }
350
351 void AutocompleteEditViewGtk::SetForcedQuery() { 341 void AutocompleteEditViewGtk::SetForcedQuery() {
352 const std::wstring current_text(GetText()); 342 const std::wstring current_text(GetText());
353 if (current_text.empty() || (current_text[0] != '?')) { 343 if (current_text.empty() || (current_text[0] != '?')) {
354 SetUserText(L"?"); 344 SetUserText(L"?");
355 } else { 345 } else {
356 GtkTextIter start, end;
357 gtk_text_buffer_get_bounds(text_buffer_, &start, &end);
358 gtk_text_buffer_get_iter_at_offset(text_buffer_, &start, 1);
359 StartUpdatingHighlightedText(); 346 StartUpdatingHighlightedText();
360 gtk_text_buffer_select_range(text_buffer_, &start, &end); 347 SetSelectedRange(CharRange(current_text.size(), 1));
361 FinishUpdatingHighlightedText(); 348 FinishUpdatingHighlightedText();
362 } 349 }
363 } 350 }
364 351
365 bool AutocompleteEditViewGtk::IsSelectAll() { 352 bool AutocompleteEditViewGtk::IsSelectAll() {
366 GtkTextIter sel_start, sel_end; 353 GtkTextIter sel_start, sel_end;
367 gtk_text_buffer_get_selection_bounds(text_buffer_, &sel_start, &sel_end); 354 gtk_text_buffer_get_selection_bounds(text_buffer_, &sel_start, &sel_end);
368 355
369 GtkTextIter start, end; 356 GtkTextIter start, end;
370 gtk_text_buffer_get_bounds(text_buffer_, &start, &end); 357 gtk_text_buffer_get_bounds(text_buffer_, &start, &end);
(...skipping 29 matching lines...) Expand all
400 model_->StartAutocomplete(std::max(sel.cp_max, sel.cp_min) < GetTextLength()); 387 model_->StartAutocomplete(std::max(sel.cp_max, sel.cp_min) < GetTextLength());
401 } 388 }
402 389
403 void AutocompleteEditViewGtk::ClosePopup() { 390 void AutocompleteEditViewGtk::ClosePopup() {
404 popup_view_->GetModel()->StopAutocomplete(); 391 popup_view_->GetModel()->StopAutocomplete();
405 } 392 }
406 393
407 void AutocompleteEditViewGtk::OnTemporaryTextMaybeChanged( 394 void AutocompleteEditViewGtk::OnTemporaryTextMaybeChanged(
408 const std::wstring& display_text, 395 const std::wstring& display_text,
409 bool save_original_selection) { 396 bool save_original_selection) {
410 if (save_original_selection) { 397 if (save_original_selection)
411 saved_temporary_selection_ = GetSelection(); 398 saved_temporary_selection_ = GetSelection();
412 saved_temporary_text_ = GetText();
413 }
414 399
415 StartUpdatingHighlightedText(); 400 StartUpdatingHighlightedText();
416 SetWindowTextAndCaretPos(display_text, display_text.length()); 401 SetWindowTextAndCaretPos(display_text, display_text.length());
417 FinishUpdatingHighlightedText(); 402 FinishUpdatingHighlightedText();
418 TextChanged(); 403 TextChanged();
419 } 404 }
420 405
421 bool AutocompleteEditViewGtk::OnInlineAutocompleteTextMaybeChanged( 406 bool AutocompleteEditViewGtk::OnInlineAutocompleteTextMaybeChanged(
422 const std::wstring& display_text, 407 const std::wstring& display_text,
423 size_t user_text_length) { 408 size_t user_text_length) {
424 if (display_text == GetText()) 409 if (display_text == GetText())
425 return false; 410 return false;
426 411
427 StartUpdatingHighlightedText(); 412 StartUpdatingHighlightedText();
428 SetWindowTextAndCaretPos(display_text, 0); 413 CharRange range(display_text.size(), user_text_length);
429 414 SetTextAndSelectedRange(display_text, range);
430 // Select the part of the text that was inline autocompleted.
431 GtkTextIter bound, insert;
432 gtk_text_buffer_get_bounds(text_buffer_, &insert, &bound);
433 gtk_text_buffer_get_iter_at_offset(text_buffer_, &insert, user_text_length);
434 gtk_text_buffer_select_range(text_buffer_, &insert, &bound);
435
436 FinishUpdatingHighlightedText(); 415 FinishUpdatingHighlightedText();
437 TextChanged(); 416 TextChanged();
438 return true; 417 return true;
439 } 418 }
440 419
441 void AutocompleteEditViewGtk::OnRevertTemporaryText() { 420 void AutocompleteEditViewGtk::OnRevertTemporaryText() {
442 StartUpdatingHighlightedText(); 421 StartUpdatingHighlightedText();
443 SetTextAndSelectedRange(saved_temporary_text_, saved_temporary_selection_); 422 SetSelectedRange(saved_temporary_selection_);
444 FinishUpdatingHighlightedText(); 423 FinishUpdatingHighlightedText();
445 saved_temporary_text_.clear();
446 TextChanged(); 424 TextChanged();
447 } 425 }
448 426
449 void AutocompleteEditViewGtk::OnBeforePossibleChange() { 427 void AutocompleteEditViewGtk::OnBeforePossibleChange() {
450 // If this change is caused by a paste clipboard action and all text is 428 // If this change is caused by a paste clipboard action and all text is
451 // selected, then call model_->on_paste_replacing_all() to prevent inline 429 // selected, then call model_->on_paste_replacing_all() to prevent inline
452 // autocomplete. 430 // autocomplete.
453 if (paste_clipboard_requested_) { 431 if (paste_clipboard_requested_) {
454 paste_clipboard_requested_ = false; 432 paste_clipboard_requested_ = false;
455 if (IsSelectAll()) 433 if (IsSelectAll())
(...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after
1168 const std::string& selected_text) { 1146 const std::string& selected_text) {
1169 GtkClipboard* clipboard = 1147 GtkClipboard* clipboard =
1170 gtk_widget_get_clipboard(text_view_, GDK_SELECTION_PRIMARY); 1148 gtk_widget_get_clipboard(text_view_, GDK_SELECTION_PRIMARY);
1171 DCHECK(clipboard); 1149 DCHECK(clipboard);
1172 if (!clipboard) 1150 if (!clipboard)
1173 return; 1151 return;
1174 1152
1175 gtk_clipboard_set_text( 1153 gtk_clipboard_set_text(
1176 clipboard, selected_text.data(), selected_text.size()); 1154 clipboard, selected_text.data(), selected_text.size());
1177 } 1155 }
1156
1157 void AutocompleteEditViewGtk::SetTextAndSelectedRange(const std::wstring& text,
1158 const CharRange& range) {
1159 std::string utf8 = WideToUTF8(text);
1160 gtk_text_buffer_set_text(text_buffer_, utf8.data(), utf8.length());
1161 SetSelectedRange(range);
1162 }
1163
1164 void AutocompleteEditViewGtk::SetSelectedRange(const CharRange& range) {
1165 GtkTextIter insert, bound;
1166 ItersFromCharRange(range, &bound, &insert);
1167 gtk_text_buffer_select_range(text_buffer_, &insert, &bound);
1168 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698