acbc40dfd576db6cc7dad3a0079b24fb034e6fc0
[lxde/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 /* command to configure desktop, it will be set by .config callback */
37 static const char *configure_command = NULL;
38
39 static void on_realize(GtkWidget *p, LXPanel *panel)
40 {
41 WnckPager *pager = WNCK_PAGER(gtk_bin_get_child(GTK_BIN(p)));
42 int border = gtk_container_get_border_width(GTK_CONTAINER(p));
43 int rows, r, h = panel_get_height(panel);
44
45 /* set geometry */
46 wnck_pager_set_orientation(pager, panel_get_orientation(panel));
47 if (panel_get_orientation(panel) == GTK_ORIENTATION_VERTICAL)
48 h *= ((gfloat) gdk_screen_height() / (gfloat) gdk_screen_width());
49 rows = h / (panel_get_icon_size(panel) * 2) + 1; /* min */
50 r = (h - 2 - 2 * border) / panel_get_icon_size(panel); /* max */
51 /* g_debug("pager for height %d and icon size %d: %d to %d",panel_get_height(panel),panel_get_icon_size(panel),r,rows); */
52 rows = MAX(rows, r);
53 wnck_pager_set_n_rows(pager, rows);
54 }
55
56 static void on_size_allocate(GtkWidget *p, GdkRectangle *allocation, LXPanel *panel)
57 {
58 int border = 1; /* NOTE: old 'pager' used 2, WnckPager has 1, need 1 more */
59
60 /* g_debug("pager: on_size_allocate(): %dx%d", allocation->width, allocation->height); */
61 /* NOTE: we enforce border here since panel sets it to 0
62 FIXME: this behavior should be configurable */
63 gtk_container_set_border_width(GTK_CONTAINER(p), border);
64 on_realize(p, panel);
65 }
66
67 static GtkWidget *pager_constructor(LXPanel *panel, config_setting_t *settings)
68 {
69 GtkWidget *p, *w;
70
71 /* FIXME: use some global setting for border */
72 #if GTK_CHECK_VERSION(3, 0, 0)
73 w = wnck_pager_new();
74 #else
75 w = wnck_pager_new(NULL);
76 #endif
77 g_return_val_if_fail(w != NULL, 0);
78 p = gtk_event_box_new();
79 gtk_widget_set_has_window(p, FALSE);
80
81 /* we cannot configure pager until it added into widgets hierarchy */
82 g_signal_connect(p, "realize", G_CALLBACK(on_realize), panel);
83 g_signal_connect(p, "size-allocate", G_CALLBACK(on_size_allocate), panel);
84 wnck_pager_set_display_mode(WNCK_PAGER(w), WNCK_PAGER_DISPLAY_CONTENT);
85
86 gtk_widget_show(w);
87
88 gtk_container_add(GTK_CONTAINER(p), w);
89
90 return p;
91 }
92
93 /* this is a modified version of patch from Lubuntu */
94 static GtkWidget *pager_configure(LXPanel *panel, GtkWidget *instance)
95 {
96 if (configure_command)
97 fm_launch_command_simple(NULL, NULL, G_APP_INFO_CREATE_NONE,
98 configure_command, NULL);
99 else
100 fm_show_error(NULL, NULL,
101 _("Sorry, there was no window manager configuration program found."));
102 return NULL; /* no configuration dialog of lxpanel available */
103 }
104
105 static void pager_menu_callback(GtkWidget *widget, gpointer data)
106 {
107 gtk_widget_set_sensitive(widget, FALSE);
108 }
109
110 static gboolean pager_update_context_menu(GtkWidget *plugin, GtkMenu *menu)
111 {
112 GdkScreen *screen = gdk_screen_get_default();
113 const char *wm_name = gdk_x11_screen_get_window_manager_name(screen);
114 char *path = NULL;
115
116 /* update configure_command */
117 configure_command = NULL;
118 if (g_strcmp0(wm_name, "Openbox") == 0)
119 {
120 if ((path = g_find_program_in_path("obconf")))
121 {
122 configure_command = "obconf --tab 6";
123 }
124 }
125 else if (g_strcmp0(wm_name, "compiz") == 0)
126 {
127 if ((path = g_find_program_in_path("ccsm")))
128 {
129 configure_command = "ccsm";
130 }
131 else if ((path = g_find_program_in_path("simple-ccsm")))
132 {
133 configure_command = "simple-ccsm";
134 }
135 }
136 /* FIXME: support other WMs */
137 if (configure_command == NULL)
138 {
139 /* disable 'Settings' menu item */
140 gtk_container_foreach(GTK_CONTAINER(menu), pager_menu_callback, NULL);
141 }
142 /* FIXME: else replace 'Settings' item label with "Launch Workspaces Configurator (%s)" */
143 g_free(path);
144 return FALSE;
145 }
146
147 static void pager_panel_configuration_changed(LXPanel *panel, GtkWidget *p)
148 {
149 on_realize(p, panel);
150 }
151
152 static LXPanelPluginInit wnck_pager = {
153 .name = N_("Desktop Pager"),
154 .description = N_("Simple pager plugin"),
155
156 .superseded = TRUE,
157 .new_instance = pager_constructor,
158 .config = pager_configure,
159 .update_context_menu = pager_update_context_menu,
160 .reconfigure = pager_panel_configuration_changed
161 };
162
163 static void pager_wnck_init(void)
164 {
165 lxpanel_register_plugin_type("wnckpager", &wnck_pager);
166 }
167
168 LXPanelPluginInit lxpanel_static_plugin_pager = {
169 .name = N_("Desktop Pager"),
170 .description = N_("Simple pager plugin"),
171
172 .init = pager_wnck_init,
173 .new_instance = pager_constructor,
174 .config = pager_configure,
175 .update_context_menu = pager_update_context_menu,
176 .reconfigure = pager_panel_configuration_changed
177 };