client_moveresize fix
[awesome.git] / widgets / textbox.c
blob2ff82e83de513fb4a6f72d45e105cd086f4c29c5
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"
26 extern AwesomeConf globalconf;
28 typedef struct
30 char *text;
31 int width;
32 Alignment align;
33 XColor fg;
34 XColor bg;
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
45 widget->area.width = MIN(draw_textwidth(ctx->display, widget->font, d->text),
46 widget->statusbar->width - used);
48 widget->area.height = widget->statusbar->height;
50 if(!widget->user_supplied_x)
51 widget->area.x = widget_calculate_offset(widget->statusbar->width,
52 widget->area.width,
53 offset,
54 widget->alignment);
55 if(!widget->user_supplied_y)
56 widget->area.y = 0;
58 draw_text(ctx, widget->area, d->align, 0, widget->font, d->text, d->fg, d->bg);
60 return widget->area.width;
63 static void
64 textbox_tell(Widget *widget, char *command)
66 char *ntok, *tok;
67 ssize_t command_len = a_strlen(command);
68 int i = 0, phys_screen = get_phys_screen(widget->statusbar->screen);
70 Data *d = widget->data;
71 if (d->text)
72 p_delete(&d->text);
74 for (i = 0, tok = command; tok && i < 2 && *tok == '#'; i++)
76 ntok = strchr(tok, ' ');
77 if((!ntok && command_len - (tok - command) == 7) ||
78 ntok - tok == 7)
80 if(ntok)
81 *ntok = 0;
82 if(!i)
83 draw_color_new(globalconf.display, phys_screen, tok, &d->fg);
84 else
85 draw_color_new(globalconf.display, phys_screen, tok, &d->bg);
86 if (ntok)
87 *ntok = ' ';
88 tok = ntok + (ntok != NULL);
90 else
91 break;
93 d->text = a_strdup(tok);
96 Widget *
97 textbox_new(Statusbar *statusbar, cfg_t *config)
99 Widget *w;
100 Data *d;
101 char *buf;
103 w = p_new(Widget, 1);
104 widget_common_new(w, statusbar, config);
105 w->draw = textbox_draw;
106 w->tell = textbox_tell;
107 w->alignment = draw_get_align(cfg_getstr(config, "align"));
109 w->data = d = p_new(Data, 1);
111 if((buf = cfg_getstr(config, "fg")))
112 draw_color_new(globalconf.display, get_phys_screen(statusbar->screen), buf, &d->fg);
113 else
114 d->fg = globalconf.screens[statusbar->screen].colors_normal[ColFG];
116 if((buf = cfg_getstr(config, "bg")))
117 draw_color_new(globalconf.display, get_phys_screen(statusbar->screen), buf, &d->bg);
118 else
119 d->bg = globalconf.screens[statusbar->screen].colors_normal[ColBG];
121 d->width = cfg_getint(config, "width");
122 d->align = draw_get_align(cfg_getstr(config, "text_align"));
124 if((buf = cfg_getstr(config, "font")))
125 w->font = XftFontOpenName(globalconf.display, get_phys_screen(statusbar->screen), buf);
127 if(!w->font)
128 w->font = globalconf.screens[statusbar->screen].font;
130 d->text = a_strdup(cfg_getstr(config, "text"));
132 return w;
134 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80