[uicb] uicb_client_toggletag() with NULL use as sticky toggle
[awesome.git] / widgets / textbox.c
blobdf33c66fc53650e91c57966e01ef8b05ddf26ce0
1 /*
2 * textbox.c - text box widget
4 * Copyright © 2007 Aldo Cortesi <aldo@nullcube.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "widget.h"
23 #include "screen.h"
24 #include "common/util.h"
25 #include "common/configopts.h"
27 extern AwesomeConf globalconf;
29 typedef struct
31 char *text;
32 int width;
33 Alignment align;
34 style_t style;
35 } Data;
37 static int
38 textbox_draw(Widget *widget, DrawCtx *ctx, int offset, int used)
40 Data *d = widget->data;
42 if(d->width)
43 widget->area.width = d->width;
44 else if(widget->alignment == AlignFlex)
45 widget->area.width = widget->statusbar->width - used;
46 else
47 widget->area.width = MIN(draw_textwidth(ctx->display, d->style.font, d->text),
48 widget->statusbar->width - used);
50 widget->area.height = widget->statusbar->height;
52 if(!widget->user_supplied_x)
53 widget->area.x = widget_calculate_offset(widget->statusbar->width,
54 widget->area.width,
55 offset,
56 widget->alignment);
57 if(!widget->user_supplied_y)
58 widget->area.y = 0;
60 draw_text(ctx, widget->area, d->align, 0, d->text, d->style);
62 return widget->area.width;
65 static widget_tell_status_t
66 textbox_tell(Widget *widget, char *property, char *command)
68 Data *d = widget->data;
69 font_t *newfont;
71 if(!a_strcmp(property, "text"))
73 if (d->text)
74 p_delete(&d->text);
75 d->text = a_strdup(command);
77 /* !command means a not existing string. So return here */
78 else if(!command)
79 return WIDGET_ERROR_NOVALUE;
80 else if(!a_strcmp(property, "fg"))
81 if(draw_color_new(globalconf.display, widget->statusbar->screen, command, &d->style.fg))
82 return WIDGET_NOERROR;
83 else
84 return WIDGET_ERROR_FORMAT_COLOR;
85 else if(!a_strcmp(property, "bg"))
86 if(draw_color_new(globalconf.display, widget->statusbar->screen, command, &d->style.bg))
87 return WIDGET_NOERROR;
88 else
89 return WIDGET_ERROR_FORMAT_COLOR;
90 else if(!a_strcmp(property, "font"))
92 if((newfont = draw_font_new(globalconf.display, command)))
94 if(d->style.font != globalconf.screens[widget->statusbar->screen].styles.normal.font)
95 draw_font_delete(&d->style.font);
96 d->style.font = newfont;
98 else
99 return WIDGET_ERROR_FORMAT_FONT;
101 else if(!a_strcmp(property, "width"))
102 d->width = atoi(command);
103 else if(!a_strcmp(property, "text_align"))
104 d->align = draw_align_get_from_str(command);
105 else
106 return WIDGET_ERROR;
108 return WIDGET_NOERROR;
111 Widget *
112 textbox_new(Statusbar *statusbar, cfg_t *config)
114 Widget *w;
115 Data *d;
117 w = p_new(Widget, 1);
118 widget_common_new(w, statusbar, config);
119 w->draw = textbox_draw;
120 w->tell = textbox_tell;
121 w->alignment = cfg_getalignment(config, "align");
123 w->data = d = p_new(Data, 1);
125 draw_style_init(globalconf.display, statusbar->phys_screen,
126 cfg_getsec(config, "style"),
127 &d->style,
128 &globalconf.screens[statusbar->screen].styles.normal);
130 d->width = cfg_getint(config, "width");
131 d->align = cfg_getalignment(config, "text_align");
133 d->text = a_strdup(cfg_getstr(config, "text"));
135 return w;
137 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80