rename screen to phys_screen to avoid confusion
[awesome.git] / widget.c
blobc36af93e3bd747fb9fe99876f6af76aea3bc1b93
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)
35 if(widget->alignment == AlignAuto)
36 widget->alignment = AlignLeft;
38 if(widget)
39 for(widget = widget->next; widget; widget = widget->next)
41 if(widget->alignment == AlignFlex)
42 warn("Multiple flex widgets in panel -"
43 " ignoring flex for all but the first.");
44 if(widget->alignment == AlignAuto)
45 widget->alignment = AlignRight;
49 int
50 widget_calculate_offset(int barwidth, int widgetwidth, int offset, int alignment)
52 switch(alignment)
54 case AlignLeft:
55 case AlignFlex:
56 return offset;
58 return barwidth - offset - widgetwidth;
61 static Widget *
62 widget_find(char *name, int screen)
64 Widget *widget;
65 Statusbar *sb;
67 for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
68 for(widget = sb->widgets; widget; widget = widget->next)
69 if(a_strcmp(name, widget->name) == 0)
70 return widget;
72 return NULL;
75 static void
76 widget_common_button_press(Widget *widget, XButtonPressedEvent *ev)
78 Button *b;
80 for(b = widget->buttons; b; b = b->next)
81 if(ev->button == b->button && CLEANMASK(ev->state) == b->mod && b->func)
83 b->func(widget->statusbar->screen, b->arg);
84 break;
88 static void
89 widget_common_tell(Widget *widget, char *command __attribute__ ((unused)))
91 warn("%s widget does not accept commands.\n", widget->name);
94 void
95 widget_common_new(Widget *widget, Statusbar *statusbar, cfg_t* config)
97 const char *name;
99 widget->statusbar = statusbar;
100 name = cfg_title(config);
101 widget->name = a_strdup(name);
102 widget->tell = widget_common_tell;
103 widget->button_press = widget_common_button_press;
104 widget->area.x = cfg_getint(config, "x");
105 widget->area.y = cfg_getint(config, "y");
106 widget->user_supplied_x = (widget->area.x != (int) 0xffffffff);
107 widget->user_supplied_y = (widget->area.y != (int) 0xffffffff);
110 void
111 widget_invalidate_cache(int screen, int flags)
113 Statusbar *statusbar;
114 Widget *widget;
116 for(statusbar = globalconf.screens[screen].statusbar;
117 statusbar;
118 statusbar = statusbar->next)
119 for(widget = statusbar->widgets; widget; widget = widget->next)
120 if(widget->cache.flags & flags)
121 widget->cache.needs_update = True;
124 /** Send command to widget
125 * \param screen Screen ID
126 * \param arg Widget command. Syntax depends on specific widget.
127 * \ingroup ui_callback
129 void
130 uicb_widget_tell(int screen, char *arg)
132 Widget *widget;
133 char *p, *command;
134 ssize_t len;
136 if (!arg)
138 warn("Must specify a widget.\n");
139 return;
142 len = a_strlen(arg);
143 p = strtok(arg, " ");
144 if(!p)
146 warn("Ignoring malformed widget command.\n");
147 return;
150 widget = widget_find(p, screen);
151 if(!widget)
153 warn("No such widget: %s\n", p);
154 return;
157 if(p + a_strlen(p) < arg + len)
159 p = p + a_strlen(p) + 1;
160 len = a_strlen(p);
161 command = p_new(char, len + 1);
162 a_strncpy(command, len + 1, p, len);
163 widget->tell(widget, command);
164 p_delete(&command);
166 else
167 widget->tell(widget, NULL);
169 widget->cache.needs_update = True;
171 return;
174 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80