draw.c
changeset 3 e969f3575b7a
parent 2 a79188fe4a40
child 5 e5018cae273f
equal deleted inserted replaced
2:a79188fe4a40 3:e969f3575b7a
    12 static void
    12 static void
    13 drawborder(Display *dpy, Brush *b)
    13 drawborder(Display *dpy, Brush *b)
    14 {
    14 {
    15 	XPoint points[5];
    15 	XPoint points[5];
    16 	XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter);
    16 	XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter);
    17 	XSetForeground(dpy, b->gc, b->color.border);
    17 	XSetForeground(dpy, b->gc, b->border);
    18 	points[0].x = b->rect.x;
    18 	points[0].x = b->rect.x;
    19 	points[0].y = b->rect.y;
    19 	points[0].y = b->rect.y;
    20 	points[1].x = b->rect.width - 1;
    20 	points[1].x = b->rect.width - 1;
    21 	points[1].y = 0;
    21 	points[1].y = 0;
    22 	points[2].x = 0;
    22 	points[2].x = 0;
    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 {
   158 		if (!font->xfont)
   158 		if (!font->xfont)
   159 			error("error, cannot load 'fixed' font\n");
   159 			error("error, cannot load 'fixed' font\n");
   160 		font->ascent = font->xfont->ascent;
   160 		font->ascent = font->xfont->ascent;
   161 		font->descent = font->xfont->descent;
   161 		font->descent = font->xfont->descent;
   162 	}
   162 	}
       
   163 	font->height = font->ascent + font->descent;
   163 }
   164 }