add show_all option to tasklist
[awesome.git] / widgets / tasklist.c
blobf2e3280d2b038304033cbe22d895e464d1e65c35
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 for(r = globalconf.rules; r; r = r->next)
90 if(r->icon && client_match_rule(c, r))
92 area = draw_get_image_size(r->icon);
93 icon_width = ((double) widget->statusbar->height / (double) area.height) * area.width;
94 draw_image(ctx,
95 widget->area.x + box_width * i,
96 widget->area.y,
97 widget->statusbar->height,
98 r->icon);
101 if(!icon_width && (icon = ewmh_get_window_icon(c->win)))
103 icon_width = ((double) widget->statusbar->height / (double) icon->height)
104 * icon->width;
105 draw_image_from_argb_data(ctx,
106 widget->area.x + box_width * i,
107 widget->area.y,
108 icon->width, icon->height,
109 widget->statusbar->height, icon->image);
110 p_delete(&icon->image);
111 p_delete(&icon);
115 if(sel == c)
117 draw_text(ctx, widget->area.x + icon_width + box_width * i,
118 widget->area.y,
119 box_width - icon_width,
120 widget->statusbar->height,
121 d->align,
122 widget->font->height / 2, widget->font, c->name,
123 d->fg_sel, d->bg_sel);
125 else
126 draw_text(ctx, widget->area.x + icon_width + box_width * i,
127 widget->area.y,
128 box_width - icon_width,
129 widget->statusbar->height,
130 d->align,
131 widget->font->height / 2, widget->font, c->name,
132 d->fg, d->bg);
133 if(c->isfloating)
134 draw_circle(ctx, widget->area.x + icon_width + box_width * i,
135 widget->area.y,
136 (widget->font->height + 2) / 4,
137 c->ismax, d->fg);
138 i++;
141 widget->area.width = widget->statusbar->width - used;
142 widget->area.height = widget->statusbar->height;
144 return widget->area.width;
147 static void
148 tasklist_button_press(Widget *widget, XButtonPressedEvent *ev)
150 Button *b;
151 Client *c;
152 Data *d = widget->data;
153 Tag *tag;
154 int n = 0, box_width = 0, i = 0, ci = 0;
156 /* button1 give focus */
157 if(ev->button == Button1 && CLEANMASK(ev->state) == NoSymbol)
159 for(c = globalconf.clients; c; c = c->next)
160 if(ISVISIBLE_ON_TB(c, widget->statusbar->screen, d->show_all))
161 n++;
163 if(!n)
164 return;
166 box_width = widget->area.width / n;
168 if(ev->button == Button1 && CLEANMASK(ev->state) == NoSymbol)
170 switch(widget->statusbar->position)
172 case Top:
173 case Bottom:
174 ci = (ev->x - widget->area.x) / box_width;
175 break;
176 case Right:
177 ci = (ev->y - widget->area.x) / box_width;
178 break;
179 default:
180 ci = ((widget->statusbar->width - ev->y) - widget->area.x) / box_width;
181 break;
183 /* found first visible client */
184 for(c = globalconf.clients;
185 c && !ISVISIBLE_ON_TB(c, widget->statusbar->screen, d->show_all);
186 c = c->next);
187 /* found ci-th visible client */
188 for(; c && i < ci; c = c->next)
189 if(ISVISIBLE_ON_TB(c, widget->statusbar->screen, d->show_all))
190 i++;
192 /* first switch tag if client not visible */
193 if(!client_isvisible(c, widget->statusbar->screen))
194 for(i = 0, tag = globalconf.screens[c->screen].tags; tag; tag = tag->next, i++)
195 if(is_client_tagged(c, tag))
196 tag_view(c->screen, i);
197 focus(c, True, widget->statusbar->screen);
199 return;
203 for(b = widget->buttons; b; b = b->next)
204 if(ev->button == b->button && CLEANMASK(ev->state) == b->mod && b->func)
206 b->func(widget->statusbar->screen, b->arg);
207 return;
211 Widget *
212 tasklist_new(Statusbar *statusbar, cfg_t *config)
214 Widget *w;
215 Data *d;
216 char *buf;
217 int phys_screen = get_phys_screen(statusbar->screen);
219 w = p_new(Widget, 1);
220 widget_common_new(w, statusbar, config);
221 w->draw = tasklist_draw;
222 w->button_press = tasklist_button_press;
223 w->alignment = AlignFlex;
224 w->data = d = p_new(Data, 1);
226 if((buf = cfg_getstr(config, "fg")))
227 d->fg = initxcolor(phys_screen, buf);
228 else
229 d->fg = globalconf.screens[statusbar->screen].colors_normal[ColFG];
231 if((buf = cfg_getstr(config, "bg")))
232 d->bg = initxcolor(phys_screen, buf);
233 else
234 d->bg = globalconf.screens[statusbar->screen].colors_normal[ColBG];
236 if((buf = cfg_getstr(config, "focus_bg")))
237 d->bg_sel = initxcolor(phys_screen, buf);
238 else
239 d->bg_sel = globalconf.screens[statusbar->screen].colors_selected[ColBG];
241 if((buf = cfg_getstr(config, "focus_fg")))
242 d->fg_sel = initxcolor(phys_screen, buf);
243 else
244 d->fg_sel = globalconf.screens[statusbar->screen].colors_selected[ColFG];
246 d->align = draw_get_align(cfg_getstr(config, "align"));
247 d->show_icons = cfg_getbool(config, "show_icons");
248 d->show_all = cfg_getbool(config, "show_all");
250 if((buf = cfg_getstr(config, "font")))
251 w->font = XftFontOpenName(globalconf.display, get_phys_screen(statusbar->screen), buf);
253 if(!w->font)
254 w->font = globalconf.screens[statusbar->screen].font;
256 return w;
259 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80