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.
25 extern awesome_t globalconf
;
27 /** The imagebox private data structure */
38 imagebox_geometry(widget_t
*widget
, int screen
, int height
, int width
)
41 imagebox_data_t
*d
= widget
->data
;
47 geometry
.width
= ((double) height
/ (double) d
->image
->height
) * d
->image
->width
;
48 geometry
.height
= height
;
49 if(geometry
.width
> width
)
55 else if(d
->image
->width
<= width
)
57 geometry
.width
= d
->image
->width
;
58 geometry
.height
= height
;
76 * \param widget The widget.
77 * \param ctx The draw context.
78 * \param geometry The geometry we draw in.
79 * \param screen The screen.
80 * \param p A pointer to the object we're draw onto.
83 imagebox_draw(widget_t
*widget
, draw_context_t
*ctx
, area_t geometry
,
84 int screen
, wibox_t
*p
)
86 imagebox_data_t
*d
= widget
->data
;
88 if(d
->image
&& geometry
.width
&& geometry
.height
)
91 draw_rectangle(ctx
, geometry
, 1.0, true, &d
->bg
);
97 y
+= geometry
.height
- d
->image
->height
;
100 y
+= (geometry
.height
- d
->image
->height
) / 2;
106 draw_image(ctx
, geometry
.x
, y
, d
->resize
? ctx
->height
: 0, d
->image
);
110 /** Delete a imagebox widget.
111 * \param w The widget to destroy.
114 imagebox_destructor(widget_t
*w
)
116 imagebox_data_t
*d
= w
->data
;
117 image_unref(&d
->image
);
122 * \param L The Lua VM state.
123 * \param token The key token.
124 * \param resize Resize image.
125 * \param valign Vertical alignment, top, bottom or center.
126 * \return The number of elements pushed on stack.
128 * \lfield image The image to display.
129 * \lfield bg The background color to use.
132 luaA_imagebox_index(lua_State
*L
, awesome_token_t token
)
134 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
135 imagebox_data_t
*d
= (*widget
)->data
;
141 return luaA_image_userdata_new(L
, d
->image
);
145 luaA_pushcolor(L
, &d
->bg
);
148 lua_pushboolean(L
, d
->resize
);
151 lua_pushstring(L
, draw_align_tostr(d
->valign
));
160 /** The __newindex method for a imagebox object.
161 * \param L The Lua VM state.
162 * \param token The key token.
163 * \return The number of elements pushed on stack.
166 luaA_imagebox_newindex(lua_State
*L
, awesome_token_t token
)
168 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
169 image_t
**image
= NULL
;
170 imagebox_data_t
*d
= (*widget
)->data
;
179 || (image
= luaA_checkudata(L
, 3, "image")))
182 image_unref(&d
->image
);
184 d
->image
= image_ref(image
);
192 else if((buf
= luaL_checklstring(L
, 3, &len
)))
193 xcolor_init_reply(xcolor_init_unchecked(&d
->bg
, buf
, len
));
196 d
->resize
= luaA_checkboolean(L
, 3);
199 if((buf
= luaL_checklstring(L
, 3, &len
)))
200 d
->valign
= draw_align_fromstr(buf
, len
);
206 widget_invalidate_bywidget(*widget
);
212 /** Create a new imagebox widget.
213 * \param align Widget alignment.
214 * \return A brand new widget.
217 imagebox_new(alignment_t align
)
219 widget_t
*w
= p_new(widget_t
, 1);
221 widget_common_new(w
);
223 w
->draw
= imagebox_draw
;
224 w
->index
= luaA_imagebox_index
;
225 w
->newindex
= luaA_imagebox_newindex
;
226 w
->destructor
= imagebox_destructor
;
227 w
->geometry
= imagebox_geometry
;
228 w
->data
= d
= p_new(imagebox_data_t
, 1);
230 d
->valign
= AlignTop
;
235 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80