Debian Codegraph @

systray/src/gtk3/main.c

raw symbols SCIP
1 /* System tray main() */
2
3 /*
4 * Copyright (C) 2002 Red Hat, Inc.
5 * Copyright (C) 2003-2006 Vincent Untz
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23 #include <config.h>
24 #include <string.h>
25
26 #include <panel-applet.h>
27 #include <panel-applet-gconf.h>
28
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31
32 #include "main.h"
33 #include "na-tray-manager.h"
34 #include "na-tray.h"
35 #include "fixedtip.h"
36
37 #define NOTIFICATION_AREA_ICON "gnome-panel-notification-area"
38
39 struct _NaTrayAppletPrivate
40 {
41 NaTray *tray;
42 };
43
44 G_DEFINE_TYPE (NaTrayApplet, na_tray_applet, PANEL_TYPE_APPLET)
45
46 static GtkOrientation
47 get_gtk_orientation_from_applet_orient (PanelAppletOrient orient)
48 {
49 switch (orient)
50 {
51 case PANEL_APPLET_ORIENT_LEFT:
52 case PANEL_APPLET_ORIENT_RIGHT:
53 return GTK_ORIENTATION_VERTICAL;
54 case PANEL_APPLET_ORIENT_UP:
55 case PANEL_APPLET_ORIENT_DOWN:
56 default:
57 return GTK_ORIENTATION_HORIZONTAL;
58 }
59
60 g_assert_not_reached ();
61
62 return GTK_ORIENTATION_HORIZONTAL;
63 }
64
65 static void
66 na_tray_applet_realize (GtkWidget *widget)
67 {
68 NaTrayApplet *applet = NA_TRAY_APPLET (widget);
69 PanelAppletOrient orient;
70
71 GTK_WIDGET_CLASS (na_tray_applet_parent_class)->realize (widget);
72
73 g_assert (applet->priv->tray == NULL);
74
75 orient = panel_applet_get_orient (PANEL_APPLET (widget));
76
77 applet->priv->tray = na_tray_new_for_screen (gtk_widget_get_screen (widget),
78 get_gtk_orientation_from_applet_orient (orient));
79
80 gtk_container_add (GTK_CONTAINER (widget), GTK_WIDGET (applet->priv->tray));
81 gtk_widget_show (GTK_WIDGET (applet->priv->tray));
82 }
83
84 static void
85 na_tray_applet_unrealize (GtkWidget *widget)
86 {
87 NaTrayApplet *applet = NA_TRAY_APPLET (widget);
88
89 g_assert (applet->priv->tray != NULL);
90
91 gtk_widget_destroy (GTK_WIDGET (applet->priv->tray));
92 applet->priv->tray = NULL;
93
94 GTK_WIDGET_CLASS (na_tray_applet_parent_class)->unrealize (widget);
95 }
96
97 static inline gboolean
98 style_context_lookup_color (GtkStyleContext *context,
99 const gchar *color_name,
100 GdkColor *color)
101 {
102 GdkRGBA rgba;
103
104 if (!gtk_style_context_lookup_color (context, color_name, &rgba))
105 return FALSE;
106
107 color->red = rgba.red * 65535;
108 color->green = rgba.green * 65535;
109 color->blue = rgba.blue * 65535;
110
111 return TRUE;
112 }
113
114 static void
115 na_tray_applet_style_updated (GtkWidget *widget)
116 {
117 NaTrayApplet *applet = NA_TRAY_APPLET (widget);
118 GtkStyleContext *context;
119 GdkRGBA rgba;
120 GdkColor fg;
121 GdkColor error;
122 GdkColor warning;
123 GdkColor success;
124 gint padding;
125 gint icon_size;
126
127 GTK_WIDGET_CLASS (na_tray_applet_parent_class)->style_updated (widget);
128
129 if (!applet->priv->tray)
130 return;
131
132 context = gtk_widget_get_style_context (widget);
133
134 gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &rgba);
135 fg.red = rgba.red * 65535;
136 fg.green = rgba.green * 65535;
137 fg.blue = rgba.blue * 65535;
138
139 if (!style_context_lookup_color (context, "error_color", &error))
140 error = fg;
141 if (!style_context_lookup_color (context, "warning_color", &warning))
142 warning = fg;
143 if (!style_context_lookup_color (context, "success_color", &success))
144 success = fg;
145
146 na_tray_set_colors (applet->priv->tray, &fg, &error, &warning, &success);
147
148 gtk_widget_style_get (widget, "icon-padding", &padding, NULL);
149 na_tray_set_padding (applet->priv->tray, padding);
150
151 gtk_widget_style_get (widget, "icon-size", &icon_size, NULL);
152 na_tray_set_icon_size (applet->priv->tray, icon_size);
153 }
154
155 static void
156 na_tray_applet_change_background (PanelApplet *panel_applet,
157 cairo_pattern_t *pattern)
158 {
159 NaTrayApplet *applet = NA_TRAY_APPLET (panel_applet);
160
161 if (!applet->priv->tray)
162 return;
163
164 na_tray_force_redraw (applet->priv->tray);
165 }
166
167 static void
168 na_tray_applet_change_orient (PanelApplet *panel_applet,
169 PanelAppletOrient orient)
170 {
171 NaTrayApplet *applet = NA_TRAY_APPLET (panel_applet);
172
173 if (!applet->priv->tray)
174 return;
175
176 na_tray_set_orientation (applet->priv->tray,
177 get_gtk_orientation_from_applet_orient (orient));
178 }
179
180 static inline void
181 force_no_focus_padding (GtkWidget *widget)
182 {
183 GtkCssProvider *provider;
184
185 provider = gtk_css_provider_new ();
186 gtk_css_provider_load_from_data (provider,
187 "NaTrayApplet {\n"
188 " -GtkWidget-focus-line-width: 0px;\n"
189 " -GtkWidget-focus-padding: 0px;\n"
190 "}",
191 -1, NULL);
192 gtk_style_context_add_provider (gtk_widget_get_style_context (widget),
193 GTK_STYLE_PROVIDER (provider),
194 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
195 g_object_unref (provider);
196 }
197
198 static void
199 na_tray_applet_class_init (NaTrayAppletClass *class)
200 {
201 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
202 PanelAppletClass *applet_class = PANEL_APPLET_CLASS (class);
203
204 widget_class->realize = na_tray_applet_realize;
205 widget_class->unrealize = na_tray_applet_unrealize;
206 widget_class->style_updated = na_tray_applet_style_updated;
207 applet_class->change_background = na_tray_applet_change_background;
208 applet_class->change_orient = na_tray_applet_change_orient;
209
210 gtk_widget_class_install_style_property (
211 widget_class,
212 g_param_spec_int ("icon-padding",
213 "Padding around icons",
214 "Padding that should be put around icons, in pixels",
215 0, G_MAXINT, 0,
216 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
217
218 gtk_widget_class_install_style_property (
219 widget_class,
220 g_param_spec_int ("icon-size",
221 "Icon size",
222 "If non-zero, hardcodes the size of the icons in pixels",
223 0, G_MAXINT, 0,
224 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
225
226 g_type_class_add_private (class, sizeof (NaTrayAppletPrivate));
227 }
228
229 static void
230 na_tray_applet_init (NaTrayApplet *applet)
231 {
232 AtkObject *atko;
233
234 applet->priv = G_TYPE_INSTANCE_GET_PRIVATE (applet, NA_TYPE_TRAY_APPLET,
235 NaTrayAppletPrivate);
236
237 /* Defer creating NaTray until applet is added to panel so
238 * gtk_widget_get_screen returns correct information */
239 applet->priv->tray = NULL;
240
241 atko = gtk_widget_get_accessible (GTK_WIDGET (applet));
242 atk_object_set_name (atko, D_("Panel Notification Area"));
243
244 panel_applet_set_flags (PANEL_APPLET (applet),
245 PANEL_APPLET_HAS_HANDLE|PANEL_APPLET_EXPAND_MINOR);
246
247 panel_applet_set_background_widget (PANEL_APPLET (applet),
248 GTK_WIDGET (applet));
249
250 force_no_focus_padding (GTK_WIDGET (applet));
251 }
252
253 static gboolean
254 applet_factory (PanelApplet *applet,
255 const gchar *iid,
256 gpointer user_data)
257 {
258 if (!(strcmp (iid, "NotificationArea") == 0 ||
259 strcmp (iid, "SystemTrayApplet") == 0))
260 return FALSE;
261
262 #ifndef NOTIFICATION_AREA_INPROCESS
263 gtk_window_set_default_icon_name (NOTIFICATION_AREA_ICON);
264 #endif
265
266 gtk_widget_show_all (GTK_WIDGET (applet));
267
268 return TRUE;
269 }
270
271 #ifdef NOTIFICATION_AREA_INPROCESS
272 PANEL_APPLET_IN_PROCESS_FACTORY ("NotificationAreaAppletFactory",
273 NA_TYPE_TRAY_APPLET,
274 applet_factory,
275 NULL)
276 #else
277 PANEL_APPLET_OUT_PROCESS_FACTORY ("NotificationAreaAppletFactory",
278 NA_TYPE_TRAY_APPLET,
279 applet_factory,
280 NULL)
281 #endif