tests: add a test for test_glist_gtype_container_in()
[pygobject.git] / examples / cairo-demo.py
blob7b90ec3ec7b6f6a39921ee3fef7c5d26c52c6489
1 #!/usr/bin/env python
2 """
3 Based on cairo-demo/X11/cairo-demo.c
4 """
6 import cairo
7 from gi.repository import Gtk
9 SIZE = 30
12 def triangle(ctx):
13 ctx.move_to(SIZE, 0)
14 ctx.rel_line_to(SIZE, 2 * SIZE)
15 ctx.rel_line_to(-2 * SIZE, 0)
16 ctx.close_path()
19 def square(ctx):
20 ctx.move_to(0, 0)
21 ctx.rel_line_to(2 * SIZE, 0)
22 ctx.rel_line_to(0, 2 * SIZE)
23 ctx.rel_line_to(-2 * SIZE, 0)
24 ctx.close_path()
27 def bowtie(ctx):
28 ctx.move_to(0, 0)
29 ctx.rel_line_to(2 * SIZE, 2 * SIZE)
30 ctx.rel_line_to(-2 * SIZE, 0)
31 ctx.rel_line_to(2 * SIZE, -2 * SIZE)
32 ctx.close_path()
35 def inf(ctx):
36 ctx.move_to(0, SIZE)
37 ctx.rel_curve_to(0, SIZE, SIZE, SIZE, 2 * SIZE, 0)
38 ctx.rel_curve_to(SIZE, -SIZE, 2 * SIZE, -SIZE, 2 * SIZE, 0)
39 ctx.rel_curve_to(0, SIZE, -SIZE, SIZE, - 2 * SIZE, 0)
40 ctx.rel_curve_to(-SIZE, -SIZE, - 2 * SIZE, -SIZE, - 2 * SIZE, 0)
41 ctx.close_path()
44 def draw_shapes(ctx, x, y, fill):
45 ctx.save()
47 ctx.new_path()
48 ctx.translate(x + SIZE, y + SIZE)
49 bowtie(ctx)
50 if fill:
51 ctx.fill()
52 else:
53 ctx.stroke()
55 ctx.new_path()
56 ctx.translate(3 * SIZE, 0)
57 square(ctx)
58 if fill:
59 ctx.fill()
60 else:
61 ctx.stroke()
63 ctx.new_path()
64 ctx.translate(3 * SIZE, 0)
65 triangle(ctx)
66 if fill:
67 ctx.fill()
68 else:
69 ctx.stroke()
71 ctx.new_path()
72 ctx.translate(3 * SIZE, 0)
73 inf(ctx)
74 if fill:
75 ctx.fill()
76 else:
77 ctx.stroke()
79 ctx.restore()
82 def fill_shapes(ctx, x, y):
83 draw_shapes(ctx, x, y, True)
86 def stroke_shapes(ctx, x, y):
87 draw_shapes(ctx, x, y, False)
90 def draw(da, ctx):
91 ctx.set_source_rgb(0, 0, 0)
93 ctx.set_line_width(SIZE / 4)
94 ctx.set_tolerance(0.1)
96 ctx.set_line_join(cairo.LINE_JOIN_ROUND)
97 ctx.set_dash([SIZE / 4.0, SIZE / 4.0], 0)
98 stroke_shapes(ctx, 0, 0)
100 ctx.set_dash([], 0)
101 stroke_shapes(ctx, 0, 3 * SIZE)
103 ctx.set_line_join(cairo.LINE_JOIN_BEVEL)
104 stroke_shapes(ctx, 0, 6 * SIZE)
106 ctx.set_line_join(cairo.LINE_JOIN_MITER)
107 stroke_shapes(ctx, 0, 9 * SIZE)
109 fill_shapes(ctx, 0, 12 * SIZE)
111 ctx.set_line_join(cairo.LINE_JOIN_BEVEL)
112 fill_shapes(ctx, 0, 15 * SIZE)
113 ctx.set_source_rgb(1, 0, 0)
114 stroke_shapes(ctx, 0, 15 * SIZE)
117 def main():
118 win = Gtk.Window()
119 win.connect('destroy', lambda w: Gtk.main_quit())
120 win.set_default_size(450, 550)
122 drawingarea = Gtk.DrawingArea()
123 win.add(drawingarea)
124 drawingarea.connect('draw', draw)
126 win.show_all()
127 Gtk.main()
129 if __name__ == '__main__':
130 main()