draw.c
changeset 33 e90449e03167
parent 32 082c75b937b5
child 34 cd30cce52b78
equal deleted inserted replaced
32:082c75b937b5 33:e90449e03167
     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(Display *dpy, Brush *b)
    14 drawborder(Brush *b)
    15 {
    15 {
    16 	XPoint points[5];
    16 	XPoint points[5];
    17 	XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter);
    17 	XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter);
    18 	XSetForeground(dpy, b->gc, b->border);
    18 	XSetForeground(dpy, b->gc, b->border);
    19 	points[0].x = b->x;
    19 	points[0].x = b->x;
    28 	points[4].y = -(b->h - 1);
    28 	points[4].y = -(b->h - 1);
    29 	XDrawLines(dpy, b->drawable, b->gc, points, 5, CoordModePrevious);
    29 	XDrawLines(dpy, b->drawable, b->gc, points, 5, CoordModePrevious);
    30 }
    30 }
    31 
    31 
    32 void
    32 void
    33 draw(Display *dpy, Brush *b, Bool border, const char *text)
    33 draw(Brush *b, Bool border, const char *text)
    34 {
    34 {
    35 	unsigned int x, y, w, h, len;
    35 	int x, y, w, h;
       
    36 	unsigned int len;
    36 	static char buf[256];
    37 	static char buf[256];
    37 	XGCValues gcv;
    38 	XGCValues gcv;
    38 	XRectangle r = { b->x, b->y, b->w, b->h };
    39 	XRectangle r = { b->x, b->y, b->w, b->h };
    39 
    40 
    40 	XSetForeground(dpy, b->gc, b->bg);
    41 	XSetForeground(dpy, b->gc, b->bg);
    41 	XFillRectangles(dpy, b->drawable, b->gc, &r, 1);
    42 	XFillRectangles(dpy, b->drawable, b->gc, &r, 1);
    42 
    43 
    43 	w = 0;
    44 	w = 0;
    44 	if(border)
    45 	if(border)
    45 		drawborder(dpy, b);
    46 		drawborder(b);
    46 
    47 
    47 	if(!text)
    48 	if(!text)
    48 		return;
    49 		return;
    49 
    50 
    50 	len = strlen(text);
    51 	len = strlen(text);
    77 		XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len);
    78 		XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len);
    78 	}
    79 	}
    79 }
    80 }
    80 
    81 
    81 static unsigned long
    82 static unsigned long
    82 xloadcolors(Display *dpy, Colormap cmap, const char *colstr)
    83 xloadcolors(Colormap cmap, const char *colstr)
    83 {
    84 {
    84 	XColor color;
    85 	XColor color;
    85 	XAllocNamedColor(dpy, cmap, colstr, &color, &color);
    86 	XAllocNamedColor(dpy, cmap, colstr, &color, &color);
    86 	return color.pixel;
    87 	return color.pixel;
    87 }
    88 }
    88 
    89 
    89 void
    90 void
    90 loadcolors(Display *dpy, int screen, Brush *b,
    91 loadcolors(int scr, Brush *b,
    91 		const char *bg, const char *fg, const char *border)
    92 		const char *bg, const char *fg, const char *border)
    92 {
    93 {
    93 	Colormap cmap = DefaultColormap(dpy, screen);
    94 	Colormap cmap = DefaultColormap(dpy, scr);
    94 	b->bg = xloadcolors(dpy, cmap, bg);
    95 	b->bg = xloadcolors(cmap, bg);
    95 	b->fg = xloadcolors(dpy, cmap, fg);
    96 	b->fg = xloadcolors(cmap, fg);
    96 	b->border = xloadcolors(dpy, cmap, border);
    97 	b->border = xloadcolors(cmap, border);
    97 }
    98 }
    98 
    99 
    99 unsigned int
   100 unsigned int
   100 textnw(Fnt *font, char *text, unsigned int len)
   101 textnw(Fnt *font, char *text, unsigned int len)
   101 {
   102 {
   118 {
   119 {
   119 	return font->height + 4;
   120 	return font->height + 4;
   120 }
   121 }
   121 
   122 
   122 void
   123 void
   123 loadfont(Display *dpy, Fnt *font, const char *fontstr)
   124 loadfont(Fnt *font, const char *fontstr)
   124 {
   125 {
   125 	char **missing, *def;
   126 	char **missing, *def;
   126 	int n;
   127 	int i, n;
   127 
   128 
   128 	missing = NULL;
   129 	missing = NULL;
   129 	def = "?";
       
   130 	setlocale(LC_ALL, "");
   130 	setlocale(LC_ALL, "");
   131 	if(font->set)
   131 	if(font->set)
   132 		XFreeFontSet(dpy, font->set);
   132 		XFreeFontSet(dpy, font->set);
   133 	font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
   133 	font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
   134 	if(missing) {
   134 	if(missing) {
   142 	}
   142 	}
   143 	if(font->set) {
   143 	if(font->set) {
   144 		XFontSetExtents *font_extents;
   144 		XFontSetExtents *font_extents;
   145 		XFontStruct **xfonts;
   145 		XFontStruct **xfonts;
   146 		char **font_names;
   146 		char **font_names;
   147 		unsigned int i;
       
   148 
   147 
   149 		font->ascent = font->descent = 0;
   148 		font->ascent = font->descent = 0;
   150 		font_extents = XExtentsOfFontSet(font->set);
   149 		font_extents = XExtentsOfFontSet(font->set);
   151 		n = XFontsOfFontSet(font->set, &xfonts, &font_names);
   150 		n = XFontsOfFontSet(font->set, &xfonts, &font_names);
   152 		for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) {
   151 		for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) {