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.
24 #include "statusbar.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
37 widget_calculate_alignments(Widget
*widget
)
39 for(; widget
&& widget
->alignment
!= AlignFlex
; widget
= widget
->next
)
41 switch(widget
->alignment
)
44 warn("widgets cannot be center aligned\n");
46 widget
->alignment
= AlignLeft
;
54 for(widget
= widget
->next
; widget
; widget
= widget
->next
)
55 switch(widget
->alignment
)
58 warn("multiple flex widgets (%s) in panel -"
59 " ignoring flex for all but the first.\n", widget
->name
);
60 widget
->alignment
= AlignRight
;
63 warn("widget %s cannot be center aligned\n", widget
->name
);
65 widget
->alignment
= AlignRight
;
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
79 widget_calculate_offset(int barwidth
, int widgetwidth
, int offset
, int alignment
)
87 return barwidth
- offset
- widgetwidth
;
90 /** Find a widget on a screen by its name
91 * \param name the widget name
92 * \param screen the screen to look into
96 widget_find(char *name
, int screen
)
101 for(sb
= globalconf
.screens
[screen
].statusbar
; sb
; sb
= sb
->next
)
102 for(widget
= sb
->widgets
; widget
; widget
= widget
->next
)
103 if(a_strcmp(name
, widget
->name
) == 0)
109 /** Common function for button press event on widget.
110 * It will look into configuration to find the callback function to call.
111 * \param widget the widget
112 * \param ev the XButtonPressedEvent the widget received
115 widget_common_button_press(Widget
*widget
, XButtonPressedEvent
*ev
)
119 for(b
= widget
->buttons
; b
; b
= b
->next
)
120 if(ev
->button
== b
->button
&& CLEANMASK(ev
->state
) == b
->mod
&& b
->func
)
122 b
->func(widget
->statusbar
->screen
, b
->arg
);
127 /** Common tell function for widget, which only warn user that widget
128 * cannot be told anything
129 * \param widget the widget
130 * \param command unused argument
131 * \return widget_tell_status_t enum (see structs.h)
133 static widget_tell_status_t
134 widget_common_tell(Widget
*widget
, char *property
__attribute__ ((unused
)),
135 char *command
__attribute__ ((unused
)))
137 warn("%s widget does not accept commands.\n", widget
->name
);
138 return WIDGET_ERROR_CUSTOM
;
141 /** Common function for creating a widget
142 * \param widget The allocated widget
143 * \param statusbar the statusbar the widget is on
144 * \param config the cfg_t structure we will parse to set common info
147 widget_common_new(Widget
*widget
, Statusbar
*statusbar
, cfg_t
*config
)
149 widget
->statusbar
= statusbar
;
150 widget
->name
= a_strdup(cfg_title(config
));
151 widget
->tell
= widget_common_tell
;
152 widget
->button_press
= widget_common_button_press
;
153 widget
->area
.x
= cfg_getint(config
, "x");
154 widget
->area
.y
= cfg_getint(config
, "y");
155 widget
->user_supplied_x
= (widget
->area
.x
!= (int) 0xffffffff);
156 widget
->user_supplied_y
= (widget
->area
.y
!= (int) 0xffffffff);
159 /** Invalidate widgets which should be refresh upon
160 * external modifications. Widget who watch flags will
161 * be set to be refreshed.
162 * \param screen screen id
163 * \param flags cache flags
166 widget_invalidate_cache(int screen
, int flags
)
168 Statusbar
*statusbar
;
171 for(statusbar
= globalconf
.screens
[screen
].statusbar
;
173 statusbar
= statusbar
->next
)
174 for(widget
= statusbar
->widgets
; widget
; widget
= widget
->next
)
175 if(widget
->cache
.flags
& flags
)
176 widget
->cache
.needs_update
= True
;
179 /** Send command to widget
180 * \param screen Screen ID
181 * \param arg Widget command. Syntax depends on specific widget.
182 * \ingroup ui_callback
185 uicb_widget_tell(int screen
, char *arg
)
188 char *p
, *property
= NULL
, *command
;
193 warn("Must specify a widget.\n");
198 p
= strtok(arg
, " ");
201 warn("Ignoring malformed widget command.\n");
205 widget
= widget_find(p
, screen
);
208 warn("No such widget: %s\n", p
);
212 p
= strtok(NULL
, " ");
215 warn("Ignoring malformed widget command.\n");
220 p
= p
+ a_strlen(p
) + 1; /* could be out of 'arg' now */
222 /* arg + len points to the finishing \0.
223 * p to the char right of the first space (strtok delimiter)
225 * \0 is on the right(>) of p pointer => some text (command) */
229 command
= p_new(char, len
+ 1);
230 a_strncpy(command
, len
+ 1, p
, len
);
231 switch(widget
->tell(widget
, property
, command
))
234 warn("error changing property %s of widget %s\n",
235 property
, widget
->name
);
237 case WIDGET_ERROR_NOVALUE
:
238 warn("error changing property %s of widget %s, no value given\n",
239 property
, widget
->name
);
241 case WIDGET_ERROR_FORMAT_BOOL
:
242 warn("error changing property %s of widget %s, must is boolean (0 or 1)\n",
243 property
, widget
->name
);
245 case WIDGET_ERROR_FORMAT_FONT
:
246 warn("error changing property %s of widget %s, must be a valid font\n",
247 property
, widget
->name
);
249 case WIDGET_ERROR_FORMAT_COLOR
:
250 warn("error changing property %s of widget %s, must be a valid color\n",
251 property
, widget
->name
);
253 case WIDGET_ERROR_FORMAT_SECTION
:
254 warn("error changing property %s of widget %s, section/title not found\n",
255 property
, widget
->name
);
258 case WIDGET_ERROR_CUSTOM
:
264 widget
->tell(widget
, property
, NULL
);
266 widget
->cache
.needs_update
= True
;
271 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80