1 from __future__
import generators
5 from xml
.dom
import Node
7 from constants
import XMLNS_NAMESPACE
10 default_font
= __main__
.default_font
12 drag_cursor
= g
.gdk
.Cursor(g
.gdk
.HAND1
)
14 def calc_node(display
, node
, pos
):
16 if node
.nodeType
== Node
.TEXT_NODE
:
17 text
= node
.nodeValue
.strip()
18 elif node
.nodeType
== Node
.ELEMENT_NODE
:
20 text
= display
.view
.model
.namespaces
.prefix
.get(node
.namespaceURI
, 'ERROR') + \
24 elif node
.nodeType
== Node
.ATTRIBUTE_NODE
:
26 text
= display
.view
.model
.namespaces
.prefix
.get(node
.namespaceURI
, 'ERROR') + \
30 text
= ' %s=%s' % (unicode(text
), unicode(node
.value
))
31 elif node
.nodeType
== Node
.COMMENT_NODE
:
32 text
= node
.nodeValue
.strip()
33 elif node
.nodeType
== Node
.DOCUMENT_NODE
:
34 chroots
= len(display
.view
.chroots
)
36 text
= "subtree (%d levels)" % chroots
38 text
= 'subtree (one level)'
40 text
= display
.view
.model
.uri
44 text
= '<noname>' + node
.nodeValue
48 # PyGtk leaks PangoLayouts, so just reuse a single one
49 layout
= display
.surface_layout
51 width
, height
= layout
.get_pixel_size()
55 if node
.nodeType
!= Node
.ATTRIBUTE_NODE
:
60 style
= display
.surface
.style
# Different surface ;-)
64 if node
in display
.selection
:
65 state
= g
.STATE_SELECTED
67 state
= g
.STATE_NORMAL
69 if node
.nodeType
!= Node
.ATTRIBUTE_NODE
:
70 if node
.nodeType
== Node
.ELEMENT_NODE
:
71 surface
.draw_rectangle(style
.fg_gc
[state
], False, x
, y
, 7, height
- 2)
72 surface
.draw_rectangle(style
.bg_gc
[state
], True, x
+ 1, y
+ 1, 6, height
- 3)
73 elif node
.nodeType
== Node
.DOCUMENT_NODE
:
74 surface
.draw_arc(style
.fg_gc
[state
], False, x
, y
, 7, height
- 2, 0, 64 * 360)
77 surface
.draw_rectangle(style
.text_gc
[state
], False, x
, y
, 7, height
- 2)
78 surface
.draw_rectangle(style
.base_gc
[state
], True, x
+ 1, y
+ 1, 6, height
- 3)
80 if node
in display
.view
.model
.hidden
:
81 surface
.draw_layout(fg
[g
.STATE_PRELIGHT
], text_x
+ width
+ 2, y
,
82 display
.create_pango_layout('(%s)' % display
.view
.model
.hidden
[node
]))
84 if node
in display
.selection
:
85 surface
.draw_rectangle(bg
[g
.STATE_SELECTED
], True,
86 text_x
, y
, width
- 1, height
- 1)
87 surface
.draw_layout(fg
[g
.STATE_SELECTED
], text_x
, y
, layout
)
89 if node
.nodeType
== Node
.TEXT_NODE
:
90 gc
= style
.text_gc
[g
.STATE_NORMAL
]
91 elif node
.nodeType
== Node
.ATTRIBUTE_NODE
:
92 gc
= style
.fg_gc
[g
.STATE_INSENSITIVE
]
93 elif node
.nodeType
== Node
.COMMENT_NODE
:
94 gc
= style
.text_gc
[g
.STATE_INSENSITIVE
]
96 gc
= style
.fg_gc
[g
.STATE_NORMAL
]
97 surface
.draw_layout(gc
, text_x
, y
, layout
)
99 if node
in display
.view
.marked
:
100 surface
.draw_rectangle(style
.text_gc
[g
.STATE_PRELIGHT
], False,
101 x
- 1, y
- 1, width
+ (text_x
- x
), height
)
103 bbox
= (x
, y
, text_x
+ width
, y
+ height
)
106 class Display(g
.HBox
):
107 visible
= 1 # Always visible
109 def __init__(self
, window
, view
):
110 g
.HBox
.__init
__(self
, False, 0)
112 self
.surface
= g
.EventBox()
113 self
.surface_layout
= self
.surface
.create_pango_layout('')
114 self
.pack_start(self
.surface
, True, True, 0)
116 self
.surface
.set_app_paintable(True)
117 self
.surface
.set_double_buffered(False)
118 self
.update_timeout
= 0
119 self
.cached_nodes
= None
121 self
.scroll_adj
= g
.Adjustment(lower
= 0, upper
= 100, step_incr
= 1)
122 self
.scroll_adj
.connect('value-changed', self
.scroll_to
)
123 scale
= g
.VScrollbar(self
.scroll_adj
)
124 scale
.unset_flags(g
.CAN_FOCUS
)
125 #scale.set_draw_value(False)
126 self
.pack_start(scale
, False, True, 0)
129 self
.parent_window
= window
132 s
= self
.surface
.get_style().copy()
133 s
.bg
[g
.STATE_NORMAL
] = g
.gdk
.color_parse('old lace')
134 s
.text
[g
.STATE_NORMAL
] = g
.gdk
.color_parse('blue')
135 s
.text
[g
.STATE_PRELIGHT
] = g
.gdk
.color_parse('orange') # Mark
136 s
.text
[g
.STATE_INSENSITIVE
] = g
.gdk
.color_parse('dark green')# Comment
137 s
.fg
[g
.STATE_PRELIGHT
] = g
.gdk
.color_parse('red') # Hidden
138 self
.surface
.set_style(s
)
140 self
.signals
= [self
.connect('destroy', self
.destroyed
)]
141 self
.surface
.connect('button-press-event', self
.bg_event
)
142 self
.surface
.connect('motion-notify-event', self
.bg_motion
)
143 self
.surface
.connect('button-release-event', self
.bg_event
)
145 # Display is relative to this node, which is the highest
146 # displayed node (possibly off the top of the screen)
147 self
.ref_node
= view
.root
148 self
.ref_pos
= (0, 0)
150 self
.drag_info
= None
152 self
.last_alloc
= None
153 self
.surface
.connect('size-allocate', lambda w
, a
: self
.size_allocate(a
))
154 self
.surface
.connect('size-request', lambda w
, r
: self
.size_request(r
))
155 self
.surface
.connect('expose-event', lambda w
, e
: 1)
157 self
.pan_timeout
= None
158 self
.h_limits
= (0, 0)
161 def destroyed(self
, widget
):
162 self
.view
.remove_display(self
)
163 for s
in self
.signals
:
165 if self
.update_timeout
:
166 g
.timeout_remove(self
.update_timeout
)
167 self
.update_timeout
= 0
171 del self
.parent_window
173 del self
.surface_layout
179 def size_allocate(self
, alloc
):
180 new
= (alloc
.width
, alloc
.height
)
181 if self
.last_alloc
== new
:
183 self
.last_alloc
= new
185 #print "Alloc", alloc.width, alloc.height
186 pm
= g
.gdk
.Pixmap(self
.surface
.window
, alloc
.width
, alloc
.height
, -1)
187 self
.surface
.window
.set_back_pixmap(pm
, False)
190 if self
.update_timeout
:
191 g
.timeout_remove(self
.update_timeout
)
192 self
.update_timeout
= 0
196 # Must be called either:
197 # - With no update_timeout running, or
198 # - From the timeout callback
200 self
.update_timeout
= 0
202 if not self
.pm
: return 0
205 self
.pm
.draw_rectangle(self
.surface
.style
.bg_gc
[g
.STATE_NORMAL
], True,
206 0, 0, self
.last_alloc
[0], self
.last_alloc
[1])
208 self
.drawn
= {} # xmlNode -> ((x1, y1, y2, y2), attrib_parent)
211 p
= self
.view
.root
.parentNode
216 self
.ref_node
= self
.view
.root
217 self
.ref_pos
= (0, 0)
220 if self
.view
.current_attrib
:
221 self
.selection
= {self
.view
.current_attrib
: None}
224 for n
in self
.view
.current_nodes
:
225 self
.selection
[n
] = None
227 pos
= list(self
.ref_pos
)
228 self
.h_limits
= (self
.ref_pos
[0], self
.ref_pos
[0]) # Left, Right
232 for node
, bbox
, draw_fn
in self
.walk_tree(self
.ref_node
, self
.ref_pos
):
233 if bbox
[1] > self
.last_alloc
[1]: break # Off-screen
234 if bbox
[1] > -self
.last_alloc
[1]:
238 pass#print 'Warning: Ref node way off:', bbox[1]
239 if node
.nodeType
== Node
.ATTRIBUTE_NODE
:
240 self
.drawn
[node
] = (bbox
, attr_parent
)
243 self
.drawn
[node
] = (bbox
, None)
245 if bbox
[1] < 0 and node
.nodeType
!= Node
.ATTRIBUTE_NODE
:
247 self
.ref_pos
= bbox
[:2]
248 self
.h_limits
= (min(self
.h_limits
[0], bbox
[0]),
249 max(self
.h_limits
[1], bbox
[2]))
251 # Didn't have enough nodes to fill the screen
252 frac_filled
= float(bbox
[3]) / self
.last_alloc
[1]
256 self
.surface
.window
.clear()
261 pos
= self
.cached_nodes
.index(self
.ref_node
)
264 print "Missing ref node!!"
265 self
.scroll_adj
.value
= float(pos
)
266 self
.scroll_adj
.upper
= float(len(self
.cached_nodes
) + drawn
)
268 self
.scroll_adj
.page_size
= float(drawn
)
269 self
.scroll_adj
.page_increment
= float(drawn
)
273 def ensure_cache(self
):
274 "Find all the nodes in the document, in document order. Not attributes."
275 if self
.cached_nodes
is not None:
277 nodes
= [self
.view
.root
.parentNode
]
278 node
= self
.view
.root
282 node
= node
.childNodes
[0]
284 while not node
.nextSibling
:
285 node
= node
.parentNode
287 self
.cached_nodes
= nodes
289 node
= node
.nextSibling
290 self
.cached_nodes
= nodes
292 def walk_tree(self
, node
, pos
):
293 """Yield this (node, bbox), and all following ones in document order."""
295 hidden
= self
.view
.model
.hidden
297 bbox
, draw_fn
= calc_node(self
, node
, pos
)
298 yield (node
, bbox
, draw_fn
)
300 if node
.nodeType
== Node
.ELEMENT_NODE
:
301 if node
not in hidden
:
302 apos
= [bbox
[2] + 4, bbox
[1]]
303 for key
in node
.attributes
:
304 a
= node
.attributes
[key
]
305 if a
.namespaceURI
== XMLNS_NAMESPACE
:
307 abbox
, draw_fn
= calc_node(self
, a
, apos
)
308 apos
[0] = abbox
[2] + 4
309 yield (a
, abbox
, draw_fn
)
312 if node
.childNodes
and node
not in hidden
:
313 node
= node
.childNodes
[0]
316 while not node
.nextSibling
:
317 node
= node
.parentNode
320 node
= node
.nextSibling
322 def size_request(self
, req
):
326 def do_update_now(self
):
327 # Update now, if we need to
328 if self
.update_timeout
:
329 g
.timeout_remove(self
.update_timeout
)
330 self
.update_timeout
= 0
333 def update_all(self
, node
= None):
334 self
.cached_nodes
= None
336 if self
.update_timeout
:
337 return # Going to update anyway...
339 if self
.view
.running():
340 self
.update_timeout
= g
.timeout_add(2000, self
.update
)
342 self
.update_timeout
= g
.timeout_add(10, self
.update
)
344 def move_from(self
, old
= []):
345 if not self
.pm
: return
346 if self
.view
.current_nodes
:
348 for n
in self
.view
.current_nodes
:
351 for node
, bbox
, draw_fn
in self
.walk_tree(self
.ref_node
, self
.ref_pos
):
352 if bbox
[1] > self
.last_alloc
[1]: break # Off-screen
353 if bbox
[3] > 0 and node
in selection
:
355 break # A selected node is shown
357 #print "(selected nodes not shown)"
358 self
.ref_node
= self
.view
.current_nodes
[0]
359 self
.ref_pos
= (40, self
.last_alloc
[1] / 2)
360 self
.backup_ref_node()
363 def set_view(self
, view
):
365 self
.view
.remove_display(self
)
367 self
.view
.add_display(self
)
370 def show_menu(self
, bev
):
373 def node_clicked(self
, node
, event
):
376 def xy_to_node(self
, x
, y
):
377 "Return the node at this point and, if it's an attribute, its parent."
378 for (n
, ((x1
, y1
, x2
, y2
), attrib_parent
)) in self
.drawn
.iteritems():
379 if x
>= x1
and x
<= x2
and y
>= y1
and y
<= y2
:
380 return n
, attrib_parent
385 val
= (float(abs(x
)) ** 1.4)
391 if x
> 10: return x
- 10
392 if x
< -10: return x
+ 10
394 x
, y
, mask
= self
.surface
.window
.get_pointer()
395 sx
, sy
= self
.pan_start
396 dx
, dy
= scale(chop(x
- sx
)) / 20, scale(chop(y
- sy
))
397 dx
= max(dx
, 10 - self
.h_limits
[1])
398 dx
= min(dx
, self
.last_alloc
[0] - 10 - self
.h_limits
[0])
399 new
= [self
.ref_pos
[0] + dx
, self
.ref_pos
[1] + dy
]
401 if new
== self
.ref_pos
:
406 self
.backup_ref_node()
408 if self
.update_timeout
:
409 g
.timeout_remove(self
.update_timeout
)
410 self
.update_timeout
= 0
415 def backup_ref_node(self
):
416 self
.ref_pos
= list(self
.ref_pos
)
417 # Walk up the parents until we get a ref node above the start of the screen
418 # (redraw will come back down)
419 while self
.ref_pos
[1] > 0:
422 if self
.ref_node
.previousSibling
:
423 self
.ref_node
= self
.ref_node
.previousSibling
424 elif self
.ref_node
.parentNode
:
425 self
.ref_node
= self
.ref_node
.parentNode
429 # Walk from the parent node to find how far it is to this node...
430 for node
, bbox
, draw_fn
in self
.walk_tree(self
.ref_node
, (0, 0)):
431 if node
is src
: break
435 self
.ref_pos
[0] -= bbox
[0]
436 self
.ref_pos
[1] -= bbox
[1]
438 #print "(start from %s at (%d,%d))" % (self.ref_node, self.ref_pos[0], self.ref_pos[1])
440 if self
.ref_pos
[1] > 10:
442 elif self
.ref_pos
[1] < -100:
443 for node
, bbox
, draw_fn
in self
.walk_tree(self
.ref_node
, self
.ref_pos
):
444 if bbox
[3] > 10: break # Something is visible
446 self
.ref_pos
[1] = -100
448 def bg_motion(self
, widget
, event
):
449 if not self
.drag_info
:
451 node
, attr_parent
, x
, y
, in_progress
= self
.drag_info
453 if abs(event
.x
- x
) > 5 or abs(event
.y
- y
) > 5:
454 self
.drag_info
= (node
, attr_parent
, event
.x
, event
.y
, True)
455 self
.window
.set_cursor(drag_cursor
)
460 def do_drag(self
, src
, dst
):
463 def bg_event(self
, widget
, event
):
464 if event
.type == g
.gdk
.BUTTON_PRESS
and event
.button
== 3:
465 self
.show_menu(event
)
466 elif event
.type == g
.gdk
.BUTTON_PRESS
or event
.type == g
.gdk
._2BUTTON
_PRESS
:
468 node
, attr_parent
= self
.xy_to_node(event
.x
, event
.y
)
469 if event
.button
== 1:
470 self
.drag_info
= (node
, attr_parent
, event
.x
, event
.y
, False)
473 self
.attrib_clicked(attr_parent
, node
, event
)
475 self
.node_clicked(node
, event
)
476 elif event
.button
== 2:
477 assert self
.pan_timeout
is None
478 self
.pan_start
= (event
.x
, event
.y
)
479 self
.pan_timeout
= g
.timeout_add(100, self
.pan
)
480 elif event
.type == g
.gdk
.BUTTON_RELEASE
:
481 if event
.button
== 2:
482 assert self
.pan_timeout
is not None
483 g
.timeout_remove(self
.pan_timeout
)
484 self
.pan_timeout
= None
485 elif event
.button
== 1 and self
.drag_info
:
486 src_node
, src_attr_parent
, x
, y
, in_progress
= self
.drag_info
489 dst_node
, dst_attr_parent
= self
.xy_to_node(event
.x
, event
.y
)
490 self
.window
.set_cursor(None)
491 if not dst_node
or (dst_node
== src_node
and dst_attr_parent
== src_attr_parent
):
493 self
.do_drag((src_node
, src_attr_parent
), (dst_node
, dst_attr_parent
))
498 def marked_changed(self
, nodes
):
499 "nodes is a list of nodes to be rechecked."
502 def options_changed(self
):
503 if default_font
.has_changed
:
504 #self.modify_font(pango.FontDescription(default_font.value))
507 def scroll_to(self
, adj
):
511 node
= self
.cached_nodes
[n
]
513 node
= self
.cached_nodes
[-1]
514 if self
.ref_node
== node
:
518 while node
.parentNode
:
520 node
= node
.parentNode
521 self
.ref_pos
= (x
, 0)
524 def set_status(self
, message
):
525 self
.parent_window
.set_status(message
)