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

Side by Side Diff: chrome/browser/ui/gtk/infobars/extension_infobar_gtk.cc

Issue 7809007: GTK: Clean up the gtk extension infobar implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Get rid of stray mark. Created 9 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/infobars/extension_infobar_gtk.h" 5 #include "chrome/browser/ui/gtk/infobars/extension_infobar_gtk.h"
6 6
7 #include "chrome/browser/extensions/extension_context_menu_model.h"
7 #include "chrome/browser/extensions/extension_host.h" 8 #include "chrome/browser/extensions/extension_host.h"
9 #include "chrome/browser/platform_util.h"
10 #include "chrome/browser/ui/gtk/browser_window_gtk.h"
11 #include "chrome/browser/ui/gtk/custom_button.h"
12 #include "chrome/browser/ui/gtk/gtk_chrome_button.h"
13 #include "chrome/browser/ui/gtk/gtk_util.h"
8 #include "chrome/common/extensions/extension.h" 14 #include "chrome/common/extensions/extension.h"
9 #include "chrome/common/extensions/extension_icon_set.h" 15 #include "chrome/common/extensions/extension_icon_set.h"
10 #include "chrome/common/extensions/extension_resource.h" 16 #include "chrome/common/extensions/extension_resource.h"
11 #include "content/browser/renderer_host/render_view_host.h" 17 #include "content/browser/renderer_host/render_view_host.h"
12 #include "content/browser/renderer_host/render_widget_host_view.h" 18 #include "content/browser/renderer_host/render_widget_host_view.h"
13 #include "grit/theme_resources.h" 19 #include "grit/theme_resources.h"
14 #include "ui/base/resource/resource_bundle.h" 20 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/gfx/canvas_skia.h"
15 #include "ui/gfx/gtk_util.h" 22 #include "ui/gfx/gtk_util.h"
23 #include "ui/gfx/image/image.h"
16 24
17 ExtensionInfoBarGtk::ExtensionInfoBarGtk(TabContentsWrapper* owner, 25 ExtensionInfoBarGtk::ExtensionInfoBarGtk(TabContentsWrapper* owner,
18 ExtensionInfoBarDelegate* delegate) 26 ExtensionInfoBarDelegate* delegate)
19 : InfoBarGtk(owner, delegate), 27 : InfoBarGtk(owner, delegate),
20 tracker_(this), 28 tracker_(this),
21 delegate_(delegate), 29 delegate_(delegate),
22 view_(NULL) { 30 view_(NULL) {
31 // Always render the close button as if we were doing chrome style widget
32 // rendering. For extension infobars, we force chrome style rendering because
33 // extension authros are going to expect to match the declared gradient in
Finnur 2011/08/31 08:06:59 typo: authros -> authors.
34 // extensions_infobar.css, and the close button provided by some GTK+ themes
35 // won't look good on this background.
36 close_button_->ForceBrowserTheme();
37
23 BuildWidgets(); 38 BuildWidgets();
24 } 39 }
25 40
26 ExtensionInfoBarGtk::~ExtensionInfoBarGtk() { 41 ExtensionInfoBarGtk::~ExtensionInfoBarGtk() {}
27 // This view is not owned by us, so unparent. 42
28 gtk_widget_unparent(view_->native_view()); 43 void ExtensionInfoBarGtk::PlatformSpecificHide(bool animate) {
44 // This view is not owned by us; we can't unparent it because we aren't the
45 // owning container.
46 gtk_container_remove(GTK_CONTAINER(alignment_), view_->native_view());
47 }
48
49 void ExtensionInfoBarGtk::GetTopColor(InfoBarDelegate::Type type,
50 double* r, double* g, double *b) {
51 // Extension infobars are always drawn with chrome-theme colors.
52 *r = *g = *b = 233.0 / 255.0;
53 }
54
55 void ExtensionInfoBarGtk::GetBottomColor(InfoBarDelegate::Type type,
56 double* r, double* g, double *b) {
57 *r = *g = *b = 218.0 / 255.0;
29 } 58 }
30 59
31 void ExtensionInfoBarGtk::OnImageLoaded( 60 void ExtensionInfoBarGtk::OnImageLoaded(
32 SkBitmap* image, const ExtensionResource& resource, int index) { 61 SkBitmap* image, const ExtensionResource& resource, int index) {
33 if (!delegate_) 62 if (!delegate_)
34 return; // The delegate can go away while we asynchronously load images. 63 return; // The delegate can go away while we asynchronously load images.
35 64
36 // ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 65 // TODO(erg): IDR_EXTENSIONS_SECTION should have an IDR_INFOBAR_EXTENSIONS
37 // 66 // icon of the correct size; as is this is hueg.
Finnur 2011/08/31 08:06:59 typo: hueg -> huge.
Elliot Glaysher 2011/08/31 17:53:09 Comment partially replaced. Can we get an actual
38 // SkBitmap* icon; 67 SkBitmap* icon = image;
39 // if (!image || image->empty()) 68 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
40 // icon = rb.GetBitmapNamed(IDR_EXTENSIONS_SECTION); 69 if (!image || image->empty()) {
41 // else 70 icon = rb.GetBitmapNamed(IDR_EXTENSIONS_SECTION);
42 // icon = image; 71 }
Finnur 2011/08/31 08:06:59 nit: Don't need curly braces on one-liners.
43 // TODO(finnur): Use the above code. 72
44 // We now have the icon for the menu button, show the menu button and layout. 73 SkBitmap* drop_image = rb.GetBitmapNamed(IDR_APP_DROPARROW);
74
75 int image_size = Extension::EXTENSION_ICON_BITTY;
76 // The margin between the extension icon and the drop-down arrow bitmap.
77 static const int kDropArrowLeftMargin = 3;
78 scoped_ptr<gfx::CanvasSkia> canvas(new gfx::CanvasSkia(
79 image_size + kDropArrowLeftMargin + drop_image->width(), image_size,
80 false));
81 canvas->DrawBitmapInt(*icon, 0, 0, icon->width(), icon->height(), 0, 0,
82 image_size, image_size, false);
83 canvas->DrawBitmapInt(*drop_image, image_size + kDropArrowLeftMargin,
84 image_size / 2);
85
86 SkBitmap bitmap = canvas->ExtractBitmap();
87 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&bitmap);
88 gtk_image_set_from_pixbuf(GTK_IMAGE(icon_), pixbuf);
89 g_object_unref(pixbuf);
45 } 90 }
46 91
47 void ExtensionInfoBarGtk::BuildWidgets() { 92 void ExtensionInfoBarGtk::BuildWidgets() {
93 button_ = gtk_chrome_button_new();
94 gtk_chrome_button_set_use_gtk_rendering(GTK_CHROME_BUTTON(button_), FALSE);
95 g_object_set_data(G_OBJECT(button_), "left-align-popup",
96 reinterpret_cast<void*>(true));
97
98 icon_ = gtk_image_new();
99 gtk_misc_set_alignment(GTK_MISC(icon_), 0.5, 0.5);
100 gtk_button_set_image(GTK_BUTTON(button_), icon_);
101 gtk_util::CenterWidgetInHBox(hbox_, button_, false, 0);
102
48 // Start loading the image for the menu button. 103 // Start loading the image for the menu button.
49 const Extension* extension = delegate_->extension_host()->extension(); 104 const Extension* extension = delegate_->extension_host()->extension();
50 ExtensionResource icon_resource = extension->GetIconResource( 105 ExtensionResource icon_resource = extension->GetIconResource(
51 Extension::EXTENSION_ICON_BITTY, ExtensionIconSet::MATCH_EXACTLY); 106 Extension::EXTENSION_ICON_BITTY, ExtensionIconSet::MATCH_EXACTLY);
52 if (!icon_resource.relative_path().empty()) { 107 if (!icon_resource.relative_path().empty()) {
53 // Create a tracker to load the image. It will report back on OnImageLoaded. 108 // Create a tracker to load the image. It will report back on OnImageLoaded.
54 tracker_.LoadImage(extension, icon_resource, 109 tracker_.LoadImage(extension, icon_resource,
55 gfx::Size(Extension::EXTENSION_ICON_BITTY, 110 gfx::Size(Extension::EXTENSION_ICON_BITTY,
56 Extension::EXTENSION_ICON_BITTY), 111 Extension::EXTENSION_ICON_BITTY),
57 ImageLoadingTracker::DONT_CACHE); 112 ImageLoadingTracker::DONT_CACHE);
58 } else { 113 } else {
59 OnImageLoaded(NULL, icon_resource, 0); // |image|, ..., |index|. 114 OnImageLoaded(NULL, icon_resource, 0);
60 } 115 }
61 116
117 // Pad the bottom of the infobar by one pixel for the border.
118 alignment_ = gtk_alignment_new(0.0, 0.0, 1.0, 1.0);
119 gtk_alignment_set_padding(GTK_ALIGNMENT(alignment_), 0, 1, 0, 0);
120 gtk_box_pack_start(GTK_BOX(hbox_), alignment_, TRUE, TRUE, 0);
121
62 ExtensionHost* extension_host = delegate_->extension_host(); 122 ExtensionHost* extension_host = delegate_->extension_host();
63 view_ = extension_host->view(); 123 view_ = extension_host->view();
124
64 if (gtk_widget_get_parent(view_->native_view())) { 125 if (gtk_widget_get_parent(view_->native_view())) {
65 gtk_widget_reparent(view_->native_view(), hbox_); 126 gtk_widget_reparent(view_->native_view(), alignment_);
66 gtk_box_set_child_packing(GTK_BOX(hbox_), view_->native_view(),
67 TRUE, TRUE, 0, GTK_PACK_START);
68 } else { 127 } else {
69 gtk_box_pack_start(GTK_BOX(hbox_), view_->native_view(), TRUE, TRUE, 0); 128 gtk_container_add(GTK_CONTAINER(alignment_), view_->native_view());
70 } 129 }
71 130
72 g_signal_connect(view_->native_view(), "size_allocate", 131 signals_.Connect(button_, "button-press-event",
132 G_CALLBACK(OnButtonPressThunk), this);
133 signals_.Connect(view_->native_view(), "size_allocate",
73 G_CALLBACK(&OnSizeAllocateThunk), this); 134 G_CALLBACK(&OnSizeAllocateThunk), this);
74 } 135 }
75 136
137 void ExtensionInfoBarGtk::StoppedShowing() {
138 gtk_chrome_button_unset_paint_state(GTK_CHROME_BUTTON(button_));
139 }
140
141 Browser* ExtensionInfoBarGtk::GetBrowser() {
142 // Get the Browser object this infobar is attached to.
143 GtkWindow* parent = platform_util::GetTopLevel(icon_);
144 if (!parent)
145 return NULL;
146
147 return BrowserWindowGtk::GetBrowserWindowForNativeWindow(parent)->browser();
148 }
149
150 MenuGtk* ExtensionInfoBarGtk::BuildContextMenu() {
151 const Extension* extension = delegate_->extension();
152 if (!extension->ShowConfigureContextMenus())
153 return NULL;
154
155 Browser* browser = GetBrowser();
156 if (!browser)
157 return NULL;
158
159 context_menu_model_ =
160 new ExtensionContextMenuModel(extension, browser, NULL);
161 context_menu_.reset(
162 new MenuGtk(this, context_menu_model_.get()));
163 return context_menu_.get();
164 }
165
76 void ExtensionInfoBarGtk::OnSizeAllocate(GtkWidget* widget, 166 void ExtensionInfoBarGtk::OnSizeAllocate(GtkWidget* widget,
77 GtkAllocation* allocation) { 167 GtkAllocation* allocation) {
78 gfx::Size new_size(allocation->width, allocation->height); 168 gfx::Size new_size(allocation->width, allocation->height);
79 169
80 delegate_->extension_host()->view()->render_view_host()->view() 170 delegate_->extension_host()->view()->render_view_host()->view()
81 ->SetSize(new_size); 171 ->SetSize(new_size);
82 } 172 }
83 173
174 gboolean ExtensionInfoBarGtk::OnButtonPress(GtkWidget* widget,
175 GdkEventButton* event) {
176 if (event->button != 1)
177 return FALSE;
178
179 MenuGtk* menu = BuildContextMenu();
180 if (!menu)
181 return FALSE;
182
183 gtk_chrome_button_set_paint_state(GTK_CHROME_BUTTON(widget),
184 GTK_STATE_ACTIVE);
185 menu->PopupForWidget(widget, event->button, event->time);
186
187 return TRUE;
188 }
189
84 InfoBar* ExtensionInfoBarDelegate::CreateInfoBar(TabContentsWrapper* owner) { 190 InfoBar* ExtensionInfoBarDelegate::CreateInfoBar(TabContentsWrapper* owner) {
85 return new ExtensionInfoBarGtk(owner, this); 191 return new ExtensionInfoBarGtk(owner, this);
86 } 192 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698