Rename panel, plugin, and plugin_class to Panel, Plugin, and
[lxde/lxpanel.git] / src / plugins / deskno / deskno2.c
1 #include <sys/types.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <glib/gi18n.h>
5
6 #include "panel.h"
7 #include "misc.h"
8 #include "plugin.h"
9
10 //#define DEBUG
11 #include "dbg.h"
12
13 typedef struct {
14 GtkWidget *main;
15 int dno; // current desktop nomer
16 int dnum; // number of desktops
17 char **dnames; // desktop names
18 int dnames_num; // number of desktop names
19 char **lnames; // label names
20 char *fmt;
21 } deskno;
22
23
24
25 static void
26 clicked(GtkWidget *widget, deskno *dc)
27 {
28 system("xfce-setting-show workspaces");
29 }
30
31 static void
32 update_dno(GtkWidget *widget, deskno *dc)
33 {
34 ENTER;
35 dc->dno = fb_ev_current_desktop(fbev);
36 gtk_button_set_label(GTK_BUTTON(dc->main), dc->lnames[dc->dno]);
37
38 RET();
39 }
40
41
42
43 static void
44 update_all(GtkWidget *widget, deskno *dc)
45 {
46 int i;
47
48 ENTER;
49 dc->dnum = fb_ev_number_of_desktops(fbev);
50 if (dc->dnames)
51 g_strfreev (dc->dnames);
52 if (dc->lnames)
53 g_strfreev (dc->lnames);
54 dc->dnames = get_utf8_property_list(GDK_ROOT_WINDOW(), a_NET_DESKTOP_NAMES, &(dc->dnames_num));
55 dc->lnames = g_new0 (gchar*, dc->dnum + 1);
56 for (i = 0; i < MIN(dc->dnum, dc->dnames_num); i++) {
57 dc->lnames[i] = g_strdup(dc->dnames[i]);
58 }
59 for (; i < dc->dnum; i++) {
60 dc->lnames[i] = g_strdup_printf("%d", i + 1);
61 }
62 update_dno(widget, dc);
63 RET();
64 }
65
66
67 static gboolean
68 scroll (GtkWidget *widget, GdkEventScroll *event, deskno *dc)
69 {
70 int dno;
71
72 ENTER;
73 dno = dc->dno + ((event->direction == GDK_SCROLL_UP) ? (-1) : (+1));
74 if (dno < 0)
75 dno = dc->dnum - 1;
76 else if (dno == dc->dnum)
77 dno = 0;
78 Xclimsg(GDK_ROOT_WINDOW(), a_NET_CURRENT_DESKTOP, dno, 0, 0, 0, 0);
79 RET(TRUE);
80
81 }
82
83 static int
84 deskno_constructor(Plugin *p, char** fp)
85 {
86 deskno *dc;
87 ENTER;
88 dc = g_new0(deskno, 1);
89 g_return_val_if_fail(dc != NULL, 0);
90 p->priv = dc;
91
92 dc->main = gtk_button_new_with_label("w");
93 gtk_button_set_relief(GTK_BUTTON(dc->main),GTK_RELIEF_NONE);
94 gtk_container_set_border_width(GTK_CONTAINER(dc->main), 0);
95 //gtk_button_set_alignment(GTK_BUTTON(dc->main), 0, 0.5);
96 g_signal_connect(G_OBJECT(dc->main), "clicked", G_CALLBACK (clicked), (gpointer) dc);
97 g_signal_connect(G_OBJECT(dc->main), "scroll-event", G_CALLBACK(scroll), (gpointer) dc);
98
99 update_all(dc->main, dc);
100
101 p->pwid = dc->main;
102 gtk_widget_show_all(p->pwid);
103
104 g_signal_connect (G_OBJECT (fbev), "current_desktop", G_CALLBACK (update_dno), (gpointer) dc);
105 g_signal_connect (G_OBJECT (fbev), "desktop_names", G_CALLBACK (update_all), (gpointer) dc);
106 g_signal_connect (G_OBJECT (fbev), "number_of_desktops", G_CALLBACK (update_all), (gpointer) dc);
107
108 RET(1);
109 }
110
111
112 static void
113 deskno_destructor(Plugin *p)
114 {
115 deskno *dc = (deskno *)p->priv;
116
117 ENTER;
118 dc = (deskno *) p->priv;
119 //g_signal_handlers_disconnect_by_func(G_OBJECT (fbev), name_update, dc);
120 g_free(dc);
121 RET();
122 }
123
124 PluginClass deskno2_plugin_class = {
125 fname: NULL,
126 count: 0,
127
128 type : "deskno2",
129 name : N_("Desktop No / Workspace Name"),
130 version: "0.6",
131 description : N_("Display workspace number, by cmeury@users.sf.net"),
132
133 constructor : deskno_constructor,
134 destructor : deskno_destructor,
135 config : NULL,
136 save : NULL
137 };