Merging upstream version 0.8.0 (Closes: #639729, #761971).
[debian/lxpanel.git] / plugins / deskno / deskno.c
1 /**
2 * Desktop number plugin to lxpanel
3 *
4 * Copyright (c) 2008-2014 LxDE Developers, see the file AUTHORS for details.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22 // reused dclock.c and variables from pager.c
23 // 11/23/04 by cmeury
24
25 #include <sys/types.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <glib/gi18n.h>
29
30 #include "plugin.h"
31 #include "misc.h"
32 #include "ev.h"
33
34 /* Private context for desktop number plugin. */
35 typedef struct {
36 LXPanel * panel; /* Back pointer to Panel */
37 config_setting_t *settings;
38 GtkWidget * label; /* The label */
39 int number_of_desktops; /* Number of desktops */
40 char * * desktop_labels; /* Vector of desktop labels */
41 gboolean bold; /* User preference: True if bold font */
42 gboolean wm_labels; /* User preference: True to display window manager labels */
43 } DesknoPlugin;
44
45 static void deskno_destructor(gpointer user_data);
46
47 /* Handler for current_desktop event from window manager. */
48 static gboolean deskno_name_update(GtkWidget * widget, DesknoPlugin * dc)
49 {
50 /* Compute and redraw the desktop number. */
51 int desktop_number = get_net_current_desktop();
52 if (desktop_number < dc->number_of_desktops)
53 lxpanel_draw_label_text(dc->panel, dc->label, dc->desktop_labels[desktop_number], dc->bold, 1, TRUE);
54 return TRUE;
55 }
56
57 /* Handler for desktop_name and number_of_desktops events from window manager.
58 * Also used on a configuration change to get a full redraw. */
59 static void deskno_redraw(GtkWidget * widget, DesknoPlugin * dc)
60 {
61 /* Get the NET_DESKTOP_NAMES property. */
62 dc->number_of_desktops = get_net_number_of_desktops();
63 int number_of_desktop_names;
64 char * * desktop_names;
65 desktop_names = get_utf8_property_list(GDK_ROOT_WINDOW(), a_NET_DESKTOP_NAMES, &number_of_desktop_names);
66
67 /* Reallocate the vector of labels. */
68 if (dc->desktop_labels != NULL)
69 g_strfreev(dc->desktop_labels);
70 dc->desktop_labels = g_new0(gchar *, dc->number_of_desktops + 1);
71
72 /* Loop to copy the desktop names to the vector of labels.
73 * If there are more desktops than labels, label the extras with a decimal number. */
74 int i = 0;
75 if (dc->wm_labels)
76 for ( ; ((desktop_names != NULL) && (i < MIN(dc->number_of_desktops, number_of_desktop_names))); i++)
77 dc->desktop_labels[i] = g_strdup(desktop_names[i]);
78 for ( ; i < dc->number_of_desktops; i++)
79 dc->desktop_labels[i] = g_strdup_printf("%d", i + 1);
80
81 /* Free the property. */
82 if (desktop_names != NULL)
83 g_strfreev(desktop_names);
84
85 /* Redraw the label. */
86 deskno_name_update(widget, dc);
87 }
88
89 /* Handler for button-press-event on top level widget. */
90 static gboolean deskno_button_press_event(GtkWidget * widget, GdkEventButton * event, LXPanel * p)
91 {
92 /* Right-click goes to next desktop, wrapping around to first. */
93 int desknum = get_net_current_desktop();
94 int desks = get_net_number_of_desktops();
95 int newdesk = desknum + 1;
96 if (newdesk >= desks)
97 newdesk = 0;
98
99 /* Ask the window manager to make the new desktop current. */
100 Xclimsg(GDK_ROOT_WINDOW(), a_NET_CURRENT_DESKTOP, newdesk, 0, 0, 0, 0);
101 return TRUE;
102 }
103
104 /* Plugin constructor. */
105 static GtkWidget *deskno_constructor(LXPanel *panel, config_setting_t *settings)
106 {
107 /* Allocate plugin context and set into Plugin private data pointer. */
108 DesknoPlugin * dc = g_new0(DesknoPlugin, 1);
109 GtkWidget *p;
110 int tmp_int;
111
112 g_return_val_if_fail(dc != NULL, 0);
113 dc->panel = panel;
114 dc->settings = settings;
115
116 /* Default parameters. */
117 dc->wm_labels = TRUE;
118
119 /* Load parameters from the configuration file. */
120 if (config_setting_lookup_int(settings, "BoldFont", &tmp_int))
121 dc->bold = tmp_int != 0;
122 if (config_setting_lookup_int(settings, "WMLabels", &tmp_int))
123 dc->wm_labels = tmp_int != 0;
124
125 /* Allocate top level widget and set into Plugin widget pointer. */
126 p = gtk_event_box_new();
127 lxpanel_plugin_set_data(p, dc, deskno_destructor);
128
129 /* Allocate label widget and add to top level. */
130 dc->label = gtk_label_new(NULL);
131 gtk_container_add(GTK_CONTAINER(p), dc->label);
132
133 /* Connect signals. Note use of window manager event object. */
134 g_signal_connect(G_OBJECT(fbev), "current-desktop", G_CALLBACK(deskno_name_update), (gpointer) dc);
135 g_signal_connect(G_OBJECT(fbev), "desktop-names", G_CALLBACK(deskno_redraw), (gpointer) dc);
136 g_signal_connect(G_OBJECT(fbev), "number-of-desktops", G_CALLBACK(deskno_redraw), (gpointer) dc);
137
138 /* Initialize value and show the widget. */
139 deskno_redraw(NULL, dc);
140 gtk_widget_show_all(p);
141 return p;
142 }
143
144 /* Plugin destructor. */
145 static void deskno_destructor(gpointer user_data)
146 {
147 DesknoPlugin * dc = (DesknoPlugin *) user_data;
148
149 /* Disconnect signal from window manager event object. */
150 g_signal_handlers_disconnect_by_func(G_OBJECT(fbev), deskno_name_update, dc);
151 g_signal_handlers_disconnect_by_func(G_OBJECT(fbev), deskno_redraw, dc);
152
153 /* Deallocate all memory. */
154 if (dc->desktop_labels != NULL)
155 g_strfreev(dc->desktop_labels);
156 g_free(dc);
157 }
158
159 /* Callback when the configuration dialog has recorded a configuration change. */
160 static gboolean deskno_apply_configuration(gpointer user_data)
161 {
162 DesknoPlugin * dc = lxpanel_plugin_get_data(user_data);
163 deskno_redraw(NULL, dc);
164 config_group_set_int(dc->settings, "BoldFont", dc->bold);
165 config_group_set_int(dc->settings, "WMLabels", dc->wm_labels);
166 return FALSE;
167 }
168
169 /* Callback when the configuration dialog is to be shown. */
170 static GtkWidget *deskno_configure(LXPanel *panel, GtkWidget *p)
171 {
172 DesknoPlugin * dc = lxpanel_plugin_get_data(p);
173 GtkWidget * dlg = lxpanel_generic_config_dlg(_("Desktop Number / Workspace Name"),
174 panel, deskno_apply_configuration, p,
175 _("Bold font"), &dc->bold, CONF_TYPE_BOOL,
176 _("Display desktop names"), &dc->wm_labels, CONF_TYPE_BOOL,
177 NULL);
178 gtk_widget_set_size_request(GTK_WIDGET(dlg), 400, -1); /* Improve geometry */
179 return dlg;
180 }
181
182 /* Callback when panel configuration changes. */
183 static void deskno_panel_configuration_changed(LXPanel *panel, GtkWidget *p)
184 {
185 DesknoPlugin * dc = lxpanel_plugin_get_data(p);
186 deskno_name_update(NULL, dc);
187 }
188
189 FM_DEFINE_MODULE(lxpanel_gtk, deskno)
190
191 /* Plugin descriptor. */
192 LXPanelPluginInit fm_module_init_lxpanel_gtk = {
193 .name = N_("Desktop Number / Workspace Name"),
194 .description = N_("Display workspace number, by cmeury@users.sf.net"),
195
196 .new_instance = deskno_constructor,
197 .config = deskno_configure,
198 .reconfigure = deskno_panel_configuration_changed,
199 .button_press_event = deskno_button_press_event
200 };