1 from xml
.dom
import Node
2 from rox
.loading
import XDSLoader
5 from rox
import g
, TRUE
, FALSE
9 from Display2
import Display
11 from GetArg
import GetArg
12 from Path
import make_relative_path
14 from rox
.Menu
import Menu
17 ('/File', None, '<Branch>', ''),
18 ('/File/Save', 'menu_save', '<StockItem>', '<Ctrl>S', g
.STOCK_SAVE
),
19 ('/File/Blank document', 'do_blank_all', '<StockItem>', '<Ctrl>N', g
.STOCK_NEW
),
20 ('/File/Clear undo buffer', 'menu_clear_undo', '<StockItem>', '', g
.STOCK_CLEAR
),
22 ('/Edit', None, '<Branch>', ''),
23 ('/Edit/Copy attributes', 'do_yank_attributes', '<StockItem>', '', g
.STOCK_COPY
),
24 ('/Edit/Paste attributes', 'do_paste_attribs', '<StockItem>', '', g
.STOCK_PASTE
),
25 ('/Edit/Copy attrib value', 'do_yank_value', '<StockItem>', '', g
.STOCK_COPY
),
26 ('/Edit/Rename attribute', 'menu_rename_attr', '<StockItem>', '', g
.STOCK_PROPERTIES
),
27 ('/Edit/', '', '', '<separator>'),
28 ('/Edit/Cut', 'do_delete_node', '<StockItem>', '<Ctrl>X', g
.STOCK_DELETE
),
29 ('/Edit/Delete', 'do_delete_node_no_clipboard', '<StockItem>', '', g
.STOCK_DELETE
),
30 ('/Edit/Shallow cut', 'do_delete_shallow', '<StockItem>', '<Shift>X', g
.STOCK_DELETE
),
31 ('/Edit/', '', '', '<separator>'),
32 ('/Edit/Copy', 'do_yank', '<StockItem>', '<Ctrl>C', g
.STOCK_COPY
),
33 ('/Edit/Shallow copy', 'do_shallow_yank', '<StockItem>', '<Shift>Y', g
.STOCK_COPY
),
34 ('/Edit/', '', '', '<separator>'),
35 ('/Edit/Paste (replace)','do_put_replace', '<StockItem>', '<Ctrl>V', g
.STOCK_PASTE
),
36 ('/Edit/Paste (inside)', 'do_put_as_child', '<StockItem>', 'bracketright', g
.STOCK_GO_FORWARD
),
37 ('/Edit/Paste (before)', 'do_put_before', '<StockItem>', '<Shift>P', g
.STOCK_GO_UP
),
38 ('/Edit/Paste (after)', 'do_put_after', '<StockItem>', 'p', g
.STOCK_GO_DOWN
),
39 ('/Edit/', '', '', '<separator>'),
40 ('/Edit/Edit value', 'toggle_edit', '<StockItem>', 'Return', g
.STOCK_PROPERTIES
),
41 ('/Edit/', '', '', '<separator>'),
42 ('/Edit/Undo', 'do_undo', '<StockItem>', '<Ctrl>Z', g
.STOCK_UNDO
),
43 ('/Edit/Redo', 'do_redo', '<StockItem>', '<Ctrl>Y', g
.STOCK_REDO
),
45 ('/Move', None, '<Branch>', ''),
46 ('/Move/XPath search', 'menu_show_search', '<StockItem>', 'slash', g
.STOCK_FIND
),
47 ('/Move/Text search', 'menu_show_text_search', '<StockItem>', 'T', g
.STOCK_FIND
),
48 ('/Move/Enter', 'do_enter', '', '<Shift>greater'),
49 ('/Move/Leave', 'do_leave', '', '<Shift>less'),
51 ('/Move/Root node', 'move_home', '<StockItem>', 'Home', g
.STOCK_HOME
),
52 ('/Move/Previous sibling', 'move_prev_sib', '<StockItem>', 'Up', g
.STOCK_GO_UP
),
53 ('/Move/Next sibling', 'move_next_sib', '<StockItem>', 'Down', g
.STOCK_GO_DOWN
),
54 ('/Move/Parent', 'move_left', '<StockItem>', 'Left', g
.STOCK_GO_BACK
),
55 ('/Move/First child', 'move_right', '<StockItem>', 'Right', g
.STOCK_GO_FORWARD
),
56 ('/Move/Last child', 'move_end', '<StockItem>', 'End', g
.STOCK_GOTO_LAST
),
58 ('/Move/To attribute', 'menu_select_attrib', '<StockItem>', 'At', g
.STOCK_JUMP_TO
),
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', '<StockItem>', '', g
.STOCK_CLEAR
),
70 ('/Mark/Move marked here', 'do_move_marked', '<StockItem>', '', g
.STOCK_GO_FORWARD
),
72 ('/Network', None, '<Branch>', ''),
73 ('/Network/HTTP GET', 'do_suck', '', '<Shift>asciicircum'),
74 ('/Network/HTTP POST', 'do_http_post', '', ''),
75 ('/Network/Send SOAP message', 'do_soap_send', '', ''),
77 ('/Create', None, '<Branch>', ''),
78 ('/Create/Insert node', 'menu_insert_element', '<StockItem>', 'I', g
.STOCK_ADD
),
79 ('/Create/Append node', 'menu_append_element', '<StockItem>', 'A', g
.STOCK_ADD
),
80 ('/Create/Open node inside', 'menu_open_element', '<StockItem>', 'O', g
.STOCK_ADD
),
81 ('/Create/Open node at end', 'menu_open_element_end', '<StockItem>', 'E', g
.STOCK_ADD
),
83 ('/Process', None, '<Branch>', ''),
84 ('/Process/Substitute', 'menu_show_subst', '', 's'),
85 ('/Process/Python expression', 'menu_show_pipe', '', '<Shift>exclam'),
86 ('/Process/XPath expression', 'menu_show_xpath', '', ''),
87 ('/Process/Normalise', 'do_normalise', '', ''),
88 ('/Process/Remove default namespaces', 'do_remove_ns', '', 'r'),
89 ('/Process/Convert to text', 'do_convert_to_text', '', ''),
90 ('/Process/Convert to comment', 'do_convert_to_comment', '', ''),
91 ('/Process/Convert to element', 'do_convert_to_element', '', ''),
93 ('/Program', None, '<Branch>', ''),
94 ('/Program/Input', 'menu_show_ask', '', 'question'),
95 ('/Program/Compare', 'do_compare', '', 'equal'),
96 ('/Program/Fail', 'do_fail', '', ''),
97 ('/Program/Pass', 'do_pass', '', ''),
98 ('/Program/Repeat last', 'do_again', '', 'dot'),
100 ('/View', None, '<Branch>', ''),
101 ('/View/Toggle hidden', 'do_toggle_hidden', '', '<Ctrl>H'),
102 ('/View/Hide with expr', 'menu_hide_with_expr', '', ''),
103 ('/View/Show as HTML', 'do_show_html', '', ''),
104 ('/View/Show as canvas', 'do_show_canvas', '', ''),
105 ('/View/Show namespaces', 'show_namespaces', '', '<Ctrl>;'),
106 ('/View/Close Window', 'menu_close_window', '<StockItem>', '<Ctrl>Q', g
.STOCK_CLOSE
),
108 #('/Options...', 'menu_options', '', '<Ctrl>O'),
112 return lambda(self
): self
.view
.may_record([action
])
114 class GUIView(Display
, XDSLoader
):
115 def __init__(self
, window
, view
):
116 Display
.__init
__(self
, window
, view
)
117 XDSLoader
.__init
__(self
, ['application/x-dome', 'text/xml',
119 window
.connect('key-press-event', self
.key_press
)
120 self
.cursor_node
= None
121 self
.edit_dialog
= None
124 menu
.attach(window
, self
)
126 def destroyed(self
, widget
):
127 print "GUIView destroyed!"
128 Display
.destroyed(self
, widget
)
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 do_drag(self
, src
, dst
):
178 return # Don't do attribute drags yet
179 src
, dst
= src
[0], dst
[0]
180 path
= make_relative_path(src
, dst
, None, self
.view
.model
.namespaces
)
181 self
.view
.may_record(['move_selection', path
])
183 def node_clicked(self
, node
, bev
):
184 print "Clicked", node
.namespaceURI
, node
.localName
186 if bev
.type == g
.gdk
.BUTTON_PRESS
:
187 if len(self
.view
.current_nodes
) == 0:
190 src
= self
.view
.current_nodes
[-1]
191 shift
= bev
.state
& g
.gdk
.SHIFT_MASK
192 add
= bev
.state
& g
.gdk
.CONTROL_MASK
193 select_region
= shift
and node
.nodeType
== Node
.ELEMENT_NODE
194 lit
= shift
and not select_region
196 path
= make_relative_path(src
, node
, lit
, self
.view
.model
.namespaces
)
197 if path
== '.' and self
.view
.current_nodes
and not self
.view
.current_attrib
:
200 self
.view
.may_record(["select_region", path
, "unused"])
202 self
.view
.may_record(["do_search", path
, "unused", add
])
204 self
.view
.may_record(["toggle_hidden"])
206 def attrib_clicked(self
, element
, attrib
, event
):
207 if len(self
.view
.current_nodes
) == 0:
210 src
= self
.view
.current_nodes
[-1]
212 print "attrib_clicked", attrib
, attrib
.namespaceURI
, attrib
.localName
213 path
= make_relative_path(src
, element
, FALSE
, self
.view
.model
.namespaces
)
215 self
.view
.may_record(["do_search", path
, "unused", FALSE
])
216 self
.view
.may_record(["attribute", attrib
.namespaceURI
, attrib
.localName
])
219 self
.parent_window
.save()
221 def show_menu(self
, bev
):
222 menu
.popup(self
, bev
)
224 def playback(self
, macro
, map):
225 "Called when the user clicks on a macro button."
226 Exec
.exec_state
.clean()
228 self
.view
.may_record(['map', macro
.uri
])
230 self
.view
.may_record(['play', macro
.uri
])
232 def menu_show_ask(self
):
233 def do_ask(q
, self
= self
):
235 self
.view
.may_record(action
)
236 GetArg('Input:', do_ask
, ('Prompt:',))
238 def menu_show_subst(self
):
239 def do_subst(args
, self
= self
):
240 action
= ["subst", args
[0], args
[1]]
241 self
.view
.may_record(action
)
242 GetArg('Substitute:', do_subst
, ('Replace:', 'With:'))
244 def show_namespaces(self
):
246 Namespaces
.GUI(self
.view
.model
).show()
248 def move_from(self
, old
= []):
250 Display
.move_from(self
, old
)
252 def hide_editbox(self
):
254 if self
.cursor_attrib
:
255 self
.cursor_hidden_text
.set(text
= '%s=%s' %
256 (self
.cursor_attrib
.name
, self
.cursor_attrib
.value
))
257 self
.cursor_hidden_text
.show()
258 self
.auto_highlight(self
.cursor_node
)
259 self
.cursor_node
= None
260 self
.edit_box_item
.destroy()
262 def toggle_edit(self
):
263 node
= self
.view
.current_nodes
[0]
264 attrib
= self
.view
.current_attrib
267 self
.edit_dialog
.destroy()
268 self
.edit_dialog
= rox
.Dialog()
269 eb
= self
.edit_dialog
271 if node
.nodeType
== Node
.ELEMENT_NODE
:
273 text
= unicode(attrib
.value
)
278 entry
.set_activates_default(True)
279 def get_text(): return entry
.get_text()
281 text
= node
.nodeValue
283 buffer = entry
.get_buffer()
284 buffer.insert_at_cursor(text
)
285 entry
.set_size_request(400, 200)
288 start
= buffer.get_start_iter()
289 end
= buffer.get_end_iter()
290 return buffer.get_text(start
, end
, True)
291 eb
.vbox
.pack_start(entry
, TRUE
, FALSE
, 0)
293 eb
.add_button(g
.STOCK_CANCEL
, g
.RESPONSE_CANCEL
)
294 eb
.add_button(g
.STOCK_APPLY
, g
.RESPONSE_OK
)
295 eb
.set_default_response(g
.RESPONSE_OK
)
298 self
.edit_dialog
= None
299 eb
.connect('destroy', destroy
)
300 def response(eb
, resp
):
301 if resp
== g
.RESPONSE_CANCEL
:
303 elif resp
== g
.RESPONSE_OK
:
307 self
.view
.may_record(['set_attrib', new
])
309 self
.view
.may_record(['change_node', new
])
311 eb
.connect('response', response
)
315 def menu_select_attrib(self
):
318 (prefix
, localName
) = name
.split(':', 1)
319 namespaceURI
= self
.view
.model
.prefix_to_namespace(self
.view
.get_current(), prefix
)
321 (prefix
, localName
) = (None, name
)
323 action
= ["attribute", namespaceURI
, localName
]
324 self
.view
.may_record(action
)
325 GetArg('Select attribute:', do_attrib
, ['Name:'])
327 def menu_show_add_attrib(self
):
329 action
= ["add_attrib", "UNUSED", name
]
330 self
.view
.may_record(action
)
331 GetArg('Create attribute:', do_it
, ['Name:'])
333 def menu_show_pipe(self
):
335 action
= ["python", expr
]
336 self
.view
.may_record(action
)
337 GetArg('Python expression:', do_pipe
, ['Eval:'], "'x' is the old text...")
339 def menu_show_xpath(self
):
341 action
= ["xpath", expr
]
342 self
.view
.may_record(action
)
343 GetArg('XPath expression:', go
, ['Eval:'], "Result goes on the clipboard")
345 def menu_hide_with_expr(self
):
347 action
= ["toggle_hidden", expr
]
348 self
.view
.may_record(action
)
349 GetArg('XPath expression:', go
, ['Eval:'], "Expr is used for the 'hidden' message.\nEg: @title")
351 def menu_show_global(self
):
352 def do_global(pattern
):
353 action
= ["do_global", pattern
]
354 self
.view
.may_record(action
)
355 GetArg('Global:', do_global
, ['Pattern:'],
356 '(@CURRENT@ is the current node\'s value)\n' +
357 'Perform next action on all nodes matching')
359 def menu_show_text_search(self
):
360 def do_text_search(pattern
):
361 action
= ["do_text_search", pattern
]
362 self
.view
.may_record(action
)
363 GetArg('Search for:', do_text_search
, ['Text pattern:'],
364 '(@CURRENT@ is the current node\'s value)\n')
366 def menu_show_search(self
):
367 def do_search(pattern
):
368 action
= ["do_search", pattern
]
369 self
.view
.may_record(action
)
370 GetArg('Search for:',
371 do_search
, ['XPath:'],
372 '(@CURRENT@ is the current node\'s value)')
374 def menu_rename_attr(self
):
376 action
= ["rename_attrib", name
]
377 self
.view
.may_record(action
)
378 GetArg('Rename to:', do
, ['New name:'])
381 def do_create(self
, action
, nodeType
, data
):
384 if nodeType
== Node
.ELEMENT_NODE
:
386 elif nodeType
== Node
.TEXT_NODE
:
389 elif nodeType
== Node
.ATTRIBUTE_NODE
:
393 # Check name is valid
394 # XXX: Should be more strict
396 assert '\n' not in data
397 assert ' ' not in data
399 self
.view
.may_record(['add_node', action
, data
])
401 def show_add_box(self
, action
):
404 elif action
[0] == 'a':
406 elif action
[0] == 'o':
408 elif action
[0] == 'e':
415 elif action
[1] == 't':
423 box
.vbox
.pack_start(text
, TRUE
, FALSE
, 0)
424 text
.set_size_request(400, 200)
425 box
.set_has_separator(False)
427 box
.add_button(g
.STOCK_CANCEL
, g
.RESPONSE_CANCEL
)
428 box
.add_button('Add _Attribute', Node
.ATTRIBUTE_NODE
)
429 box
.add_button('Add _Text', Node
.TEXT_NODE
)
430 box
.add_button('Add _Element', Node
.ELEMENT_NODE
)
431 box
.set_default_response(g
.RESPONSE_OK
)
433 def response(box
, resp
):
434 if resp
== g
.RESPONSE_CANCEL
:
437 buffer = text
.get_buffer()
438 start
= buffer.get_start_iter()
439 end
= buffer.get_end_iter()
440 new
= buffer.get_text(start
, end
, True)
442 self
.do_create(action
, resp
, new
)
444 box
.connect('response', response
)
449 cur
= self
.view
.get_current()
450 if cur
.nodeType
== Node
.ELEMENT_NODE
:
452 return cur
.parentNode
.nodeName
454 def menu_insert_element(self
):
456 self
.show_add_box('ie')
458 def menu_append_element(self
):
460 self
.show_add_box('ae')
462 def menu_open_element(self
):
464 self
.show_add_box('oe')
466 def menu_open_element_end(self
):
467 "Open element at end"
468 self
.show_add_box('ee')
470 def menu_insert_text(self
):
472 self
.show_add_box('it')
474 def menu_append_text(self
):
476 self
.show_add_box('at')
478 def menu_open_text(self
):
480 self
.show_add_box('ot')
482 def menu_open_text_end(self
):
484 self
.show_add_box('et')
486 def menu_close_window(self
):
487 self
.parent_window
.destroy()
489 def menu_options(self
):
492 def menu_clear_undo(self
):
493 if rox
.confirm('Really clear the undo buffer?',
495 self
.view
.model
.clear_undo()
497 do_blank_all
= make_do('blank_all')
498 do_enter
= make_do('enter')
499 do_leave
= make_do('leave')
500 do_suck
= make_do('suck')
501 do_http_post
= make_do('http_post')
502 do_soap_send
= make_do('soap_send')
503 do_select_dups
= make_do('select_dups')
504 do_paste_attribs
= make_do('paste_attribs')
505 do_yank_value
= make_do('yank_value')
506 do_yank_attributes
= make_do('yank_attribs')
507 do_delete_node
= make_do('delete_node')
508 do_delete_node_no_clipboard
= make_do('delete_node_no_clipboard')
509 do_delete_shallow
= make_do('delete_shallow')
510 do_yank
= make_do('yank')
511 do_shallow_yank
= make_do('shallow_yank')
512 do_put_replace
= make_do('put_replace')
513 do_put_as_child
= make_do('put_as_child')
514 do_put_before
= make_do('put_before')
515 do_put_after
= make_do('put_after')
516 do_undo
= make_do('undo')
517 do_redo
= make_do('redo')
518 do_fail
= make_do('fail')
519 do_pass
= make_do('do_pass')
520 do_toggle_hidden
= make_do('toggle_hidden')
521 do_show_html
= make_do('show_html')
522 do_show_canvas
= make_do('show_canvas')
523 do_compare
= make_do('compare')
524 do_again
= make_do('again')
525 do_normalise
= make_do('normalise')
526 do_convert_to_text
= make_do('convert_to_text')
527 do_convert_to_comment
= make_do('convert_to_comment')
528 do_convert_to_element
= make_do('convert_to_element')
529 do_convert_to_pi
= make_do('convert_to_pi')
530 do_remove_ns
= make_do('remove_ns')
532 do_clear_mark
= make_do('clear_mark')
533 do_move_marked
= make_do('move_marked')
534 do_mark_switch
= make_do('mark_switch')
535 do_mark_selection
= make_do('mark_selection')
536 do_select_marked
= make_do('select_marked_region')
537 do_select_children
= make_do('select_children')
539 move_home
= make_do('move_home')
540 move_end
= make_do('move_end')
541 move_left
= make_do('move_left')
542 move_right
= make_do('move_right')
543 move_next_sib
= make_do('move_next_sib')
544 move_prev_sib
= make_do('move_prev_sib')