27 points[4].y = -(b->rect.height - 1); |
27 points[4].y = -(b->rect.height - 1); |
28 XDrawLines(dpy, b->drawable, b->gc, points, 5, CoordModePrevious); |
28 XDrawLines(dpy, b->drawable, b->gc, points, 5, CoordModePrevious); |
29 } |
29 } |
30 |
30 |
31 void |
31 void |
32 draw(Display *dpy, Brush *b) |
32 draw(Display *dpy, Brush *b, Bool border, const char *text) |
33 { |
33 { |
34 unsigned int x, y, w, h, len; |
34 unsigned int x, y, w, h, len; |
35 static char buf[256]; |
35 static char buf[256]; |
36 XGCValues gcv; |
36 XGCValues gcv; |
37 |
37 |
38 XSetForeground(dpy, b->gc, b->color.bg); |
38 XSetForeground(dpy, b->gc, b->bg); |
39 XFillRectangles(dpy, b->drawable, b->gc, &b->rect, 1); |
39 XFillRectangles(dpy, b->drawable, b->gc, &b->rect, 1); |
40 |
40 |
41 if(b->border) |
41 if(border) |
42 drawborder(dpy, b); |
42 drawborder(dpy, b); |
43 |
43 |
44 if(!b->text) |
44 if(!text) |
45 return; |
45 return; |
46 |
46 |
47 len = strlen(b->text); |
47 len = strlen(text); |
48 if(len >= sizeof(buf)) |
48 if(len >= sizeof(buf)) |
49 len = sizeof(buf) - 1; |
49 len = sizeof(buf) - 1; |
50 memcpy(buf, b->text, len); |
50 memcpy(buf, text, len); |
51 buf[len] = 0; |
51 buf[len] = 0; |
52 |
52 |
53 h = b->font->ascent + b->font->descent; |
53 h = b->font.ascent + b->font.descent; |
54 y = b->rect.y + (b->rect.height / 2) - (h / 2) + b->font->ascent; |
54 y = b->rect.y + (b->rect.height / 2) - (h / 2) + b->font.ascent; |
55 x = b->rect.x + (h / 2); |
55 x = b->rect.x + (h / 2); |
56 |
56 |
57 /* shorten text if necessary */ |
57 /* shorten text if necessary */ |
58 while(len && (w = textwidth_l(b->font, buf, len)) > b->rect.width - h) |
58 while(len && (w = textwidth_l(&b->font, buf, len)) > b->rect.width - h) |
59 buf[--len] = 0; |
59 buf[--len] = 0; |
60 |
60 |
61 if(w > b->rect.width) |
61 if(w > b->rect.width) |
62 return; /* too long */ |
62 return; /* too long */ |
63 |
63 |
64 gcv.foreground = b->color.fg; |
64 gcv.foreground = b->fg; |
65 gcv.background = b->color.bg; |
65 gcv.background = b->bg; |
66 if(b->font->set) { |
66 if(b->font.set) { |
67 XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv); |
67 XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv); |
68 XmbDrawImageString(dpy, b->drawable, b->font->set, b->gc, |
68 XmbDrawImageString(dpy, b->drawable, b->font.set, b->gc, |
69 x, y, buf, len); |
69 x, y, buf, len); |
70 } |
70 } |
71 else { |
71 else { |
72 gcv.font = b->font->xfont->fid; |
72 gcv.font = b->font.xfont->fid; |
73 XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv); |
73 XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv); |
74 XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len); |
74 XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len); |
75 } |
75 } |
76 } |
76 } |
77 |
77 |
78 static unsigned long |
78 static unsigned long |
79 xloadcolor(Display *dpy, Colormap cmap, const char *colstr) |
79 xloadcolors(Display *dpy, Colormap cmap, const char *colstr) |
80 { |
80 { |
81 XColor color; |
81 XColor color; |
82 XAllocNamedColor(dpy, cmap, colstr, &color, &color); |
82 XAllocNamedColor(dpy, cmap, colstr, &color, &color); |
83 return color.pixel; |
83 return color.pixel; |
84 } |
84 } |
85 |
85 |
86 void |
86 void |
87 loadcolor(Display *dpy, int screen, Color *c, |
87 loadcolors(Display *dpy, int screen, Brush *b, |
88 const char *bg, const char *fg, const char *border) |
88 const char *bg, const char *fg, const char *border) |
89 { |
89 { |
90 Colormap cmap = DefaultColormap(dpy, screen); |
90 Colormap cmap = DefaultColormap(dpy, screen); |
91 c->bg = xloadcolor(dpy, cmap, bg); |
91 b->bg = xloadcolors(dpy, cmap, bg); |
92 c->fg = xloadcolor(dpy, cmap, fg); |
92 b->fg = xloadcolors(dpy, cmap, fg); |
93 c->border = xloadcolor(dpy, cmap, border); |
93 b->border = xloadcolors(dpy, cmap, border); |
94 } |
94 } |
95 |
95 |
96 unsigned int |
96 unsigned int |
97 textwidth_l(Fnt *font, char *text, unsigned int len) |
97 textwidth_l(Fnt *font, char *text, unsigned int len) |
98 { |
98 { |