Improved UI for 'Select by XPath'.
[dom-editor.git] / Dome / GetArg.py
blob03dc1dd90910b5433999a5246cf2fd65cddcc07e
1 import rox
2 from rox import g, TRUE, FALSE
4 # text -> last value
5 history = {}
7 class Examples(g.ScrolledWindow):
8 def __init__(self, hints):
9 g.ScrolledWindow.__init__(self)
10 self.set_shadow_type(g.SHADOW_IN)
11 self.set_policy(g.POLICY_NEVER, g.POLICY_AUTOMATIC)
12 self.set_border_width(4)
14 model = g.ListStore(str, str)
15 view = g.TreeView(model)
16 self.add(view)
17 view.show()
19 cell = g.CellRendererText()
20 column = g.TreeViewColumn('Example pattern', cell, text = 0)
21 view.append_column(column)
22 column = g.TreeViewColumn('Meaning', cell, text = 1)
23 view.append_column(column)
25 for c, m in hints:
26 new = model.append()
27 model.set(new, 0, c, 1, m)
29 self.set_size_request(-1, 150)
31 view.get_selection().set_mode(g.SELECTION_NONE)
34 # A window which allows the user to enter a string.
35 # When this is done, callback(string) or callback(strings) is called.
36 # args is a list like ('Replace:', 'With:')
37 # If 'destroy_return' is true then closing the window does callback(None).
39 class GetArg(rox.Dialog):
40 def __init__(self, text, callback, args, message = None,
41 destroy_return = 0, init = None, hints = None):
42 rox.Dialog.__init__(self)
43 self.set_has_separator(False)
44 self.set_position(g.WIN_POS_MOUSE)
46 if init:
47 init = init[:]
49 self.callback = callback
50 self.text = text
52 self.set_title(text)
54 if message:
55 self.vbox.pack_start(g.Label(message), not hints, True, 0)
56 if hints:
57 self.vbox.pack_end(Examples(hints), True, True, 0)
59 self.args = []
61 for a in args:
62 hbox = g.HBox(FALSE, 4)
63 hbox.pack_start(g.Label(a), FALSE, TRUE, 0)
64 arg = g.Entry()
65 hbox.pack_start(arg, TRUE, TRUE, 0)
66 self.vbox.pack_start(hbox, FALSE, TRUE, 0)
67 if init and init[0]:
68 arg.set_text(init[0])
69 del init[0]
70 if history.has_key(a):
71 arg.set_text(history[a])
72 if not self.args:
73 arg.grab_focus()
74 arg.select_region(0, -1)
75 self.args.append((a, arg))
76 if len(self.args) < len(args):
77 arg.connect('activate', self.to_next)
78 else:
79 arg.connect('activate', lambda w: self.do_it())
81 actions = g.HBox(TRUE, 32)
82 self.vbox.pack_end(actions, FALSE, TRUE, 0)
84 self.add_button(g.STOCK_CANCEL, g.RESPONSE_CANCEL)
85 self.add_button(g.STOCK_OK, g.RESPONSE_OK)
87 def resp(widget, resp):
88 if resp == g.RESPONSE_OK:
89 self.do_it()
90 widget.destroy()
91 self.connect('response', resp)
93 if destroy_return:
94 self.connect('destroy', lambda widget, cb = callback: cb(None))
96 self.show_all()
98 def to_next(self, widget):
99 next = 0
100 for (a, entry) in self.args:
101 if next:
102 entry.grab_focus()
103 entry.select_region(0, -1)
104 return
105 if entry == widget:
106 next = 1
108 def do_it(self):
109 values = []
110 for (a, entry) in self.args:
111 val = entry.get_text()
112 values.append(val)
113 history[a] = val
114 if len(values) > 1:
115 self.callback(values)
116 else:
117 self.callback(values[0])
118 self.destroy()
119 return 1