Fix pager widget size update on panel configuration change.
[lxde/lxpanel.git] / src / plugins / pager.c
1 /* pager.c -- pager module of lxpanel project
2 *
3 * Copyright (C) 2009 Dongxu Li <song6song@sourceforge.net>
4 *
5 * This file is part of lxpanel.
6 *
7 * lxpanel is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * lxpanel is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with sawfish; see the file COPYING. If not, write to
19 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301 USA.
21 */
22
23 #include <gtk/gtk.h>
24
25 #include <glib/gi18n.h>
26 #ifndef WNCK_I_KNOW_THIS_IS_UNSTABLE
27 #define WNCK_I_KNOW_THIS_IS_UNSTABLE
28 #endif
29 #include <libwnck/libwnck.h>
30
31 #include "plugin.h"
32
33
34 static void on_realize(GtkWidget *p, Panel *panel)
35 {
36 WnckPager *pager = WNCK_PAGER(gtk_bin_get_child(GTK_BIN(p)));
37 int rows, r, h = panel_get_height(panel);
38
39 /* set geometry */
40 wnck_pager_set_orientation(pager, panel_get_orientation(panel));
41 if (panel_get_orientation(panel) == GTK_ORIENTATION_VERTICAL)
42 h *= ((gfloat) gdk_screen_height() / (gfloat) gdk_screen_width());
43 rows = h / (panel_get_icon_size(panel) * 2) + 1; /* min */
44 r = (h - 4) / panel_get_icon_size(panel); /* max */
45 /* g_debug("pager for height %d and icon size %d: %d to %d",panel_get_height(panel),panel_get_icon_size(panel),r,rows); */
46 rows = MAX(rows, r);
47 wnck_pager_set_n_rows(pager, rows);
48 }
49
50 static void on_size_allocate(GtkWidget *p, GdkRectangle *allocation, Panel *panel)
51 {
52 /* g_debug("pager: on_size_allocate(): %dx%d", allocation->width, allocation->height); */
53 on_realize(p, panel);
54 }
55
56 static GtkWidget *pager_constructor(Panel *panel, config_setting_t *settings)
57 {
58 GtkWidget *p, *w;
59
60 w = wnck_pager_new(NULL);
61 g_return_val_if_fail(w != NULL, 0);
62 p = gtk_event_box_new();
63 GTK_WIDGET_SET_FLAGS(p, GTK_NO_WINDOW);
64
65 /* we cannot configure pager until it added into widgets hierarchy */
66 g_signal_connect(p, "realize", G_CALLBACK(on_realize), panel);
67 g_signal_connect(p, "size-allocate", G_CALLBACK(on_size_allocate), panel);
68 wnck_pager_set_display_mode(WNCK_PAGER(w), WNCK_PAGER_DISPLAY_CONTENT);
69
70 gtk_widget_show(w);
71
72 gtk_container_set_border_width(GTK_CONTAINER(p), 2);
73 gtk_container_add(GTK_CONTAINER(p), w);
74
75 return p;
76 }
77
78 static void pager_panel_configuration_changed(Panel *panel, GtkWidget *p)
79 {
80 on_realize(p, panel);
81 }
82
83 LXPanelPluginInit lxpanel_static_plugin_pager = {
84 .name = N_("Desktop Pager"),
85 .description = N_("Simple pager plugin"),
86
87 .new_instance = pager_constructor,
88 .reconfigure = pager_panel_configuration_changed
89 };