add scratch window support
[awesome.git] / widget.c
blob68159c043f2f839f7e828d38c4cb39027cfb0e08
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 = widget->next)
36 if(widget->alignment == AlignFlex)
38 widget = widget->next;
39 break;
41 widget->alignment = AlignLeft;
44 if(widget)
45 for(; widget; widget = widget->next)
47 if (widget->alignment == AlignFlex)
48 warn("Multiple flex widgets in panel -"
49 " ignoring flex for all but the first.");
50 widget->alignment = AlignRight;
54 int
55 widget_calculate_offset(int barwidth, int widgetwidth, int offset, int alignment)
57 switch(alignment)
59 case AlignLeft:
60 case AlignFlex:
61 return offset;
63 return barwidth - offset - widgetwidth;
66 static Widget *
67 widget_find(char *name, int screen)
69 Widget *widget;
70 Statusbar *sb;
72 for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
73 for(widget = sb->widgets; widget; widget = widget->next)
74 if(a_strcmp(name, widget->name) == 0)
75 return widget;
77 return NULL;
80 static void
81 widget_common_button_press(Widget *widget, XButtonPressedEvent *ev)
83 Button *b;
85 for(b = widget->buttons; b; b = b->next)
86 if(ev->button == b->button && CLEANMASK(ev->state) == b->mod && b->func)
88 b->func(widget->statusbar->screen, b->arg);
89 break;
93 static void
94 widget_common_tell(Widget *widget, char *command __attribute__ ((unused)))
96 warn("%s widget does not accept commands.\n", widget->name);
99 void
100 widget_common_new(Widget *widget, Statusbar *statusbar, cfg_t* config)
102 const char *name;
104 widget->statusbar = statusbar;
105 name = cfg_title(config);
106 widget->name = a_strdup(name);
107 widget->tell = widget_common_tell;
108 widget->button_press = widget_common_button_press;
109 widget->area.x = cfg_getint(config, "x");
110 widget->area.y = cfg_getint(config, "y");
111 widget->user_supplied_x = (widget->area.x != (int) 0xffffffff);
112 widget->user_supplied_y = (widget->area.y != (int) 0xffffffff);
115 void
116 widget_invalidate_cache(int screen, int flags)
118 Statusbar *statusbar;
119 Widget *widget;
121 for(statusbar = globalconf.screens[screen].statusbar;
122 statusbar;
123 statusbar = statusbar->next)
124 for(widget = statusbar->widgets; widget; widget = widget->next)
125 if(widget->cache.flags & flags)
126 widget->cache.needs_update = True;
129 /** Send command to widget
130 * \param screen Screen ID
131 * \param arg Widget command. Syntax depends on specific widget.
132 * \ingroup ui_callback
134 void
135 uicb_widget_tell(int screen, char *arg)
137 Widget *widget;
138 char *p, *command;
139 ssize_t len;
141 if (!arg)
143 warn("Must specify a widget.\n");
144 return;
147 len = a_strlen(arg);
148 p = strtok(arg, " ");
149 if(!p)
151 warn("Ignoring malformed widget command.\n");
152 return;
155 widget = widget_find(p, screen);
156 if(!widget)
158 warn("No such widget: %s\n", p);
159 return;
162 if(p + a_strlen(p) < arg + len)
164 p = p + a_strlen(p) + 1;
165 len = a_strlen(p);
166 command = p_new(char, len + 1);
167 a_strncpy(command, len + 1, p, len);
168 widget->tell(widget, command);
169 p_delete(&command);
171 else
172 widget->tell(widget, NULL);
174 widget->cache.needs_update = True;
176 return;
179 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80