Cope with python2.2.
[dom-editor.git] / Dome / Canvas.py
blobf1d5ef08b3e89e5e2609fddf338618651e1cc09e
1 from rox import g
3 import sys
4 import traceback
6 class Canvas(g.Window):
7 def __init__(self, view, node):
8 from gnome import canvas
9 g.Window.__init__(self)
10 self.view = view
11 self.display_root = node
13 self.canvas = canvas.Canvas()
14 self.add(self.canvas)
15 self.canvas.show()
17 self.pict_group = None
19 s = self.canvas.get_style().copy()
20 s.bg[g.STATE_NORMAL] = g.gdk.color_parse('LemonChiffon')
21 self.canvas.set_style(s)
23 self.set_title(self.display_root.nodeName)
24 self.view.add_display(self)
25 self.update_all()
26 self.connect('destroy', self.destroyed)
28 def destroyed(self, widget):
29 print "Gone!"
30 self.view.remove_display(self)
32 def update_all(self, node = None):
33 print "Update!"
34 if not self.view.has_ancestor(self.display_root, self.view.root):
35 print "Display node lost - killing canvas!"
36 self.destroy()
37 return
38 if self.pict_group:
39 self.pict_group.destroy()
40 self.pict_group = self.build(self.canvas.root(), self.display_root)
41 self.set_bounds()
43 def set_bounds(self):
44 m = 16
46 min_x, min_y, max_x, max_y = self.canvas.root().get_bounds()
47 min_x -= m
48 min_y -= m
49 max_x += m
50 max_y += m
51 self.canvas.set_scroll_region(min_x, min_y, max_x, max_y)
52 self.canvas.root().move(0, 0) # Magic!
53 self.canvas.set_usize(max_x - min_x, max_y - min_y)
55 def build(self, group, node):
56 attrs = {}
57 try:
58 for a in node.attributes:
59 try:
60 attrs[str(a.localName)] = eval(str(a.value))
61 except:
62 pass
63 except:
64 type, val, tb = sys.exc_info()
65 list = traceback.extract_tb(tb)
66 stack = traceback.format_list(list[-2:])
67 ex = traceback.format_exception_only(type, val) + ['\n\n'] + stack
68 traceback.print_exception(type, val, tb)
70 if attrs.has_key('onClick'):
71 onClick = attrs['onClick']
72 del attrs['onClick']
73 else:
74 onClick = None
76 name = str(node.localName)
77 if name not in ('rect', 'line', 'text', 'ellipse'):
78 name = 'group'
79 try:
80 item = apply(group.add, [name], attrs)
81 except:
82 type, val, tb = sys.exc_info()
83 list = traceback.extract_tb(tb)
84 stack = traceback.format_list(list[-2:])
85 ex = traceback.format_exception_only(type, val) + ['\n\n'] + stack
86 traceback.print_exception(type, val, tb)
88 item = group.add('ellipse',
89 fill_color = 'red',
90 outline_color = 'black',
91 x1 = -10, x2 = 10,
92 y1 = -10, y2 = 10,
93 width_pixels = 1)
95 if onClick:
96 item.connect('event', self.button_event, node, onClick)
98 for k in node.childNodes:
99 self.build(item, k)
101 return item
103 def button_event(self, item, event, node, prog):
104 if event.type == BUTTON_PRESS and event.button == 1:
105 self.view.run_new()
106 self.view.move_to(node)
107 self.view.do_action(['play', prog])
109 def move_from(self, old_nodes):
110 pass