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

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: rebase 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 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 gtk_text_buffer_get_iter_at_mark(buffer, &selection_bound, 139 gtk_text_buffer_get_iter_at_mark(buffer, &selection_bound,
140 gtk_text_buffer_get_selection_bound(buffer)); 140 gtk_text_buffer_get_selection_bound(buffer));
141 141
142 if (!gtk_text_iter_equal(&insert, &selection_bound)) { 142 if (!gtk_text_iter_equal(&insert, &selection_bound)) {
143 gtk_text_buffer_move_mark(buffer, 143 gtk_text_buffer_move_mark(buffer,
144 gtk_text_buffer_get_selection_bound(buffer), 144 gtk_text_buffer_get_selection_bound(buffer),
145 &insert); 145 &insert);
146 } 146 }
147 } 147 }
148 148
149 // Returns the |menu| item whose label matches |label|.
150 guint GetPopupMenuIndexForStockLabel(const char* label, GtkMenu* menu) {
151 string16 stock_label(UTF8ToUTF16(label));
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 string16 menu_item_label = UTF8ToUTF16(
160 gtk_menu_item_get_label(GTK_MENU_ITEM(item->data)));
161 if (menu_item_label == stock_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 (toolbar_model()->WouldReplaceSearchURLWithSearchTerms() &&
1290 !model()->user_input_in_progress()) {
1291 GtkWidget* copy_url_menuitem = gtk_menu_item_new_with_mnemonic(
1292 ui::ConvertAcceleratorsFromWindowsStyle(
1293 l10n_util::GetStringUTF8(IDS_COPY_URL)).c_str());
1294 // If we don't find the stock Copy menu item, the Copy URL item will be
1295 // appended at the end of the popup menu.
1296 gtk_menu_shell_insert(GTK_MENU_SHELL(menu), copy_url_menuitem,
1297 GetPopupMenuIndexForStockLabel(GTK_STOCK_COPY, menu));
1298 g_signal_connect(copy_url_menuitem, "activate",
1299 G_CALLBACK(HandleCopyURLClipboardThunk), this);
1300 gtk_widget_set_sensitive(copy_url_menuitem,
1301 command_updater()->IsCommandEnabled(IDC_COPY_URL));
1302 gtk_widget_show(copy_url_menuitem);
1303 }
1304
1305 // Paste and Go menu item.
1288 GtkWidget* paste_go_menuitem = gtk_menu_item_new_with_mnemonic( 1306 GtkWidget* paste_go_menuitem = gtk_menu_item_new_with_mnemonic(
1289 ui::ConvertAcceleratorsFromWindowsStyle(l10n_util::GetStringUTF8( 1307 ui::ConvertAcceleratorsFromWindowsStyle(l10n_util::GetStringUTF8(
1290 model()->IsPasteAndSearch(sanitized_text_for_paste_and_go_) ? 1308 model()->IsPasteAndSearch(sanitized_text_for_paste_and_go_) ?
1291 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO)).c_str()); 1309 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO)).c_str());
1292 gtk_menu_shell_insert(GTK_MENU_SHELL(menu), paste_go_menuitem, index); 1310
1311 // Detect the Paste and Copy menu items by searching for the ones that uses
1312 // the stock labels (i.e. gtk-paste and gtk-copy).
1313
1314 // If we don't find the stock Paste menu item, the Paste and Go item will be
1315 // appended at the end of the popup menu.
1316 gtk_menu_shell_insert(GTK_MENU_SHELL(menu), paste_go_menuitem,
1317 GetPopupMenuIndexForStockLabel(GTK_STOCK_PASTE, menu));
1318
1293 g_signal_connect(paste_go_menuitem, "activate", 1319 g_signal_connect(paste_go_menuitem, "activate",
1294 G_CALLBACK(HandlePasteAndGoThunk), this); 1320 G_CALLBACK(HandlePasteAndGoThunk), this);
1295 gtk_widget_set_sensitive(paste_go_menuitem, 1321 gtk_widget_set_sensitive(paste_go_menuitem,
1296 model()->CanPasteAndGo(sanitized_text_for_paste_and_go_)); 1322 model()->CanPasteAndGo(sanitized_text_for_paste_and_go_));
1297 gtk_widget_show(paste_go_menuitem); 1323 gtk_widget_show(paste_go_menuitem);
1298 1324
1299 g_signal_connect(menu, "deactivate", 1325 g_signal_connect(menu, "deactivate",
1300 G_CALLBACK(HandlePopupMenuDeactivateThunk), this); 1326 G_CALLBACK(HandlePopupMenuDeactivateThunk), this);
1301 } 1327 }
1302 1328
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
1570 if (handled) { 1596 if (handled) {
1571 static guint signal_id = g_signal_lookup("move-focus", GTK_TYPE_WIDGET); 1597 static guint signal_id = g_signal_lookup("move-focus", GTK_TYPE_WIDGET);
1572 g_signal_stop_emission(widget, signal_id, 0); 1598 g_signal_stop_emission(widget, signal_id, 0);
1573 } 1599 }
1574 } 1600 }
1575 1601
1576 void OmniboxViewGtk::HandleCopyClipboard(GtkWidget* sender) { 1602 void OmniboxViewGtk::HandleCopyClipboard(GtkWidget* sender) {
1577 HandleCopyOrCutClipboard(true); 1603 HandleCopyOrCutClipboard(true);
1578 } 1604 }
1579 1605
1606 void OmniboxViewGtk::HandleCopyURLClipboard(GtkWidget* sender) {
1607 const GURL& url = toolbar_model()->GetURL();
1608 const string16& text = toolbar_model()->GetText(false);
1609
1610 BookmarkNodeData data;
1611 data.ReadFromTuple(url, text);
1612 data.WriteToClipboard(NULL);
1613 }
1614
1580 void OmniboxViewGtk::HandleCutClipboard(GtkWidget* sender) { 1615 void OmniboxViewGtk::HandleCutClipboard(GtkWidget* sender) {
1581 HandleCopyOrCutClipboard(false); 1616 HandleCopyOrCutClipboard(false);
1582 } 1617 }
1583 1618
1584 void OmniboxViewGtk::HandleCopyOrCutClipboard(bool copy) { 1619 void OmniboxViewGtk::HandleCopyOrCutClipboard(bool copy) {
1585 DCHECK(text_view_); 1620 DCHECK(text_view_);
1586 1621
1587 // On copy or cut, we manually update the PRIMARY selection to contain the 1622 // 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 1623 // 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 1624 // 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() { 2203 void OmniboxViewGtk::AdjustVerticalAlignmentOfInstantView() {
2169 // By default, GtkTextView layouts an anchored child widget just above the 2204 // 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 2205 // baseline, so we need to move the |instant_view_| down to make sure it
2171 // has the same baseline as the |text_view_|. 2206 // has the same baseline as the |text_view_|.
2172 PangoLayout* layout = gtk_label_get_layout(GTK_LABEL(instant_view_)); 2207 PangoLayout* layout = gtk_label_get_layout(GTK_LABEL(instant_view_));
2173 int height; 2208 int height;
2174 pango_layout_get_size(layout, NULL, &height); 2209 pango_layout_get_size(layout, NULL, &height);
2175 int baseline = pango_layout_get_baseline(layout); 2210 int baseline = pango_layout_get_baseline(layout);
2176 g_object_set(instant_anchor_tag_, "rise", baseline - height, NULL); 2211 g_object_set(instant_anchor_tag_, "rise", baseline - height, NULL);
2177 } 2212 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698