WINGs buttons and labels now have text
[wmaker-crm.git] / test / cairo / draw.c
blob515d4b60dd0ae77aa4c6e15793588483ca6a5221
1 #include <stdio.h>
2 #include <X11/Xlib.h>
3 #include <X11/Xutil.h>
4 #include <X11/Xproto.h>
5 #include <cairo/cairo.h>
8 /* This is meant to be a simple app to quickly test some cairo stuff.
9 * It creates a single window and runs the "draw" function when it
10 * receives an ExposeEvent. The idea is that you can put some quick
11 * cairo code you want to try in draw and very quickly see the results
12 * -- JH
14 static void curve_rectangle(cairo_t *cr,
15 double x0, double y0, double rect_width, double rect_height,
16 double radius)
18 double x1,y1;
20 x1=x0+rect_width;
21 y1=y0+rect_height;
22 if (!rect_width || !rect_height)
23 return;
24 if (rect_width/2<radius) {
25 if (rect_height/2<radius) {
26 cairo_move_to (cr, x0, (y0 + y1)/2);
27 cairo_curve_to (cr, x0 ,y0, x0, y0, (x0 + x1)/2, y0);
28 cairo_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1)/2);
29 cairo_curve_to (cr, x1, y1, x1, y1, (x1 + x0)/2, y1);
30 cairo_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1)/2);
31 } else {
32 cairo_move_to (cr, x0, y0 + radius);
33 cairo_curve_to (cr, x0 ,y0, x0, y0, (x0 + x1)/2, y0);
34 cairo_curve_to (cr, x1, y0, x1, y0, x1, y0 + radius);
35 cairo_line_to (cr, x1 , y1 - radius);
36 cairo_curve_to (cr, x1, y1, x1, y1, (x1 + x0)/2, y1);
37 cairo_curve_to (cr, x0, y1, x0, y1, x0, y1- radius);
39 } else {
40 if (rect_height/2<radius) {
41 cairo_move_to (cr, x0, (y0 + y1)/2);
42 cairo_curve_to (cr, x0 , y0, x0 , y0, x0 + radius, y0);
43 cairo_line_to (cr, x1 - radius, y0);
44 cairo_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1)/2);
45 cairo_curve_to (cr, x1, y1, x1, y1, x1 - radius, y1);
46 cairo_line_to (cr, x0 + radius, y1);
47 cairo_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1)/2);
48 } else {
49 cairo_move_to (cr, x0, y0 + radius);
50 cairo_curve_to (cr, x0 , y0, x0 , y0, x0 + radius, y0);
51 cairo_line_to (cr, x1 - radius, y0);
52 cairo_curve_to (cr, x1, y0, x1, y0, x1, y0 + radius);
53 cairo_line_to (cr, x1 , y1 - radius);
54 cairo_curve_to (cr, x1, y1, x1, y1, x1 - radius, y1);
55 cairo_line_to (cr, x0 + radius, y1);
56 cairo_curve_to (cr, x0, y1, x0, y1, x0, y1- radius);
59 cairo_close_path (cr);
62 void draw(Display *dpy, Screen *scr, Visual *vis, Window win) {
63 cairo_surface_t *surf;
64 cairo_t *ct;
66 int width=200;
67 int height = 50;
68 int x = 10;
69 int y = 10;
71 unsigned char border[4]= {0x00, 0x00, 0x00, 0x70};
72 //unsigned char color1[4]= {0x8c, 0xb1, 0xbc, 0xff};
73 //unsigned char color2[4]= {0xcb, 0xf3, 0xff, 0xff};
74 unsigned char color1[4]= {0x0, 0x0, 0x0, 0xff};
75 unsigned char color2[4]= {0xcf, 0xcf, 0xcf, 0xff};
76 unsigned char scolor1[4]= {0xff, 0xff, 0xff, 0xe5};
77 unsigned char scolor2[4]= {0xff, 0xff, 0xff, 0x70};
79 surf = (cairo_surface_t *) cairo_xlib_surface_create(dpy, win, vis, 500, 500);
80 ct = cairo_create(surf);
82 cairo_pattern_t *shine;
83 cairo_pattern_t *base;
85 shine= cairo_pattern_create_linear(0, 0, 0, height*2/5);
86 cairo_pattern_add_color_stop_rgba(shine, 0,
87 scolor1[0]/255.0, scolor1[1]/255.0, scolor1[2]/255.0,
88 scolor1[3]/255.0);
89 cairo_pattern_add_color_stop_rgba(shine, 1,
90 scolor2[0]/255.0, scolor2[1]/255.0, scolor2[2]/255.0,
91 scolor2[3]/255.0);
93 base= cairo_pattern_create_linear(0, 0, 0, height-1);
94 cairo_pattern_add_color_stop_rgba(base, 0,
95 color1[0]/255.0, color1[1]/255.0, color1[2]/255.0,
96 color1[3]/255.0);
97 cairo_pattern_add_color_stop_rgba(base, 1,
98 color2[0]/255.0, color2[1]/255.0, color2[2]/255.0,
99 color2[3]/255.0);
102 curve_rectangle(ct, x, y, width-1, height-1, height*2/3);
103 cairo_set_source(ct, base);
104 cairo_fill_preserve(ct);
105 cairo_clip(ct);
107 curve_rectangle(ct, x, y, width-1, height*2/5, width);
108 cairo_set_source(ct, shine);
109 cairo_fill(ct);
111 curve_rectangle(ct, x, y, width-1, height-1, height*2/3);
112 cairo_set_source_rgba(ct, border[0]/255.0, border[1]/255.0, border[2]/255.0, border[3]/255.0);
113 cairo_set_line_width(ct, 2.0);
114 cairo_stroke(ct);
116 cairo_pattern_destroy(shine);
117 cairo_pattern_destroy(base);
119 cairo_destroy(ct);
120 cairo_surface_destroy(surf);
123 int main() {
125 Display *dpy;
126 Window win;
127 Screen *scr;
128 int scrnum;
129 Visual *vis;
130 Atom delete_win;
131 Atom prots[6];
132 XClassHint classhint;
133 XSetWindowAttributes win_attr;
134 char win_title[32];
135 char alive;
137 dpy = XOpenDisplay("");
138 if (!dpy) {
139 printf("could not open display");
140 return 1;
143 delete_win = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
144 prots[0] = delete_win;
146 scr = DefaultScreenOfDisplay(dpy);
147 scrnum = DefaultScreen(dpy);
148 vis = DefaultVisualOfScreen(scr);
150 win_attr.background_pixel = WhitePixel(dpy,scrnum);
151 win_attr.event_mask = ExposureMask;
153 win = XCreateWindow(dpy,
154 DefaultRootWindow(dpy),
157 200,
158 200,
160 CopyFromParent,
161 InputOutput,
162 vis,
163 CWEventMask | CWBackPixel,
164 &win_attr);
166 XSetWMProtocols(dpy,win,prots,1);
168 sprintf(win_title,"Cairo Test");
169 XStoreName(dpy,win,win_title);
171 classhint.res_name = "cairo";
172 classhint.res_class = "Cairo";
173 XSetClassHint(dpy,win,&classhint);
175 XMapWindow(dpy,win);
177 XFlush(dpy);
178 alive = 1;
179 while (alive) {
180 XEvent ev;
181 XNextEvent(dpy, &ev);
182 if (ev.type == ClientMessage) {
183 if (ev.xclient.data.l[0] == delete_win) {
184 XDestroyWindow(dpy, ev.xclient.window);
185 alive = 0;
188 if (ev.type == Expose) {
189 draw(dpy,scr,vis,win);
194 return 0;