draw.c
changeset 43 989178822938
parent 34 cd30cce52b78
child 57 f005d46462e8
equal deleted inserted replaced
42:040a7074d23c 43:989178822938
     6 #include <stdio.h>
     6 #include <stdio.h>
     7 #include <string.h>
     7 #include <string.h>
     8 
     8 
     9 #include <X11/Xlocale.h>
     9 #include <X11/Xlocale.h>
    10 
    10 
    11 #include "wm.h"
    11 #include "dwm.h"
    12 
    12 
    13 static void
    13 static void
    14 drawborder(void)
    14 drawborder(void)
    15 {
    15 {
    16 	XPoint points[5];
    16 	XPoint points[5];
    57 	h = dc.font.ascent + dc.font.descent;
    57 	h = dc.font.ascent + dc.font.descent;
    58 	y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
    58 	y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
    59 	x = dc.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(&dc.font, buf, len)) > dc.w - h)
    62 	while(len && (w = textnw(buf, len)) > dc.w - h)
    63 		buf[--len] = 0;
    63 		buf[--len] = 0;
    64 
    64 
    65 	if(w > dc.w)
    65 	if(w > dc.w)
    66 		return; /* too long */
    66 		return; /* too long */
    67 
    67 
    77 		XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
    77 		XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
    78 		XDrawImageString(dpy, dc.drawable, dc.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 unsigned long
    83 xinitcolors(Colormap cmap, const char *colstr)
    83 initcolor(const char *colstr)
    84 {
    84 {
    85 	XColor color;
    85 	XColor color;
       
    86 	Colormap cmap = DefaultColormap(dpy, screen);
       
    87 
    86 	XAllocNamedColor(dpy, cmap, colstr, &color, &color);
    88 	XAllocNamedColor(dpy, cmap, colstr, &color, &color);
    87 	return color.pixel;
    89 	return color.pixel;
    88 }
    90 }
    89 
    91 
    90 void
    92 unsigned int
    91 initcolors(const char *bg, const char *fg, const char *border)
    93 textnw(char *text, unsigned int len)
    92 {
    94 {
    93 	Colormap cmap = DefaultColormap(dpy, screen);
    95 	XRectangle r;
    94 	dc.bg = xinitcolors(cmap, bg);
    96 	if(dc.font.set) {
    95 	dc.fg = xinitcolors(cmap, fg);
    97 		XmbTextExtents(dc.font.set, text, len, NULL, &r);
    96 	dc.border = xinitcolors(cmap, border);
    98 		return r.width;
       
    99 	}
       
   100 	return XTextWidth(dc.font.xfont, text, len);
    97 }
   101 }
    98 
   102 
    99 unsigned int
   103 unsigned int
   100 textnw(Fnt *font, char *text, unsigned int len)
   104 textw(char *text)
   101 {
   105 {
   102 	XRectangle r;
   106 	return textnw(text, strlen(text));
   103 	if(font->set) {
       
   104 		XmbTextExtents(font->set, text, len, NULL, &r);
       
   105 		return r.width;
       
   106 	}
       
   107 	return XTextWidth(font->xfont, text, len);
       
   108 }
       
   109 
       
   110 unsigned int
       
   111 textw(Fnt *font, char *text)
       
   112 {
       
   113 	return textnw(font, text, strlen(text));
       
   114 }
       
   115 
       
   116 unsigned int
       
   117 texth(Fnt *font)
       
   118 {
       
   119 	return font->height + 4;
       
   120 }
   107 }
   121 
   108 
   122 void
   109 void
   123 initfont(Fnt *font, const char *fontstr)
   110 initfont(const char *fontstr)
   124 {
   111 {
   125 	char **missing, *def;
   112 	char **missing, *def;
   126 	int i, n;
   113 	int i, n;
   127 
   114 
   128 	missing = NULL;
   115 	missing = NULL;
   129 	setlocale(LC_ALL, "");
   116 	setlocale(LC_ALL, "");
   130 	if(font->set)
   117 	if(dc.font.set)
   131 		XFreeFontSet(dpy, font->set);
   118 		XFreeFontSet(dpy, dc.font.set);
   132 	font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
   119 	dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
   133 	if(missing) {
   120 	if(missing) {
   134 		while(n--)
   121 		while(n--)
   135 			fprintf(stderr, "missing fontset: %s\n", missing[n]);
   122 			fprintf(stderr, "missing fontset: %s\n", missing[n]);
   136 		XFreeStringList(missing);
   123 		XFreeStringList(missing);
   137 		if(font->set) {
   124 		if(dc.font.set) {
   138 			XFreeFontSet(dpy, font->set);
   125 			XFreeFontSet(dpy, dc.font.set);
   139 			font->set = NULL;
   126 			dc.font.set = NULL;
   140 		}
   127 		}
   141 	}
   128 	}
   142 	if(font->set) {
   129 	if(dc.font.set) {
   143 		XFontSetExtents *font_extents;
   130 		XFontSetExtents *font_extents;
   144 		XFontStruct **xfonts;
   131 		XFontStruct **xfonts;
   145 		char **font_names;
   132 		char **font_names;
   146 
   133 
   147 		font->ascent = font->descent = 0;
   134 		dc.font.ascent = dc.font.descent = 0;
   148 		font_extents = XExtentsOfFontSet(font->set);
   135 		font_extents = XExtentsOfFontSet(dc.font.set);
   149 		n = XFontsOfFontSet(font->set, &xfonts, &font_names);
   136 		n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
   150 		for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) {
   137 		for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
   151 			if(font->ascent < (*xfonts)->ascent)
   138 			if(dc.font.ascent < (*xfonts)->ascent)
   152 				font->ascent = (*xfonts)->ascent;
   139 				dc.font.ascent = (*xfonts)->ascent;
   153 			if(font->descent < (*xfonts)->descent)
   140 			if(dc.font.descent < (*xfonts)->descent)
   154 				font->descent = (*xfonts)->descent;
   141 				dc.font.descent = (*xfonts)->descent;
   155 			xfonts++;
   142 			xfonts++;
   156 		}
   143 		}
   157 	}
   144 	}
   158 	else {
   145 	else {
   159 		if(font->xfont)
   146 		if(dc.font.xfont)
   160 			XFreeFont(dpy, font->xfont);
   147 			XFreeFont(dpy, dc.font.xfont);
   161 		font->xfont = NULL;
   148 		dc.font.xfont = NULL;
   162 		font->xfont = XLoadQueryFont(dpy, fontstr);
   149 		dc.font.xfont = XLoadQueryFont(dpy, fontstr);
   163 		if (!font->xfont)
   150 		if (!dc.font.xfont)
   164 			font->xfont = XLoadQueryFont(dpy, "fixed");
   151 			dc.font.xfont = XLoadQueryFont(dpy, "fixed");
   165 		if (!font->xfont)
   152 		if (!dc.font.xfont)
   166 			error("error, cannot init 'fixed' font\n");
   153 			error("error, cannot init 'fixed' font\n");
   167 		font->ascent = font->xfont->ascent;
   154 		dc.font.ascent = dc.font.xfont->ascent;
   168 		font->descent = font->xfont->descent;
   155 		dc.font.descent = dc.font.xfont->descent;
   169 	}
   156 	}
   170 	font->height = font->ascent + font->descent;
   157 	dc.font.height = dc.font.ascent + dc.font.descent;
   171 }
   158 }