1 from __future__
import generators
5 from xml
.dom
import Node
7 from constants
import XMLNS_NAMESPACE
10 default_font
= __main__
.default_font
12 def calc_node(display
, node
, pos
):
14 if node
.nodeType
== Node
.TEXT_NODE
:
15 text
= node
.nodeValue
.strip()
16 elif node
.nodeType
== Node
.ELEMENT_NODE
:
18 text
= display
.view
.model
.namespaces
.prefix
.get(node
.namespaceURI
, 'ERROR') + \
22 elif node
.nodeType
== Node
.ATTRIBUTE_NODE
:
24 text
= display
.view
.model
.namespaces
.prefix
.get(node
.namespaceURI
, 'ERROR') + \
28 text
= ' %s=%s' % (unicode(text
), unicode(node
.value
))
29 elif node
.nodeType
== Node
.COMMENT_NODE
:
30 text
= node
.nodeValue
.strip()
34 text
= '<noname>' + node
.nodeValue
38 # PyGtk leaks PangoLayouts, so just reuse a single one
39 layout
= display
.surface_layout
40 layout
.set_text(text
, -1)
41 width
, height
= layout
.get_pixel_size()
45 if node
.nodeType
!= Node
.ATTRIBUTE_NODE
:
50 style
= display
.surface
.style
# Different surface ;-)
54 if node
.nodeType
!= Node
.ATTRIBUTE_NODE
:
56 surface
.draw_rectangle(fg
[g
.STATE_NORMAL
], True,
58 if node
.nodeType
== Node
.ELEMENT_NODE
and node
.hasAttributeNS(None, 'hidden'):
59 surface
.draw_layout(fg
[g
.STATE_PRELIGHT
], text_x
+ width
+ 2, y
,
60 display
.create_pango_layout('(hidden)'))
64 if node
in display
.selection
:
66 surface
.draw_rectangle(style
.bg_gc
[g
.STATE_SELECTED
], True,
67 x
+ 1, y
+ 1, 6, height
- 3)
68 surface
.draw_rectangle(bg
[g
.STATE_SELECTED
], True,
69 text_x
, y
, width
- 1, height
- 1)
70 surface
.draw_layout(fg
[g
.STATE_SELECTED
], text_x
, y
, layout
)
73 surface
.draw_rectangle(style
.white_gc
, True, x
+ 1, y
+ 1, 6, height
- 3)
74 if node
.nodeType
== Node
.TEXT_NODE
:
75 gc
= style
.text_gc
[g
.STATE_NORMAL
]
76 elif node
.nodeType
== Node
.ATTRIBUTE_NODE
:
77 gc
= style
.fg_gc
[g
.STATE_INSENSITIVE
]
78 elif node
.nodeType
== Node
.COMMENT_NODE
:
79 gc
= style
.text_gc
[g
.STATE_INSENSITIVE
]
81 gc
= style
.fg_gc
[g
.STATE_NORMAL
]
82 surface
.draw_layout(gc
, text_x
, y
, layout
)
84 if node
in display
.view
.marked
:
85 surface
.draw_rectangle(style
.text_gc
[g
.STATE_PRELIGHT
], False,
86 x
- 1, y
- 1, width
+ (text_x
- x
), height
)
88 bbox
= (x
, y
, text_x
+ width
, y
+ height
)
91 class Display(g
.HBox
):
92 visible
= 1 # Always visible
94 def __init__(self
, window
, view
):
95 g
.HBox
.__init
__(self
, False, 0)
97 self
.surface
= g
.EventBox()
98 self
.surface_layout
= self
.surface
.create_pango_layout('')
99 self
.pack_start(self
.surface
, True, True, 0)
101 self
.surface
.set_app_paintable(True)
102 self
.surface
.set_double_buffered(False)
103 self
.update_timeout
= 0
104 self
.cached_nodes
= None
106 self
.scroll_adj
= g
.Adjustment(lower
= 0, upper
= 100, step_incr
= 1)
107 self
.scroll_adj
.connect('value-changed', self
.scroll_to
)
108 scale
= g
.VScale(self
.scroll_adj
)
109 scale
.unset_flags(g
.CAN_FOCUS
)
110 scale
.set_draw_value(False)
111 self
.pack_start(scale
, False, True, 0)
114 self
.parent_window
= window
117 s
= self
.surface
.get_style().copy()
118 s
.bg
[g
.STATE_NORMAL
] = g
.gdk
.color_parse('old lace')
119 s
.text
[g
.STATE_NORMAL
] = g
.gdk
.color_parse('blue')
120 s
.text
[g
.STATE_PRELIGHT
] = g
.gdk
.color_parse('orange') # Mark
121 s
.text
[g
.STATE_INSENSITIVE
] = g
.gdk
.color_parse('dark green')# Comment
122 s
.fg
[g
.STATE_PRELIGHT
] = g
.gdk
.color_parse('red') # Hidden
123 self
.surface
.set_style(s
)
125 self
.signals
= [self
.connect('destroy', self
.destroyed
)]
126 self
.surface
.connect('button-press-event', self
.bg_event
)
127 self
.surface
.connect('button-release-event', self
.bg_event
)
129 # Display is relative to this node, which is the highest
130 # displayed node (possibly off the top of the screen)
131 self
.ref_node
= view
.root
132 self
.ref_pos
= (0, 0)
134 self
.last_alloc
= None
135 self
.surface
.connect('size-allocate', lambda w
, a
: self
.size_allocate(a
))
136 self
.surface
.connect('size-request', lambda w
, r
: self
.size_request(r
))
137 self
.surface
.connect('expose-event', lambda w
, e
: 1)
139 self
.pan_timeout
= None
140 self
.h_limits
= (0, 0)
143 def destroyed(self
, widget
):
144 self
.view
.remove_display(self
)
145 for s
in self
.signals
:
147 if self
.update_timeout
:
148 g
.timeout_remove(self
.update_timeout
)
149 self
.update_timeout
= 0
153 del self
.parent_window
155 del self
.surface_layout
161 def size_allocate(self
, alloc
):
162 new
= (alloc
.width
, alloc
.height
)
163 if self
.last_alloc
== new
:
165 self
.last_alloc
= new
167 #print "Alloc", alloc.width, alloc.height
168 pm
= g
.gdk
.Pixmap(self
.surface
.window
, alloc
.width
, alloc
.height
, -1)
169 self
.surface
.window
.set_back_pixmap(pm
, False)
172 if self
.update_timeout
:
173 g
.timeout_remove(self
.update_timeout
)
174 self
.update_timeout
= 0
178 # Must be called either:
179 # - With no update_timeout running, or
180 # - From the timeout callback
182 self
.update_timeout
= 0
184 if not self
.pm
: return 0
187 self
.pm
.draw_rectangle(self
.surface
.style
.bg_gc
[g
.STATE_NORMAL
], True,
188 0, 0, self
.last_alloc
[0], self
.last_alloc
[1])
190 self
.drawn
= {} # xmlNode -> ((x1, y1, y2, y2), attrib_parent)
193 p
= self
.view
.root
.parentNode
198 self
.ref_node
= self
.view
.root
199 self
.ref_pos
= (0, 0)
202 if self
.view
.current_attrib
:
203 self
.selection
= {self
.view
.current_attrib
: None}
206 for n
in self
.view
.current_nodes
:
207 self
.selection
[n
] = None
209 pos
= list(self
.ref_pos
)
210 self
.h_limits
= (self
.ref_pos
[0], self
.ref_pos
[0]) # Left, Right
213 for node
, bbox
, draw_fn
in self
.walk_tree(self
.ref_node
, self
.ref_pos
):
214 if bbox
[1] > self
.last_alloc
[1]: break # Off-screen
215 if bbox
[1] > -self
.last_alloc
[1]:
218 pass#print 'Warning: Ref node way off:', bbox[1]
219 if node
.nodeType
== Node
.ATTRIBUTE_NODE
:
220 self
.drawn
[node
] = (bbox
, attr_parent
)
223 self
.drawn
[node
] = (bbox
, None)
225 if bbox
[1] < 0 and node
.nodeType
!= Node
.ATTRIBUTE_NODE
:
227 self
.ref_pos
= bbox
[:2]
228 self
.h_limits
= (min(self
.h_limits
[0], bbox
[0]),
229 max(self
.h_limits
[1], bbox
[2]))
231 self
.surface
.window
.clear()
236 pos
= self
.cached_nodes
.index(self
.ref_node
)
239 print "Missing ref node!!"
240 self
.scroll_adj
.value
= float(pos
)
241 self
.scroll_adj
.upper
= float(len(self
.cached_nodes
))
245 def ensure_cache(self
):
246 "Find all the nodes in the document, in document order. Not attributes."
247 if self
.cached_nodes
is not None:
249 nodes
= [self
.view
.root
.parentNode
]
250 node
= self
.view
.root
254 node
= node
.childNodes
[0]
256 while not node
.nextSibling
:
257 node
= node
.parentNode
259 self
.cached_nodes
= nodes
261 node
= node
.nextSibling
262 self
.cached_nodes
= nodes
264 def walk_tree(self
, node
, pos
):
265 """Yield this (node, bbox), and all following ones in document order."""
268 bbox
, draw_fn
= calc_node(self
, node
, pos
)
269 yield (node
, bbox
, draw_fn
)
272 if node
.nodeType
== Node
.ELEMENT_NODE
:
273 hidden
= node
.hasAttributeNS(None, 'hidden')
275 apos
= [bbox
[2] + 4, bbox
[1]]
276 for key
in node
.attributes
:
277 a
= node
.attributes
[key
]
278 if a
.namespaceURI
== XMLNS_NAMESPACE
:
280 abbox
, draw_fn
= calc_node(self
, a
, apos
)
281 apos
[0] = abbox
[2] + 4
282 yield (a
, abbox
, draw_fn
)
285 if node
.childNodes
and not hidden
:
286 node
= node
.childNodes
[0]
289 while not node
.nextSibling
:
290 node
= node
.parentNode
293 node
= node
.nextSibling
295 def size_request(self
, req
):
299 def do_update_now(self
):
300 # Update now, if we need to
301 if self
.update_timeout
:
302 g
.timeout_remove(self
.update_timeout
)
303 self
.update_timeout
= 0
306 def update_all(self
, node
= None):
307 self
.cached_nodes
= None
309 if self
.update_timeout
:
310 return # Going to update anyway...
312 if self
.view
.running():
313 self
.update_timeout
= g
.timeout_add(2000, self
.update
)
315 self
.update_timeout
= g
.timeout_add(10, self
.update
)
317 def move_from(self
, old
= []):
318 if not self
.pm
: return
319 if self
.view
.current_nodes
:
321 for n
in self
.view
.current_nodes
:
324 for node
, bbox
, draw_fn
in self
.walk_tree(self
.ref_node
, self
.ref_pos
):
325 if bbox
[1] > self
.last_alloc
[1]: break # Off-screen
326 if bbox
[3] > 0 and node
in selection
:
328 break # A selected node is shown
330 #print "(selected nodes not shown)"
331 self
.ref_node
= self
.view
.current_nodes
[0]
332 self
.ref_pos
= (40, self
.last_alloc
[1] / 2)
333 self
.backup_ref_node()
336 def set_view(self
, view
):
338 self
.view
.remove_display(self
)
340 self
.view
.add_display(self
)
343 def show_menu(self
, bev
):
346 def node_clicked(self
, node
, event
):
349 def xy_to_node(self
, x
, y
):
350 "Return the node at this point and, if it's an attribute, its parent."
351 for (n
, ((x1
, y1
, x2
, y2
), attrib_parent
)) in self
.drawn
.iteritems():
352 if x
>= x1
and x
<= x2
and y
>= y1
and y
<= y2
:
353 return n
, attrib_parent
358 val
= (float(abs(x
)) ** 1.4)
364 if x
> 10: return x
- 10
365 if x
< -10: return x
+ 10
367 x
, y
, mask
= self
.surface
.window
.get_pointer()
368 sx
, sy
= self
.pan_start
369 dx
, dy
= scale(chop(x
- sx
)) / 20, scale(chop(y
- sy
))
370 dx
= max(dx
, 10 - self
.h_limits
[1])
371 dx
= min(dx
, self
.last_alloc
[0] - 10 - self
.h_limits
[0])
372 new
= [self
.ref_pos
[0] + dx
, self
.ref_pos
[1] + dy
]
374 if new
== self
.ref_pos
:
379 self
.backup_ref_node()
381 if self
.update_timeout
:
382 g
.timeout_remove(self
.update_timeout
)
383 self
.update_timeout
= 0
388 def backup_ref_node(self
):
389 self
.ref_pos
= list(self
.ref_pos
)
390 # Walk up the parents until we get a ref node above the start of the screen
391 # (redraw will come back down)
392 while self
.ref_pos
[1] > 0:
395 if self
.ref_node
.previousSibling
:
396 self
.ref_node
= self
.ref_node
.previousSibling
397 elif self
.ref_node
.parentNode
:
398 self
.ref_node
= self
.ref_node
.parentNode
402 # Walk from the parent node to find how far it is to this node...
403 for node
, bbox
, draw_fn
in self
.walk_tree(self
.ref_node
, (0, 0)):
404 if node
is src
: break
408 self
.ref_pos
[0] -= bbox
[0]
409 self
.ref_pos
[1] -= bbox
[1]
411 #print "(start from %s at (%d,%d))" % (self.ref_node, self.ref_pos[0], self.ref_pos[1])
413 if self
.ref_pos
[1] > 10:
415 elif self
.ref_pos
[1] < -100:
416 for node
, bbox
, draw_fn
in self
.walk_tree(self
.ref_node
, self
.ref_pos
):
417 if bbox
[3] > 10: break # Something is visible
419 self
.ref_pos
[1] = -100
421 def bg_event(self
, widget
, event
):
422 if event
.type == g
.gdk
.BUTTON_PRESS
and event
.button
== 3:
423 self
.show_menu(event
)
424 elif event
.type == g
.gdk
.BUTTON_PRESS
or event
.type == g
.gdk
._2BUTTON
_PRESS
:
426 node
, attr_parent
= self
.xy_to_node(event
.x
, event
.y
)
427 if event
.button
== 1:
430 self
.attrib_clicked(attr_parent
, node
, event
)
432 self
.node_clicked(node
, event
)
433 elif event
.button
== 2:
434 assert self
.pan_timeout
is None
435 self
.pan_start
= (event
.x
, event
.y
)
436 self
.pan_timeout
= g
.timeout_add(100, self
.pan
)
437 elif event
.type == g
.gdk
.BUTTON_RELEASE
and event
.button
== 2:
438 assert self
.pan_timeout
is not None
439 g
.timeout_remove(self
.pan_timeout
)
440 self
.pan_timeout
= None
445 def marked_changed(self
, nodes
):
446 "nodes is a list of nodes to be rechecked."
449 def options_changed(self
):
450 if default_font
.has_changed
:
451 #self.modify_font(pango.FontDescription(default_font.value))
454 def scroll_to(self
, adj
):
458 node
= self
.cached_nodes
[n
]
460 node
= self
.cached_nodes
[-1]
461 if self
.ref_node
== node
:
465 while node
.parentNode
:
467 node
= node
.parentNode
468 self
.ref_pos
= (x
, 0)
471 def set_status(self
, message
):
472 self
.parent_window
.set_status(message
)