OLD | NEW |
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/browser_actions_toolbar_gtk.h" | 5 #include "chrome/browser/ui/gtk/browser_actions_toolbar_gtk.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <utility> |
10 #include <vector> | 11 #include <vector> |
11 | 12 |
12 #include "base/bind.h" | 13 #include "base/bind.h" |
13 #include "base/i18n/rtl.h" | 14 #include "base/i18n/rtl.h" |
14 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
16 #include "chrome/browser/chrome_notification_types.h" | 17 #include "chrome/browser/chrome_notification_types.h" |
17 #include "chrome/browser/extensions/api/commands/command_service.h" | 18 #include "chrome/browser/extensions/api/commands/command_service.h" |
18 #include "chrome/browser/extensions/extension_action.h" | 19 #include "chrome/browser/extensions/extension_action.h" |
19 #include "chrome/browser/extensions/extension_action_icon_factory.h" | 20 #include "chrome/browser/extensions/extension_action_icon_factory.h" |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 return NULL; | 247 return NULL; |
247 | 248 |
248 context_menu_model_ = | 249 context_menu_model_ = |
249 new ExtensionContextMenuModel(extension_, toolbar_->browser(), this); | 250 new ExtensionContextMenuModel(extension_, toolbar_->browser(), this); |
250 context_menu_.reset( | 251 context_menu_.reset( |
251 new MenuGtk(this, context_menu_model_.get())); | 252 new MenuGtk(this, context_menu_model_.get())); |
252 return context_menu_.get(); | 253 return context_menu_.get(); |
253 } | 254 } |
254 | 255 |
255 private: | 256 private: |
256 // Activate the browser action. | 257 // Activate the browser action. Returns true if a popup was shown. Showing the |
257 void Activate(GtkWidget* widget) { | 258 // popup will grant tab permissions if |should_grant| is true. Popup's shown |
| 259 // via an API should not grant permissions. |
| 260 bool Activate(GtkWidget* widget, bool should_grant) { |
258 ExtensionToolbarModel* model = toolbar_->model(); | 261 ExtensionToolbarModel* model = toolbar_->model(); |
259 const Extension* extension = extension_; | 262 const Extension* extension = extension_; |
260 Browser* browser = toolbar_->browser(); | 263 Browser* browser = toolbar_->browser(); |
261 GURL popup_url; | 264 GURL popup_url; |
262 | 265 |
263 switch (model->ExecuteBrowserAction(extension, browser, &popup_url)) { | 266 switch (model->ExecuteBrowserAction( |
| 267 extension, browser, &popup_url, should_grant)) { |
264 case ExtensionToolbarModel::ACTION_NONE: | 268 case ExtensionToolbarModel::ACTION_NONE: |
265 break; | 269 break; |
266 case ExtensionToolbarModel::ACTION_SHOW_POPUP: | 270 case ExtensionToolbarModel::ACTION_SHOW_POPUP: |
267 ExtensionPopupGtk::Show(popup_url, browser, widget, | 271 ExtensionPopupGtk::Show(popup_url, browser, widget, |
268 ExtensionPopupGtk::SHOW); | 272 ExtensionPopupGtk::SHOW); |
269 break; | 273 return true; |
270 } | 274 } |
| 275 return false; |
271 } | 276 } |
272 | 277 |
273 // MenuGtk::Delegate implementation. | 278 // MenuGtk::Delegate implementation. |
274 virtual void StoppedShowing() OVERRIDE { | 279 virtual void StoppedShowing() OVERRIDE { |
275 if (enabled_) | 280 if (enabled_) |
276 button_->UnsetPaintOverride(); | 281 button_->UnsetPaintOverride(); |
277 else | 282 else |
278 button_->SetPaintOverride(GTK_STATE_INSENSITIVE); | 283 button_->SetPaintOverride(GTK_STATE_INSENSITIVE); |
279 | 284 |
280 // If the context menu was showing for the overflow menu, re-assert the | 285 // If the context menu was showing for the overflow menu, re-assert the |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 return FALSE; | 322 return FALSE; |
318 | 323 |
319 button->button_->SetPaintOverride(GTK_STATE_ACTIVE); | 324 button->button_->SetPaintOverride(GTK_STATE_ACTIVE); |
320 menu->PopupForWidget(widget, event->button, event->time); | 325 menu->PopupForWidget(widget, event->button, event->time); |
321 | 326 |
322 return TRUE; | 327 return TRUE; |
323 } | 328 } |
324 | 329 |
325 static void OnClicked(GtkWidget* widget, BrowserActionButton* button) { | 330 static void OnClicked(GtkWidget* widget, BrowserActionButton* button) { |
326 if (button->enabled_) | 331 if (button->enabled_) |
327 button->Activate(widget); | 332 button->Activate(widget, true); |
328 } | 333 } |
329 | 334 |
330 static gboolean OnExposeEvent(GtkWidget* widget, | 335 static gboolean OnExposeEvent(GtkWidget* widget, |
331 GdkEventExpose* event, | 336 GdkEventExpose* event, |
332 BrowserActionButton* button) { | 337 BrowserActionButton* button) { |
333 int tab_id = button->toolbar_->GetCurrentTabId(); | 338 int tab_id = button->toolbar_->GetCurrentTabId(); |
334 if (tab_id < 0) | 339 if (tab_id < 0) |
335 return FALSE; | 340 return FALSE; |
336 | 341 |
337 ExtensionAction* action = button->browser_action(); | 342 ExtensionAction* action = button->browser_action(); |
(...skipping 20 matching lines...) Expand all Loading... |
358 static gboolean OnGtkAccelerator(GtkAccelGroup* accel_group, | 363 static gboolean OnGtkAccelerator(GtkAccelGroup* accel_group, |
359 GObject* acceleratable, | 364 GObject* acceleratable, |
360 guint keyval, | 365 guint keyval, |
361 GdkModifierType modifier, | 366 GdkModifierType modifier, |
362 BrowserActionButton* button) { | 367 BrowserActionButton* button) { |
363 // Open the popup for this extension. | 368 // Open the popup for this extension. |
364 GtkWidget* anchor = button->widget(); | 369 GtkWidget* anchor = button->widget(); |
365 // The anchor might be in the overflow menu. Then we point to the chevron. | 370 // The anchor might be in the overflow menu. Then we point to the chevron. |
366 if (!gtk_widget_get_visible(anchor)) | 371 if (!gtk_widget_get_visible(anchor)) |
367 anchor = button->toolbar_->chevron(); | 372 anchor = button->toolbar_->chevron(); |
368 button->Activate(anchor); | 373 button->Activate(anchor, true); |
369 return TRUE; | 374 return TRUE; |
370 } | 375 } |
371 | 376 |
372 // The handler for when the browser action is realized. |user_data| contains a | 377 // The handler for when the browser action is realized. |user_data| contains a |
373 // pointer to the BrowserAction shown. | 378 // pointer to the BrowserAction shown. |
374 static void OnRealize(GtkWidget* widget, void* user_data) { | 379 static void OnRealize(GtkWidget* widget, void* user_data) { |
375 BrowserActionButton* button = static_cast<BrowserActionButton*>(user_data); | 380 BrowserActionButton* button = static_cast<BrowserActionButton*>(user_data); |
376 button->ConnectBrowserActionPopupAccelerator(); | 381 button->ConnectBrowserActionPopupAccelerator(); |
377 } | 382 } |
378 | 383 |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 signals_.Connect(button->widget(), "show", | 668 signals_.Connect(button->widget(), "show", |
664 G_CALLBACK(&OnButtonShowOrHideThunk), this); | 669 G_CALLBACK(&OnButtonShowOrHideThunk), this); |
665 signals_.Connect(button->widget(), "hide", | 670 signals_.Connect(button->widget(), "hide", |
666 G_CALLBACK(&OnButtonShowOrHideThunk), this); | 671 G_CALLBACK(&OnButtonShowOrHideThunk), this); |
667 | 672 |
668 gtk_widget_show(button->widget()); | 673 gtk_widget_show(button->widget()); |
669 | 674 |
670 UpdateVisibility(); | 675 UpdateVisibility(); |
671 } | 676 } |
672 | 677 |
| 678 BrowserActionButton* BrowserActionsToolbarGtk::GetBrowserActionButton( |
| 679 const Extension* extension) { |
| 680 ExtensionButtonMap::iterator it = extension_button_map_.find( |
| 681 extension->id()); |
| 682 return it == extension_button_map_.end() ? NULL : it->second.get(); |
| 683 } |
| 684 |
673 GtkWidget* BrowserActionsToolbarGtk::GetBrowserActionWidget( | 685 GtkWidget* BrowserActionsToolbarGtk::GetBrowserActionWidget( |
674 const Extension* extension) { | 686 const Extension* extension) { |
675 ExtensionButtonMap::iterator it = extension_button_map_.find( | 687 BrowserActionButton* button = GetBrowserActionButton(extension); |
676 extension->id()); | 688 return button == NULL ? NULL : button->widget(); |
677 if (it == extension_button_map_.end()) | |
678 return NULL; | |
679 | |
680 return it->second.get()->widget(); | |
681 } | 689 } |
682 | 690 |
683 void BrowserActionsToolbarGtk::RemoveButtonForExtension( | 691 void BrowserActionsToolbarGtk::RemoveButtonForExtension( |
684 const Extension* extension) { | 692 const Extension* extension) { |
685 if (extension_button_map_.erase(extension->id())) | 693 if (extension_button_map_.erase(extension->id())) |
686 UpdateVisibility(); | 694 UpdateVisibility(); |
687 UpdateChevronVisibility(); | 695 UpdateChevronVisibility(); |
688 } | 696 } |
689 | 697 |
690 void BrowserActionsToolbarGtk::UpdateVisibility() { | 698 void BrowserActionsToolbarGtk::UpdateVisibility() { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
763 NOTREACHED(); | 771 NOTREACHED(); |
764 return; | 772 return; |
765 } | 773 } |
766 | 774 |
767 if (profile_->IsOffTheRecord()) | 775 if (profile_->IsOffTheRecord()) |
768 index = model_->OriginalIndexToIncognito(index); | 776 index = model_->OriginalIndexToIncognito(index); |
769 | 777 |
770 gtk_box_reorder_child(GTK_BOX(button_hbox_.get()), button_widget, index); | 778 gtk_box_reorder_child(GTK_BOX(button_hbox_.get()), button_widget, index); |
771 } | 779 } |
772 | 780 |
| 781 bool BrowserActionsToolbarGtk::BrowserActionShowPopup( |
| 782 const Extension* extension) { |
| 783 // Do not override other popups and only show in active window. |
| 784 if (ExtensionPopupGtk::get_current_extension_popup() || |
| 785 !browser_->window()->IsActive()) { |
| 786 return false; |
| 787 } |
| 788 |
| 789 BrowserActionButton* button = GetBrowserActionButton(extension); |
| 790 if (button == NULL || button->widget() == NULL) |
| 791 return false; |
| 792 |
| 793 GtkWidget* anchor = button->widget(); |
| 794 if (!gtk_widget_get_visible(anchor)) |
| 795 anchor = button->toolbar_->chevron(); |
| 796 return button->Activate(anchor, false); |
| 797 } |
| 798 |
773 void BrowserActionsToolbarGtk::ModelLoaded() { | 799 void BrowserActionsToolbarGtk::ModelLoaded() { |
774 SetContainerWidth(); | 800 SetContainerWidth(); |
775 } | 801 } |
776 | 802 |
777 void BrowserActionsToolbarGtk::AnimationProgressed( | 803 void BrowserActionsToolbarGtk::AnimationProgressed( |
778 const gfx::Animation* animation) { | 804 const gfx::Animation* animation) { |
779 int width = start_width_ + (desired_width_ - start_width_) * | 805 int width = start_width_ + (desired_width_ - start_width_) * |
780 animation->GetCurrentValue(); | 806 animation->GetCurrentValue(); |
781 gtk_widget_set_size_request(button_hbox_.get(), width, -1); | 807 gtk_widget_set_size_request(button_hbox_.get(), width, -1); |
782 | 808 |
(...skipping 19 matching lines...) Expand all Loading... |
802 bool BrowserActionsToolbarGtk::GetAcceleratorForCommandId( | 828 bool BrowserActionsToolbarGtk::GetAcceleratorForCommandId( |
803 int command_id, | 829 int command_id, |
804 ui::Accelerator* accelerator) { | 830 ui::Accelerator* accelerator) { |
805 return false; | 831 return false; |
806 } | 832 } |
807 | 833 |
808 void BrowserActionsToolbarGtk::ExecuteCommand(int command_id, int event_flags) { | 834 void BrowserActionsToolbarGtk::ExecuteCommand(int command_id, int event_flags) { |
809 const Extension* extension = model_->toolbar_items()[command_id].get(); | 835 const Extension* extension = model_->toolbar_items()[command_id].get(); |
810 GURL popup_url; | 836 GURL popup_url; |
811 | 837 |
812 switch (model_->ExecuteBrowserAction(extension, browser(), &popup_url)) { | 838 switch (model_->ExecuteBrowserAction( |
| 839 extension, browser(), &popup_url, true)) { |
813 case ExtensionToolbarModel::ACTION_NONE: | 840 case ExtensionToolbarModel::ACTION_NONE: |
814 break; | 841 break; |
815 case ExtensionToolbarModel::ACTION_SHOW_POPUP: | 842 case ExtensionToolbarModel::ACTION_SHOW_POPUP: |
816 ExtensionPopupGtk::Show(popup_url, browser(), chevron(), | 843 ExtensionPopupGtk::Show(popup_url, browser(), chevron(), |
817 ExtensionPopupGtk::SHOW); | 844 ExtensionPopupGtk::SHOW); |
818 break; | 845 break; |
819 } | 846 } |
820 } | 847 } |
821 | 848 |
822 void BrowserActionsToolbarGtk::StoppedShowing() { | 849 void BrowserActionsToolbarGtk::StoppedShowing() { |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1072 NOTREACHED(); | 1099 NOTREACHED(); |
1073 return FALSE; | 1100 return FALSE; |
1074 } | 1101 } |
1075 | 1102 |
1076 item_index += gtk_chrome_shrinkable_hbox_get_visible_child_count( | 1103 item_index += gtk_chrome_shrinkable_hbox_get_visible_child_count( |
1077 GTK_CHROME_SHRINKABLE_HBOX(button_hbox_.get())); | 1104 GTK_CHROME_SHRINKABLE_HBOX(button_hbox_.get())); |
1078 if (profile_->IsOffTheRecord()) | 1105 if (profile_->IsOffTheRecord()) |
1079 item_index = model_->IncognitoIndexToOriginal(item_index); | 1106 item_index = model_->IncognitoIndexToOriginal(item_index); |
1080 | 1107 |
1081 const Extension* extension = model_->toolbar_items()[item_index].get(); | 1108 const Extension* extension = model_->toolbar_items()[item_index].get(); |
1082 ExtensionButtonMap::iterator it = extension_button_map_.find(extension->id()); | 1109 BrowserActionButton* button = GetBrowserActionButton(extension); |
1083 if (it == extension_button_map_.end()) { | 1110 if (button == NULL) { |
1084 NOTREACHED(); | 1111 NOTREACHED(); |
1085 return FALSE; | 1112 return FALSE; |
1086 } | 1113 } |
1087 | 1114 |
1088 MenuGtk* menu = it->second.get()->GetContextMenu(); | 1115 MenuGtk* menu = button->GetContextMenu(); |
1089 if (!menu) | 1116 if (!menu) |
1090 return FALSE; | 1117 return FALSE; |
1091 | 1118 |
1092 menu->PopupAsContext(gfx::Point(event->x_root, event->y_root), | 1119 menu->PopupAsContext(gfx::Point(event->x_root, event->y_root), |
1093 event->time); | 1120 event->time); |
1094 return TRUE; | 1121 return TRUE; |
1095 } | 1122 } |
1096 | 1123 |
1097 void BrowserActionsToolbarGtk::OnButtonShowOrHide(GtkWidget* sender) { | 1124 void BrowserActionsToolbarGtk::OnButtonShowOrHide(GtkWidget* sender) { |
1098 if (!resize_animation_.is_animating()) | 1125 if (!resize_animation_.is_animating()) |
1099 UpdateChevronVisibility(); | 1126 UpdateChevronVisibility(); |
1100 } | 1127 } |
OLD | NEW |