diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | dmenu.h | 5 | ||||
-rw-r--r-- | draw.c | 71 | ||||
-rw-r--r-- | main.c | 64 | ||||
-rw-r--r-- | util.c | 2 |
5 files changed, 77 insertions, 67 deletions
@@ -3,7 +3,7 @@ | |||
3 | 3 | ||
4 | include config.mk | 4 | include config.mk |
5 | 5 | ||
6 | SRC = main.c util.c | 6 | SRC = draw.c main.c util.c |
7 | OBJ = ${SRC:.c=.o} | 7 | OBJ = ${SRC:.c=.o} |
8 | 8 | ||
9 | all: options dmenu | 9 | all: options dmenu |
@@ -38,6 +38,11 @@ extern int screen; | |||
38 | extern Display *dpy; | 38 | extern Display *dpy; |
39 | extern DC dc; /* global drawing context */ | 39 | extern DC dc; /* global drawing context */ |
40 | 40 | ||
41 | /* draw.c */ | ||
42 | extern void drawtext(const char *text, unsigned long col[ColLast]); | ||
43 | extern unsigned int textw(const char *text); | ||
44 | extern unsigned int textnw(const char *text, unsigned int len); | ||
45 | |||
41 | /* util.c */ | 46 | /* util.c */ |
42 | extern void *emalloc(unsigned int size); /* allocates memory, exits on error */ | 47 | extern void *emalloc(unsigned int size); /* allocates memory, exits on error */ |
43 | extern void eprint(const char *errstr, ...); /* prints errstr and exits with 1 */ | 48 | extern void eprint(const char *errstr, ...); /* prints errstr and exits with 1 */ |
@@ -0,0 +1,71 @@ | |||
1 | /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com> | ||
2 | * (C)opyright MMVI-MMVII Sander van Dijk <a dot h dot vandijk at gmail dot com> | ||
3 | * See LICENSE file for license details. | ||
4 | */ | ||
5 | #include "dmenu.h" | ||
6 | #include <string.h> | ||
7 | |||
8 | /* extern */ | ||
9 | |||
10 | void | ||
11 | drawtext(const char *text, unsigned long col[ColLast]) { | ||
12 | int x, y, w, h; | ||
13 | static char buf[256]; | ||
14 | unsigned int len, olen; | ||
15 | XGCValues gcv; | ||
16 | XRectangle r = { dc.x, dc.y, dc.w, dc.h }; | ||
17 | |||
18 | XSetForeground(dpy, dc.gc, col[ColBG]); | ||
19 | XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); | ||
20 | if(!text) | ||
21 | return; | ||
22 | w = 0; | ||
23 | olen = len = strlen(text); | ||
24 | if(len >= sizeof buf) | ||
25 | len = sizeof buf - 1; | ||
26 | memcpy(buf, text, len); | ||
27 | buf[len] = 0; | ||
28 | h = dc.font.ascent + dc.font.descent; | ||
29 | y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; | ||
30 | x = dc.x + (h / 2); | ||
31 | /* shorten text if necessary */ | ||
32 | while(len && (w = textnw(buf, len)) > dc.w - h) | ||
33 | buf[--len] = 0; | ||
34 | if(len < olen) { | ||
35 | if(len > 1) | ||
36 | buf[len - 1] = '.'; | ||
37 | if(len > 2) | ||
38 | buf[len - 2] = '.'; | ||
39 | if(len > 3) | ||
40 | buf[len - 3] = '.'; | ||
41 | } | ||
42 | if(w > dc.w) | ||
43 | return; /* too long */ | ||
44 | gcv.foreground = col[ColFG]; | ||
45 | if(dc.font.set) { | ||
46 | XChangeGC(dpy, dc.gc, GCForeground, &gcv); | ||
47 | XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, | ||
48 | x, y, buf, len); | ||
49 | } | ||
50 | else { | ||
51 | gcv.font = dc.font.xfont->fid; | ||
52 | XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv); | ||
53 | XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len); | ||
54 | } | ||
55 | } | ||
56 | |||
57 | unsigned int | ||
58 | textw(const char *text) { | ||
59 | return textnw(text, strlen(text)) + dc.font.height; | ||
60 | } | ||
61 | |||
62 | unsigned int | ||
63 | textnw(const char *text, unsigned int len) { | ||
64 | XRectangle r; | ||
65 | |||
66 | if(dc.font.set) { | ||
67 | XmbTextExtents(dc.font.set, text, len, NULL, &r); | ||
68 | return r.width; | ||
69 | } | ||
70 | return XTextWidth(dc.font.xfont, text, len); | ||
71 | } | ||
@@ -3,7 +3,6 @@ | |||
3 | * See LICENSE file for license details. | 3 | * See LICENSE file for license details. |
4 | */ | 4 | */ |
5 | #include "dmenu.h" | 5 | #include "dmenu.h" |
6 | |||
7 | #include <ctype.h> | 6 | #include <ctype.h> |
8 | #include <locale.h> | 7 | #include <locale.h> |
9 | #include <stdlib.h> | 8 | #include <stdlib.h> |
@@ -42,22 +41,6 @@ static Item *curr = NULL; | |||
42 | static Window root; | 41 | static Window root; |
43 | static Window win; | 42 | static Window win; |
44 | 43 | ||
45 | static unsigned int | ||
46 | textnw(const char *text, unsigned int len) { | ||
47 | XRectangle r; | ||
48 | |||
49 | if(dc.font.set) { | ||
50 | XmbTextExtents(dc.font.set, text, len, NULL, &r); | ||
51 | return r.width; | ||
52 | } | ||
53 | return XTextWidth(dc.font.xfont, text, len); | ||
54 | } | ||
55 | |||
56 | static unsigned int | ||
57 | textw(const char *text) { | ||
58 | return textnw(text, strlen(text)) + dc.font.height; | ||
59 | } | ||
60 | |||
61 | static void | 44 | static void |
62 | calcoffsets(void) { | 45 | calcoffsets(void) { |
63 | unsigned int tw, w; | 46 | unsigned int tw, w; |
@@ -85,53 +68,6 @@ calcoffsets(void) { | |||
85 | } | 68 | } |
86 | 69 | ||
87 | static void | 70 | static void |
88 | drawtext(const char *text, unsigned long col[ColLast]) { | ||
89 | int x, y, w, h; | ||
90 | static char buf[256]; | ||
91 | unsigned int len, olen; | ||
92 | XGCValues gcv; | ||
93 | XRectangle r = { dc.x, dc.y, dc.w, dc.h }; | ||
94 | |||
95 | XSetForeground(dpy, dc.gc, col[ColBG]); | ||
96 | XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); | ||
97 | if(!text) | ||
98 | return; | ||
99 | w = 0; | ||
100 | olen = len = strlen(text); | ||
101 | if(len >= sizeof buf) | ||
102 | len = sizeof buf - 1; | ||
103 | memcpy(buf, text, len); | ||
104 | buf[len] = 0; | ||
105 | h = dc.font.ascent + dc.font.descent; | ||
106 | y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; | ||
107 | x = dc.x + (h / 2); | ||
108 | /* shorten text if necessary */ | ||
109 | while(len && (w = textnw(buf, len)) > dc.w - h) | ||
110 | buf[--len] = 0; | ||
111 | if(len < olen) { | ||
112 | if(len > 1) | ||
113 | buf[len - 1] = '.'; | ||
114 | if(len > 2) | ||
115 | buf[len - 2] = '.'; | ||
116 | if(len > 3) | ||
117 | buf[len - 3] = '.'; | ||
118 | } | ||
119 | if(w > dc.w) | ||
120 | return; /* too long */ | ||
121 | gcv.foreground = col[ColFG]; | ||
122 | if(dc.font.set) { | ||
123 | XChangeGC(dpy, dc.gc, GCForeground, &gcv); | ||
124 | XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, | ||
125 | x, y, buf, len); | ||
126 | } | ||
127 | else { | ||
128 | gcv.font = dc.font.xfont->fid; | ||
129 | XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv); | ||
130 | XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len); | ||
131 | } | ||
132 | } | ||
133 | |||
134 | static void | ||
135 | drawmenu(void) { | 71 | drawmenu(void) { |
136 | Item *i; | 72 | Item *i; |
137 | 73 | ||
@@ -6,8 +6,6 @@ | |||
6 | #include <stdio.h> | 6 | #include <stdio.h> |
7 | #include <stdlib.h> | 7 | #include <stdlib.h> |
8 | #include <string.h> | 8 | #include <string.h> |
9 | #include <sys/wait.h> | ||
10 | #include <unistd.h> | ||
11 | 9 | ||
12 | void * | 10 | void * |
13 | emalloc(unsigned int size) { | 11 | emalloc(unsigned int size) { |