taglist now supports clicks if statusbar is right/left
[awesome.git] / widgets / taglist.c
blob504ef1c619c5fc947822cf4fd73b0f7844cbd626
1 /*
2 * taglist.c - tag list widget
4 * Copyright © 2007 Aldo Cortesi <aldo@nullcube.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "config.h"
23 #include "util.h"
24 #include "widget.h"
25 #include "tag.h"
26 #include "event.h"
28 extern AwesomeConf globalconf;
30 /** Check if at least one client is tagged with tag number t and is on screen
31 * screen
32 * \param screen screen number
33 * \param t tag
34 * \return True or False
36 static Bool
37 isoccupied(Tag *t)
39 Client *c;
41 for(c = globalconf.clients; c; c = c->next)
42 if(is_client_tagged(c, t))
43 return True;
44 return False;
47 static Bool
48 isurgent(Tag *t)
50 Client *c;
52 for(c = globalconf.clients; c; c = c->next)
53 if(is_client_tagged(c, t) && c->isurgent)
54 return True;
56 return False;
59 static int
60 taglist_draw(Widget *widget,
61 DrawCtx *ctx,
62 int offset,
63 int used __attribute__ ((unused)))
65 Tag *tag;
66 Client *sel = globalconf.focus->client;
67 VirtScreen vscreen = globalconf.screens[widget->statusbar->screen];
68 int w = 0, flagsize;
69 XColor *colors;
71 flagsize = (vscreen.font->height + 2) / 3;
73 widget->width = 0;
75 for(tag = vscreen.tags; tag; tag = tag->next)
76 widget->width += textwidth(vscreen.font, tag->name) + vscreen.font->height;
78 widget->location = widget_calculate_offset(widget->statusbar->width,
79 widget->width,
80 offset,
81 widget->alignment);
83 widget->width = 0;
84 for(tag = vscreen.tags; tag; tag = tag->next)
86 w = textwidth(vscreen.font, tag->name) + vscreen.font->height;
87 if(tag->selected)
88 colors = vscreen.colors_selected;
89 else if(isurgent(tag))
90 colors = vscreen.colors_urgent;
91 else
92 colors = vscreen.colors_normal;
93 draw_text(ctx,
94 widget->location + widget->width, 0,
95 w, widget->statusbar->height,
96 AlignCenter,
97 vscreen.font->height / 2,
98 vscreen.font,
99 tag->name,
100 colors[ColFG],
101 colors[ColBG]);
102 if(isoccupied(tag))
103 draw_rectangle(ctx, widget->location + widget->width, 0, flagsize, flagsize,
104 sel && is_client_tagged(sel, tag), colors[ColFG]);
105 widget->width += w;
108 return widget->width;
111 static void
112 taglist_button_press(Widget *widget, XButtonPressedEvent *ev)
114 VirtScreen vscreen = globalconf.screens[widget->statusbar->screen];
115 Button *b;
116 Tag *tag;
117 char buf[4];
118 int prev_width = 0, width = 0, i = 1;
120 for(b = widget->buttons; b; b = b->next)
121 if(ev->button == b->button && CLEANMASK(ev->state) == b->mod && b->func)
123 if(widget->statusbar->position == BarTop
124 || widget->statusbar->position == BarBot)
125 for(tag = vscreen.tags; tag; tag = tag->next, i++)
127 width = textwidth(vscreen.font, tag->name) + vscreen.font->height;
128 if(ev->x >= widget->location + prev_width
129 && ev->x <= widget->location + prev_width + width)
131 snprintf(buf, sizeof(buf), "%d", i);
132 b->func(widget->statusbar->screen, buf);
133 return;
135 prev_width += width;
137 else if(widget->statusbar->position == BarRight)
138 for(tag = vscreen.tags; tag; tag = tag->next, i++)
140 width = textwidth(vscreen.font, tag->name) + vscreen.font->height;
141 if(ev->y >= widget->location + prev_width
142 && ev->y <= widget->location + prev_width + width)
144 snprintf(buf, sizeof(buf), "%d", i);
145 b->func(widget->statusbar->screen, buf);
146 return;
148 prev_width += width;
150 else
151 for(tag = vscreen.tags; tag; tag = tag->next, i++)
153 width = textwidth(vscreen.font, tag->name) + vscreen.font->height;
154 if(widget->statusbar->width - ev->y >= widget->location + prev_width
155 && widget->statusbar->width - ev->y <= widget->location + prev_width + width)
157 snprintf(buf, sizeof(buf), "%d", i);
158 b->func(widget->statusbar->screen, buf);
159 return;
161 prev_width += width;
168 Widget *
169 taglist_new(Statusbar *statusbar, cfg_t *config)
171 Widget *w;
172 w = p_new(Widget, 1);
173 widget_common_new(w, statusbar, config);
174 w->draw = taglist_draw;
175 w->button_press = taglist_button_press;
176 return w;
179 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80