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

Side by Side Diff: chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.cc

Issue 10915069: Add Copy URL option to Omnibox context menu when URL is replaced by Instant Extended. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ShouldAllowCopyURLMenu fix Created 8 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 13 matching lines...) Expand all
24 #include "chrome/browser/ui/browser.h" 24 #include "chrome/browser/ui/browser.h"
25 #include "chrome/browser/ui/browser_tabstrip.h" 25 #include "chrome/browser/ui/browser_tabstrip.h"
26 #include "chrome/browser/ui/gtk/gtk_theme_service.h" 26 #include "chrome/browser/ui/gtk/gtk_theme_service.h"
27 #include "chrome/browser/ui/gtk/gtk_util.h" 27 #include "chrome/browser/ui/gtk/gtk_util.h"
28 #include "chrome/browser/ui/gtk/location_bar_view_gtk.h" 28 #include "chrome/browser/ui/gtk/location_bar_view_gtk.h"
29 #include "chrome/browser/ui/gtk/omnibox/omnibox_popup_view_gtk.h" 29 #include "chrome/browser/ui/gtk/omnibox/omnibox_popup_view_gtk.h"
30 #include "chrome/browser/ui/gtk/view_id_util.h" 30 #include "chrome/browser/ui/gtk/view_id_util.h"
31 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h" 31 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
32 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" 32 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
33 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" 33 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
34 #include "chrome/browser/ui/search/search.h"
34 #include "chrome/browser/ui/toolbar/toolbar_model.h" 35 #include "chrome/browser/ui/toolbar/toolbar_model.h"
35 #include "chrome/common/chrome_notification_types.h" 36 #include "chrome/common/chrome_notification_types.h"
36 #include "content/public/browser/notification_source.h" 37 #include "content/public/browser/notification_source.h"
37 #include "content/public/browser/web_contents.h" 38 #include "content/public/browser/web_contents.h"
38 #include "googleurl/src/gurl.h" 39 #include "googleurl/src/gurl.h"
39 #include "grit/generated_resources.h" 40 #include "grit/generated_resources.h"
40 #include "net/base/escape.h" 41 #include "net/base/escape.h"
41 #include "third_party/undoview/undo_view.h" 42 #include "third_party/undoview/undo_view.h"
42 #include "ui/base/animation/multi_animation.h" 43 #include "ui/base/animation/multi_animation.h"
43 #include "ui/base/dragdrop/drag_drop_types.h" 44 #include "ui/base/dragdrop/drag_drop_types.h"
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 gtk_text_buffer_get_iter_at_mark(buffer, &selection_bound, 140 gtk_text_buffer_get_iter_at_mark(buffer, &selection_bound,
140 gtk_text_buffer_get_selection_bound(buffer)); 141 gtk_text_buffer_get_selection_bound(buffer));
141 142
142 if (!gtk_text_iter_equal(&insert, &selection_bound)) { 143 if (!gtk_text_iter_equal(&insert, &selection_bound)) {
143 gtk_text_buffer_move_mark(buffer, 144 gtk_text_buffer_move_mark(buffer,
144 gtk_text_buffer_get_selection_bound(buffer), 145 gtk_text_buffer_get_selection_bound(buffer),
145 &insert); 146 &insert);
146 } 147 }
147 } 148 }
148 149
150 // Returns the |menu| item whose label matches |label|.
151 guint GetPopupMenuIndexForStockLabel(const char* label, GtkMenu* menu) {
152 GList* list = gtk_container_get_children(GTK_CONTAINER(menu));
153 guint index = 1;
154 for (GList* item = list; item != NULL; item = item->next, ++index) {
155 if (GTK_IS_IMAGE_MENU_ITEM(item->data)) {
156 gboolean is_stock = gtk_image_menu_item_get_use_stock(
157 GTK_IMAGE_MENU_ITEM(item->data));
158 if (is_stock) {
159 std::string menu_item_label =
160 gtk_menu_item_get_label(GTK_MENU_ITEM(item->data));
161 if (menu_item_label == label)
162 break;
163 }
164 }
165 }
166 g_list_free(list);
167 return index;
168 }
169
149 } // namespace 170 } // namespace
150 171
151 OmniboxViewGtk::OmniboxViewGtk(OmniboxEditController* controller, 172 OmniboxViewGtk::OmniboxViewGtk(OmniboxEditController* controller,
152 ToolbarModel* toolbar_model, 173 ToolbarModel* toolbar_model,
153 Browser* browser, 174 Browser* browser,
154 CommandUpdater* command_updater, 175 CommandUpdater* command_updater,
155 bool popup_window_mode, 176 bool popup_window_mode,
156 GtkWidget* location_bar) 177 GtkWidget* location_bar)
157 : OmniboxView(browser->profile(), controller, toolbar_model, 178 : OmniboxView(browser->profile(), controller, toolbar_model,
158 command_updater), 179 command_updater),
(...skipping 1091 matching lines...) Expand 10 before | Expand all | Expand 10 after
1250 GtkWidget* search_engine_menuitem = gtk_menu_item_new_with_mnemonic( 1271 GtkWidget* search_engine_menuitem = gtk_menu_item_new_with_mnemonic(
1251 ui::ConvertAcceleratorsFromWindowsStyle( 1272 ui::ConvertAcceleratorsFromWindowsStyle(
1252 l10n_util::GetStringUTF8(IDS_EDIT_SEARCH_ENGINES)).c_str()); 1273 l10n_util::GetStringUTF8(IDS_EDIT_SEARCH_ENGINES)).c_str());
1253 gtk_menu_shell_append(GTK_MENU_SHELL(menu), search_engine_menuitem); 1274 gtk_menu_shell_append(GTK_MENU_SHELL(menu), search_engine_menuitem);
1254 g_signal_connect(search_engine_menuitem, "activate", 1275 g_signal_connect(search_engine_menuitem, "activate",
1255 G_CALLBACK(HandleEditSearchEnginesThunk), this); 1276 G_CALLBACK(HandleEditSearchEnginesThunk), this);
1256 gtk_widget_set_sensitive(search_engine_menuitem, 1277 gtk_widget_set_sensitive(search_engine_menuitem,
1257 command_updater()->IsCommandEnabled(IDC_EDIT_SEARCH_ENGINES)); 1278 command_updater()->IsCommandEnabled(IDC_EDIT_SEARCH_ENGINES));
1258 gtk_widget_show(search_engine_menuitem); 1279 gtk_widget_show(search_engine_menuitem);
1259 1280
1260 // Detect the Paste menu item by searching for the one that
1261 // uses the stock Paste label (i.e. gtk-paste).
1262 string16 stock_paste_label(UTF8ToUTF16(GTK_STOCK_PASTE));
1263 GList* list = gtk_container_get_children(GTK_CONTAINER(menu));
1264 guint index = 1;
1265 for (GList* item = list; item != NULL; item = item->next, ++index) {
1266 if (GTK_IS_IMAGE_MENU_ITEM(item->data)) {
1267 gboolean is_stock = gtk_image_menu_item_get_use_stock(
1268 GTK_IMAGE_MENU_ITEM(item->data));
1269 if (is_stock) {
1270 string16 menu_item_label
1271 (UTF8ToUTF16(gtk_menu_item_get_label(GTK_MENU_ITEM(item->data))));
1272 if (menu_item_label == stock_paste_label) {
1273 break;
1274 }
1275 }
1276 }
1277 }
1278 g_list_free(list);
1279
1280 // If we don't find the stock Paste menu item,
1281 // the Paste and Go item will be appended at the end of the popup menu.
1282 GtkClipboard* x_clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); 1281 GtkClipboard* x_clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1283 gchar* text = gtk_clipboard_wait_for_text(x_clipboard); 1282 gchar* text = gtk_clipboard_wait_for_text(x_clipboard);
1284 sanitized_text_for_paste_and_go_ = text ? 1283 sanitized_text_for_paste_and_go_ = text ?
1285 StripJavascriptSchemas(CollapseWhitespace(UTF8ToUTF16(text), true)) : 1284 StripJavascriptSchemas(CollapseWhitespace(UTF8ToUTF16(text), true)) :
1286 string16(); 1285 string16();
1287 g_free(text); 1286 g_free(text);
1287
1288 // Copy URL menu item.
1289 if (chrome::search::IsInstantExtendedAPIEnabled(browser_->profile())) {
1290 GtkWidget* copy_url_menuitem = gtk_menu_item_new_with_mnemonic(
1291 ui::ConvertAcceleratorsFromWindowsStyle(
1292 l10n_util::GetStringUTF8(IDS_COPY_URL)).c_str());
1293 // If we don't find the stock Copy menu item, the Copy URL item will be
1294 // appended at the end of the popup menu.
1295 gtk_menu_shell_insert(GTK_MENU_SHELL(menu), copy_url_menuitem,
1296 GetPopupMenuIndexForStockLabel(GTK_STOCK_COPY, menu));
1297 g_signal_connect(copy_url_menuitem, "activate",
1298 G_CALLBACK(HandleCopyURLClipboardThunk), this);
1299 gtk_widget_set_sensitive(
1300 copy_url_menuitem,
1301 toolbar_model()->WouldReplaceSearchURLWithSearchTerms() &&
1302 !model()->user_input_in_progress());
1303 gtk_widget_show(copy_url_menuitem);
1304 }
1305
1306 // Paste and Go menu item.
1288 GtkWidget* paste_go_menuitem = gtk_menu_item_new_with_mnemonic( 1307 GtkWidget* paste_go_menuitem = gtk_menu_item_new_with_mnemonic(
1289 ui::ConvertAcceleratorsFromWindowsStyle(l10n_util::GetStringUTF8( 1308 ui::ConvertAcceleratorsFromWindowsStyle(l10n_util::GetStringUTF8(
1290 model()->IsPasteAndSearch(sanitized_text_for_paste_and_go_) ? 1309 model()->IsPasteAndSearch(sanitized_text_for_paste_and_go_) ?
1291 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO)).c_str()); 1310 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO)).c_str());
1292 gtk_menu_shell_insert(GTK_MENU_SHELL(menu), paste_go_menuitem, index); 1311
1312 // Detect the Paste and Copy menu items by searching for the ones that uses
Peter Kasting 2012/09/11 21:52:59 Nit: uses -> use
dominich 2012/09/12 15:23:09 Done.
1313 // the stock labels (i.e. gtk-paste and gtk-copy).
Peter Kasting 2012/09/11 21:52:59 Nit: This comment seems a bit out of place given t
dominich 2012/09/12 15:23:09 Done.
1314
1315 // If we don't find the stock Paste menu item, the Paste and Go item will be
1316 // appended at the end of the popup menu.
1317 gtk_menu_shell_insert(GTK_MENU_SHELL(menu), paste_go_menuitem,
1318 GetPopupMenuIndexForStockLabel(GTK_STOCK_PASTE, menu));
1319
1293 g_signal_connect(paste_go_menuitem, "activate", 1320 g_signal_connect(paste_go_menuitem, "activate",
1294 G_CALLBACK(HandlePasteAndGoThunk), this); 1321 G_CALLBACK(HandlePasteAndGoThunk), this);
1295 gtk_widget_set_sensitive(paste_go_menuitem, 1322 gtk_widget_set_sensitive(paste_go_menuitem,
1296 model()->CanPasteAndGo(sanitized_text_for_paste_and_go_)); 1323 model()->CanPasteAndGo(sanitized_text_for_paste_and_go_));
1297 gtk_widget_show(paste_go_menuitem); 1324 gtk_widget_show(paste_go_menuitem);
1298 1325
1299 g_signal_connect(menu, "deactivate", 1326 g_signal_connect(menu, "deactivate",
1300 G_CALLBACK(HandlePopupMenuDeactivateThunk), this); 1327 G_CALLBACK(HandlePopupMenuDeactivateThunk), this);
1301 } 1328 }
1302 1329
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
1570 if (handled) { 1597 if (handled) {
1571 static guint signal_id = g_signal_lookup("move-focus", GTK_TYPE_WIDGET); 1598 static guint signal_id = g_signal_lookup("move-focus", GTK_TYPE_WIDGET);
1572 g_signal_stop_emission(widget, signal_id, 0); 1599 g_signal_stop_emission(widget, signal_id, 0);
1573 } 1600 }
1574 } 1601 }
1575 1602
1576 void OmniboxViewGtk::HandleCopyClipboard(GtkWidget* sender) { 1603 void OmniboxViewGtk::HandleCopyClipboard(GtkWidget* sender) {
1577 HandleCopyOrCutClipboard(true); 1604 HandleCopyOrCutClipboard(true);
1578 } 1605 }
1579 1606
1607 void OmniboxViewGtk::HandleCopyURLClipboard(GtkWidget* sender) {
1608 const GURL& url = toolbar_model()->GetURL();
1609 const string16& text = toolbar_model()->GetText(false);
1610
1611 BookmarkNodeData data;
Peter Kasting 2012/09/11 21:52:59 Nit: Would be nice to factor out this chunk from h
dominich 2012/09/12 15:23:09 No - it's an activate signal on the menu item rath
1612 data.ReadFromTuple(url, text);
1613 data.WriteToClipboard(NULL);
1614 }
1615
1580 void OmniboxViewGtk::HandleCutClipboard(GtkWidget* sender) { 1616 void OmniboxViewGtk::HandleCutClipboard(GtkWidget* sender) {
1581 HandleCopyOrCutClipboard(false); 1617 HandleCopyOrCutClipboard(false);
1582 } 1618 }
1583 1619
1584 void OmniboxViewGtk::HandleCopyOrCutClipboard(bool copy) { 1620 void OmniboxViewGtk::HandleCopyOrCutClipboard(bool copy) {
1585 DCHECK(text_view_); 1621 DCHECK(text_view_);
1586 1622
1587 // On copy or cut, we manually update the PRIMARY selection to contain the 1623 // On copy or cut, we manually update the PRIMARY selection to contain the
1588 // highlighted text. This matches Firefox -- we highlight the URL but don't 1624 // highlighted text. This matches Firefox -- we highlight the URL but don't
1589 // update PRIMARY on Ctrl-L, so Ctrl-L, Ctrl-C and then middle-click is a 1625 // update PRIMARY on Ctrl-L, so Ctrl-L, Ctrl-C and then middle-click is a
(...skipping 578 matching lines...) Expand 10 before | Expand all | Expand 10 after
2168 void OmniboxViewGtk::AdjustVerticalAlignmentOfInstantView() { 2204 void OmniboxViewGtk::AdjustVerticalAlignmentOfInstantView() {
2169 // By default, GtkTextView layouts an anchored child widget just above the 2205 // By default, GtkTextView layouts an anchored child widget just above the
2170 // baseline, so we need to move the |instant_view_| down to make sure it 2206 // baseline, so we need to move the |instant_view_| down to make sure it
2171 // has the same baseline as the |text_view_|. 2207 // has the same baseline as the |text_view_|.
2172 PangoLayout* layout = gtk_label_get_layout(GTK_LABEL(instant_view_)); 2208 PangoLayout* layout = gtk_label_get_layout(GTK_LABEL(instant_view_));
2173 int height; 2209 int height;
2174 pango_layout_get_size(layout, NULL, &height); 2210 pango_layout_get_size(layout, NULL, &height);
2175 int baseline = pango_layout_get_baseline(layout); 2211 int baseline = pango_layout_get_baseline(layout);
2176 g_object_set(instant_anchor_tag_, "rise", baseline - height, NULL); 2212 g_object_set(instant_anchor_tag_, "rise", baseline - height, NULL);
2177 } 2213 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698