remove bstack layout
[awesome.git] / draw.c
blob5ee5af804d7286ba440d4934acaf09b07d142db2
1 /*
2 * draw.c - draw functions
3 *
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
5 *
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 "layout.h"
24 extern Client *clients, *sel, *stack; /* global client list and stack */
26 /* static */
28 static void
29 drawtext(Display *disp, DC drawcontext, const char *text, unsigned long col[ColLast])
31 int x, y, w, h;
32 static char buf[256];
33 unsigned int len, olen;
34 XRectangle r = { drawcontext.x, drawcontext.y, drawcontext.w, drawcontext.h };
36 XSetForeground(disp, drawcontext.gc, col[ColBG]);
37 XFillRectangles(disp, drawcontext.drawable, drawcontext.gc, &r, 1);
38 if(!text)
39 return;
40 w = 0;
41 olen = len = a_strlen(text);
42 if(len >= sizeof buf)
43 len = sizeof buf - 1;
44 memcpy(buf, text, len);
45 buf[len] = 0;
46 h = drawcontext.font.ascent + drawcontext.font.descent;
47 y = drawcontext.y + (drawcontext.h / 2) - (h / 2) + drawcontext.font.ascent;
48 x = drawcontext.x + (h / 2);
49 /* shorten text if necessary */
50 while(len && (w = textnw(drawcontext.font.set, drawcontext.font.xfont, buf, len)) > drawcontext.w - h)
51 buf[--len] = 0;
52 if(w > drawcontext.w)
53 return; /* too long */
54 if(len < olen)
56 if(len > 1)
57 buf[len - 1] = '.';
58 if(len > 2)
59 buf[len - 2] = '.';
60 if(len > 3)
61 buf[len - 3] = '.';
63 XSetForeground(disp, drawcontext.gc, col[ColFG]);
64 if(drawcontext.font.set)
65 XmbDrawString(disp, drawcontext.drawable, drawcontext.font.set, drawcontext.gc, x, y, buf, len);
66 else
67 XDrawString(disp, drawcontext.drawable, drawcontext.gc, x, y, buf, len);
70 static void
71 drawsquare(Bool filled, Bool empty, unsigned long col[ColLast], Display *disp, DC drawcontext)
73 int x;
74 XGCValues gcv;
75 XRectangle r = { drawcontext.x, drawcontext.y, drawcontext.w, drawcontext.h };
77 gcv.foreground = col[ColFG];
78 XChangeGC(disp, drawcontext.gc, GCForeground, &gcv);
79 x = (drawcontext.font.ascent + drawcontext.font.descent + 2) / 4;
80 r.x = drawcontext.x + 1;
81 r.y = drawcontext.y + 1;
82 if(filled)
84 r.width = r.height = x + 1;
85 XFillRectangles(disp, drawcontext.drawable, drawcontext.gc, &r, 1);
87 else if(empty)
89 r.width = r.height = x;
90 XDrawRectangles(disp, drawcontext.drawable, drawcontext.gc, &r, 1);
94 /** Check if at least a client is tagged with tag number t
95 * \param t tag number
96 * \return True or False
98 static Bool
99 isoccupied(unsigned int t)
101 Client *c;
103 for(c = clients; c; c = c->next)
104 if(c->tags[t])
105 return True;
106 return False;
110 /* extern */
112 unsigned int
113 textnw(XFontSet set, XFontStruct *xfont, const char *text, unsigned int len)
115 XRectangle r;
117 if(set)
119 XmbTextExtents(set, text, len, NULL, &r);
120 return r.width;
122 return XTextWidth(xfont, text, len);
125 void
126 drawstatus(Display *disp, DC *drawcontext, awesome_config * awesomeconf)
128 int x, i;
129 drawcontext->x = drawcontext->y = 0;
130 for(i = 0; i < awesomeconf->ntags; i++)
132 drawcontext->w = textw(drawcontext->font.set, drawcontext->font.xfont, awesomeconf->tags[i], drawcontext->font.height);
133 if(awesomeconf->selected_tags[i])
135 drawtext(disp, *drawcontext, awesomeconf->tags[i], drawcontext->sel);
136 drawsquare(sel && sel->tags[i], isoccupied(i), drawcontext->sel, disp, *drawcontext);
138 else
140 drawtext(disp, *drawcontext, awesomeconf->tags[i], drawcontext->norm);
141 drawsquare(sel && sel->tags[i], isoccupied(i), drawcontext->norm, disp, *drawcontext);
143 drawcontext->x += drawcontext->w;
145 drawcontext->w = awesomeconf->statusbar.width;
146 drawtext(disp, *drawcontext, awesomeconf->current_layout->symbol, drawcontext->norm);
147 x = drawcontext->x + drawcontext->w;
148 drawcontext->w = textw(drawcontext->font.set, drawcontext->font.xfont, awesomeconf->statustext, drawcontext->font.height);
149 drawcontext->x = DisplayWidth(disp, DefaultScreen(disp)) - drawcontext->w;
150 if(drawcontext->x < x)
152 drawcontext->x = x;
153 drawcontext->w = DisplayWidth(disp, DefaultScreen(disp)) - x;
155 drawtext(disp, *drawcontext, awesomeconf->statustext, drawcontext->norm);
156 if((drawcontext->w = drawcontext->x - x) > awesomeconf->statusbar.height)
158 drawcontext->x = x;
159 if(sel)
161 drawtext(disp, *drawcontext, sel->name, drawcontext->sel);
162 drawsquare(sel->ismax, sel->isfloating, drawcontext->sel, disp, *drawcontext);
164 else
165 drawtext(disp, *drawcontext, NULL, drawcontext->norm);
167 XCopyArea(disp, drawcontext->drawable, awesomeconf->statusbar.window, drawcontext->gc, 0, 0, DisplayWidth(disp, DefaultScreen(disp)), awesomeconf->statusbar.height, 0, 0);
168 XSync(disp, False);