1 /* See LICENSE file for copyright and license details. */
10 draw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) {
11 Draw *draw = (Draw *)calloc(1, sizeof(Draw));
13 draw->screen = screen;
17 draw->drawable = XCreatePixmap(dpy, win, w, h, DefaultDepth(dpy, screen));
18 draw->gc = XCreateGC(dpy, win, 0, NULL);
19 XSetLineAttributes(dpy, draw->gc, 1, LineSolid, CapButt, JoinMiter);
24 draw_resize(Draw *draw, unsigned int w, unsigned int h) {
29 XFreePixmap(draw->dpy, draw->drawable);
30 draw->drawable = XCreatePixmap(draw->dpy, draw->win, w, h, DefaultDepth(draw->dpy, draw->screen));
34 draw_free(Draw *draw) {
35 XFreePixmap(draw->dpy, draw->drawable);
36 XFreeGC(draw->dpy, draw->gc);
41 draw_font_create(Draw *draw, const char *fontname) {
48 font = (Fnt *)calloc(1, sizeof(Fnt));
49 font->set = XCreateFontSet(draw->dpy, fontname, &missing, &n, &def);
52 fprintf(stderr, "draw: missing fontset: %s\n", missing[n]);
53 XFreeStringList(missing);
59 XExtentsOfFontSet(font->set);
60 n = XFontsOfFontSet(font->set, &xfonts, &font_names);
62 font->ascent = MAX(font->ascent, (*xfonts)->ascent);
63 font->descent = MAX(font->descent,(*xfonts)->descent);
68 if(!(font->xfont = XLoadQueryFont(draw->dpy, fontname))
69 && !(font->xfont = XLoadQueryFont(draw->dpy, "fixed")))
70 die("error, cannot load font: '%s'\n", fontname);
71 font->ascent = font->xfont->ascent;
72 font->descent = font->xfont->descent;
74 font->h = font->ascent + font->descent;
79 draw_font_free(Draw *draw, Fnt *font) {
83 XFreeFontSet(draw->dpy, font->set);
85 XFreeFont(draw->dpy, font->xfont);
90 draw_col_create(Draw *draw, const char *colname) {
91 Col *col = (Col *)calloc(1, sizeof(Col));
92 Colormap cmap = DefaultColormap(draw->dpy, draw->screen);
95 if(!XAllocNamedColor(draw->dpy, cmap, colname, &color, &color))
96 die("error, cannot allocate color '%s'\n", colname);
97 col->rgb = color.pixel;
102 draw_col_free(Draw *draw, Col *col) {
109 draw_setfont(Draw *draw, Fnt *font) {
116 draw_setfg(Draw *draw, Col *col) {
123 draw_setbg(Draw *draw, Col *col) {
130 draw_rect(Draw *draw, int x, int y, unsigned int w, unsigned int h) {
133 /* TODO: draw the rectangle */
137 draw_text(Draw *draw, int x, int y, const char *text) {
140 /* TODO: draw the text */
144 draw_map(Draw *draw, int x, int y, unsigned int w, unsigned int h) {
147 /* TODO: map the draw contents in the region */
151 draw_getextents(Draw *draw, const char *text, TextExtents *extents) {
152 if(!draw || !extents)
154 /* TODO: get extents */