Initial import.
[lxde/lxpanel.git] / src / plugins / image.c
1 #include <stdlib.h>
2
3 #include <gdk-pixbuf/gdk-pixbuf.h>
4
5 #include "panel.h"
6 #include "misc.h"
7 #include "plugin.h"
8
9 //#define DEBUG
10 #include "dbg.h"
11
12
13 typedef struct {
14 GdkPixmap *pix;
15 GdkBitmap *mask;
16 GtkTooltips *tips;
17 GtkWidget *mainw;
18 } image;
19
20
21
22 static void
23 image_destructor(plugin *p)
24 {
25 image *img = (image *)p->priv;
26
27 ENTER;
28 gtk_widget_destroy(img->mainw);
29 if (img->mask)
30 g_object_unref(img->mask);
31 if (img->pix)
32 g_object_unref(img->pix);
33 //
34 g_free(img);
35 RET();
36 }
37
38
39
40
41 static int
42 image_constructor(plugin *p)
43 {
44 gchar *tooltip, *fname;
45 image *img;
46 GdkPixbuf *gp, *gps;
47 GtkWidget *wid;
48 GError *err = NULL;
49
50 line s;
51
52 s.len = 256;
53 ENTER;
54 img = g_new0(image, 1);
55 g_return_val_if_fail(img != NULL, 0);
56 img->tips = gtk_tooltips_new();
57 p->priv = img;
58 tooltip = fname = 0;
59 while (get_line(p->fp, &s) != LINE_BLOCK_END) {
60 if (s.type == LINE_NONE) {
61 ERR( "image: illegal token %s\n", s.str);
62 goto error;
63 }
64 if (s.type == LINE_VAR) {
65 if (!g_ascii_strcasecmp(s.t[0], "image"))
66 fname = expand_tilda(s.t[1]);
67 else if (!g_ascii_strcasecmp(s.t[0], "tooltip"))
68 tooltip = g_strdup(s.t[1]);
69 else {
70 ERR( "image: unknown var %s\n", s.t[0]);
71 goto error;
72 }
73 } else {
74 ERR( "image: illegal in this context %s\n", s.str);
75 goto error;
76 }
77 }
78 img->mainw = gtk_event_box_new();
79 gtk_widget_show(img->mainw);
80 //g_signal_connect(G_OBJECT(img->mainw), "expose_event",
81 // G_CALLBACK(gtk_widget_queue_draw), NULL);
82 gp = gdk_pixbuf_new_from_file(fname, &err);
83 if (!gp) {
84 g_warning("image: can't read image %s\n", fname);
85 wid = gtk_label_new("?");
86 } else {
87 float ratio;
88
89 ratio = (p->panel->orientation == ORIENT_HORIZ) ?
90 (float) (p->panel->ah - 2) / (float) gdk_pixbuf_get_height(gp)
91 : (float) (p->panel->aw - 2) / (float) gdk_pixbuf_get_width(gp);
92 gps = gdk_pixbuf_scale_simple (gp,
93 ratio * ((float) gdk_pixbuf_get_width(gp)),
94 ratio * ((float) gdk_pixbuf_get_height(gp)),
95 GDK_INTERP_HYPER);
96 gdk_pixbuf_render_pixmap_and_mask(gps, &img->pix, &img->mask, 127);
97 gdk_pixbuf_unref(gp);
98 gdk_pixbuf_unref(gps);
99 wid = gtk_image_new_from_pixmap(img->pix, img->mask);
100
101 }
102 gtk_widget_show(wid);
103 gtk_container_add(GTK_CONTAINER(img->mainw), wid);
104 gtk_container_set_border_width(GTK_CONTAINER(img->mainw), 0);
105 g_free(fname);
106 gtk_container_add(GTK_CONTAINER(p->pwid), img->mainw);
107 if (tooltip) {
108 gtk_tooltips_set_tip(GTK_TOOLTIPS (img->tips), img->mainw, tooltip, NULL);
109 g_free(tooltip);
110 }
111 RET(1);
112
113 error:
114 g_free(fname);
115 g_free(tooltip);
116 image_destructor(p);
117 RET(0);
118 }
119
120
121 plugin_class image_plugin_class = {
122 fname: NULL,
123 count: 0,
124
125 type : "image",
126 name : "image",
127 version: "1.0",
128 description : "Dispaly Image and Tooltip",
129
130 constructor : image_constructor,
131 destructor : image_destructor,
132 };