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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "widget.h"
|
2009-08-17 16:38:56 +02:00
|
|
|
#include "luaa.h"
|
2008-09-11 15:57:06 +02:00
|
|
|
#include "titlebar.h"
|
|
|
|
|
|
|
|
/** The imagebox private data structure */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
/** Imagebox image */
|
|
|
|
image_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)
|
|
|
|
{
|
2009-02-10 17:30:57 +01:00
|
|
|
geometry.width = image_getwidth(d->image);
|
|
|
|
geometry.height = image_getheight(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
|
|
|
|
2009-02-10 17:30:57 +01:00
|
|
|
double ratio = d->resize ? (double) geometry.height / image_getheight(d->image) : 1;
|
|
|
|
draw_image(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;
|
|
|
|
p_delete(&d);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Imagebox widget.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param token The key token.
|
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
|
|
|
|
luaA_imagebox_index(lua_State *L, awesome_token_t token)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
switch(token)
|
|
|
|
{
|
|
|
|
case A_TK_IMAGE:
|
2009-07-28 10:29:30 +02:00
|
|
|
luaA_object_push_item(L, 1, d->image);
|
2009-04-09 11:38:56 +02:00
|
|
|
break;
|
2008-10-20 15:56:11 +02:00
|
|
|
case A_TK_BG:
|
2009-04-17 18:59:50 +02:00
|
|
|
luaA_pushcolor(L, &d->bg);
|
2008-10-20 15:56:11 +02:00
|
|
|
break;
|
2008-11-04 15:31:47 +01:00
|
|
|
case A_TK_RESIZE:
|
|
|
|
lua_pushboolean(L, d->resize);
|
|
|
|
break;
|
2008-09-11 15:57:06 +02:00
|
|
|
default:
|
2008-11-04 15:29:43 +01:00
|
|
|
return 0;
|
2008-09-11 15:57:06 +02:00
|
|
|
}
|
2008-11-04 15:29:43 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
luaA_imagebox_newindex(lua_State *L, awesome_token_t token)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
switch(token)
|
|
|
|
{
|
2008-10-20 15:56:11 +02:00
|
|
|
const char *buf;
|
|
|
|
size_t len;
|
|
|
|
|
2008-09-11 15:57:06 +02:00
|
|
|
case A_TK_IMAGE:
|
2009-09-04 15:58:43 +02:00
|
|
|
luaA_checkudata(L, -1, &image_class);
|
2009-07-28 10:29:30 +02:00
|
|
|
luaA_object_unref_item(L, 1, d->image);
|
|
|
|
d->image = luaA_object_ref_item(L, 1, 3);
|
2008-09-11 15:57:06 +02:00
|
|
|
break;
|
2008-10-20 15:56:11 +02:00
|
|
|
case A_TK_BG:
|
|
|
|
if(lua_isnil(L, 3))
|
|
|
|
p_clear(&d->bg, 1);
|
|
|
|
else if((buf = luaL_checklstring(L, 3, &len)))
|
2009-04-17 18:59:50 +02:00
|
|
|
color_init_reply(color_init_unchecked(&d->bg, buf, len));
|
2008-10-20 15:56:11 +02:00
|
|
|
break;
|
2008-11-04 15:31:47 +01:00
|
|
|
case A_TK_RESIZE:
|
|
|
|
d->resize = luaA_checkboolean(L, 3);
|
|
|
|
break;
|
2008-09-11 15:57:06 +02:00
|
|
|
default:
|
|
|
|
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
|