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
14 static void curve_rectangle(cairo_t
*cr
,
15 double x0
, double y0
, double rect_width
, double rect_height
,
22 if (!rect_width
|| !rect_height
)
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);
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
);
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);
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
;
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,
89 cairo_pattern_add_color_stop_rgba(shine
, 1,
90 scolor2
[0]/255.0, scolor2
[1]/255.0, scolor2
[2]/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,
97 cairo_pattern_add_color_stop_rgba(base
, 1,
98 color2
[0]/255.0, color2
[1]/255.0, color2
[2]/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
);
107 curve_rectangle(ct
, x
, y
, width
-1, height
*2/5, width
);
108 cairo_set_source(ct
, shine
);
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);
116 cairo_pattern_destroy(shine
);
117 cairo_pattern_destroy(base
);
120 cairo_surface_destroy(surf
);
132 XClassHint classhint
;
133 XSetWindowAttributes win_attr
;
137 dpy
= XOpenDisplay("");
139 printf("could not open display");
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
),
163 CWEventMask
| CWBackPixel
,
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
);
181 XNextEvent(dpy
, &ev
);
182 if (ev
.type
== ClientMessage
) {
183 if (ev
.xclient
.data
.l
[0] == delete_win
) {
184 XDestroyWindow(dpy
, ev
.xclient
.window
);
188 if (ev
.type
== Expose
) {
189 draw(dpy
,scr
,vis
,win
);