summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/gtk/infobars/extension_infobar_gtk.cc
blob: f3ce57c553b9ca1390a41ffdf0b3ce1172ecb46e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/gtk/infobars/extension_infobar_gtk.h"

#include "chrome/browser/extensions/extension_context_menu_model.h"
#include "chrome/browser/extensions/extension_host.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/ui/gtk/browser_window_gtk.h"
#include "chrome/browser/ui/gtk/custom_button.h"
#include "chrome/browser/ui/gtk/gtk_chrome_button.h"
#include "chrome/browser/ui/gtk/gtk_util.h"
#include "chrome/browser/ui/gtk/infobars/infobar_container_gtk.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_icon_set.h"
#include "chrome/common/extensions/extension_resource.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/renderer_host/render_widget_host_view.h"
#include "grit/theme_resources.h"
#include "ui/base/gtk/gtk_signal_registrar.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas_skia.h"
#include "ui/gfx/gtk_util.h"
#include "ui/gfx/image/image.h"

ExtensionInfoBarGtk::ExtensionInfoBarGtk(InfoBarTabHelper* owner,
                                         ExtensionInfoBarDelegate* delegate)
    : InfoBarGtk(owner, delegate),
      tracker_(this),
      delegate_(delegate),
      view_(NULL) {
  // Always render the close button as if we were doing chrome style widget
  // rendering. For extension infobars, we force chrome style rendering because
  // extension authors are going to expect to match the declared gradient in
  // extensions_infobar.css, and the close button provided by some GTK+ themes
  // won't look good on this background.
  close_button_->ForceChromeTheme();

  int height = delegate->height();
  SetBarTargetHeight((height > 0) ? (height + kSeparatorLineHeight) : 0);

  BuildWidgets();
}

ExtensionInfoBarGtk::~ExtensionInfoBarGtk() {}

void ExtensionInfoBarGtk::PlatformSpecificHide(bool animate) {
  // This view is not owned by us; we can't unparent it because we aren't the
  // owning container.
  gtk_container_remove(GTK_CONTAINER(alignment_), view_->native_view());
}

void ExtensionInfoBarGtk::GetTopColor(InfoBarDelegate::Type type,
                                      double* r, double* g, double* b) {
  // Extension infobars are always drawn with chrome-theme colors.
  *r = *g = *b = 233.0 / 255.0;
}

void ExtensionInfoBarGtk::GetBottomColor(InfoBarDelegate::Type type,
                                         double* r, double* g, double* b) {
  *r = *g = *b = 218.0 / 255.0;
}

void ExtensionInfoBarGtk::OnImageLoaded(
    SkBitmap* image, const ExtensionResource& resource, int index) {
  if (!delegate_)
    return;  // The delegate can go away while we asynchronously load images.

  // TODO(erg): IDR_EXTENSIONS_SECTION should have an IDR_INFOBAR_EXTENSIONS
  // icon of the correct size with real subpixel shading and such.
  SkBitmap* icon = image;
  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
  if (!image || image->empty())
    icon = rb.GetBitmapNamed(IDR_EXTENSIONS_SECTION);

  SkBitmap* drop_image = rb.GetBitmapNamed(IDR_APP_DROPARROW);

  int image_size = Extension::EXTENSION_ICON_BITTY;
  // The margin between the extension icon and the drop-down arrow bitmap.
  static const int kDropArrowLeftMargin = 3;
  scoped_ptr<gfx::CanvasSkia> canvas(new gfx::CanvasSkia(
      image_size + kDropArrowLeftMargin + drop_image->width(),
      image_size,
      false));
  canvas->DrawBitmapInt(*icon, 0, 0, icon->width(), icon->height(), 0, 0,
                        image_size, image_size, false);
  canvas->DrawBitmapInt(*drop_image, image_size + kDropArrowLeftMargin,
                        image_size / 2);

  SkBitmap bitmap = canvas->ExtractBitmap();
  GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&bitmap);
  gtk_image_set_from_pixbuf(GTK_IMAGE(icon_), pixbuf);
  g_object_unref(pixbuf);
}

void ExtensionInfoBarGtk::BuildWidgets() {
  button_ = gtk_chrome_button_new();
  gtk_chrome_button_set_use_gtk_rendering(GTK_CHROME_BUTTON(button_), FALSE);
  g_object_set_data(G_OBJECT(button_), "left-align-popup",
                    reinterpret_cast<void*>(true));

  icon_ = gtk_image_new();
  gtk_misc_set_alignment(GTK_MISC(icon_), 0.5, 0.5);
  gtk_button_set_image(GTK_BUTTON(button_), icon_);
  gtk_util::CenterWidgetInHBox(hbox_, button_, false, 0);

  // Start loading the image for the menu button.
  const Extension* extension = delegate_->extension_host()->extension();
  ExtensionResource icon_resource = extension->GetIconResource(
      Extension::EXTENSION_ICON_BITTY, ExtensionIconSet::MATCH_EXACTLY);
  if (!icon_resource.relative_path().empty()) {
    // Create a tracker to load the image. It will report back on OnImageLoaded.
    tracker_.LoadImage(extension, icon_resource,
                       gfx::Size(Extension::EXTENSION_ICON_BITTY,
                                 Extension::EXTENSION_ICON_BITTY),
                       ImageLoadingTracker::DONT_CACHE);
  } else {
    OnImageLoaded(NULL, icon_resource, 0);
  }

  // Pad the bottom of the infobar by one pixel for the border.
  alignment_ = gtk_alignment_new(0.0, 0.0, 1.0, 1.0);
  gtk_alignment_set_padding(GTK_ALIGNMENT(alignment_), 0, 1, 0, 0);
  gtk_box_pack_start(GTK_BOX(hbox_), alignment_, TRUE, TRUE, 0);

  ExtensionHost* extension_host = delegate_->extension_host();
  view_ = extension_host->view();

  if (gtk_widget_get_parent(view_->native_view())) {
    gtk_widget_reparent(view_->native_view(), alignment_);
  } else {
    gtk_container_add(GTK_CONTAINER(alignment_), view_->native_view());
  }

  Signals()->Connect(button_, "button-press-event",
                     G_CALLBACK(&OnButtonPressThunk), this);
  Signals()->Connect(view_->native_view(), "expose-event",
                     G_CALLBACK(&OnExposeThunk), this);
  Signals()->Connect(view_->native_view(), "size_allocate",
                     G_CALLBACK(&OnSizeAllocateThunk), this);
}

void ExtensionInfoBarGtk::StoppedShowing() {
  gtk_chrome_button_unset_paint_state(GTK_CHROME_BUTTON(button_));
}

Browser* ExtensionInfoBarGtk::GetBrowser() {
  // Get the Browser object this infobar is attached to.
  GtkWindow* parent = platform_util::GetTopLevel(icon_);
  if (!parent)
    return NULL;

  return BrowserWindowGtk::GetBrowserWindowForNativeWindow(parent)->browser();
}

ui::MenuModel* ExtensionInfoBarGtk::BuildMenuModel() {
  const Extension* extension = delegate_->extension();
  if (!extension->ShowConfigureContextMenus())
    return NULL;

  Browser* browser = GetBrowser();
  if (!browser)
    return NULL;

  return new ExtensionContextMenuModel(extension, browser, NULL);
}

void ExtensionInfoBarGtk::OnSizeAllocate(GtkWidget* widget,
                                         GtkAllocation* allocation) {
  gfx::Size new_size(allocation->width, allocation->height);

  delegate_->extension_host()->view()->render_view_host()->view()
      ->SetSize(new_size);
}

gboolean ExtensionInfoBarGtk::OnButtonPress(GtkWidget* widget,
                                            GdkEventButton* event) {
  if (event->button != 1)
    return FALSE;

  ui::MenuModel* model = BuildMenuModel();
  if (!model)
    return FALSE;

  gtk_chrome_button_set_paint_state(GTK_CHROME_BUTTON(widget),
                                    GTK_STATE_ACTIVE);
  ShowMenuWithModel(widget, this, model);

  return TRUE;
}

gboolean ExtensionInfoBarGtk::OnExpose(GtkWidget* sender,
                                       GdkEventExpose* event) {
  // We also need to draw our infobar arrows over the renderer.
  static_cast<InfoBarContainerGtk*>(container())->
      PaintInfobarBitsOn(sender, event, this);

  return FALSE;
}

InfoBar* ExtensionInfoBarDelegate::CreateInfoBar(InfoBarTabHelper* owner) {
  return new ExtensionInfoBarGtk(owner, this);
}