typedef enum Position
[awesome.git] / widgets / tasklist.c
blob10aa1f68c7bd691c70560f84bc1fcdd97e7a37f0
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"
33 #define ISVISIBLE_ON_TB(c, screen) (client_isvisible(c, screen) && !c->skiptb)
35 extern AwesomeConf globalconf;
37 typedef struct
39 Bool show_icons;
40 int align;
41 XColor fg_sel;
42 XColor bg_sel;
43 XColor fg;
44 XColor bg;
45 } Data;
47 static int
48 tasklist_draw(Widget *widget, DrawCtx *ctx, int offset, int used)
50 Client *c;
51 Data *d = widget->data;
52 Client *sel = focus_get_current_client(widget->statusbar->screen);
53 Rule *r;
54 Area area;
55 int n = 0, i = 0, box_width = 0, icon_width = 0;
56 NetWMIcon *icon;
58 for(c = globalconf.clients; c; c = c->next)
59 if(ISVISIBLE_ON_TB(c, widget->statusbar->screen))
60 n++;
62 if(!n)
64 widget->width = 0;
65 return widget->width;
68 box_width = (widget->statusbar->width - used) / n;
70 widget->location = widget_calculate_offset(widget->statusbar->width,
72 offset,
73 widget->alignment);
75 for(c = globalconf.clients; c; c = c->next)
76 if(ISVISIBLE_ON_TB(c, widget->statusbar->screen))
78 icon_width = 0;
80 if(d->show_icons)
82 for(r = globalconf.rules; r; r = r->next)
83 if(r->icon && client_match_rule(c, r))
85 area = draw_get_image_size(r->icon);
86 icon_width = ((double) widget->statusbar->height / (double) area.height) * area.width;
87 draw_image(ctx,
88 widget->location + box_width * i,
89 0, widget->statusbar->height,
90 r->icon);
93 if(!icon_width && (icon = ewmh_get_window_icon(c->win)))
95 icon_width = ((double) widget->statusbar->height / (double) icon->height)
96 * icon->width;
97 draw_image_from_argb_data(ctx,
98 widget->location + box_width * i, 0,
99 icon->width, icon->height,
100 widget->statusbar->height, icon->image);
101 p_delete(&icon->image);
102 p_delete(&icon);
106 if(sel == c)
108 draw_text(ctx, widget->location + icon_width + box_width * i, 0,
109 box_width - icon_width,
110 widget->statusbar->height,
111 d->align,
112 widget->font->height / 2, widget->font, c->name,
113 d->fg_sel, d->bg_sel);
115 else
116 draw_text(ctx, widget->location + icon_width + box_width * i, 0,
117 box_width - icon_width,
118 widget->statusbar->height,
119 d->align,
120 widget->font->height / 2, widget->font, c->name,
121 d->fg, d->bg);
122 if(c->isfloating)
123 draw_circle(ctx, widget->location + icon_width + box_width * i, 0,
124 (widget->font->height + 2) / 4,
125 c->ismax, d->fg);
126 i++;
129 widget->width = widget->statusbar->width - used;
131 return widget->width;
134 static void
135 tasklist_button_press(Widget *widget, XButtonPressedEvent *ev)
137 Button *b;
138 Client *c;
139 int n = 0, box_width = 0, i = 0, ci = 0;
141 /* button1 give focus */
142 if(ev->button == Button1 && CLEANMASK(ev->state) == NoSymbol)
144 for(c = globalconf.clients; c; c = c->next)
145 if(ISVISIBLE_ON_TB(c, widget->statusbar->screen))
146 n++;
148 if(!n)
149 return;
151 box_width = widget->width / n;
153 if(ev->button == Button1 && CLEANMASK(ev->state) == NoSymbol)
155 switch(widget->statusbar->position)
157 case Top:
158 case Bottom:
159 ci = (ev->x - widget->location) / box_width;
160 break;
161 case Right:
162 ci = (ev->y - widget->location) / box_width;
163 break;
164 default:
165 ci = ((widget->statusbar->width - ev->y) - widget->location) / box_width;
166 break;
168 /* found first visible client */
169 for(c = globalconf.clients;
170 c && !ISVISIBLE_ON_TB(c, widget->statusbar->screen);
171 c = c->next);
172 /* found ci-th visible client */
173 for(; c && i < ci; c = c->next)
174 if(ISVISIBLE_ON_TB(c, widget->statusbar->screen))
175 i++;
177 focus(c, True, widget->statusbar->screen);
178 return;
182 for(b = widget->buttons; b; b = b->next)
183 if(ev->button == b->button && CLEANMASK(ev->state) == b->mod && b->func)
185 b->func(widget->statusbar->screen, b->arg);
186 return;
190 Widget *
191 tasklist_new(Statusbar *statusbar, cfg_t *config)
193 Widget *w;
194 Data *d;
195 char *buf;
196 int phys_screen = get_phys_screen(statusbar->screen);
198 w = p_new(Widget, 1);
199 widget_common_new(w, statusbar, config);
200 w->draw = tasklist_draw;
201 w->button_press = tasklist_button_press;
202 w->alignment = AlignFlex;
203 w->data = d = p_new(Data, 1);
205 if((buf = cfg_getstr(config, "fg")))
206 d->fg = initxcolor(phys_screen, buf);
207 else
208 d->fg = globalconf.screens[statusbar->screen].colors_normal[ColFG];
210 if((buf = cfg_getstr(config, "bg")))
211 d->bg = initxcolor(phys_screen, buf);
212 else
213 d->bg = globalconf.screens[statusbar->screen].colors_normal[ColBG];
215 if((buf = cfg_getstr(config, "focus_bg")))
216 d->bg_sel = initxcolor(phys_screen, buf);
217 else
218 d->bg_sel = globalconf.screens[statusbar->screen].colors_selected[ColBG];
220 if((buf = cfg_getstr(config, "focus_fg")))
221 d->fg_sel = initxcolor(phys_screen, buf);
222 else
223 d->fg_sel = globalconf.screens[statusbar->screen].colors_selected[ColFG];
225 d->align = draw_get_align(cfg_getstr(config, "align"));
226 d->show_icons = cfg_getbool(config, "show_icons");
228 if((buf = cfg_getstr(config, "font")))
229 w->font = XftFontOpenName(globalconf.display, get_phys_screen(statusbar->screen), buf);
231 if(!w->font)
232 w->font = globalconf.screens[statusbar->screen].font;
234 return w;
237 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80