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 gnome
import canvas
18 ('/File', None, '<Branch>', ''),
19 ('/File/Save', 'menu_save', '', 'F3'),
20 ('/File/Blank document', 'do_blank_all', '', '<Ctrl>N'),
21 ('/File/Clear undo buffer', 'menu_clear_undo', '', ''),
23 ('/Edit', None, '<Branch>', ''),
24 ('/Edit/Yank attributes', 'do_yank_attributes', '', ''),
25 ('/Edit/Paste attributes', 'do_paste_attribs', '', ''),
26 ('/Edit/Yank attrib value', 'do_yank_value', '', ''),
27 ('/Edit/', '', '', '<separator>'),
28 ('/Edit/Cut', 'do_delete_node', '', 'x'),
29 ('/Edit/Delete', 'do_delete_node_no_clipboard', '', '<Ctrl>X'),
30 ('/Edit/Shallow cut', 'do_delete_shallow', '', '<Shift>X'),
31 ('/Edit/', '', '', '<separator>'),
32 ('/Edit/Yank', 'do_yank', '', 'y'),
33 ('/Edit/Shallow yank', 'do_shallow_yank', '', '<Shift>Y'),
34 ('/Edit/', '', '', '<separator>'),
35 ('/Edit/Paste (replace)','do_put_replace', '', '<Shift>R'),
36 ('/Edit/Paste (inside)', 'do_put_as_child', '', 'bracketright'),
37 ('/Edit/Paste (before)', 'do_put_before', '', '<Shift>P'),
38 ('/Edit/Paste (after)', 'do_put_after', '', 'p'),
39 ('/Edit/', '', '', '<separator>'),
40 ('/Edit/Edit value', 'toggle_edit', '', 'Return'),
41 ('/Edit/', '', '', '<separator>'),
42 ('/Edit/Undo', 'do_undo', '', 'u'),
43 ('/Edit/Redo', 'do_redo', '', '<Ctrl>R'),
45 ('/Move', None, '<Branch>', ''),
46 ('/Move/XPath search', 'menu_show_search', '', 'slash'),
47 ('/Move/Text search', 'menu_show_text_search', '', 'T'),
48 ('/Move/Enter', 'do_enter', '', '<Shift>greater'),
49 ('/Move/Leave', 'do_leave', '', '<Shift>less'),
51 ('/Move/Root node', 'move_home', '', 'Home'),
52 ('/Move/Previous sibling', 'move_prev_sib', '', 'Up'),
53 ('/Move/Next sibling', 'move_next_sib', '', 'Down'),
54 ('/Move/Parent', 'move_left', '', 'Left'),
55 ('/Move/First child', 'move_right', '', 'Right'),
56 ('/Move/Last child', 'move_end', '', 'End'),
58 ('/Move/To attribute', 'menu_select_attrib', '', 'At'),
60 ('/Select', None, '<Branch>', ''),
61 ('/Select/By XPath', 'menu_show_global', '', 'numbersign'),
62 ('/Select/Duplicate Siblings', 'do_select_dups', '', ''),
63 ('/Select/To Mark', 'do_select_marked', '', 'minus'),
64 ('/Select/Child Nodes', 'do_select_children', '', 'asterisk'),
66 ('/Mark', None, '<Branch>', ''),
67 ('/Mark/Mark Selection', 'do_mark_selection', '', 'm'),
68 ('/Mark/Switch with Selection', 'do_mark_switch', '', 'comma'),
69 ('/Mark/Clear Mark', 'do_clear_mark', '', ''),
71 ('/Network', None, '<Branch>', ''),
72 ('/Network/HTTP GET', 'do_suck', '', '<Shift>asciicircum'),
73 ('/Network/HTTP POST', 'do_http_post', '', ''),
74 ('/Network/Send SOAP message', 'do_soap_send', '', ''),
76 ('/Create', None, '<Branch>', ''),
77 ('/Create/Insert element', 'menu_insert_element', '', '<Shift>I'),
78 ('/Create/Append element', 'menu_append_element', '', '<Shift>A'),
79 ('/Create/Open element', 'menu_open_element', '', '<Shift>O'),
80 ('/Create/Open element at end', 'menu_open_element_end', '', '<Shift>E'),
82 ('/Create/', '', '', '<separator>'),
84 ('/Create/Insert text node', 'menu_insert_text', '', 'I'),
85 ('/Create/Append text node', 'menu_append_text', '', 'A'),
86 ('/Create/Open text node', 'menu_open_text', '', 'O'),
87 ('/Create/Open text node at end', 'menu_open_text_end', '', 'E'),
89 ('/Create/', '', '', '<separator>'),
91 ('/Create/Attribute', 'menu_show_add_attrib', '', '<Shift>plus'),
93 ('/Process', None, '<Branch>', ''),
94 ('/Process/Substitute', 'menu_show_subst', '', 's'),
95 ('/Process/Python expression', 'menu_show_pipe', '', '<Shift>exclam'),
96 ('/Process/XPath expression', 'menu_show_xpath', '', ''),
97 ('/Process/Normalise', 'do_normalise', '', ''),
98 ('/Process/Remove default namespaces', 'do_remove_ns', '', 'r'),
99 ('/Process/Comment to text', 'do_convert_to_text', '', ''),
101 ('/Program', None, '<Branch>', ''),
102 ('/Program/Input', 'menu_show_ask', '', 'question'),
103 ('/Program/Compare', 'do_compare', '', 'equal'),
104 ('/Program/Fail', 'do_fail', '', ''),
105 ('/Program/Pass', 'do_pass', '', ''),
106 ('/Program/Repeat last', 'do_again', '', 'dot'),
108 ('/View', None, '<Branch>', ''),
109 ('/View/Toggle hidden', 'do_toggle_hidden', '', '<Ctrl>H'),
110 ('/View/Show as HTML', 'do_show_html', '', ''),
111 ('/View/Show as canvas', 'do_show_canvas', '', ''),
112 ('/View/Close Window', 'menu_close_window', '', '<Ctrl>Q'),
114 ('/Options...', 'menu_options', '', '<Ctrl>O'),
118 return lambda(self
): self
.view
.may_record([action
])
120 class GUIView(Display
, XDSLoader
):
121 def __init__(self
, window
, view
):
122 Display
.__init
__(self
, window
, view
)
123 XDSLoader
.__init
__(self
, ['application/x-dome', 'text/xml',
125 window
.connect('key-press-event', self
.key_press
)
126 self
.cursor_node
= None
129 menu
.attach(window
, self
)
131 def update_state(self
):
132 if self
.view
.rec_point
:
133 state
= "(recording)"
134 elif self
.view
.idle_cb
or self
.view
.op_in_progress
:
138 self
.parent_window
.set_state(state
)
141 def xds_load_from_stream(self
, path
, type, stream
):
143 raise Exception('Can only load from files... sorry!')
144 if path
.endswith('.html'):
145 self
.view
.load_html(path
)
147 self
.view
.load_xml(path
)
148 if self
.view
.root
== self
.view
.model
.get_root():
149 self
.parent_window
.uri
= path
150 self
.parent_window
.update_title()
152 def key_press(self
, widget
, kev
):
153 focus
= widget
.focus_widget
154 if focus
and focus
is not widget
and focus
.get_toplevel() is widget
:
156 return TRUE
# Handled
160 if kev
.keyval
== keysyms
.Up
:
161 self
.view
.may_record(['move_prev_sib'])
162 elif kev
.keyval
== keysyms
.Down
:
163 self
.view
.may_record(['move_next_sib'])
164 elif kev
.keyval
== keysyms
.Left
:
165 self
.view
.may_record(['move_left'])
166 elif kev
.keyval
== keysyms
.Right
:
167 self
.view
.may_record(['move_right'])
168 elif kev
.keyval
== keysyms
.KP_Add
:
169 self
.menu_show_add_attrib()
170 elif kev
.keyval
== keysyms
.Tab
:
176 def node_clicked(self
, node
, bev
):
177 print "Clicked", node
.namespaceURI
, node
.localName
179 if bev
.type == g
.gdk
.BUTTON_PRESS
:
180 if len(self
.view
.current_nodes
) == 0:
183 src
= self
.view
.current_nodes
[-1]
184 shift
= bev
.state
& g
.gdk
.SHIFT_MASK
185 add
= bev
.state
& g
.gdk
.CONTROL_MASK
186 select_region
= shift
and node
.nodeType
== Node
.ELEMENT_NODE
187 lit
= shift
and not select_region
190 path
= make_relative_path(src
, node
, lit
, ns
)
191 if path
== '.' and self
.view
.current_nodes
and not self
.view
.current_attrib
:
194 self
.view
.may_record(["select_region", path
, ns
])
196 self
.view
.may_record(["do_search", path
, ns
, add
])
198 self
.view
.may_record(["toggle_hidden"])
200 def attrib_clicked(self
, element
, attrib
, event
):
201 if len(self
.view
.current_nodes
) == 0:
204 src
= self
.view
.current_nodes
[-1]
207 print "attrib_clicked", attrib
, attrib
.namespaceURI
, attrib
.localName
208 path
= make_relative_path(src
, element
, FALSE
, ns
)
210 self
.view
.may_record(["do_search", path
, ns
, FALSE
])
211 self
.view
.may_record(["attribute", attrib
.namespaceURI
, attrib
.localName
])
214 self
.parent_window
.save()
216 def show_menu(self
, bev
):
217 menu
.popup(self
, bev
)
219 def playback(self
, macro
, map):
220 "Called when the user clicks on a macro button."
221 Exec
.exec_state
.clean()
223 self
.view
.may_record(['map', macro
.uri
])
225 self
.view
.may_record(['play', macro
.uri
])
227 def menu_show_ask(self
):
228 def do_ask(q
, self
= self
):
230 self
.view
.may_record(action
)
231 GetArg('Input:', do_ask
, ('Prompt:',))
233 def menu_show_subst(self
):
234 def do_subst(args
, self
= self
):
235 action
= ["subst", args
[0], args
[1]]
236 self
.view
.may_record(action
)
237 GetArg('Substitute:', do_subst
, ('Replace:', 'With:'))
239 def move_from(self
, old
= []):
241 Display
.move_from(self
, old
)
243 def hide_editbox(self
):
245 if self
.cursor_attrib
:
246 self
.cursor_hidden_text
.set(text
= '%s=%s' %
247 (self
.cursor_attrib
.name
, self
.cursor_attrib
.value
))
248 self
.cursor_hidden_text
.show()
249 self
.auto_highlight(self
.cursor_node
)
250 self
.cursor_node
= None
251 self
.edit_box_item
.destroy()
253 def show_editbox(self
):
254 "Edit the current node/attribute"
261 raise Exception("Can't edit while display is hidden!")
263 self
.cursor_node
= self
.view
.current_nodes
[0]
264 group
= self
.node_to_group
[self
.cursor_node
]
265 self
.cursor_attrib
= self
.view
.current_attrib
267 self
.highlight(group
, FALSE
)
269 if self
.cursor_attrib
:
270 group
= group
.attrib_to_group
[self
.cursor_attrib
]
272 self
.cursor_hidden_text
= group
.text
273 if not self
.cursor_attrib
:
274 # Don't hide for attributes, so we can still see the name
277 group
.text
.set(text
= str(self
.cursor_attrib
.name
) + '=')
279 self
.update_now() # GnomeCanvas bug?
280 lx
, ly
, hx
, hy
= group
.text
.get_bounds()
281 x
, y
= group
.i2w(lx
, ly
)
289 self
.edit_box_text
= text
292 #s = eb.get_style().copy()
293 #s.font = load_font('fixed')
295 #if self.cursor_attrib:
296 # name_width = s.font.measure(self.cursor_attrib.name + '=') + 1
301 self
.edit_box_item
= self
.root().add(canvas
.CanvasWidget
, widget
= eb
,
302 x
= x
- m
+ name_width
, y
= y
- m
,
303 anchor
= g
.ANCHOR_NW
)
305 #text.set_editable(TRUE)
306 text
.get_buffer().insert_at_cursor(self
.get_edit_text(), -1)
307 text
.set_wrap_mode(g
.WRAP_WORD
)
308 text
.get_buffer().connect('changed', self
.eb_changed
)
309 text
.connect('key-press-event', self
.eb_key
)
314 #eb.select_region(0, -1)
317 def get_edit_text(self
):
318 node
= self
.cursor_node
319 if node
.nodeType
== Node
.ELEMENT_NODE
:
320 if self
.cursor_attrib
:
321 return str(self
.cursor_attrib
.value
)
324 return node
.nodeValue
326 def eb_key(self
, eb
, kev
):
328 if key
== g
.keysyms
.KP_Enter
:
329 key
= g
.keysyms
.Return
330 if key
== g
.keysyms
.Escape
:
332 elif key
== g
.keysyms
.Return
and kev
.state
& g
.gdk
.CONTROL_MASK
:
333 eb
.insert_defaults('\n')
335 elif key
== g
.keysyms
.Tab
or key
== g
.keysyms
.Return
:
336 buffer = eb
.get_buffer()
337 s
= buffer.get_start_iter()
338 e
= buffer.get_end_iter()
339 text
= buffer.get_text(s
, e
, TRUE
)
341 if text
!= self
.get_edit_text():
342 self
.commit_edit(text
)
347 def commit_edit(self
, new
):
348 if self
.cursor_attrib
:
349 self
.view
.may_record(['set_attrib', new
])
351 self
.view
.may_record(['change_node', new
])
353 def eb_changed(self
, eb
):
358 req
= self
.edit_box_text
.size_request()
360 width
= max(req
[0], 10)
361 height
= max(req
[1], 10)
362 self
.edit_box_item
.set(width
= width
+ 12, height
= height
+ 4)
365 def toggle_edit(self
):
371 def menu_select_attrib(self
):
372 def do_attrib(name
, self
= self
):
374 (prefix
, localName
) = name
.split(':', 1)
376 (prefix
, localName
) = (None, name
)
377 namespaceURI
= self
.view
.model
.prefix_to_namespace(self
.view
.get_current(), prefix
)
378 action
= ["attribute", namespaceURI
, localName
]
379 self
.view
.may_record(action
)
380 GetArg('Select attribute:', do_attrib
, ['Name:'])
382 def menu_show_add_attrib(self
):
383 def do_it(name
, self
= self
):
384 action
= ["add_attrib", "UNUSED", name
]
385 self
.view
.may_record(action
)
386 GetArg('Create attribute:', do_it
, ['Name:'])
388 def menu_show_pipe(self
):
389 def do_pipe(expr
, self
= self
):
390 action
= ["python", expr
]
391 self
.view
.may_record(action
)
392 GetArg('Python expression:', do_pipe
, ['Eval:'], "'x' is the old text...")
394 def menu_show_xpath(self
):
395 def go(expr
, self
= self
):
396 action
= ["xpath", expr
]
397 self
.view
.may_record(action
)
398 GetArg('XPath expression:', go
, ['Eval:'], "Result goes on the clipboard")
400 def menu_show_global(self
):
401 def do_global(pattern
, self
= self
):
402 action
= ["do_global", pattern
]
403 self
.view
.may_record(action
)
404 GetArg('Global:', do_global
, ['Pattern:'],
405 '(@CURRENT@ is the current node\'s value)\n' +
406 'Perform next action on all nodes matching')
408 def menu_show_text_search(self
):
409 def do_text_search(pattern
, self
= self
):
410 action
= ["do_text_search", pattern
]
411 self
.view
.may_record(action
)
412 GetArg('Search for:', do_text_search
, ['Text pattern:'],
413 '(@CURRENT@ is the current node\'s value)\n')
415 def menu_show_search(self
):
416 def do_search(pattern
, self
= self
):
417 action
= ["do_search", pattern
]
418 self
.view
.may_record(action
)
419 GetArg('Search for:',
420 do_search
, ['XPath:'],
421 '(@CURRENT@ is the current node\'s value)')
423 def show_add_box(self
, action
):
426 elif action
[0] == 'a':
428 elif action
[0] == 'o':
430 elif action
[0] == 'e':
437 elif action
[1] == 't':
444 self
.view
.may_record(['add_node', action
, value
])
445 GetArg('Add node', cb
, [prompt
], text
)
448 cur
= self
.view
.get_current()
449 if cur
.nodeType
== Node
.ELEMENT_NODE
:
451 return cur
.parentNode
.nodeName
453 def menu_insert_element(self
):
455 self
.show_add_box('ie')
457 def menu_append_element(self
):
459 self
.show_add_box('ae')
461 def menu_open_element(self
):
463 self
.show_add_box('oe')
465 def menu_open_element_end(self
):
466 "Open element at end"
467 self
.show_add_box('ee')
469 def menu_insert_text(self
):
471 self
.show_add_box('it')
473 def menu_append_text(self
):
475 self
.show_add_box('at')
477 def menu_open_text(self
):
479 self
.show_add_box('ot')
481 def menu_open_text_end(self
):
483 self
.show_add_box('et')
485 def menu_close_window(self
):
486 self
.parent_window
.destroy()
488 def menu_options(self
):
491 def menu_clear_undo(self
):
492 if rox
.confirm('Really clear the undo buffer?',
494 self
.view
.model
.clear_undo()
496 do_blank_all
= make_do('blank_all')
497 do_enter
= make_do('enter')
498 do_leave
= make_do('leave')
499 do_suck
= make_do('suck')
500 do_http_post
= make_do('http_post')
501 do_soap_send
= make_do('soap_send')
502 do_select_dups
= make_do('select_dups')
503 do_paste_attribs
= make_do('paste_attribs')
504 do_yank_value
= make_do('yank_value')
505 do_yank_attributes
= make_do('yank_attribs')
506 do_delete_node
= make_do('delete_node')
507 do_delete_node_no_clipboard
= make_do('delete_node_no_clipboard')
508 do_delete_shallow
= make_do('delete_shallow')
509 do_yank
= make_do('yank')
510 do_shallow_yank
= make_do('shallow_yank')
511 do_put_replace
= make_do('put_replace')
512 do_put_as_child
= make_do('put_as_child')
513 do_put_before
= make_do('put_before')
514 do_put_after
= make_do('put_after')
515 do_undo
= make_do('undo')
516 do_redo
= make_do('redo')
517 do_fail
= make_do('fail')
518 do_pass
= make_do('do_pass')
519 do_toggle_hidden
= make_do('toggle_hidden')
520 do_show_html
= make_do('show_html')
521 do_show_canvas
= make_do('show_canvas')
522 do_compare
= make_do('compare')
523 do_again
= make_do('again')
524 do_normalise
= make_do('normalise')
525 do_convert_to_text
= make_do('convert_to_text')
526 do_remove_ns
= make_do('remove_ns')
528 do_clear_mark
= make_do('clear_mark')
529 do_mark_switch
= make_do('mark_switch')
530 do_mark_selection
= make_do('mark_selection')
531 do_select_marked
= make_do('select_marked_region')
532 do_select_children
= make_do('select_children')
534 move_home
= make_do('move_home')
535 move_end
= make_do('move_end')
536 move_left
= make_do('move_left')
537 move_right
= make_do('move_right')
538 move_next_sib
= make_do('move_next_sib')
539 move_prev_sib
= make_do('move_prev_sib')