Adding upstream version 0.8.0.
[debian/lxpanel.git] / plugins / deskno / deskno.c
CommitLineData
2ba86315
DB
1/**
2 * Desktop number plugin to lxpanel
3 *
6b775dbb 4 * Copyright (c) 2008-2014 LxDE Developers, see the file AUTHORS for details.
2ba86315
DB
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
6cc5e1a6
DB
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
6cc5e1a6 30#include "plugin.h"
6b775dbb
AG
31#include "misc.h"
32#include "ev.h"
6cc5e1a6 33
2ba86315 34/* Private context for desktop number plugin. */
6cc5e1a6 35typedef struct {
6b775dbb
AG
36 LXPanel * panel; /* Back pointer to Panel */
37 config_setting_t *settings;
2ba86315
DB
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
6b775dbb 45static void deskno_destructor(gpointer user_data);
2ba86315
DB
46
47/* Handler for current_desktop event from window manager. */
48static 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)
6b775dbb 53 lxpanel_draw_label_text(dc->panel, dc->label, dc->desktop_labels[desktop_number], dc->bold, 1, TRUE);
2ba86315
DB
54 return TRUE;
55}
6cc5e1a6 56
2ba86315
DB
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)
6cc5e1a6 60{
2ba86315
DB
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. */
6b775dbb 90static gboolean deskno_button_press_event(GtkWidget * widget, GdkEventButton * event, LXPanel * p)
2ba86315 91{
2ba86315 92 /* Right-click goes to next desktop, wrapping around to first. */
6cc5e1a6
DB
93 int desknum = get_net_current_desktop();
94 int desks = get_net_number_of_desktops();
2ba86315
DB
95 int newdesk = desknum + 1;
96 if (newdesk >= desks)
97 newdesk = 0;
98
99 /* Ask the window manager to make the new desktop current. */
6cc5e1a6 100 Xclimsg(GDK_ROOT_WINDOW(), a_NET_CURRENT_DESKTOP, newdesk, 0, 0, 0, 0);
2ba86315 101 return TRUE;
6cc5e1a6
DB
102}
103
2ba86315 104/* Plugin constructor. */
6b775dbb 105static GtkWidget *deskno_constructor(LXPanel *panel, config_setting_t *settings)
6cc5e1a6 106{
2ba86315
DB
107 /* Allocate plugin context and set into Plugin private data pointer. */
108 DesknoPlugin * dc = g_new0(DesknoPlugin, 1);
6b775dbb
AG
109 GtkWidget *p;
110 int tmp_int;
111
2ba86315 112 g_return_val_if_fail(dc != NULL, 0);
6b775dbb
AG
113 dc->panel = panel;
114 dc->settings = settings;
2ba86315
DB
115
116 /* Default parameters. */
117 dc->wm_labels = TRUE;
118
119 /* Load parameters from the configuration file. */
6b775dbb
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;
2ba86315
DB
124
125 /* Allocate top level widget and set into Plugin widget pointer. */
6b775dbb
AG
126 p = gtk_event_box_new();
127 lxpanel_plugin_set_data(p, dc, deskno_destructor);
2ba86315
DB
128
129 /* Allocate label widget and add to top level. */
130 dc->label = gtk_label_new(NULL);
6b775dbb 131 gtk_container_add(GTK_CONTAINER(p), dc->label);
2ba86315
DB
132
133 /* Connect signals. Note use of window manager event object. */
6b775dbb
AG
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);
2ba86315
DB
137
138 /* Initialize value and show the widget. */
139 deskno_redraw(NULL, dc);
6b775dbb
AG
140 gtk_widget_show_all(p);
141 return p;
6cc5e1a6
DB
142}
143
2ba86315 144/* Plugin destructor. */
6b775dbb 145static void deskno_destructor(gpointer user_data)
6cc5e1a6 146{
6b775dbb 147 DesknoPlugin * dc = (DesknoPlugin *) user_data;
6cc5e1a6 148
2ba86315
DB
149 /* Disconnect signal from window manager event object. */
150 g_signal_handlers_disconnect_by_func(G_OBJECT(fbev), deskno_name_update, dc);
6b775dbb 151 g_signal_handlers_disconnect_by_func(G_OBJECT(fbev), deskno_redraw, dc);
6cc5e1a6 152
2ba86315
DB
153 /* Deallocate all memory. */
154 if (dc->desktop_labels != NULL)
155 g_strfreev(dc->desktop_labels);
156 g_free(dc);
157}
6cc5e1a6 158
2ba86315 159/* Callback when the configuration dialog has recorded a configuration change. */
6b775dbb 160static gboolean deskno_apply_configuration(gpointer user_data)
2ba86315 161{
6b775dbb 162 DesknoPlugin * dc = lxpanel_plugin_get_data(user_data);
2ba86315 163 deskno_redraw(NULL, dc);
6b775dbb
AG
164 config_group_set_int(dc->settings, "BoldFont", dc->bold);
165 config_group_set_int(dc->settings, "WMLabels", dc->wm_labels);
166 return FALSE;
6cc5e1a6
DB
167}
168
2ba86315 169/* Callback when the configuration dialog is to be shown. */
6b775dbb 170static GtkWidget *deskno_configure(LXPanel *panel, GtkWidget *p)
2ba86315 171{
6b775dbb
AG
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,
2ba86315
DB
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 */
6b775dbb 179 return dlg;
2ba86315 180}
6cc5e1a6 181
2ba86315 182/* Callback when panel configuration changes. */
6b775dbb 183static void deskno_panel_configuration_changed(LXPanel *panel, GtkWidget *p)
2ba86315 184{
6b775dbb 185 DesknoPlugin * dc = lxpanel_plugin_get_data(p);
2ba86315 186 deskno_name_update(NULL, dc);
6cc5e1a6
DB
187}
188
6b775dbb 189FM_DEFINE_MODULE(lxpanel_gtk, deskno)
2ba86315 190
6b775dbb
AG
191/* Plugin descriptor. */
192LXPanelPluginInit 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
6cc5e1a6 200};