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