widgets: use a geometry callback
[awesome.git] / widgets / imagebox.c
blob4ebe1b093a6bebb5432d2c0bf45e04cbe6c0b6aa
1 /*
2 * imagebox.c - imagebox widget
4 * Copyright © 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 "titlebar.h"
25 extern awesome_t globalconf;
27 /** The imagebox private data structure */
28 typedef struct
30 /** Imagebox image */
31 image_t *image;
32 xcolor_t bg;
33 } imagebox_data_t;
35 static area_t
36 imagebox_geometry(widget_t *widget, int screen, int height, int width)
38 area_t geometry;
39 imagebox_data_t *d = widget->data;
41 if(d->image)
43 geometry.height = height;
44 geometry.width = ((double) height / (double) d->image->height) * d->image->width;
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 screen The screen.
60 * \param p A pointer to the object we're draw onto.
62 static void
63 imagebox_draw(widget_t *widget, draw_context_t *ctx, area_t geometry,
64 int screen, wibox_t *p)
66 imagebox_data_t *d = widget->data;
68 if(d->image)
70 if(d->bg.initialized)
71 draw_rectangle(ctx, geometry, 1.0, true, &d->bg);
72 draw_image(ctx, geometry.x, geometry.y, ctx->height, 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 image_unref(&d->image);
84 p_delete(&d);
87 /** Imagebox widget.
88 * \param L The Lua VM state.
89 * \param token The key token.
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");
99 imagebox_data_t *d = (*widget)->data;
101 switch(token)
103 case A_TK_IMAGE:
104 if(d->image)
105 return luaA_image_userdata_new(L, d->image);
106 case A_TK_BG:
107 luaA_pushcolor(L, &d->bg);
108 break;
109 default:
110 break;
112 return 0;
115 /** The __newindex method for a imagebox object.
116 * \param L The Lua VM state.
117 * \param token The key token.
118 * \return The number of elements pushed on stack.
120 static int
121 luaA_imagebox_newindex(lua_State *L, awesome_token_t token)
123 widget_t **widget = luaA_checkudata(L, 1, "widget");
124 image_t **image = NULL;
125 imagebox_data_t *d = (*widget)->data;
127 switch(token)
129 const char *buf;
130 size_t len;
132 case A_TK_IMAGE:
133 if(lua_isnil(L, 3)
134 || (image = luaA_checkudata(L, 3, "image")))
136 /* unref image */
137 image_unref(&d->image);
138 if(image)
139 d->image = image_ref(image);
140 else
141 d->image = NULL;
143 break;
144 case A_TK_BG:
145 if(lua_isnil(L, 3))
146 p_clear(&d->bg, 1);
147 else if((buf = luaL_checklstring(L, 3, &len)))
148 xcolor_init_reply(xcolor_init_unchecked(&d->bg, buf, len));
149 break;
150 default:
151 return 0;
154 widget_invalidate_bywidget(*widget);
156 return 0;
160 /** Create a new imagebox widget.
161 * \param align Widget alignment.
162 * \return A brand new widget.
164 widget_t *
165 imagebox_new(alignment_t align)
167 widget_t *w = p_new(widget_t, 1);
168 widget_common_new(w);
169 w->align = align;
170 w->draw = imagebox_draw;
171 w->index = luaA_imagebox_index;
172 w->newindex = luaA_imagebox_newindex;
173 w->destructor = imagebox_destructor;
174 w->geometry = imagebox_geometry;
175 w->data = p_new(imagebox_data_t, 1);
177 return w;
180 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80