2007-12-22 15:46:57 +01:00
|
|
|
/*
|
|
|
|
* widget.c - widget managing
|
|
|
|
*
|
2008-03-06 11:41:26 +01:00
|
|
|
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
|
2007-12-22 15:46:57 +01:00
|
|
|
* Copyright © 2007 Aldo Cortesi <aldo@nullcube.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-12-15 07:53:53 +01:00
|
|
|
#include "widget.h"
|
2007-12-17 10:57:17 +01:00
|
|
|
#include "statusbar.h"
|
2007-12-27 15:49:00 +01:00
|
|
|
#include "event.h"
|
2007-12-15 07:53:53 +01:00
|
|
|
|
2007-12-19 04:46:44 +01:00
|
|
|
extern AwesomeConf globalconf;
|
2007-12-16 10:25:13 +01:00
|
|
|
|
2008-01-16 08:06:59 +01:00
|
|
|
#include "widgetgen.h"
|
2007-12-15 18:21:02 +01:00
|
|
|
|
2008-03-06 11:41:26 +01:00
|
|
|
/** Compute widget alignment.
|
|
|
|
* This will process all widget starting at `widget' and will check their
|
|
|
|
* alignment and guess it if set to AlignAuto.
|
2008-04-29 09:09:25 +02:00
|
|
|
* \param widget A linked list of all widgets.
|
2008-03-06 11:41:26 +01:00
|
|
|
*/
|
2007-12-15 07:53:53 +01:00
|
|
|
void
|
2008-04-11 11:26:37 +02:00
|
|
|
widget_calculate_alignments(widget_t *widget)
|
2007-12-15 07:53:53 +01:00
|
|
|
{
|
2008-02-08 10:59:55 +01:00
|
|
|
for(; widget && widget->alignment != AlignFlex; widget = widget->next)
|
2008-02-25 20:09:19 +01:00
|
|
|
switch(widget->alignment)
|
|
|
|
{
|
|
|
|
case AlignCenter:
|
|
|
|
warn("widgets cannot be center aligned\n");
|
|
|
|
case AlignAuto:
|
2008-02-08 10:59:55 +01:00
|
|
|
widget->alignment = AlignLeft;
|
2008-02-25 20:09:19 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2007-12-15 13:37:34 +01:00
|
|
|
|
|
|
|
if(widget)
|
2008-02-08 10:59:55 +01:00
|
|
|
for(widget = widget->next; widget; widget = widget->next)
|
2008-02-25 20:09:19 +01:00
|
|
|
switch(widget->alignment)
|
|
|
|
{
|
|
|
|
case AlignFlex:
|
2008-02-28 16:19:03 +01:00
|
|
|
warn("multiple flex widgets (%s) in panel -"
|
|
|
|
" ignoring flex for all but the first.\n", widget->name);
|
2008-02-08 10:59:55 +01:00
|
|
|
widget->alignment = AlignRight;
|
2008-02-25 20:09:19 +01:00
|
|
|
break;
|
|
|
|
case AlignCenter:
|
2008-02-28 16:19:03 +01:00
|
|
|
warn("widget %s cannot be center aligned\n", widget->name);
|
2008-02-25 20:09:19 +01:00
|
|
|
case AlignAuto:
|
|
|
|
widget->alignment = AlignRight;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2007-12-15 07:53:53 +01:00
|
|
|
}
|
|
|
|
|
2008-03-06 11:41:26 +01:00
|
|
|
/** Compute offset for drawing the first pixel of a widget.
|
2008-04-29 09:09:25 +02:00
|
|
|
* \param barwidth The statusbar width.
|
|
|
|
* \param widgetwidth The widget width.
|
|
|
|
* \param alignment The widget alignment on statusbar.
|
|
|
|
* \return The x coordinate to draw at.
|
2008-03-06 11:41:26 +01:00
|
|
|
*/
|
2007-12-15 07:53:53 +01:00
|
|
|
int
|
2007-12-22 20:55:17 +01:00
|
|
|
widget_calculate_offset(int barwidth, int widgetwidth, int offset, int alignment)
|
2007-12-15 07:53:53 +01:00
|
|
|
{
|
2008-01-04 19:17:20 +01:00
|
|
|
switch(alignment)
|
|
|
|
{
|
|
|
|
case AlignLeft:
|
|
|
|
case AlignFlex:
|
2007-12-15 07:53:53 +01:00
|
|
|
return offset;
|
2008-01-04 19:17:20 +01:00
|
|
|
}
|
2007-12-15 13:37:34 +01:00
|
|
|
return barwidth - offset - widgetwidth;
|
2007-12-15 07:53:53 +01:00
|
|
|
}
|
|
|
|
|
2008-04-29 09:09:25 +02:00
|
|
|
/** Find a widget on a screen by its name.
|
|
|
|
* \param statusbar The statusbar to look into.
|
|
|
|
* \param name The widget name.
|
|
|
|
* \return A widget pointer.
|
2008-03-06 11:41:26 +01:00
|
|
|
*/
|
2008-04-30 18:44:23 +02:00
|
|
|
widget_t *
|
2008-04-11 11:27:36 +02:00
|
|
|
widget_getbyname(statusbar_t *sb, char *name)
|
2007-12-16 10:25:13 +01:00
|
|
|
{
|
2008-04-11 11:26:37 +02:00
|
|
|
widget_t *widget;
|
2008-02-13 06:53:08 +01:00
|
|
|
|
2008-04-03 14:00:46 +02:00
|
|
|
for(widget = sb->widgets; widget; widget = widget->next)
|
|
|
|
if(!a_strcmp(name, widget->name))
|
|
|
|
return widget;
|
2007-12-30 21:00:34 +01:00
|
|
|
|
2007-12-16 10:25:13 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-03-06 11:41:26 +01:00
|
|
|
/** Common function for button press event on widget.
|
|
|
|
* It will look into configuration to find the callback function to call.
|
2008-04-29 09:09:25 +02:00
|
|
|
* \param widget The widget.
|
|
|
|
* \param ev The button press event the widget received.
|
2008-03-06 11:41:26 +01:00
|
|
|
*/
|
2007-12-27 15:49:00 +01:00
|
|
|
static void
|
2008-04-11 11:26:37 +02:00
|
|
|
widget_common_button_press(widget_t *widget, xcb_button_press_event_t *ev)
|
2007-12-27 15:49:00 +01:00
|
|
|
{
|
|
|
|
Button *b;
|
|
|
|
|
|
|
|
for(b = widget->buttons; b; b = b->next)
|
2008-04-29 09:09:25 +02:00
|
|
|
if(ev->detail == b->button && CLEANMASK(ev->state) == b->mod
|
|
|
|
&& b->func)
|
2007-12-27 15:49:00 +01:00
|
|
|
b->func(widget->statusbar->screen, b->arg);
|
|
|
|
}
|
|
|
|
|
2008-03-06 11:41:26 +01:00
|
|
|
/** Common tell function for widget, which only warn user that widget
|
2008-04-29 09:09:25 +02:00
|
|
|
* cannot be told anything.
|
|
|
|
* \param widget The widget.
|
|
|
|
* \param new_value Unused argument.
|
|
|
|
* \return The status of the command, which is always an error in this case.
|
2008-03-06 11:41:26 +01:00
|
|
|
*/
|
2008-03-04 19:06:04 +01:00
|
|
|
static widget_tell_status_t
|
2008-04-11 11:26:37 +02:00
|
|
|
widget_common_tell(widget_t *widget, char *property __attribute__ ((unused)),
|
2008-04-23 23:44:55 +02:00
|
|
|
char *new_value __attribute__ ((unused)))
|
2007-12-17 10:57:17 +01:00
|
|
|
{
|
|
|
|
warn("%s widget does not accept commands.\n", widget->name);
|
2008-03-04 19:06:04 +01:00
|
|
|
return WIDGET_ERROR_CUSTOM;
|
2007-12-17 10:57:17 +01:00
|
|
|
}
|
|
|
|
|
2008-04-29 09:09:25 +02:00
|
|
|
/** Common function for creating a widget.
|
|
|
|
* \param widget The allocated widget.
|
|
|
|
* \param statusbar The statusbar the widget is on.
|
|
|
|
* \param config The cfg_t structure we will parse to set common info.
|
2008-03-06 11:41:26 +01:00
|
|
|
*/
|
2007-12-16 09:34:33 +01:00
|
|
|
void
|
2008-04-11 11:27:36 +02:00
|
|
|
widget_common_new(widget_t *widget, statusbar_t *statusbar, cfg_t *config)
|
2007-12-16 09:34:33 +01:00
|
|
|
{
|
2007-12-22 15:46:57 +01:00
|
|
|
widget->statusbar = statusbar;
|
2008-03-06 11:41:26 +01:00
|
|
|
widget->name = a_strdup(cfg_title(config));
|
2007-12-22 20:55:17 +01:00
|
|
|
widget->tell = widget_common_tell;
|
2007-12-27 15:49:00 +01:00
|
|
|
widget->button_press = widget_common_button_press;
|
2008-01-04 22:05:52 +01:00
|
|
|
widget->area.x = cfg_getint(config, "x");
|
|
|
|
widget->area.y = cfg_getint(config, "y");
|
2008-01-05 13:01:40 +01:00
|
|
|
widget->user_supplied_x = (widget->area.x != (int) 0xffffffff);
|
|
|
|
widget->user_supplied_y = (widget->area.y != (int) 0xffffffff);
|
2007-12-16 09:34:33 +01:00
|
|
|
}
|
|
|
|
|
2008-03-06 11:41:26 +01:00
|
|
|
/** Invalidate widgets which should be refresh upon
|
2008-04-11 11:26:37 +02:00
|
|
|
* external modifications. widget_t who watch flags will
|
2008-03-06 11:41:26 +01:00
|
|
|
* be set to be refreshed.
|
2008-04-29 09:09:25 +02:00
|
|
|
* \param screen Virtual screen number.
|
|
|
|
* \param flags Cache flags to invalidate.
|
2008-03-06 11:41:26 +01:00
|
|
|
*/
|
2008-01-07 18:12:38 +01:00
|
|
|
void
|
|
|
|
widget_invalidate_cache(int screen, int flags)
|
|
|
|
{
|
2008-04-11 11:27:36 +02:00
|
|
|
statusbar_t *statusbar;
|
2008-04-11 11:26:37 +02:00
|
|
|
widget_t *widget;
|
2008-01-07 18:12:38 +01:00
|
|
|
|
|
|
|
for(statusbar = globalconf.screens[screen].statusbar;
|
|
|
|
statusbar;
|
|
|
|
statusbar = statusbar->next)
|
|
|
|
for(widget = statusbar->widgets; widget; widget = widget->next)
|
2008-01-11 15:53:57 +01:00
|
|
|
if(widget->cache.flags & flags)
|
2008-03-21 16:50:17 +01:00
|
|
|
widget->cache.needs_update = true;
|
2008-01-07 18:12:38 +01:00
|
|
|
}
|
|
|
|
|
2008-04-10 23:37:22 +02:00
|
|
|
/** Send commands to widgets.
|
2008-04-29 09:09:25 +02:00
|
|
|
* \param screen Virtual screen number.
|
|
|
|
* \param arg Widget command. Syntax depends on specific widget.
|
2007-12-19 04:39:59 +01:00
|
|
|
* \ingroup ui_callback
|
|
|
|
*/
|
2007-12-16 10:25:13 +01:00
|
|
|
void
|
|
|
|
uicb_widget_tell(int screen, char *arg)
|
|
|
|
{
|
2008-04-11 11:27:36 +02:00
|
|
|
statusbar_t *statusbar;
|
2008-04-11 11:26:37 +02:00
|
|
|
widget_t *widget;
|
2008-04-23 23:44:55 +02:00
|
|
|
char *p, *property = NULL, *new_value;
|
2008-01-12 23:19:58 +01:00
|
|
|
ssize_t len;
|
2008-04-02 15:31:19 +02:00
|
|
|
widget_tell_status_t status;
|
2007-12-17 10:57:17 +01:00
|
|
|
|
2008-04-03 14:00:46 +02:00
|
|
|
if(!(len = a_strlen(arg)))
|
|
|
|
return warn("must specify a statusbar and a widget.\n");
|
2007-12-17 10:57:17 +01:00
|
|
|
|
2008-04-03 14:00:46 +02:00
|
|
|
if(!(p = strtok(arg, " ")))
|
|
|
|
return warn("ignoring malformed widget command (missing statusbar name).\n");
|
2007-12-22 15:46:57 +01:00
|
|
|
|
2008-04-03 14:00:46 +02:00
|
|
|
if(!(statusbar = statusbar_getbyname(screen, p)))
|
|
|
|
return warn("no such statusbar: %s\n", p);
|
2007-12-16 10:25:13 +01:00
|
|
|
|
2008-04-03 14:00:46 +02:00
|
|
|
if(!(p = strtok(NULL, " ")))
|
|
|
|
return warn("ignoring malformed widget command (missing widget name).\n");
|
|
|
|
|
|
|
|
if(!(widget = widget_getbyname(statusbar, p)))
|
|
|
|
return warn("no such widget: %s in statusbar %s.\n", p, statusbar->name);
|
|
|
|
|
|
|
|
if(!(p = strtok(NULL, " ")))
|
|
|
|
return warn("ignoring malformed widget command (missing property name).\n");
|
2008-02-17 08:16:40 +01:00
|
|
|
|
|
|
|
property = p;
|
2008-04-03 14:00:46 +02:00
|
|
|
p += a_strlen(property) + 1; /* could be out of 'arg' now */
|
2008-02-17 08:16:40 +01:00
|
|
|
|
2008-03-13 15:46:14 +01:00
|
|
|
/* arg + len points to the finishing \0.
|
|
|
|
* p to the char right of the first space (strtok delimiter)
|
|
|
|
*
|
2008-04-23 23:44:55 +02:00
|
|
|
* \0 is on the right(>) of p pointer => some text (new_value) */
|
2008-03-13 15:46:14 +01:00
|
|
|
if(arg + len > p)
|
2007-12-17 10:57:17 +01:00
|
|
|
{
|
2008-01-12 23:19:58 +01:00
|
|
|
len = a_strlen(p);
|
2008-04-23 23:44:55 +02:00
|
|
|
new_value = p_new(char, len + 1);
|
|
|
|
a_strncpy(new_value, len + 1, p, len);
|
|
|
|
status = widget->tell(widget, property, new_value);
|
|
|
|
p_delete(&new_value);
|
2007-12-17 10:57:17 +01:00
|
|
|
}
|
|
|
|
else
|
2008-04-23 05:35:55 +02:00
|
|
|
status = widget->tell(widget, property, NULL);
|
2007-12-22 15:46:57 +01:00
|
|
|
|
2008-04-02 15:31:19 +02:00
|
|
|
switch(status)
|
|
|
|
{
|
|
|
|
case WIDGET_ERROR:
|
|
|
|
warn("error changing property %s of widget %s\n",
|
|
|
|
property, widget->name);
|
|
|
|
break;
|
|
|
|
case WIDGET_ERROR_NOVALUE:
|
|
|
|
warn("error changing property %s of widget %s, no value given\n",
|
|
|
|
property, widget->name);
|
|
|
|
break;
|
|
|
|
case WIDGET_ERROR_FORMAT_BOOL:
|
|
|
|
warn("error changing property %s of widget %s, must is boolean (0 or 1)\n",
|
|
|
|
property, widget->name);
|
|
|
|
break;
|
|
|
|
case WIDGET_ERROR_FORMAT_FONT:
|
|
|
|
warn("error changing property %s of widget %s, must be a valid font\n",
|
|
|
|
property, widget->name);
|
|
|
|
break;
|
|
|
|
case WIDGET_ERROR_FORMAT_COLOR:
|
|
|
|
warn("error changing property %s of widget %s, must be a valid color\n",
|
|
|
|
property, widget->name);
|
|
|
|
break;
|
|
|
|
case WIDGET_ERROR_FORMAT_SECTION:
|
|
|
|
warn("error changing property %s of widget %s, section/title not found\n",
|
|
|
|
property, widget->name);
|
|
|
|
break;
|
|
|
|
case WIDGET_NOERROR:
|
2008-03-21 16:50:17 +01:00
|
|
|
widget->cache.needs_update = true;
|
2008-04-02 15:31:19 +02:00
|
|
|
break;
|
|
|
|
case WIDGET_ERROR_CUSTOM:
|
|
|
|
break;
|
|
|
|
}
|
2007-12-16 10:25:13 +01:00
|
|
|
}
|
2007-12-16 09:34:33 +01:00
|
|
|
|
2007-12-18 09:24:15 +01:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|