[awesome-menu] Fix problem with filling via stdin was overwritten
[awesome.git] / widgets / iconbox.c
blobe48b668d68aaaf3de45dce9a9455a3eeccae7ee2
1 /*
2 * iconbox.c - icon widget
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "widget.h"
23 #include "common/util.h"
25 extern AwesomeConf globalconf;
27 typedef struct
29 char *image;
30 Bool resize;
31 } Data;
33 static int
34 iconbox_draw(Widget *widget, DrawCtx *ctx, int offset,
35 int used __attribute__ ((unused)))
37 Data *d = widget->data;
38 Area area = draw_get_image_size(d->image);
40 /* image not valid */
41 if(area.width < 0 || area.height < 0)
42 return (widget->area.width = 0);
44 if(d->resize)
45 widget->area.width = ((double) widget->statusbar->height / area.height) * area.width;
46 else
47 widget->area.width = area.width;
49 widget->area.height = widget->statusbar->height;
51 if(!widget->user_supplied_x)
52 widget->area.x = widget_calculate_offset(widget->statusbar->width,
53 widget->area.width,
54 offset,
55 widget->alignment);
57 if(!widget->user_supplied_y)
58 widget->area.y = 0;
60 draw_image(ctx, widget->area.x, widget->area.y,
61 d->resize ? widget->statusbar->height : 0, d->image);
63 return widget->area.width;
66 static widget_tell_status_t
67 iconbox_tell(Widget *widget, char *property, char *command)
69 Bool b;
70 Data *d = widget->data;
72 if(!property || !command)
73 return WIDGET_ERROR;
75 if(!a_strcmp(property, "image"))
77 if(d->image)
78 p_delete(&d->image);
79 d->image = a_strdup(command);
81 else if(!a_strcmp(property, "resize"))
83 if((b = cfg_parse_boolean(command)) != -1)
84 d->resize = b;
85 else
86 return WIDGET_ERROR_FORMAT_BOOL;
88 else
89 return WIDGET_ERROR;
91 return WIDGET_NOERROR;
94 Widget *
95 iconbox_new(Statusbar *statusbar, cfg_t *config)
97 Widget *w;
98 Data *d;
100 w = p_new(Widget, 1);
101 widget_common_new(w, statusbar, config);
102 w->alignment = draw_get_align(cfg_getstr(config, "align"));
103 w->draw = iconbox_draw;
104 w->tell = iconbox_tell;
105 w->data = d = p_new(Data, 1);
106 d->image = a_strdup(cfg_getstr(config, "image"));
107 d->resize = cfg_getbool(config, "resize");
109 return w;
111 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80