Adding upstream version 0.8.0.
[debian/lxpanel.git] / plugins / pager.c
1 /* pager.c -- pager module of lxpanel project
2 *
3 * Copyright (C) 2009 Dongxu Li <song6song@sourceforge.net>
4 * 2012 Julien Lavergne <gilir@ubuntu.com>
5 *
6 * This file is part of lxpanel.
7 *
8 * lxpanel is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * lxpanel is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with sawfish; see the file COPYING. If not, write to
20 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301 USA.
22 */
23
24 #include <gtk/gtk.h>
25 #include <gdk/gdkx.h>
26 #include <libfm/fm-gtk.h>
27
28 #include <glib/gi18n.h>
29 #ifndef WNCK_I_KNOW_THIS_IS_UNSTABLE
30 #define WNCK_I_KNOW_THIS_IS_UNSTABLE
31 #endif
32 #include <libwnck/libwnck.h>
33
34 #include "plugin.h"
35
36 typedef struct
37 {
38 WnckPager *pager;
39 gint border;
40 } PagerData;
41
42 /* command to configure desktop, it will be set by .config callback */
43 static const char *configure_command = NULL;
44
45 static void on_realize(GtkWidget *p, LXPanel *panel)
46 {
47 PagerData *d = lxpanel_plugin_get_data(p);
48 int rows, r, h = panel_get_height(panel) - 2 * d->border;
49
50 /* set geometry */
51 wnck_pager_set_orientation(d->pager, panel_get_orientation(panel));
52 if (panel_get_orientation(panel) == GTK_ORIENTATION_VERTICAL)
53 h *= ((gfloat) gdk_screen_height() / (gfloat) gdk_screen_width());
54 rows = h / (panel_get_icon_size(panel) * 2) + 1; /* min */
55 r = (h - 2) / panel_get_icon_size(panel); /* max */
56 /* g_debug("pager for height %d and icon size %d: %d to %d",panel_get_height(panel),panel_get_icon_size(panel),r,rows); */
57 rows = MAX(rows, r);
58 wnck_pager_set_n_rows(d->pager, rows);
59 }
60
61 static void on_size_allocate(GtkWidget *p, GdkRectangle *allocation, LXPanel *panel)
62 {
63 /* g_debug("pager: on_size_allocate(): %dx%d", allocation->width, allocation->height); */
64 on_realize(p, panel);
65 }
66
67 static GtkWidget *pager_constructor(LXPanel *panel, config_setting_t *settings)
68 {
69 GtkWidget *p, *w;
70 PagerData *d;
71 int border = 1; /* NOTE: old 'pager' used 2, WnckPager has 1, need 1 more */
72
73 /* FIXME: use some global setting for border */
74 #if GTK_CHECK_VERSION(3, 0, 0)
75 w = wnck_pager_new();
76 #else
77 w = wnck_pager_new(NULL);
78 #endif
79 g_return_val_if_fail(w != NULL, NULL);
80 d = g_new(PagerData, 1);
81 d->pager = WNCK_PAGER(w);
82 d->border = border;
83 p = gtk_event_box_new();
84 gtk_widget_set_has_window(p, FALSE);
85 lxpanel_plugin_set_data(p, d, g_free);
86
87 /* we cannot configure pager until it added into widgets hierarchy */
88 g_signal_connect(p, "realize", G_CALLBACK(on_realize), panel);
89 g_signal_connect(p, "size-allocate", G_CALLBACK(on_size_allocate), panel);
90 wnck_pager_set_display_mode(d->pager, WNCK_PAGER_DISPLAY_CONTENT);
91
92 gtk_widget_show(w);
93
94 /* add an additional container to setup a border */
95 w = gtk_event_box_new();
96 gtk_widget_set_has_window(w, FALSE);
97 gtk_container_set_border_width(GTK_CONTAINER(w), border);
98 gtk_container_add(GTK_CONTAINER(w), (GtkWidget*)d->pager);
99 gtk_widget_show(w);
100 gtk_container_add(GTK_CONTAINER(p), w);
101
102 return p;
103 }
104
105 /* this is a modified version of patch from Lubuntu */
106 static GtkWidget *pager_configure(LXPanel *panel, GtkWidget *instance)
107 {
108 if (configure_command)
109 fm_launch_command_simple(NULL, NULL, G_APP_INFO_CREATE_NONE,
110 configure_command, NULL);
111 else
112 fm_show_error(NULL, NULL,
113 _("Sorry, there was no window manager configuration program found."));
114 return NULL; /* no configuration dialog of lxpanel available */
115 }
116
117 static void pager_menu_callback(GtkWidget *widget, gpointer data)
118 {
119 gtk_widget_set_sensitive(widget, FALSE);
120 }
121
122 static gboolean pager_update_context_menu(GtkWidget *plugin, GtkMenu *menu)
123 {
124 GdkScreen *screen = gdk_screen_get_default();
125 const char *wm_name = gdk_x11_screen_get_window_manager_name(screen);
126 char *path = NULL;
127
128 /* update configure_command */
129 configure_command = NULL;
130 if (g_strcmp0(wm_name, "Openbox") == 0)
131 {
132 if ((path = g_find_program_in_path("obconf")))
133 {
134 configure_command = "obconf --tab 6";
135 }
136 }
137 else if (g_strcmp0(wm_name, "compiz") == 0)
138 {
139 if ((path = g_find_program_in_path("ccsm")))
140 {
141 configure_command = "ccsm";
142 }
143 else if ((path = g_find_program_in_path("simple-ccsm")))
144 {
145 configure_command = "simple-ccsm";
146 }
147 }
148 /* FIXME: support other WMs */
149 if (configure_command == NULL)
150 {
151 /* disable 'Settings' menu item */
152 gtk_container_foreach(GTK_CONTAINER(menu), pager_menu_callback, NULL);
153 }
154 /* FIXME: else replace 'Settings' item label with "Launch Workspaces Configurator (%s)" */
155 g_free(path);
156 return FALSE;
157 }
158
159 static void pager_panel_configuration_changed(LXPanel *panel, GtkWidget *p)
160 {
161 on_realize(p, panel);
162 }
163
164 static LXPanelPluginInit wnck_pager = {
165 .name = N_("Desktop Pager"),
166 .description = N_("Simple pager plugin"),
167
168 .superseded = TRUE,
169 .new_instance = pager_constructor,
170 .config = pager_configure,
171 .update_context_menu = pager_update_context_menu,
172 .reconfigure = pager_panel_configuration_changed
173 };
174
175 static void pager_wnck_init(void)
176 {
177 lxpanel_register_plugin_type("wnckpager", &wnck_pager);
178 }
179
180 LXPanelPluginInit lxpanel_static_plugin_pager = {
181 .name = N_("Desktop Pager"),
182 .description = N_("Simple pager plugin"),
183
184 .init = pager_wnck_init,
185 .new_instance = pager_constructor,
186 .config = pager_configure,
187 .update_context_menu = pager_update_context_menu,
188 .reconfigure = pager_panel_configuration_changed
189 };