1 /* $Id$ */
2 /* Copyright (c) 2010-2015 Pierre Pronchery <khorben@defora.org> */
3 /* This file is part of DeforaOS Desktop Panel */
4 /* This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, version 3 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>. */
15 
16 
17 
18 #include <string.h>
19 #include <libintl.h>
20 #include <gdk/gdkx.h>
21 #include <X11/X.h>
22 #include "Panel/applet.h"
23 #define _(string) gettext(string)
24 
25 
26 /* Desktop */
27 /* private */
28 /* types */
29 typedef struct _PanelApplet Desktop;
30 
31 
32 /* prototypes */
33 static Desktop * _desktop_init(PanelAppletHelper * helper, GtkWidget ** widget);
34 static void _desktop_destroy(Desktop * desktop);
35 
36 /* callbacks */
37 static void _desktop_on_clicked(GtkWidget * widget);
38 
39 
40 /* public */
41 /* variables */
42 PanelAppletDefinition applet =
43 {
44 	"Desktop switch",
45 	"panel-applet-desktop",
46 	NULL,
47 	_desktop_init,
48 	_desktop_destroy,
49 	NULL,
50 	FALSE,
51 	TRUE
52 };
53 
54 
55 /* private */
56 /* functions */
57 /* desktop_init */
_desktop_init(PanelAppletHelper * helper,GtkWidget ** widget)58 static Desktop * _desktop_init(PanelAppletHelper * helper, GtkWidget ** widget)
59 {
60 	GtkWidget * ret;
61 	GtkWidget * image;
62 
63 	ret = gtk_button_new();
64 	image = gtk_image_new_from_icon_name("panel-applet-desktop",
65 			panel_window_get_icon_size(helper->window));
66 	gtk_button_set_image(GTK_BUTTON(ret), image);
67 	gtk_button_set_relief(GTK_BUTTON(ret), GTK_RELIEF_NONE);
68 #if GTK_CHECK_VERSION(2, 12, 0)
69 	gtk_widget_set_tooltip_text(ret, _("Show desktop"));
70 #endif
71 	g_signal_connect(ret, "clicked", G_CALLBACK(_desktop_on_clicked), NULL);
72 	gtk_widget_show_all(ret);
73 	*widget = ret;
74 	/* XXX ugly workaround */
75 	return (Desktop *)ret;
76 }
77 
78 
79 /* desktop_destroy */
_desktop_destroy(Desktop * desktop)80 static void _desktop_destroy(Desktop * desktop)
81 {
82 	/* XXX just as ugly */
83 	gtk_widget_destroy((GtkWidget *)desktop);
84 }
85 
86 
87 /* callbacks */
88 /* on_clicked */
_desktop_on_clicked(GtkWidget * widget)89 static void _desktop_on_clicked(GtkWidget * widget)
90 {
91 	GdkScreen * screen;
92 	GdkDisplay * display;
93 	GdkWindow * root;
94 	XEvent xev;
95 
96 	screen = gtk_widget_get_screen(widget);
97 	display = gtk_widget_get_display(widget);
98 	root = gdk_screen_get_root_window(screen);
99 	xev.xclient.type = ClientMessage;
100 	xev.xclient.window = GDK_WINDOW_XID(root);
101 	xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display(
102 			display, "_NET_SHOWING_DESKTOP");
103 	xev.xclient.format = 32;
104 	memset(&xev.xclient.data, 0, sizeof(xev.xclient.data));
105 	xev.xclient.data.l[0] = 1;
106 	gdk_error_trap_push();
107 	XSendEvent(GDK_DISPLAY_XDISPLAY(display), GDK_WINDOW_XID(root),
108 			False,
109 			SubstructureNotifyMask | SubstructureRedirectMask,
110 			&xev);
111 	gdk_error_trap_pop();
112 }
113