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 860 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
871 int OmniboxViewGtk::OnPerformDrop( | 871 int OmniboxViewGtk::OnPerformDrop( |
872 const views::DropTargetEvent& event) { | 872 const views::DropTargetEvent& event) { |
873 string16 text; | 873 string16 text; |
874 const ui::OSExchangeData& data = event.data(); | 874 const ui::OSExchangeData& data = event.data(); |
875 if (data.HasURL()) { | 875 if (data.HasURL()) { |
876 GURL url; | 876 GURL url; |
877 string16 title; | 877 string16 title; |
878 if (data.GetURLAndTitle(&url, &title)) | 878 if (data.GetURLAndTitle(&url, &title)) |
879 text = UTF8ToUTF16(url.spec()); | 879 text = UTF8ToUTF16(url.spec()); |
880 } else { | 880 } else { |
881 string16 data_string; | 881 data.GetString(&text); |
882 if (data.GetString(&data_string)) | |
883 text = CollapseWhitespace(data_string, true); | |
884 } | 882 } |
885 | 883 |
886 if (!text.empty() && OnPerformDropImpl(text)) | 884 if (!text.empty() && OnPerformDropImpl(text)) |
887 return CopyOrLinkDragOperation(event.source_operations()); | 885 return CopyOrLinkDragOperation(event.source_operations()); |
888 | 886 |
889 return ui::DragDropTypes::DRAG_NONE; | 887 return ui::DragDropTypes::DRAG_NONE; |
890 } | 888 } |
891 #endif // defined(TOOLKIT_VIEWS) | 889 #endif // defined(TOOLKIT_VIEWS) |
892 | 890 |
893 void OmniboxViewGtk::Observe(int type, | 891 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, | 1435 gtk_widget_set_sensitive(search_engine_menuitem, |
1438 command_updater_->IsCommandEnabled(IDC_EDIT_SEARCH_ENGINES)); | 1436 command_updater_->IsCommandEnabled(IDC_EDIT_SEARCH_ENGINES)); |
1439 gtk_widget_show(search_engine_menuitem); | 1437 gtk_widget_show(search_engine_menuitem); |
1440 | 1438 |
1441 // We need to update the paste and go controller before we know what text | 1439 // 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 | 1440 // 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 | 1441 // because we'd have to account for multiple menus showing, getting called |
1444 // back after shutdown, and similar issues. | 1442 // back after shutdown, and similar issues. |
1445 GtkClipboard* x_clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); | 1443 GtkClipboard* x_clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); |
1446 gchar* text = gtk_clipboard_wait_for_text(x_clipboard); | 1444 gchar* text = gtk_clipboard_wait_for_text(x_clipboard); |
1447 string16 text_wstr = UTF8ToUTF16(text ? text : ""); | 1445 string16 sanitized_text(text ? |
1446 StripJavascriptSchemas(CollapseWhitespace(UTF8ToUTF16(text), true)) : | |
1447 string16()); | |
1448 g_free(text); | 1448 g_free(text); |
1449 | 1449 |
1450 // Paste and Go menu item. | 1450 // Paste and Go menu item. |
Peter Kasting
2011/11/23 19:02:11
Nit: To prevent future problems, maybe we should a
SanjoyPal
2011/11/24 07:36:10
Done.
| |
1451 bool can_paste_and_go = model_->CanPasteAndGo(sanitized_text); | |
1451 GtkWidget* paste_go_menuitem = gtk_menu_item_new_with_mnemonic( | 1452 GtkWidget* paste_go_menuitem = gtk_menu_item_new_with_mnemonic( |
1452 gfx::ConvertAcceleratorsFromWindowsStyle( | 1453 gfx::ConvertAcceleratorsFromWindowsStyle( |
1453 l10n_util::GetStringUTF8(model_->is_paste_and_search() ? | 1454 l10n_util::GetStringUTF8(model_->is_paste_and_search() ? |
1454 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO)).c_str()); | 1455 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO)).c_str()); |
1455 gtk_menu_shell_append(GTK_MENU_SHELL(menu), paste_go_menuitem); | 1456 gtk_menu_shell_append(GTK_MENU_SHELL(menu), paste_go_menuitem); |
1456 g_signal_connect(paste_go_menuitem, "activate", | 1457 g_signal_connect(paste_go_menuitem, "activate", |
1457 G_CALLBACK(HandlePasteAndGoThunk), this); | 1458 G_CALLBACK(HandlePasteAndGoThunk), this); |
1458 gtk_widget_set_sensitive(paste_go_menuitem, | 1459 gtk_widget_set_sensitive(paste_go_menuitem, can_paste_and_go); |
1459 model_->CanPasteAndGo(text_wstr)); | |
1460 gtk_widget_show(paste_go_menuitem); | 1460 gtk_widget_show(paste_go_menuitem); |
1461 | 1461 |
1462 g_signal_connect(menu, "deactivate", | 1462 g_signal_connect(menu, "deactivate", |
1463 G_CALLBACK(HandlePopupMenuDeactivateThunk), this); | 1463 G_CALLBACK(HandlePopupMenuDeactivateThunk), this); |
1464 } | 1464 } |
1465 | 1465 |
1466 void OmniboxViewGtk::HandleEditSearchEngines(GtkWidget* sender) { | 1466 void OmniboxViewGtk::HandleEditSearchEngines(GtkWidget* sender) { |
1467 command_updater_->ExecuteCommand(IDC_EDIT_SEARCH_ENGINES); | 1467 command_updater_->ExecuteCommand(IDC_EDIT_SEARCH_ENGINES); |
1468 } | 1468 } |
1469 | 1469 |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1625 GtkTargetList* copy_targets = | 1625 GtkTargetList* copy_targets = |
1626 gtk_text_buffer_get_copy_target_list(text_buffer_); | 1626 gtk_text_buffer_get_copy_target_list(text_buffer_); |
1627 gtk_target_list_remove(copy_targets, atom); | 1627 gtk_target_list_remove(copy_targets, atom); |
1628 dragged_text_.clear(); | 1628 dragged_text_.clear(); |
1629 } | 1629 } |
1630 | 1630 |
1631 void OmniboxViewGtk::HandleInsertText(GtkTextBuffer* buffer, | 1631 void OmniboxViewGtk::HandleInsertText(GtkTextBuffer* buffer, |
1632 GtkTextIter* location, | 1632 GtkTextIter* location, |
1633 const gchar* text, | 1633 const gchar* text, |
1634 gint len) { | 1634 gint len) { |
1635 std::string filtered_text; | 1635 string16 filtered_text; |
1636 filtered_text.reserve(len); | 1636 filtered_text.reserve(len); |
1637 | 1637 |
1638 // Filter out new line and tab characters. | 1638 // Filter out new line and tab characters. |
1639 // |text| is guaranteed to be a valid UTF-8 string, so we don't need to | 1639 // |text| is guaranteed to be a valid UTF-8 string, so we don't need to |
1640 // validate it here. | 1640 // validate it here. |
1641 // | 1641 // |
1642 // If there was only a single character, then it might be generated by a key | 1642 // 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 | 1643 // 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 | 1644 // "key-press-event" signal handler distinguish if an Enter key event is |
1645 // handled by IME or not. | 1645 // handled by IME or not. |
1646 if (len == 1 && (text[0] == '\n' || text[0] == '\r')) | 1646 if (len == 1 && (text[0] == '\n' || text[0] == '\r')) |
1647 enter_was_inserted_ = true; | 1647 enter_was_inserted_ = true; |
1648 | 1648 |
1649 const gchar* p = text; | 1649 const gchar* p = text; |
1650 while (*p && (p - text) < len) { | 1650 while (*p && (p - text) < len) { |
1651 gunichar c = g_utf8_get_char(p); | 1651 gunichar c = g_utf8_get_char(p); |
1652 const gchar* next = g_utf8_next_char(p); | 1652 const gchar* next = g_utf8_next_char(p); |
1653 | 1653 |
1654 // 0x200B is Zero Width Space, which is inserted just before the instant | 1654 // 0x200B is Zero Width Space, which is inserted just before the instant |
1655 // anchor for working around the GtkTextView's misalignment bug. | 1655 // anchor for working around the GtkTextView's misalignment bug. |
1656 // This character might be captured and inserted into the content by undo | 1656 // This character might be captured and inserted into the content by undo |
1657 // manager, so we need to filter it out here. | 1657 // manager, so we need to filter it out here. |
1658 if (c != L'\n' && c != L'\r' && c != L'\t' && c != 0x200B) | 1658 if (c != 0x200B) |
1659 filtered_text.append(p, next); | 1659 filtered_text.append(p, next); |
Peter Kasting
2011/11/23 19:02:11
What asvitkine said about using WriteUnicodeCharac
SanjoyPal
2011/11/24 07:36:10
Note that WriteUnicodeCharacter() was not exposed,
| |
1660 | 1660 |
1661 p = next; | 1661 p = next; |
1662 } | 1662 } |
1663 | 1663 |
1664 if (filtered_text.length()) { | 1664 string16 sanitized_text; |
Peter Kasting
2011/11/23 19:02:11
Nit: Simpler:
if (model_->is_pasting()) {
f
SanjoyPal
2011/11/24 07:36:10
Done.
| |
1665 if (model_->is_pasting()) | |
1666 sanitized_text = StripJavascriptSchemas( | |
1667 CollapseWhitespace(filtered_text, true)); | |
1668 else | |
1669 sanitized_text = filtered_text; | |
1670 | |
1671 if (!sanitized_text.empty()) { | |
1665 // Avoid inserting the text after the instant anchor. | 1672 // Avoid inserting the text after the instant anchor. |
1666 ValidateTextBufferIter(location); | 1673 ValidateTextBufferIter(location); |
1667 | 1674 |
1668 // Call the default handler to insert filtered text. | 1675 // Call the default handler to insert filtered text. |
1669 GtkTextBufferClass* klass = GTK_TEXT_BUFFER_GET_CLASS(buffer); | 1676 GtkTextBufferClass* klass = GTK_TEXT_BUFFER_GET_CLASS(buffer); |
1670 klass->insert_text(buffer, location, filtered_text.data(), | 1677 std::string utf8_text = UTF16ToUTF8(sanitized_text); |
1671 static_cast<gint>(filtered_text.length())); | 1678 klass->insert_text(buffer, location, utf8_text.data(), |
1679 static_cast<gint>(utf8_text.length())); | |
1672 } | 1680 } |
1673 | 1681 |
1674 // Stop propagating the signal emission to prevent the default handler from | 1682 // Stop propagating the signal emission to prevent the default handler from |
1675 // being called again. | 1683 // being called again. |
1676 static guint signal_id = g_signal_lookup("insert-text", GTK_TYPE_TEXT_BUFFER); | 1684 static guint signal_id = g_signal_lookup("insert-text", GTK_TYPE_TEXT_BUFFER); |
1677 g_signal_stop_emission(buffer, signal_id, 0); | 1685 g_signal_stop_emission(buffer, signal_id, 0); |
1678 } | 1686 } |
1679 | 1687 |
1680 void OmniboxViewGtk::HandleBackSpace(GtkWidget* sender) { | 1688 void OmniboxViewGtk::HandleBackSpace(GtkWidget* sender) { |
1681 // Checks if it's currently in keyword search mode. | 1689 // Checks if it's currently in keyword search mode. |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1788 0); | 1796 0); |
1789 | 1797 |
1790 if (!copy && gtk_text_view_get_editable(GTK_TEXT_VIEW(text_view_))) | 1798 if (!copy && gtk_text_view_get_editable(GTK_TEXT_VIEW(text_view_))) |
1791 gtk_text_buffer_delete_selection(text_buffer_, true, true); | 1799 gtk_text_buffer_delete_selection(text_buffer_, true, true); |
1792 } | 1800 } |
1793 | 1801 |
1794 OwnPrimarySelection(UTF16ToUTF8(text)); | 1802 OwnPrimarySelection(UTF16ToUTF8(text)); |
1795 } | 1803 } |
1796 | 1804 |
1797 bool OmniboxViewGtk::OnPerformDropImpl(const string16& text) { | 1805 bool OmniboxViewGtk::OnPerformDropImpl(const string16& text) { |
1798 if (model_->CanPasteAndGo(CollapseWhitespace(text, true))) { | 1806 if (model_->CanPasteAndGo(StripJavascriptSchemas |
1807 (CollapseWhitespace(text, true)))) { | |
Peter Kasting
2011/11/23 19:02:11
Nit: Put '(' on previous line.
SanjoyPal
2011/11/24 07:36:10
Done.
| |
1799 model_->PasteAndGo(); | 1808 model_->PasteAndGo(); |
1800 return true; | 1809 return true; |
1801 } | 1810 } |
1802 | 1811 |
1803 return false; | 1812 return false; |
1804 } | 1813 } |
1805 | 1814 |
1806 gfx::Font OmniboxViewGtk::GetFont() { | 1815 gfx::Font OmniboxViewGtk::GetFont() { |
1807 #if defined(TOOLKIT_VIEWS) | 1816 #if defined(TOOLKIT_VIEWS) |
1808 bool use_gtk = false; | 1817 bool use_gtk = false; |
(...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2394 | 2403 |
2395 // Make all the children of the widget visible. NOTE: this won't display | 2404 // Make all the children of the widget visible. NOTE: this won't display |
2396 // anything, it just toggles the visible flag. | 2405 // anything, it just toggles the visible flag. |
2397 gtk_widget_show_all(omnibox_view->GetNativeView()); | 2406 gtk_widget_show_all(omnibox_view->GetNativeView()); |
2398 // Hide the widget. NativeViewHostGtk will make it visible again as necessary. | 2407 // Hide the widget. NativeViewHostGtk will make it visible again as necessary. |
2399 gtk_widget_hide(omnibox_view->GetNativeView()); | 2408 gtk_widget_hide(omnibox_view->GetNativeView()); |
2400 | 2409 |
2401 return omnibox_view; | 2410 return omnibox_view; |
2402 } | 2411 } |
2403 #endif | 2412 #endif |
OLD | NEW |