awesome-client send now everything in one message
[awesome.git] / widgets / textbox.c
blobe18c315b739013a8b482190d9502b5f717b8c3ec
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 "util.h"
23 #include "widget.h"
24 #include "xutil.h"
25 #include "screen.h"
27 extern AwesomeConf globalconf;
29 typedef struct
31 char *text;
32 int width;
33 Alignment align;
34 XColor fg;
35 XColor bg;
36 } Data;
38 static int
39 textbox_draw(Widget *widget, DrawCtx *ctx, int offset,
40 int used __attribute__ ((unused)))
42 Data *d = widget->data;
44 if(d->width)
45 widget->area.width = d->width;
46 else
47 widget->area.width = draw_textwidth(widget->font, d->text);
49 widget->area.height = widget->statusbar->height;
51 if(!widget->user_supplied_x)
52 widget->area.x = widget_calculate_offset(widget->statusbar->width,
53 widget->area.width,
54 offset,
55 widget->alignment);
56 if(!widget->user_supplied_y)
57 widget->area.y = 0;
59 draw_text(ctx, widget->area.x, widget->area.y, widget->area.width,
60 widget->statusbar->height, d->align, 0, widget->font,
61 d->text, d->fg, d->bg);
63 return widget->area.width;
66 static void
67 textbox_tell(Widget *widget, char *command)
69 char *tok, *ntok, buf[8];
70 int i = 0, color = 0;
71 ssize_t command_len = a_strlen(command) + 1;
72 char* text = p_new(char, command_len);
74 Data *d = widget->data;
75 if (d->text)
76 p_delete(&d->text);
78 for(tok = command ; tok; tok = ntok, i++)
80 /* get next token */
81 ntok = strchr(tok + 1, ' ');
83 /* if not first time in the loop, drop the space and put it in the
84 * string */
85 if(i)
87 tok++;
88 a_strcat(text, command_len, " ");
91 if(*tok == '#' && i < 2)
93 if(ntok)
94 a_strncpy(buf, ssizeof(buf), tok, ntok - tok);
95 switch(i)
97 case 0:
98 d->fg = initxcolor(get_phys_screen(widget->statusbar->screen),
99 buf);
100 break;
101 case 1:
102 d->bg = initxcolor(get_phys_screen(widget->statusbar->screen),
103 buf);
104 break;
106 color++;
108 else if(ntok)
109 a_strncat(text, command_len, tok, ntok - tok);
110 else
111 a_strcat(text, command_len, tok);
114 d->text = a_strdup(text);
115 p_delete(&text);
118 Widget *
119 textbox_new(Statusbar *statusbar, cfg_t *config)
121 Widget *w;
122 Data *d;
123 char *buf;
125 w = p_new(Widget, 1);
126 widget_common_new(w, statusbar, config);
127 w->draw = textbox_draw;
128 w->tell = textbox_tell;
130 w->data = d = p_new(Data, 1);
132 if((buf = cfg_getstr(config, "fg")))
133 d->fg = initxcolor(statusbar->screen, buf);
134 else
135 d->fg = globalconf.screens[statusbar->screen].colors_normal[ColFG];
137 if((buf = cfg_getstr(config, "bg")))
138 d->bg = initxcolor(get_phys_screen(statusbar->screen), buf);
139 else
140 d->bg = globalconf.screens[statusbar->screen].colors_normal[ColBG];
142 d->width = cfg_getint(config, "width");
143 d->align = draw_get_align(cfg_getstr(config, "align"));
145 if((buf = cfg_getstr(config, "font")))
146 w->font = XftFontOpenName(globalconf.display, get_phys_screen(statusbar->screen), buf);
148 if(!w->font)
149 w->font = globalconf.screens[statusbar->screen].font;
151 d->text = a_strdup(cfg_getstr(config, "text"));
152 return w;
154 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80