Move internal data into 'private.h' header for better new plugins design.
[lxde/lxpanel.git] / src / plugins / space.c
CommitLineData
5d344669
AL
1/**
2 * Copyright (c) 2006 LxDE Developers, see the file AUTHORS for details.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
a52c2257
HJYP
19#include <stdlib.h>
20
21#include <gdk-pixbuf/gdk-pixbuf.h>
ce522551 22#include <glib/gi18n.h>
a52c2257
HJYP
23
24#include "panel.h"
25#include "misc.h"
b31cb1d2 26#include "private.h"
a52c2257 27
a52c2257
HJYP
28#include "dbg.h"
29
2918994e 30/* Private context for space plugin. */
a52c2257 31typedef struct {
2918994e 32 int size; /* Size of spacer */
33} SpacePlugin;
a52c2257 34
2918994e 35static int space_constructor(Plugin * p, char ** fp);
36static void space_destructor(Plugin * p);
37static void space_apply_configuration(Plugin * p);
38static void space_configure(Plugin * p, GtkWindow * parent);
39static void space_save_configuration(Plugin * p, FILE * fp);
a52c2257 40
2918994e 41/* Plugin constructor. */
42static int space_constructor(Plugin * p, char ** fp)
366cf5fe 43{
2918994e 44 /* Allocate plugin context and set into Plugin private data pointer. */
45 SpacePlugin * sp = g_new0(SpacePlugin, 1);
46 p->priv = sp;
366cf5fe 47
2918994e 48 /* Load parameters from the configuration file. */
a52c2257 49 line s;
4542c20d 50 s.len = 256;
2918994e 51 if (fp != NULL)
db449f6e 52 {
2918994e 53 while (lxpanel_get_line(fp, &s) != LINE_BLOCK_END)
54 {
55 if (s.type == LINE_NONE)
56 {
db449f6e 57 ERR( "space: illegal token %s\n", s.str);
2918994e 58 return 0;
db449f6e 59 }
2918994e 60 if (s.type == LINE_VAR)
61 {
62 if (g_ascii_strcasecmp(s.t[0], "size") == 0)
db449f6e 63 sp->size = atoi(s.t[1]);
2918994e 64 else
db449f6e 65 ERR( "space: unknown var %s\n", s.t[0]);
2918994e 66 }
67 else
68 {
db449f6e 69 ERR( "space: illegal in this context %s\n", s.str);
2918994e 70 return 0;
a52c2257 71 }
a52c2257
HJYP
72 }
73 }
592ce930 74
2918994e 75 /* Default the size parameter. */
76 if (sp->size == 0)
77 sp->size = 2;
4542c20d 78
2918994e 79 /* Allocate top level widget and set into Plugin widget pointer. */
592ce930 80 p->pwid = gtk_event_box_new();
d5c46ffc
JL
81#if GTK_CHECK_VERSION(2,18,0)
82 gtk_widget_set_has_window(p->pwid,FALSE);
83#else
2918994e 84 GTK_WIDGET_SET_FLAGS(p->pwid, GTK_NO_WINDOW);
d5c46ffc 85#endif
2918994e 86 gtk_widget_add_events(p->pwid, GDK_BUTTON_PRESS_MASK);
592ce930 87 gtk_container_set_border_width(GTK_CONTAINER(p->pwid), 0);
a52c2257 88
2918994e 89 /* Connect signals. */
90 g_signal_connect(p->pwid, "button-press-event", G_CALLBACK(plugin_button_press_event), p);
a52c2257 91
2918994e 92 /* Apply the configuration and show the widget. */
93 space_apply_configuration(p);
94 gtk_widget_show(p->pwid);
95 return 1;
a52c2257
HJYP
96}
97
2918994e 98/* Plugin destructor. */
99static void space_destructor(Plugin * p)
8ccd023a 100{
2918994e 101 SpacePlugin * sp = (SpacePlugin *) p->priv;
102 g_free(sp);
04883e73
HJYP
103}
104
2918994e 105/* Callback when the configuration dialog has recorded a configuration change. */
106static void space_apply_configuration(Plugin * p)
04883e73 107{
2918994e 108 SpacePlugin * sp = (SpacePlugin *) p->priv;
109
110 /* Apply settings. */
111 if (p->panel->orientation == ORIENT_HORIZ)
112 gtk_widget_set_size_request(p->pwid, sp->size, 2);
113 else
114 gtk_widget_set_size_request(p->pwid, 2, sp->size);
8ccd023a 115}
a52c2257 116
2918994e 117/* Callback when the configuration dialog is to be shown. */
118static void space_configure(Plugin * p, GtkWindow * parent)
9c97f69e 119{
2918994e 120 SpacePlugin * sp = (SpacePlugin *) p->priv;
121 GtkWidget * dlg = create_generic_config_dlg(
148553b3 122 _(p->class->name),
2918994e 123 GTK_WIDGET(parent),
124 (GSourceFunc) space_apply_configuration, (gpointer) p,
125 _("Size"), &sp->size, CONF_TYPE_INT, NULL);
126 gtk_widget_set_size_request(GTK_WIDGET(dlg), 200, -1); /* Improve geometry */
127 gtk_window_present(GTK_WINDOW(dlg));
9c97f69e
HJYP
128}
129
2918994e 130/* Callback when the configuration is to be saved. */
131static void space_save_configuration(Plugin * p, FILE * fp)
132{
133 SpacePlugin * sp = (SpacePlugin *) p->priv;
134 lxpanel_put_int(fp, "Size", sp->size);
135}
136
137/* Plugin descriptor. */
22242ed4 138PluginClass space_plugin_class = {
2918994e 139
140 PLUGINCLASS_VERSIONING,
a52c2257 141
3c3e9c9e
HG
142 .type = "space",
143 .name = N_("Spacer"),
144 .version = "1.0",
145 .description = N_("Allocate space"),
a52c2257 146
2918994e 147 /* Stretch is available but not default for this plugin. */
3c3e9c9e 148 .expand_available = TRUE,
2918994e 149
3c3e9c9e
HG
150 .constructor = space_constructor,
151 .destructor = space_destructor,
152 .config = space_configure,
153 .save = space_save_configuration
a52c2257 154};