File: gtk_status_icon.cc

package info (click to toggle)
chromium-browser 70.0.3538.110-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,619,476 kB
  • sloc: cpp: 13,024,755; ansic: 1,349,823; python: 916,672; xml: 314,489; java: 280,047; asm: 276,936; perl: 75,771; objc: 66,634; sh: 45,860; cs: 28,354; php: 11,064; makefile: 10,911; yacc: 9,109; tcl: 8,403; ruby: 4,065; lex: 1,779; pascal: 1,411; lisp: 1,055; awk: 41; jsp: 39; sed: 17; sql: 3
file content (84 lines) | stat: -rw-r--r-- 2,737 bytes parent folder | download | duplicates (2)
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
// Copyright 2014 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/libgtkui/gtk_status_icon.h"

#include <gtk/gtk.h>

#include "base/debug/leak_annotations.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/libgtkui/app_indicator_icon_menu.h"
#include "chrome/browser/ui/libgtkui/skia_utils_gtk.h"
#include "ui/base/models/menu_model.h"
#include "ui/gfx/image/image_skia.h"

G_GNUC_BEGIN_IGNORE_DEPRECATIONS

namespace libgtkui {

GtkStatusIcon::GtkStatusIcon(const gfx::ImageSkia& image,
                             const base::string16& tool_tip) {
  GdkPixbuf* pixbuf = GdkPixbufFromSkBitmap(*image.bitmap());
  {
#if GTK_MAJOR_VERSION == 3
    // GTK has a bug that leaks 384 bytes when creating a GtkStatusIcon.  It
    // will not be fixed since the status icon was deprecated in version 3.14.
    // Luckily, Chromium doesn't need to create a status icon very often, if at
    // all.
    ANNOTATE_SCOPED_MEMORY_LEAK;
#endif
    gtk_status_icon_ = gtk_status_icon_new_from_pixbuf(pixbuf);
  }
  g_object_unref(pixbuf);

  g_signal_connect(gtk_status_icon_, "activate", G_CALLBACK(OnClickThunk),
                   this);
  g_signal_connect(gtk_status_icon_, "popup_menu",
                   G_CALLBACK(OnContextMenuRequestedThunk), this);
  SetToolTip(tool_tip);
}

GtkStatusIcon::~GtkStatusIcon() {
  gtk_status_icon_set_visible(gtk_status_icon_, FALSE);
  g_object_unref(gtk_status_icon_);
}

void GtkStatusIcon::SetImage(const gfx::ImageSkia& image) {
  GdkPixbuf* pixbuf = GdkPixbufFromSkBitmap(*image.bitmap());
  gtk_status_icon_set_from_pixbuf(gtk_status_icon_, pixbuf);
  g_object_unref(pixbuf);
}

void GtkStatusIcon::SetToolTip(const base::string16& tool_tip) {
  gtk_status_icon_set_tooltip_text(gtk_status_icon_,
                                   base::UTF16ToUTF8(tool_tip).c_str());
}

void GtkStatusIcon::UpdatePlatformContextMenu(ui::MenuModel* model) {
  menu_.reset();
  if (model)
    menu_.reset(new AppIndicatorIconMenu(model));
}

void GtkStatusIcon::RefreshPlatformContextMenu() {
  if (menu_.get())
    menu_->Refresh();
}

void GtkStatusIcon::OnClick(GtkStatusIcon* status_icon) {
  if (delegate())
    delegate()->OnClick();
}

void GtkStatusIcon::OnContextMenuRequested(GtkStatusIcon* status_icon,
                                           guint button,
                                           guint32 activate_time) {
  if (menu_.get()) {
    gtk_menu_popup(menu_->GetGtkMenu(), nullptr, nullptr,
                   gtk_status_icon_position_menu, gtk_status_icon_, button,
                   activate_time);
  }
}

}  // namespace libgtkui