draw.c
changeset 26 e8f627998d6f
parent 7 49e2fc9fb94f
child 30 2e0fb4130bfb
equal deleted inserted replaced
25:e238dc4844d7 26:e8f627998d6f
    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->border);
    17 	XSetForeground(dpy, b->gc, b->border);
    18 	points[0].x = b->rect.x;
    18 	points[0].x = b->x;
    19 	points[0].y = b->rect.y;
    19 	points[0].y = b->y;
    20 	points[1].x = b->rect.width - 1;
    20 	points[1].x = b->w - 1;
    21 	points[1].y = 0;
    21 	points[1].y = 0;
    22 	points[2].x = 0;
    22 	points[2].x = 0;
    23 	points[2].y = b->rect.height - 1;
    23 	points[2].y = b->h - 1;
    24 	points[3].x = -(b->rect.width - 1);
    24 	points[3].x = -(b->w - 1);
    25 	points[3].y = 0;
    25 	points[3].y = 0;
    26 	points[4].x = 0;
    26 	points[4].x = 0;
    27 	points[4].y = -(b->rect.height - 1);
    27 	points[4].y = -(b->h - 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, Bool border, const char *text)
    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 	XRectangle r = { b->x, b->y, b->w, b->h };
    37 
    38 
    38 	XSetForeground(dpy, b->gc, b->bg);
    39 	XSetForeground(dpy, b->gc, b->bg);
    39 	XFillRectangles(dpy, b->drawable, b->gc, &b->rect, 1);
    40 	XFillRectangles(dpy, b->drawable, b->gc, &r, 1);
    40 
    41 
    41 	if(border)
    42 	if(border)
    42 		drawborder(dpy, b);
    43 		drawborder(dpy, b);
    43 
    44 
    44 	if(!text)
    45 	if(!text)
    49 		len = sizeof(buf) - 1;
    50 		len = sizeof(buf) - 1;
    50 	memcpy(buf, text, len);
    51 	memcpy(buf, text, len);
    51 	buf[len] = 0;
    52 	buf[len] = 0;
    52 
    53 
    53 	h = b->font.ascent + b->font.descent;
    54 	h = b->font.ascent + b->font.descent;
    54 	y = b->rect.y + (b->rect.height / 2) - (h / 2) + b->font.ascent;
    55 	y = b->y + (b->h / 2) - (h / 2) + b->font.ascent;
    55 	x = b->rect.x + (h / 2);
    56 	x = b->x + (h / 2);
    56 
    57 
    57 	/* shorten text if necessary */
    58 	/* shorten text if necessary */
    58 	while(len && (w = textwidth_l(&b->font, buf, len)) > b->rect.width - h)
    59 	while(len && (w = textnw(&b->font, buf, len)) > b->w - h)
    59 		buf[--len] = 0;
    60 		buf[--len] = 0;
    60 
    61 
    61 	if(w > b->rect.width)
    62 	if(w > b->w)
    62 		return; /* too long */
    63 		return; /* too long */
    63 
    64 
    64 	gcv.foreground = b->fg;
    65 	gcv.foreground = b->fg;
    65 	gcv.background = b->bg;
    66 	gcv.background = b->bg;
    66 	if(b->font.set) {
    67 	if(b->font.set) {
    92 	b->fg = xloadcolors(dpy, cmap, fg);
    93 	b->fg = xloadcolors(dpy, cmap, fg);
    93 	b->border = xloadcolors(dpy, cmap, border);
    94 	b->border = xloadcolors(dpy, cmap, border);
    94 }
    95 }
    95 
    96 
    96 unsigned int
    97 unsigned int
    97 textwidth_l(Fnt *font, char *text, unsigned int len)
    98 textnw(Fnt *font, char *text, unsigned int len)
    98 {
    99 {
       
   100 	XRectangle r;
    99 	if(font->set) {
   101 	if(font->set) {
   100 		XRectangle r;
   102 		XmbTextExtents(font->set, text, len, NULL, &r);
   101 		XmbTextExtents(font->set, text, len, 0, &r);
       
   102 		return r.width;
   103 		return r.width;
   103 	}
   104 	}
   104 	return XTextWidth(font->xfont, text, len);
   105 	return XTextWidth(font->xfont, text, len);
   105 }
   106 }
   106 
   107 
   107 unsigned int
   108 unsigned int
   108 textwidth(Fnt *font, char *text)
   109 textw(Fnt *font, char *text)
   109 {
   110 {
   110 	return textwidth_l(font, text, strlen(text));
   111 	return textnw(font, text, strlen(text));
       
   112 }
       
   113 
       
   114 unsigned int
       
   115 texth(Fnt *font)
       
   116 {
       
   117 	return font->height + 4;
   111 }
   118 }
   112 
   119 
   113 void
   120 void
   114 loadfont(Display *dpy, Fnt *font, const char *fontstr)
   121 loadfont(Display *dpy, Fnt *font, const char *fontstr)
   115 {
   122 {
   160 		font->ascent = font->xfont->ascent;
   167 		font->ascent = font->xfont->ascent;
   161 		font->descent = font->xfont->descent;
   168 		font->descent = font->xfont->descent;
   162 	}
   169 	}
   163 	font->height = font->ascent + font->descent;
   170 	font->height = font->ascent + font->descent;
   164 }
   171 }
   165 
       
   166 unsigned int
       
   167 labelheight(Fnt *font)
       
   168 {
       
   169 	return font->height + 4;
       
   170 }