Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / libgtk2ui / gtk2_status_icon.cc
blobef1a680547caf77a9b4dad7a1cadbc2a06d08f3c
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/libgtk2ui/gtk2_status_icon.h"
7 #include <gtk/gtk.h>
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/ui/libgtk2ui/app_indicator_icon_menu.h"
11 #include "chrome/browser/ui/libgtk2ui/skia_utils_gtk2.h"
12 #include "ui/base/models/menu_model.h"
13 #include "ui/gfx/image/image_skia.h"
15 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
17 namespace libgtk2ui {
19 Gtk2StatusIcon::Gtk2StatusIcon(const gfx::ImageSkia& image,
20 const base::string16& tool_tip) {
21 GdkPixbuf* pixbuf = GdkPixbufFromSkBitmap(*image.bitmap());
22 gtk_status_icon_ = gtk_status_icon_new_from_pixbuf(pixbuf);
23 g_object_unref(pixbuf);
25 g_signal_connect(gtk_status_icon_, "activate", G_CALLBACK(OnClickThunk),
26 this);
27 g_signal_connect(gtk_status_icon_, "popup_menu",
28 G_CALLBACK(OnContextMenuRequestedThunk), this);
29 SetToolTip(tool_tip);
32 Gtk2StatusIcon::~Gtk2StatusIcon() {
33 gtk_status_icon_set_visible(gtk_status_icon_, FALSE);
34 g_object_unref(gtk_status_icon_);
37 void Gtk2StatusIcon::SetImage(const gfx::ImageSkia& image) {
38 GdkPixbuf* pixbuf = GdkPixbufFromSkBitmap(*image.bitmap());
39 gtk_status_icon_set_from_pixbuf(gtk_status_icon_, pixbuf);
40 g_object_unref(pixbuf);
43 void Gtk2StatusIcon::SetToolTip(const base::string16& tool_tip) {
44 gtk_status_icon_set_tooltip_text(gtk_status_icon_,
45 base::UTF16ToUTF8(tool_tip).c_str());
48 void Gtk2StatusIcon::UpdatePlatformContextMenu(ui::MenuModel* model) {
49 menu_.reset();
50 if (model)
51 menu_.reset(new AppIndicatorIconMenu(model));
54 void Gtk2StatusIcon::RefreshPlatformContextMenu() {
55 if (menu_.get())
56 menu_->Refresh();
59 void Gtk2StatusIcon::OnClick(GtkStatusIcon* status_icon) {
60 if (delegate())
61 delegate()->OnClick();
64 void Gtk2StatusIcon::OnContextMenuRequested(GtkStatusIcon* status_icon,
65 guint button,
66 guint32 activate_time) {
67 if (menu_.get()) {
68 gtk_menu_popup(menu_->GetGtkMenu(),
69 NULL,
70 NULL,
71 gtk_status_icon_position_menu,
72 gtk_status_icon_,
73 button,
74 activate_time);
78 } // namespace libgtk2ui