client_moveresize fix
[awesome.git] / widget.c
blobc0137de82309f5f034fe96c5b223be9535fb64a4
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"
27 extern AwesomeConf globalconf;
29 #include "widgetgen.h"
31 void
32 widget_calculate_alignments(Widget *widget)
34 for(; widget && widget->alignment != AlignFlex; widget = widget->next)
36 switch(widget->alignment)
38 case AlignCenter:
39 warn("widgets cannot be center aligned\n");
40 case AlignAuto:
41 widget->alignment = AlignLeft;
42 break;
43 default:
44 break;
48 if(widget)
49 for(widget = widget->next; widget; widget = widget->next)
50 switch(widget->alignment)
52 case AlignFlex:
53 warn("multiple flex widgets in panel -"
54 " ignoring flex for all but the first.\n");
55 widget->alignment = AlignRight;
56 break;
57 case AlignCenter:
58 warn("widgets cannot be center aligned\n");
59 case AlignAuto:
60 widget->alignment = AlignRight;
61 break;
62 default:
63 break;
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)
101 b->func(widget->statusbar->screen, b->arg);
102 break;
106 static void
107 widget_common_tell(Widget *widget, char *command __attribute__ ((unused)))
109 warn("%s widget does not accept commands.\n", widget->name);
112 void
113 widget_common_new(Widget *widget, Statusbar *statusbar, cfg_t* config)
115 const char *name;
117 widget->statusbar = statusbar;
118 name = cfg_title(config);
119 widget->name = a_strdup(name);
120 widget->tell = widget_common_tell;
121 widget->button_press = widget_common_button_press;
122 widget->area.x = cfg_getint(config, "x");
123 widget->area.y = cfg_getint(config, "y");
124 widget->user_supplied_x = (widget->area.x != (int) 0xffffffff);
125 widget->user_supplied_y = (widget->area.y != (int) 0xffffffff);
128 void
129 widget_invalidate_cache(int screen, int flags)
131 Statusbar *statusbar;
132 Widget *widget;
134 for(statusbar = globalconf.screens[screen].statusbar;
135 statusbar;
136 statusbar = statusbar->next)
137 for(widget = statusbar->widgets; widget; widget = widget->next)
138 if(widget->cache.flags & flags)
139 widget->cache.needs_update = True;
142 /** Send command to widget
143 * \param screen Screen ID
144 * \param arg Widget command. Syntax depends on specific widget.
145 * \ingroup ui_callback
147 void
148 uicb_widget_tell(int screen, char *arg)
150 Widget *widget;
151 char *p, *command;
152 ssize_t len;
154 if (!arg)
156 warn("Must specify a widget.\n");
157 return;
160 len = a_strlen(arg);
161 p = strtok(arg, " ");
162 if(!p)
164 warn("Ignoring malformed widget command.\n");
165 return;
168 widget = widget_find(p, screen);
169 if(!widget)
171 warn("No such widget: %s\n", p);
172 return;
175 if(p + a_strlen(p) < arg + len)
177 p = p + a_strlen(p) + 1;
178 len = a_strlen(p);
179 command = p_new(char, len + 1);
180 a_strncpy(command, len + 1, p, len);
181 widget->tell(widget, command);
182 p_delete(&command);
184 else
185 widget->tell(widget, NULL);
187 widget->cache.needs_update = True;
189 return;
192 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80