i18n of the tooltip on wincmd plue-in.
[lxde/lxpanel.git] / src / plugins / wincmd.c
1 #include <stdlib.h>
2
3 #include <gdk-pixbuf/gdk-pixbuf.h>
4 #include <glib/gi18n.h>
5
6 #include "panel.h"
7 #include "misc.h"
8 #include "plugin.h"
9 #include "gtkbgbox.h"
10 //#define DEBUG
11 #include "dbg.h"
12
13
14 typedef struct {
15 GdkPixmap *pix;
16 GdkBitmap *mask;
17 GtkTooltips *tips;
18 int button1, button2;
19 int action1, action2;
20 } wincmd;
21
22 enum { WC_NONE, WC_ICONIFY, WC_SHADE };
23 static pair wincmd_pair [] = {
24 { WC_NONE, "none" },
25 { WC_ICONIFY, "iconify" },
26 { WC_SHADE, "shade" },
27 { 0, NULL },
28 };
29
30
31
32 static void
33 toggle_shaded(wincmd *wc, guint32 action)
34 {
35 Window *win = NULL;
36 int num, i;
37 guint32 tmp2, dno;
38 net_wm_window_type nwwt;
39
40 ENTER;
41 win = get_xaproperty (GDK_ROOT_WINDOW(), a_NET_CLIENT_LIST, XA_WINDOW, &num);
42 if (!win)
43 RET();
44 if (!num)
45 goto end;
46 //tmp = get_xaproperty (GDK_ROOT_WINDOW(), a_NET_CURRENT_DESKTOP, XA_CARDINAL, 0);
47 //dno = *tmp;
48 dno = get_net_current_desktop();
49 DBG("wincmd: #desk=%d\n", dno);
50 //XFree(tmp);
51 for (i = 0; i < num; i++) {
52 int skip;
53
54 tmp2 = get_net_wm_desktop(win[i]);
55 DBG("wincmd: win=0x%x dno=%d...", win[i], tmp2);
56 if ((tmp2 != -1) && (tmp2 != dno)) {
57 DBG("skip - not cur desk\n");
58 continue;
59 }
60 get_net_wm_window_type(win[i], &nwwt);
61 skip = (nwwt.dock || nwwt.desktop || nwwt.splash);
62 if (skip) {
63 DBG("skip - omnipresent window type\n");
64 continue;
65 }
66 Xclimsg(win[i], a_NET_WM_STATE,
67 action ? a_NET_WM_STATE_ADD : a_NET_WM_STATE_REMOVE,
68 a_NET_WM_STATE_SHADED, 0, 0, 0);
69 DBG("ok\n");
70 }
71
72 end:
73 XFree(win);
74 RET();
75 }
76
77
78
79 static void
80 toggle_iconify(wincmd *wc, guint32 action)
81 {
82 Window *win = NULL;
83 int num, i;
84 guint32 tmp2, dno;
85 net_wm_window_type nwwt;
86
87 ENTER;
88 win = get_xaproperty (GDK_ROOT_WINDOW(), a_NET_CLIENT_LIST, XA_WINDOW, &num);
89 if (!win)
90 RET();
91 if (!num)
92 goto end;
93 //tmp = get_xaproperty (GDK_ROOT_WINDOW(), a_NET_CURRENT_DESKTOP, XA_CARDINAL, 0);
94 dno = get_net_current_desktop();
95 DBG("wincmd: #desk=%d\n", dno);
96 //XFree(tmp);
97 for (i = 0; i < num; i++) {
98 int skip;
99
100 tmp2 = get_net_wm_desktop(win[i]);
101 DBG("wincmd: win=0x%x dno=%d...", win[i], tmp2);
102 if ((tmp2 != -1) && (tmp2 != dno)) {
103 DBG("skip - not cur desk\n");
104 continue;
105 }
106 get_net_wm_window_type(win[i], &nwwt);
107 skip = (nwwt.dock || nwwt.desktop || nwwt.splash);
108 if (skip) {
109 DBG("skip - omnipresent window type\n");
110 continue;
111 }
112 if (action)
113 XIconifyWindow(GDK_DISPLAY(), win[i], DefaultScreen(GDK_DISPLAY()));
114 else
115 XMapWindow (GDK_DISPLAY(), win[i]);
116 DBG("ok\n");
117 }
118
119 end:
120 XFree(win);
121 RET();
122 }
123
124 static gint
125 clicked (GtkWidget *widget, GdkEventButton *event, gpointer data)
126 {
127 wincmd *wc = (wincmd *) data;
128
129 ENTER;
130 if (event->type != GDK_BUTTON_PRESS)
131 RET(FALSE);
132
133 if (event->button == 1) {
134 wc->action1 = 1 - wc->action1;
135 toggle_iconify(wc, wc->action1);
136 DBG("wincmd: iconify all\n");
137 } else if (event->button == 2) {
138 wc->action2 = 1 - wc->action2;
139 toggle_shaded(wc, wc->action2);
140 DBG("wincmd: shade all\n");
141 } else {
142 DBG("wincmd: unsupported command\n");
143 }
144
145 RET(FALSE);
146 }
147
148 static void
149 wincmd_destructor(plugin *p)
150 {
151 wincmd *wc = (wincmd *)p->priv;
152
153 ENTER;
154 if (wc->mask)
155 g_object_unref(wc->mask);
156 if (wc->pix)
157 g_object_unref(wc->pix);
158 //gtk_widget_destroy(wc->tips);
159 g_free(wc);
160 RET();
161 }
162
163
164
165 static int
166 wincmd_constructor(plugin *p)
167 {
168 line s;
169 gchar *fname;
170 wincmd *wc;
171 //GdkPixbuf *gp, *gps;
172 GtkWidget *button;
173 int w, h;
174
175 ENTER;
176 s.len = 256;
177 wc = g_new0(wincmd, 1);
178 g_return_val_if_fail(wc != NULL, 0);
179 wc->tips = gtk_tooltips_new();
180 p->priv = wc;
181 fname = NULL;
182 while (get_line(p->fp, &s) != LINE_BLOCK_END) {
183 if (s.type == LINE_NONE) {
184 ERR( "wincmd: illegal token %s\n", s.str);
185 goto error;
186 }
187 if (s.type == LINE_VAR) {
188 if (!g_ascii_strcasecmp(s.t[0], "Button1"))
189 wc->button1 = str2num(wincmd_pair, s.t[1], WC_ICONIFY);
190 else if (!g_ascii_strcasecmp(s.t[0], "Button2"))
191 wc->button2 = str2num(wincmd_pair, s.t[1], WC_SHADE);
192 else if (!g_ascii_strcasecmp(s.t[0], "image"))
193 fname = expand_tilda(s.t[1]);
194 else {
195 ERR( "wincmd: unknown var %s\n", s.t[0]);
196 goto error;
197 }
198 } else {
199 ERR( "wincmd: illegal in this context %s\n", s.str);
200 goto error;
201 }
202 }
203 if (p->panel->orientation == ORIENT_HORIZ) {
204 w = 10000;
205 h = p->panel->ah;
206 } else {
207 w = p->panel->aw;
208 h = 10000;
209 }
210 button = fb_button_new_from_file(fname, w, h, 0x202020, TRUE);
211 gtk_container_set_border_width(GTK_CONTAINER(button), 0);
212 g_signal_connect(G_OBJECT(button), "button_press_event",
213 G_CALLBACK(clicked), (gpointer)wc);
214
215 gtk_widget_show(button);
216 gtk_container_add(GTK_CONTAINER(p->pwid), button);
217 if (p->panel->transparent)
218 gtk_bgbox_set_background(button, BG_ROOT, p->panel->tintcolor, p->panel->alpha);
219
220 g_free(fname);
221 gtk_tooltips_set_tip(GTK_TOOLTIPS (wc->tips), button, _("Left click to iconify all windows. Middle click to shade them"), NULL);
222 RET(1);
223
224 error:
225 g_free(fname);
226 wincmd_destructor(p);
227 ERR( "%s - exit\n", __FUNCTION__);
228 RET(0);
229 }
230
231
232 plugin_class wincmd_plugin_class = {
233 fname: NULL,
234 count: 0,
235
236 type : "wincmd",
237 name : "Window Commander ;-)",
238 version: "1.0",
239 description : N_("Sends commands to all desktop windows.\nSupported commnds are 1)toggle iconify and 2) toggle shade"),
240
241
242 constructor : wincmd_constructor,
243 destructor : wincmd_destructor,
244 };