[doc] Use statusbar name in widget_tell
[awesome.git] / widget.c
blobe1dc579d5515c9edaf2d1ee7416bc51ffcb86109
1 /*
2 * widget.c - widget managing
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
5 * Copyright © 2007 Aldo Cortesi <aldo@nullcube.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "widget.h"
24 #include "statusbar.h"
25 #include "event.h"
27 extern AwesomeConf globalconf;
29 #include "widgetgen.h"
31 /** Compute widget alignment.
32 * This will process all widget starting at `widget' and will check their
33 * alignment and guess it if set to AlignAuto.
34 * \param widget a linked list of all widgets
36 void
37 widget_calculate_alignments(Widget *widget)
39 for(; widget && widget->alignment != AlignFlex; widget = widget->next)
41 switch(widget->alignment)
43 case AlignCenter:
44 warn("widgets cannot be center aligned\n");
45 case AlignAuto:
46 widget->alignment = AlignLeft;
47 break;
48 default:
49 break;
53 if(widget)
54 for(widget = widget->next; widget; widget = widget->next)
55 switch(widget->alignment)
57 case AlignFlex:
58 warn("multiple flex widgets (%s) in panel -"
59 " ignoring flex for all but the first.\n", widget->name);
60 widget->alignment = AlignRight;
61 break;
62 case AlignCenter:
63 warn("widget %s cannot be center aligned\n", widget->name);
64 case AlignAuto:
65 widget->alignment = AlignRight;
66 break;
67 default:
68 break;
72 /** Compute offset for drawing the first pixel of a widget.
73 * \param barwidth the statusbar width
74 * \param widgetwidth the widget width
75 * \param alignment the widget alignment on statusbar
76 * \return the x coordinate to draw at
78 int
79 widget_calculate_offset(int barwidth, int widgetwidth, int offset, int alignment)
81 switch(alignment)
83 case AlignLeft:
84 case AlignFlex:
85 return offset;
87 return barwidth - offset - widgetwidth;
90 /** Find a widget on a screen by its name
91 * \param statusbar the statusbar to look into
92 * \param name the widget name
93 * \return a widget
95 static Widget *
96 widget_getbyname(Statusbar *sb, char *name)
98 Widget *widget;
100 for(widget = sb->widgets; widget; widget = widget->next)
101 if(!a_strcmp(name, widget->name))
102 return widget;
104 return NULL;
107 /** Common function for button press event on widget.
108 * It will look into configuration to find the callback function to call.
109 * \param widget the widget
110 * \param ev the XButtonPressedEvent the widget received
112 static void
113 widget_common_button_press(Widget *widget, XButtonPressedEvent *ev)
115 Button *b;
117 for(b = widget->buttons; b; b = b->next)
118 if(ev->button == b->button && CLEANMASK(ev->state) == b->mod && b->func)
120 b->func(widget->statusbar->screen, b->arg);
121 break;
125 /** Common tell function for widget, which only warn user that widget
126 * cannot be told anything
127 * \param widget the widget
128 * \param command unused argument
129 * \return widget_tell_status_t enum (see structs.h)
131 static widget_tell_status_t
132 widget_common_tell(Widget *widget, char *property __attribute__ ((unused)),
133 char *command __attribute__ ((unused)))
135 warn("%s widget does not accept commands.\n", widget->name);
136 return WIDGET_ERROR_CUSTOM;
139 /** Common function for creating a widget
140 * \param widget The allocated widget
141 * \param statusbar the statusbar the widget is on
142 * \param config the cfg_t structure we will parse to set common info
144 void
145 widget_common_new(Widget *widget, Statusbar *statusbar, cfg_t *config)
147 widget->statusbar = statusbar;
148 widget->name = a_strdup(cfg_title(config));
149 widget->tell = widget_common_tell;
150 widget->button_press = widget_common_button_press;
151 widget->area.x = cfg_getint(config, "x");
152 widget->area.y = cfg_getint(config, "y");
153 widget->user_supplied_x = (widget->area.x != (int) 0xffffffff);
154 widget->user_supplied_y = (widget->area.y != (int) 0xffffffff);
157 /** Invalidate widgets which should be refresh upon
158 * external modifications. Widget who watch flags will
159 * be set to be refreshed.
160 * \param screen screen id
161 * \param flags cache flags
163 void
164 widget_invalidate_cache(int screen, int flags)
166 Statusbar *statusbar;
167 Widget *widget;
169 for(statusbar = globalconf.screens[screen].statusbar;
170 statusbar;
171 statusbar = statusbar->next)
172 for(widget = statusbar->widgets; widget; widget = widget->next)
173 if(widget->cache.flags & flags)
174 widget->cache.needs_update = True;
177 /** Send command to widget
178 * \param screen Screen ID
179 * \param arg Widget command. Syntax depends on specific widget.
180 * \ingroup ui_callback
182 void
183 uicb_widget_tell(int screen, char *arg)
185 Statusbar *statusbar;
186 Widget *widget;
187 char *p, *property = NULL, *command;
188 ssize_t len;
189 widget_tell_status_t status;
191 if(!(len = a_strlen(arg)))
192 return warn("must specify a statusbar and a widget.\n");
194 if(!(p = strtok(arg, " ")))
195 return warn("ignoring malformed widget command (missing statusbar name).\n");
197 if(!(statusbar = statusbar_getbyname(screen, p)))
198 return warn("no such statusbar: %s\n", p);
200 if(!(p = strtok(NULL, " ")))
201 return warn("ignoring malformed widget command (missing widget name).\n");
203 if(!(widget = widget_getbyname(statusbar, p)))
204 return warn("no such widget: %s in statusbar %s.\n", p, statusbar->name);
206 if(!(p = strtok(NULL, " ")))
207 return warn("ignoring malformed widget command (missing property name).\n");
209 property = p;
210 p += a_strlen(property) + 1; /* could be out of 'arg' now */
212 /* arg + len points to the finishing \0.
213 * p to the char right of the first space (strtok delimiter)
215 * \0 is on the right(>) of p pointer => some text (command) */
216 if(arg + len > p)
218 len = a_strlen(p);
219 command = p_new(char, len + 1);
220 a_strncpy(command, len + 1, p, len);
221 status = widget->tell(widget, property, command);
222 p_delete(&command);
224 else
225 status = WIDGET_ERROR_NOVALUE;
227 switch(status)
229 case WIDGET_ERROR:
230 warn("error changing property %s of widget %s\n",
231 property, widget->name);
232 break;
233 case WIDGET_ERROR_NOVALUE:
234 warn("error changing property %s of widget %s, no value given\n",
235 property, widget->name);
236 break;
237 case WIDGET_ERROR_FORMAT_BOOL:
238 warn("error changing property %s of widget %s, must is boolean (0 or 1)\n",
239 property, widget->name);
240 break;
241 case WIDGET_ERROR_FORMAT_FONT:
242 warn("error changing property %s of widget %s, must be a valid font\n",
243 property, widget->name);
244 break;
245 case WIDGET_ERROR_FORMAT_COLOR:
246 warn("error changing property %s of widget %s, must be a valid color\n",
247 property, widget->name);
248 break;
249 case WIDGET_ERROR_FORMAT_SECTION:
250 warn("error changing property %s of widget %s, section/title not found\n",
251 property, widget->name);
252 break;
253 case WIDGET_NOERROR:
254 widget->cache.needs_update = True;
255 break;
256 case WIDGET_ERROR_CUSTOM:
257 break;
261 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80