[markup] Add generic substitution of elements and use it for titlebar

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-04-28 11:37:53 +02:00
parent a4159bbaf7
commit 7a8e39b3e7
5 changed files with 31 additions and 36 deletions

View File

@ -197,11 +197,11 @@ cfg_opt_t titlebar_opts[] =
/** Text alignment. */ /** Text alignment. */
CFG_ALIGNMENT((char *) "text_align", (char *) "center", CFGF_NONE), CFG_ALIGNMENT((char *) "text_align", (char *) "center", CFGF_NONE),
/** Titlebar markup string for normal windows. */ /** Titlebar markup string for normal windows. */
CFG_STR((char *) "text_normal", (char *) "<bg color=\"#444444\"/>%t", CFGF_NONE), CFG_STR((char *) "text_normal", (char *) "<bg color=\"#444444\"/><title/>", CFGF_NONE),
/** Titlebar markup string for focused windows. */ /** Titlebar markup string for focused windows. */
CFG_STR((char *) "text_focus", (char *) "<bg color=\"#535d6c\"/>%t", CFGF_NONE), CFG_STR((char *) "text_focus", (char *) "<bg color=\"#535d6c\"/><title/>", CFGF_NONE),
/** Titlebar markup string for urgent windows. */ /** Titlebar markup string for urgent windows. */
CFG_STR((char *) "text_urgent", (char *) "<bg color=\"#ff4500\"/>%t", CFGF_NONE), CFG_STR((char *) "text_urgent", (char *) "<bg color=\"#ff4500\"/><title/>", CFGF_NONE),
CFG_AWESOME_END() CFG_AWESOME_END()
}; };
/** This section defines general options. */ /** This section defines general options. */

View File

@ -228,19 +228,19 @@ static bool
draw_text_markup_expand(draw_parser_data_t *data, draw_text_markup_expand(draw_parser_data_t *data,
const char *str, ssize_t slen) const char *str, ssize_t slen)
{ {
const char *elements[] = { "bg", NULL}; const char *elements[] = { "bg", NULL };
markup_parser_data_t *p; markup_parser_data_t *p;
int i; int i;
p = markup_parser_data_new(elements, countof(elements)); p = markup_parser_data_new(elements, NULL, countof(elements));
if(!markup_parse(p, str, slen)) if(!markup_parse(p, str, slen))
return false; return false;
/* bg */ /* bg */
if(p->attribute_values[0]) if(p->attribute_names[0])
for(i = 0; p->attribute_values[0][i]; i++) for(i = 0; p->attribute_names[0][i]; i++)
if(!strcmp(p->attribute_values[0][i], "color")) if(!a_strcmp(p->attribute_names[0][i], "color"))
data->has_bg_color = draw_color_new(data->connection, data->phys_screen, data->has_bg_color = draw_color_new(data->connection, data->phys_screen,
p->attribute_values[0][i], &data->bg_color); p->attribute_values[0][i], &data->bg_color);

View File

@ -64,6 +64,12 @@ markup_parse_start_element(GMarkupParseContext *context __attribute__ ((unused))
p->attribute_names[i][j] = a_strdup(attribute_names[j]); p->attribute_names[i][j] = a_strdup(attribute_names[j]);
p->attribute_values[i][j] = a_strdup(attribute_values[j]); p->attribute_values[i][j] = a_strdup(attribute_values[j]);
} }
if(p->elements_sub && p->elements_sub[i])
{
asprintf(&newtext, "%s%s", NONULL(p->text), p->elements_sub[i]);
p_delete(&p->text);
p->text = newtext;
}
return; return;
} }
@ -150,18 +156,21 @@ markup_parse_text(GMarkupParseContext *context __attribute__ ((unused)),
/** Create a markup_parser_data_t structure with elements list. /** Create a markup_parser_data_t structure with elements list.
* \param elements an array of elements to look for, NULL terminated * \param elements an array of elements to look for, NULL terminated
* \param number of elements in the array (without NULL) * \param elements_sub an optionnal array of values to substitude to elements, NULL
* terminated, or NULL if not needed
* \param nelem number of elements in the array (without NULL)
* \return a pointer to an allocated markup_parser_data_t which must be freed * \return a pointer to an allocated markup_parser_data_t which must be freed
* with markup_parser_data_delete() * with markup_parser_data_delete()
*/ */
markup_parser_data_t * markup_parser_data_t *
markup_parser_data_new(const char **elements, ssize_t nelem) markup_parser_data_new(const char **elements, const char **elements_sub, ssize_t nelem)
{ {
markup_parser_data_t *p; markup_parser_data_t *p;
p = p_new(markup_parser_data_t, 1); p = p_new(markup_parser_data_t, 1);
p->elements = elements; p->elements = elements;
p->elements_sub = elements_sub;
p->attribute_names = p_new(char **, nelem); p->attribute_names = p_new(char **, nelem);
p->attribute_values = p_new(char **, nelem); p->attribute_values = p_new(char **, nelem);

View File

@ -28,11 +28,12 @@ typedef struct
{ {
char *text; char *text;
const char **elements; const char **elements;
const char **elements_sub;
char ***attribute_names; char ***attribute_names;
char ***attribute_values; char ***attribute_values;
} markup_parser_data_t; } markup_parser_data_t;
markup_parser_data_t * markup_parser_data_new(const char **, ssize_t); markup_parser_data_t * markup_parser_data_new(const char **, const char **, ssize_t);
void markup_parser_data_delete(markup_parser_data_t **); void markup_parser_data_delete(markup_parser_data_t **);
bool markup_parse(markup_parser_data_t *data, const char *, ssize_t); bool markup_parse(markup_parser_data_t *data, const char *, ssize_t);

View File

@ -27,38 +27,23 @@
#include "titlebar.h" #include "titlebar.h"
#include "screen.h" #include "screen.h"
#include "layouts/floating.h" #include "layouts/floating.h"
#include "common/markup.h"
extern AwesomeConf globalconf; extern AwesomeConf globalconf;
static char * static char *
titlebar_markup_parse(client_t *c, const char *str, ssize_t len) titlebar_markup_parse(client_t *c, const char *str, ssize_t len)
{ {
const char *ps; const char *elements[] = { "title", NULL };
char *new; const char *elements_sub[] = { c->name , NULL };
int i = 0; markup_parser_data_t *p;
ssize_t clen;
new = p_new(char, ++len); p = markup_parser_data_new(elements, elements_sub, countof(elements));
for(ps = str; *ps; ps++, i++) if(!markup_parse(p, str, len))
if(*ps == '%') return a_strdup(str);
{
ps++;
if(*ps == '%')
new[i] = '%';
else if(*ps == 't')
{
clen = a_strlen(c->name);
len += clen;
p_realloc(&new, len);
a_strcat(new + i, len - i, c->name);
i += clen - 1;
}
}
else
new[i] = *ps;
return new; return p->text;
} }
static char * static char *