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

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: Always add copy url item 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 std::string stock_label(label);
Evan Stade 2012/09/06 01:35:03 you don't need this variable. compare directly to
dominich 2012/09/06 17:50:25 Done.
153 GList* list = gtk_container_get_children(GTK_CONTAINER(menu));
154 guint index = 1;
155 for (GList* item = list; item != NULL; item = item->next, ++index) {
156 if (GTK_IS_IMAGE_MENU_ITEM(item->data)) {
157 gboolean is_stock = gtk_image_menu_item_get_use_stock(
158 GTK_IMAGE_MENU_ITEM(item->data));
159 if (is_stock) {
160 std::string menu_item_label =
161 gtk_menu_item_get_label(GTK_MENU_ITEM(item->data));
162 if (menu_item_label == stock_label)
163 break;
164 }
165 }
166 }
167 g_list_free(list);
168 return index;
169 }
170
149 } // namespace 171 } // namespace
150 172
151 OmniboxViewGtk::OmniboxViewGtk(OmniboxEditController* controller, 173 OmniboxViewGtk::OmniboxViewGtk(OmniboxEditController* controller,
152 ToolbarModel* toolbar_model, 174 ToolbarModel* toolbar_model,
153 Browser* browser, 175 Browser* browser,
154 CommandUpdater* command_updater, 176 CommandUpdater* command_updater,
155 bool popup_window_mode, 177 bool popup_window_mode,
156 GtkWidget* location_bar) 178 GtkWidget* location_bar)
157 : OmniboxView(browser->profile(), controller, toolbar_model, 179 : OmniboxView(browser->profile(), controller, toolbar_model,
158 command_updater), 180 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( 1272 GtkWidget* search_engine_menuitem = gtk_menu_item_new_with_mnemonic(
1251 ui::ConvertAcceleratorsFromWindowsStyle( 1273 ui::ConvertAcceleratorsFromWindowsStyle(
1252 l10n_util::GetStringUTF8(IDS_EDIT_SEARCH_ENGINES)).c_str()); 1274 l10n_util::GetStringUTF8(IDS_EDIT_SEARCH_ENGINES)).c_str());
1253 gtk_menu_shell_append(GTK_MENU_SHELL(menu), search_engine_menuitem); 1275 gtk_menu_shell_append(GTK_MENU_SHELL(menu), search_engine_menuitem);
1254 g_signal_connect(search_engine_menuitem, "activate", 1276 g_signal_connect(search_engine_menuitem, "activate",
1255 G_CALLBACK(HandleEditSearchEnginesThunk), this); 1277 G_CALLBACK(HandleEditSearchEnginesThunk), this);
1256 gtk_widget_set_sensitive(search_engine_menuitem, 1278 gtk_widget_set_sensitive(search_engine_menuitem,
1257 command_updater()->IsCommandEnabled(IDC_EDIT_SEARCH_ENGINES)); 1279 command_updater()->IsCommandEnabled(IDC_EDIT_SEARCH_ENGINES));
1258 gtk_widget_show(search_engine_menuitem); 1280 gtk_widget_show(search_engine_menuitem);
1259 1281
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); 1282 GtkClipboard* x_clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1283 gchar* text = gtk_clipboard_wait_for_text(x_clipboard); 1283 gchar* text = gtk_clipboard_wait_for_text(x_clipboard);
1284 sanitized_text_for_paste_and_go_ = text ? 1284 sanitized_text_for_paste_and_go_ = text ?
1285 StripJavascriptSchemas(CollapseWhitespace(UTF8ToUTF16(text), true)) : 1285 StripJavascriptSchemas(CollapseWhitespace(UTF8ToUTF16(text), true)) :
1286 string16(); 1286 string16();
1287 g_free(text); 1287 g_free(text);
1288
1289 // Copy URL menu item.
1290 if (chrome::search::IsInstantExtendedAPIEnabled(browser_->profile())) {
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(
1301 copy_url_menuitem,
1302 toolbar_model()->WouldReplaceSearchURLWithSearchTerms() &&
1303 !model()->user_input_in_progress());
1304 gtk_widget_show(copy_url_menuitem);
1305 }
1306
1307 // Paste and Go menu item.
1288 GtkWidget* paste_go_menuitem = gtk_menu_item_new_with_mnemonic( 1308 GtkWidget* paste_go_menuitem = gtk_menu_item_new_with_mnemonic(
1289 ui::ConvertAcceleratorsFromWindowsStyle(l10n_util::GetStringUTF8( 1309 ui::ConvertAcceleratorsFromWindowsStyle(l10n_util::GetStringUTF8(
1290 model()->IsPasteAndSearch(sanitized_text_for_paste_and_go_) ? 1310 model()->IsPasteAndSearch(sanitized_text_for_paste_and_go_) ?
1291 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO)).c_str()); 1311 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO)).c_str());
1292 gtk_menu_shell_insert(GTK_MENU_SHELL(menu), paste_go_menuitem, index); 1312
1313 // Detect the Paste and Copy menu items by searching for the ones that uses
1314 // the stock labels (i.e. gtk-paste and gtk-copy).
1315
1316 // If we don't find the stock Paste menu item, the Paste and Go item will be
1317 // appended at the end of the popup menu.
1318 gtk_menu_shell_insert(GTK_MENU_SHELL(menu), paste_go_menuitem,
1319 GetPopupMenuIndexForStockLabel(GTK_STOCK_PASTE, menu));
1320
1293 g_signal_connect(paste_go_menuitem, "activate", 1321 g_signal_connect(paste_go_menuitem, "activate",
1294 G_CALLBACK(HandlePasteAndGoThunk), this); 1322 G_CALLBACK(HandlePasteAndGoThunk), this);
1295 gtk_widget_set_sensitive(paste_go_menuitem, 1323 gtk_widget_set_sensitive(paste_go_menuitem,
1296 model()->CanPasteAndGo(sanitized_text_for_paste_and_go_)); 1324 model()->CanPasteAndGo(sanitized_text_for_paste_and_go_));
1297 gtk_widget_show(paste_go_menuitem); 1325 gtk_widget_show(paste_go_menuitem);
1298 1326
1299 g_signal_connect(menu, "deactivate", 1327 g_signal_connect(menu, "deactivate",
1300 G_CALLBACK(HandlePopupMenuDeactivateThunk), this); 1328 G_CALLBACK(HandlePopupMenuDeactivateThunk), this);
1301 } 1329 }
1302 1330
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
1570 if (handled) { 1598 if (handled) {
1571 static guint signal_id = g_signal_lookup("move-focus", GTK_TYPE_WIDGET); 1599 static guint signal_id = g_signal_lookup("move-focus", GTK_TYPE_WIDGET);
1572 g_signal_stop_emission(widget, signal_id, 0); 1600 g_signal_stop_emission(widget, signal_id, 0);
1573 } 1601 }
1574 } 1602 }
1575 1603
1576 void OmniboxViewGtk::HandleCopyClipboard(GtkWidget* sender) { 1604 void OmniboxViewGtk::HandleCopyClipboard(GtkWidget* sender) {
1577 HandleCopyOrCutClipboard(true); 1605 HandleCopyOrCutClipboard(true);
1578 } 1606 }
1579 1607
1608 void OmniboxViewGtk::HandleCopyURLClipboard(GtkWidget* sender) {
1609 const GURL& url = toolbar_model()->GetURL();
1610 const string16& text = toolbar_model()->GetText(false);
1611
1612 BookmarkNodeData data;
1613 data.ReadFromTuple(url, text);
1614 data.WriteToClipboard(NULL);
1615 }
1616
1580 void OmniboxViewGtk::HandleCutClipboard(GtkWidget* sender) { 1617 void OmniboxViewGtk::HandleCutClipboard(GtkWidget* sender) {
1581 HandleCopyOrCutClipboard(false); 1618 HandleCopyOrCutClipboard(false);
1582 } 1619 }
1583 1620
1584 void OmniboxViewGtk::HandleCopyOrCutClipboard(bool copy) { 1621 void OmniboxViewGtk::HandleCopyOrCutClipboard(bool copy) {
1585 DCHECK(text_view_); 1622 DCHECK(text_view_);
1586 1623
1587 // On copy or cut, we manually update the PRIMARY selection to contain the 1624 // 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 1625 // 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 1626 // 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() { 2205 void OmniboxViewGtk::AdjustVerticalAlignmentOfInstantView() {
2169 // By default, GtkTextView layouts an anchored child widget just above the 2206 // 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 2207 // baseline, so we need to move the |instant_view_| down to make sure it
2171 // has the same baseline as the |text_view_|. 2208 // has the same baseline as the |text_view_|.
2172 PangoLayout* layout = gtk_label_get_layout(GTK_LABEL(instant_view_)); 2209 PangoLayout* layout = gtk_label_get_layout(GTK_LABEL(instant_view_));
2173 int height; 2210 int height;
2174 pango_layout_get_size(layout, NULL, &height); 2211 pango_layout_get_size(layout, NULL, &height);
2175 int baseline = pango_layout_get_baseline(layout); 2212 int baseline = pango_layout_get_baseline(layout);
2176 g_object_set(instant_anchor_tag_, "rise", baseline - height, NULL); 2213 g_object_set(instant_anchor_tag_, "rise", baseline - height, NULL);
2177 } 2214 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.h ('k') | chrome/browser/ui/omnibox/omnibox_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698