]>
git.ipfire.org Git - thirdparty/newt.git/blob - snack.py
1 # snack.py: maps C extension module _snack to proper python types in module
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.
13 def setCallback(self
, obj
):
14 self
.w
.setCallback(obj
)
18 def __init__(self
, text
):
19 self
.w
= _snack
.button(text
)
21 class Checkbox(Widget
):
24 return self
.w
.checkboxValue
27 return self
.w
.checkboxValue
!= 0
29 def __init__(self
, text
, isOn
= 0):
30 self
.w
= _snack
.checkbox(text
, isOn
)
32 class SingleRadioButton(Widget
):
35 return self
.w
.key
== self
.w
.radioValue
;
37 def __init__(self
, text
, group
, isOn
= 0):
39 self
.w
= _snack
.radiobutton(text
, group
.w
, isOn
)
41 self
.w
= _snack
.radiobutton(text
, None, isOn
)
43 class Listbox(Widget
):
45 def append(self
, text
, item
):
46 key
= self
.w
.listboxAddItem(text
)
47 self
.key2item
[key
] = item
48 self
.item2key
[item
] = key
50 def insert(self
, text
, item
, before
):
52 key
= self
.w
.listboxInsertItem(text
, 0)
54 key
= self
.w
.listboxInsertItem(text
, self
.item2key
[before
])
55 self
.key2item
[key
] = item
56 self
.item2key
[item
] = key
58 def delete(self
, item
):
59 self
.w
.listboxDeleteItem(self
.item2key
[item
])
60 del self
.key2item
[self
.item2key
[item
]]
61 del self
.item2key
[item
]
63 def replace(self
, text
, item
):
64 key
= self
.w
.listboxInsertItem(text
, self
.item2key
[item
])
65 self
.w
.listboxDeleteItem(self
.item2key
[item
])
66 del self
.key2item
[self
.item2key
[item
]]
67 self
.item2key
[item
] = key
68 self
.key2item
[key
] = item
71 return self
.key2item
[self
.w
.listboxGetCurrent()]
73 def setCurrent(self
, item
):
75 #self.w.listboxSetCurrent(self.item2key[item])
77 def __init__(self
, height
, scroll
= 0, returnExit
= 0, width
= 0):
78 self
.w
= _snack
.listbox(height
, scroll
, returnExit
)
82 self
.w
.listboxSetWidth(width
)
84 class Textbox(Widget
):
86 def __init__(self
, width
, height
, text
, scroll
= 0):
87 self
.w
= _snack
.textbox(width
, height
, text
, scroll
)
89 class TextboxReflowed(Textbox
):
91 def __init__(self
, width
, text
, flexDown
= 5, flexUp
= 10):
92 (newtext
, width
, height
) = reflow(text
, width
, flexDown
, flexUp
)
93 Textbox
.__init
__(self
, width
, height
, newtext
, 0)
97 def __init__(self
, text
):
98 self
.w
= _snack
.label(text
)
103 return self
.w
.entryValue
106 return self
.w
.entrySetValue(text
)
108 def __init__(self
, width
, text
= "", hidden
= 0, scroll
= 1,
110 self
.w
= _snack
.entry(width
, text
, hidden
, scroll
, returnExit
)
114 hotkeys
= { "F1" : _snack
.KEY_F1
, "F2" : _snack
.KEY_F2
, "F3" : _snack
.KEY_F3
,
115 "F4" : _snack
.KEY_F4
, "F5" : _snack
.KEY_F5
, "F6" : _snack
.KEY_F6
,
116 "F7" : _snack
.KEY_F7
, "F8" : _snack
.KEY_F8
, "F9" : _snack
.KEY_F9
,
117 "F10" : _snack
.KEY_F10
, "F11" : _snack
.KEY_F11
,
118 "F12" : _snack
.KEY_F12
}
120 for n
in hotkeys
.keys():
121 hotkeys
[hotkeys
[n
]] = n
125 def addHotKey(self
, keyname
):
126 self
.w
.addhotkey(hotkeys
[keyname
])
128 def add(self
, widget
):
129 if widget
.__dict
__.has_key('hotkeys'):
130 for key
in widget
.hotkeys
.keys():
133 if widget
.__dict
__.has_key('gridmembers'):
134 for w
in widget
.gridmembers
:
136 elif widget
.__dict
__.has_key('w'):
137 self
.trans
[widget
.w
.key
] = widget
138 return self
.w
.add(widget
.w
)
142 (what
, which
) = self
.w
.run()
143 if (what
== _snack
.FORM_EXIT_WIDGET
):
144 return self
.trans
[which
]
146 return hotkeys
[which
]
150 self
.w
= _snack
.form()
154 def place(self
, x
, y
):
155 return self
.g
.place(x
, y
)
157 def setField(self
, what
, col
, row
, padding
= (0, 0, 0, 0),
158 anchorLeft
= 0, anchorTop
= 0, anchorRight
= 0,
159 anchorBottom
= 0, growx
= 0, growy
= 0):
160 self
.gridmembers
.append(what
)
163 anchorFlags
= _snack
.ANCHOR_LEFT
165 anchorFlags
= _snack
.ANCHOR_RIGHT
168 anchorFlags
= anchorFlags | _snack
.ANCHOR_TOP
170 anchorFlags
= anchorFlags | _snack
.ANCHOR_BOTTOM
174 gridFlags
= _snack
.GRID_GROWX
176 gridFlags
= gridFlags | _snack
.GRID_GROWY
178 if (what
.__dict
__.has_key('g')):
179 return self
.g
.setfield(col
, row
, what
.g
, padding
, anchorFlags
,
182 return self
.g
.setfield(col
, row
, what
.w
, padding
, anchorFlags
)
184 def __init__(self
, *args
):
185 self
.g
= apply(_snack
.grid
, args
)
186 self
.gridmembers
= []
192 (self
.width
, self
.height
) = _snack
.size()
193 self
.pushHelpLine(None)
196 return _snack
.finish()
198 def openWindow(self
, left
, top
, width
, height
, title
):
199 return _snack
.openwindow(left
, top
, width
, height
, title
)
201 def pushHelpLine(self
, text
):
203 return _snack
.pushhelpline("*default*")
205 return _snack
.pushhelpline(text
)
207 def popHelpLine(self
):
208 return _snack
.pophelpline()
210 def drawRootText(self
, left
, top
, text
):
211 return _snack
.drawroottext(left
, top
, text
)
213 def centeredWindow(self
, width
, height
, title
):
214 return _snack
.centeredwindow(width
, height
, title
)
216 def gridWrappedWindow(self
, grid
, title
):
217 return _snack
.gridwrappedwindow(grid
.g
, title
)
220 return _snack
.popwindow()
223 return _snack
.refresh()
225 # returns a tuple of the wrapped text, the actual width, and the actual height
226 def reflow(text
, width
, flexDown
= 5, flexUp
= 5):
227 return _snack
.reflow(text
, width
, flexDown
, flexUp
)
231 class RadioGroup(Widget
):
237 def add(self
, title
, value
, default
= None):
238 if not self
.prev
and default
== None:
239 # If the first element is not explicitly set to
240 # not be the default, make it be the default
242 b
= SingleRadioButton(title
, self
.prev
, default
)
244 self
.buttonlist
.append((b
, value
))
247 def getSelection(self
):
248 for (b
, value
) in self
.buttonlist
:
249 if b
.selected(): return value
253 class RadioBar(Grid
):
255 def __init__(self
, screen
, buttonlist
):
258 self
.group
= RadioGroup()
259 Grid
.__init
__(self
, 1, len(buttonlist
))
260 for (title
, value
, default
) in buttonlist
:
261 b
= self
.group
.add(title
, value
, default
)
262 self
.list.append(b
, value
)
263 self
.setField(b
, 0, self
.item
, anchorLeft
= 1)
264 self
.item
= self
.item
+ 1
266 def getSelection(self
):
267 return self
.group
.getSelection()
270 # you normally want to pack a ButtonBar with growx = 1
272 class ButtonBar(Grid
):
274 def __init__(self
, screen
, buttonlist
):
278 Grid
.__init
__(self
, len(buttonlist
), 1)
279 for blist
in buttonlist
:
280 if (type(blist
) == types
.StringType
):
282 value
= string
.lower(blist
)
283 elif len(blist
) == 2:
284 (title
, value
) = blist
286 (title
, value
, hotkey
) = blist
287 self
.hotkeys
[hotkey
] = value
290 self
.list.append(b
, value
)
291 self
.setField(b
, self
.item
, 0, (1, 0, 1, 0))
292 self
.item
= self
.item
+ 1
294 def buttonPressed(self
, result
):
295 """Takes the widget returned by Form.run and looks to see
296 if it was one of the widgets in the ButtonBar."""
298 if self
.hotkeys
.has_key(result
):
299 return self
.hotkeys
[result
]
301 for (button
, value
) in self
.list:
307 class GridForm(Grid
):
309 def __init__(self
, screen
, title
, *args
):
314 self
.form_created
= 0
317 apply(Grid
.__init
__, tuple(args
))
319 def add(self
, widget
, col
, row
, padding
= (0, 0, 0, 0),
320 anchorLeft
= 0, anchorTop
= 0, anchorRight
= 0,
321 anchorBottom
= 0, growx
= 0, growy
= 0):
322 self
.setField(widget
, col
, row
, padding
, anchorLeft
,
323 anchorTop
, anchorRight
, anchorBottom
,
325 self
.childList
.append(widget
)
329 self
.screen
.popWindow()
333 if not self
.form_created
:
335 for child
in self
.childList
:
337 self
.screen
.gridWrappedWindow(self
, self
.title
)
338 self
.form_created
= 1
339 return self
.form
.run()
342 if not self
.form_created
:
344 for child
in self
.childList
:
346 self
.form_created
= 1
347 self
.screen
.gridWrappedWindow(self
, self
.title
)
348 result
= self
.form
.run()
349 self
.screen
.popWindow()
352 def ListboxChoiceWindow(screen
, title
, text
, items
,
353 buttons
= ('Ok', 'Cancel'),
354 width
= 40, scroll
= 0, height
= -1):
355 if (height
== -1): height
= len(items
)
357 bb
= ButtonBar(screen
, buttons
)
358 t
= TextboxReflowed(width
, text
)
359 l
= Listbox(height
, scroll
= scroll
, returnExit
= 1)
362 if (type(item
) == types
.TupleType
):
371 g
= GridForm(screen
, title
, 1, 3)
373 g
.add(l
, 0, 1, padding
= (0, 1, 0, 1))
374 g
.add(bb
, 0, 2, growx
= 1)
378 return (bb
.buttonPressed(rc
), l
.current())
380 def ButtonChoiceWindow(screen
, title
, text
,
381 buttons
= [ 'Ok', 'Cancel' ],
383 bb
= ButtonBar(screen
, buttons
)
384 t
= TextboxReflowed(width
, text
)
386 g
= GridForm(screen
, title
, 1, 2)
387 g
.add(t
, 0, 0, padding
= (0, 0, 0, 1))
388 g
.add(bb
, 0, 1, growx
= 1)
389 return bb
.buttonPressed(g
.runOnce())
391 def EntryWindow(screen
, title
, text
, prompts
, allowCancel
= 1, width
= 40,
393 bb
= ButtonBar(screen
, [ 'Ok', 'Cancel' ]);
394 t
= TextboxReflowed(width
, text
)
405 if (type(n
) == types
.TupleType
):
408 e
= Entry(entryWidth
)
410 sg
.setField(Label(n
), 0, count
, padding
= (0, 0, 1, 0), anchorLeft
= 1)
411 sg
.setField(e
, 1, count
, anchorLeft
= 1)
415 g
= GridForm(screen
, title
, 1, 3)
417 g
.add(t
, 0, 0, padding
= (0, 0, 0, 1))
418 g
.add(sg
, 0, 1, padding
= (0, 0, 0, 1))
419 g
.add(bb
, 0, 2, growx
= 1)
426 entryValues
.append(entryList
[count
].value())
429 return (bb
.buttonPressed(result
), tuple(entryValues
))