[l10n] Update POT file.
[lxde/lxpanel.git] / plugins / image.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>
e7cb732b 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
30
31typedef struct {
a52c2257 32 GtkWidget *mainw;
da76d5cf 33 char* config_data;
a52c2257
HJYP
34} image;
35
a52c2257 36static void
22242ed4 37image_destructor(Plugin *p)
a52c2257
HJYP
38{
39 image *img = (image *)p->priv;
40
41 ENTER;
42 gtk_widget_destroy(img->mainw);
da76d5cf 43 g_free( img->config_data );
a52c2257
HJYP
44 g_free(img);
45 RET();
46}
47
a52c2257 48static int
22242ed4 49image_constructor(Plugin *p, char **fp)
a52c2257
HJYP
50{
51 gchar *tooltip, *fname;
52 image *img;
53 GdkPixbuf *gp, *gps;
54 GtkWidget *wid;
55 GError *err = NULL;
da76d5cf
HJYP
56 char *config_start, *config_end;
57
a52c2257
HJYP
58 line s;
59
60 s.len = 256;
61 ENTER;
62 img = g_new0(image, 1);
63 g_return_val_if_fail(img != NULL, 0);
a52c2257
HJYP
64 p->priv = img;
65 tooltip = fname = 0;
da76d5cf
HJYP
66 if( fp ) {
67 config_start = *fp;
db449f6e
HJYP
68 while (lxpanel_get_line(fp, &s) != LINE_BLOCK_END) {
69 if (s.type == LINE_NONE) {
70 ERR( "image: illegal token %s\n", s.str);
71 goto error;
72 }
73 if (s.type == LINE_VAR) {
da76d5cf 74 if (!g_ascii_strcasecmp(s.t[0], "image"))
db449f6e
HJYP
75 fname = expand_tilda(s.t[1]);
76 else if (!g_ascii_strcasecmp(s.t[0], "tooltip"))
77 tooltip = g_strdup(s.t[1]);
78 else {
79 ERR( "image: unknown var %s\n", s.t[0]);
80 goto error;
81 }
82 } else {
83 ERR( "image: illegal in this context %s\n", s.str);
a52c2257
HJYP
84 goto error;
85 }
a52c2257 86 }
da76d5cf
HJYP
87 config_end = *fp - 1;
88 while( *config_end != '}' && config_end > config_start ) {
89 --config_end;
90 }
91 if( *config_end == '}' )
92 --config_end;
93 img->config_data = g_strndup( config_start,
94 (config_end-config_start) );
95 }
96 else {
97 config_start = config_end = NULL;
a52c2257
HJYP
98 }
99 img->mainw = gtk_event_box_new();
100 gtk_widget_show(img->mainw);
101 //g_signal_connect(G_OBJECT(img->mainw), "expose_event",
102 // G_CALLBACK(gtk_widget_queue_draw), NULL);
103 gp = gdk_pixbuf_new_from_file(fname, &err);
104 if (!gp) {
105 g_warning("image: can't read image %s\n", fname);
106 wid = gtk_label_new("?");
107 } else {
108 float ratio;
cfde283a 109 ratio = (p->panel->orientation == GTK_ORIENTATION_HORIZONTAL) ?
a52c2257
HJYP
110 (float) (p->panel->ah - 2) / (float) gdk_pixbuf_get_height(gp)
111 : (float) (p->panel->aw - 2) / (float) gdk_pixbuf_get_width(gp);
112 gps = gdk_pixbuf_scale_simple (gp,
113 ratio * ((float) gdk_pixbuf_get_width(gp)),
114 ratio * ((float) gdk_pixbuf_get_height(gp)),
115 GDK_INTERP_HYPER);
f176735a 116 wid = gtk_image_new_from_pixbuf(gps);
d40b6601
HG
117 g_object_unref(gp);
118 g_object_unref(gps);
a52c2257
HJYP
119
120 }
121 gtk_widget_show(wid);
122 gtk_container_add(GTK_CONTAINER(img->mainw), wid);
123 gtk_container_set_border_width(GTK_CONTAINER(img->mainw), 0);
124 g_free(fname);
4542c20d 125
a52c2257 126 if (tooltip) {
f176735a 127 gtk_widget_set_tooltip_text(img->mainw, tooltip);
a52c2257
HJYP
128 g_free(tooltip);
129 }
130 RET(1);
131
132 error:
133 g_free(fname);
134 g_free(tooltip);
135 image_destructor(p);
136 RET(0);
137}
138
22242ed4 139static void save_config( Plugin* p, FILE* fp )
9c97f69e 140{
da76d5cf
HJYP
141 image *img = (image *)p->priv;
142 if( img->config_data ) {
143 char** lines = g_strsplit( img->config_data, "\n", 0 );
144 char** line;
145 for( line = lines; *line; ++line ) {
146 g_strstrip( *line );
147 if( **line )
148 lxpanel_put_line( fp, *line );
149 }
150 g_strfreev( lines );
151 }
9c97f69e 152}
a52c2257 153
22242ed4 154PluginClass image_plugin_class = {
3c3e9c9e
HG
155 .fname = NULL,
156 .count = 0,
157
158 .type = "image",
159 .name = "image",
160 .version = "1.0",
161 .description = N_("Display Image and Tooltip"),
162
163 .constructor = image_constructor,
164 .destructor = image_destructor,
165 .config = NULL,
166 .save = save_config
a52c2257 167};