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