summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/gtk/action_box_button_gtk.cc
blob: 18a7bc1182baaa352e6c33de4fa1be3bbc85eb58 (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
// Copyright (c) 2012 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/action_box_button_gtk.h"

#include <gtk/gtk.h>

#include "base/logging.h"
#include "chrome/browser/extensions/extension_icon_image.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/gtk/custom_button.h"
#include "chrome/browser/ui/gtk/view_id_util.h"
#include "chrome/browser/ui/toolbar/action_box_menu_model.h"
#include "chrome/browser/ui/view_ids.h"
#include "chrome/common/extensions/api/extension_action/action_info.h"
#include "chrome/common/extensions/api/icons/icons_handler.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_constants.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/layout.h"
#include "ui/gfx/gtk_util.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_rep.h"

using extensions::ActionInfo;
using extensions::Extension;
using extensions::IconImage;

namespace {

// This class provides a GtkWidget that shows an Extension's page launcher icon.
// We can't use GtkImage directly because loading the icon from the extension
// must be done asynchronously. The lifetime of this object is tied to its
// GtkWidget, and it will delete itself upon receiving a "destroy" signal from
// the widget.
class ExtensionIcon : public IconImage::Observer {
 public:
  ExtensionIcon(Profile* profile, const Extension* extension);

  GtkWidget* GetWidget();

 private:
  virtual ~ExtensionIcon() {}

  virtual void OnExtensionIconImageChanged(IconImage* image) OVERRIDE;
  void UpdateIcon();

  CHROMEGTK_CALLBACK_0(ExtensionIcon, void, OnDestroyed);

  GtkWidget* image_;

  scoped_ptr<IconImage> icon_;

  DISALLOW_COPY_AND_ASSIGN(ExtensionIcon);
};

ExtensionIcon::ExtensionIcon(Profile* profile, const Extension* extension)
    : image_(NULL) {
  const ActionInfo* page_launcher_info =
      ActionInfo::GetPageLauncherInfo(extension);
  icon_.reset(new IconImage(profile,
                            extension,
                            page_launcher_info->default_icon,
                            extension_misc::EXTENSION_ICON_ACTION,
                            extensions::IconsInfo::GetDefaultAppIcon(),
                            this));
  UpdateIcon();
}

GtkWidget* ExtensionIcon::GetWidget() {
  return image_;
}

void ExtensionIcon::OnExtensionIconImageChanged(IconImage* image)  {
  DCHECK_EQ(image, icon_.get());
  UpdateIcon();
}

void ExtensionIcon::UpdateIcon() {
  const gfx::ImageSkiaRep& rep =
      icon_->image_skia().GetRepresentation(ui::SCALE_FACTOR_NONE);
  GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(rep.sk_bitmap());

  if (!image_) {
    image_ = gtk_image_new_from_pixbuf(pixbuf);
    g_signal_connect(image_, "destroy", G_CALLBACK(OnDestroyedThunk), this);
  } else {
    gtk_image_set_from_pixbuf(GTK_IMAGE(image_), pixbuf);
  }
  g_object_unref(pixbuf);
}

void ExtensionIcon::OnDestroyed(GtkWidget* sender) {
  DCHECK_EQ(sender, image_);
  delete this;
}

}  // namespace

ActionBoxButtonGtk::ActionBoxButtonGtk(Browser* browser)
    : controller_(browser, this),
      browser_(browser) {
  button_.reset(new CustomDrawButton(
      IDR_ACTION_BOX_BUTTON_NORMAL,
      IDR_ACTION_BOX_BUTTON_PRESSED,
      IDR_ACTION_BOX_BUTTON_HOVER,
      0));  // TODO: disabled?
  gtk_widget_set_tooltip_text(widget(),
      l10n_util::GetStringUTF8(IDS_TOOLTIP_ACTION_BOX_BUTTON).c_str());

  g_signal_connect(widget(), "button-press-event",
                   G_CALLBACK(OnButtonPressThunk), this);

  ViewIDUtil::SetID(widget(), VIEW_ID_ACTION_BOX_BUTTON);
}

ActionBoxButtonGtk::~ActionBoxButtonGtk() {
}

void ActionBoxButtonGtk:: StoppedShowing() {
  button_->UnsetPaintOverride();
}

bool ActionBoxButtonGtk::AlwaysShowIconForCmd(int command_id) const {
  return true;
}

GtkWidget* ActionBoxButtonGtk::GetImageForCommandId(int command_id) const {
  int index = model_->GetIndexOfCommandId(command_id);
  if (model_->IsItemExtension(index)) {
    const Extension* extension = model_->GetExtensionAt(index);
    return (new ExtensionIcon(browser_->profile(), extension))->GetWidget();
  }

  return GetDefaultImageForCommandId(command_id);
}

GtkWidget* ActionBoxButtonGtk::widget() {
  return button_->widget();
}

void ActionBoxButtonGtk::ShowMenu(scoped_ptr<ActionBoxMenuModel> model) {
  button_->SetPaintOverride(GTK_STATE_ACTIVE);
  model_ = model.Pass();
  menu_.reset(new MenuGtk(this, model_.get()));
  menu_->PopupForWidget(
      button_->widget(),
      // The mouse button. This is 1 because currently it can only be generated
      // from a mouse click, but if ShowMenu can be called in other situations
      // (e.g. other mouse buttons, or without any click at all) then it will
      // need to be that button or 0.
      1,
      gtk_get_current_event_time());
}

gboolean ActionBoxButtonGtk::OnButtonPress(GtkWidget* widget,
                                           GdkEventButton* event) {
  if (event->button == 1)
    controller_.OnButtonClicked();
  return FALSE;
}