clean some tag stuff
[awesome.git] / widgets / tasklist.c
blob8cb87b26892eda4f57926fcc469f81a7fe207fbf
1 /*
2 * tasklist.c - task list widget
4 * Copyright © 2008 Julien Danjou <julien@danjou.info>
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 <stdio.h>
23 #include "util.h"
24 #include "widget.h"
25 #include "client.h"
26 #include "focus.h"
27 #include "xutil.h"
28 #include "screen.h"
29 #include "event.h"
30 #include "ewmh.h"
31 #include "rules.h"
32 #include "tag.h"
34 #define ISVISIBLE_ON_TB(c, nscreen, show_all) ((!show_all && client_isvisible(c, nscreen) && !c->skiptb) \
35 || (show_all && c->screen == nscreen && !c->skiptb))
37 extern AwesomeConf globalconf;
39 typedef struct
41 Bool show_all;
42 Bool show_icons;
43 Alignment align;
44 XColor fg_sel;
45 XColor bg_sel;
46 XColor fg;
47 XColor bg;
48 } Data;
50 static int
51 tasklist_draw(Widget *widget, DrawCtx *ctx, int offset, int used)
53 Client *c;
54 Data *d = widget->data;
55 Client *sel = focus_get_current_client(widget->statusbar->screen);
56 Rule *r;
57 Area area;
58 int n = 0, i = 0, box_width = 0, icon_width = 0;
59 NetWMIcon *icon;
61 for(c = globalconf.clients; c; c = c->next)
62 if(ISVISIBLE_ON_TB(c, widget->statusbar->screen, d->show_all))
63 n++;
65 if(!n)
67 widget->area.width = 0;
68 return widget->area.width;
71 box_width = (widget->statusbar->width - used) / n;
73 if(!widget->user_supplied_x)
74 widget->area.x = widget_calculate_offset(widget->statusbar->width,
76 offset,
77 widget->alignment);
79 if(!widget->user_supplied_y)
80 widget->area.y = widget->area.y = 0;
82 for(c = globalconf.clients; c; c = c->next)
83 if(ISVISIBLE_ON_TB(c, widget->statusbar->screen, d->show_all))
85 icon_width = 0;
87 if(d->show_icons)
89 if((r = rule_matching_client(c)) && r->icon)
91 area = draw_get_image_size(r->icon);
92 icon_width = ((double) widget->statusbar->height / (double) area.height) * area.width;
93 draw_image(ctx,
94 widget->area.x + box_width * i,
95 widget->area.y,
96 widget->statusbar->height,
97 r->icon);
100 if(!icon_width && (icon = ewmh_get_window_icon(c->win)))
102 icon_width = ((double) widget->statusbar->height / (double) icon->height)
103 * icon->width;
104 draw_image_from_argb_data(ctx,
105 widget->area.x + box_width * i,
106 widget->area.y,
107 icon->width, icon->height,
108 widget->statusbar->height, icon->image);
109 p_delete(&icon->image);
110 p_delete(&icon);
114 area.x = widget->area.x + icon_width + box_width * i;
115 area.y = widget->area.y;
116 area.width = box_width - icon_width;
117 area.height = widget->statusbar->height;
118 if(sel == c)
119 draw_text(ctx, area, d->align,
120 widget->font->height / 2, widget->font, c->name,
121 d->fg_sel, d->bg_sel);
122 else
123 draw_text(ctx, area, d->align,
124 widget->font->height / 2, widget->font, c->name,
125 d->fg, d->bg);
127 if(c->isfloating || c->ismax)
128 draw_circle(ctx, widget->area.x + icon_width + box_width * i,
129 widget->area.y,
130 (widget->font->height + 2) / 4,
131 c->ismax, d->fg);
132 i++;
135 widget->area.width = widget->statusbar->width - used;
136 widget->area.height = widget->statusbar->height;
138 return widget->area.width;
141 static void
142 tasklist_button_press(Widget *widget, XButtonPressedEvent *ev)
144 Button *b;
145 Client *c;
146 Data *d = widget->data;
147 Tag *tag;
148 int n = 0, box_width = 0, i, ci = 0;
150 /* button1 give focus */
151 if(ev->button == Button1 && CLEANMASK(ev->state) == NoSymbol)
153 for(c = globalconf.clients; c; c = c->next)
154 if(ISVISIBLE_ON_TB(c, widget->statusbar->screen, d->show_all))
155 n++;
157 if(!n)
158 return;
160 box_width = widget->area.width / n;
162 if(ev->button == Button1 && CLEANMASK(ev->state) == NoSymbol)
164 switch(widget->statusbar->position)
166 case Top:
167 case Bottom:
168 ci = (ev->x - widget->area.x) / box_width;
169 break;
170 case Right:
171 ci = (ev->y - widget->area.x) / box_width;
172 break;
173 default:
174 ci = ((widget->statusbar->width - ev->y) - widget->area.x) / box_width;
175 break;
177 /* found first visible client */
178 for(c = globalconf.clients;
179 c && !ISVISIBLE_ON_TB(c, widget->statusbar->screen, d->show_all);
180 c = c->next);
181 /* found ci-th visible client */
182 for(i = 0; c ; c = c->next)
183 if(ISVISIBLE_ON_TB(c, widget->statusbar->screen, d->show_all))
184 if(i++ >= ci)
185 break;
187 if(c)
189 /* first switch tag if client not visible */
190 if(!client_isvisible(c, widget->statusbar->screen))
191 for(i = 0, tag = globalconf.screens[c->screen].tags; tag; tag = tag->next, i++)
192 if(is_client_tagged(c, tag))
193 tag_view_only_byindex(c->screen, i);
194 focus(c, True, widget->statusbar->screen);
197 return;
201 for(b = widget->buttons; b; b = b->next)
202 if(ev->button == b->button && CLEANMASK(ev->state) == b->mod && b->func)
204 b->func(widget->statusbar->screen, b->arg);
205 return;
209 Widget *
210 tasklist_new(Statusbar *statusbar, cfg_t *config)
212 Widget *w;
213 Data *d;
214 char *buf;
215 int phys_screen = get_phys_screen(statusbar->screen);
217 w = p_new(Widget, 1);
218 widget_common_new(w, statusbar, config);
219 w->draw = tasklist_draw;
220 w->button_press = tasklist_button_press;
221 w->alignment = AlignFlex;
222 w->data = d = p_new(Data, 1);
224 if((buf = cfg_getstr(config, "fg")))
225 d->fg = initxcolor(phys_screen, buf);
226 else
227 d->fg = globalconf.screens[statusbar->screen].colors_normal[ColFG];
229 if((buf = cfg_getstr(config, "bg")))
230 d->bg = initxcolor(phys_screen, buf);
231 else
232 d->bg = globalconf.screens[statusbar->screen].colors_normal[ColBG];
234 if((buf = cfg_getstr(config, "focus_bg")))
235 d->bg_sel = initxcolor(phys_screen, buf);
236 else
237 d->bg_sel = globalconf.screens[statusbar->screen].colors_selected[ColBG];
239 if((buf = cfg_getstr(config, "focus_fg")))
240 d->fg_sel = initxcolor(phys_screen, buf);
241 else
242 d->fg_sel = globalconf.screens[statusbar->screen].colors_selected[ColFG];
244 d->align = draw_get_align(cfg_getstr(config, "align"));
245 d->show_icons = cfg_getbool(config, "show_icons");
246 d->show_all = cfg_getbool(config, "show_all");
248 if((buf = cfg_getstr(config, "font")))
249 w->font = XftFontOpenName(globalconf.display, get_phys_screen(statusbar->screen), buf);
251 if(!w->font)
252 w->font = globalconf.screens[statusbar->screen].font;
254 /* Set cache property */
255 w->cache.flags = WIDGET_CACHE_CLIENTS;
257 return w;
260 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80