Add URI display, remove attrib print.
[dom-editor.git] / Dome / Canvas.py
blob9eadb7575d83391215b0295a9dbd14709130aea1
1 from gtk import *
2 from GDK import *
4 import sys
5 import traceback
7 class Canvas(GtkWindow):
8 def __init__(self, view, node):
9 from gnome.ui import GnomeCanvas
10 GtkWindow.__init__(self)
11 self.view = view
12 self.display_root = node
14 self.canvas = GnomeCanvas()
15 self.add(self.canvas)
16 self.canvas.show()
18 self.group = None
20 s = self.canvas.get_style().copy()
21 s.bg[STATE_NORMAL] = self.canvas.get_color('LemonChiffon')
22 self.canvas.set_style(s)
24 self.set_title(self.display_root.nodeName)
25 self.view.add_display(self)
26 self.update_all()
27 self.connect('destroy', self.destroyed)
29 def destroyed(self, widget):
30 print "Gone!"
31 self.view.remove_display(self)
33 def update_all(self, node = None):
34 print "Update!"
35 if not self.view.has_ancestor(self.display_root, self.view.root):
36 print "Display node lost - killing canvas!"
37 self.destroy()
38 return
39 if self.group:
40 self.group.destroy()
41 self.group = self.build(self.canvas.root(), self.display_root)
42 self.set_bounds()
44 def set_bounds(self):
45 m = 16
47 min_x, min_y, max_x, max_y = self.canvas.root().get_bounds()
48 min_x -= m
49 min_y -= m
50 max_x += m
51 max_y += m
52 self.canvas.set_scroll_region(min_x, min_y, max_x, max_y)
53 self.canvas.root().move(0, 0) # Magic!
54 self.canvas.set_usize(max_x - min_x, max_y - min_y)
56 def build(self, group, node):
57 attrs = {}
58 try:
59 for a in node.attributes:
60 try:
61 attrs[str(a.localName)] = eval(str(a.value))
62 except:
63 pass
64 except:
65 type, val, tb = sys.exc_info()
66 list = traceback.extract_tb(tb)
67 stack = traceback.format_list(list[-2:])
68 ex = traceback.format_exception_only(type, val) + ['\n\n'] + stack
69 traceback.print_exception(type, val, tb)
71 if attrs.has_key('onClick'):
72 onClick = attrs['onClick']
73 del attrs['onClick']
74 else:
75 onClick = None
77 name = str(node.localName)
78 if name not in ('rect', 'line', 'text', 'ellipse'):
79 name = 'group'
80 try:
81 item = apply(group.add, [name], attrs)
82 except:
83 type, val, tb = sys.exc_info()
84 list = traceback.extract_tb(tb)
85 stack = traceback.format_list(list[-2:])
86 ex = traceback.format_exception_only(type, val) + ['\n\n'] + stack
87 traceback.print_exception(type, val, tb)
89 item = group.add('ellipse',
90 fill_color = 'red',
91 outline_color = 'black',
92 x1 = -10, x2 = 10,
93 y1 = -10, y2 = 10,
94 width_pixels = 1)
96 if onClick:
97 item.connect('event', self.button_event, node, onClick)
99 for k in node.childNodes:
100 self.build(item, k)
102 return item
104 def button_event(self, item, event, node, prog):
105 if event.type == BUTTON_PRESS and event.button == 1:
106 self.view.run_new()
107 self.view.move_to(node)
108 self.view.do_action(['play', prog])
110 def move_from(self, old_nodes):
111 pass