1 # -*- coding: utf-8 -*-
4 import gtk, gobject, cairo
13 def __init__(self, x=0, y=0, w=0, h=0):
19 def scale(self, w=0, h=0):
28 def __init__(self, text, color=BLACK, font="Sans", fontsize=12, **kwargs):
29 TBObject.__init__(self, **kwargs)
30 self.text = text.split("\n")
33 self.fontsize = fontsize
37 for line in self.text:
38 cr.set_source_rgb(*self.color)
39 cr.select_font_face(self.font, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
40 cr.set_font_size(self.fontsize)
41 x_bearing, y_bearing, width, height = cr.text_extents(line)[:4]
42 cr.move_to(x - width / 2 - x_bearing, y - height / 2 - y_bearing)
47 def __init__(self, file, **kwargs):
48 TBObject.__init__(self, **kwargs)
50 self.svg = rsvg.Handle(file)
54 ws, hs = self.scale_svg()
57 self.svg.render_cairo(cr)
62 w, h = self.svg.props.width, self.svg.props.height
63 if not self.width or not self.height:
64 self.width, self.height = w, h
66 w_scale = self.width / float(w)
67 h_scale = self.height / float(h)
68 return w_scale, h_scale
70 class Rectangle(TBObject):
71 def __init__(self, color=BLACK, fill=WHITE, line_width=1, **kwargs):
72 TBObject.__init__(self, **kwargs)
75 self.line_width = line_width
78 cr.set_line_width(self.line_width)
79 cr.set_source_rgb(*self.color)
80 cr.rectangle(self.x, self.y, self.width, self.height)
82 cr.set_source_rgb(*self.fill)
83 cr.rectangle(self.x, self.y, self.width, self.height)
86 class DArea(gtk.DrawingArea):
88 gtk.DrawingArea.__init__(self)
89 self.connect("expose-event", self.expose)
91 self.add_events(gtk.gdk.BUTTON_PRESS_MASK |
92 gtk.gdk.BUTTON1_MOTION_MASK)
94 self.connect("expose_event", self.expose)
95 self.connect("button_press_event", self.pressing)
96 self.connect("motion_notify_event", self.moving)
98 def expose(self, widget, event):
99 self.context = self.window.cairo_create()
101 self.context.rectangle(event.area.x, event.area.y,
102 event.area.width, event.area.height)
105 self.draw(self.context, *self.window.get_size())
107 def draw(self, cr, width, height):
108 r = Rectangle(x=10, y=10, w=width-20, h=height-20, line_width=10)
112 cr.set_source_rgb(0.0, 0.0, 0.8)
113 cr.move_to(width / 3.0, height / 3.0)
114 cr.rel_line_to(0, height / 6.0)
115 cr.move_to(2 * width / 3.0, height / 3.0)
116 cr.rel_line_to(0, height / 6.0)
120 cr.set_source_rgb(1.0, 0.0, 0.0)
121 radius = min(width, height)
122 cr.arc(width / 2.0, height / 2.0, radius / 2.0 - 20, 0, 2 * pi)
124 cr.arc(width / 2.0, height / 2.0, radius / 3.0 - 10, pi / 3, 2 * pi / 3)
127 globo = SVG("globo.svg", x=width-300, y=40, w=200, h=120)
137 tbotext = Text(text, x=width-200, y=60, fontsize=12, font="Kid Kosmic")
140 def pressing(self, widget, event):
141 print "pressing", event.x, event.y
143 def moving(self, widget, event):
144 print "moving", event.x, event.y
149 w.set_default_size(800, 500)
153 w.connect('destroy', gtk.main_quit)
156 if __name__ == '__main__':