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-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-07-10 15:06:09 +02:00
|
|
|
/** Textbox text length */
|
|
|
|
size_t len;
|
2008-06-25 17:47:51 +02:00
|
|
|
/** Textbox width */
|
2007-12-30 21:03:22 +01:00
|
|
|
int width;
|
2008-08-14 00:06:38 +02:00
|
|
|
/** Extents */
|
|
|
|
int extents;
|
2008-11-06 17:04:23 +01:00
|
|
|
PangoEllipsizeMode ellip;
|
|
|
|
PangoWrapMode wrap;
|
2008-08-14 00:06:38 +02:00
|
|
|
/** Draw parser data */
|
|
|
|
draw_parser_data_t pdata;
|
2008-06-14 09:18:51 +02:00
|
|
|
} textbox_data_t;
|
2007-12-17 10:57:17 +01:00
|
|
|
|
2008-10-22 17:32:17 +02:00
|
|
|
static area_t
|
|
|
|
textbox_geometry(widget_t *widget, int screen, int height, int width)
|
2007-12-15 07:53:53 +01:00
|
|
|
{
|
2008-10-22 17:32:17 +02:00
|
|
|
area_t geometry;
|
|
|
|
textbox_data_t *d = widget->data;
|
2007-12-22 15:41:28 +01:00
|
|
|
|
2008-10-22 17:32:17 +02:00
|
|
|
geometry.height = height;
|
2008-08-01 17:44:51 +02:00
|
|
|
|
2007-12-30 21:03:22 +01:00
|
|
|
if(d->width)
|
2008-10-22 17:32:17 +02:00
|
|
|
geometry.width = d->width;
|
|
|
|
else if(widget->align == AlignFlex)
|
|
|
|
geometry.width = width;
|
2007-12-30 21:03:22 +01:00
|
|
|
else
|
2008-06-23 13:09:42 +02:00
|
|
|
{
|
2008-10-22 17:32:17 +02:00
|
|
|
geometry.width = MIN(d->extents, width);
|
2008-07-09 12:12:52 +02:00
|
|
|
|
2008-08-14 00:06:38 +02:00
|
|
|
if(d->pdata.bg_image)
|
2008-10-22 17:32:17 +02:00
|
|
|
geometry.width = MAX(geometry.width,
|
|
|
|
d->pdata.bg_resize ? ((double) d->pdata.bg_image->width / (double) d->pdata.bg_image->height) * geometry.height : d->pdata.bg_image->width);
|
2008-06-23 13:09:42 +02:00
|
|
|
}
|
2007-12-30 14:49:03 +01:00
|
|
|
|
2008-10-22 17:32:17 +02:00
|
|
|
return geometry;
|
|
|
|
}
|
2007-12-27 11:22:57 +01:00
|
|
|
|
2008-10-22 17:32:17 +02:00
|
|
|
/** Draw a textbox widget.
|
|
|
|
* \param widget The widget.
|
|
|
|
* \param ctx The draw context.
|
|
|
|
* \param screen The screen.
|
|
|
|
* \param p A pointer to the object we're draw onto.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
textbox_draw(widget_t *widget, draw_context_t *ctx, area_t geometry,
|
|
|
|
int screen, wibox_t *p)
|
|
|
|
{
|
|
|
|
textbox_data_t *d = widget->data;
|
2008-11-06 17:04:23 +01:00
|
|
|
draw_text(ctx, globalconf.font, d->ellip, d->wrap, geometry, d->text, d->len, &d->pdata);
|
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;
|
2008-08-14 00:06:38 +02:00
|
|
|
draw_parser_data_wipe(&d->pdata);
|
2008-06-14 22:55:17 +02:00
|
|
|
p_delete(&d->text);
|
|
|
|
p_delete(&d);
|
|
|
|
}
|
|
|
|
|
2008-07-28 16:03:38 +02:00
|
|
|
/** Textbox widget.
|
2008-06-25 17:47:51 +02:00
|
|
|
* \param L The Lua VM state.
|
2008-07-01 15:27:41 +02:00
|
|
|
* \param token The key token.
|
2008-06-25 17:47:51 +02:00
|
|
|
* \return The number of elements pushed on stack.
|
2008-07-02 17:59:06 +02:00
|
|
|
* \luastack
|
|
|
|
* \lfield text The text to display.
|
|
|
|
* \lfield width The width of the textbox. Set to 0 for auto.
|
2008-11-07 16:01:20 +01:00
|
|
|
* \lfield wrap The wrap mode: word, char, word_char.
|
|
|
|
* \lfield ellipsize The ellipsize mode: start, middle or end.
|
2008-06-25 17:47:51 +02:00
|
|
|
*/
|
|
|
|
static int
|
2008-07-01 15:27:41 +02:00
|
|
|
luaA_textbox_index(lua_State *L, awesome_token_t token)
|
2008-06-25 17:47:51 +02:00
|
|
|
{
|
2008-06-27 12:49:24 +02:00
|
|
|
widget_t **widget = luaA_checkudata(L, 1, "widget");
|
|
|
|
textbox_data_t *d = (*widget)->data;
|
2008-06-25 17:47:51 +02:00
|
|
|
|
2008-07-01 15:27:41 +02:00
|
|
|
switch(token)
|
2008-06-25 17:47:51 +02:00
|
|
|
{
|
2008-06-27 12:49:24 +02:00
|
|
|
case A_TK_TEXT:
|
|
|
|
lua_pushstring(L, d->text);
|
|
|
|
return 1;
|
2008-06-27 20:24:30 +02:00
|
|
|
case A_TK_WIDTH:
|
|
|
|
lua_pushnumber(L, d->width);
|
|
|
|
return 1;
|
2008-11-06 17:04:23 +01:00
|
|
|
case A_TK_WRAP:
|
|
|
|
switch(d->wrap)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
lua_pushliteral(L, "word");
|
|
|
|
break;
|
|
|
|
case A_TK_CHAR:
|
|
|
|
lua_pushliteral(L, "char");
|
|
|
|
break;
|
|
|
|
case A_TK_WORD_CHAR:
|
|
|
|
lua_pushliteral(L, "word_char");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
case A_TK_ELLIPSIZE:
|
|
|
|
switch(d->ellip)
|
|
|
|
{
|
|
|
|
case A_TK_START:
|
|
|
|
lua_pushliteral(L, "start");
|
|
|
|
break;
|
|
|
|
case A_TK_MIDDLE:
|
|
|
|
lua_pushliteral(L, "middle");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lua_pushliteral(L, "end");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
2008-06-25 17:47:51 +02:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-27 12:49:24 +02:00
|
|
|
/** The __newindex method for a textbox object.
|
|
|
|
* \param L The Lua VM state.
|
2008-07-01 15:27:41 +02:00
|
|
|
* \param token The key token.
|
2008-06-27 12:49:24 +02:00
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
static int
|
2008-07-01 15:27:41 +02:00
|
|
|
luaA_textbox_newindex(lua_State *L, awesome_token_t token)
|
2008-06-27 12:49:24 +02:00
|
|
|
{
|
2008-08-12 18:13:23 +02:00
|
|
|
size_t len = 0;
|
2008-06-27 12:49:24 +02:00
|
|
|
widget_t **widget = luaA_checkudata(L, 1, "widget");
|
2008-08-12 18:13:23 +02:00
|
|
|
const char *buf = NULL;
|
2008-06-27 12:49:24 +02:00
|
|
|
textbox_data_t *d = (*widget)->data;
|
|
|
|
|
2008-07-01 15:27:41 +02:00
|
|
|
switch(token)
|
2008-06-27 12:49:24 +02:00
|
|
|
{
|
|
|
|
case A_TK_TEXT:
|
2008-08-12 18:13:23 +02:00
|
|
|
if(lua_isnil(L, 3)
|
|
|
|
|| (buf = luaL_checklstring(L, 3, &len)))
|
2008-06-27 12:49:24 +02:00
|
|
|
{
|
2008-08-14 00:06:38 +02:00
|
|
|
/* delete */
|
|
|
|
draw_parser_data_wipe(&d->pdata);
|
2008-09-02 14:03:11 +02:00
|
|
|
/* reinit since we are giving it as arg to draw_text unconditionally */
|
|
|
|
draw_parser_data_init(&d->pdata);
|
2008-06-27 12:49:24 +02:00
|
|
|
p_delete(&d->text);
|
2008-08-14 00:06:38 +02:00
|
|
|
|
|
|
|
/* re-init */
|
|
|
|
d->len = len;
|
2008-08-12 18:13:23 +02:00
|
|
|
if(buf)
|
2008-08-14 00:06:38 +02:00
|
|
|
{
|
2008-08-12 18:13:23 +02:00
|
|
|
a_iso2utf8(&d->text, buf, len);
|
2008-11-04 17:50:10 +01:00
|
|
|
d->extents = draw_text_extents(globalconf.font, d->text, d->len, &d->pdata).width;
|
2008-08-14 00:06:38 +02:00
|
|
|
}
|
2008-08-15 01:11:39 +02:00
|
|
|
else
|
|
|
|
d->extents = 0;
|
2008-06-27 12:49:24 +02:00
|
|
|
}
|
|
|
|
break;
|
2008-06-27 20:24:30 +02:00
|
|
|
case A_TK_WIDTH:
|
|
|
|
d->width = luaL_checknumber(L, 3);
|
|
|
|
break;
|
2008-11-06 17:04:23 +01:00
|
|
|
case A_TK_WRAP:
|
|
|
|
if((buf = luaL_checklstring(L, 3, &len)))
|
|
|
|
switch(a_tokenize(buf, len))
|
|
|
|
{
|
|
|
|
case A_TK_WORD:
|
|
|
|
d->wrap = PANGO_WRAP_WORD;
|
|
|
|
break;
|
|
|
|
case A_TK_CHAR:
|
|
|
|
d->wrap = PANGO_WRAP_CHAR;
|
|
|
|
break;
|
|
|
|
case A_TK_WORD_CHAR:
|
|
|
|
d->wrap = PANGO_WRAP_WORD_CHAR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case A_TK_ELLIPSIZE:
|
|
|
|
if((buf = luaL_checklstring(L, 3, &len)))
|
|
|
|
switch(a_tokenize(buf, len))
|
|
|
|
{
|
|
|
|
case A_TK_START:
|
|
|
|
d->ellip = PANGO_ELLIPSIZE_START;
|
|
|
|
break;
|
|
|
|
case A_TK_MIDDLE:
|
|
|
|
d->ellip = PANGO_ELLIPSIZE_MIDDLE;
|
|
|
|
break;
|
|
|
|
case A_TK_END:
|
|
|
|
d->ellip = PANGO_ELLIPSIZE_END;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2008-06-27 12:49:24 +02:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-27 20:24:30 +02:00
|
|
|
widget_invalidate_bywidget(*widget);
|
|
|
|
|
2008-06-27 12:49:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-25 17:47:51 +02:00
|
|
|
/** 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
|
|
|
w->align = align;
|
2008-10-23 17:28:07 +02:00
|
|
|
w->align_supported |= AlignFlex;
|
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-27 12:49:24 +02:00
|
|
|
w->newindex = luaA_textbox_newindex;
|
2008-06-14 22:55:17 +02:00
|
|
|
w->destructor = textbox_destructor;
|
2008-10-22 17:32:17 +02:00
|
|
|
w->geometry = textbox_geometry;
|
2008-06-14 09:18:51 +02:00
|
|
|
w->data = d = p_new(textbox_data_t, 1);
|
2008-11-06 17:04:23 +01:00
|
|
|
d->ellip = PANGO_ELLIPSIZE_END;
|
2007-12-17 10:57:17 +01:00
|
|
|
|
2007-12-15 07:53:53 +01:00
|
|
|
return w;
|
|
|
|
}
|
2008-06-26 23:32:33 +02:00
|
|
|
|
2007-12-18 09:24:15 +01:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|