invalidate cache
[awesome.git] / widget.c
blob1a7226d7316856cdcb08a688e132b78c6597bde1
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 "util.h"
24 #include "widget.h"
25 #include "statusbar.h"
26 #include "event.h"
28 extern AwesomeConf globalconf;
30 const NameFuncLink WidgetList[] =
32 {"taglist", taglist_new},
33 {"layoutinfo", layoutinfo_new},
34 {"focustitle", focustitle_new},
35 {"textbox", textbox_new},
36 {"iconbox", iconbox_new},
37 {"netwmicon", netwmicon_new},
38 {"progressbar", progressbar_new},
39 {"graph", graph_new},
40 {"tasklist", tasklist_new},
41 {NULL, NULL}
44 void
45 widget_calculate_alignments(Widget *widget)
47 for(; widget; widget = widget->next)
49 if(widget->alignment == AlignFlex)
51 widget = widget->next;
52 break;
54 widget->alignment = AlignLeft;
57 if(widget)
58 for(; widget; widget = widget->next)
60 if (widget->alignment == AlignFlex)
61 warn("Multiple flex widgets in panel -"
62 " ignoring flex for all but the first.");
63 widget->alignment = AlignRight;
67 int
68 widget_calculate_offset(int barwidth, int widgetwidth, int offset, int alignment)
70 switch(alignment)
72 case AlignLeft:
73 case AlignFlex:
74 return offset;
76 return barwidth - offset - widgetwidth;
79 static Widget *
80 widget_find(char *name, int screen)
82 Widget *widget;
83 Statusbar *sb;
85 for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
86 for(widget = sb->widgets; widget; widget = widget->next)
87 if(a_strcmp(name, widget->name) == 0)
88 return widget;
90 return NULL;
93 static void
94 widget_common_button_press(Widget *widget, XButtonPressedEvent *ev)
96 Button *b;
98 for(b = widget->buttons; b; b = b->next)
99 if(ev->button == b->button && CLEANMASK(ev->state) == b->mod && b->func)
100 b->func(widget->statusbar->screen, b->arg);
103 static void
104 widget_common_tell(Widget *widget, char *command __attribute__ ((unused)))
106 warn("%s widget does not accept commands.\n", widget->name);
109 void
110 widget_common_new(Widget *widget, Statusbar *statusbar, cfg_t* config)
112 const char *name;
114 widget->statusbar = statusbar;
115 name = cfg_title(config);
116 widget->name = a_strdup(name);
117 widget->tell = widget_common_tell;
118 widget->button_press = widget_common_button_press;
119 widget->area.x = cfg_getint(config, "x");
120 widget->area.y = cfg_getint(config, "y");
121 widget->user_supplied_x = (widget->area.x != (int) 0xffffffff);
122 widget->user_supplied_y = (widget->area.y != (int) 0xffffffff);
125 void
126 widget_invalidate_cache(int screen, int flags)
128 Statusbar *statusbar;
129 Widget *widget;
131 for(statusbar = globalconf.screens[screen].statusbar;
132 statusbar;
133 statusbar = statusbar->next)
134 for(widget = statusbar->widgets; widget; widget = widget->next)
135 if(widget->cache.flags & flags)
136 widget->cache.needs_update = True;
139 /** Send command to widget
140 * \param screen Screen ID
141 * \param arg Widget command. Syntax depends on specific widget.
142 * \ingroup ui_callback
144 void
145 uicb_widget_tell(int screen, char *arg)
147 Widget *widget;
148 char *p, *command;
149 int len;
151 if (!arg)
153 warn("Must specify a widget.\n");
154 return;
157 len = strlen(arg);
158 p = strtok(arg, " ");
159 if (!p)
161 warn("Ignoring malformed widget command.\n");
162 return;
165 widget = widget_find(p, screen);
166 if (!widget)
168 warn("No such widget: %s\n", p);
169 return;
172 if (p+strlen(p) < arg+len)
174 p = p + strlen(p) + 1;
175 command = p_new(char, strlen(p)+1);
176 strncpy(command, p, strlen(p));
177 widget->tell(widget, command);
178 p_delete(&command);
180 else
181 widget->tell(widget, NULL);
183 widget->cache.needs_update = True;
185 return;
188 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80