2008-09-11 15:57:06 +02:00
|
|
|
/*
|
|
|
|
* imagebox.c - imagebox widget
|
|
|
|
*
|
2009-08-17 16:38:56 +02:00
|
|
|
* Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
|
2008-09-11 15:57:06 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-09-14 20:26:33 +02:00
|
|
|
#include "objects/widget.h"
|
2009-08-17 16:38:56 +02:00
|
|
|
#include "luaa.h"
|
2008-09-11 15:57:06 +02:00
|
|
|
|
|
|
|
/** The imagebox private data structure */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
/** Imagebox image */
|
2010-08-20 12:13:03 +02:00
|
|
|
cairo_surface_t *image;
|
2009-04-17 18:59:50 +02:00
|
|
|
color_t bg;
|
2008-11-04 15:31:47 +01:00
|
|
|
bool resize;
|
2008-09-11 15:57:06 +02:00
|
|
|
} imagebox_data_t;
|
|
|
|
|
2008-10-22 17:32:17 +02:00
|
|
|
static area_t
|
2009-08-25 18:26:58 +02:00
|
|
|
imagebox_extents(lua_State *L, widget_t *widget)
|
2008-10-22 17:32:17 +02:00
|
|
|
{
|
2009-08-25 18:26:58 +02:00
|
|
|
area_t geometry = { .x = 0, .y = 0 };
|
2008-10-22 17:32:17 +02:00
|
|
|
imagebox_data_t *d = widget->data;
|
|
|
|
|
|
|
|
if(d->image)
|
|
|
|
{
|
2010-08-20 12:13:03 +02:00
|
|
|
geometry.width = cairo_image_surface_get_width(d->image);
|
|
|
|
geometry.height = cairo_image_surface_get_height(d->image);
|
2008-10-22 17:32:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
geometry.width = 0;
|
|
|
|
geometry.height = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return geometry;
|
|
|
|
}
|
|
|
|
|
2008-09-11 15:57:06 +02:00
|
|
|
/** Draw an image.
|
2008-10-22 17:32:17 +02:00
|
|
|
* \param widget The widget.
|
2008-09-11 15:57:06 +02:00
|
|
|
* \param ctx The draw context.
|
2008-10-22 17:32:17 +02:00
|
|
|
* \param geometry The geometry we draw in.
|
2008-09-11 15:57:06 +02:00
|
|
|
* \param p A pointer to the object we're draw onto.
|
|
|
|
*/
|
2008-10-22 17:32:17 +02:00
|
|
|
static void
|
2009-04-17 16:14:09 +02:00
|
|
|
imagebox_draw(widget_t *widget, draw_context_t *ctx, area_t geometry, wibox_t *p)
|
2008-09-11 15:57:06 +02:00
|
|
|
{
|
2008-10-22 17:32:17 +02:00
|
|
|
imagebox_data_t *d = widget->data;
|
2008-09-11 15:57:06 +02:00
|
|
|
|
2008-11-10 15:22:24 +01:00
|
|
|
if(d->image && geometry.width && geometry.height)
|
2008-09-11 15:57:06 +02:00
|
|
|
{
|
2009-04-17 18:59:50 +02:00
|
|
|
if(d->bg.initialized)
|
|
|
|
draw_rectangle(ctx, geometry, 1.0, true, &d->bg);
|
2008-11-04 16:38:06 +01:00
|
|
|
|
2010-08-20 12:13:03 +02:00
|
|
|
double ratio = d->resize ? (double) geometry.height / cairo_image_surface_get_height(d->image) : 1;
|
|
|
|
draw_surface(ctx, geometry.x, geometry.y, ratio, d->image);
|
2008-09-11 15:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Delete a imagebox widget.
|
|
|
|
* \param w The widget to destroy.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
imagebox_destructor(widget_t *w)
|
|
|
|
{
|
|
|
|
imagebox_data_t *d = w->data;
|
2010-08-20 12:13:03 +02:00
|
|
|
if(d->image)
|
|
|
|
cairo_surface_destroy(d->image);
|
2008-09-11 15:57:06 +02:00
|
|
|
p_delete(&d);
|
|
|
|
}
|
|
|
|
|
2010-08-20 12:13:03 +02:00
|
|
|
static int
|
|
|
|
imagebox_set_image(lua_State *L, widget_t *widget, int idx)
|
|
|
|
{
|
|
|
|
imagebox_data_t *d = widget->data;
|
|
|
|
|
|
|
|
if(lua_isnil(L, idx))
|
|
|
|
{
|
|
|
|
if(d->image)
|
|
|
|
cairo_surface_destroy(d->image);
|
|
|
|
d->image = NULL;
|
|
|
|
} else {
|
|
|
|
bool is_surface = false;
|
|
|
|
/* This inlines luaL_checkudata() but skips luaL_typerror() */
|
|
|
|
if(lua_getmetatable(L, idx))
|
|
|
|
{
|
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, OOCAIRO_MT_NAME_SURFACE);
|
|
|
|
if(lua_rawequal(L, -1, -2))
|
|
|
|
is_surface = true;
|
|
|
|
lua_pop(L, 2);
|
|
|
|
}
|
|
|
|
if(is_surface)
|
|
|
|
{
|
|
|
|
/* Ugly lua-OOCairo magic. */
|
|
|
|
cairo_surface_t **cairo_surface = (cairo_surface_t **)luaL_checkudata(L, idx, OOCAIRO_MT_NAME_SURFACE);
|
|
|
|
|
|
|
|
if(d->image)
|
|
|
|
cairo_surface_destroy(d->image);
|
|
|
|
|
|
|
|
d->image = draw_dup_image_surface(*cairo_surface);
|
|
|
|
} else {
|
|
|
|
luaA_checkudata(L, idx, &image_class);
|
|
|
|
image_t *image = (void *) lua_topointer(L, idx);
|
|
|
|
cairo_surface_t *surface = cairo_image_surface_create_for_data(
|
|
|
|
image_getdata(image), CAIRO_FORMAT_ARGB32,
|
|
|
|
image_getwidth(image), image_getheight(image),
|
|
|
|
image_getwidth(image) * 4);
|
|
|
|
/* Cairo doesn't copy the data we give to it so we have to make a
|
|
|
|
* copy of the data. This is the lazy way to do that. */
|
|
|
|
d->image = draw_dup_image_surface(surface);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
widget_invalidate_bywidget(widget);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-11 15:57:06 +02:00
|
|
|
/** Imagebox widget.
|
|
|
|
* \param L The Lua VM state.
|
2010-09-01 16:11:48 +02:00
|
|
|
* \param prop The key that is being indexed.
|
2008-11-04 15:31:47 +01:00
|
|
|
* \param resize Resize image.
|
2008-09-11 15:57:06 +02:00
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lfield image The image to display.
|
2008-10-21 18:32:48 +02:00
|
|
|
* \lfield bg The background color to use.
|
2008-09-11 15:57:06 +02:00
|
|
|
*/
|
|
|
|
static int
|
2010-09-01 16:11:48 +02:00
|
|
|
luaA_imagebox_index(lua_State *L, const char *prop)
|
2008-09-11 15:57:06 +02:00
|
|
|
{
|
2009-08-17 15:52:41 +02:00
|
|
|
widget_t *widget = luaA_checkudata(L, 1, &widget_class);
|
2009-04-09 15:12:17 +02:00
|
|
|
imagebox_data_t *d = widget->data;
|
2008-09-11 15:57:06 +02:00
|
|
|
|
2010-09-01 16:11:48 +02:00
|
|
|
if(a_strcmp(prop, "image") == 0)
|
2010-08-20 12:13:03 +02:00
|
|
|
oocairo_surface_push(L, d->image);
|
2010-09-01 16:11:48 +02:00
|
|
|
else if(a_strcmp(prop, "bg") == 0)
|
2009-04-17 18:59:50 +02:00
|
|
|
luaA_pushcolor(L, &d->bg);
|
2010-09-01 16:11:48 +02:00
|
|
|
else if(a_strcmp(prop, "resize") == 0)
|
2008-11-04 15:31:47 +01:00
|
|
|
lua_pushboolean(L, d->resize);
|
2010-09-01 16:11:48 +02:00
|
|
|
else
|
2008-11-04 15:29:43 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
2008-09-11 15:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** The __newindex method for a imagebox object.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param token The key token.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
static int
|
2010-09-01 16:11:48 +02:00
|
|
|
luaA_imagebox_newindex(lua_State *L, const char *prop)
|
2008-09-11 15:57:06 +02:00
|
|
|
{
|
2009-08-17 15:52:41 +02:00
|
|
|
widget_t *widget = luaA_checkudata(L, 1, &widget_class);
|
2009-04-09 15:12:17 +02:00
|
|
|
imagebox_data_t *d = widget->data;
|
2008-09-11 15:57:06 +02:00
|
|
|
|
2010-09-01 16:11:48 +02:00
|
|
|
if(a_strcmp(prop, "image") == 0)
|
2010-08-20 12:13:03 +02:00
|
|
|
imagebox_set_image(L, widget, 3);
|
2010-09-01 16:11:48 +02:00
|
|
|
else if(a_strcmp(prop, "bg") == 0)
|
|
|
|
{
|
|
|
|
const char *buf;
|
|
|
|
size_t len;
|
2008-10-20 15:56:11 +02:00
|
|
|
if(lua_isnil(L, 3))
|
|
|
|
p_clear(&d->bg, 1);
|
|
|
|
else if((buf = luaL_checklstring(L, 3, &len)))
|
2010-09-10 11:27:40 +02:00
|
|
|
color_init_unchecked(&d->bg, buf, len);
|
2010-09-01 16:11:48 +02:00
|
|
|
}
|
|
|
|
else if(a_strcmp(prop, "resize") == 0)
|
2008-11-04 15:31:47 +01:00
|
|
|
d->resize = luaA_checkboolean(L, 3);
|
2010-09-01 16:11:48 +02:00
|
|
|
else
|
2008-09-11 15:57:06 +02:00
|
|
|
return 0;
|
|
|
|
|
2009-04-09 15:12:17 +02:00
|
|
|
widget_invalidate_bywidget(widget);
|
2008-09-11 15:57:06 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Create a new imagebox widget.
|
2008-11-12 14:55:30 +01:00
|
|
|
* \param w The widget to initialize.
|
2008-09-11 15:57:06 +02:00
|
|
|
* \return A brand new widget.
|
|
|
|
*/
|
|
|
|
widget_t *
|
2008-11-12 14:55:30 +01:00
|
|
|
widget_imagebox(widget_t *w)
|
2008-09-11 15:57:06 +02:00
|
|
|
{
|
2008-11-04 15:31:47 +01:00
|
|
|
imagebox_data_t *d;
|
2008-09-11 15:57:06 +02:00
|
|
|
w->draw = imagebox_draw;
|
|
|
|
w->index = luaA_imagebox_index;
|
|
|
|
w->newindex = luaA_imagebox_newindex;
|
|
|
|
w->destructor = imagebox_destructor;
|
2009-05-05 19:55:29 +02:00
|
|
|
w->extents = imagebox_extents;
|
2008-11-04 15:31:47 +01:00
|
|
|
w->data = d = p_new(imagebox_data_t, 1);
|
|
|
|
d->resize = true;
|
2008-09-11 15:57:06 +02:00
|
|
|
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|