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