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/action_box_button_gtk.h" | 5 #include "chrome/browser/ui/gtk/action_box_button_gtk.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 | 8 |
| 9 #include "base/logging.h" |
| 10 #include "chrome/browser/extensions/extension_icon_image.h" |
| 11 #include "chrome/browser/ui/browser.h" |
9 #include "chrome/browser/ui/gtk/custom_button.h" | 12 #include "chrome/browser/ui/gtk/custom_button.h" |
10 #include "chrome/browser/ui/gtk/view_id_util.h" | 13 #include "chrome/browser/ui/gtk/view_id_util.h" |
11 #include "chrome/browser/ui/toolbar/action_box_menu_model.h" | 14 #include "chrome/browser/ui/toolbar/action_box_menu_model.h" |
12 #include "chrome/browser/ui/view_ids.h" | 15 #include "chrome/browser/ui/view_ids.h" |
| 16 #include "chrome/common/extensions/api/extension_action/action_info.h" |
| 17 #include "chrome/common/extensions/extension.h" |
| 18 #include "chrome/common/extensions/extension_constants.h" |
13 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
14 #include "grit/theme_resources.h" | 20 #include "grit/theme_resources.h" |
15 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 22 #include "ui/base/layout.h" |
| 23 #include "ui/gfx/gtk_util.h" |
| 24 #include "ui/gfx/image/image_skia.h" |
| 25 #include "ui/gfx/image/image_skia_rep.h" |
| 26 |
| 27 using extensions::ActionInfo; |
| 28 using extensions::Extension; |
| 29 using extensions::IconImage; |
| 30 |
| 31 namespace { |
| 32 |
| 33 // This class provides a GtkWidget that shows an Extension's page launcher icon. |
| 34 // We can't use GtkImage directly because loading the icon from the extension |
| 35 // must be done asynchronously. The lifetime of this object is tied to its |
| 36 // GtkWidget, and it will delete itself upon receiving a "destroy" signal from |
| 37 // the widget. |
| 38 class ExtensionIcon : public IconImage::Observer { |
| 39 public: |
| 40 ExtensionIcon(Profile* profile, const Extension* extension); |
| 41 |
| 42 GtkWidget* GetWidget(); |
| 43 |
| 44 private: |
| 45 virtual ~ExtensionIcon() {} |
| 46 |
| 47 virtual void OnExtensionIconImageChanged(IconImage* image) OVERRIDE; |
| 48 void UpdateIcon(); |
| 49 |
| 50 CHROMEGTK_CALLBACK_0(ExtensionIcon, void, OnDestroyed); |
| 51 |
| 52 GtkWidget* image_; |
| 53 |
| 54 scoped_ptr<IconImage> icon_; |
| 55 |
| 56 DISALLOW_COPY_AND_ASSIGN(ExtensionIcon); |
| 57 }; |
| 58 |
| 59 ExtensionIcon::ExtensionIcon(Profile* profile, const Extension* extension) |
| 60 : image_(NULL) { |
| 61 const ActionInfo* page_launcher_info = |
| 62 ActionInfo::GetPageLauncherInfo(extension); |
| 63 icon_.reset(new IconImage(profile, |
| 64 extension, |
| 65 page_launcher_info->default_icon, |
| 66 extension_misc::EXTENSION_ICON_ACTION, |
| 67 Extension::GetDefaultIcon(true), |
| 68 this)); |
| 69 UpdateIcon(); |
| 70 } |
| 71 |
| 72 GtkWidget* ExtensionIcon::GetWidget() { |
| 73 return image_; |
| 74 } |
| 75 |
| 76 void ExtensionIcon::OnExtensionIconImageChanged(IconImage* image) { |
| 77 DCHECK_EQ(image, icon_.get()); |
| 78 UpdateIcon(); |
| 79 } |
| 80 |
| 81 void ExtensionIcon::UpdateIcon() { |
| 82 const gfx::ImageSkiaRep& rep = |
| 83 icon_->image_skia().GetRepresentation(ui::SCALE_FACTOR_NONE); |
| 84 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(rep.sk_bitmap()); |
| 85 |
| 86 if (!image_) { |
| 87 image_ = gtk_image_new_from_pixbuf(pixbuf); |
| 88 g_signal_connect(image_, "destroy", G_CALLBACK(OnDestroyedThunk), this); |
| 89 } else { |
| 90 gtk_image_set_from_pixbuf(GTK_IMAGE(image_), pixbuf); |
| 91 } |
| 92 g_object_unref(pixbuf); |
| 93 } |
| 94 |
| 95 void ExtensionIcon::OnDestroyed(GtkWidget* sender) { |
| 96 DCHECK_EQ(sender, image_); |
| 97 delete this; |
| 98 } |
| 99 |
| 100 } // namespace |
16 | 101 |
17 ActionBoxButtonGtk::ActionBoxButtonGtk(Browser* browser) | 102 ActionBoxButtonGtk::ActionBoxButtonGtk(Browser* browser) |
18 : controller_(browser, this) { | 103 : controller_(browser, this), |
| 104 browser_(browser) { |
19 button_.reset(new CustomDrawButton( | 105 button_.reset(new CustomDrawButton( |
20 IDR_ACTION_BOX_BUTTON, | 106 IDR_ACTION_BOX_BUTTON, |
21 IDR_ACTION_BOX_BUTTON_PRESSED, | 107 IDR_ACTION_BOX_BUTTON_PRESSED, |
22 IDR_ACTION_BOX_BUTTON_PRESSED, // TODO: hover | 108 IDR_ACTION_BOX_BUTTON_PRESSED, // TODO: hover |
23 0)); // TODO: disabled? | 109 0)); // TODO: disabled? |
24 gtk_widget_set_tooltip_text(widget(), | 110 gtk_widget_set_tooltip_text(widget(), |
25 l10n_util::GetStringUTF8(IDS_TOOLTIP_ACTION_BOX_BUTTON).c_str()); | 111 l10n_util::GetStringUTF8(IDS_TOOLTIP_ACTION_BOX_BUTTON).c_str()); |
26 | 112 |
27 g_signal_connect(widget(), "button-press-event", | 113 g_signal_connect(widget(), "button-press-event", |
28 G_CALLBACK(OnButtonPressThunk), this); | 114 G_CALLBACK(OnButtonPressThunk), this); |
29 | 115 |
30 ViewIDUtil::SetID(widget(), VIEW_ID_ACTION_BOX_BUTTON); | 116 ViewIDUtil::SetID(widget(), VIEW_ID_ACTION_BOX_BUTTON); |
31 } | 117 } |
32 | 118 |
33 ActionBoxButtonGtk::~ActionBoxButtonGtk() { | 119 ActionBoxButtonGtk::~ActionBoxButtonGtk() { |
34 } | 120 } |
35 | 121 |
36 bool ActionBoxButtonGtk::AlwaysShowIconForCmd(int command_id) const { | 122 bool ActionBoxButtonGtk::AlwaysShowIconForCmd(int command_id) const { |
37 return true; | 123 return true; |
38 } | 124 } |
39 | 125 |
| 126 GtkWidget* ActionBoxButtonGtk::GetImageForCommandId(int command_id) const { |
| 127 int index = model_->GetIndexOfCommandId(command_id); |
| 128 if (model_->IsItemExtension(index)) { |
| 129 const Extension* extension = model_->GetExtensionAt(index); |
| 130 return (new ExtensionIcon(browser_->profile(), extension))->GetWidget(); |
| 131 } |
| 132 |
| 133 return GetDefaultImageForCommandId(command_id); |
| 134 } |
| 135 |
40 GtkWidget* ActionBoxButtonGtk::widget() { | 136 GtkWidget* ActionBoxButtonGtk::widget() { |
41 return button_->widget(); | 137 return button_->widget(); |
42 } | 138 } |
43 | 139 |
44 void ActionBoxButtonGtk::ShowMenu(scoped_ptr<ActionBoxMenuModel> model) { | 140 void ActionBoxButtonGtk::ShowMenu(scoped_ptr<ActionBoxMenuModel> model) { |
45 model_ = model.Pass(); | 141 model_ = model.Pass(); |
46 menu_.reset(new MenuGtk(this, model_.get())); | 142 menu_.reset(new MenuGtk(this, model_.get())); |
47 menu_->PopupForWidget( | 143 menu_->PopupForWidget( |
48 button_->widget(), | 144 button_->widget(), |
49 // The mouse button. This is 1 because currently it can only be generated | 145 // The mouse button. This is 1 because currently it can only be generated |
50 // from a mouse click, but if ShowMenu can be called in other situations | 146 // from a mouse click, but if ShowMenu can be called in other situations |
51 // (e.g. other mouse buttons, or without any click at all) then it will | 147 // (e.g. other mouse buttons, or without any click at all) then it will |
52 // need to be that button or 0. | 148 // need to be that button or 0. |
53 1, | 149 1, |
54 gtk_get_current_event_time()); | 150 gtk_get_current_event_time()); |
55 } | 151 } |
56 | 152 |
57 gboolean ActionBoxButtonGtk::OnButtonPress(GtkWidget* widget, | 153 gboolean ActionBoxButtonGtk::OnButtonPress(GtkWidget* widget, |
58 GdkEventButton* event) { | 154 GdkEventButton* event) { |
59 if (event->button == 1) | 155 if (event->button == 1) |
60 controller_.OnButtonClicked(); | 156 controller_.OnButtonClicked(); |
61 return FALSE; | 157 return FALSE; |
62 } | 158 } |
OLD | NEW |