2007-12-22 15:41:28 +01:00
|
|
|
/*
|
|
|
|
* textbox.c - text box widget
|
|
|
|
*
|
2008-05-20 15:39:47 +02:00
|
|
|
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
|
2007-12-22 15:41:28 +01: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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-12-15 07:53:53 +01:00
|
|
|
#include "widget.h"
|
2008-01-01 18:02:36 +01:00
|
|
|
#include "screen.h"
|
2008-01-21 18:14:59 +01:00
|
|
|
#include "common/util.h"
|
2008-03-19 12:05:36 +01:00
|
|
|
#include "common/configopts.h"
|
2008-06-23 13:24:03 +02:00
|
|
|
#include "common/tokenize.h"
|
2007-12-15 07:53:53 +01:00
|
|
|
|
2008-05-24 08:59:27 +02:00
|
|
|
extern awesome_t globalconf;
|
2007-12-16 02:45:38 +01:00
|
|
|
|
2008-06-25 17:47:51 +02:00
|
|
|
/** The textbox private data structure */
|
2007-12-22 15:41:28 +01:00
|
|
|
typedef struct
|
2007-12-17 10:57:17 +01:00
|
|
|
{
|
2008-06-25 17:47:51 +02:00
|
|
|
/** Textbox text */
|
2007-12-17 10:57:17 +01:00
|
|
|
char *text;
|
2008-06-25 17:47:51 +02:00
|
|
|
/** Textbox width */
|
2007-12-30 21:03:22 +01:00
|
|
|
int width;
|
2008-06-14 09:18:51 +02:00
|
|
|
} textbox_data_t;
|
2007-12-17 10:57:17 +01:00
|
|
|
|
2008-06-25 17:47:51 +02:00
|
|
|
/** Draw a textbox 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.
|
|
|
|
* \return The width used.
|
|
|
|
*/
|
2007-12-15 07:53:53 +01:00
|
|
|
static int
|
2008-06-02 12:18:17 +02:00
|
|
|
textbox_draw(draw_context_t *ctx, int screen __attribute__ ((unused)),
|
|
|
|
widget_node_t *w,
|
2008-06-03 16:08:33 +02:00
|
|
|
int offset, int used,
|
2008-06-02 12:18:17 +02:00
|
|
|
void *p __attribute__ ((unused)))
|
2007-12-15 07:53:53 +01:00
|
|
|
{
|
2008-06-14 09:18:51 +02:00
|
|
|
textbox_data_t *d = w->widget->data;
|
2008-06-23 13:09:42 +02:00
|
|
|
draw_parser_data_t pdata, *pdata_arg = NULL;
|
2007-12-22 15:41:28 +01:00
|
|
|
|
2007-12-30 21:03:22 +01:00
|
|
|
if(d->width)
|
2008-05-20 15:39:47 +02:00
|
|
|
w->area.width = d->width;
|
|
|
|
else if(w->widget->align == AlignFlex)
|
2008-06-03 16:08:33 +02:00
|
|
|
w->area.width = ctx->width - used;
|
2007-12-30 21:03:22 +01:00
|
|
|
else
|
2008-06-23 13:09:42 +02:00
|
|
|
{
|
2008-06-23 15:21:29 +02:00
|
|
|
draw_parser_data_init(&pdata);
|
2008-06-02 12:18:17 +02:00
|
|
|
w->area.width = MIN(draw_text_extents(ctx->connection,
|
|
|
|
ctx->phys_screen,
|
2008-06-23 13:09:42 +02:00
|
|
|
globalconf.font, d->text, &pdata).width,
|
2008-06-03 16:08:33 +02:00
|
|
|
ctx->width - used);
|
2008-06-23 13:09:42 +02:00
|
|
|
pdata_arg = &pdata;
|
|
|
|
}
|
2007-12-30 14:49:03 +01:00
|
|
|
|
2008-06-03 16:08:33 +02:00
|
|
|
w->area.height = ctx->height;
|
2008-01-05 12:44:14 +01:00
|
|
|
|
2008-06-03 16:08:33 +02:00
|
|
|
w->area.x = widget_calculate_offset(ctx->width,
|
2008-05-20 15:39:47 +02:00
|
|
|
w->area.width,
|
|
|
|
offset,
|
|
|
|
w->widget->align);
|
|
|
|
w->area.y = 0;
|
2007-12-22 15:41:28 +01:00
|
|
|
|
2008-06-23 13:09:42 +02:00
|
|
|
draw_text(ctx, globalconf.font, w->area, d->text, pdata_arg);
|
2008-06-23 15:21:29 +02:00
|
|
|
if (pdata_arg)
|
|
|
|
draw_parser_data_wipe(pdata_arg);
|
2007-12-27 11:22:57 +01:00
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
return w->area.width;
|
2007-12-15 07:53:53 +01:00
|
|
|
}
|
|
|
|
|
2008-06-25 17:47:51 +02:00
|
|
|
/** Delete a textbox widget.
|
|
|
|
* \param w The widget to destroy.
|
|
|
|
*/
|
2008-06-14 22:55:17 +02:00
|
|
|
static void
|
|
|
|
textbox_destructor(widget_t *w)
|
|
|
|
{
|
|
|
|
textbox_data_t *d = w->data;
|
|
|
|
p_delete(&d->text);
|
|
|
|
p_delete(&d);
|
|
|
|
}
|
|
|
|
|
2008-06-25 17:47:51 +02:00
|
|
|
/** Set the text of a textbox.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \lstack
|
|
|
|
* \lvalue A widget.
|
|
|
|
* \lparam The text to set.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_textbox_text_set(lua_State *L)
|
|
|
|
{
|
|
|
|
widget_t **widget = luaA_checkudata(L, 1, "widget");
|
|
|
|
const char *text = luaL_checkstring(L, 2);
|
|
|
|
textbox_data_t *d = (*widget)->data;
|
|
|
|
|
|
|
|
p_delete(&d->text);
|
|
|
|
a_iso2utf8(text, &d->text);
|
|
|
|
|
|
|
|
widget_invalidate_bywidget(*widget);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Set the width of a textbox.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \lstack
|
|
|
|
* \lvalue A widget.
|
|
|
|
* \lparam The width to set.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_textbox_width_set(lua_State *L)
|
|
|
|
{
|
|
|
|
widget_t **widget = luaA_checkudata(L, 1, "widget");
|
|
|
|
int width = luaL_checknumber(L, 2);
|
|
|
|
textbox_data_t *d = (*widget)->data;
|
|
|
|
|
|
|
|
d->width = width;
|
|
|
|
|
|
|
|
widget_invalidate_bywidget(*widget);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** The __index method for a textbox object.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \param The numbre of elements return on the stack.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_textbox_index(lua_State *L)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
const char *attr = luaL_checklstring(L, 2, &len);
|
|
|
|
|
|
|
|
switch(a_tokenize(attr, len))
|
|
|
|
{
|
|
|
|
case A_TK_TEXT_SET:
|
|
|
|
lua_pushcfunction(L, luaA_textbox_text_set);
|
|
|
|
return 1;
|
|
|
|
case A_TK_WIDTH_SET:
|
|
|
|
lua_pushcfunction(L, luaA_textbox_width_set);
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Create a new textbox widget.
|
|
|
|
* \param align Widget alignment.
|
|
|
|
* \return A brand new widget.
|
|
|
|
*/
|
2008-04-11 11:26:37 +02:00
|
|
|
widget_t *
|
2008-05-20 15:39:47 +02:00
|
|
|
textbox_new(alignment_t align)
|
2007-12-15 07:53:53 +01:00
|
|
|
{
|
2008-04-11 11:26:37 +02:00
|
|
|
widget_t *w;
|
2008-06-14 09:18:51 +02:00
|
|
|
textbox_data_t *d;
|
2007-12-17 10:57:17 +01:00
|
|
|
|
2008-04-11 11:26:37 +02:00
|
|
|
w = p_new(widget_t, 1);
|
2008-05-20 15:39:47 +02:00
|
|
|
widget_common_new(w);
|
|
|
|
w->align = align;
|
2007-12-15 16:25:54 +01:00
|
|
|
w->draw = textbox_draw;
|
2008-06-25 17:47:51 +02:00
|
|
|
w->index = luaA_textbox_index;
|
2008-06-14 22:55:17 +02:00
|
|
|
w->destructor = textbox_destructor;
|
2008-06-14 09:18:51 +02:00
|
|
|
w->data = d = p_new(textbox_data_t, 1);
|
2007-12-17 10:57:17 +01:00
|
|
|
|
2007-12-15 07:53:53 +01:00
|
|
|
return w;
|
|
|
|
}
|
2007-12-18 09:24:15 +01:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|