From 71ac6710e5a70e95fbb7e71202ba6b48d8a863e5 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Thu, 11 Sep 2008 15:57:06 +0200 Subject: [PATCH] widgets: replace appicon by imagebox Signed-off-by: Julien Danjou --- CMakeLists.txt | 2 +- awesomerc.lua.in | 11 ++- lib/awful.lua.in | 7 +- widget.h | 2 +- widgets/appicon.c | 94 -------------------------- widgets/imagebox.c | 165 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 177 insertions(+), 104 deletions(-) delete mode 100644 widgets/appicon.c create mode 100644 widgets/imagebox.c diff --git a/CMakeLists.txt b/CMakeLists.txt index ec660502..1d4e6785 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,7 +83,7 @@ set(AWE_SRCS ${SOURCE_DIR}/widgets/tasklist.c ${SOURCE_DIR}/widgets/textbox.c ${SOURCE_DIR}/widgets/systray.c - ${SOURCE_DIR}/widgets/appicon.c) + ${SOURCE_DIR}/widgets/imagebox.c) set(AWE_CLIENT_SRCS ${SOURCE_DIR}/awesome-client.c diff --git a/awesomerc.lua.in b/awesomerc.lua.in index 57514657..5330f55b 100644 --- a/awesomerc.lua.in +++ b/awesomerc.lua.in @@ -131,14 +131,14 @@ mysystray = widget({ type = "systray", name = "mysystray", align = "right" }) -- We need one layoutbox per screen. mylayoutbox = {} for s = 1, screen.count() do - mylayoutbox[s] = widget({ type = "textbox", name = "mylayoutbox", align = "right" }) + mylayoutbox[s] = widget({ type = "imagebox", name = "mylayoutbox", align = "right" }) mylayoutbox[s]:buttons({ button({ }, 1, function () awful.layout.inc(layouts, 1) end), button({ }, 3, function () awful.layout.inc(layouts, -1) end), button({ }, 4, function () awful.layout.inc(layouts, 1) end), button({ }, 5, function () awful.layout.inc(layouts, -1) end) }) - mylayoutbox[s].text = "" + mylayoutbox[s].image = image("@AWESOME_ICON_PATH@/layouts/tilew.png") end -- Create a statusbar for each screen and add it @@ -417,10 +417,9 @@ end) awful.hooks.arrange.register(function (screen) local layout = awful.layout.get(screen) if layout then - mylayoutbox[screen].text = - "" - else - mylayoutbox[screen].text = "No layout." + mylayoutbox[screen].image = image("@AWESOME_ICON_PATH@/layouts/" .. layout .. "w.png") + else + mylayoutbox[screen].image = nil end -- Give focus to the latest client in history if no window has focus diff --git a/lib/awful.lua.in b/lib/awful.lua.in index 2bcff936..a2760479 100644 --- a/lib/awful.lua.in +++ b/lib/awful.lua.in @@ -1522,6 +1522,9 @@ function titlebar.add(c, args) } title:buttons(bts) + local appicon = capi.widget({ type = "imagebox", name = "appicon", align = "left" }) + appicon.image = c.icon + if theme.titlebar_close_button == "true" then local closef = widget.button({ name = "closef", align = "right", image = theme.titlebar_close_button_focus @@ -1543,13 +1546,13 @@ function titlebar.add(c, args) close:buttons(bts) tb:widgets({ - capi.widget({ type = "appicon", name = "appicon", align = "left" }), + appicon, title, closef, close }) else tb:widgets({ - capi.widget({ type = "appicon", name = "appicon", align = "left" }), + appicon, title }) end diff --git a/widget.h b/widget.h index 83127bab..f521e7e4 100644 --- a/widget.h +++ b/widget.h @@ -45,7 +45,7 @@ widget_constructor_t progressbar_new; widget_constructor_t graph_new; widget_constructor_t tasklist_new; widget_constructor_t systray_new; -widget_constructor_t appicon_new; +widget_constructor_t imagebox_new; #endif diff --git a/widgets/appicon.c b/widgets/appicon.c deleted file mode 100644 index 392a1627..00000000 --- a/widgets/appicon.c +++ /dev/null @@ -1,94 +0,0 @@ -/* - * appicon.c - application icon widget - * - * Copyright © 2008 Julien Danjou - * - * 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" -#include "ewmh.h" -#include "titlebar.h" - -extern awesome_t globalconf; - -/** Draw a application icon widget. - * \param ctx The draw context. - * \param screen The screen. - * \param w The widget node we are linked from. - * \param offset Offset to draw at. - * \param used The size used on the element. - * \param p A pointer to the object we're draw onto. - * \param type The type of the object. - * \return The width used. - */ -static int -appicon_draw(draw_context_t *ctx, int screen __attribute__ ((unused)), - widget_node_t *w, - int offset, int used, - void *p, awesome_type_t type) -{ - client_t *c = NULL; - draw_image_t *image; - - switch(type) - { - case AWESOME_TYPE_STATUSBAR: - c = globalconf.screen_focus->client_focus; - break; - case AWESOME_TYPE_TITLEBAR: - c = client_getbytitlebar(p); - break; - } - - if(c) - { - if(c->icon) - { - image = c->icon->image; - w->area.width = ((double) ctx->height / (double) image->height) * image->width; - w->area.x = widget_calculate_offset(ctx->width, - w->area.width, - offset, - w->widget->align); - draw_image(ctx, w->area.x, - w->area.y, ctx->height, image); - } - } - else - w->area.width = 0; - - return w->area.width; -} - -/** Create a new appicon widget. - * \param align Widget alignment. - * \return A brand new widget. - */ -widget_t * -appicon_new(alignment_t align) -{ - widget_t *w; - - w = p_new(widget_t, 1); - widget_common_new(w); - w->align = align; - w->draw = appicon_draw; - - return w; -} - -// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/widgets/imagebox.c b/widgets/imagebox.c new file mode 100644 index 00000000..7d6fb2ea --- /dev/null +++ b/widgets/imagebox.c @@ -0,0 +1,165 @@ +/* + * imagebox.c - imagebox widget + * + * Copyright © 2008 Julien Danjou + * + * 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" +#include "titlebar.h" + +extern awesome_t globalconf; + +/** The imagebox private data structure */ +typedef struct +{ + /** Imagebox image */ + image_t *image; +} imagebox_data_t; + +/** Draw an image. + * \param ctx The draw context. + * \param screen The screen. + * \param w The widget node we are linked from. + * \param offset Offset to draw at. + * \param used The size used on the element. + * \param p A pointer to the object we're draw onto. + * \param type The type of the object. + * \return The width used. + */ +static int +imagebox_draw(draw_context_t *ctx, int screen __attribute__ ((unused)), + widget_node_t *w, + int offset, int used, + void *p, awesome_type_t type) +{ + imagebox_data_t *d = w->widget->data; + + w->area.y = 0; + + if(d->image) + { + w->area.height = ctx->height; + w->area.width = ((double) ctx->height / (double) d->image->image->height) * d->image->image->width; + w->area.x = widget_calculate_offset(ctx->width, + w->area.width, + offset, + w->widget->align); + + draw_image(ctx, w->area.x, w->area.y, ctx->height, d->image->image); + } + else + { + w->area.width = 0; + w->area.height = 0; + } + + return w->area.width; +} + +/** Delete a imagebox widget. + * \param w The widget to destroy. + */ +static void +imagebox_destructor(widget_t *w) +{ + imagebox_data_t *d = w->data; + image_unref(&d->image); + p_delete(&d); +} + +/** Imagebox widget. + * \param L The Lua VM state. + * \param token The key token. + * \return The number of elements pushed on stack. + * \luastack + * \lfield image The image to display. + * \lfield width The width of the imagebox. Set to 0 for auto. + */ +static int +luaA_imagebox_index(lua_State *L, awesome_token_t token) +{ + widget_t **widget = luaA_checkudata(L, 1, "widget"); + imagebox_data_t *d = (*widget)->data; + + switch(token) + { + case A_TK_IMAGE: + if(d->image) + return luaA_image_userdata_new(L, d->image); + default: + break; + } + return 0; +} + +/** 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) +{ + widget_t **widget = luaA_checkudata(L, 1, "widget"); + image_t **image = NULL; + imagebox_data_t *d = (*widget)->data; + + switch(token) + { + case A_TK_IMAGE: + if(lua_isnil(L, 3) + || (image = luaA_checkudata(L, 3, "image"))) + { + /* unref image */ + image_unref(&d->image); + if(image) + d->image = image_ref(image); + else + d->image = NULL; + } + break; + default: + return 0; + } + + widget_invalidate_bywidget(*widget); + + return 0; +} + + +/** Create a new imagebox widget. + * \param align Widget alignment. + * \return A brand new widget. + */ +widget_t * +imagebox_new(alignment_t align) +{ + widget_t *w = p_new(widget_t, 1); + widget_common_new(w); + w->align = align; + w->draw = imagebox_draw; + w->index = luaA_imagebox_index; + w->newindex = luaA_imagebox_newindex; + w->destructor = imagebox_destructor; + w->data = p_new(imagebox_data_t, 1); + + return w; +} + +// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80