]>
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.
14 def __init__(self
, text
):
15 self
.w
= _snack
.button(text
)
17 class Checkbox(Widget
):
20 return self
.w
.checkboxValue
23 return self
.w
.checkboxValue
!= 0
25 def __init__(self
, text
, isOn
= 0):
26 self
.w
= _snack
.checkbox(text
, isOn
)
28 class SingleRadioButton(Widget
):
31 return self
.w
.key
== self
.w
.radioValue
;
33 def __init__(self
, text
, group
, isOn
= 0):
35 self
.w
= _snack
.radiobutton(text
, group
.w
, isOn
)
37 self
.w
= _snack
.radiobutton(text
, None, isOn
)
39 class Listbox(Widget
):
41 def append(self
, text
):
42 return self
.w
.listboxAddItem(text
)
45 return self
.w
.listboxGetCurrent()
47 def __init__(self
, height
, scroll
= 0, returnExit
= 0, width
= 0):
48 self
.w
= _snack
.listbox(height
, scroll
, returnExit
)
50 self
.w
.listboxSetWidth(width
)
52 class Textbox(Widget
):
54 def __init__(self
, width
, height
, text
, scroll
= 0):
55 self
.w
= _snack
.textbox(width
, height
, text
, scroll
)
59 def __init__(self
, text
):
60 self
.w
= _snack
.label(text
)
65 return self
.w
.entryValue
68 return self
.w
.entrySetValue(text
)
70 def __init__(self
, width
, text
= "", hidden
= 0, scroll
= 1):
71 self
.w
= _snack
.entry(width
, text
, hidden
, scroll
)
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
}
81 for n
in hotkeys
.keys():
82 hotkeys
[hotkeys
[n
]] = n
86 def addHotKey(self
, keyname
):
87 self
.w
.addhotkey(hotkeys
[keyname
])
89 def add(self
, widget
):
90 if widget
.__dict
__.has_key('gridmembers'):
91 for w
in widget
.gridmembers
:
93 elif widget
.__dict
__.has_key('w'):
94 self
.trans
[widget
.w
.key
] = widget
95 return self
.w
.add(widget
.w
)
99 (what
, which
) = self
.w
.run()
100 if (what
== _snack
.FORM_EXIT_WIDGET
):
101 return self
.trans
[which
]
103 return hotkeys
[which
]
107 self
.w
= _snack
.form()
111 def place(self
, x
, y
):
112 return self
.g
.place(x
, y
)
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
)
120 anchorFlags
= _snack
.ANCHOR_LEFT
122 anchorFlags
= _snack
.ANCHOR_RIGHT
125 anchorFlags
= anchorFlags | _snack
.ANCHOR_TOP
127 anchorFlags
= anchorFlags | _snack
.ANCHOR_BOTTOM
131 gridFlags
= _snack
.GRID_GROWX
133 gridFlags
= gridFlags | _snack
.GRID_GROWY
135 if (what
.__dict
__.has_key('g')):
136 return self
.g
.setfield(col
, row
, what
.g
, padding
, anchorFlags
,
139 return self
.g
.setfield(col
, row
, what
.w
, padding
, anchorFlags
)
141 def __init__(self
, *args
):
142 self
.g
= apply(_snack
.grid
, args
)
143 self
.gridmembers
= []
151 return _snack
.finish()
153 def openWindow(self
, left
, top
, width
, height
, title
):
154 return _snack
.openwindow(left
, top
, width
, height
, title
)
156 def centeredWindow(self
, width
, height
, title
):
157 return _snack
.centeredwindow(width
, height
, title
)
159 def gridWrappedWindow(self
, grid
, title
):
160 return _snack
.gridwrappedwindow(grid
.g
, title
)
163 return _snack
.popwindow()
166 return _snack
.refresh()
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
)
175 class RadioGroup(Widget
):
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
186 b
= SingleRadioButton(title
, self
.prev
, default
)
188 self
.buttonlist
.append((b
, value
))
191 def getSelection(self
):
192 for (b
, value
) in self
.buttonlist
:
193 if b
.selected(): return value
197 class RadioBar(Grid
):
199 def __init__(self
, screen
, buttonlist
):
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
210 def getSelection(self
):
211 return self
.group
.getSelection()
214 # you normally want to pack a ButtonBar with growx = 1
216 class ButtonBar(Grid
):
218 def __init__(self
, screen
, buttonlist
):
221 Grid
.__init
__(self
, len(buttonlist
), 1)
222 for (title
, value
) in buttonlist
:
224 self
.list.append(b
, value
)
225 self
.setField(b
, self
.item
, 0, (1, 0, 1, 0))
226 self
.item
= self
.item
+ 1
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:
237 class GridForm(Grid
):
239 def __init__(self
, screen
, title
, *args
):
244 self
.form_created
= 0
247 apply(Grid
.__init
__, tuple(args
))
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
,
255 self
.childList
.append(widget
)
259 self
.screen
.popWindow()
263 if not self
.form_created
:
265 for child
in self
.childList
:
267 self
.screen
.gridWrappedWindow(self
, self
.title
)
268 self
.form_created
= 1
269 return self
.form
.run()