Mercurial > dwm-meillo
annotate draw.c @ 27:f96fb3fd8203
added grid mode on Mod1Mask g
author | Anselm R. Garbe <garbeam@wmii.de> |
---|---|
date | Wed, 12 Jul 2006 16:00:51 +0200 |
parents | e8f627998d6f |
children | 2e0fb4130bfb |
rev | line source |
---|---|
2 | 1 /* |
2 * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> | |
3 * See LICENSE file for license details. | |
4 */ | |
5 | |
6 #include <stdio.h> | |
7 #include <string.h> | |
8 | |
9 #include "draw.h" | |
10 #include "util.h" | |
11 | |
12 static void | |
13 drawborder(Display *dpy, Brush *b) | |
14 { | |
15 XPoint points[5]; | |
16 XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter); | |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
17 XSetForeground(dpy, b->gc, b->border); |
26
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
18 points[0].x = b->x; |
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
19 points[0].y = b->y; |
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
20 points[1].x = b->w - 1; |
2 | 21 points[1].y = 0; |
22 points[2].x = 0; | |
26
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
23 points[2].y = b->h - 1; |
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
24 points[3].x = -(b->w - 1); |
2 | 25 points[3].y = 0; |
26 points[4].x = 0; | |
26
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
27 points[4].y = -(b->h - 1); |
2 | 28 XDrawLines(dpy, b->drawable, b->gc, points, 5, CoordModePrevious); |
29 } | |
30 | |
31 void | |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
32 draw(Display *dpy, Brush *b, Bool border, const char *text) |
2 | 33 { |
34 unsigned int x, y, w, h, len; | |
35 static char buf[256]; | |
36 XGCValues gcv; | |
26
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
37 XRectangle r = { b->x, b->y, b->w, b->h }; |
2 | 38 |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
39 XSetForeground(dpy, b->gc, b->bg); |
26
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
40 XFillRectangles(dpy, b->drawable, b->gc, &r, 1); |
2 | 41 |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
42 if(border) |
2 | 43 drawborder(dpy, b); |
44 | |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
45 if(!text) |
2 | 46 return; |
47 | |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
48 len = strlen(text); |
2 | 49 if(len >= sizeof(buf)) |
50 len = sizeof(buf) - 1; | |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
51 memcpy(buf, text, len); |
2 | 52 buf[len] = 0; |
53 | |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
54 h = b->font.ascent + b->font.descent; |
26
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
55 y = b->y + (b->h / 2) - (h / 2) + b->font.ascent; |
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
56 x = b->x + (h / 2); |
2 | 57 |
58 /* shorten text if necessary */ | |
26
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
59 while(len && (w = textnw(&b->font, buf, len)) > b->w - h) |
2 | 60 buf[--len] = 0; |
61 | |
26
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
62 if(w > b->w) |
2 | 63 return; /* too long */ |
64 | |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
65 gcv.foreground = b->fg; |
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
66 gcv.background = b->bg; |
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
67 if(b->font.set) { |
2 | 68 XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv); |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
69 XmbDrawImageString(dpy, b->drawable, b->font.set, b->gc, |
2 | 70 x, y, buf, len); |
71 } | |
72 else { | |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
73 gcv.font = b->font.xfont->fid; |
2 | 74 XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv); |
75 XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len); | |
76 } | |
77 } | |
78 | |
79 static unsigned long | |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
80 xloadcolors(Display *dpy, Colormap cmap, const char *colstr) |
2 | 81 { |
82 XColor color; | |
83 XAllocNamedColor(dpy, cmap, colstr, &color, &color); | |
84 return color.pixel; | |
85 } | |
86 | |
87 void | |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
88 loadcolors(Display *dpy, int screen, Brush *b, |
2 | 89 const char *bg, const char *fg, const char *border) |
90 { | |
91 Colormap cmap = DefaultColormap(dpy, screen); | |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
92 b->bg = xloadcolors(dpy, cmap, bg); |
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
93 b->fg = xloadcolors(dpy, cmap, fg); |
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
94 b->border = xloadcolors(dpy, cmap, border); |
2 | 95 } |
96 | |
97 unsigned int | |
26
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
98 textnw(Fnt *font, char *text, unsigned int len) |
2 | 99 { |
26
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
100 XRectangle r; |
2 | 101 if(font->set) { |
26
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
102 XmbTextExtents(font->set, text, len, NULL, &r); |
2 | 103 return r.width; |
104 } | |
105 return XTextWidth(font->xfont, text, len); | |
106 } | |
107 | |
108 unsigned int | |
26
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
109 textw(Fnt *font, char *text) |
2 | 110 { |
26
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
111 return textnw(font, text, strlen(text)); |
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
112 } |
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
113 |
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
114 unsigned int |
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
115 texth(Fnt *font) |
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
116 { |
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
7
diff
changeset
|
117 return font->height + 4; |
2 | 118 } |
119 | |
120 void | |
121 loadfont(Display *dpy, Fnt *font, const char *fontstr) | |
122 { | |
123 char **missing, *def; | |
124 int n; | |
125 | |
7 | 126 missing = NULL; |
2 | 127 def = "?"; |
128 setlocale(LC_ALL, ""); | |
129 if(font->set) | |
130 XFreeFontSet(dpy, font->set); | |
131 font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); | |
132 if(missing) { | |
133 while(n--) | |
134 fprintf(stderr, "missing fontset: %s\n", missing[n]); | |
135 XFreeStringList(missing); | |
136 if(font->set) { | |
137 XFreeFontSet(dpy, font->set); | |
7 | 138 font->set = NULL; |
2 | 139 } |
140 } | |
141 if(font->set) { | |
142 XFontSetExtents *font_extents; | |
143 XFontStruct **xfonts; | |
144 char **font_names; | |
145 unsigned int i; | |
146 | |
147 font->ascent = font->descent = 0; | |
148 font_extents = XExtentsOfFontSet(font->set); | |
149 n = XFontsOfFontSet(font->set, &xfonts, &font_names); | |
150 for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) { | |
151 if(font->ascent < (*xfonts)->ascent) | |
152 font->ascent = (*xfonts)->ascent; | |
153 if(font->descent < (*xfonts)->descent) | |
154 font->descent = (*xfonts)->descent; | |
155 xfonts++; | |
156 } | |
157 } | |
158 else { | |
159 if(font->xfont) | |
160 XFreeFont(dpy, font->xfont); | |
7 | 161 font->xfont = NULL; |
2 | 162 font->xfont = XLoadQueryFont(dpy, fontstr); |
163 if (!font->xfont) | |
164 font->xfont = XLoadQueryFont(dpy, "fixed"); | |
165 if (!font->xfont) | |
166 error("error, cannot load 'fixed' font\n"); | |
167 font->ascent = font->xfont->ascent; | |
168 font->descent = font->xfont->descent; | |
169 } | |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
170 font->height = font->ascent + font->descent; |
2 | 171 } |