aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnselm R. Garbe <arg@suckless.org>2007-02-20 13:54:00 +0100
committerAnselm R. Garbe <arg@suckless.org>2007-02-20 13:54:00 +0100
commita1913a6af7c9cd6de18e15e989050690fdaa6600 (patch)
tree2a6269418606217571e026efab125866ef2a5376
parent66b2e8379f0b851d0535a50321e22b58fdaa247d (diff)
readded draw.c again (except getcolor and setfont)
-rw-r--r--Makefile2
-rw-r--r--dmenu.h5
-rw-r--r--draw.c71
-rw-r--r--main.c64
-rw-r--r--util.c2
5 files changed, 77 insertions, 67 deletions
diff --git a/Makefile b/Makefile
index 66040d9..18279f6 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@
3 3
4include config.mk 4include config.mk
5 5
6SRC = main.c util.c 6SRC = draw.c main.c util.c
7OBJ = ${SRC:.c=.o} 7OBJ = ${SRC:.c=.o}
8 8
9all: options dmenu 9all: options dmenu
diff --git a/dmenu.h b/dmenu.h
index 5c047ff..bb3b144 100644
--- a/dmenu.h
+++ b/dmenu.h
@@ -38,6 +38,11 @@ extern int screen;
38extern Display *dpy; 38extern Display *dpy;
39extern DC dc; /* global drawing context */ 39extern DC dc; /* global drawing context */
40 40
41/* draw.c */
42extern void drawtext(const char *text, unsigned long col[ColLast]);
43extern unsigned int textw(const char *text);
44extern unsigned int textnw(const char *text, unsigned int len);
45
41/* util.c */ 46/* util.c */
42extern void *emalloc(unsigned int size); /* allocates memory, exits on error */ 47extern void *emalloc(unsigned int size); /* allocates memory, exits on error */
43extern void eprint(const char *errstr, ...); /* prints errstr and exits with 1 */ 48extern void eprint(const char *errstr, ...); /* prints errstr and exits with 1 */
diff --git a/draw.c b/draw.c
new file mode 100644
index 0000000..145df16
--- /dev/null
+++ b/draw.c
@@ -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
10void
11drawtext(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
57unsigned int
58textw(const char *text) {
59 return textnw(text, strlen(text)) + dc.font.height;
60}
61
62unsigned int
63textnw(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}
diff --git a/main.c b/main.c
index 54a3a12..8b24bb0 100644
--- a/main.c
+++ b/main.c
@@ -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;
42static Window root; 41static Window root;
43static Window win; 42static Window win;
44 43
45static unsigned int
46textnw(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
56static unsigned int
57textw(const char *text) {
58 return textnw(text, strlen(text)) + dc.font.height;
59}
60
61static void 44static void
62calcoffsets(void) { 45calcoffsets(void) {
63 unsigned int tw, w; 46 unsigned int tw, w;
@@ -85,53 +68,6 @@ calcoffsets(void) {
85} 68}
86 69
87static void 70static void
88drawtext(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
134static void
135drawmenu(void) { 71drawmenu(void) {
136 Item *i; 72 Item *i;
137 73
diff --git a/util.c b/util.c
index 01b38bb..124bcd5 100644
--- a/util.c
+++ b/util.c
@@ -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
12void * 10void *
13emalloc(unsigned int size) { 11emalloc(unsigned int size) {