change warn() message and use simply , as strtok delimiter
[awesome.git] / widget.c
blob38f8632dfb764df2121ed7fcc4af478f4a97b482
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 {NULL, NULL}
42 void
43 widget_calculate_alignments(Widget *widget)
45 for(; widget; widget = widget->next)
47 if(widget->alignment == AlignFlex)
49 widget = widget->next;
50 break;
52 widget->alignment = AlignLeft;
55 if(widget)
56 for(; widget; widget = widget->next)
58 if (widget->alignment == AlignFlex)
59 warn("Multiple flex widgets in panel -"
60 " ignoring flex for all but the first.");
61 widget->alignment = AlignRight;
65 int
66 widget_calculate_offset(int barwidth, int widgetwidth, int offset, int alignment)
68 if (alignment == AlignLeft || alignment == AlignFlex)
69 return offset;
70 return barwidth - offset - widgetwidth;
73 static Widget *
74 widget_find(char *name, int screen)
76 Widget *widget;
77 Statusbar *sb;
79 for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
80 for(widget = sb->widgets; widget; widget = widget->next)
81 if(a_strcmp(name, widget->name) == 0)
82 return widget;
84 return NULL;
87 static void
88 widget_common_button_press(Widget *widget, XButtonPressedEvent *ev)
90 Button *b;
92 for(b = widget->buttons; b; b = b->next)
93 if(ev->button == b->button && CLEANMASK(ev->state) == b->mod && b->func)
94 b->func(widget->statusbar->screen, b->arg);
97 static void
98 widget_common_tell(Widget *widget, char *command __attribute__ ((unused)))
100 warn("%s widget does not accept commands.\n", widget->name);
103 void
104 widget_common_new(Widget *widget, Statusbar *statusbar, cfg_t* config)
106 const char *name;
108 widget->statusbar = statusbar;
109 name = cfg_title(config);
110 widget->name = a_strdup(name);
111 widget->tell = widget_common_tell;
112 widget->button_press = widget_common_button_press;
115 /** Send command to widget
116 * \param screen Screen ID
117 * \param arg Widget command. Syntax depends on specific widget.
118 * \ingroup ui_callback
120 void
121 uicb_widget_tell(int screen, char *arg)
123 Widget *widget;
124 char *p, *command;
125 int len;
127 if (!arg)
129 warn("Must specify a widget.\n");
130 return;
133 len = strlen(arg);
134 p = strtok(arg, " ");
135 if (!p)
137 warn("Ignoring malformed widget command.\n");
138 return;
141 widget = widget_find(p, screen);
142 if (!widget)
144 warn("No such widget: %s\n", p);
145 return;
148 if (p+strlen(p) < arg+len)
150 p = p + strlen(p) + 1;
151 command = p_new(char, strlen(p)+1);
152 strncpy(command, p, strlen(p));
153 widget->tell(widget, command);
154 p_delete(&command);
156 else
157 widget->tell(widget, NULL);
159 statusbar_draw_all(screen);
160 return;
163 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80