Repository navigation
Expand file tree
/
Copy pathtaglist.c
More file actions
217 lines (192 loc) · 6.87 KB
/
Copy pathtaglist.c
File metadata and controls
217 lines (192 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* taglist.c - tag list widget
*
* Copyright © 2007 Aldo Cortesi <aldo@nullcube.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "config.h"
#include "client.h"
#include "widget.h"
#include "tag.h"
#include "event.h"
#include "common/util.h"
#include "common/configopts.h"
extern AwesomeConf globalconf;
/** Check if at least one client is tagged with tag number t and is on screen
* screen
* \param screen screen number
* \param t tag
* \return True or False
*/
static Bool
isoccupied(Tag *t)
{
Client *c;
for(c = globalconf.clients; c; c = c->next)
if(is_client_tagged(c, t) && !c->skip && c != globalconf.scratch.client)
return True;
return False;
}
static Bool
isurgent(Tag *t)
{
Client *c;
for(c = globalconf.clients; c; c = c->next)
if(is_client_tagged(c, t) && c->isurgent)
return True;
return False;
}
static style_t
taglist_style_get(VirtScreen vscreen, Tag *tag)
{
if(tag->selected)
return vscreen.styles.focus;
else if(isurgent(tag))
return vscreen.styles.urgent;
return vscreen.styles.normal;
}
static int
taglist_draw(Widget *widget,
DrawCtx *ctx,
int offset,
int used __attribute__ ((unused)))
{
Tag *tag;
Client *sel = globalconf.focus->client;
VirtScreen vscreen = globalconf.screens[widget->statusbar->screen];
int w = 0, flagsize;
style_t style;
area_t area;
flagsize = (vscreen.styles.normal.font->height + 2) / 3;
widget->area.width = 0;
for(tag = vscreen.tags; tag; tag = tag->next)
{
style = tag->selected ? vscreen.styles.focus : vscreen.styles.normal;
widget->area.width += draw_textwidth(ctx->display, style.font, tag->name)
+ style.font->height;
}
if(!widget->user_supplied_x)
widget->area.x = widget_calculate_offset(widget->statusbar->width,
widget->area.width,
offset,
widget->alignment);
if(!widget->user_supplied_y)
widget->area.y = 0;
widget->area.width = 0;
for(tag = vscreen.tags; tag; tag = tag->next)
{
style = taglist_style_get(vscreen, tag);
w = draw_textwidth(ctx->display, style.font, tag->name) + style.font->height;
area.x = widget->area.x + widget->area.width;
area.y = widget->area.y;
area.width = w;
area.height = widget->statusbar->height;
draw_text(ctx, area,
AlignCenter,
style.font->height / 2,
tag->name,
style);
if(isoccupied(tag))
{
area_t rectangle = { widget->area.x + widget->area.width,
widget->area.y,
flagsize,
flagsize,
NULL, NULL };
draw_rectangle(ctx, rectangle, 1.0, sel && is_client_tagged(sel, tag), style.fg);
}
widget->area.width += w;
}
widget->area.height = widget->statusbar->height;
return widget->area.width;
}
static void
taglist_button_press(Widget *widget, XButtonPressedEvent *ev)
{
VirtScreen vscreen = globalconf.screens[widget->statusbar->screen];
Button *b;
Tag *tag;
char buf[4];
int prev_width = 0, width = 0, i = 1;
style_t style;
for(b = widget->buttons; b; b = b->next)
if(ev->button == b->button && CLEANMASK(ev->state) == b->mod && b->func)
switch(widget->statusbar->position)
{
case Top:
case Bottom:
for(tag = vscreen.tags; tag; tag = tag->next, i++)
{
style = taglist_style_get(vscreen, tag);
width = draw_textwidth(globalconf.display, style.font, tag->name)
+ style.font->height;
if(ev->x >= widget->area.x + prev_width
&& ev->x < widget->area.x + prev_width + width)
{
snprintf(buf, sizeof(buf), "%d", i);
b->func(widget->statusbar->screen, buf);
return;
}
prev_width += width;
}
break;
case Right:
for(tag = vscreen.tags; tag; tag = tag->next, i++)
{
style = taglist_style_get(vscreen, tag);
width = draw_textwidth(globalconf.display, style.font, tag->name) + style.font->height;
if(ev->y >= widget->area.x + prev_width
&& ev->y < widget->area.x + prev_width + width)
{
snprintf(buf, sizeof(buf), "%d", i);
b->func(widget->statusbar->screen, buf);
return;
}
prev_width += width;
}
break;
default:
for(tag = vscreen.tags; tag; tag = tag->next, i++)
{
style = taglist_style_get(vscreen, tag);
width = draw_textwidth(globalconf.display, style.font, tag->name) + style.font->height;
if(widget->statusbar->width - ev->y >= widget->area.x + prev_width
&& widget->statusbar->width - ev->y < widget->area.x + prev_width + width)
{
snprintf(buf, sizeof(buf), "%d", i);
b->func(widget->statusbar->screen, buf);
return;
}
prev_width += width;
}
break;
}
}
Widget *
taglist_new(Statusbar *statusbar, cfg_t *config)
{
Widget *w;
w = p_new(Widget, 1);
widget_common_new(w, statusbar, config);
w->draw = taglist_draw;
w->button_press = taglist_button_press;
w->alignment = cfg_getalignment(config, "align");
/* Set cache property */
w->cache.flags = WIDGET_CACHE_TAGS | WIDGET_CACHE_CLIENTS;
return w;
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80