summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--draw.c75
-rw-r--r--draw.h25
2 files changed, 71 insertions, 29 deletions
diff --git a/draw.c b/draw.c
index e88a70c..fa340ff 100644
--- a/draw.c
+++ b/draw.c
@@ -1,6 +1,7 @@
1/* See LICENSE file for copyright and license details. */ 1/* See LICENSE file for copyright and license details. */
2#include <stdio.h> 2#include <stdio.h>
3#include <stdlib.h> 3#include <stdlib.h>
4#include <string.h>
4#include <X11/Xlib.h> 5#include <X11/Xlib.h>
5 6
6#include "draw.h" 7#include "draw.h"
@@ -55,7 +56,6 @@ draw_font_create(Draw *draw, const char *fontname) {
55 if(font->set) { 56 if(font->set) {
56 XFontStruct **xfonts; 57 XFontStruct **xfonts;
57 char **font_names; 58 char **font_names;
58
59 XExtentsOfFontSet(font->set); 59 XExtentsOfFontSet(font->set);
60 n = XFontsOfFontSet(font->set, &xfonts, &font_names); 60 n = XFontsOfFontSet(font->set, &xfonts, &font_names);
61 while(n--) { 61 while(n--) {
@@ -107,49 +107,96 @@ draw_col_free(Draw *draw, Col *col) {
107 107
108void 108void
109draw_setfont(Draw *draw, Fnt *font) { 109draw_setfont(Draw *draw, Fnt *font) {
110 if(!draw || !font) 110 if(!draw)
111 return; 111 return;
112 draw->font = font; 112 draw->font = font;
113} 113}
114 114
115void 115void
116draw_setfg(Draw *draw, Col *col) { 116draw_setfg(Draw *draw, Col *col) {
117 if(!draw || !col) 117 if(!draw)
118 return; 118 return;
119 draw->fg = col; 119 draw->fg = col;
120} 120}
121 121
122void 122void
123draw_setbg(Draw *draw, Col *col) { 123draw_setbg(Draw *draw, Col *col) {
124 if(!draw || !col) 124 if(!draw)
125 return; 125 return;
126 draw->bg = col; 126 draw->bg = col;
127} 127}
128 128
129void 129void
130draw_rect(Draw *draw, int x, int y, unsigned int w, unsigned int h) { 130draw_rect(Draw *draw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert) {
131 if(!draw) 131 int dx;
132
133 if(!draw || !draw->font || !draw->fg || !draw->bg)
132 return; 134 return;
133 /* TODO: draw the rectangle */ 135 XSetForeground(draw->dpy, draw->gc, invert ? draw->bg->rgb : draw->fg->rgb);
136 dx = (draw->font->ascent + draw->font->descent + 2) / 4;
137 if(filled)
138 XFillRectangle(draw->dpy, draw->drawable, draw->gc, x+1, y+1, dx+1, dx+1);
139 else if(empty)
140 XDrawRectangle(draw->dpy, draw->drawable, draw->gc, x+1, y+1, dx, dx);
134} 141}
135 142
136void 143void
137draw_text(Draw *draw, int x, int y, const char *text) { 144draw_text(Draw *draw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert) {
138 if(!draw) 145 char buf[256];
146 int i, tx, ty, len, olen;
147 TextExtents tex;
148
149 if(!draw || !draw->fg || !draw->bg)
139 return; 150 return;
140 /* TODO: draw the text */ 151 XSetForeground(draw->dpy, draw->gc, invert ? draw->fg->rgb : draw->bg->rgb);
152 XFillRectangle(draw->dpy, draw->drawable, draw->gc, x, y, w, h);
153 if(!text || !draw->font)
154 return;
155 olen = strlen(text);
156 draw_getextents(draw, text, olen, &tex);
157 ty = y + (h / 2) - tex.yOff;
158 tx = x + tex.xOff;
159 /* shorten text if necessary */
160 for(len = MIN(olen, sizeof buf); len && tex.w > w - tex.h; len--)
161 draw_getextents(draw, text, len, &tex);
162 if(!len)
163 return;
164 memcpy(buf, text, len);
165 if(len < olen)
166 for(i = len; i && i > len - 3; buf[--i] = '.');
167 XSetForeground(draw->dpy, draw->gc, invert ? draw->bg->rgb : draw->fg->rgb);
168 if(draw->font->set)
169 XmbDrawString(draw->dpy, draw->drawable, draw->font->set, draw->gc, tx, ty, buf, len);
170 else
171 XDrawString(draw->dpy, draw->drawable, draw->gc, tx, ty, buf, len);
141} 172}
142 173
143void 174void
144draw_map(Draw *draw, int x, int y, unsigned int w, unsigned int h) { 175draw_map(Draw *draw, int x, int y, unsigned int w, unsigned int h) {
145 if(!draw) 176 if(!draw)
146 return; 177 return;
147 /* TODO: map the draw contents in the region */ 178 XCopyArea(draw->dpy, draw->drawable, draw->win, draw->gc, x, y, w, h, x, y);
179 XSync(draw->dpy, False);
148} 180}
149 181
182
150void 183void
151draw_getextents(Draw *draw, const char *text, TextExtents *extents) { 184draw_getextents(Draw *draw, const char *text, unsigned int len, TextExtents *extents) {
152 if(!draw || !extents) 185 XRectangle r;
186
187 if(!draw || !draw->font || !text)
153 return; 188 return;
154 /* TODO: get extents */ 189 if(draw->font->set) {
190 XmbTextExtents(draw->font->set, text, len, NULL, &r);
191 extents->xOff = r.x;
192 extents->yOff = r.y;
193 extents->w = r.width;
194 extents->h = r.height;
195 }
196 else {
197 extents->h = draw->font->ascent + draw->font->descent;
198 extents->w = XTextWidth(draw->font->xfont, text, len);
199 extents->xOff = extents->h / 2;
200 extents->yOff = (extents->h / 2) + draw->font->ascent;
201 }
155} 202}
diff --git a/draw.h b/draw.h
index 0c811ab..17d2c89 100644
--- a/draw.h
+++ b/draw.h
@@ -13,19 +13,7 @@ struct _XFont {
13 XFontStruct *xfont; 13 XFontStruct *xfont;
14}; 14};
15typedef struct _XFont Fnt; 15typedef struct _XFont Fnt;
16/* X11 types - end */
17 16
18typedef struct {
19 unsigned int w;
20 unsigned int h;
21 int x;
22 int y;
23 int xOff;
24 int yOff;
25} TextExtents;
26
27
28/* X11 types - begin */
29typedef struct _XDraw Draw; 17typedef struct _XDraw Draw;
30struct _XDraw { 18struct _XDraw {
31 unsigned int w, h; 19 unsigned int w, h;
@@ -39,6 +27,13 @@ struct _XDraw {
39 Fnt *font; 27 Fnt *font;
40}; 28};
41 29
30typedef struct {
31 unsigned int w;
32 unsigned int h;
33 int xOff;
34 int yOff;
35} TextExtents;
36
42/* Drawable abstraction */ 37/* Drawable abstraction */
43Draw *draw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h); 38Draw *draw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h);
44void draw_resize(Draw *draw, unsigned int w, unsigned int h); 39void draw_resize(Draw *draw, unsigned int w, unsigned int h);
@@ -58,12 +53,12 @@ void draw_setfg(Draw *draw, Col *col);
58void draw_setbg(Draw *draw, Col *col); 53void draw_setbg(Draw *draw, Col *col);
59 54
60/* Drawing functions */ 55/* Drawing functions */
61void draw_rect(Draw *draw, int x, int y, unsigned int w, unsigned int h); 56void draw_rect(Draw *draw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert);
62void draw_text(Draw *draw, int x, int y, const char *text); 57void draw_text(Draw *draw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert);
63 58
64/* Map functions */ 59/* Map functions */
65void draw_map(Draw *draw, int x, int y, unsigned int w, unsigned int h); 60void draw_map(Draw *draw, int x, int y, unsigned int w, unsigned int h);
66 61
67/* Text functions */ 62/* Text functions */
68void draw_getextents(Draw *draw, const char *text, TextExtents *extents); 63void draw_getextents(Draw *draw, const char *text, unsigned int len, TextExtents *extents);
69 64