rename some client functions
[awesome.git] / widget.c
blob1c9674996c20b3081a7a7aff2b84adf66b39de2f
1 /*
2 * widget.c - widget managing
4 * Copyright © 2007 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"
26 #include "common/util.h"
28 extern AwesomeConf globalconf;
30 #include "widgetgen.h"
32 void
33 widget_calculate_alignments(Widget *widget)
35 for(; widget; widget = widget->next)
37 if(widget->alignment == AlignFlex)
39 widget = widget->next;
40 break;
42 widget->alignment = AlignLeft;
45 if(widget)
46 for(; widget; widget = widget->next)
48 if (widget->alignment == AlignFlex)
49 warn("Multiple flex widgets in panel -"
50 " ignoring flex for all but the first.");
51 widget->alignment = AlignRight;
55 int
56 widget_calculate_offset(int barwidth, int widgetwidth, int offset, int alignment)
58 switch(alignment)
60 case AlignLeft:
61 case AlignFlex:
62 return offset;
64 return barwidth - offset - widgetwidth;
67 static Widget *
68 widget_find(char *name, int screen)
70 Widget *widget;
71 Statusbar *sb;
73 for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
74 for(widget = sb->widgets; widget; widget = widget->next)
75 if(a_strcmp(name, widget->name) == 0)
76 return widget;
78 return NULL;
81 static void
82 widget_common_button_press(Widget *widget, XButtonPressedEvent *ev)
84 Button *b;
86 for(b = widget->buttons; b; b = b->next)
87 if(ev->button == b->button && CLEANMASK(ev->state) == b->mod && b->func)
89 b->func(widget->statusbar->screen, b->arg);
90 break;
94 static void
95 widget_common_tell(Widget *widget, char *command __attribute__ ((unused)))
97 warn("%s widget does not accept commands.\n", widget->name);
100 void
101 widget_common_new(Widget *widget, Statusbar *statusbar, cfg_t* config)
103 const char *name;
105 widget->statusbar = statusbar;
106 name = cfg_title(config);
107 widget->name = a_strdup(name);
108 widget->tell = widget_common_tell;
109 widget->button_press = widget_common_button_press;
110 widget->area.x = cfg_getint(config, "x");
111 widget->area.y = cfg_getint(config, "y");
112 widget->user_supplied_x = (widget->area.x != (int) 0xffffffff);
113 widget->user_supplied_y = (widget->area.y != (int) 0xffffffff);
116 void
117 widget_invalidate_cache(int screen, int flags)
119 Statusbar *statusbar;
120 Widget *widget;
122 for(statusbar = globalconf.screens[screen].statusbar;
123 statusbar;
124 statusbar = statusbar->next)
125 for(widget = statusbar->widgets; widget; widget = widget->next)
126 if(widget->cache.flags & flags)
127 widget->cache.needs_update = True;
130 /** Send command to widget
131 * \param screen Screen ID
132 * \param arg Widget command. Syntax depends on specific widget.
133 * \ingroup ui_callback
135 void
136 uicb_widget_tell(int screen, char *arg)
138 Widget *widget;
139 char *p, *command;
140 ssize_t len;
142 if (!arg)
144 warn("Must specify a widget.\n");
145 return;
148 len = a_strlen(arg);
149 p = strtok(arg, " ");
150 if(!p)
152 warn("Ignoring malformed widget command.\n");
153 return;
156 widget = widget_find(p, screen);
157 if(!widget)
159 warn("No such widget: %s\n", p);
160 return;
163 if(p + a_strlen(p) < arg + len)
165 p = p + a_strlen(p) + 1;
166 len = a_strlen(p);
167 command = p_new(char, len + 1);
168 a_strncpy(command, len + 1, p, len);
169 widget->tell(widget, command);
170 p_delete(&command);
172 else
173 widget->tell(widget, NULL);
175 widget->cache.needs_update = True;
177 return;
180 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80