Adding upstream version 0.3.5.2+svn20080509.
[debian/lxpanel.git] / src / plugins / image.c
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
19 #include <stdlib.h>
20
21 #include <gdk-pixbuf/gdk-pixbuf.h>
22 #include <glib/gi18n.h>
23
24 #include "panel.h"
25 #include "misc.h"
26 #include "plugin.h"
27
28 #include "dbg.h"
29
30
31 typedef struct {
32 GdkPixmap *pix;
33 GdkBitmap *mask;
34 GtkTooltips *tips;
35 GtkWidget *mainw;
36 char* config_data;
37 } image;
38
39 static void
40 image_destructor(Plugin *p)
41 {
42 image *img = (image *)p->priv;
43
44 ENTER;
45 gtk_widget_destroy(img->mainw);
46 if (img->mask)
47 g_object_unref(img->mask);
48 if (img->pix)
49 g_object_unref(img->pix);
50 g_free( img->config_data );
51 g_object_unref( img->tips );
52 g_free(img);
53 RET();
54 }
55
56 static int
57 image_constructor(Plugin *p, char **fp)
58 {
59 gchar *tooltip, *fname;
60 image *img;
61 GdkPixbuf *gp, *gps;
62 GtkWidget *wid;
63 GError *err = NULL;
64 char *config_start, *config_end;
65
66 line s;
67
68 s.len = 256;
69 ENTER;
70 img = g_new0(image, 1);
71 g_return_val_if_fail(img != NULL, 0);
72 img->tips = gtk_tooltips_new();
73 #if GLIB_CHECK_VERSION( 2, 10, 0 )
74 g_object_ref_sink( img->tips );
75 #else
76 g_object_ref( img->tips );
77 gtk_object_sink( img->tips );
78 #endif
79 p->priv = img;
80 tooltip = fname = 0;
81 if( fp ) {
82 config_start = *fp;
83 while (lxpanel_get_line(fp, &s) != LINE_BLOCK_END) {
84 if (s.type == LINE_NONE) {
85 ERR( "image: illegal token %s\n", s.str);
86 goto error;
87 }
88 if (s.type == LINE_VAR) {
89 if (!g_ascii_strcasecmp(s.t[0], "image"))
90 fname = expand_tilda(s.t[1]);
91 else if (!g_ascii_strcasecmp(s.t[0], "tooltip"))
92 tooltip = g_strdup(s.t[1]);
93 else {
94 ERR( "image: unknown var %s\n", s.t[0]);
95 goto error;
96 }
97 } else {
98 ERR( "image: illegal in this context %s\n", s.str);
99 goto error;
100 }
101 }
102 config_end = *fp - 1;
103 while( *config_end != '}' && config_end > config_start ) {
104 --config_end;
105 }
106 if( *config_end == '}' )
107 --config_end;
108 img->config_data = g_strndup( config_start,
109 (config_end-config_start) );
110 }
111 else {
112 config_start = config_end = NULL;
113 }
114 img->mainw = gtk_event_box_new();
115 gtk_widget_show(img->mainw);
116 //g_signal_connect(G_OBJECT(img->mainw), "expose_event",
117 // G_CALLBACK(gtk_widget_queue_draw), NULL);
118 gp = gdk_pixbuf_new_from_file(fname, &err);
119 if (!gp) {
120 g_warning("image: can't read image %s\n", fname);
121 wid = gtk_label_new("?");
122 } else {
123 float ratio;
124 ratio = (p->panel->orientation == ORIENT_HORIZ) ?
125 (float) (p->panel->ah - 2) / (float) gdk_pixbuf_get_height(gp)
126 : (float) (p->panel->aw - 2) / (float) gdk_pixbuf_get_width(gp);
127 gps = gdk_pixbuf_scale_simple (gp,
128 ratio * ((float) gdk_pixbuf_get_width(gp)),
129 ratio * ((float) gdk_pixbuf_get_height(gp)),
130 GDK_INTERP_HYPER);
131 gdk_pixbuf_render_pixmap_and_mask(gps, &img->pix, &img->mask, 127);
132 gdk_pixbuf_unref(gp);
133 gdk_pixbuf_unref(gps);
134 wid = gtk_image_new_from_pixmap(img->pix, img->mask);
135
136 }
137 gtk_widget_show(wid);
138 gtk_container_add(GTK_CONTAINER(img->mainw), wid);
139 gtk_container_set_border_width(GTK_CONTAINER(img->mainw), 0);
140 g_free(fname);
141
142 if (tooltip) {
143 gtk_tooltips_set_tip(GTK_TOOLTIPS (img->tips), img->mainw, tooltip, NULL);
144 g_free(tooltip);
145 }
146 RET(1);
147
148 error:
149 g_free(fname);
150 g_free(tooltip);
151 image_destructor(p);
152 RET(0);
153 }
154
155 static void save_config( Plugin* p, FILE* fp )
156 {
157 image *img = (image *)p->priv;
158 if( img->config_data ) {
159 char** lines = g_strsplit( img->config_data, "\n", 0 );
160 char** line;
161 for( line = lines; *line; ++line ) {
162 g_strstrip( *line );
163 if( **line )
164 lxpanel_put_line( fp, *line );
165 }
166 g_strfreev( lines );
167 }
168 }
169
170 PluginClass image_plugin_class = {
171 fname: NULL,
172 count: 0,
173
174 type : "image",
175 name : "image",
176 version: "1.0",
177 description : N_("Dispaly Image and Tooltip"),
178
179 constructor : image_constructor,
180 destructor : image_destructor,
181 config : NULL,
182 save : save_config
183 };