widgets: allow some value to be nil
[awesome.git] / widgets / imagebox.c
blobf62f53f0d0c4f4335fbaeff8a433ab3a627762a2
1 /*
2 * imagebox.c - imagebox widget
4 * Copyright © 2008-2009 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 "luaa.h"
24 #include "titlebar.h"
26 /** The imagebox private data structure */
27 typedef struct
29 /** Imagebox image */
30 image_t *image;
31 color_t bg;
32 bool resize;
33 } imagebox_data_t;
35 static area_t
36 imagebox_extents(lua_State *L, widget_t *widget)
38 area_t geometry = { .x = 0, .y = 0 };
39 imagebox_data_t *d = widget->data;
41 if(d->image)
43 geometry.width = image_getwidth(d->image);
44 geometry.height = image_getheight(d->image);
46 else
48 geometry.width = 0;
49 geometry.height = 0;
52 return geometry;
55 /** Draw an image.
56 * \param widget The widget.
57 * \param ctx The draw context.
58 * \param geometry The geometry we draw in.
59 * \param p A pointer to the object we're draw onto.
61 static void
62 imagebox_draw(widget_t *widget, draw_context_t *ctx, area_t geometry, wibox_t *p)
64 imagebox_data_t *d = widget->data;
66 if(d->image && geometry.width && geometry.height)
68 if(d->bg.initialized)
69 draw_rectangle(ctx, geometry, 1.0, true, &d->bg);
71 double ratio = d->resize ? (double) geometry.height / image_getheight(d->image) : 1;
72 draw_image(ctx, geometry.x, geometry.y, ratio, d->image);
76 /** Delete a imagebox widget.
77 * \param w The widget to destroy.
79 static void
80 imagebox_destructor(widget_t *w)
82 imagebox_data_t *d = w->data;
83 p_delete(&d);
86 /** Imagebox widget.
87 * \param L The Lua VM state.
88 * \param token The key token.
89 * \param resize Resize image.
90 * \return The number of elements pushed on stack.
91 * \luastack
92 * \lfield image The image to display.
93 * \lfield bg The background color to use.
95 static int
96 luaA_imagebox_index(lua_State *L, awesome_token_t token)
98 widget_t *widget = luaA_checkudata(L, 1, &widget_class);
99 imagebox_data_t *d = widget->data;
101 switch(token)
103 case A_TK_IMAGE:
104 luaA_object_push_item(L, 1, d->image);
105 break;
106 case A_TK_BG:
107 luaA_pushcolor(L, &d->bg);
108 break;
109 case A_TK_RESIZE:
110 lua_pushboolean(L, d->resize);
111 break;
112 default:
113 return 0;
116 return 1;
119 /** The __newindex method for a imagebox object.
120 * \param L The Lua VM state.
121 * \param token The key token.
122 * \return The number of elements pushed on stack.
124 static int
125 luaA_imagebox_newindex(lua_State *L, awesome_token_t token)
127 widget_t *widget = luaA_checkudata(L, 1, &widget_class);
128 imagebox_data_t *d = widget->data;
130 switch(token)
132 const char *buf;
133 size_t len;
135 case A_TK_IMAGE:
136 luaA_checkudataornil(L, -1, &image_class);
137 luaA_object_unref_item(L, 1, d->image);
138 d->image = luaA_object_ref_item(L, 1, 3);
139 break;
140 case A_TK_BG:
141 if(lua_isnil(L, 3))
142 p_clear(&d->bg, 1);
143 else if((buf = luaL_checklstring(L, 3, &len)))
144 color_init_reply(color_init_unchecked(&d->bg, buf, len));
145 break;
146 case A_TK_RESIZE:
147 d->resize = luaA_checkboolean(L, 3);
148 break;
149 default:
150 return 0;
153 widget_invalidate_bywidget(widget);
155 return 0;
159 /** Create a new imagebox widget.
160 * \param w The widget to initialize.
161 * \return A brand new widget.
163 widget_t *
164 widget_imagebox(widget_t *w)
166 imagebox_data_t *d;
167 w->draw = imagebox_draw;
168 w->index = luaA_imagebox_index;
169 w->newindex = luaA_imagebox_newindex;
170 w->destructor = imagebox_destructor;
171 w->extents = imagebox_extents;
172 w->data = d = p_new(imagebox_data_t, 1);
173 d->resize = true;
175 return w;
178 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80