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.
4
5#include "chrome/browser/ui/libgtk2ui/gtk2_status_icon.h"
6
7#include <gtk/gtk.h>
8
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"
14
15namespace libgtk2ui {
16
17Gtk2StatusIcon::Gtk2StatusIcon(const gfx::ImageSkia& image,
18                               const base::string16& tool_tip) {
19  GdkPixbuf* pixbuf = GdkPixbufFromSkBitmap(*image.bitmap());
20  gtk_status_icon_ = gtk_status_icon_new_from_pixbuf(pixbuf);
21  g_object_unref(pixbuf);
22
23  g_signal_connect(gtk_status_icon_, "activate", G_CALLBACK(OnClickThunk),
24      this);
25  g_signal_connect(gtk_status_icon_, "popup_menu",
26      G_CALLBACK(OnContextMenuRequestedThunk), this);
27  SetToolTip(tool_tip);
28}
29
30Gtk2StatusIcon::~Gtk2StatusIcon() {
31  g_object_unref(gtk_status_icon_);
32}
33
34void Gtk2StatusIcon::SetImage(const gfx::ImageSkia& image) {
35  GdkPixbuf* pixbuf = GdkPixbufFromSkBitmap(*image.bitmap());
36  gtk_status_icon_set_from_pixbuf(gtk_status_icon_, pixbuf);
37  g_object_unref(pixbuf);
38}
39
40void Gtk2StatusIcon::SetToolTip(const base::string16& tool_tip) {
41  gtk_status_icon_set_tooltip_text(gtk_status_icon_,
42                                   base::UTF16ToUTF8(tool_tip).c_str());
43}
44
45void Gtk2StatusIcon::UpdatePlatformContextMenu(ui::MenuModel* model) {
46  menu_.reset();
47  if (model)
48    menu_.reset(new AppIndicatorIconMenu(model));
49}
50
51void Gtk2StatusIcon::RefreshPlatformContextMenu() {
52  if (menu_.get())
53    menu_->Refresh();
54}
55
56void Gtk2StatusIcon::OnClick(GtkStatusIcon* status_icon) {
57  if (delegate())
58    delegate()->OnClick();
59}
60
61void Gtk2StatusIcon::OnContextMenuRequested(GtkStatusIcon* status_icon,
62                                            guint button,
63                                            guint32 activate_time) {
64  if (menu_.get()) {
65    gtk_menu_popup(menu_->GetGtkMenu(),
66                   NULL,
67                   NULL,
68                   gtk_status_icon_position_menu,
69                   gtk_status_icon_,
70                   button,
71                   activate_time);
72  }
73}
74
75}  // namespace libgtk2ui
76