Method size_allocate of PanelIconGrid should use gtk_widget_set_allocation().
[lxde/lxpanel.git] / src / plugins / pager.c
CommitLineData
365bfaec
AG
1/* pager.c -- pager module of lxpanel project
2 *
3 * Copyright (C) 2009 Dongxu Li <song6song@sourceforge.net>
eb713fbc 4 * 2012 Julien Lavergne <gilir@ubuntu.com>
365bfaec
AG
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>
eb713fbc
AG
25#include <gdk/gdkx.h>
26#include <libfm/fm-gtk.h>
365bfaec
AG
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
0c86059b 34#include "plugin.h"
365bfaec 35
eb713fbc
AG
36/* command to configure desktop, it will be set by .config callback */
37static const char *configure_command = NULL;
365bfaec 38
a7bd16a4 39static void on_realize(GtkWidget *p, LXPanel *panel)
365bfaec 40{
0c86059b 41 WnckPager *pager = WNCK_PAGER(gtk_bin_get_child(GTK_BIN(p)));
4da53742 42 int rows, r, h = panel_get_height(panel);
0c86059b
AG
43
44 /* set geometry */
45 wnck_pager_set_orientation(pager, panel_get_orientation(panel));
4da53742
AG
46 if (panel_get_orientation(panel) == GTK_ORIENTATION_VERTICAL)
47 h *= ((gfloat) gdk_screen_height() / (gfloat) gdk_screen_width());
48 rows = h / (panel_get_icon_size(panel) * 2) + 1; /* min */
49 r = (h - 4) / panel_get_icon_size(panel); /* max */
0c86059b
AG
50 /* g_debug("pager for height %d and icon size %d: %d to %d",panel_get_height(panel),panel_get_icon_size(panel),r,rows); */
51 rows = MAX(rows, r);
52 wnck_pager_set_n_rows(pager, rows);
53}
365bfaec 54
a7bd16a4 55static void on_size_allocate(GtkWidget *p, GdkRectangle *allocation, LXPanel *panel)
4da53742
AG
56{
57 /* g_debug("pager: on_size_allocate(): %dx%d", allocation->width, allocation->height); */
58 on_realize(p, panel);
59}
60
a7bd16a4 61static GtkWidget *pager_constructor(LXPanel *panel, config_setting_t *settings)
0c86059b
AG
62{
63 GtkWidget *p, *w;
365bfaec 64
0c86059b
AG
65 w = wnck_pager_new(NULL);
66 g_return_val_if_fail(w != NULL, 0);
67 p = gtk_event_box_new();
09fa171b 68 gtk_widget_set_has_window(p, FALSE);
365bfaec 69
0c86059b
AG
70 /* we cannot configure pager until it added into widgets hierarchy */
71 g_signal_connect(p, "realize", G_CALLBACK(on_realize), panel);
4da53742 72 g_signal_connect(p, "size-allocate", G_CALLBACK(on_size_allocate), panel);
0c86059b 73 wnck_pager_set_display_mode(WNCK_PAGER(w), WNCK_PAGER_DISPLAY_CONTENT);
365bfaec 74
0c86059b 75 gtk_widget_show(w);
365bfaec 76
0c86059b
AG
77 gtk_container_set_border_width(GTK_CONTAINER(p), 2);
78 gtk_container_add(GTK_CONTAINER(p), w);
365bfaec 79
0c86059b 80 return p;
365bfaec
AG
81}
82
eb713fbc 83/* this is a modified version of patch from Lubuntu */
752ee4e2 84static GtkWidget *pager_configure(LXPanel *panel, GtkWidget *instance)
eb713fbc
AG
85{
86 if (configure_command)
87 fm_launch_command_simple(NULL, NULL, G_APP_INFO_CREATE_NONE,
88 configure_command, NULL);
89 return NULL; /* no configuration dialog of lxpanel available */
90}
91
92static void pager_menu_callback(GtkWidget *widget, gpointer data)
93{
94 gtk_widget_set_sensitive(widget, FALSE);
95}
96
97static gboolean pager_update_context_menu(GtkWidget *plugin, GtkMenu *menu)
98{
99 GdkScreen *screen = gdk_screen_get_default();
100 const char *wm_name = gdk_x11_screen_get_window_manager_name(screen);
300d5400 101 char *path = NULL;
eb713fbc
AG
102
103 /* update configure_command */
104 configure_command = NULL;
105 if (g_strcmp0(wm_name, "Openbox") == 0)
106 {
300d5400 107 if ((path = g_find_program_in_path("obconf")))
eb713fbc
AG
108 {
109 configure_command = "obconf --tab 6";
110 }
111 }
112 else if (g_strcmp0(wm_name, "compiz") == 0)
113 {
300d5400 114 if ((path = g_find_program_in_path("ccsm")))
eb713fbc
AG
115 {
116 configure_command = "ccsm";
117 }
300d5400 118 else if ((path = g_find_program_in_path("simple-ccsm")))
eb713fbc
AG
119 {
120 configure_command = "simple-ccsm";
121 }
122 }
123 /* FIXME: support other WMs */
124 if (configure_command == NULL)
125 {
126 /* disable 'Settings' menu item */
127 gtk_container_foreach(GTK_CONTAINER(menu), pager_menu_callback, NULL);
128 }
300d5400 129 g_free(path);
eb713fbc
AG
130 return FALSE;
131}
132
a7bd16a4 133static void pager_panel_configuration_changed(LXPanel *panel, GtkWidget *p)
365bfaec 134{
0c86059b 135 on_realize(p, panel);
365bfaec
AG
136}
137
ae4e6dc6
AG
138static LXPanelPluginInit wnck_pager = {
139 .name = N_("Desktop Pager"),
140 .description = N_("Simple pager plugin"),
141
142 .superseded = TRUE,
143 .new_instance = pager_constructor,
eb713fbc
AG
144 .config = pager_configure,
145 .update_context_menu = pager_update_context_menu,
ae4e6dc6
AG
146 .reconfigure = pager_panel_configuration_changed
147};
148
149static void pager_wnck_init(void)
150{
151 lxpanel_register_plugin_type("wnckpager", &wnck_pager);
152}
153
0c86059b 154LXPanelPluginInit lxpanel_static_plugin_pager = {
365bfaec 155 .name = N_("Desktop Pager"),
365bfaec
AG
156 .description = N_("Simple pager plugin"),
157
ae4e6dc6 158 .init = pager_wnck_init,
0c86059b 159 .new_instance = pager_constructor,
eb713fbc
AG
160 .config = pager_configure,
161 .update_context_menu = pager_update_context_menu,
0c86059b 162 .reconfigure = pager_panel_configuration_changed
365bfaec 163};