draw.c (view raw)
1/* See LICENSE file for copyright and license details. */
2#include <stdlib.h>
3#include <X11/Xlib.h>
4
5#include "draw.h"
6
7Draw *
8draw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) {
9 Draw *draw = (Draw *)calloc(1, sizeof(Draw));
10 draw->dpy = dpy;
11 draw->screen = screen;
12 draw->win = win;
13 draw->w = w;
14 draw->h = h;
15 draw->drawable = XCreatePixmap(dpy, win, w, h, DefaultDepth(dpy, screen));
16 draw->gc = XCreateGC(dpy, win, 0, NULL);
17 XSetLineAttributes(dpy, draw->gc, 1, LineSolid, CapButt, JoinMiter);
18 return draw;
19}
20
21void
22draw_resize(Draw *draw, unsigned int w, unsigned int h) {
23 if(!draw)
24 return;
25 draw->w = w;
26 draw->h = h;
27 XFreePixmap(draw->dpy, draw->drawable);
28 draw->drawable = XCreatePixmap(draw->dpy, draw->win, w, h, DefaultDepth(draw->dpy, draw->screen));
29}
30
31void
32draw_free(Draw *draw) {
33 XFreePixmap(draw->dpy, draw->drawable);
34 XFreeGC(draw->dpy, draw->gc);
35 free(draw);
36}
37
38DDC *
39dc_create(Draw *draw) {
40 DDC *dc = (DDC *)calloc(1, sizeof(DDC));
41 dc->draw = draw;
42 dc->next = draw->dc;
43 draw->dc = dc;
44 return dc;
45}
46
47void
48dc_free(DDC *dc) {
49 DDC **tdc;
50
51 if(!dc)
52 return;
53 /* remove from dc list */
54 for(tdc = &dc->draw->dc; *tdc && *tdc != dc; tdc = &(*tdc)->next);
55 *tdc = dc->next;
56 /* TODO: deallocate any resources of this dc, if needed */
57 free(dc);
58}
59
60Fnt *
61font_create(const char *fontname) {
62 Fnt *font = (Fnt *)calloc(1, sizeof(Fnt));
63 /* TODO: allocate actual font */
64 return font;
65}
66
67void
68font_free(Fnt *font) {
69 if(!font)
70 return;
71 /* TODO: deallocate any font resources */
72 free(font);
73}
74
75Col *
76col_create(const char *colname) {
77 Col *col = (Col *)calloc(1, sizeof(Col));
78 /* TODO: allocate color */
79 return col;
80}
81
82void
83col_free(Col *col) {
84 if(!col)
85 return;
86 /* TODO: deallocate any color resource */
87 free(col);
88}
89
90void
91dc_setfont(DDC *dc, Fnt *font) {
92 if(!dc || !font)
93 return;
94 dc->font = font;
95}
96
97void
98dc_setfg(DDC *dc, Col *col) {
99 if(!dc || !col)
100 return;
101 dc->fg = col;
102}
103
104void
105dc_setbg(DDC *dc, Col *col) {
106 if(!dc || !col)
107 return;
108 dc->bg = col;
109}
110
111void
112dc_setfill(DDC *dc, Bool fill) {
113 if(!dc)
114 return;
115 dc->fill = fill;
116}
117
118void
119dc_drawrect(DDC *dc, int x, int y, unsigned int w, unsigned int h) {
120 if(!dc)
121 return;
122 /* TODO: draw the rectangle */
123}
124
125void
126dc_drawtext(DDC *dc, int x, int y, const char *text) {
127 if(!dc)
128 return;
129 /* TODO: draw the text */
130}
131
132void
133dc_map(DDC *dc, int x, int y, unsigned int w, unsigned int h) {
134 if(!dc)
135 return;
136 /* TODO: map the dc contents in the region */
137}
138
139void
140dc_getextents(DDC *dc, const char *text, TextExtents *extents) {
141 if(!dc || !extents)
142 return;
143 /* TODO: get extents */
144}