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

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

Issue 6306011: Remove wstring from autocomplete. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 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) 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 "chrome/browser/autocomplete/autocomplete_edit_view_gtk.h" 5 #include "chrome/browser/autocomplete/autocomplete_edit_view_gtk.h"
6 6
7 #include <gdk/gdkkeysyms.h> 7 #include <gdk/gdkkeysyms.h>
8 #include <gtk/gtk.h> 8 #include <gtk/gtk.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 54
55 const gchar* kAutocompleteEditViewGtkKey = "__ACE_VIEW_GTK__"; 55 const gchar* kAutocompleteEditViewGtkKey = "__ACE_VIEW_GTK__";
56 56
57 const char kTextBaseColor[] = "#808080"; 57 const char kTextBaseColor[] = "#808080";
58 const char kSecureSchemeColor[] = "#079500"; 58 const char kSecureSchemeColor[] = "#079500";
59 const char kSecurityErrorSchemeColor[] = "#a20000"; 59 const char kSecurityErrorSchemeColor[] = "#a20000";
60 60
61 const double kStrikethroughStrokeRed = 162.0 / 256.0; 61 const double kStrikethroughStrokeRed = 162.0 / 256.0;
62 const double kStrikethroughStrokeWidth = 2.0; 62 const double kStrikethroughStrokeWidth = 2.0;
63 63
64 size_t GetUTF8Offset(const std::wstring& wide_text, size_t wide_text_offset) { 64 size_t GetUTF8Offset(const string16& text, size_t text_offset) {
65 return WideToUTF8(wide_text.substr(0, wide_text_offset)).size(); 65 return UTF16ToUTF8(text.substr(0, text_offset)).size();
66 } 66 }
67 67
68 // Stores GTK+-specific state so it can be restored after switching tabs. 68 // Stores GTK+-specific state so it can be restored after switching tabs.
69 struct ViewState { 69 struct ViewState {
70 explicit ViewState(const AutocompleteEditViewGtk::CharRange& selection_range) 70 explicit ViewState(const AutocompleteEditViewGtk::CharRange& selection_range)
71 : selection_range(selection_range) { 71 : selection_range(selection_range) {
72 } 72 }
73 73
74 // Range of selected text. 74 // Range of selected text.
75 AutocompleteEditViewGtk::CharRange selection_range; 75 AutocompleteEditViewGtk::CharRange selection_range;
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 // NOTE: GetStateForTabSwitch may affect GetSelection, so order is important. 449 // NOTE: GetStateForTabSwitch may affect GetSelection, so order is important.
450 AutocompleteEditModel::State model_state = model_->GetStateForTabSwitch(); 450 AutocompleteEditModel::State model_state = model_->GetStateForTabSwitch();
451 GetStateAccessor()->SetProperty( 451 GetStateAccessor()->SetProperty(
452 tab->property_bag(), 452 tab->property_bag(),
453 AutocompleteEditState(model_state, ViewState(GetSelection()))); 453 AutocompleteEditState(model_state, ViewState(GetSelection())));
454 } 454 }
455 455
456 void AutocompleteEditViewGtk::Update(const TabContents* contents) { 456 void AutocompleteEditViewGtk::Update(const TabContents* contents) {
457 // NOTE: We're getting the URL text here from the ToolbarModel. 457 // NOTE: We're getting the URL text here from the ToolbarModel.
458 bool visibly_changed_permanent_text = 458 bool visibly_changed_permanent_text =
459 model_->UpdatePermanentText(toolbar_model_->GetText()); 459 model_->UpdatePermanentText(WideToUTF16Hack(toolbar_model_->GetText()));
460 460
461 ToolbarModel::SecurityLevel security_level = 461 ToolbarModel::SecurityLevel security_level =
462 toolbar_model_->GetSecurityLevel(); 462 toolbar_model_->GetSecurityLevel();
463 bool changed_security_level = (security_level != security_level_); 463 bool changed_security_level = (security_level != security_level_);
464 security_level_ = security_level; 464 security_level_ = security_level;
465 465
466 if (contents) { 466 if (contents) {
467 selected_text_.clear(); 467 selected_text_.clear();
468 RevertAll(); 468 RevertAll();
469 const AutocompleteEditState* state = 469 const AutocompleteEditState* state =
(...skipping 13 matching lines...) Expand all
483 } else if (changed_security_level) { 483 } else if (changed_security_level) {
484 EmphasizeURLComponents(); 484 EmphasizeURLComponents();
485 } 485 }
486 } 486 }
487 487
488 void AutocompleteEditViewGtk::OpenURL(const GURL& url, 488 void AutocompleteEditViewGtk::OpenURL(const GURL& url,
489 WindowOpenDisposition disposition, 489 WindowOpenDisposition disposition,
490 PageTransition::Type transition, 490 PageTransition::Type transition,
491 const GURL& alternate_nav_url, 491 const GURL& alternate_nav_url,
492 size_t selected_line, 492 size_t selected_line,
493 const std::wstring& keyword) { 493 const string16& keyword) {
494 if (!url.is_valid()) 494 if (!url.is_valid())
495 return; 495 return;
496 496
497 model_->OpenURL(url, disposition, transition, alternate_nav_url, 497 model_->OpenURL(url, disposition, transition, alternate_nav_url,
498 selected_line, keyword); 498 selected_line, keyword);
499 } 499 }
500 500
501 std::wstring AutocompleteEditViewGtk::GetText() const { 501 string16 AutocompleteEditViewGtk::GetText() const {
502 GtkTextIter start, end; 502 GtkTextIter start, end;
503 GetTextBufferBounds(&start, &end); 503 GetTextBufferBounds(&start, &end);
504 gchar* utf8 = gtk_text_buffer_get_text(text_buffer_, &start, &end, false); 504 gchar* utf8 = gtk_text_buffer_get_text(text_buffer_, &start, &end, false);
505 std::wstring out(UTF8ToWide(utf8)); 505 string16 out(UTF8ToUTF16(utf8));
506 g_free(utf8); 506 g_free(utf8);
507 507
508 #if GTK_CHECK_VERSION(2, 20, 0) 508 #if GTK_CHECK_VERSION(2, 20, 0)
509 // We need to treat the text currently being composed by the input method as 509 // We need to treat the text currently being composed by the input method as
510 // part of the text content, so that omnibox can work correctly in the middle 510 // part of the text content, so that omnibox can work correctly in the middle
511 // of composition. 511 // of composition.
512 if (preedit_.size()) { 512 if (preedit_.size()) {
513 GtkTextMark* mark = gtk_text_buffer_get_insert(text_buffer_); 513 GtkTextMark* mark = gtk_text_buffer_get_insert(text_buffer_);
514 gtk_text_buffer_get_iter_at_mark(text_buffer_, &start, mark); 514 gtk_text_buffer_get_iter_at_mark(text_buffer_, &start, mark);
515 out.insert(gtk_text_iter_get_offset(&start), preedit_); 515 out.insert(gtk_text_iter_get_offset(&start), preedit_);
516 } 516 }
517 #endif 517 #endif
518 return out; 518 return out;
519 } 519 }
520 520
521 bool AutocompleteEditViewGtk::IsEditingOrEmpty() const { 521 bool AutocompleteEditViewGtk::IsEditingOrEmpty() const {
522 return model_->user_input_in_progress() || (GetTextLength() == 0); 522 return model_->user_input_in_progress() || (GetTextLength() == 0);
523 } 523 }
524 524
525 int AutocompleteEditViewGtk::GetIcon() const { 525 int AutocompleteEditViewGtk::GetIcon() const {
526 return IsEditingOrEmpty() ? 526 return IsEditingOrEmpty() ?
527 AutocompleteMatch::TypeToIcon(model_->CurrentTextType()) : 527 AutocompleteMatch::TypeToIcon(model_->CurrentTextType()) :
528 toolbar_model_->GetIcon(); 528 toolbar_model_->GetIcon();
529 } 529 }
530 530
531 void AutocompleteEditViewGtk::SetUserText(const std::wstring& text) { 531 void AutocompleteEditViewGtk::SetUserText(const string16& text) {
532 SetUserText(text, text, true); 532 SetUserText(text, text, true);
533 } 533 }
534 534
535 void AutocompleteEditViewGtk::SetUserText(const std::wstring& text, 535 void AutocompleteEditViewGtk::SetUserText(const string16& text,
536 const std::wstring& display_text, 536 const string16& display_text,
537 bool update_popup) { 537 bool update_popup) {
538 model_->SetUserText(text); 538 model_->SetUserText(text);
539 // TODO(deanm): something about selection / focus change here. 539 // TODO(deanm): something about selection / focus change here.
540 SetWindowTextAndCaretPos(display_text, display_text.length()); 540 SetWindowTextAndCaretPos(display_text, display_text.length());
541 if (update_popup) 541 if (update_popup)
542 UpdatePopup(); 542 UpdatePopup();
543 TextChanged(); 543 TextChanged();
544 } 544 }
545 545
546 void AutocompleteEditViewGtk::SetWindowTextAndCaretPos(const std::wstring& text, 546 void AutocompleteEditViewGtk::SetWindowTextAndCaretPos(const string16& text,
547 size_t caret_pos) { 547 size_t caret_pos) {
548 CharRange range(static_cast<int>(caret_pos), static_cast<int>(caret_pos)); 548 CharRange range(static_cast<int>(caret_pos), static_cast<int>(caret_pos));
549 SetTextAndSelectedRange(text, range); 549 SetTextAndSelectedRange(text, range);
550 } 550 }
551 551
552 void AutocompleteEditViewGtk::SetForcedQuery() { 552 void AutocompleteEditViewGtk::SetForcedQuery() {
553 const std::wstring current_text(GetText()); 553 const string16 current_text(GetText());
554 const size_t start = current_text.find_first_not_of(kWhitespaceWide); 554 const size_t start = current_text.find_first_not_of(kWhitespaceUTF16);
555 if (start == std::wstring::npos || (current_text[start] != '?')) { 555 if (start == string16::npos || (current_text[start] != '?')) {
556 SetUserText(L"?"); 556 SetUserText(ASCIIToUTF16("?"));
557 } else { 557 } else {
558 StartUpdatingHighlightedText(); 558 StartUpdatingHighlightedText();
559 SetSelectedRange(CharRange(current_text.size(), start + 1)); 559 SetSelectedRange(CharRange(current_text.size(), start + 1));
560 FinishUpdatingHighlightedText(); 560 FinishUpdatingHighlightedText();
561 } 561 }
562 } 562 }
563 563
564 bool AutocompleteEditViewGtk::IsSelectAll() { 564 bool AutocompleteEditViewGtk::IsSelectAll() {
565 GtkTextIter sel_start, sel_end; 565 GtkTextIter sel_start, sel_end;
566 gtk_text_buffer_get_selection_bounds(text_buffer_, &sel_start, &sel_end); 566 gtk_text_buffer_get_selection_bounds(text_buffer_, &sel_start, &sel_end);
567 567
568 GtkTextIter start, end; 568 GtkTextIter start, end;
569 GetTextBufferBounds(&start, &end); 569 GetTextBufferBounds(&start, &end);
570 570
571 // Returns true if the |text_buffer_| is empty. 571 // Returns true if the |text_buffer_| is empty.
572 return gtk_text_iter_equal(&start, &sel_start) && 572 return gtk_text_iter_equal(&start, &sel_start) &&
573 gtk_text_iter_equal(&end, &sel_end); 573 gtk_text_iter_equal(&end, &sel_end);
574 } 574 }
575 575
576 bool AutocompleteEditViewGtk::DeleteAtEndPressed() { 576 bool AutocompleteEditViewGtk::DeleteAtEndPressed() {
577 return delete_at_end_pressed_; 577 return delete_at_end_pressed_;
578 } 578 }
579 579
580 void AutocompleteEditViewGtk::GetSelectionBounds(std::wstring::size_type* start, 580 void AutocompleteEditViewGtk::GetSelectionBounds(string16::size_type* start,
581 std::wstring::size_type* end) { 581 string16::size_type* end) {
582 CharRange selection = GetSelection(); 582 CharRange selection = GetSelection();
583 *start = static_cast<size_t>(selection.cp_min); 583 *start = static_cast<size_t>(selection.cp_min);
584 *end = static_cast<size_t>(selection.cp_max); 584 *end = static_cast<size_t>(selection.cp_max);
585 } 585 }
586 586
587 void AutocompleteEditViewGtk::SelectAll(bool reversed) { 587 void AutocompleteEditViewGtk::SelectAll(bool reversed) {
588 // SelectAll() is invoked as a side effect of other actions (e.g. switching 588 // SelectAll() is invoked as a side effect of other actions (e.g. switching
589 // tabs or hitting Escape) in autocomplete_edit.cc, so we don't update the 589 // tabs or hitting Escape) in autocomplete_edit.cc, so we don't update the
590 // PRIMARY selection here. 590 // PRIMARY selection here.
591 SelectAllInternal(reversed, false); 591 SelectAllInternal(reversed, false);
(...skipping 19 matching lines...) Expand all
611 } 611 }
612 612
613 void AutocompleteEditViewGtk::ClosePopup() { 613 void AutocompleteEditViewGtk::ClosePopup() {
614 if (popup_view_->GetModel()->IsOpen()) 614 if (popup_view_->GetModel()->IsOpen())
615 controller_->OnAutocompleteWillClosePopup(); 615 controller_->OnAutocompleteWillClosePopup();
616 616
617 popup_view_->GetModel()->StopAutocomplete(); 617 popup_view_->GetModel()->StopAutocomplete();
618 } 618 }
619 619
620 void AutocompleteEditViewGtk::OnTemporaryTextMaybeChanged( 620 void AutocompleteEditViewGtk::OnTemporaryTextMaybeChanged(
621 const std::wstring& display_text, 621 const string16& display_text,
622 bool save_original_selection) { 622 bool save_original_selection) {
623 if (save_original_selection) 623 if (save_original_selection)
624 saved_temporary_selection_ = GetSelection(); 624 saved_temporary_selection_ = GetSelection();
625 625
626 StartUpdatingHighlightedText(); 626 StartUpdatingHighlightedText();
627 SetWindowTextAndCaretPos(display_text, display_text.length()); 627 SetWindowTextAndCaretPos(display_text, display_text.length());
628 FinishUpdatingHighlightedText(); 628 FinishUpdatingHighlightedText();
629 TextChanged(); 629 TextChanged();
630 } 630 }
631 631
632 bool AutocompleteEditViewGtk::OnInlineAutocompleteTextMaybeChanged( 632 bool AutocompleteEditViewGtk::OnInlineAutocompleteTextMaybeChanged(
633 const std::wstring& display_text, 633 const string16& display_text,
634 size_t user_text_length) { 634 size_t user_text_length) {
635 if (display_text == GetText()) 635 if (display_text == GetText())
636 return false; 636 return false;
637 637
638 StartUpdatingHighlightedText(); 638 StartUpdatingHighlightedText();
639 CharRange range(display_text.size(), user_text_length); 639 CharRange range(display_text.size(), user_text_length);
640 SetTextAndSelectedRange(display_text, range); 640 SetTextAndSelectedRange(display_text, range);
641 FinishUpdatingHighlightedText(); 641 FinishUpdatingHighlightedText();
642 TextChanged(); 642 TextChanged();
643 return true; 643 return true;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
695 CharRange new_sel = GetSelection(); 695 CharRange new_sel = GetSelection();
696 int length = GetTextLength(); 696 int length = GetTextLength();
697 bool selection_differs = 697 bool selection_differs =
698 ((new_sel.cp_min != new_sel.cp_max) || 698 ((new_sel.cp_min != new_sel.cp_max) ||
699 (sel_before_change_.cp_min != sel_before_change_.cp_max)) && 699 (sel_before_change_.cp_min != sel_before_change_.cp_max)) &&
700 ((new_sel.cp_min != sel_before_change_.cp_min) || 700 ((new_sel.cp_min != sel_before_change_.cp_min) ||
701 (new_sel.cp_max != sel_before_change_.cp_max)); 701 (new_sel.cp_max != sel_before_change_.cp_max));
702 bool at_end_of_edit = (new_sel.cp_min == length && new_sel.cp_max == length); 702 bool at_end_of_edit = (new_sel.cp_min == length && new_sel.cp_max == length);
703 703
704 // See if the text or selection have changed since OnBeforePossibleChange(). 704 // See if the text or selection have changed since OnBeforePossibleChange().
705 std::wstring new_text(GetText()); 705 string16 new_text(GetText());
706 text_changed_ = (new_text != text_before_change_); 706 text_changed_ = (new_text != text_before_change_);
707 #if GTK_CHECK_VERSION(2, 20, 0) 707 #if GTK_CHECK_VERSION(2, 20, 0)
708 text_changed_ = 708 text_changed_ =
709 text_changed_ || (preedit_.size() != preedit_size_before_change_); 709 text_changed_ || (preedit_.size() != preedit_size_before_change_);
710 #endif 710 #endif
711 711
712 if (text_changed_) 712 if (text_changed_)
713 AdjustTextJustification(); 713 AdjustTextJustification();
714 714
715 // When the user has deleted text, we don't allow inline autocomplete. Make 715 // When the user has deleted text, we don't allow inline autocomplete. Make
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
829 #if defined(TOOLKIT_VIEWS) 829 #if defined(TOOLKIT_VIEWS)
830 views::View* AutocompleteEditViewGtk::AddToView(views::View* parent) { 830 views::View* AutocompleteEditViewGtk::AddToView(views::View* parent) {
831 views::NativeViewHost* host = new views::NativeViewHost; 831 views::NativeViewHost* host = new views::NativeViewHost;
832 parent->AddChildView(host); 832 parent->AddChildView(host);
833 host->set_focus_view(parent); 833 host->set_focus_view(parent);
834 host->Attach(GetNativeView()); 834 host->Attach(GetNativeView());
835 return host; 835 return host;
836 } 836 }
837 837
838 bool AutocompleteEditViewGtk::CommitInstantSuggestion( 838 bool AutocompleteEditViewGtk::CommitInstantSuggestion(
839 const std::wstring& typed_text, 839 const string16& typed_text,
840 const std::wstring& suggestion) { 840 const string16& suggestion) {
841 return CommitInstantSuggestion(); 841 return CommitInstantSuggestion();
842 } 842 }
843 843
844 void AutocompleteEditViewGtk::EnableAccessibility() { 844 void AutocompleteEditViewGtk::EnableAccessibility() {
845 accessible_widget_helper_.reset( 845 accessible_widget_helper_.reset(
846 new AccessibleWidgetHelper(text_view(), model_->profile())); 846 new AccessibleWidgetHelper(text_view(), model_->profile()));
847 accessible_widget_helper_->SetWidgetName( 847 accessible_widget_helper_->SetWidgetName(
848 text_view(), l10n_util::GetStringUTF8(IDS_ACCNAME_LOCATION)); 848 text_view(), l10n_util::GetStringUTF8(IDS_ACCNAME_LOCATION));
849 } 849 }
850 850
(...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after
1426 gtk_widget_set_sensitive(search_engine_menuitem, 1426 gtk_widget_set_sensitive(search_engine_menuitem,
1427 command_updater_->IsCommandEnabled(IDC_EDIT_SEARCH_ENGINES)); 1427 command_updater_->IsCommandEnabled(IDC_EDIT_SEARCH_ENGINES));
1428 gtk_widget_show(search_engine_menuitem); 1428 gtk_widget_show(search_engine_menuitem);
1429 1429
1430 // We need to update the paste and go controller before we know what text 1430 // We need to update the paste and go controller before we know what text
1431 // to show. We could do this all asynchronously, but it would be elaborate 1431 // to show. We could do this all asynchronously, but it would be elaborate
1432 // because we'd have to account for multiple menus showing, getting called 1432 // because we'd have to account for multiple menus showing, getting called
1433 // back after shutdown, and similar issues. 1433 // back after shutdown, and similar issues.
1434 GtkClipboard* x_clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); 1434 GtkClipboard* x_clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1435 gchar* text = gtk_clipboard_wait_for_text(x_clipboard); 1435 gchar* text = gtk_clipboard_wait_for_text(x_clipboard);
1436 std::wstring text_wstr = UTF8ToWide(text); 1436 string16 text_wstr = UTF8ToUTF16(text);
James Su 2011/01/26 01:42:05 This line causes crash when text is NULL. As UTF8T
1437 g_free(text); 1437 g_free(text);
1438 1438
1439 // Paste and Go menu item. 1439 // Paste and Go menu item.
1440 GtkWidget* paste_go_menuitem = gtk_menu_item_new_with_mnemonic( 1440 GtkWidget* paste_go_menuitem = gtk_menu_item_new_with_mnemonic(
1441 gfx::ConvertAcceleratorsFromWindowsStyle( 1441 gfx::ConvertAcceleratorsFromWindowsStyle(
1442 l10n_util::GetStringUTF8(model_->is_paste_and_search() ? 1442 l10n_util::GetStringUTF8(model_->is_paste_and_search() ?
1443 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO)).c_str()); 1443 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO)).c_str());
1444 gtk_menu_shell_append(GTK_MENU_SHELL(menu), paste_go_menuitem); 1444 gtk_menu_shell_append(GTK_MENU_SHELL(menu), paste_go_menuitem);
1445 g_signal_connect(paste_go_menuitem, "activate", 1445 g_signal_connect(paste_go_menuitem, "activate",
1446 G_CALLBACK(HandlePasteAndGoThunk), this); 1446 G_CALLBACK(HandlePasteAndGoThunk), this);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
1521 1521
1522 // Don't try to PasteAndGo on drops originating from this omnibox. However, do 1522 // Don't try to PasteAndGo on drops originating from this omnibox. However, do
1523 // allow default behavior for such drags. 1523 // allow default behavior for such drags.
1524 if (context->source_window == text_view_->window) 1524 if (context->source_window == text_view_->window)
1525 return; 1525 return;
1526 1526
1527 guchar* text = gtk_selection_data_get_text(selection_data); 1527 guchar* text = gtk_selection_data_get_text(selection_data);
1528 if (!text) 1528 if (!text)
1529 return; 1529 return;
1530 1530
1531 std::wstring possible_url = UTF8ToWide(reinterpret_cast<char*>(text)); 1531 string16 possible_url = UTF8ToUTF16(reinterpret_cast<char*>(text));
1532 g_free(text); 1532 g_free(text);
1533 if (model_->CanPasteAndGo(CollapseWhitespace(possible_url, true))) { 1533 if (model_->CanPasteAndGo(CollapseWhitespace(possible_url, true))) {
1534 model_->PasteAndGo(); 1534 model_->PasteAndGo();
1535 gtk_drag_finish(context, TRUE, TRUE, time); 1535 gtk_drag_finish(context, TRUE, TRUE, time);
1536 1536
1537 static guint signal_id = 1537 static guint signal_id =
1538 g_signal_lookup("drag-data-received", GTK_TYPE_WIDGET); 1538 g_signal_lookup("drag-data-received", GTK_TYPE_WIDGET);
1539 g_signal_stop_emission(text_view_, signal_id, 0); 1539 g_signal_stop_emission(text_view_, signal_id, 0);
1540 } 1540 }
1541 } 1541 }
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
1670 if (!gtk_text_buffer_get_has_selection(text_buffer_)) 1670 if (!gtk_text_buffer_get_has_selection(text_buffer_))
1671 return; 1671 return;
1672 1672
1673 GtkClipboard* clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY); 1673 GtkClipboard* clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1674 DCHECK(clipboard); 1674 DCHECK(clipboard);
1675 if (!clipboard) 1675 if (!clipboard)
1676 return; 1676 return;
1677 1677
1678 CharRange selection = GetSelection(); 1678 CharRange selection = GetSelection();
1679 GURL url; 1679 GURL url;
1680 std::wstring text(UTF8ToWide(GetSelectedText())); 1680 string16 text(UTF8ToUTF16(GetSelectedText()));
1681 bool write_url; 1681 bool write_url;
1682 model_->AdjustTextForCopy(selection.selection_min(), IsSelectAll(), &text, 1682 model_->AdjustTextForCopy(selection.selection_min(), IsSelectAll(), &text,
1683 &url, &write_url); 1683 &url, &write_url);
1684 1684
1685 if (write_url) { 1685 if (write_url) {
1686 string16 text16(WideToUTF16(text));
1687 BookmarkNodeData data; 1686 BookmarkNodeData data;
1688 data.ReadFromTuple(url, text16); 1687 data.ReadFromTuple(url, text);
1689 data.WriteToClipboard(NULL); 1688 data.WriteToClipboard(NULL);
1690 1689
1691 // Stop propagating the signal. 1690 // Stop propagating the signal.
1692 static guint copy_signal_id = 1691 static guint copy_signal_id =
1693 g_signal_lookup("copy-clipboard", GTK_TYPE_TEXT_VIEW); 1692 g_signal_lookup("copy-clipboard", GTK_TYPE_TEXT_VIEW);
1694 static guint cut_signal_id = 1693 static guint cut_signal_id =
1695 g_signal_lookup("cut-clipboard", GTK_TYPE_TEXT_VIEW); 1694 g_signal_lookup("cut-clipboard", GTK_TYPE_TEXT_VIEW);
1696 g_signal_stop_emission(text_view_, 1695 g_signal_stop_emission(text_view_,
1697 copy ? copy_signal_id : cut_signal_id, 1696 copy ? copy_signal_id : cut_signal_id,
1698 0); 1697 0);
1699 1698
1700 if (!copy) 1699 if (!copy)
1701 gtk_text_buffer_delete_selection(text_buffer_, true, true); 1700 gtk_text_buffer_delete_selection(text_buffer_, true, true);
1702 } 1701 }
1703 1702
1704 OwnPrimarySelection(WideToUTF8(text)); 1703 OwnPrimarySelection(UTF16ToUTF8(text));
1705 } 1704 }
1706 1705
1707 void AutocompleteEditViewGtk::OwnPrimarySelection(const std::string& text) { 1706 void AutocompleteEditViewGtk::OwnPrimarySelection(const std::string& text) {
1708 primary_selection_text_ = text; 1707 primary_selection_text_ = text;
1709 1708
1710 GtkTargetList* list = gtk_target_list_new(NULL, 0); 1709 GtkTargetList* list = gtk_target_list_new(NULL, 0);
1711 gtk_target_list_add_text_targets(list, 0); 1710 gtk_target_list_add_text_targets(list, 0);
1712 gint len; 1711 gint len;
1713 GtkTargetEntry* entries = gtk_target_table_new_from_list(list, &len); 1712 GtkTargetEntry* entries = gtk_target_table_new_from_list(list, &len);
1714 1713
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
1885 return; 1884 return;
1886 } 1885 }
1887 #endif 1886 #endif
1888 // See whether the contents are a URL with a non-empty host portion, which we 1887 // See whether the contents are a URL with a non-empty host portion, which we
1889 // should emphasize. To check for a URL, rather than using the type returned 1888 // should emphasize. To check for a URL, rather than using the type returned
1890 // by Parse(), ask the model, which will check the desired page transition for 1889 // by Parse(), ask the model, which will check the desired page transition for
1891 // this input. This can tell us whether an UNKNOWN input string is going to 1890 // this input. This can tell us whether an UNKNOWN input string is going to
1892 // be treated as a search or a navigation, and is the same method the Paste 1891 // be treated as a search or a navigation, and is the same method the Paste
1893 // And Go system uses. 1892 // And Go system uses.
1894 url_parse::Component scheme, host; 1893 url_parse::Component scheme, host;
1895 std::wstring text(GetText()); 1894 string16 text(GetText());
1896 AutocompleteInput::ParseForEmphasizeComponents( 1895 AutocompleteInput::ParseForEmphasizeComponents(
1897 text, model_->GetDesiredTLD(), &scheme, &host); 1896 text, model_->GetDesiredTLD(), &scheme, &host);
1898 const bool emphasize = model_->CurrentTextIsURL() && (host.len > 0); 1897 const bool emphasize = model_->CurrentTextIsURL() && (host.len > 0);
1899 1898
1900 // Set the baseline emphasis. 1899 // Set the baseline emphasis.
1901 GtkTextIter start, end; 1900 GtkTextIter start, end;
1902 GetTextBufferBounds(&start, &end); 1901 GetTextBufferBounds(&start, &end);
1903 gtk_text_buffer_remove_all_tags(text_buffer_, &start, &end); 1902 gtk_text_buffer_remove_all_tags(text_buffer_, &start, &end);
1904 if (emphasize) { 1903 if (emphasize) {
1905 gtk_text_buffer_apply_tag(text_buffer_, faded_text_tag_, &start, &end); 1904 gtk_text_buffer_apply_tag(text_buffer_, faded_text_tag_, &start, &end);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1947 instant_animation_->Stop(); 1946 instant_animation_->Stop();
1948 UpdateInstantViewColors(); 1947 UpdateInstantViewColors();
1949 } 1948 }
1950 1949
1951 bool AutocompleteEditViewGtk::CommitInstantSuggestion() { 1950 bool AutocompleteEditViewGtk::CommitInstantSuggestion() {
1952 const gchar* suggestion = gtk_label_get_text(GTK_LABEL(instant_view_)); 1951 const gchar* suggestion = gtk_label_get_text(GTK_LABEL(instant_view_));
1953 if (!suggestion || !*suggestion) 1952 if (!suggestion || !*suggestion)
1954 return false; 1953 return false;
1955 1954
1956 model()->FinalizeInstantQuery(GetText(), 1955 model()->FinalizeInstantQuery(GetText(),
1957 UTF8ToWide(suggestion)); 1956 UTF8ToUTF16(suggestion));
1958 return true; 1957 return true;
1959 } 1958 }
1960 1959
1961 void AutocompleteEditViewGtk::TextChanged() { 1960 void AutocompleteEditViewGtk::TextChanged() {
1962 EmphasizeURLComponents(); 1961 EmphasizeURLComponents();
1963 controller_->OnChanged(); 1962 controller_->OnChanged();
1964 } 1963 }
1965 1964
1966 void AutocompleteEditViewGtk::SavePrimarySelection( 1965 void AutocompleteEditViewGtk::SavePrimarySelection(
1967 const std::string& selected_text) { 1966 const std::string& selected_text) {
1968 GtkClipboard* clipboard = 1967 GtkClipboard* clipboard =
1969 gtk_widget_get_clipboard(text_view_, GDK_SELECTION_PRIMARY); 1968 gtk_widget_get_clipboard(text_view_, GDK_SELECTION_PRIMARY);
1970 DCHECK(clipboard); 1969 DCHECK(clipboard);
1971 if (!clipboard) 1970 if (!clipboard)
1972 return; 1971 return;
1973 1972
1974 gtk_clipboard_set_text( 1973 gtk_clipboard_set_text(
1975 clipboard, selected_text.data(), selected_text.size()); 1974 clipboard, selected_text.data(), selected_text.size());
1976 } 1975 }
1977 1976
1978 void AutocompleteEditViewGtk::SetTextAndSelectedRange(const std::wstring& text, 1977 void AutocompleteEditViewGtk::SetTextAndSelectedRange(const string16& text,
1979 const CharRange& range) { 1978 const CharRange& range) {
1980 if (text != GetText()) { 1979 if (text != GetText()) {
1981 std::string utf8 = WideToUTF8(text); 1980 std::string utf8 = UTF16ToUTF8(text);
1982 gtk_text_buffer_set_text(text_buffer_, utf8.data(), utf8.length()); 1981 gtk_text_buffer_set_text(text_buffer_, utf8.data(), utf8.length());
1983 } 1982 }
1984 SetSelectedRange(range); 1983 SetSelectedRange(range);
1985 AdjustTextJustification(); 1984 AdjustTextJustification();
1986 } 1985 }
1987 1986
1988 void AutocompleteEditViewGtk::SetSelectedRange(const CharRange& range) { 1987 void AutocompleteEditViewGtk::SetSelectedRange(const CharRange& range) {
1989 GtkTextIter insert, bound; 1988 GtkTextIter insert, bound;
1990 ItersFromCharRange(range, &bound, &insert); 1989 ItersFromCharRange(range, &bound, &insert);
1991 gtk_text_buffer_select_range(text_buffer_, &insert, &bound); 1990 gtk_text_buffer_select_range(text_buffer_, &insert, &bound);
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
2137 gchar* text = gtk_text_iter_get_text(&start, &end); 2136 gchar* text = gtk_text_iter_get_text(&start, &end);
2138 size_t text_len = strlen(text); 2137 size_t text_len = strlen(text);
2139 if (text_len) 2138 if (text_len)
2140 result = std::string(text, text_len); 2139 result = std::string(text, text_len);
2141 g_free(text); 2140 g_free(text);
2142 } 2141 }
2143 return result; 2142 return result;
2144 } 2143 }
2145 2144
2146 void AutocompleteEditViewGtk::UpdatePrimarySelectionIfValidURL() { 2145 void AutocompleteEditViewGtk::UpdatePrimarySelectionIfValidURL() {
2147 std::wstring text = UTF8ToWide(GetSelectedText()); 2146 string16 text = UTF8ToUTF16(GetSelectedText());
2148 2147
2149 if (text.empty()) 2148 if (text.empty())
2150 return; 2149 return;
2151 2150
2152 // Use AdjustTextForCopy to make sure we prefix the text with 'http://'. 2151 // Use AdjustTextForCopy to make sure we prefix the text with 'http://'.
2153 CharRange selection = GetSelection(); 2152 CharRange selection = GetSelection();
2154 GURL url; 2153 GURL url;
2155 bool write_url; 2154 bool write_url;
2156 model_->AdjustTextForCopy(selection.selection_min(), IsSelectAll(), &text, 2155 model_->AdjustTextForCopy(selection.selection_min(), IsSelectAll(), &text,
2157 &url, &write_url); 2156 &url, &write_url);
2158 if (write_url) { 2157 if (write_url) {
2159 selected_text_ = WideToUTF8(text); 2158 selected_text_ = UTF16ToUTF8(text);
2160 OwnPrimarySelection(selected_text_); 2159 OwnPrimarySelection(selected_text_);
2161 } 2160 }
2162 } 2161 }
2163 2162
2164 #if GTK_CHECK_VERSION(2, 20, 0) 2163 #if GTK_CHECK_VERSION(2, 20, 0)
2165 void AutocompleteEditViewGtk::HandlePreeditChanged(GtkWidget* sender, 2164 void AutocompleteEditViewGtk::HandlePreeditChanged(GtkWidget* sender,
2166 const gchar* preedit) { 2165 const gchar* preedit) {
2167 // GtkTextView won't fire "begin-user-action" and "end-user-action" signals 2166 // GtkTextView won't fire "begin-user-action" and "end-user-action" signals
2168 // when changing the preedit string, so we need to call 2167 // when changing the preedit string, so we need to call
2169 // OnBeforePossibleChange() and OnAfterPossibleChange() by ourselves. 2168 // OnBeforePossibleChange() and OnAfterPossibleChange() by ourselves.
2170 OnBeforePossibleChange(); 2169 OnBeforePossibleChange();
2171 if (preedit && *preedit) { 2170 if (preedit && *preedit) {
2172 // GtkTextView will only delete the selection range when committing the 2171 // GtkTextView will only delete the selection range when committing the
2173 // preedit string, which will cause very strange behavior, so we need to 2172 // preedit string, which will cause very strange behavior, so we need to
2174 // delete the selection range here explicitly. See http://crbug.com/18808. 2173 // delete the selection range here explicitly. See http://crbug.com/18808.
2175 if (preedit_.empty()) 2174 if (preedit_.empty())
2176 gtk_text_buffer_delete_selection(text_buffer_, false, true); 2175 gtk_text_buffer_delete_selection(text_buffer_, false, true);
2177 preedit_ = UTF8ToWide(preedit); 2176 preedit_ = UTF8ToUTF16(preedit);
2178 } else { 2177 } else {
2179 preedit_.clear(); 2178 preedit_.clear();
2180 } 2179 }
2181 OnAfterPossibleChange(); 2180 OnAfterPossibleChange();
2182 } 2181 }
2183 #endif 2182 #endif
2184 2183
2185 void AutocompleteEditViewGtk::HandleWindowSetFocus( 2184 void AutocompleteEditViewGtk::HandleWindowSetFocus(
2186 GtkWindow* sender, GtkWidget* focus) { 2185 GtkWindow* sender, GtkWidget* focus) {
2187 // This is actually a guess. If the focused widget changes in "focus-out" 2186 // This is actually a guess. If the focused widget changes in "focus-out"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
2219 // baseline, so we need to move the |instant_view_| down to make sure it 2218 // baseline, so we need to move the |instant_view_| down to make sure it
2220 // has the same baseline as the |text_view_|. 2219 // has the same baseline as the |text_view_|.
2221 PangoLayout* layout = gtk_label_get_layout(GTK_LABEL(instant_view_)); 2220 PangoLayout* layout = gtk_label_get_layout(GTK_LABEL(instant_view_));
2222 int height; 2221 int height;
2223 pango_layout_get_size(layout, NULL, &height); 2222 pango_layout_get_size(layout, NULL, &height);
2224 PangoLayoutIter* iter = pango_layout_get_iter(layout); 2223 PangoLayoutIter* iter = pango_layout_get_iter(layout);
2225 int baseline = pango_layout_iter_get_baseline(iter); 2224 int baseline = pango_layout_iter_get_baseline(iter);
2226 pango_layout_iter_free(iter); 2225 pango_layout_iter_free(iter);
2227 g_object_set(instant_anchor_tag_, "rise", baseline - height, NULL); 2226 g_object_set(instant_anchor_tag_, "rise", baseline - height, NULL);
2228 } 2227 }
OLDNEW
« no previous file with comments | « chrome/browser/autocomplete/autocomplete_edit_view_gtk.h ('k') | chrome/browser/autocomplete/autocomplete_edit_view_mac.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698