Enabling multithreaded compilation.
[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 GtkWidget *mainw;
33 char* config_data;
34 } image;
35
36 static void
37 image_destructor(Plugin *p)
38 {
39 image *img = (image *)p->priv;
40
41 ENTER;
42 gtk_widget_destroy(img->mainw);
43 g_free( img->config_data );
44 g_free(img);
45 RET();
46 }
47
48 static int
49 image_constructor(Plugin *p, char **fp)
50 {
51 gchar *tooltip, *fname;
52 image *img;
53 GdkPixbuf *gp, *gps;
54 GtkWidget *wid;
55 GError *err = NULL;
56 char *config_start, *config_end;
57
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);
64 p->priv = img;
65 tooltip = fname = 0;
66 if( fp ) {
67 config_start = *fp;
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) {
74 if (!g_ascii_strcasecmp(s.t[0], "image"))
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);
84 goto error;
85 }
86 }
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;
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;
109 ratio = (p->panel->orientation == ORIENT_HORIZ) ?
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);
116 wid = gtk_image_new_from_pixbuf(gps);
117 g_object_unref(gp);
118 g_object_unref(gps);
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);
125
126 if (tooltip) {
127 gtk_widget_set_tooltip_text(img->mainw, tooltip);
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
139 static void save_config( Plugin* p, FILE* fp )
140 {
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 }
152 }
153
154 PluginClass image_plugin_class = {
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
167 };