[SF#679]X11 library uses unsigned long as cardinal-32 data, not uint32_t.
[lxde/lxpanel.git] / plugins / deskno / deskno.c
CommitLineData
9dd114c4 1/**
2 * Desktop number plugin to lxpanel
3 *
63df19e6 4 * Copyright (c) 2008-2014 LxDE Developers, see the file AUTHORS for details.
9dd114c4 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
a52c2257
HJYP
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>
e7cb732b 28#include <glib/gi18n.h>
a52c2257 29
63df19e6 30#include "plugin.h"
a52c2257 31#include "misc.h"
63df19e6 32#include "ev.h"
a52c2257 33
9dd114c4 34/* Private context for desktop number plugin. */
a52c2257 35typedef struct {
a7bd16a4 36 LXPanel * panel; /* Back pointer to Panel */
63df19e6 37 config_setting_t *settings;
2918994e 38 GtkWidget * label; /* The label */
39 int number_of_desktops; /* Number of desktops */
40 char * * desktop_labels; /* Vector of desktop labels */
aff07791 41 gboolean bold; /* User preference: True if bold font */
42 gboolean wm_labels; /* User preference: True to display window manager labels */
9dd114c4 43} DesknoPlugin;
44
63df19e6 45static void deskno_destructor(gpointer user_data);
a52c2257 46
9dd114c4 47/* Handler for current_desktop event from window manager. */
aff07791 48static gboolean deskno_name_update(GtkWidget * widget, DesknoPlugin * dc)
a52c2257 49{
9dd114c4 50 /* Compute and redraw the desktop number. */
2918994e 51 int desktop_number = get_net_current_desktop();
52 if (desktop_number < dc->number_of_desktops)
a7bd16a4 53 lxpanel_draw_label_text(dc->panel, dc->label, dc->desktop_labels[desktop_number], dc->bold, 1, TRUE);
9dd114c4 54 return TRUE;
a52c2257
HJYP
55}
56
aff07791 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. */
59static void deskno_redraw(GtkWidget * widget, DesknoPlugin * dc)
2918994e 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. */
aff07791 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]);
2918994e 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. */
aff07791 86 deskno_name_update(widget, dc);
2918994e 87}
88
9dd114c4 89/* Handler for button-press-event on top level widget. */
a7bd16a4 90static gboolean deskno_button_press_event(GtkWidget * widget, GdkEventButton * event, LXPanel * p)
9dd114c4 91{
9dd114c4 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;
a52c2257 98
9dd114c4 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;
a52c2257
HJYP
102}
103
9dd114c4 104/* Plugin constructor. */
a7bd16a4 105static GtkWidget *deskno_constructor(LXPanel *panel, config_setting_t *settings)
a52c2257 106{
9dd114c4 107 /* Allocate plugin context and set into Plugin private data pointer. */
108 DesknoPlugin * dc = g_new0(DesknoPlugin, 1);
63df19e6
AG
109 GtkWidget *p;
110 int tmp_int;
111
a52c2257 112 g_return_val_if_fail(dc != NULL, 0);
63df19e6
AG
113 dc->panel = panel;
114 dc->settings = settings;
4542c20d 115
aff07791 116 /* Default parameters. */
117 dc->wm_labels = TRUE;
118
119 /* Load parameters from the configuration file. */
63df19e6
AG
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;
aff07791 124
9dd114c4 125 /* Allocate top level widget and set into Plugin widget pointer. */
63df19e6
AG
126 p = gtk_event_box_new();
127 lxpanel_plugin_set_data(p, dc, deskno_destructor);
128 gtk_container_set_border_width(GTK_CONTAINER (p), 1);
a52c2257 129
9dd114c4 130 /* Allocate label widget and add to top level. */
131 dc->label = gtk_label_new(NULL);
63df19e6 132 gtk_container_add(GTK_CONTAINER(p), dc->label);
9dd114c4 133
134 /* Connect signals. Note use of window manager event object. */
8e05828e
AG
135 g_signal_connect(G_OBJECT(fbev), "current-desktop", G_CALLBACK(deskno_name_update), (gpointer) dc);
136 g_signal_connect(G_OBJECT(fbev), "desktop-names", G_CALLBACK(deskno_redraw), (gpointer) dc);
137 g_signal_connect(G_OBJECT(fbev), "number-of-desktops", G_CALLBACK(deskno_redraw), (gpointer) dc);
a52c2257 138
9dd114c4 139 /* Initialize value and show the widget. */
aff07791 140 deskno_redraw(NULL, dc);
63df19e6
AG
141 gtk_widget_show_all(p);
142 return p;
9dd114c4 143}
a52c2257 144
9dd114c4 145/* Plugin destructor. */
63df19e6 146static void deskno_destructor(gpointer user_data)
a52c2257 147{
63df19e6 148 DesknoPlugin * dc = (DesknoPlugin *) user_data;
a52c2257 149
9dd114c4 150 /* Disconnect signal from window manager event object. */
aff07791 151 g_signal_handlers_disconnect_by_func(G_OBJECT(fbev), deskno_name_update, dc);
63df19e6 152 g_signal_handlers_disconnect_by_func(G_OBJECT(fbev), deskno_redraw, dc);
2918994e 153
154 /* Deallocate all memory. */
155 if (dc->desktop_labels != NULL)
156 g_strfreev(dc->desktop_labels);
9dd114c4 157 g_free(dc);
a52c2257
HJYP
158}
159
aff07791 160/* Callback when the configuration dialog has recorded a configuration change. */
63df19e6 161static gboolean deskno_apply_configuration(gpointer user_data)
aff07791 162{
63df19e6 163 DesknoPlugin * dc = lxpanel_plugin_get_data(user_data);
aff07791 164 deskno_redraw(NULL, dc);
63df19e6
AG
165 config_group_set_int(dc->settings, "BoldFont", dc->bold);
166 config_group_set_int(dc->settings, "WMLabels", dc->wm_labels);
167 return FALSE;
aff07791 168}
169
170/* Callback when the configuration dialog is to be shown. */
752ee4e2 171static GtkWidget *deskno_configure(LXPanel *panel, GtkWidget *p)
aff07791 172{
63df19e6
AG
173 DesknoPlugin * dc = lxpanel_plugin_get_data(p);
174 GtkWidget * dlg = lxpanel_generic_config_dlg(_("Desktop Number / Workspace Name"),
175 panel, deskno_apply_configuration, p,
aff07791 176 _("Bold font"), &dc->bold, CONF_TYPE_BOOL,
177 _("Display desktop names"), &dc->wm_labels, CONF_TYPE_BOOL,
178 NULL);
179 gtk_widget_set_size_request(GTK_WIDGET(dlg), 400, -1); /* Improve geometry */
63df19e6 180 return dlg;
aff07791 181}
182
2918994e 183/* Callback when panel configuration changes. */
a7bd16a4 184static void deskno_panel_configuration_changed(LXPanel *panel, GtkWidget *p)
2918994e 185{
63df19e6 186 DesknoPlugin * dc = lxpanel_plugin_get_data(p);
aff07791 187 deskno_name_update(NULL, dc);
2918994e 188}
189
63df19e6 190FM_DEFINE_MODULE(lxpanel_gtk, deskno)
2918994e 191
63df19e6
AG
192/* Plugin descriptor. */
193LXPanelPluginInit fm_module_init_lxpanel_gtk = {
3c3e9c9e 194 .name = N_("Desktop Number / Workspace Name"),
3c3e9c9e
HG
195 .description = N_("Display workspace number, by cmeury@users.sf.net"),
196
63df19e6 197 .new_instance = deskno_constructor,
3c3e9c9e 198 .config = deskno_configure,
63df19e6
AG
199 .reconfigure = deskno_panel_configuration_changed,
200 .button_press_event = deskno_button_press_event
a52c2257 201};