| @@ -20,6 +20,7 @@ |
| 20 | 20 | | #define _WINDOW_CONFIG |
| 21 | 21 | | |
| 22 | 22 | | #include <string.h> |
| 23 | + | #include <locale.h> |
| 23 | 24 | | #include <X11/Xutil.h> |
| 24 | 25 | | #include <X11/cursorfont.h> |
| 25 | 26 | | |
| @@ -28,6 +29,11 @@ |
| 28 | 29 | | #include "window.h" |
| 29 | 30 | | #include "config.h" |
| 30 | 31 | | |
| 32 | + | enum { |
| 33 | + | H_TEXT_PAD = 5, |
| 34 | + | V_TEXT_PAD = 1 |
| 35 | + | }; |
| 36 | + | |
| 31 | 37 | | static Cursor carrow; |
| 32 | 38 | | static Cursor cnone; |
| 33 | 39 | | static Cursor chand; |
| @@ -36,9 +42,62 @@ static GC gc; |
| 36 | 42 | | |
| 37 | 43 | | Atom wm_delete_win; |
| 38 | 44 | | |
| 45 | + | struct { |
| 46 | + | int ascent; |
| 47 | + | int descent; |
| 48 | + | XFontStruct *xfont; |
| 49 | + | XFontSet set; |
| 50 | + | } font; |
| 51 | + | |
| 52 | + | void win_init_font(Display *dpy, const char *fontstr) { |
| 53 | + | int n; |
| 54 | + | char *def, **missing; |
| 55 | + | |
| 56 | + | font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); |
| 57 | + | if (missing) { |
| 58 | + | while (n--) |
| 59 | + | warn("missing fontset: %s", missing[n]); |
| 60 | + | XFreeStringList(missing); |
| 61 | + | } |
| 62 | + | if (font.set) { |
| 63 | + | XFontStruct **xfonts; |
| 64 | + | char **font_names; |
| 65 | + | |
| 66 | + | font.ascent = font.descent = 0; |
| 67 | + | XExtentsOfFontSet(font.set); |
| 68 | + | n = XFontsOfFontSet(font.set, &xfonts, &font_names); |
| 69 | + | while (n--) { |
| 70 | + | font.ascent = MAX(font.ascent, (*xfonts)->ascent); |
| 71 | + | font.descent = MAX(font.descent,(*xfonts)->descent); |
| 72 | + | xfonts++; |
| 73 | + | } |
| 74 | + | } else { |
| 75 | + | if ((font.xfont = XLoadQueryFont(dpy, fontstr)) == NULL && |
| 76 | + | (font.xfont = XLoadQueryFont(dpy, "fixed")) == NULL) |
| 77 | + | { |
| 78 | + | die("could not load font: %s", fontstr); |
| 79 | + | } |
| 80 | + | font.ascent = font.xfont->ascent; |
| 81 | + | font.descent = font.xfont->descent; |
| 82 | + | } |
| 83 | + | } |
| 84 | + | |
| 85 | + | unsigned long win_alloc_color(win_t *win, const char *name) { |
| 86 | + | XColor col; |
| 87 | + | |
| 88 | + | if (win == NULL) |
| 89 | + | return 0UL; |
| 90 | + | if (XAllocNamedColor(win->env.dpy, |
| 91 | + | DefaultColormap(win->env.dpy, win->env.scr), |
| 92 | + | name, &col, &col) == 0) |
| 93 | + | { |
| 94 | + | die("could not allocate color: %s", name); |
| 95 | + | } |
| 96 | + | return col.pixel; |
| 97 | + | } |
| 98 | + | |
| 39 | 99 | | void win_init(win_t *win) { |
| 40 | 100 | | win_env_t *e; |
| 41 | | − | XColor col; |
| 42 | 101 | | |
| 43 | 102 | | if (win == NULL) |
| 44 | 103 | | return; |
| @@ -57,25 +116,21 @@ void win_init(win_t *win) { |
| 57 | 116 | | win->black = BlackPixel(e->dpy, e->scr); |
| 58 | 117 | | win->white = WhitePixel(e->dpy, e->scr); |
| 59 | 118 | | |
| 60 | | − | if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR, |
| 61 | | − | &col, &col) != 0) |
| 62 | | − | { |
| 63 | | − | win->bgcol = col.pixel; |
| 64 | | − | } else { |
| 65 | | − | die("could not allocate color: %s", BG_COLOR); |
| 66 | | − | } |
| 67 | | − | |
| 68 | | − | if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), SEL_COLOR, |
| 69 | | − | &col, &col) != 0) |
| 70 | | − | { |
| 71 | | − | win->selcol = col.pixel; |
| 72 | | − | } else { |
| 73 | | − | die("could not allocate color: %s", SEL_COLOR); |
| 74 | | − | } |
| 119 | + | win->bgcol = win_alloc_color(win, WIN_BG_COLOR); |
| 120 | + | win->selcol = win_alloc_color(win, SEL_COLOR); |
| 121 | + | win->barbgcol = win_alloc_color(win, BAR_BG_COLOR); |
| 122 | + | win->barfgcol = win_alloc_color(win, BAR_FG_COLOR); |
| 75 | 123 | | |
| 76 | 124 | | win->xwin = 0; |
| 77 | 125 | | win->pm = 0; |
| 78 | 126 | | win->fullscreen = false; |
| 127 | + | win->lbar = NULL; |
| 128 | + | win->rbar = NULL; |
| 129 | + | |
| 130 | + | if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0) |
| 131 | + | warn("no locale support"); |
| 132 | + | |
| 133 | + | win_init_font(e->dpy, BAR_FONT); |
| 79 | 134 | | } |
| 80 | 135 | | |
| 81 | 136 | | void win_set_sizehints(win_t *win) { |
| @@ -87,8 +142,8 @@ void win_set_sizehints(win_t *win) { |
| 87 | 142 | | sizehints.flags = PMinSize | PMaxSize; |
| 88 | 143 | | sizehints.min_width = win->w; |
| 89 | 144 | | sizehints.max_width = win->w; |
| 90 | | − | sizehints.min_height = win->h; |
| 91 | | − | sizehints.max_height = win->h; |
| 145 | + | sizehints.min_height = win->h + win->barh; |
| 146 | + | sizehints.max_height = win->h + win->barh; |
| 92 | 147 | | XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints); |
| 93 | 148 | | } |
| 94 | 149 | | |
| @@ -157,6 +212,9 @@ void win_open(win_t *win) { |
| 157 | 212 | | classhint.res_class = "sxiv"; |
| 158 | 213 | | XSetClassHint(e->dpy, win->xwin, &classhint); |
| 159 | 214 | | |
| 215 | + | win->barh = font.ascent + font.descent + 2 * V_TEXT_PAD; |
| 216 | + | win->h -= win->barh; |
| 217 | + | |
| 160 | 218 | | if (options->fixed_win) |
| 161 | 219 | | win_set_sizehints(win); |
| 162 | 220 | | |
| @@ -191,12 +249,12 @@ bool win_configure(win_t *win, XConfigureEvent *c) { |
| 191 | 249 | | if (win == NULL) |
| 192 | 250 | | return false; |
| 193 | 251 | | |
| 194 | | − | changed = win->w != c->width || win->h != c->height; |
| 252 | + | changed = win->w != c->width || win->h + win->barh != c->height; |
| 195 | 253 | | |
| 196 | 254 | | win->x = c->x; |
| 197 | 255 | | win->y = c->y; |
| 198 | 256 | | win->w = c->width; |
| 199 | | − | win->h = c->height; |
| 257 | + | win->h = c->height - win->barh; |
| 200 | 258 | | win->bw = c->border_width; |
| 201 | 259 | | |
| 202 | 260 | | return changed; |
| @@ -211,18 +269,18 @@ bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) { |
| 211 | 269 | | w = MIN(w, win->env.scrw - 2 * win->bw); |
| 212 | 270 | | h = MIN(h, win->env.scrh - 2 * win->bw); |
| 213 | 271 | | |
| 214 | | − | if (win->x == x && win->y == y && win->w == w && win->h == h) |
| 272 | + | if (win->x == x && win->y == y && win->w == w && win->h + win->barh == h) |
| 215 | 273 | | return false; |
| 216 | 274 | | |
| 217 | 275 | | win->x = x; |
| 218 | 276 | | win->y = y; |
| 219 | 277 | | win->w = w; |
| 220 | | − | win->h = h; |
| 278 | + | win->h = h - win->barh; |
| 221 | 279 | | |
| 222 | 280 | | if (options->fixed_win) |
| 223 | 281 | | win_set_sizehints(win); |
| 224 | 282 | | |
| 225 | | − | XMoveResizeWindow(win->env.dpy, win->xwin, win->x, win->y, win->w, win->h); |
| 283 | + | XMoveResizeWindow(win->env.dpy, win->xwin, x, y, w, h); |
| 226 | 284 | | |
| 227 | 285 | | return true; |
| 228 | 286 | | } |
| @@ -253,26 +311,87 @@ void win_toggle_fullscreen(win_t *win) { |
| 253 | 311 | | |
| 254 | 312 | | void win_clear(win_t *win) { |
| 255 | 313 | | win_env_t *e; |
| 256 | | − | XGCValues gcval; |
| 257 | 314 | | |
| 258 | 315 | | if (win == NULL || win->xwin == None) |
| 259 | 316 | | return; |
| 260 | 317 | | |
| 261 | 318 | | e = &win->env; |
| 262 | | − | gcval.foreground = win->fullscreen ? win->black : win->bgcol; |
| 263 | 319 | | |
| 264 | 320 | | if (win->pm != None) |
| 265 | 321 | | XFreePixmap(e->dpy, win->pm); |
| 266 | 322 | | win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth); |
| 267 | 323 | | |
| 268 | | − | XChangeGC(e->dpy, gc, GCForeground, &gcval); |
| 324 | + | XSetForeground(e->dpy, gc, win->fullscreen ? win->black : win->bgcol); |
| 269 | 325 | | XFillRectangle(e->dpy, win->pm, gc, 0, 0, e->scrw, e->scrh); |
| 270 | 326 | | } |
| 271 | 327 | | |
| 328 | + | int win_textwidth(const char *text, unsigned int len) { |
| 329 | + | XRectangle r; |
| 330 | + | |
| 331 | + | if (font.set) { |
| 332 | + | XmbTextExtents(font.set, text, len, NULL, &r); |
| 333 | + | return r.width; |
| 334 | + | } else { |
| 335 | + | return XTextWidth(font.xfont, text, len); |
| 336 | + | } |
| 337 | + | } |
| 338 | + | void win_draw_bar(win_t *win) { |
| 339 | + | win_env_t *e; |
| 340 | + | int len, x, y, w, tw = 0; |
| 341 | + | const char *rt; |
| 342 | + | |
| 343 | + | if (win == NULL || win->xwin == None) |
| 344 | + | return; |
| 345 | + | |
| 346 | + | e = &win->env; |
| 347 | + | x = H_TEXT_PAD; |
| 348 | + | y = win->h + font.ascent + V_TEXT_PAD; |
| 349 | + | w = win->w - 2 * H_TEXT_PAD; |
| 350 | + | |
| 351 | + | XSetForeground(e->dpy, gc, win->barbgcol); |
| 352 | + | XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->barh); |
| 353 | + | |
| 354 | + | XSetForeground(e->dpy, gc, win->barfgcol); |
| 355 | + | XSetBackground(e->dpy, gc, win->barbgcol); |
| 356 | + | |
| 357 | + | if (win->lbar != NULL) { |
| 358 | + | len = strlen(win->lbar); |
| 359 | + | while (len > 0 && (tw = win_textwidth(win->lbar, len)) > w) |
| 360 | + | len--; |
| 361 | + | w -= tw + 2 * H_TEXT_PAD; |
| 362 | + | if (font.set) |
| 363 | + | XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->lbar, len); |
| 364 | + | else |
| 365 | + | XDrawString(e->dpy, win->pm, gc, x, y, win->lbar, len); |
| 366 | + | } |
| 367 | + | if (win->rbar != NULL) { |
| 368 | + | len = strlen(win->rbar); |
| 369 | + | rt = win->rbar; |
| 370 | + | while (len > 0 && (tw = win_textwidth(rt, len)) > w) { |
| 371 | + | rt = strstr(rt, " "); |
| 372 | + | if (rt != NULL) { |
| 373 | + | rt += 2; |
| 374 | + | len = strlen(rt); |
| 375 | + | } else { |
| 376 | + | len = 0; |
| 377 | + | } |
| 378 | + | } |
| 379 | + | if (len > 0) { |
| 380 | + | x = win->w - tw - H_TEXT_PAD; |
| 381 | + | if (font.set) |
| 382 | + | XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, rt, len); |
| 383 | + | else |
| 384 | + | XDrawString(e->dpy, win->pm, gc, x, y, rt, len); |
| 385 | + | } |
| 386 | + | } |
| 387 | + | } |
| 388 | + | |
| 272 | 389 | | void win_draw(win_t *win) { |
| 273 | 390 | | if (win == NULL || win->xwin == None) |
| 274 | 391 | | return; |
| 275 | 392 | | |
| 393 | + | win_draw_bar(win); |
| 394 | + | |
| 276 | 395 | | XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm); |
| 277 | 396 | | XClearWindow(win->env.dpy, win->xwin); |
| 278 | 397 | | } |
| @@ -314,6 +433,13 @@ void win_set_title(win_t *win, const char *title) { |
| 314 | 433 | | PropModeReplace, (unsigned char *) title, strlen(title)); |
| 315 | 434 | | } |
| 316 | 435 | | |
| 436 | + | void win_set_bar_info(win_t *win, const char *li, const char *ri) { |
| 437 | + | if (win != NULL) { |
| 438 | + | win->lbar = li; |
| 439 | + | win->rbar = ri; |
| 440 | + | } |
| 441 | + | } |
| 442 | + | |
| 317 | 443 | | void win_set_cursor(win_t *win, cursor_t cursor) { |
| 318 | 444 | | if (win == NULL || win->xwin == None) |
| 319 | 445 | | return; |