]> git.ipfire.org Git - thirdparty/newt.git/blob - snack.py
GridForm's run_once separated from run, so that run can be called multiple
[thirdparty/newt.git] / snack.py
1 # snack.py: maps C extension module _snack to proper python types in module
2 # snack.
3 # The first section is a very literal mapping.
4 # The second section contains convenience classes that amalgamate
5 # the literal classes and make them more object-oriented.
6
7 import _snack
8
9 class Widget:
10 pass
11
12 class Button(Widget):
13
14 def __init__(self, text):
15 self.w = _snack.button(text)
16
17 class Checkbox(Widget):
18
19 def value(self):
20 return self.w.checkboxValue
21
22 def selected(self):
23 return self.w.checkboxValue != 0
24
25 def __init__(self, text, isOn = 0):
26 self.w = _snack.checkbox(text, isOn)
27
28 class SingleRadioButton(Widget):
29
30 def selected(self):
31 return self.w.key == self.w.radioValue;
32
33 def __init__(self, text, group, isOn = 0):
34 if group:
35 self.w = _snack.radiobutton(text, group.w, isOn)
36 else:
37 self.w = _snack.radiobutton(text, None, isOn)
38
39 class Listbox(Widget):
40
41 def append(self, text):
42 return self.w.listboxAddItem(text)
43
44 def current(self):
45 return self.w.listboxGetCurrent()
46
47 def __init__(self, height, scroll = 0, returnExit = 0, width = 0):
48 self.w = _snack.listbox(height, scroll, returnExit)
49 if (width):
50 self.w.listboxSetWidth(width)
51
52 class Textbox(Widget):
53
54 def __init__(self, width, height, text, scroll = 0):
55 self.w = _snack.textbox(width, height, text, scroll)
56
57 class Label(Widget):
58
59 def __init__(self, text):
60 self.w = _snack.label(text)
61
62 class Entry(Widget):
63
64 def value(self):
65 return self.w.entryValue
66
67 def set(self, text):
68 return self.w.entrySetValue(text)
69
70 def __init__(self, width, text = "", hidden = 0, scroll = 1):
71 self.w = _snack.entry(width, text, hidden, scroll)
72
73
74 # Form uses hotkeys
75 hotkeys = { "F1" : _snack.KEY_F1, "F2" : _snack.KEY_F2, "F3" : _snack.KEY_F3,
76 "F4" : _snack.KEY_F4, "F5" : _snack.KEY_F5, "F6" : _snack.KEY_F6,
77 "F7" : _snack.KEY_F7, "F8" : _snack.KEY_F8, "F9" : _snack.KEY_F9,
78 "F10" : _snack.KEY_F10, "F11" : _snack.KEY_F11,
79 "F12" : _snack.KEY_F12 }
80
81 for n in hotkeys.keys():
82 hotkeys[hotkeys[n]] = n
83
84 class Form:
85
86 def addHotKey(self, keyname):
87 self.w.addhotkey(hotkeys[keyname])
88
89 def add(self, widget):
90 if widget.__dict__.has_key('gridmembers'):
91 for w in widget.gridmembers:
92 self.add(w)
93 elif widget.__dict__.has_key('w'):
94 self.trans[widget.w.key] = widget
95 return self.w.add(widget.w)
96 return None
97
98 def run(self):
99 (what, which) = self.w.run()
100 if (what == _snack.FORM_EXIT_WIDGET):
101 return self.trans[which]
102
103 return hotkeys[which]
104
105 def __init__(self):
106 self.trans = {}
107 self.w = _snack.form()
108
109 class Grid:
110
111 def place(self, x, y):
112 return self.g.place(x, y)
113
114 def setField(self, what, col, row, padding = (0, 0, 0, 0),
115 anchorLeft = 0, anchorTop = 0, anchorRight = 0,
116 anchorBottom = 0, growx = 0, growy = 0):
117 self.gridmembers.append(what)
118 anchorFlags = 0
119 if (anchorLeft):
120 anchorFlags = _snack.ANCHOR_LEFT
121 elif (anchorRight):
122 anchorFlags = _snack.ANCHOR_RIGHT
123
124 if (anchorTop):
125 anchorFlags = anchorFlags | _snack.ANCHOR_TOP
126 elif (anchorBottom):
127 anchorFlags = anchorFlags | _snack.ANCHOR_BOTTOM
128
129 gridFlags = 0
130 if (growx):
131 gridFlags = _snack.GRID_GROWX
132 if (growy):
133 gridFlags = gridFlags | _snack.GRID_GROWY
134
135 if (what.__dict__.has_key('g')):
136 return self.g.setfield(col, row, what.g, padding, anchorFlags,
137 gridFlags)
138 else:
139 return self.g.setfield(col, row, what.w, padding, anchorFlags)
140
141 def __init__(self, *args):
142 self.g = apply(_snack.grid, args)
143 self.gridmembers = []
144
145 class SnackScreen:
146
147 def __init__(self):
148 _snack.init()
149
150 def finish(self):
151 return _snack.finish()
152
153 def openWindow(self, left, top, width, height, title):
154 return _snack.openwindow(left, top, width, height, title)
155
156 def centeredWindow(self, width, height, title):
157 return _snack.centeredwindow(width, height, title)
158
159 def gridWrappedWindow(self, grid, title):
160 return _snack.gridwrappedwindow(grid.g, title)
161
162 def popWindow(self):
163 return _snack.popwindow()
164
165 def refresh(self):
166 return _snack.refresh()
167
168 # returns a tuple of the wrapped text, the actual width, and the actual height
169 def reflow(text, width, flexDown = 5, flexUp = 5):
170 return _snack.reflow(text, width, flexDown, flexUp)
171
172
173 # combo widgets
174
175 class RadioGroup(Widget):
176
177 def __init__(self):
178 self.prev = None
179 self.buttonlist = []
180
181 def add(self, title, value, default = None):
182 if not self.prev and default == None:
183 # If the first element is not explicitly set to
184 # not be the default, make it be the default
185 default = 1
186 b = SingleRadioButton(title, self.prev, default)
187 self.prev = b
188 self.buttonlist.append((b, value))
189 return b
190
191 def getSelection(self):
192 for (b, value) in self.buttonlist:
193 if b.selected(): return value
194 return None
195
196
197 class RadioBar(Grid):
198
199 def __init__(self, screen, buttonlist):
200 self.list = []
201 self.item = 0
202 self.group = RadioGroup()
203 Grid.__init__(self, 1, len(buttonlist))
204 for (title, value, default) in buttonlist:
205 b = self.group.add(title, value, default)
206 self.list.append(b, value)
207 self.setField(b, 0, self.item, anchorLeft = 1)
208 self.item = self.item + 1
209
210 def getSelection(self):
211 return self.group.getSelection()
212
213
214 # you normally want to pack a ButtonBar with growx = 1
215
216 class ButtonBar(Grid):
217
218 def __init__(self, screen, buttonlist):
219 self.list = []
220 self.item = 0
221 Grid.__init__(self, len(buttonlist), 1)
222 for (title, value) in buttonlist:
223 b = Button(title)
224 self.list.append(b, value)
225 self.setField(b, self.item, 0, (1, 0, 1, 0))
226 self.item = self.item + 1
227
228 def buttonPressed(self, widget):
229 """Takes the widget returned by Form.run and looks to see
230 if it was one of the widgets in the ButtonBar."""
231 for (button, value) in self.list:
232 if widget == button:
233 return value
234 return None
235
236
237 class GridForm(Grid):
238
239 def __init__(self, screen, title, *args):
240 self.screen = screen
241 self.title = title
242 self.form = Form()
243 self.childList = []
244 self.form_created = 0
245 args = list(args)
246 args[:0] = [self]
247 apply(Grid.__init__, tuple(args))
248
249 def add(self, widget, col, row, padding = (0, 0, 0, 0),
250 anchorLeft = 0, anchorTop = 0, anchorRight = 0,
251 anchorBottom = 0, growx = 0, growy = 0):
252 self.setField(widget, col, row, padding, anchorLeft,
253 anchorTop, anchorRight, anchorBottom,
254 growx, growy);
255 self.childList.append(widget)
256
257 def run_once(self):
258 result = self.run()
259 self.screen.popWindow()
260 return result
261
262 def run(self):
263 if not self.form_created:
264 self.place(1,1)
265 for child in self.childList:
266 self.form.add(child)
267 self.screen.gridWrappedWindow(self, self.title)
268 self.form_created = 1
269 return self.form.run()
270
271