1 from xml
.dom
import Node
2 from rox
.loading
import XDSLoader
5 from rox
import g
, TRUE
, FALSE
9 from Display
import Display
11 from GetArg
import GetArg
12 from Path
import make_relative_path
14 from rox
.Menu
import Menu
15 from gnome2
import canvas
18 ('/File', None, '<Branch>', ''),
19 ('/File/Save', 'menu_save', '', 'F3'),
20 ('/File/Blank document', 'do_blank_all', '', '<Ctrl>N'),
22 ('/Edit', None, '<Branch>', ''),
23 ('/Edit/Yank attributes', 'do_yank_attributes', '', ''),
24 ('/Edit/Paste attributes', 'do_paste_attribs', '', ''),
25 ('/Edit/Yank attrib value', 'do_yank_value', '', ''),
26 ('/Edit/', '', '', '<separator>'),
27 ('/Edit/Cut', 'do_delete_node', '', 'x'),
28 ('/Edit/Delete', 'do_delete_node_no_clipboard', '', '<Ctrl>X'),
29 ('/Edit/Shallow cut', 'do_delete_shallow', '', '<Shift>X'),
30 ('/Edit/', '', '', '<separator>'),
31 ('/Edit/Yank', 'do_yank', '', 'y'),
32 ('/Edit/Shallow yank', 'do_shallow_yank', '', '<Shift>Y'),
33 ('/Edit/', '', '', '<separator>'),
34 ('/Edit/Paste (replace)','do_put_replace', '', '<Shift>R'),
35 ('/Edit/Paste (inside)', 'do_put_as_child', '', 'bracketright'),
36 ('/Edit/Paste (before)', 'do_put_before', '', '<Shift>P'),
37 ('/Edit/Paste (after)', 'do_put_after', '', 'p'),
38 ('/Edit/', '', '', '<separator>'),
39 ('/Edit/Edit value', 'toggle_edit', '', 'Return'),
40 ('/Edit/', '', '', '<separator>'),
41 ('/Edit/Undo', 'do_undo', '', 'u'),
42 ('/Edit/Redo', 'do_redo', '', '<Ctrl>R'),
44 ('/Move', None, '<Branch>', ''),
45 ('/Move/XPath search', 'menu_show_search', '', 'slash'),
46 ('/Move/Text search', 'menu_show_text_search', '', 'T'),
47 ('/Move/Enter', 'do_enter', '', '<Shift>greater'),
48 ('/Move/Leave', 'do_leave', '', '<Shift>less'),
50 ('/Move/Root node', 'move_home', '', 'Home'),
51 ('/Move/Previous sibling', 'move_prev_sib', '', 'Up'),
52 ('/Move/Next sibling', 'move_next_sib', '', 'Down'),
53 ('/Move/Parent', 'move_left', '', 'Left'),
54 ('/Move/First child', 'move_right', '', 'Right'),
55 ('/Move/Last child', 'move_end', '', 'End'),
57 ('/Move/To attribute', 'menu_select_attrib', '', 'At'),
59 ('/Select', None, '<Branch>', ''),
60 ('/Select/By XPath', 'menu_show_global', '', 'numbersign'),
61 ('/Select/Duplicate Siblings', 'do_select_dups', '', ''),
62 ('/Select/To Mark', 'do_select_marked', '', 'minus'),
64 ('/Mark', None, '<Branch>', ''),
65 ('/Mark/Mark Selection', 'do_mark_selection', '', 'm'),
66 ('/Mark/Switch with Selection', 'do_mark_switch', '', 'comma'),
67 ('/Mark/Clear Mark', 'do_clear_mark', '', ''),
69 ('/Network', None, '<Branch>', ''),
70 ('/Network/HTTP GET', 'do_suck', '', '<Shift>asciicircum'),
71 ('/Network/HTTP POST', 'do_http_post', '', ''),
72 ('/Network/Send SOAP message', 'do_soap_send', '', ''),
74 ('/Create', None, '<Branch>', ''),
75 ('/Create/Insert element', 'menu_insert_element', '', '<Shift>I'),
76 ('/Create/Append element', 'menu_append_element', '', '<Shift>A'),
77 ('/Create/Open element', 'menu_open_element', '', '<Shift>O'),
78 ('/Create/Open element at end', 'menu_open_element_end', '', '<Shift>E'),
80 ('/Create/', '', '', '<separator>'),
82 ('/Create/Insert text node', 'menu_insert_text', '', 'I'),
83 ('/Create/Append text node', 'menu_append_text', '', 'A'),
84 ('/Create/Open text node', 'menu_open_text', '', 'O'),
85 ('/Create/Open text node at end', 'menu_open_text_end', '', 'E'),
87 ('/Create/', '', '', '<separator>'),
89 ('/Create/Attribute', 'menu_show_add_attrib', '', '<Shift>plus'),
91 ('/Process', None, '<Branch>', ''),
92 ('/Process/Substitute', 'menu_show_subst', '', 's'),
93 ('/Process/Python expression', 'menu_show_pipe', '', '<Shift>exclam'),
94 ('/Process/Normalise', 'do_normalise', '', ''),
95 ('/Process/Remove default namespaces', 'do_remove_ns', '', 'r'),
96 ('/Process/Comment to text', 'do_convert_to_text', '', ''),
98 ('/Program', None, '<Branch>', ''),
99 ('/Program/Input', 'menu_show_ask', '', 'question'),
100 ('/Program/Compare', 'do_compare', '', 'equal'),
101 ('/Program/Fail', 'do_fail', '', ''),
102 ('/Program/Pass', 'do_pass', '', ''),
103 ('/Program/Repeat last', 'do_again', '', 'dot'),
105 ('/View', None, '<Branch>', ''),
106 ('/View/Toggle hidden', 'do_toggle_hidden', '', '<Ctrl>H'),
107 ('/View/Show as HTML', 'do_show_html', '', ''),
108 ('/View/Show as canvas', 'do_show_canvas', '', ''),
109 ('/View/Close Window', 'menu_close_window', '', '<Ctrl>Q'),
111 #('/Options...', 'menu_options', '', '<Ctrl>O'),
115 return lambda(self
): self
.view
.may_record([action
])
117 class GUIView(Display
, XDSLoader
):
118 def __init__(self
, window
, view
):
119 Display
.__init
__(self
, window
, view
)
120 XDSLoader
.__init
__(self
, ['application/x-dome', 'text/xml',
122 window
.connect('key-press-event', self
.key_press
)
123 self
.cursor_node
= None
126 menu
.attach(window
, self
)
128 def update_state(self
):
129 if self
.view
.rec_point
:
130 state
= "(recording)"
131 elif self
.view
.idle_cb
or self
.view
.op_in_progress
:
135 self
.parent_window
.set_state(state
)
138 def xds_load_from_stream(self
, path
, type, stream
):
140 raise Exception('Can only load from files... sorry!')
141 if path
.endswith('.html'):
142 self
.view
.load_html(path
)
144 self
.view
.load_xml(path
)
145 if self
.view
.root
== self
.view
.model
.get_root():
146 self
.parent_window
.uri
= path
147 self
.parent_window
.update_title()
149 def key_press(self
, widget
, kev
):
150 focus
= widget
.focus_widget
151 if focus
and focus
is not widget
and focus
.get_toplevel() is widget
:
153 return TRUE
# Handled
157 if kev
.keyval
== keysyms
.Up
:
158 self
.view
.may_record(['move_prev_sib'])
159 elif kev
.keyval
== keysyms
.Down
:
160 self
.view
.may_record(['move_next_sib'])
161 elif kev
.keyval
== keysyms
.Left
:
162 self
.view
.may_record(['move_left'])
163 elif kev
.keyval
== keysyms
.Right
:
164 self
.view
.may_record(['move_right'])
165 elif kev
.keyval
== keysyms
.KP_Add
:
166 self
.menu_show_add_attrib()
167 elif kev
.keyval
== keysyms
.Tab
:
173 def node_clicked(self
, node
, bev
):
174 print "Clicked", node
.namespaceURI
, node
.localName
176 if bev
.type == g
.gdk
.BUTTON_PRESS
:
177 if len(self
.view
.current_nodes
) == 0:
180 src
= self
.view
.current_nodes
[-1]
181 shift
= bev
.state
& g
.gdk
.SHIFT_MASK
182 add
= bev
.state
& g
.gdk
.CONTROL_MASK
183 select_region
= shift
and node
.nodeType
== Node
.ELEMENT_NODE
184 lit
= shift
and not select_region
187 path
= make_relative_path(src
, node
, lit
, ns
)
188 if path
== '.' and self
.view
.current_nodes
and not self
.view
.current_attrib
:
191 self
.view
.may_record(["select_region", path
, ns
])
193 self
.view
.may_record(["do_search", path
, ns
, add
])
195 self
.view
.may_record(["toggle_hidden"])
197 def attrib_clicked(self
, element
, attrib
, event
):
198 if len(self
.view
.current_nodes
) == 0:
201 src
= self
.view
.current_nodes
[-1]
204 print "attrib_clicked", attrib
, attrib
.namespaceURI
, attrib
.localName
205 path
= make_relative_path(src
, element
, FALSE
, ns
)
207 self
.view
.may_record(["do_search", path
, ns
, FALSE
])
208 self
.view
.may_record(["attribute", attrib
.namespaceURI
, attrib
.localName
])
211 self
.parent_window
.save()
213 def show_menu(self
, bev
):
214 menu
.popup(self
, bev
)
216 def playback(self
, macro
, map):
217 "Called when the user clicks on a macro button."
218 Exec
.exec_state
.clean()
220 self
.view
.may_record(['map', macro
.uri
])
222 self
.view
.may_record(['play', macro
.uri
])
224 def menu_show_ask(self
):
225 def do_ask(q
, self
= self
):
227 self
.view
.may_record(action
)
228 GetArg('Input:', do_ask
, ('Prompt:',))
230 def menu_show_subst(self
):
231 def do_subst(args
, self
= self
):
232 action
= ["subst", args
[0], args
[1]]
233 self
.view
.may_record(action
)
234 GetArg('Substitute:', do_subst
, ('Replace:', 'With:'))
236 def move_from(self
, old
= []):
238 Display
.move_from(self
, old
)
240 def hide_editbox(self
):
242 if self
.cursor_attrib
:
243 self
.cursor_hidden_text
.set(text
= '%s=%s' %
244 (self
.cursor_attrib
.name
, self
.cursor_attrib
.value
))
245 self
.cursor_hidden_text
.show()
246 self
.auto_highlight(self
.cursor_node
)
247 self
.cursor_node
= None
248 self
.edit_box_item
.destroy()
250 def show_editbox(self
):
251 "Edit the current node/attribute"
258 raise Exception("Can't edit while display is hidden!")
260 self
.cursor_node
= self
.view
.current_nodes
[0]
261 group
= self
.node_to_group
[self
.cursor_node
]
262 self
.cursor_attrib
= self
.view
.current_attrib
264 self
.highlight(group
, FALSE
)
266 if self
.cursor_attrib
:
267 group
= group
.attrib_to_group
[self
.cursor_attrib
]
269 self
.cursor_hidden_text
= group
.text
270 if not self
.cursor_attrib
:
271 # Don't hide for attributes, so we can still see the name
274 group
.text
.set(text
= str(self
.cursor_attrib
.name
) + '=')
276 self
.update_now() # GnomeCanvas bug?
277 lx
, ly
, hx
, hy
= group
.text
.get_bounds()
278 x
, y
= group
.i2w(lx
, ly
)
286 self
.edit_box_text
= text
289 #s = eb.get_style().copy()
290 #s.font = load_font('fixed')
292 #if self.cursor_attrib:
293 # name_width = s.font.measure(self.cursor_attrib.name + '=') + 1
298 self
.edit_box_item
= self
.root().add(canvas
.CanvasWidget
, widget
= eb
,
299 x
= x
- m
+ name_width
, y
= y
- m
,
300 anchor
= g
.ANCHOR_NW
)
302 #text.set_editable(TRUE)
303 text
.get_buffer().insert_at_cursor(self
.get_edit_text(), -1)
304 text
.set_wrap_mode(g
.WRAP_WORD
)
305 text
.get_buffer().connect('changed', self
.eb_changed
)
306 text
.connect('key-press-event', self
.eb_key
)
311 #eb.select_region(0, -1)
314 def get_edit_text(self
):
315 node
= self
.cursor_node
316 if node
.nodeType
== Node
.ELEMENT_NODE
:
317 if self
.cursor_attrib
:
318 return str(self
.cursor_attrib
.value
)
321 return node
.nodeValue
323 def eb_key(self
, eb
, kev
):
325 if key
== g
.keysyms
.KP_Enter
:
326 key
= g
.keysyms
.Return
327 if key
== g
.keysyms
.Escape
:
329 elif key
== g
.keysyms
.Return
and kev
.state
& g
.gdk
.CONTROL_MASK
:
330 eb
.insert_defaults('\n')
332 elif key
== g
.keysyms
.Tab
or key
== g
.keysyms
.Return
:
333 buffer = eb
.get_buffer()
334 s
= buffer.get_start_iter()
335 e
= buffer.get_end_iter()
336 text
= buffer.get_text(s
, e
, TRUE
)
338 if text
!= self
.get_edit_text():
339 self
.commit_edit(text
)
344 def commit_edit(self
, new
):
345 if self
.cursor_attrib
:
346 self
.view
.may_record(['set_attrib', new
])
348 self
.view
.may_record(['change_node', new
])
350 def eb_changed(self
, eb
):
355 req
= self
.edit_box_text
.size_request()
357 width
= max(req
[0], 10)
358 height
= max(req
[1], 10)
359 self
.edit_box_item
.set(width
= width
+ 12, height
= height
+ 4)
362 def toggle_edit(self
):
368 def menu_select_attrib(self
):
369 def do_attrib(name
, self
= self
):
371 (prefix
, localName
) = name
.split(':', 1)
373 (prefix
, localName
) = (None, name
)
374 namespaceURI
= self
.view
.model
.prefix_to_namespace(self
.view
.get_current(), prefix
)
375 action
= ["attribute", namespaceURI
, localName
]
376 self
.view
.may_record(action
)
377 GetArg('Select attribute:', do_attrib
, ['Name:'])
379 def menu_show_add_attrib(self
):
380 def do_it(name
, self
= self
):
381 action
= ["add_attrib", "UNUSED", name
]
382 self
.view
.may_record(action
)
383 GetArg('Create attribute:', do_it
, ['Name:'])
385 def menu_show_pipe(self
):
386 def do_pipe(expr
, self
= self
):
387 action
= ["python", expr
]
388 self
.view
.may_record(action
)
389 GetArg('Python expression:', do_pipe
, ['Eval:'], "'x' is the old text...")
391 def menu_show_global(self
):
392 def do_global(pattern
, self
= self
):
393 action
= ["do_global", pattern
]
394 self
.view
.may_record(action
)
395 GetArg('Global:', do_global
, ['Pattern:'],
396 '(@CURRENT@ is the current node\'s value)\n' +
397 'Perform next action on all nodes matching')
399 def menu_show_text_search(self
):
400 def do_text_search(pattern
, self
= self
):
401 action
= ["do_text_search", pattern
]
402 self
.view
.may_record(action
)
403 GetArg('Search for:', do_text_search
, ['Text pattern:'],
404 '(@CURRENT@ is the current node\'s value)\n')
406 def menu_show_search(self
):
407 def do_search(pattern
, self
= self
):
408 action
= ["do_search", pattern
]
409 self
.view
.may_record(action
)
410 GetArg('Search for:',
411 do_search
, ['XPath:'],
412 '(@CURRENT@ is the current node\'s value)')
414 def show_add_box(self
, action
):
417 elif action
[0] == 'a':
419 elif action
[0] == 'o':
421 elif action
[0] == 'e':
428 elif action
[1] == 't':
435 self
.view
.may_record(['add_node', action
, value
])
436 GetArg('Add node', cb
, [prompt
], text
)
439 cur
= self
.view
.get_current()
440 if cur
.nodeType
== Node
.ELEMENT_NODE
:
442 return cur
.parentNode
.nodeName
444 def menu_insert_element(self
):
446 self
.show_add_box('ie')
448 def menu_append_element(self
):
450 self
.show_add_box('ae')
452 def menu_open_element(self
):
454 self
.show_add_box('oe')
456 def menu_open_element_end(self
):
457 "Open element at end"
458 self
.show_add_box('ee')
460 def menu_insert_text(self
):
462 self
.show_add_box('it')
464 def menu_append_text(self
):
466 self
.show_add_box('at')
468 def menu_open_text(self
):
470 self
.show_add_box('ot')
472 def menu_open_text_end(self
):
474 self
.show_add_box('et')
476 def menu_close_window(self
):
477 self
.parent_window
.destroy()
479 def menu_options(self
):
482 do_blank_all
= make_do('blank_all')
483 do_enter
= make_do('enter')
484 do_leave
= make_do('leave')
485 do_suck
= make_do('suck')
486 do_http_post
= make_do('http_post')
487 do_soap_send
= make_do('soap_send')
488 do_select_dups
= make_do('select_dups')
489 do_paste_attribs
= make_do('paste_attribs')
490 do_yank_value
= make_do('yank_value')
491 do_yank_attributes
= make_do('yank_attribs')
492 do_delete_node
= make_do('delete_node')
493 do_delete_node_no_clipboard
= make_do('delete_node_no_clipboard')
494 do_delete_shallow
= make_do('delete_shallow')
495 do_yank
= make_do('yank')
496 do_shallow_yank
= make_do('shallow_yank')
497 do_put_replace
= make_do('put_replace')
498 do_put_as_child
= make_do('put_as_child')
499 do_put_before
= make_do('put_before')
500 do_put_after
= make_do('put_after')
501 do_undo
= make_do('undo')
502 do_redo
= make_do('redo')
503 do_fail
= make_do('fail')
504 do_pass
= make_do('do_pass')
505 do_toggle_hidden
= make_do('toggle_hidden')
506 do_show_html
= make_do('show_html')
507 do_show_canvas
= make_do('show_canvas')
508 do_compare
= make_do('compare')
509 do_again
= make_do('again')
510 do_normalise
= make_do('normalise')
511 do_convert_to_text
= make_do('convert_to_text')
512 do_remove_ns
= make_do('remove_ns')
514 do_clear_mark
= make_do('clear_mark')
515 do_mark_switch
= make_do('mark_switch')
516 do_mark_selection
= make_do('mark_selection')
517 do_select_marked
= make_do('select_marked_region')
519 move_home
= make_do('move_home')
520 move_end
= make_do('move_end')
521 move_left
= make_do('move_left')
522 move_right
= make_do('move_right')
523 move_next_sib
= make_do('move_next_sib')
524 move_prev_sib
= make_do('move_prev_sib')