separating drawsquare from drawtext, made drawtext extern
f4d15b1f
2 file(s) · +65 −49
| 131 | 131 | ||
| 132 | 132 | /* main.c */ |
|
| 133 | 133 | extern void drawstatus(void); /* draw the bar */ |
|
| 134 | + | extern void drawtext(const char *text, |
|
| 135 | + | unsigned long col[ColLast]); /* draw text */ |
|
| 134 | 136 | extern unsigned int textw(const char *text); /* return the width of text in px*/ |
|
| 135 | 137 | extern void quit(Arg *arg); /* quit dwm nicely */ |
|
| 136 | 138 | extern void sendevent(Window w, Atom a, long value); /* send synthetic event to w */ |
| 73 | 73 | } |
|
| 74 | 74 | ||
| 75 | 75 | static void |
|
| 76 | - | drawtext(const char *text, unsigned long col[ColLast], Bool filledsquare, Bool emptysquare) { |
|
| 77 | - | int x, y, w, h; |
|
| 78 | - | static char buf[256]; |
|
| 79 | - | unsigned int len, olen; |
|
| 76 | + | drawsquare(Bool filled, Bool empty, unsigned long col[ColLast]) { |
|
| 77 | + | int x; |
|
| 80 | 78 | XGCValues gcv; |
|
| 81 | 79 | XRectangle r = { dc.x, dc.y, dc.w, dc.h }; |
|
| 82 | 80 | ||
| 83 | - | XSetForeground(dpy, dc.gc, col[ColBG]); |
|
| 84 | - | XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); |
|
| 85 | - | if(!text) |
|
| 86 | - | return; |
|
| 87 | - | w = 0; |
|
| 88 | - | olen = len = strlen(text); |
|
| 89 | - | if(len >= sizeof buf) |
|
| 90 | - | len = sizeof buf - 1; |
|
| 91 | - | memcpy(buf, text, len); |
|
| 92 | - | buf[len] = 0; |
|
| 93 | - | h = dc.font.ascent + dc.font.descent; |
|
| 94 | - | y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; |
|
| 95 | - | x = dc.x + (h / 2); |
|
| 96 | - | /* shorten text if necessary */ |
|
| 97 | - | while(len && (w = textnw(buf, len)) > dc.w - h) |
|
| 98 | - | buf[--len] = 0; |
|
| 99 | - | if(len < olen) { |
|
| 100 | - | if(len > 1) |
|
| 101 | - | buf[len - 1] = '.'; |
|
| 102 | - | if(len > 2) |
|
| 103 | - | buf[len - 2] = '.'; |
|
| 104 | - | if(len > 3) |
|
| 105 | - | buf[len - 3] = '.'; |
|
| 106 | - | } |
|
| 107 | - | if(w > dc.w) |
|
| 108 | - | return; /* too long */ |
|
| 109 | 81 | gcv.foreground = col[ColFG]; |
|
| 110 | - | if(dc.font.set) { |
|
| 111 | - | XChangeGC(dpy, dc.gc, GCForeground, &gcv); |
|
| 112 | - | XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len); |
|
| 113 | - | } |
|
| 114 | - | else { |
|
| 115 | - | gcv.font = dc.font.xfont->fid; |
|
| 116 | - | XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv); |
|
| 117 | - | XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len); |
|
| 118 | - | } |
|
| 119 | - | x = (h + 2) / 4; |
|
| 82 | + | XChangeGC(dpy, dc.gc, GCForeground, &gcv); |
|
| 83 | + | x = (dc.font.ascent + dc.font.descent + 2) / 4; |
|
| 120 | 84 | r.x = dc.x + 1; |
|
| 121 | 85 | r.y = dc.y + 1; |
|
| 122 | - | if(filledsquare) { |
|
| 86 | + | if(filled) { |
|
| 123 | 87 | r.width = r.height = x + 1; |
|
| 124 | 88 | XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); |
|
| 125 | 89 | } |
|
| 126 | - | else if(emptysquare) { |
|
| 90 | + | else if(empty) { |
|
| 127 | 91 | r.width = r.height = x; |
|
| 128 | 92 | XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1); |
|
| 129 | 93 | } |
|
| 306 | 270 | dc.x = dc.y = 0; |
|
| 307 | 271 | for(i = 0; i < ntags; i++) { |
|
| 308 | 272 | dc.w = textw(tags[i]); |
|
| 309 | - | if(seltag[i]) |
|
| 310 | - | drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i)); |
|
| 311 | - | else |
|
| 312 | - | drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i)); |
|
| 273 | + | if(seltag[i]) { |
|
| 274 | + | drawtext(tags[i], dc.sel); |
|
| 275 | + | drawsquare(sel && sel->tags[i], isoccupied(i), dc.sel); |
|
| 276 | + | } |
|
| 277 | + | else { |
|
| 278 | + | drawtext(tags[i], dc.norm); |
|
| 279 | + | drawsquare(sel && sel->tags[i], isoccupied(i), dc.norm); |
|
| 280 | + | } |
|
| 313 | 281 | dc.x += dc.w; |
|
| 314 | 282 | } |
|
| 315 | 283 | dc.w = blw; |
|
| 316 | - | drawtext(lt->symbol, dc.norm, False, False); |
|
| 284 | + | drawtext(lt->symbol, dc.norm); |
|
| 317 | 285 | x = dc.x + dc.w; |
|
| 318 | 286 | dc.w = textw(stext); |
|
| 319 | 287 | dc.x = sw - dc.w; |
|
| 321 | 289 | dc.x = x; |
|
| 322 | 290 | dc.w = sw - x; |
|
| 323 | 291 | } |
|
| 324 | - | drawtext(stext, dc.norm, False, False); |
|
| 292 | + | drawtext(stext, dc.norm); |
|
| 325 | 293 | if((dc.w = dc.x - x) > bh) { |
|
| 326 | 294 | dc.x = x; |
|
| 327 | - | drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False); |
|
| 295 | + | drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm); |
|
| 328 | 296 | } |
|
| 329 | 297 | XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0); |
|
| 330 | 298 | XSync(dpy, False); |
|
| 299 | + | } |
|
| 300 | + | ||
| 301 | + | void |
|
| 302 | + | drawtext(const char *text, unsigned long col[ColLast]) { |
|
| 303 | + | int x, y, w, h; |
|
| 304 | + | static char buf[256]; |
|
| 305 | + | unsigned int len, olen; |
|
| 306 | + | XGCValues gcv; |
|
| 307 | + | XRectangle r = { dc.x, dc.y, dc.w, dc.h }; |
|
| 308 | + | ||
| 309 | + | XSetForeground(dpy, dc.gc, col[ColBG]); |
|
| 310 | + | XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); |
|
| 311 | + | if(!text) |
|
| 312 | + | return; |
|
| 313 | + | w = 0; |
|
| 314 | + | olen = len = strlen(text); |
|
| 315 | + | if(len >= sizeof buf) |
|
| 316 | + | len = sizeof buf - 1; |
|
| 317 | + | memcpy(buf, text, len); |
|
| 318 | + | buf[len] = 0; |
|
| 319 | + | h = dc.font.ascent + dc.font.descent; |
|
| 320 | + | y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; |
|
| 321 | + | x = dc.x + (h / 2); |
|
| 322 | + | /* shorten text if necessary */ |
|
| 323 | + | while(len && (w = textnw(buf, len)) > dc.w - h) |
|
| 324 | + | buf[--len] = 0; |
|
| 325 | + | if(len < olen) { |
|
| 326 | + | if(len > 1) |
|
| 327 | + | buf[len - 1] = '.'; |
|
| 328 | + | if(len > 2) |
|
| 329 | + | buf[len - 2] = '.'; |
|
| 330 | + | if(len > 3) |
|
| 331 | + | buf[len - 3] = '.'; |
|
| 332 | + | } |
|
| 333 | + | if(w > dc.w) |
|
| 334 | + | return; /* too long */ |
|
| 335 | + | gcv.foreground = col[ColFG]; |
|
| 336 | + | if(dc.font.set) { |
|
| 337 | + | XChangeGC(dpy, dc.gc, GCForeground, &gcv); |
|
| 338 | + | XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len); |
|
| 339 | + | } |
|
| 340 | + | else { |
|
| 341 | + | gcv.font = dc.font.xfont->fid; |
|
| 342 | + | XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv); |
|
| 343 | + | XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len); |
|
| 344 | + | } |
|
| 331 | 345 | } |
|
| 332 | 346 | ||
| 333 | 347 | void |
|