Repository navigation
Expand file tree
/
Copy pathimagebox.c
More file actions
178 lines (157 loc) · 4.4 KB
/
Copy pathimagebox.c
File metadata and controls
178 lines (157 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* imagebox.c - imagebox widget
*
* Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
*
* 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 "luaa.h"
#include "titlebar.h"
/** The imagebox private data structure */
typedef struct
{
/** Imagebox image */
image_t *image;
color_t bg;
bool resize;
} imagebox_data_t;
static area_t
imagebox_extents(lua_State *L, widget_t *widget)
{
area_t geometry = { .x = 0, .y = 0 };
imagebox_data_t *d = widget->data;
if(d->image)
{
geometry.width = image_getwidth(d->image);
geometry.height = image_getheight(d->image);
}
else
{
geometry.width = 0;
geometry.height = 0;
}
return geometry;
}
/** Draw an image.
* \param widget The widget.
* \param ctx The draw context.
* \param geometry The geometry we draw in.
* \param p A pointer to the object we're draw onto.
*/
static void
imagebox_draw(widget_t *widget, draw_context_t *ctx, area_t geometry, wibox_t *p)
{
imagebox_data_t *d = widget->data;
if(d->image && geometry.width && geometry.height)
{
if(d->bg.initialized)
draw_rectangle(ctx, geometry, 1.0, true, &d->bg);
double ratio = d->resize ? (double) geometry.height / image_getheight(d->image) : 1;
draw_image(ctx, geometry.x, geometry.y, ratio, d->image);
}
}
/** 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.
* \param resize Resize image.
* \return The number of elements pushed on stack.
* \luastack
* \lfield image The image to display.
* \lfield bg The background color to use.
*/
static int
luaA_imagebox_index(lua_State *L, awesome_token_t token)
{
widget_t *widget = luaA_checkudata(L, 1, &widget_class);
imagebox_data_t *d = widget->data;
switch(token)
{
case A_TK_IMAGE:
luaA_object_push_item(L, 1, d->image);
break;
case A_TK_BG:
luaA_pushcolor(L, &d->bg);
break;
case A_TK_RESIZE:
lua_pushboolean(L, d->resize);
break;
default:
return 0;
}
return 1;
}
/** 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_class);
imagebox_data_t *d = widget->data;
switch(token)
{
const char *buf;
size_t len;
case A_TK_IMAGE:
luaA_checkudataornil(L, -1, &image_class);
luaA_object_unref_item(L, 1, d->image);
d->image = luaA_object_ref_item(L, 1, 3);
break;
case A_TK_BG:
if(lua_isnil(L, 3))
p_clear(&d->bg, 1);
else if((buf = luaL_checklstring(L, 3, &len)))
color_init_reply(color_init_unchecked(&d->bg, buf, len));
break;
case A_TK_RESIZE:
d->resize = luaA_checkboolean(L, 3);
break;
default:
return 0;
}
widget_invalidate_bywidget(widget);
return 0;
}
/** Create a new imagebox widget.
* \param w The widget to initialize.
* \return A brand new widget.
*/
widget_t *
widget_imagebox(widget_t *w)
{
imagebox_data_t *d;
w->draw = imagebox_draw;
w->index = luaA_imagebox_index;
w->newindex = luaA_imagebox_newindex;
w->destructor = imagebox_destructor;
w->extents = imagebox_extents;
w->data = d = p_new(imagebox_data_t, 1);
d->resize = true;
return w;
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80