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 "chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.h" | 5 #include "chrome/browser/ui/gtk/omnibox/omnibox_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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 | 60 |
61 const gchar* kOmniboxViewGtkKey = "__OMNIBOX_VIEW_GTK__"; | 61 const gchar* kOmniboxViewGtkKey = "__OMNIBOX_VIEW_GTK__"; |
62 | 62 |
63 const char kTextBaseColor[] = "#808080"; | 63 const char kTextBaseColor[] = "#808080"; |
64 const char kSecureSchemeColor[] = "#079500"; | 64 const char kSecureSchemeColor[] = "#079500"; |
65 const char kSecurityErrorSchemeColor[] = "#a20000"; | 65 const char kSecurityErrorSchemeColor[] = "#a20000"; |
66 | 66 |
67 const double kStrikethroughStrokeRed = 162.0 / 256.0; | 67 const double kStrikethroughStrokeRed = 162.0 / 256.0; |
68 const double kStrikethroughStrokeWidth = 2.0; | 68 const double kStrikethroughStrokeWidth = 2.0; |
69 | 69 |
| 70 // static |
| 71 const char16 kInvalidChars[] = { |
| 72 '\n', '\r', '\t', |
| 73 0x200B, // Zero Width Space |
| 74 0 |
| 75 }; |
| 76 |
70 size_t GetUTF8Offset(const string16& text, size_t text_offset) { | 77 size_t GetUTF8Offset(const string16& text, size_t text_offset) { |
71 return UTF16ToUTF8(text.substr(0, text_offset)).size(); | 78 return UTF16ToUTF8(text.substr(0, text_offset)).size(); |
72 } | 79 } |
73 | 80 |
74 // A helper method for determining a valid drag operation given the allowed | 81 // A helper method for determining a valid drag operation given the allowed |
75 // operation. We prefer copy over link. | 82 // operation. We prefer copy over link. |
76 int CopyOrLinkDragOperation(int drag_operation) { | 83 int CopyOrLinkDragOperation(int drag_operation) { |
77 if (drag_operation & ui::DragDropTypes::DRAG_COPY) | 84 if (drag_operation & ui::DragDropTypes::DRAG_COPY) |
78 return ui::DragDropTypes::DRAG_COPY; | 85 return ui::DragDropTypes::DRAG_COPY; |
79 if (drag_operation & ui::DragDropTypes::DRAG_LINK) | 86 if (drag_operation & ui::DragDropTypes::DRAG_LINK) |
(...skipping 791 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
871 int OmniboxViewGtk::OnPerformDrop( | 878 int OmniboxViewGtk::OnPerformDrop( |
872 const views::DropTargetEvent& event) { | 879 const views::DropTargetEvent& event) { |
873 string16 text; | 880 string16 text; |
874 const ui::OSExchangeData& data = event.data(); | 881 const ui::OSExchangeData& data = event.data(); |
875 if (data.HasURL()) { | 882 if (data.HasURL()) { |
876 GURL url; | 883 GURL url; |
877 string16 title; | 884 string16 title; |
878 if (data.GetURLAndTitle(&url, &title)) | 885 if (data.GetURLAndTitle(&url, &title)) |
879 text = UTF8ToUTF16(url.spec()); | 886 text = UTF8ToUTF16(url.spec()); |
880 } else { | 887 } else { |
881 string16 data_string; | 888 data.GetString(&text); |
882 if (data.GetString(&data_string)) | |
883 text = CollapseWhitespace(data_string, true); | |
884 } | 889 } |
885 | 890 |
886 if (!text.empty() && OnPerformDropImpl(text)) | 891 if (!text.empty() && OnPerformDropImpl(text)) |
887 return CopyOrLinkDragOperation(event.source_operations()); | 892 return CopyOrLinkDragOperation(event.source_operations()); |
888 | 893 |
889 return ui::DragDropTypes::DRAG_NONE; | 894 return ui::DragDropTypes::DRAG_NONE; |
890 } | 895 } |
891 #endif // defined(TOOLKIT_VIEWS) | 896 #endif // defined(TOOLKIT_VIEWS) |
892 | 897 |
893 void OmniboxViewGtk::Observe(int type, | 898 void OmniboxViewGtk::Observe(int type, |
(...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1437 gtk_widget_set_sensitive(search_engine_menuitem, | 1442 gtk_widget_set_sensitive(search_engine_menuitem, |
1438 command_updater_->IsCommandEnabled(IDC_EDIT_SEARCH_ENGINES)); | 1443 command_updater_->IsCommandEnabled(IDC_EDIT_SEARCH_ENGINES)); |
1439 gtk_widget_show(search_engine_menuitem); | 1444 gtk_widget_show(search_engine_menuitem); |
1440 | 1445 |
1441 // We need to update the paste and go controller before we know what text | 1446 // We need to update the paste and go controller before we know what text |
1442 // to show. We could do this all asynchronously, but it would be elaborate | 1447 // to show. We could do this all asynchronously, but it would be elaborate |
1443 // because we'd have to account for multiple menus showing, getting called | 1448 // because we'd have to account for multiple menus showing, getting called |
1444 // back after shutdown, and similar issues. | 1449 // back after shutdown, and similar issues. |
1445 GtkClipboard* x_clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); | 1450 GtkClipboard* x_clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); |
1446 gchar* text = gtk_clipboard_wait_for_text(x_clipboard); | 1451 gchar* text = gtk_clipboard_wait_for_text(x_clipboard); |
1447 string16 text_wstr = UTF8ToUTF16(text ? text : ""); | 1452 string16 sanitized_text(text ? |
| 1453 StripJavascriptSchemas(CollapseWhitespace(UTF8ToUTF16(text), true)) : |
| 1454 string16()); |
1448 g_free(text); | 1455 g_free(text); |
1449 | 1456 |
1450 // Paste and Go menu item. | 1457 // Paste and Go menu item. Note that CanPasteAndGo() needs to be called |
| 1458 // before is_paste_and_search() in order to set up the paste-and-go state. |
| 1459 bool can_paste_and_go = model_->CanPasteAndGo(sanitized_text); |
1451 GtkWidget* paste_go_menuitem = gtk_menu_item_new_with_mnemonic( | 1460 GtkWidget* paste_go_menuitem = gtk_menu_item_new_with_mnemonic( |
1452 gfx::ConvertAcceleratorsFromWindowsStyle( | 1461 gfx::ConvertAcceleratorsFromWindowsStyle( |
1453 l10n_util::GetStringUTF8(model_->is_paste_and_search() ? | 1462 l10n_util::GetStringUTF8(model_->is_paste_and_search() ? |
1454 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO)).c_str()); | 1463 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO)).c_str()); |
1455 gtk_menu_shell_append(GTK_MENU_SHELL(menu), paste_go_menuitem); | 1464 gtk_menu_shell_append(GTK_MENU_SHELL(menu), paste_go_menuitem); |
1456 g_signal_connect(paste_go_menuitem, "activate", | 1465 g_signal_connect(paste_go_menuitem, "activate", |
1457 G_CALLBACK(HandlePasteAndGoThunk), this); | 1466 G_CALLBACK(HandlePasteAndGoThunk), this); |
1458 gtk_widget_set_sensitive(paste_go_menuitem, | 1467 gtk_widget_set_sensitive(paste_go_menuitem, can_paste_and_go); |
1459 model_->CanPasteAndGo(text_wstr)); | |
1460 gtk_widget_show(paste_go_menuitem); | 1468 gtk_widget_show(paste_go_menuitem); |
1461 | 1469 |
1462 g_signal_connect(menu, "deactivate", | 1470 g_signal_connect(menu, "deactivate", |
1463 G_CALLBACK(HandlePopupMenuDeactivateThunk), this); | 1471 G_CALLBACK(HandlePopupMenuDeactivateThunk), this); |
1464 } | 1472 } |
1465 | 1473 |
1466 void OmniboxViewGtk::HandleEditSearchEngines(GtkWidget* sender) { | 1474 void OmniboxViewGtk::HandleEditSearchEngines(GtkWidget* sender) { |
1467 command_updater_->ExecuteCommand(IDC_EDIT_SEARCH_ENGINES); | 1475 command_updater_->ExecuteCommand(IDC_EDIT_SEARCH_ENGINES); |
1468 } | 1476 } |
1469 | 1477 |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1625 GtkTargetList* copy_targets = | 1633 GtkTargetList* copy_targets = |
1626 gtk_text_buffer_get_copy_target_list(text_buffer_); | 1634 gtk_text_buffer_get_copy_target_list(text_buffer_); |
1627 gtk_target_list_remove(copy_targets, atom); | 1635 gtk_target_list_remove(copy_targets, atom); |
1628 dragged_text_.clear(); | 1636 dragged_text_.clear(); |
1629 } | 1637 } |
1630 | 1638 |
1631 void OmniboxViewGtk::HandleInsertText(GtkTextBuffer* buffer, | 1639 void OmniboxViewGtk::HandleInsertText(GtkTextBuffer* buffer, |
1632 GtkTextIter* location, | 1640 GtkTextIter* location, |
1633 const gchar* text, | 1641 const gchar* text, |
1634 gint len) { | 1642 gint len) { |
1635 std::string filtered_text; | 1643 string16 filtered_text; |
1636 filtered_text.reserve(len); | 1644 filtered_text.reserve(len); |
1637 | 1645 |
1638 // Filter out new line and tab characters. | 1646 // Filter out new line and tab characters. |
1639 // |text| is guaranteed to be a valid UTF-8 string, so we don't need to | 1647 // |text| is guaranteed to be a valid UTF-8 string, so we don't need to |
1640 // validate it here. | 1648 // validate it here. |
1641 // | 1649 // |
1642 // If there was only a single character, then it might be generated by a key | 1650 // If there was only a single character, then it might be generated by a key |
1643 // event. In this case, we save the single character to help our | 1651 // event. In this case, we save the single character to help our |
1644 // "key-press-event" signal handler distinguish if an Enter key event is | 1652 // "key-press-event" signal handler distinguish if an Enter key event is |
1645 // handled by IME or not. | 1653 // handled by IME or not. |
1646 if (len == 1 && (text[0] == '\n' || text[0] == '\r')) | 1654 if (len == 1 && (text[0] == '\n' || text[0] == '\r')) |
1647 enter_was_inserted_ = true; | 1655 enter_was_inserted_ = true; |
1648 | 1656 |
1649 const gchar* p = text; | 1657 filtered_text = UTF8ToUTF16(text); |
1650 while (*p && (p - text) < len) { | 1658 RemoveChars(filtered_text, kInvalidChars, &filtered_text); |
1651 gunichar c = g_utf8_get_char(p); | |
1652 const gchar* next = g_utf8_next_char(p); | |
1653 | 1659 |
1654 // 0x200B is Zero Width Space, which is inserted just before the instant | 1660 if (model_->is_pasting()) |
1655 // anchor for working around the GtkTextView's misalignment bug. | 1661 filtered_text = StripJavascriptSchemas(filtered_text); |
1656 // This character might be captured and inserted into the content by undo | |
1657 // manager, so we need to filter it out here. | |
1658 if (c != L'\n' && c != L'\r' && c != L'\t' && c != 0x200B) | |
1659 filtered_text.append(p, next); | |
1660 | 1662 |
1661 p = next; | 1663 if (!filtered_text.empty()) { |
1662 } | |
1663 | |
1664 if (filtered_text.length()) { | |
1665 // Avoid inserting the text after the instant anchor. | 1664 // Avoid inserting the text after the instant anchor. |
1666 ValidateTextBufferIter(location); | 1665 ValidateTextBufferIter(location); |
1667 | 1666 |
1668 // Call the default handler to insert filtered text. | 1667 // Call the default handler to insert filtered text. |
1669 GtkTextBufferClass* klass = GTK_TEXT_BUFFER_GET_CLASS(buffer); | 1668 GtkTextBufferClass* klass = GTK_TEXT_BUFFER_GET_CLASS(buffer); |
1670 klass->insert_text(buffer, location, filtered_text.data(), | 1669 std::string utf8_text = UTF16ToUTF8(filtered_text); |
1671 static_cast<gint>(filtered_text.length())); | 1670 klass->insert_text(buffer, location, utf8_text.data(), |
| 1671 static_cast<gint>(utf8_text.length())); |
1672 } | 1672 } |
1673 | 1673 |
1674 // Stop propagating the signal emission to prevent the default handler from | 1674 // Stop propagating the signal emission to prevent the default handler from |
1675 // being called again. | 1675 // being called again. |
1676 static guint signal_id = g_signal_lookup("insert-text", GTK_TYPE_TEXT_BUFFER); | 1676 static guint signal_id = g_signal_lookup("insert-text", GTK_TYPE_TEXT_BUFFER); |
1677 g_signal_stop_emission(buffer, signal_id, 0); | 1677 g_signal_stop_emission(buffer, signal_id, 0); |
1678 } | 1678 } |
1679 | 1679 |
1680 void OmniboxViewGtk::HandleBackSpace(GtkWidget* sender) { | 1680 void OmniboxViewGtk::HandleBackSpace(GtkWidget* sender) { |
1681 // Checks if it's currently in keyword search mode. | 1681 // Checks if it's currently in keyword search mode. |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1788 0); | 1788 0); |
1789 | 1789 |
1790 if (!copy && gtk_text_view_get_editable(GTK_TEXT_VIEW(text_view_))) | 1790 if (!copy && gtk_text_view_get_editable(GTK_TEXT_VIEW(text_view_))) |
1791 gtk_text_buffer_delete_selection(text_buffer_, true, true); | 1791 gtk_text_buffer_delete_selection(text_buffer_, true, true); |
1792 } | 1792 } |
1793 | 1793 |
1794 OwnPrimarySelection(UTF16ToUTF8(text)); | 1794 OwnPrimarySelection(UTF16ToUTF8(text)); |
1795 } | 1795 } |
1796 | 1796 |
1797 bool OmniboxViewGtk::OnPerformDropImpl(const string16& text) { | 1797 bool OmniboxViewGtk::OnPerformDropImpl(const string16& text) { |
1798 if (model_->CanPasteAndGo(CollapseWhitespace(text, true))) { | 1798 if (model_->CanPasteAndGo(StripJavascriptSchemas( |
| 1799 CollapseWhitespace(text, true)))) { |
1799 model_->PasteAndGo(); | 1800 model_->PasteAndGo(); |
1800 return true; | 1801 return true; |
1801 } | 1802 } |
1802 | 1803 |
1803 return false; | 1804 return false; |
1804 } | 1805 } |
1805 | 1806 |
1806 gfx::Font OmniboxViewGtk::GetFont() { | 1807 gfx::Font OmniboxViewGtk::GetFont() { |
1807 #if defined(TOOLKIT_VIEWS) | 1808 #if defined(TOOLKIT_VIEWS) |
1808 bool use_gtk = false; | 1809 bool use_gtk = false; |
(...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2394 | 2395 |
2395 // Make all the children of the widget visible. NOTE: this won't display | 2396 // Make all the children of the widget visible. NOTE: this won't display |
2396 // anything, it just toggles the visible flag. | 2397 // anything, it just toggles the visible flag. |
2397 gtk_widget_show_all(omnibox_view->GetNativeView()); | 2398 gtk_widget_show_all(omnibox_view->GetNativeView()); |
2398 // Hide the widget. NativeViewHostGtk will make it visible again as necessary. | 2399 // Hide the widget. NativeViewHostGtk will make it visible again as necessary. |
2399 gtk_widget_hide(omnibox_view->GetNativeView()); | 2400 gtk_widget_hide(omnibox_view->GetNativeView()); |
2400 | 2401 |
2401 return omnibox_view; | 2402 return omnibox_view; |
2402 } | 2403 } |
2403 #endif | 2404 #endif |
OLD | NEW |