| @@ -42,13 +42,16 @@ static GC gc; |
| 42 | 42 | | |
| 43 | 43 | | Atom wm_delete_win; |
| 44 | 44 | | |
| 45 | | − | struct { |
| 45 | + | static struct { |
| 46 | 46 | | int ascent; |
| 47 | 47 | | int descent; |
| 48 | 48 | | XFontStruct *xfont; |
| 49 | 49 | | XFontSet set; |
| 50 | 50 | | } font; |
| 51 | 51 | | |
| 52 | + | static int fontheight; |
| 53 | + | static int barheight; |
| 54 | + | |
| 52 | 55 | | void win_init_font(Display *dpy, const char *fontstr) { |
| 53 | 56 | | int n; |
| 54 | 57 | | char *def, **missing; |
| @@ -77,6 +80,8 @@ void win_init_font(Display *dpy, const char *fontstr) { |
| 77 | 80 | | font.ascent = font.xfont->ascent; |
| 78 | 81 | | font.descent = font.xfont->descent; |
| 79 | 82 | | } |
| 83 | + | fontheight = font.ascent + font.descent; |
| 84 | + | barheight = fontheight + 2 * V_TEXT_PAD; |
| 80 | 85 | | } |
| 81 | 86 | | |
| 82 | 87 | | unsigned long win_alloc_color(win_t *win, const char *name) { |
| @@ -85,8 +90,8 @@ unsigned long win_alloc_color(win_t *win, const char *name) { |
| 85 | 90 | | if (win == NULL) |
| 86 | 91 | | return 0UL; |
| 87 | 92 | | if (XAllocNamedColor(win->env.dpy, |
| 88 | | − | DefaultColormap(win->env.dpy, win->env.scr), |
| 89 | | − | name, &col, &col) == 0) |
| 93 | + | DefaultColormap(win->env.dpy, win->env.scr), |
| 94 | + | name, &col, &col) == 0) |
| 90 | 95 | | { |
| 91 | 96 | | die("could not allocate color: %s", name); |
| 92 | 97 | | } |
| @@ -99,6 +104,8 @@ void win_init(win_t *win) { |
| 99 | 104 | | if (win == NULL) |
| 100 | 105 | | return; |
| 101 | 106 | | |
| 107 | + | memset(win, 0, sizeof(win_t)); |
| 108 | + | |
| 102 | 109 | | e = &win->env; |
| 103 | 110 | | if ((e->dpy = XOpenDisplay(NULL)) == NULL) |
| 104 | 111 | | die("could not open display"); |
| @@ -110,19 +117,12 @@ void win_init(win_t *win) { |
| 110 | 117 | | e->cmap = DefaultColormap(e->dpy, e->scr); |
| 111 | 118 | | e->depth = DefaultDepth(e->dpy, e->scr); |
| 112 | 119 | | |
| 113 | | − | win->white = WhitePixel(e->dpy, e->scr); |
| 114 | | − | win->bgcol = win_alloc_color(win, WIN_BG_COLOR); |
| 115 | | − | win->fscol = win_alloc_color(win, WIN_FS_COLOR); |
| 116 | | − | win->selcol = win_alloc_color(win, SEL_COLOR); |
| 117 | | − | win->barbgcol = win_alloc_color(win, BAR_BG_COLOR); |
| 118 | | − | win->barfgcol = win_alloc_color(win, BAR_FG_COLOR); |
| 119 | | − | |
| 120 | | − | win->xwin = 0; |
| 121 | | − | win->pm = 0; |
| 122 | | − | win->fullscreen = false; |
| 123 | | − | win->barh = 0; |
| 124 | | − | win->lbar = NULL; |
| 125 | | − | win->rbar = NULL; |
| 120 | + | win->white = WhitePixel(e->dpy, e->scr); |
| 121 | + | win->bgcol = win_alloc_color(win, WIN_BG_COLOR); |
| 122 | + | win->fscol = win_alloc_color(win, WIN_FS_COLOR); |
| 123 | + | win->selcol = win_alloc_color(win, SEL_COLOR); |
| 124 | + | win->bar.bgcol = win_alloc_color(win, BAR_BG_COLOR); |
| 125 | + | win->bar.fgcol = win_alloc_color(win, BAR_FG_COLOR); |
| 126 | 126 | | |
| 127 | 127 | | if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0) |
| 128 | 128 | | warn("no locale support"); |
| @@ -141,8 +141,8 @@ void win_set_sizehints(win_t *win) { |
| 141 | 141 | | sizehints.flags = PMinSize | PMaxSize; |
| 142 | 142 | | sizehints.min_width = win->w; |
| 143 | 143 | | sizehints.max_width = win->w; |
| 144 | | − | sizehints.min_height = win->h + win->barh; |
| 145 | | − | sizehints.max_height = win->h + win->barh; |
| 144 | + | sizehints.min_height = win->h + win->bar.h; |
| 145 | + | sizehints.max_height = win->h + win->bar.h; |
| 146 | 146 | | XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints); |
| 147 | 147 | | } |
| 148 | 148 | | |
| @@ -215,8 +215,8 @@ void win_open(win_t *win) { |
| 215 | 215 | | XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1); |
| 216 | 216 | | |
| 217 | 217 | | if (!options->hide_bar) { |
| 218 | | − | win->barh = font.ascent + font.descent + 2 * V_TEXT_PAD; |
| 219 | | − | win->h -= win->barh; |
| 218 | + | win->bar.h = barheight; |
| 219 | + | win->h -= win->bar.h; |
| 220 | 220 | | } |
| 221 | 221 | | |
| 222 | 222 | | if (options->fixed_win) |
| @@ -249,8 +249,8 @@ bool win_configure(win_t *win, XConfigureEvent *c) { |
| 249 | 249 | | |
| 250 | 250 | | if (win == NULL || c == NULL) |
| 251 | 251 | | return false; |
| 252 | | − | |
| 253 | | − | if ((changed = win->w != c->width || win->h + win->barh != c->height)) { |
| 252 | + | |
| 253 | + | if ((changed = win->w != c->width || win->h + win->bar.h != c->height)) { |
| 254 | 254 | | if (win->pm != None) { |
| 255 | 255 | | XFreePixmap(win->env.dpy, win->pm); |
| 256 | 256 | | win->pm = None; |
| @@ -260,7 +260,7 @@ bool win_configure(win_t *win, XConfigureEvent *c) { |
| 260 | 260 | | win->x = c->x; |
| 261 | 261 | | win->y = c->y; |
| 262 | 262 | | win->w = c->width; |
| 263 | | − | win->h = c->height - win->barh; |
| 263 | + | win->h = c->height - win->bar.h; |
| 264 | 264 | | win->bw = c->border_width; |
| 265 | 265 | | |
| 266 | 266 | | return changed; |
| @@ -283,13 +283,13 @@ bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) { |
| 283 | 283 | | w = MIN(w, win->env.scrw - 2 * win->bw); |
| 284 | 284 | | h = MIN(h, win->env.scrh - 2 * win->bw); |
| 285 | 285 | | |
| 286 | | − | if (win->x == x && win->y == y && win->w == w && win->h + win->barh == h) |
| 286 | + | if (win->x == x && win->y == y && win->w == w && win->h + win->bar.h == h) |
| 287 | 287 | | return false; |
| 288 | 288 | | |
| 289 | 289 | | win->x = x; |
| 290 | 290 | | win->y = y; |
| 291 | 291 | | win->w = w; |
| 292 | | − | win->h = h - win->barh; |
| 292 | + | win->h = h - win->bar.h; |
| 293 | 293 | | |
| 294 | 294 | | if (options->fixed_win) |
| 295 | 295 | | win_set_sizehints(win); |
| @@ -327,12 +327,12 @@ void win_toggle_bar(win_t *win) { |
| 327 | 327 | | if (win == NULL || win->xwin == None) |
| 328 | 328 | | return; |
| 329 | 329 | | |
| 330 | | − | if (win->barh != 0) { |
| 331 | | − | win->h += win->barh; |
| 332 | | − | win->barh = 0; |
| 330 | + | if (win->bar.h != 0) { |
| 331 | + | win->h += win->bar.h; |
| 332 | + | win->bar.h = 0; |
| 333 | 333 | | } else { |
| 334 | | − | win->barh = font.ascent + font.descent + 2 * V_TEXT_PAD; |
| 335 | | − | win->h -= win->barh; |
| 334 | + | win->bar.h = barheight; |
| 335 | + | win->h -= win->bar.h; |
| 336 | 336 | | } |
| 337 | 337 | | } |
| 338 | 338 | | |
| @@ -343,7 +343,7 @@ void win_clear(win_t *win) { |
| 343 | 343 | | if (win == NULL || win->xwin == None) |
| 344 | 344 | | return; |
| 345 | 345 | | |
| 346 | | − | h = win->h + win->barh; |
| 346 | + | h = win->h + win->bar.h; |
| 347 | 347 | | e = &win->env; |
| 348 | 348 | | |
| 349 | 349 | | if (win->pm == None) |
| @@ -354,53 +354,56 @@ void win_clear(win_t *win) { |
| 354 | 354 | | } |
| 355 | 355 | | |
| 356 | 356 | | void win_draw_bar(win_t *win) { |
| 357 | + | int len, olen, x, y, w, tw; |
| 358 | + | char rest[3]; |
| 359 | + | const char *dots = "..."; |
| 357 | 360 | | win_env_t *e; |
| 358 | | − | int len, x, y, w, tw = 0, seplen; |
| 359 | | − | const char *rt; |
| 360 | 361 | | |
| 361 | 362 | | if (win == NULL || win->xwin == None || win->pm == None) |
| 362 | 363 | | return; |
| 363 | 364 | | |
| 364 | 365 | | e = &win->env; |
| 365 | | − | x = H_TEXT_PAD; |
| 366 | 366 | | y = win->h + font.ascent + V_TEXT_PAD; |
| 367 | | − | w = win->w - 2 * H_TEXT_PAD; |
| 367 | + | w = win->w; |
| 368 | 368 | | |
| 369 | | − | XSetForeground(e->dpy, gc, win->barbgcol); |
| 370 | | − | XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->barh); |
| 369 | + | XSetForeground(e->dpy, gc, win->bar.bgcol); |
| 370 | + | XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->bar.h); |
| 371 | 371 | | |
| 372 | | − | XSetForeground(e->dpy, gc, win->barfgcol); |
| 373 | | − | XSetBackground(e->dpy, gc, win->barbgcol); |
| 372 | + | XSetForeground(e->dpy, gc, win->bar.fgcol); |
| 373 | + | XSetBackground(e->dpy, gc, win->bar.bgcol); |
| 374 | 374 | | |
| 375 | | − | if (win->lbar != NULL) { |
| 376 | | − | len = strlen(win->lbar); |
| 377 | | − | while (len > 0 && (tw = win_textwidth(win->lbar, len, false)) > w) |
| 378 | | − | len--; |
| 379 | | − | w -= tw + 2 * H_TEXT_PAD; |
| 380 | | − | if (font.set) |
| 381 | | − | XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->lbar, len); |
| 382 | | − | else |
| 383 | | − | XDrawString(e->dpy, win->pm, gc, x, y, win->lbar, len); |
| 384 | | − | } |
| 385 | | − | if (win->rbar != NULL) { |
| 386 | | − | len = strlen(win->rbar); |
| 387 | | − | seplen = strlen(BAR_SEPARATOR); |
| 388 | | − | rt = win->rbar; |
| 389 | | − | while (len > 0 && (tw = win_textwidth(rt, len, false)) > w) { |
| 390 | | − | rt = strstr(rt, BAR_SEPARATOR); |
| 391 | | − | if (rt != NULL) { |
| 392 | | − | rt += seplen; |
| 393 | | − | len = strlen(rt); |
| 394 | | − | } else { |
| 395 | | − | len = 0; |
| 396 | | − | } |
| 375 | + | if (win->bar.r != NULL) { |
| 376 | + | len = strlen(win->bar.r); |
| 377 | + | if (len > 0) { |
| 378 | + | if ((tw = win_textwidth(win->bar.r, len, true)) > w) |
| 379 | + | return; |
| 380 | + | x = win->w - tw + H_TEXT_PAD; |
| 381 | + | w -= tw; |
| 382 | + | if (font.set) |
| 383 | + | XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len); |
| 384 | + | else |
| 385 | + | XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len); |
| 397 | 386 | | } |
| 387 | + | } |
| 388 | + | if (win->bar.l != NULL) { |
| 389 | + | olen = len = strlen(win->bar.l); |
| 390 | + | while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w) |
| 391 | + | len--; |
| 398 | 392 | | if (len > 0) { |
| 399 | | − | x = win->w - tw - H_TEXT_PAD; |
| 393 | + | if (len != olen) { |
| 394 | + | w = strlen(dots); |
| 395 | + | if (len <= w) |
| 396 | + | return; |
| 397 | + | memcpy(rest, win->bar.l + len - w, w); |
| 398 | + | memcpy(win->bar.l + len - w, dots, w); |
| 399 | + | } |
| 400 | + | x = H_TEXT_PAD; |
| 400 | 401 | | if (font.set) |
| 401 | | − | XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, rt, len); |
| 402 | + | XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len); |
| 402 | 403 | | else |
| 403 | | − | XDrawString(e->dpy, win->pm, gc, x, y, rt, len); |
| 404 | + | XDrawString(e->dpy, win->pm, gc, x, y, win->bar.l, len); |
| 405 | + | if (len != olen) |
| 406 | + | memcpy(win->bar.l + len - w, rest, w); |
| 404 | 407 | | } |
| 405 | 408 | | } |
| 406 | 409 | | } |
| @@ -409,11 +412,11 @@ void win_draw(win_t *win) { |
| 409 | 412 | | if (win == NULL || win->xwin == None || win->pm == None) |
| 410 | 413 | | return; |
| 411 | 414 | | |
| 412 | | − | if (win->barh > 0) |
| 415 | + | if (win->bar.h > 0) |
| 413 | 416 | | win_draw_bar(win); |
| 414 | 417 | | |
| 415 | 418 | | XCopyArea(win->env.dpy, win->pm, win->xwin, gc, |
| 416 | | − | 0, 0, win->w, win->h + win->barh, 0, 0); |
| 419 | + | 0, 0, win->w, win->h + win->bar.h, 0, 0); |
| 417 | 420 | | } |
| 418 | 421 | | |
| 419 | 422 | | void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h, |
| @@ -466,10 +469,10 @@ void win_set_title(win_t *win, const char *title) { |
| 466 | 469 | | PropModeReplace, (unsigned char *) title, strlen(title)); |
| 467 | 470 | | } |
| 468 | 471 | | |
| 469 | | − | void win_set_bar_info(win_t *win, const char *li, const char *ri) { |
| 472 | + | void win_set_bar_info(win_t *win, char *linfo, char *rinfo) { |
| 470 | 473 | | if (win != NULL) { |
| 471 | | − | win->lbar = li; |
| 472 | | − | win->rbar = ri; |
| 474 | + | win->bar.l = linfo; |
| 475 | + | win->bar.r = rinfo; |
| 473 | 476 | | } |
| 474 | 477 | | } |
| 475 | 478 | | |