draw.c
changeset 34 cd30cce52b78
parent 33 e90449e03167
child 43 989178822938
equal deleted inserted replaced
33:e90449e03167 34:cd30cce52b78
     9 #include <X11/Xlocale.h>
     9 #include <X11/Xlocale.h>
    10 
    10 
    11 #include "wm.h"
    11 #include "wm.h"
    12 
    12 
    13 static void
    13 static void
    14 drawborder(Brush *b)
    14 drawborder(void)
    15 {
    15 {
    16 	XPoint points[5];
    16 	XPoint points[5];
    17 	XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter);
    17 	XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
    18 	XSetForeground(dpy, b->gc, b->border);
    18 	XSetForeground(dpy, dc.gc, dc.border);
    19 	points[0].x = b->x;
    19 	points[0].x = dc.x;
    20 	points[0].y = b->y;
    20 	points[0].y = dc.y;
    21 	points[1].x = b->w - 1;
    21 	points[1].x = dc.w - 1;
    22 	points[1].y = 0;
    22 	points[1].y = 0;
    23 	points[2].x = 0;
    23 	points[2].x = 0;
    24 	points[2].y = b->h - 1;
    24 	points[2].y = dc.h - 1;
    25 	points[3].x = -(b->w - 1);
    25 	points[3].x = -(dc.w - 1);
    26 	points[3].y = 0;
    26 	points[3].y = 0;
    27 	points[4].x = 0;
    27 	points[4].x = 0;
    28 	points[4].y = -(b->h - 1);
    28 	points[4].y = -(dc.h - 1);
    29 	XDrawLines(dpy, b->drawable, b->gc, points, 5, CoordModePrevious);
    29 	XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
    30 }
    30 }
    31 
    31 
    32 void
    32 void
    33 draw(Brush *b, Bool border, const char *text)
    33 draw(Bool border, const char *text)
    34 {
    34 {
    35 	int x, y, w, h;
    35 	int x, y, w, h;
    36 	unsigned int len;
    36 	unsigned int len;
    37 	static char buf[256];
    37 	static char buf[256];
    38 	XGCValues gcv;
    38 	XGCValues gcv;
    39 	XRectangle r = { b->x, b->y, b->w, b->h };
    39 	XRectangle r = { dc.x, dc.y, dc.w, dc.h };
    40 
    40 
    41 	XSetForeground(dpy, b->gc, b->bg);
    41 	XSetForeground(dpy, dc.gc, dc.bg);
    42 	XFillRectangles(dpy, b->drawable, b->gc, &r, 1);
    42 	XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
    43 
    43 
    44 	w = 0;
    44 	w = 0;
    45 	if(border)
    45 	if(border)
    46 		drawborder(b);
    46 		drawborder();
    47 
    47 
    48 	if(!text)
    48 	if(!text)
    49 		return;
    49 		return;
    50 
    50 
    51 	len = strlen(text);
    51 	len = strlen(text);
    52 	if(len >= sizeof(buf))
    52 	if(len >= sizeof(buf))
    53 		len = sizeof(buf) - 1;
    53 		len = sizeof(buf) - 1;
    54 	memcpy(buf, text, len);
    54 	memcpy(buf, text, len);
    55 	buf[len] = 0;
    55 	buf[len] = 0;
    56 
    56 
    57 	h = b->font.ascent + b->font.descent;
    57 	h = dc.font.ascent + dc.font.descent;
    58 	y = b->y + (b->h / 2) - (h / 2) + b->font.ascent;
    58 	y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
    59 	x = b->x + (h / 2);
    59 	x = dc.x + (h / 2);
    60 
    60 
    61 	/* shorten text if necessary */
    61 	/* shorten text if necessary */
    62 	while(len && (w = textnw(&b->font, buf, len)) > b->w - h)
    62 	while(len && (w = textnw(&dc.font, buf, len)) > dc.w - h)
    63 		buf[--len] = 0;
    63 		buf[--len] = 0;
    64 
    64 
    65 	if(w > b->w)
    65 	if(w > dc.w)
    66 		return; /* too long */
    66 		return; /* too long */
    67 
    67 
    68 	gcv.foreground = b->fg;
    68 	gcv.foreground = dc.fg;
    69 	gcv.background = b->bg;
    69 	gcv.background = dc.bg;
    70 	if(b->font.set) {
    70 	if(dc.font.set) {
    71 		XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv);
    71 		XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
    72 		XmbDrawImageString(dpy, b->drawable, b->font.set, b->gc,
    72 		XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
    73 				x, y, buf, len);
    73 				x, y, buf, len);
    74 	}
    74 	}
    75 	else {
    75 	else {
    76 		gcv.font = b->font.xfont->fid;
    76 		gcv.font = dc.font.xfont->fid;
    77 		XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv);
    77 		XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
    78 		XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len);
    78 		XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
    79 	}
    79 	}
    80 }
    80 }
    81 
    81 
    82 static unsigned long
    82 static unsigned long
    83 xloadcolors(Colormap cmap, const char *colstr)
    83 xinitcolors(Colormap cmap, const char *colstr)
    84 {
    84 {
    85 	XColor color;
    85 	XColor color;
    86 	XAllocNamedColor(dpy, cmap, colstr, &color, &color);
    86 	XAllocNamedColor(dpy, cmap, colstr, &color, &color);
    87 	return color.pixel;
    87 	return color.pixel;
    88 }
    88 }
    89 
    89 
    90 void
    90 void
    91 loadcolors(int scr, Brush *b,
    91 initcolors(const char *bg, const char *fg, const char *border)
    92 		const char *bg, const char *fg, const char *border)
       
    93 {
    92 {
    94 	Colormap cmap = DefaultColormap(dpy, scr);
    93 	Colormap cmap = DefaultColormap(dpy, screen);
    95 	b->bg = xloadcolors(cmap, bg);
    94 	dc.bg = xinitcolors(cmap, bg);
    96 	b->fg = xloadcolors(cmap, fg);
    95 	dc.fg = xinitcolors(cmap, fg);
    97 	b->border = xloadcolors(cmap, border);
    96 	dc.border = xinitcolors(cmap, border);
    98 }
    97 }
    99 
    98 
   100 unsigned int
    99 unsigned int
   101 textnw(Fnt *font, char *text, unsigned int len)
   100 textnw(Fnt *font, char *text, unsigned int len)
   102 {
   101 {
   119 {
   118 {
   120 	return font->height + 4;
   119 	return font->height + 4;
   121 }
   120 }
   122 
   121 
   123 void
   122 void
   124 loadfont(Fnt *font, const char *fontstr)
   123 initfont(Fnt *font, const char *fontstr)
   125 {
   124 {
   126 	char **missing, *def;
   125 	char **missing, *def;
   127 	int i, n;
   126 	int i, n;
   128 
   127 
   129 	missing = NULL;
   128 	missing = NULL;
   162 		font->xfont = NULL;
   161 		font->xfont = NULL;
   163 		font->xfont = XLoadQueryFont(dpy, fontstr);
   162 		font->xfont = XLoadQueryFont(dpy, fontstr);
   164 		if (!font->xfont)
   163 		if (!font->xfont)
   165 			font->xfont = XLoadQueryFont(dpy, "fixed");
   164 			font->xfont = XLoadQueryFont(dpy, "fixed");
   166 		if (!font->xfont)
   165 		if (!font->xfont)
   167 			error("error, cannot load 'fixed' font\n");
   166 			error("error, cannot init 'fixed' font\n");
   168 		font->ascent = font->xfont->ascent;
   167 		font->ascent = font->xfont->ascent;
   169 		font->descent = font->xfont->descent;
   168 		font->descent = font->xfont->descent;
   170 	}
   169 	}
   171 	font->height = font->ascent + font->descent;
   170 	font->height = font->ascent + font->descent;
   172 }
   171 }