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 <vector> | 10 #include <vector> |
(...skipping 16 matching lines...) Expand all Loading... |
27 #include "chrome/browser/ui/gtk/gtk_chrome_button.h" | 27 #include "chrome/browser/ui/gtk/gtk_chrome_button.h" |
28 #include "chrome/browser/ui/gtk/gtk_chrome_shrinkable_hbox.h" | 28 #include "chrome/browser/ui/gtk/gtk_chrome_shrinkable_hbox.h" |
29 #include "chrome/browser/ui/gtk/gtk_theme_service.h" | 29 #include "chrome/browser/ui/gtk/gtk_theme_service.h" |
30 #include "chrome/browser/ui/gtk/gtk_util.h" | 30 #include "chrome/browser/ui/gtk/gtk_util.h" |
31 #include "chrome/browser/ui/gtk/hover_controller_gtk.h" | 31 #include "chrome/browser/ui/gtk/hover_controller_gtk.h" |
32 #include "chrome/browser/ui/gtk/menu_gtk.h" | 32 #include "chrome/browser/ui/gtk/menu_gtk.h" |
33 #include "chrome/browser/ui/gtk/view_id_util.h" | 33 #include "chrome/browser/ui/gtk/view_id_util.h" |
34 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 34 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
35 #include "chrome/common/chrome_notification_types.h" | 35 #include "chrome/common/chrome_notification_types.h" |
36 #include "chrome/common/extensions/extension.h" | 36 #include "chrome/common/extensions/extension.h" |
37 #include "chrome/common/extensions/extension_action.h" | 37 #include "chrome/browser/extensions/extension_action.h" |
38 #include "chrome/common/extensions/extension_manifest_constants.h" | 38 #include "chrome/common/extensions/extension_manifest_constants.h" |
39 #include "chrome/common/extensions/extension_resource.h" | 39 #include "chrome/common/extensions/extension_resource.h" |
40 #include "content/public/browser/notification_details.h" | 40 #include "content/public/browser/notification_details.h" |
41 #include "content/public/browser/notification_source.h" | 41 #include "content/public/browser/notification_source.h" |
42 #include "grit/theme_resources.h" | 42 #include "grit/theme_resources.h" |
43 #include "grit/ui_resources.h" | 43 #include "grit/ui_resources.h" |
44 #include "ui/base/accelerators/accelerator_gtk.h" | 44 #include "ui/base/accelerators/accelerator_gtk.h" |
45 #include "ui/base/gtk/gtk_compat.h" | 45 #include "ui/base/gtk/gtk_compat.h" |
46 #include "ui/base/resource/resource_bundle.h" | 46 #include "ui/base/resource/resource_bundle.h" |
47 #include "ui/gfx/canvas_skia_paint.h" | 47 #include "ui/gfx/canvas_skia_paint.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 IDR_BROWSER_ACTION, | 110 IDR_BROWSER_ACTION, |
111 IDR_BROWSER_ACTION_P, | 111 IDR_BROWSER_ACTION_P, |
112 IDR_BROWSER_ACTION_H, | 112 IDR_BROWSER_ACTION_H, |
113 0, | 113 0, |
114 NULL)); | 114 NULL)); |
115 gtk_widget_set_size_request(button(), kButtonWidth, kButtonWidth); | 115 gtk_widget_set_size_request(button(), kButtonWidth, kButtonWidth); |
116 alignment_.Own(gtk_alignment_new(0, 0, 1, 1)); | 116 alignment_.Own(gtk_alignment_new(0, 0, 1, 1)); |
117 gtk_container_add(GTK_CONTAINER(alignment_.get()), button()); | 117 gtk_container_add(GTK_CONTAINER(alignment_.get()), button()); |
118 gtk_widget_show(button()); | 118 gtk_widget_show(button()); |
119 | 119 |
120 DCHECK(extension_->browser_action()); | 120 DCHECK(browser_action()); |
121 | 121 |
122 // The Browser Action API does not allow the default icon path to be | 122 // The Browser Action API does not allow the default icon path to be |
123 // changed at runtime, so we can load this now and cache it. | 123 // changed at runtime, so we can load this now and cache it. |
124 std::string path = extension_->browser_action()->default_icon_path(); | 124 std::string path = browser_action()->default_icon_path(); |
125 if (!path.empty()) { | 125 if (!path.empty()) { |
126 tracker_.LoadImage(extension_, extension_->GetResource(path), | 126 tracker_.LoadImage(extension_, extension_->GetResource(path), |
127 gfx::Size(Extension::kBrowserActionIconMaxSize, | 127 gfx::Size(Extension::kBrowserActionIconMaxSize, |
128 Extension::kBrowserActionIconMaxSize), | 128 Extension::kBrowserActionIconMaxSize), |
129 ImageLoadingTracker::DONT_CACHE); | 129 ImageLoadingTracker::DONT_CACHE); |
130 } | 130 } |
131 | 131 |
132 UpdateState(); | 132 UpdateState(); |
133 | 133 |
134 signals_.Connect(button(), "button-press-event", | 134 signals_.Connect(button(), "button-press-event", |
(...skipping 10 matching lines...) Expand all Loading... |
145 // is reloaded. | 145 // is reloaded. |
146 ConnectBrowserActionPopupAccelerator(); | 146 ConnectBrowserActionPopupAccelerator(); |
147 } else { | 147 } else { |
148 // Window doesn't exist yet, wait for it. | 148 // Window doesn't exist yet, wait for it. |
149 signals_.Connect(toolbar->widget(), "realize", | 149 signals_.Connect(toolbar->widget(), "realize", |
150 G_CALLBACK(OnRealize), this); | 150 G_CALLBACK(OnRealize), this); |
151 } | 151 } |
152 | 152 |
153 registrar_.Add( | 153 registrar_.Add( |
154 this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, | 154 this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, |
155 content::Source<ExtensionAction>(extension->browser_action())); | 155 content::Source<ExtensionAction>(browser_action())); |
156 registrar_.Add( | 156 registrar_.Add( |
157 this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 157 this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
158 content::Source<Profile>( | 158 content::Source<Profile>( |
159 toolbar->browser()->profile()->GetOriginalProfile())); | 159 toolbar->browser()->profile()->GetOriginalProfile())); |
160 registrar_.Add( | 160 registrar_.Add( |
161 this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, | 161 this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, |
162 content::Source<Profile>( | 162 content::Source<Profile>( |
163 toolbar->browser()->profile()->GetOriginalProfile())); | 163 toolbar->browser()->profile()->GetOriginalProfile())); |
164 registrar_.Add( | 164 registrar_.Add( |
165 this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, | 165 this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 default: | 215 default: |
216 NOTREACHED(); | 216 NOTREACHED(); |
217 break; | 217 break; |
218 } | 218 } |
219 } | 219 } |
220 | 220 |
221 // ImageLoadingTracker::Observer implementation. | 221 // ImageLoadingTracker::Observer implementation. |
222 void OnImageLoaded(const gfx::Image& image, | 222 void OnImageLoaded(const gfx::Image& image, |
223 const std::string& extension_id, | 223 const std::string& extension_id, |
224 int index) OVERRIDE { | 224 int index) OVERRIDE { |
225 extension_->browser_action()->CacheIcon( | 225 browser_action()->CacheIcon(browser_action()->default_icon_path(), image); |
226 extension_->browser_action()->default_icon_path(), image); | |
227 UpdateState(); | 226 UpdateState(); |
228 } | 227 } |
229 | 228 |
230 // Updates the button based on the latest state from the associated | 229 // Updates the button based on the latest state from the associated |
231 // browser action. | 230 // browser action. |
232 void UpdateState() { | 231 void UpdateState() { |
233 int tab_id = toolbar_->GetCurrentTabId(); | 232 int tab_id = toolbar_->GetCurrentTabId(); |
234 if (tab_id < 0) | 233 if (tab_id < 0) |
235 return; | 234 return; |
236 | 235 |
237 std::string tooltip = extension_->browser_action()->GetTitle(tab_id); | 236 std::string tooltip = browser_action()->GetTitle(tab_id); |
238 if (tooltip.empty()) | 237 if (tooltip.empty()) |
239 gtk_widget_set_has_tooltip(button(), FALSE); | 238 gtk_widget_set_has_tooltip(button(), FALSE); |
240 else | 239 else |
241 gtk_widget_set_tooltip_text(button(), tooltip.c_str()); | 240 gtk_widget_set_tooltip_text(button(), tooltip.c_str()); |
242 | 241 |
243 gfx::Image image = extension_->browser_action()->GetIcon(tab_id); | 242 gfx::Image image = browser_action()->GetIcon(tab_id); |
244 if (!image.IsEmpty()) | 243 if (!image.IsEmpty()) |
245 SetImage(image.ToGdkPixbuf()); | 244 SetImage(image.ToGdkPixbuf()); |
246 bool enabled = extension_->browser_action()->GetIsVisible(tab_id); | 245 bool enabled = browser_action()->GetIsVisible(tab_id); |
247 gtk_widget_set_sensitive(button(), enabled); | 246 gtk_widget_set_sensitive(button(), enabled); |
248 | 247 |
249 gtk_widget_queue_draw(button()); | 248 gtk_widget_queue_draw(button()); |
250 } | 249 } |
251 | 250 |
252 gfx::Image GetIcon() { | 251 gfx::Image GetIcon() { |
253 return extension_->browser_action()->GetIcon( | 252 return browser_action()->GetIcon( |
254 toolbar_->GetCurrentTabId()); | 253 toolbar_->GetCurrentTabId()); |
255 } | 254 } |
256 | 255 |
257 MenuGtk* GetContextMenu() { | 256 MenuGtk* GetContextMenu() { |
258 if (!extension_->ShowConfigureContextMenus()) | 257 if (!extension_->ShowConfigureContextMenus()) |
259 return NULL; | 258 return NULL; |
260 | 259 |
261 context_menu_model_ = | 260 context_menu_model_ = |
262 new ExtensionContextMenuModel(extension_, toolbar_->browser(), this); | 261 new ExtensionContextMenuModel(extension_, toolbar_->browser(), this); |
263 context_menu_.reset( | 262 context_menu_.reset( |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 action->Activate(widget); | 335 action->Activate(widget); |
337 } | 336 } |
338 | 337 |
339 static gboolean OnExposeEvent(GtkWidget* widget, | 338 static gboolean OnExposeEvent(GtkWidget* widget, |
340 GdkEventExpose* event, | 339 GdkEventExpose* event, |
341 BrowserActionButton* button) { | 340 BrowserActionButton* button) { |
342 int tab_id = button->toolbar_->GetCurrentTabId(); | 341 int tab_id = button->toolbar_->GetCurrentTabId(); |
343 if (tab_id < 0) | 342 if (tab_id < 0) |
344 return FALSE; | 343 return FALSE; |
345 | 344 |
346 ExtensionAction* action = button->extension_->browser_action(); | 345 ExtensionAction* action = button->browser_action(); |
347 if (action->GetBadgeText(tab_id).empty()) | 346 if (action->GetBadgeText(tab_id).empty()) |
348 return FALSE; | 347 return FALSE; |
349 | 348 |
350 gfx::CanvasSkiaPaint canvas(event, false); | 349 gfx::CanvasSkiaPaint canvas(event, false); |
351 GtkAllocation allocation; | 350 GtkAllocation allocation; |
352 gtk_widget_get_allocation(widget, &allocation); | 351 gtk_widget_get_allocation(widget, &allocation); |
353 action->PaintBadge(&canvas, gfx::Rect(allocation), tab_id); | 352 action->PaintBadge(&canvas, gfx::Rect(allocation), tab_id); |
354 return FALSE; | 353 return FALSE; |
355 } | 354 } |
356 | 355 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 accel_group_ = NULL; | 436 accel_group_ = NULL; |
438 keybinding_.reset(NULL); | 437 keybinding_.reset(NULL); |
439 | 438 |
440 // We've removed the accelerator, so no need to listen to this anymore. | 439 // We've removed the accelerator, so no need to listen to this anymore. |
441 registrar_.Remove(this, | 440 registrar_.Remove(this, |
442 chrome::NOTIFICATION_WINDOW_CLOSED, | 441 chrome::NOTIFICATION_WINDOW_CLOSED, |
443 content::Source<GtkWindow>(window)); | 442 content::Source<GtkWindow>(window)); |
444 } | 443 } |
445 } | 444 } |
446 | 445 |
| 446 ExtensionAction* browser_action() { |
| 447 return GetBrowserAction(toolbar_->browser()->profile(), *extension_); |
| 448 } |
| 449 |
447 // The toolbar containing this button. | 450 // The toolbar containing this button. |
448 BrowserActionsToolbarGtk* toolbar_; | 451 BrowserActionsToolbarGtk* toolbar_; |
449 | 452 |
450 // The extension that contains this browser action. | 453 // The extension that contains this browser action. |
451 const Extension* extension_; | 454 const Extension* extension_; |
452 | 455 |
453 // The button for this browser action. | 456 // The button for this browser action. |
454 scoped_ptr<CustomDrawButton> button_; | 457 scoped_ptr<CustomDrawButton> button_; |
455 | 458 |
456 // The top level widget (parent of |button_|). | 459 // The top level widget (parent of |button_|). |
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
792 gtk_widget_set_size_request(button_hbox_.get(), desired_width_, -1); | 795 gtk_widget_set_size_request(button_hbox_.get(), desired_width_, -1); |
793 UpdateChevronVisibility(); | 796 UpdateChevronVisibility(); |
794 } | 797 } |
795 | 798 |
796 bool BrowserActionsToolbarGtk::IsCommandIdChecked(int command_id) const { | 799 bool BrowserActionsToolbarGtk::IsCommandIdChecked(int command_id) const { |
797 return false; | 800 return false; |
798 } | 801 } |
799 | 802 |
800 bool BrowserActionsToolbarGtk::IsCommandIdEnabled(int command_id) const { | 803 bool BrowserActionsToolbarGtk::IsCommandIdEnabled(int command_id) const { |
801 const Extension* extension = model_->toolbar_items()[command_id]; | 804 const Extension* extension = model_->toolbar_items()[command_id]; |
802 return extension->browser_action()->GetIsVisible(GetCurrentTabId()); | 805 return GetBrowserAction(profile_, *extension)-> |
| 806 GetIsVisible(GetCurrentTabId()); |
803 } | 807 } |
804 | 808 |
805 bool BrowserActionsToolbarGtk::GetAcceleratorForCommandId( | 809 bool BrowserActionsToolbarGtk::GetAcceleratorForCommandId( |
806 int command_id, | 810 int command_id, |
807 ui::Accelerator* accelerator) { | 811 ui::Accelerator* accelerator) { |
808 return false; | 812 return false; |
809 } | 813 } |
810 | 814 |
811 void BrowserActionsToolbarGtk::ExecuteCommand(int command_id) { | 815 void BrowserActionsToolbarGtk::ExecuteCommand(int command_id) { |
812 const Extension* extension = model_->toolbar_items()[command_id]; | 816 const Extension* extension = model_->toolbar_items()[command_id]; |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1094 | 1098 |
1095 menu->PopupAsContext(gfx::Point(event->x_root, event->y_root), | 1099 menu->PopupAsContext(gfx::Point(event->x_root, event->y_root), |
1096 event->time); | 1100 event->time); |
1097 return TRUE; | 1101 return TRUE; |
1098 } | 1102 } |
1099 | 1103 |
1100 void BrowserActionsToolbarGtk::OnButtonShowOrHide(GtkWidget* sender) { | 1104 void BrowserActionsToolbarGtk::OnButtonShowOrHide(GtkWidget* sender) { |
1101 if (!resize_animation_.is_animating()) | 1105 if (!resize_animation_.is_animating()) |
1102 UpdateChevronVisibility(); | 1106 UpdateChevronVisibility(); |
1103 } | 1107 } |
OLD | NEW |