diff options
| author | Anselm R Garbe <anselm@garbe.us> | 2012-12-09 19:11:11 +0100 |
|---|---|---|
| committer | Anselm R Garbe <anselm@garbe.us> | 2012-12-09 19:11:11 +0100 |
| commit | aafeaf731701e528f4ae9d7c7432b3e92fee4392 (patch) | |
| tree | 5e47eacfcecae118e738558ee7355cde769c1157 /draw.c | |
| parent | continued with draw.c abstraction, also started util.{h,c} implementation, th... (diff) | |
| download | dwm-aafeaf731701e528f4ae9d7c7432b3e92fee4392.tar.gz dwm-aafeaf731701e528f4ae9d7c7432b3e92fee4392.zip | |
continued with draw.c and draw.h implementation, now the integration begins
Diffstat (limited to 'draw.c')
| -rw-r--r-- | draw.c | 75 |
1 files changed, 61 insertions, 14 deletions
| @@ -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 | ||
| 108 | void | 108 | void |
| 109 | draw_setfont(Draw *draw, Fnt *font) { | 109 | draw_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 | ||
| 115 | void | 115 | void |
| 116 | draw_setfg(Draw *draw, Col *col) { | 116 | draw_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 | ||
| 122 | void | 122 | void |
| 123 | draw_setbg(Draw *draw, Col *col) { | 123 | draw_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 | ||
| 129 | void | 129 | void |
| 130 | draw_rect(Draw *draw, int x, int y, unsigned int w, unsigned int h) { | 130 | draw_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 | ||
| 136 | void | 143 | void |
| 137 | draw_text(Draw *draw, int x, int y, const char *text) { | 144 | draw_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 | ||
| 143 | void | 174 | void |
| 144 | draw_map(Draw *draw, int x, int y, unsigned int w, unsigned int h) { | 175 | draw_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 | |||
| 150 | void | 183 | void |
| 151 | draw_getextents(Draw *draw, const char *text, TextExtents *extents) { | 184 | draw_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 | } |
