diff options
| author | Anselm R. Garbe <arg@suckless.org> | 2007-02-20 13:40:31 +0100 |
|---|---|---|
| committer | Anselm R. Garbe <arg@suckless.org> | 2007-02-20 13:40:31 +0100 |
| commit | f4d15b1fec52bdd6d512577fd96490efaddb55eb (patch) | |
| tree | 15543a14e188515cdd33297113e5c0d9d2344c09 | |
| parent | rechecked with OpenBSD (diff) | |
| download | dwm-f4d15b1fec52bdd6d512577fd96490efaddb55eb.tar.gz dwm-f4d15b1fec52bdd6d512577fd96490efaddb55eb.zip | |
separating drawsquare from drawtext, made drawtext extern
| -rw-r--r-- | dwm.h | 2 | ||||
| -rw-r--r-- | main.c | 112 |
2 files changed, 65 insertions, 49 deletions
| @@ -131,6 +131,8 @@ extern void versatile(void); /* arranges all windows versatile */ | |||
| 131 | 131 | ||
| 132 | /* main.c */ | 132 | /* main.c */ |
| 133 | extern void drawstatus(void); /* draw the bar */ | 133 | extern void drawstatus(void); /* draw the bar */ |
| 134 | extern void drawtext(const char *text, | ||
| 135 | unsigned long col[ColLast]); /* draw text */ | ||
| 134 | extern unsigned int textw(const char *text); /* return the width of text in px*/ | 136 | extern unsigned int textw(const char *text); /* return the width of text in px*/ |
| 135 | extern void quit(Arg *arg); /* quit dwm nicely */ | 137 | extern void quit(Arg *arg); /* quit dwm nicely */ |
| 136 | extern void sendevent(Window w, Atom a, long value); /* send synthetic event to w */ | 138 | extern void sendevent(Window w, Atom a, long value); /* send synthetic event to w */ |
| @@ -73,57 +73,21 @@ textnw(const char *text, unsigned int len) { | |||
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | static void | 75 | static void |
| 76 | drawtext(const char *text, unsigned long col[ColLast], Bool filledsquare, Bool emptysquare) { | 76 | drawsquare(Bool filled, Bool empty, unsigned long col[ColLast]) { |
| 77 | int x, y, w, h; | 77 | int x; |
| 78 | static char buf[256]; | ||
| 79 | unsigned int len, olen; | ||
| 80 | XGCValues gcv; | 78 | XGCValues gcv; |
| 81 | XRectangle r = { dc.x, dc.y, dc.w, dc.h }; | 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 | gcv.foreground = col[ColFG]; | 81 | gcv.foreground = col[ColFG]; |
| 110 | if(dc.font.set) { | 82 | XChangeGC(dpy, dc.gc, GCForeground, &gcv); |
| 111 | XChangeGC(dpy, dc.gc, GCForeground, &gcv); | 83 | x = (dc.font.ascent + dc.font.descent + 2) / 4; |
| 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; | ||
| 120 | r.x = dc.x + 1; | 84 | r.x = dc.x + 1; |
| 121 | r.y = dc.y + 1; | 85 | r.y = dc.y + 1; |
| 122 | if(filledsquare) { | 86 | if(filled) { |
| 123 | r.width = r.height = x + 1; | 87 | r.width = r.height = x + 1; |
| 124 | XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); | 88 | XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); |
| 125 | } | 89 | } |
| 126 | else if(emptysquare) { | 90 | else if(empty) { |
| 127 | r.width = r.height = x; | 91 | r.width = r.height = x; |
| 128 | XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1); | 92 | XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1); |
| 129 | } | 93 | } |
| @@ -306,14 +270,18 @@ drawstatus(void) { | |||
| 306 | dc.x = dc.y = 0; | 270 | dc.x = dc.y = 0; |
| 307 | for(i = 0; i < ntags; i++) { | 271 | for(i = 0; i < ntags; i++) { |
| 308 | dc.w = textw(tags[i]); | 272 | dc.w = textw(tags[i]); |
| 309 | if(seltag[i]) | 273 | if(seltag[i]) { |
| 310 | drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i)); | 274 | drawtext(tags[i], dc.sel); |
| 311 | else | 275 | drawsquare(sel && sel->tags[i], isoccupied(i), dc.sel); |
| 312 | drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i)); | 276 | } |
| 277 | else { | ||
| 278 | drawtext(tags[i], dc.norm); | ||
| 279 | drawsquare(sel && sel->tags[i], isoccupied(i), dc.norm); | ||
| 280 | } | ||
| 313 | dc.x += dc.w; | 281 | dc.x += dc.w; |
| 314 | } | 282 | } |
| 315 | dc.w = blw; | 283 | dc.w = blw; |
| 316 | drawtext(lt->symbol, dc.norm, False, False); | 284 | drawtext(lt->symbol, dc.norm); |
| 317 | x = dc.x + dc.w; | 285 | x = dc.x + dc.w; |
| 318 | dc.w = textw(stext); | 286 | dc.w = textw(stext); |
| 319 | dc.x = sw - dc.w; | 287 | dc.x = sw - dc.w; |
| @@ -321,16 +289,62 @@ drawstatus(void) { | |||
| 321 | dc.x = x; | 289 | dc.x = x; |
| 322 | dc.w = sw - x; | 290 | dc.w = sw - x; |
| 323 | } | 291 | } |
| 324 | drawtext(stext, dc.norm, False, False); | 292 | drawtext(stext, dc.norm); |
| 325 | if((dc.w = dc.x - x) > bh) { | 293 | if((dc.w = dc.x - x) > bh) { |
| 326 | dc.x = x; | 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 | XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0); | 297 | XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0); |
| 330 | XSync(dpy, False); | 298 | XSync(dpy, False); |
| 331 | } | 299 | } |
| 332 | 300 | ||
| 333 | void | 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 | } | ||
| 345 | } | ||
| 346 | |||
| 347 | void | ||
| 334 | sendevent(Window w, Atom a, long value) { | 348 | sendevent(Window w, Atom a, long value) { |
| 335 | XEvent e; | 349 | XEvent e; |
| 336 | 350 | ||
