Mercurial > dwm-meillo
annotate draw.c @ 34:cd30cce52b78
added logo+description
author | Anselm R. Garbe <garbeam@wmii.de> |
---|---|
date | Thu, 13 Jul 2006 09:32:22 +0200 |
parents | e90449e03167 |
children | 989178822938 |
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 | |
32 | 9 #include <X11/Xlocale.h> |
10 | |
11 #include "wm.h" | |
2 | 12 |
13 static void | |
34 | 14 drawborder(void) |
2 | 15 { |
16 XPoint points[5]; | |
34 | 17 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter); |
18 XSetForeground(dpy, dc.gc, dc.border); | |
19 points[0].x = dc.x; | |
20 points[0].y = dc.y; | |
21 points[1].x = dc.w - 1; | |
2 | 22 points[1].y = 0; |
23 points[2].x = 0; | |
34 | 24 points[2].y = dc.h - 1; |
25 points[3].x = -(dc.w - 1); | |
2 | 26 points[3].y = 0; |
27 points[4].x = 0; | |
34 | 28 points[4].y = -(dc.h - 1); |
29 XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious); | |
2 | 30 } |
31 | |
32 void | |
34 | 33 draw(Bool border, const char *text) |
2 | 34 { |
33
e90449e03167
new stuff (some warning elimination)
Anselm R. Garbe <garbeam@wmii.de>
parents:
32
diff
changeset
|
35 int x, y, w, h; |
e90449e03167
new stuff (some warning elimination)
Anselm R. Garbe <garbeam@wmii.de>
parents:
32
diff
changeset
|
36 unsigned int len; |
2 | 37 static char buf[256]; |
38 XGCValues gcv; | |
34 | 39 XRectangle r = { dc.x, dc.y, dc.w, dc.h }; |
2 | 40 |
34 | 41 XSetForeground(dpy, dc.gc, dc.bg); |
42 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); | |
2 | 43 |
30
2e0fb4130bfb
new stuff, fixed several issues
Anselm R. Garbe <garbeam@wmii.de>
parents:
26
diff
changeset
|
44 w = 0; |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
45 if(border) |
34 | 46 drawborder(); |
2 | 47 |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
48 if(!text) |
2 | 49 return; |
50 | |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
51 len = strlen(text); |
2 | 52 if(len >= sizeof(buf)) |
53 len = sizeof(buf) - 1; | |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
54 memcpy(buf, text, len); |
2 | 55 buf[len] = 0; |
56 | |
34 | 57 h = dc.font.ascent + dc.font.descent; |
58 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; | |
59 x = dc.x + (h / 2); | |
2 | 60 |
61 /* shorten text if necessary */ | |
34 | 62 while(len && (w = textnw(&dc.font, buf, len)) > dc.w - h) |
2 | 63 buf[--len] = 0; |
64 | |
34 | 65 if(w > dc.w) |
2 | 66 return; /* too long */ |
67 | |
34 | 68 gcv.foreground = dc.fg; |
69 gcv.background = dc.bg; | |
70 if(dc.font.set) { | |
71 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv); | |
72 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc, | |
2 | 73 x, y, buf, len); |
74 } | |
75 else { | |
34 | 76 gcv.font = dc.font.xfont->fid; |
77 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv); | |
78 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len); | |
2 | 79 } |
80 } | |
81 | |
82 static unsigned long | |
34 | 83 xinitcolors(Colormap cmap, const char *colstr) |
2 | 84 { |
85 XColor color; | |
86 XAllocNamedColor(dpy, cmap, colstr, &color, &color); | |
87 return color.pixel; | |
88 } | |
89 | |
90 void | |
34 | 91 initcolors(const char *bg, const char *fg, const char *border) |
2 | 92 { |
34 | 93 Colormap cmap = DefaultColormap(dpy, screen); |
94 dc.bg = xinitcolors(cmap, bg); | |
95 dc.fg = xinitcolors(cmap, fg); | |
96 dc.border = xinitcolors(cmap, border); | |
2 | 97 } |
98 | |
99 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
|
100 textnw(Fnt *font, char *text, unsigned int len) |
2 | 101 { |
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 XRectangle r; |
2 | 103 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
|
104 XmbTextExtents(font->set, text, len, NULL, &r); |
2 | 105 return r.width; |
106 } | |
107 return XTextWidth(font->xfont, text, len); | |
108 } | |
109 | |
110 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
|
111 textw(Fnt *font, char *text) |
2 | 112 { |
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
|
113 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
|
114 } |
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 |
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 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
|
117 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
|
118 { |
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
|
119 return font->height + 4; |
2 | 120 } |
121 | |
122 void | |
34 | 123 initfont(Fnt *font, const char *fontstr) |
2 | 124 { |
125 char **missing, *def; | |
33
e90449e03167
new stuff (some warning elimination)
Anselm R. Garbe <garbeam@wmii.de>
parents:
32
diff
changeset
|
126 int i, n; |
2 | 127 |
7 | 128 missing = NULL; |
2 | 129 setlocale(LC_ALL, ""); |
130 if(font->set) | |
131 XFreeFontSet(dpy, font->set); | |
132 font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); | |
133 if(missing) { | |
134 while(n--) | |
135 fprintf(stderr, "missing fontset: %s\n", missing[n]); | |
136 XFreeStringList(missing); | |
137 if(font->set) { | |
138 XFreeFontSet(dpy, font->set); | |
7 | 139 font->set = NULL; |
2 | 140 } |
141 } | |
142 if(font->set) { | |
143 XFontSetExtents *font_extents; | |
144 XFontStruct **xfonts; | |
145 char **font_names; | |
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) | |
34 | 166 error("error, cannot init 'fixed' font\n"); |
2 | 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 } |