]> git.ipfire.org Git - thirdparty/newt.git/blob - snack.py
0.52.24
[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 """
8 This module provides the NEWT Windowing toolkit API for Python
9 This is a lightweight text-mode windowing library, based on slang.
10
11 Classes:
12
13 - Widget
14 - Button
15 - CompactButton
16 - Checkbox
17 - SingleRadioButton
18 - Listbox
19 - Textbox
20 - TextboxReflowed
21 - Label
22 - Scale
23 - Entry
24 - Form
25 - Grid
26 - SnackScreen
27 - RadioGroup
28 - RadioBar
29 - ButtonBar
30 - GridFormHelp
31 - GridForm
32 - CheckboxTree
33 - Clistbox
34
35 Functions:
36
37 - ListboxChoiceWindow
38 - ButtonChoiceWindow
39 - EntryWindow
40 """
41
42
43 from __future__ import absolute_import, print_function, unicode_literals
44 import _snack
45 import string
46 import sys
47
48 from _snack import FLAG_DISABLED, FLAGS_SET, FLAGS_RESET, FLAGS_TOGGLE, FD_READ, FD_WRITE, FD_EXCEPT
49
50 LEFT = (-1, 0)
51 DOWN = (-1, -1)
52 CENTER = (0, 0)
53 UP = (1, 1)
54 RIGHT = (1, 0)
55
56 snackArgs = {"append":-1}
57
58 class Widget:
59 """Base class for NEWT toolkit - Do not use directly
60
61 methods:
62
63 - Widget(self)
64 - setCallback(self, obj, data = None) :
65 The callback for when object activated.
66 data is passed to obj.
67 """
68 def setCallback(self, obj, data = None):
69 if data:
70 self.w.setCallback(obj, data)
71 else:
72 self.w.setCallback(obj)
73
74 def __init__(self):
75 raise NotImplementedError
76
77 class Button(Widget):
78 """Basic button class, takes button text as parameter
79
80 method:
81
82 - Button(self, text): returns a button
83 """
84 def __init__(self, text):
85 self.w = _snack.button(text)
86
87 class CompactButton(Widget):
88 """Compact Button class (less frilly button decoration).
89
90 methods:
91
92 - CompactButton(self,text) : create button, with text.
93 """
94 def __init__(self, text):
95 self.w = _snack.compactbutton(text)
96
97 class Checkbox(Widget):
98 """A checkbox.
99
100 methods:
101
102 - Checkbox(self, text, isOn = 0) : text, and boolean as to default value
103 - setValue(self) : set value
104 - value(self, value) : return checkbox value
105 - selected(self) : returns boolean
106 - setFlags(self, flag, sense) : set flags
107
108 flags: FLAG_DISABLED, FLAGS_SET, FLAGS_RESET
109 """
110 def value(self):
111 return self.w.checkboxValue
112
113 def selected(self):
114 return self.w.checkboxValue != 0
115
116 def setFlags (self, flag, sense):
117
118 return self.w.checkboxSetFlags(flag, sense)
119
120 def setValue (self, value):
121 return self.w.checkboxSetValue(value)
122
123 def __init__(self, text, isOn = 0):
124 self.w = _snack.checkbox(text, isOn)
125
126 class SingleRadioButton(Widget):
127 """Single Radio Button.
128
129 methods:
130
131 - SingleRadioButton(text, group, isOn = 0) : create button
132 - selected(self) : returns bool, whether or not is selected.
133 """
134
135 def selected(self):
136 return self.w.key == self.w.radioValue;
137
138 def __init__(self, text, group, isOn = 0):
139 if group:
140 self.w = _snack.radiobutton(text, group.w, isOn)
141 else:
142 self.w = _snack.radiobutton(text, None, isOn)
143
144 class Listbox(Widget):
145 """Listbox class.
146
147 methods:
148
149 - Listbox(self, height, scroll = 0, returnExit = 0, width = 0, showCursor = 0, multiple = 0, border = 0)
150 - insert(self, text, item, before) : insert element; before = key to item to insert before, or None.
151 - delete(self, item) : delete item from list.
152 - replace(self, text,item) : Replace a given item's text
153 - current(self) : returns currently selected item
154 - getSelection(self) : returns a list of selected items
155 - setCurrent(self,i tem) : select current.
156 - clear(self) : clear listbox
157 """
158
159 def append(self, text, item):
160 key = self.w.listboxAddItem(text)
161 self.key2item[key] = item
162 self.item2key[item] = key
163
164 def insert(self, text, item, before):
165 if (not before):
166 key = self.w.listboxInsertItem(text, 0)
167 else:
168 key = self.w.listboxInsertItem(text, self.item2key[before])
169 self.key2item[key] = item
170 self.item2key[item] = key
171
172 def delete(self, item):
173 self.w.listboxDeleteItem(self.item2key[item])
174 del self.key2item[self.item2key[item]]
175 del self.item2key[item]
176
177 def replace(self, text, item):
178 key = self.w.listboxInsertItem(text, self.item2key[item])
179 self.w.listboxDeleteItem(self.item2key[item])
180 del self.key2item[self.item2key[item]]
181 self.item2key[item] = key
182 self.key2item[key] = item
183
184 def current(self):
185 return self.key2item[self.w.listboxGetCurrent()]
186
187 def getSelection(self):
188 selection = []
189 list = self.w.listboxGetSelection()
190 for key in list:
191 selection.append(self.key2item[key])
192 return selection
193
194 def setCurrent(self, item):
195 self.w.listboxSetCurrent(self.item2key[item])
196
197 def clear(self):
198 self.key2item = {}
199 self.item2key = {}
200 self.w.listboxClear()
201
202 def __init__(self, height, scroll = 0, returnExit = 0, width = 0, showCursor = 0, multiple = 0, border = 0):
203 self.w = _snack.listbox(height, scroll, returnExit, showCursor, multiple, border)
204 self.key2item = {}
205 self.item2key = {}
206 if (width):
207 self.w.listboxSetWidth(width)
208
209 class Textbox(Widget):
210 """Textbox, container for text.
211
212 methods:
213
214 - Textbox(self, width, height, scroll = 0, wrap = 0): scroll, wrap are flags
215 include scroll bars, or text wrap.
216 - setText(text) : set text.
217 - setHeight(height): set height.
218 """
219
220 def setText(self, text):
221 self.w.textboxText(text)
222
223 def setHeight(self, height):
224 self.w.textboxHeight(height)
225
226 def __init__(self, width, height, text, scroll = 0, wrap = 0):
227 self.w = _snack.textbox(width, height, text, scroll, wrap)
228
229 class TextboxReflowed(Textbox):
230
231 def __init__(self, width, text, flexDown = 5, flexUp = 10, maxHeight = -1):
232 (newtext, width, height) = reflow(text, width, flexDown, flexUp)
233 if maxHeight != -1 and height > maxHeight:
234 Textbox.__init__(self, width, maxHeight, newtext, 1)
235 else:
236 Textbox.__init__(self, width, height, newtext, 0)
237
238 class Label(Widget):
239 """A Label (simple text).
240
241 methods:
242
243 - Label(self,text) : create label
244 - setText(self,text) : change text.
245 - setColors(self, colorset) : change individual colors
246 """
247 def setText(self, text):
248 self.w.labelText(text)
249
250 def __init__(self, text):
251 self.w = _snack.label(text)
252
253 def setColors(self, colorset):
254 self.w.labelSetColors(colorset)
255
256 class Scale(Widget):
257 """A Scale (progress bar).
258
259 methods:
260
261 - Scale(self,width, total) : create scale; width: size on screen, fullamount: integer.
262 - set(self,amount) : set amount to integer.
263 """
264 def set(self, amount):
265 self.w.scaleSet(amount)
266
267 def __init__(self, width, total):
268 self.w = _snack.scale(width, total)
269
270 class Entry(Widget):
271 """Entry widget.
272
273 methods:
274
275 - Entry(self, width, text = "", hidden = 0, password = 0, scroll = 1, returnExit = 0)
276 constructor. hidden doesn't show text, password stars it out,
277 scroll includes scroll bars;
278 if returnExit is set, return from Form when exiting this element, else
279 proceed to next entry widget.
280 - value(self): return value.
281 - set(text, cursorAtEnd = 1) : set the text
282 - setFlags (flag, sense) : flags can be FLAG_DISABLED, FLAGS_SET, FLAGS_RESET, FLAGS_TOGGLE
283 """
284 def value(self):
285 return self.w.entryValue
286
287 def set(self, text, cursorAtEnd = 1):
288 return self.w.entrySetValue(text, cursorAtEnd)
289
290 def setFlags (self, flag, sense):
291 return self.w.entrySetFlags(flag, sense)
292
293 def __init__(self, width, text = "", hidden = 0, password = 0, scroll = 1,
294 returnExit = 0):
295 self.w = _snack.entry(width, text, hidden, password, scroll, returnExit)
296
297
298 # Form uses hotkeys
299 hotkeys = { "F1" : _snack.KEY_F1, "F2" : _snack.KEY_F2, "F3" : _snack.KEY_F3,
300 "F4" : _snack.KEY_F4, "F5" : _snack.KEY_F5, "F6" : _snack.KEY_F6,
301 "F7" : _snack.KEY_F7, "F8" : _snack.KEY_F8, "F9" : _snack.KEY_F9,
302 "F10" : _snack.KEY_F10, "F11" : _snack.KEY_F11,
303 "F12" : _snack.KEY_F12, "ESC" : _snack.KEY_ESC,
304 "ENTER": _snack.KEY_ENTER, "SUSPEND" : _snack.KEY_SUSPEND,
305 "BACKSPACE": _snack.KEY_BACKSPACE, "DELETE": _snack.KEY_DELETE,
306 "INSERT": _snack.KEY_INSERT, "RESIZE": _snack.KEY_RESIZE,
307 " " : ord(" ") }
308
309 for n in list(hotkeys.keys()):
310 hotkeys[hotkeys[n]] = n
311 for o,c in [ (ord(c),c) for c in string.ascii_letters+string.digits ]:
312 hotkeys[c] = o
313 hotkeys[o] = c
314
315 class Form:
316 """ Base Form class, from which Grid, etc. inherit
317
318 methods:
319
320 - Form(self, helpArg = None) : constructor.
321 - addHotKey(self, keyname) : keynames of form "F1" through "F12", "ESC"
322 - add(self, widget) : Add a widget
323 - run(self): run a form, expecting input
324 - draw(self): draw form.
325 - setTimer(self, timer) : add a timer
326 - watchFile(self, file, flags) : watch a named file
327 - setCurrent (self, co): Set a given widget as the current focus
328 """
329 def addHotKey(self, keyname):
330 self.w.addhotkey(hotkeys[keyname])
331
332 def add(self, widget):
333 if 'hotkeys' in widget.__dict__:
334 for key in widget.hotkeys.keys():
335 self.addHotKey(key)
336
337 if 'gridmembers' in widget.__dict__:
338 for w in widget.gridmembers:
339 self.add(w)
340 elif 'w' in widget.__dict__:
341 self.trans[widget.w.key] = widget
342 return self.w.add(widget.w)
343 return None
344
345 def run(self):
346 (what, which) = self.w.run()
347 if (what == _snack.FORM_EXIT_WIDGET):
348 return self.trans[which]
349 elif (what == _snack.FORM_EXIT_TIMER):
350 return "TIMER"
351 elif (what == _snack.FORM_EXIT_FDREADY):
352 return self.filemap[which]
353 elif (what == _snack.FORM_EXIT_HOTKEY):
354 return hotkeys[which]
355 raise RuntimeError("EOF or IO error")
356
357 def draw(self):
358 self.w.draw()
359 return None
360
361 def __init__(self, helpArg = None):
362 self.trans = {}
363 self.filemap = {}
364 self.w = _snack.form(helpArg)
365 # we do the reference count for the helpArg in python! gross
366 self.helpArg = helpArg
367
368 def setCurrent (self, co):
369 self.w.setcurrent (co.w)
370
371 def setTimer (self, timer):
372 self.w.settimer (timer)
373
374 def watchFile (self, file, flags):
375 self.filemap[file.fileno()] = file
376 self.w.watchfd (file.fileno(), flags)
377
378 class Grid:
379 """Grid class.
380
381 methods:
382
383 - place(self,x,y): Return what is placed at (x,y)
384 - setField(self, what, col, row, padding = (0, 0, 0, 0),
385 anchorLeft = 0, anchorTop = 0, anchorRight = 0,
386 anchorBottom = 0, growx = 0, growy = 0):
387 used to add widget 'what' to grid.
388 - Grid(self, *args): eg. g = Grid(2,3) for 2x3 grid
389 """
390 def place(self, x, y):
391 return self.g.place(x, y)
392
393 def setField(self, what, col, row, padding = (0, 0, 0, 0),
394 anchorLeft = 0, anchorTop = 0, anchorRight = 0,
395 anchorBottom = 0, growx = 0, growy = 0):
396 self.gridmembers.append(what)
397 anchorFlags = 0
398 if (anchorLeft):
399 anchorFlags = _snack.ANCHOR_LEFT
400 elif (anchorRight):
401 anchorFlags = _snack.ANCHOR_RIGHT
402
403 if (anchorTop):
404 anchorFlags = anchorFlags | _snack.ANCHOR_TOP
405 elif (anchorBottom):
406 anchorFlags = anchorFlags | _snack.ANCHOR_BOTTOM
407
408 gridFlags = 0
409 if (growx):
410 gridFlags = _snack.GRID_GROWX
411 if (growy):
412 gridFlags = gridFlags | _snack.GRID_GROWY
413
414 if 'g' in what.__dict__:
415 return self.g.setfield(col, row, what.g, padding, anchorFlags,
416 gridFlags)
417 else:
418 return self.g.setfield(col, row, what.w, padding, anchorFlags)
419
420 def __init__(self, *args):
421 self.g = _snack.grid(*args)
422 self.gridmembers = []
423
424 colorsets = { "ROOT" : _snack.COLORSET_ROOT,
425 "BORDER" : _snack.COLORSET_BORDER,
426 "WINDOW" : _snack.COLORSET_WINDOW,
427 "SHADOW" : _snack.COLORSET_SHADOW,
428 "TITLE" : _snack.COLORSET_TITLE,
429 "BUTTON" : _snack.COLORSET_BUTTON,
430 "ACTBUTTON" : _snack.COLORSET_ACTBUTTON,
431 "CHECKBOX" : _snack.COLORSET_CHECKBOX,
432 "ACTCHECKBOX" : _snack.COLORSET_ACTCHECKBOX,
433 "ENTRY" : _snack.COLORSET_ENTRY,
434 "LABEL" : _snack.COLORSET_LABEL,
435 "LISTBOX" : _snack.COLORSET_LISTBOX,
436 "ACTLISTBOX" : _snack.COLORSET_ACTLISTBOX,
437 "TEXTBOX" : _snack.COLORSET_TEXTBOX,
438 "ACTTEXTBOX" : _snack.COLORSET_ACTTEXTBOX,
439 "HELPLINE" : _snack.COLORSET_HELPLINE,
440 "ROOTTEXT" : _snack.COLORSET_ROOTTEXT,
441 "EMPTYSCALE" : _snack.COLORSET_EMPTYSCALE,
442 "FULLSCALE" : _snack.COLORSET_FULLSCALE,
443 "DISENTRY" : _snack.COLORSET_DISENTRY,
444 "COMPACTBUTTON" : _snack.COLORSET_COMPACTBUTTON,
445 "ACTSELLISTBOX" : _snack.COLORSET_ACTSELLISTBOX,
446 "SELLISTBOX" : _snack.COLORSET_SELLISTBOX }
447
448 class SnackScreen:
449 """A Screen;
450
451 methods:
452
453 - Screen(self) : constructor
454 - finish(self)
455 - resume(self)
456 - suspend(self)
457 - doHelpCallback(self,arg) call callback with arg
458 - helpCallback(self,cb): Set help callback
459 - suspendcallback(self,cb, data=None) : set callback. data=data to pass to cb.
460 - openWindow(self,left, top, width, height, title): Open a window.
461 - pushHelpLine(self,text): put help line on screen. Returns current help line if text=None
462 - setColor(self, colorset, fg, bg): Set foreground and background colors;
463 colorset = key from snack.colorsets,
464 fg & bg = english color names defined by S-Lang
465 (ref: S-Lang Library C Programmer's Guide section:
466 8.4.4. Setting Character Attributes)
467 """
468 def __init__(self):
469 _snack.init()
470 (self.width, self.height) = _snack.size()
471 self.pushHelpLine(None)
472
473 def finish(self):
474 return _snack.finish()
475
476 def resume(self):
477 _snack.resume()
478
479 def suspend(self):
480 _snack.suspend()
481
482 def doHelpCallback(self, arg):
483 self.helpCb(self, arg)
484
485 def helpCallback(self, cb):
486 self.helpCb = cb
487 return _snack.helpcallback(self.doHelpCallback)
488
489 def suspendCallback(self, cb, data = None):
490 if data:
491 return _snack.suspendcallback(cb, data)
492 return _snack.suspendcallback(cb)
493
494 def openWindow(self, left, top, width, height, title):
495 return _snack.openwindow(left, top, width, height, title)
496
497 def pushHelpLine(self, text):
498 if (not text):
499 return _snack.pushhelpline("*default*")
500 else:
501 return _snack.pushhelpline(text)
502
503 def popHelpLine(self):
504 return _snack.pophelpline()
505
506 def drawRootText(self, left, top, text):
507 return _snack.drawroottext(left, top, text)
508
509 def centeredWindow(self, width, height, title):
510 return _snack.centeredwindow(width, height, title)
511
512 def gridWrappedWindow(self, grid, title, x = None, y = None):
513 if x and y:
514 return _snack.gridwrappedwindow(grid.g, title, x, y)
515
516 return _snack.gridwrappedwindow(grid.g, title)
517
518 def popWindow(self, refresh = True):
519 if refresh:
520 return _snack.popwindow()
521 return _snack.popwindownorefresh()
522
523 def refresh(self):
524 return _snack.refresh()
525
526 def setColor(self, colorset, fg, bg):
527 if colorset in colorsets:
528 return _snack.setcolor(colorsets[colorset], fg, bg)
529 else:
530 # assume colorset is an integer for the custom color set
531 return _snack.setcolor(colorset, fg, bg)
532
533 def reflow(text, width, flexDown = 5, flexUp = 5):
534 """ returns a tuple of the wrapped text, the actual width, and the actual height
535 """
536 return _snack.reflow(text, width, flexDown, flexUp)
537
538 # combo widgets
539
540 class RadioGroup(Widget):
541 """ Combo widget: Group of Radio buttons
542
543 methods:
544
545 - RadioGroup(self): constructor.
546 - add(self,title, value, default = None): add a button. Returns button.
547 - getSelection(self) : returns value of selected button | None
548 """
549 def __init__(self):
550 self.prev = None
551 self.buttonlist = []
552
553 def add(self, title, value, default = None):
554 if not self.prev and default == None:
555 # If the first element is not explicitly set to
556 # not be the default, make it be the default
557 default = 1
558 b = SingleRadioButton(title, self.prev, default)
559 self.prev = b
560 self.buttonlist.append((b, value))
561 return b
562
563 def getSelection(self):
564 for (b, value) in self.buttonlist:
565 if b.selected(): return value
566 return None
567
568
569 class RadioBar(Grid):
570 """ Bar of Radio buttons, based on Grid.
571
572 methods:
573
574 - RadioBar(self, screen, buttonlist) : constructor.
575 - getSelection(self): return value of selected button
576 """
577
578 def __init__(self, screen, buttonlist):
579 self.list = []
580 self.item = 0
581 self.group = RadioGroup()
582 Grid.__init__(self, 1, len(buttonlist))
583 for (title, value, default) in buttonlist:
584 b = self.group.add(title, value, default)
585 self.list.append((b, value))
586 self.setField(b, 0, self.item, anchorLeft = 1)
587 self.item = self.item + 1
588
589 def getSelection(self):
590 return self.group.getSelection()
591
592
593 # you normally want to pack a ButtonBar with growx = 1
594
595 class ButtonBar(Grid):
596 """ Bar of buttons, based on grid.
597
598 methods:
599
600 - ButtonBar(screen, buttonlist,buttonlist, compact = 0):
601 - buttonPressed(self, result): Takes the widget returned by Form.run and looks to see
602 if it was one of the widgets in the ButtonBar.
603 """
604 def __init__(self, screen, buttonlist, compact = 0):
605 self.list = []
606 self.hotkeys = {}
607 self.item = 0
608 Grid.__init__(self, len(buttonlist), 1)
609 for blist in buttonlist:
610 if isinstance(blist, str if sys.version >= '3' else basestring):
611 title = blist
612 value = blist.lower()
613 elif len(blist) == 2:
614 (title, value) = blist
615 else:
616 (title, value, hotkey) = blist
617 self.hotkeys[hotkey] = value
618
619 if compact:
620 b = CompactButton(title)
621 else:
622 b = Button(title)
623 self.list.append((b, value))
624 self.setField(b, self.item, 0, (1, 0, 1, 0))
625 self.item = self.item + 1
626
627 def buttonPressed(self, result):
628 if result in self.hotkeys:
629 return self.hotkeys[result]
630
631 for (button, value) in self.list:
632 if result == button:
633 return value
634 return None
635
636
637 class GridFormHelp(Grid):
638 """ Subclass of Grid, for the help form text.
639
640 methods:
641
642 - GridFormHelp(self, screen, title, help, *args) :
643 - add (self, widget, col, row, padding = (0, 0, 0, 0),
644 anchorLeft = 0, anchorTop = 0, anchorRight = 0,
645 anchorBottom = 0, growx = 0, growy = 0):
646 - runOnce(self, x = None, y = None): pop up the help window
647 - addHotKey(self, keyname):
648 - setTimer(self, keyname):
649 - create(self, x = None, y = None):
650 - run(self, x = None, y = None):
651 - draw(self):
652 - runPopup(self):
653 - setCurrent (self, co):
654 """
655 def __init__(self, screen, title, help, *args):
656 self.screen = screen
657 self.title = title
658 self.form = Form(help)
659 self.childList = []
660 self.form_created = 0
661 args = list(args)
662 args[:0] = [self]
663 Grid.__init__(*tuple(args))
664
665 def add(self, widget, col, row, padding = (0, 0, 0, 0),
666 anchorLeft = 0, anchorTop = 0, anchorRight = 0,
667 anchorBottom = 0, growx = 0, growy = 0):
668 self.setField(widget, col, row, padding, anchorLeft,
669 anchorTop, anchorRight, anchorBottom,
670 growx, growy);
671 self.childList.append(widget)
672
673 def runOnce(self, x = None, y = None):
674 result = self.run(x, y)
675 self.screen.popWindow()
676 return result
677
678 def addHotKey(self, keyname):
679 self.form.addHotKey(keyname)
680
681 def setTimer(self, keyname):
682 self.form.setTimer(keyname)
683
684 def create(self, x = None, y = None):
685 if not self.form_created:
686 self.place(1,1)
687 for child in self.childList:
688 self.form.add(child)
689 self.screen.gridWrappedWindow(self, self.title, x, y)
690 self.form_created = 1
691
692 def run(self, x = None, y = None):
693 self.create(x, y)
694 return self.form.run()
695
696 def draw(self):
697 self.create()
698 return self.form.draw()
699
700 def runPopup(self):
701 self.create()
702 self.screen.gridWrappedWindow(self, self.title)
703 result = self.form.run()
704 self.screen.popWindow()
705 return result
706
707 def setCurrent (self, co):
708 self.form.setCurrent (co)
709
710 class GridForm(GridFormHelp):
711 """ GridForm class (extends GridFormHelp):
712
713 methods:
714
715 - GridForm(self, screen, title, *args):
716 """
717 def __init__(self, screen, title, *args):
718 myargs = (self, screen, title, None) + args
719 GridFormHelp.__init__(*myargs)
720
721 class CheckboxTree(Widget):
722 """ CheckboxTree combo widget,
723
724 methods:
725
726 - CheckboxTree(self, height, scroll = 0, width = None, hide_checkbox = 0, unselectable = 0)
727 constructor.
728 - append(self, text, item = None, selected = 0):
729 - addItem(self, text, path, item = None, selected = 0):
730 - getCurrent(self):
731 - getSelection(self):
732 - setEntry(self, item, text):
733 - setCurrent(self, item):
734 - setEntryValue(self, item, selected = 1):
735 - getEntryValue(self, item):
736 """
737 def append(self, text, item = None, selected = 0):
738 self.addItem(text, (snackArgs['append'], ), item, selected)
739
740 def addItem(self, text, path, item = None, selected = 0):
741 if item is None:
742 item = text
743 key = self.w.checkboxtreeAddItem(text, path, selected)
744 self.key2item[key] = item
745 self.item2key[item] = key
746
747 def getCurrent(self):
748 curr = self.w.checkboxtreeGetCurrent()
749 return self.key2item[curr]
750
751 def __init__(self, height, scroll = 0, width = None, hide_checkbox = 0, unselectable = 0):
752 self.w = _snack.checkboxtree(height, scroll, hide_checkbox, unselectable)
753 self.key2item = {}
754 self.item2key = {}
755 if (width):
756 self.w.checkboxtreeSetWidth(width)
757
758 def getSelection(self):
759 selection = []
760 list = self.w.checkboxtreeGetSelection()
761 for key in list:
762 selection.append(self.key2item[key])
763 return selection
764
765 def setEntry(self, item, text):
766 self.w.checkboxtreeSetEntry(self.item2key[item], text)
767
768 def setCurrent(self, item):
769 self.w.checkboxtreeSetCurrent(self.item2key[item])
770
771 def setEntryValue(self, item, selected = 1):
772 self.w.checkboxtreeSetEntryValue(self.item2key[item], selected)
773
774 def getEntryValue(self, item):
775 return self.w.checkboxtreeGetEntryValue(self.item2key[item])
776
777 def ListboxChoiceWindow(screen, title, text, items,
778 buttons = ('Ok', 'Cancel'),
779 width = 40, scroll = 0, height = -1, default = None,
780 help = None):
781 """
782 - ListboxChoiceWindow(screen, title, text, items,
783 buttons = ('Ok', 'Cancel'),
784 width = 40, scroll = 0, height = -1, default = None,
785 help = None):
786 """
787 if (height == -1): height = len(items)
788
789 bb = ButtonBar(screen, buttons)
790 t = TextboxReflowed(width, text)
791 l = Listbox(height, scroll = scroll, returnExit = 1)
792 count = 0
793 for item in items:
794 if type(item) == tuple:
795 (text, key) = item
796 else:
797 text = item
798 key = count
799
800 if (default == count):
801 default = key
802 elif (default == item):
803 default = key
804
805 l.append(text, key)
806 count = count + 1
807
808 if (default != None):
809 l.setCurrent (default)
810
811 g = GridFormHelp(screen, title, help, 1, 3)
812 g.add(t, 0, 0)
813 g.add(l, 0, 1, padding = (0, 1, 0, 1))
814 g.add(bb, 0, 2, growx = 1)
815
816 rc = g.runOnce()
817
818 return (bb.buttonPressed(rc), l.current())
819
820 def ButtonChoiceWindow(screen, title, text,
821 buttons = [ 'Ok', 'Cancel' ],
822 width = 40, x = None, y = None, help = None):
823 """
824 - ButtonChoiceWindow(screen, title, text,
825 buttons = [ 'Ok', 'Cancel' ],
826 width = 40, x = None, y = None, help = None):
827 """
828 bb = ButtonBar(screen, buttons)
829 t = TextboxReflowed(width, text, maxHeight = screen.height - 12)
830
831 g = GridFormHelp(screen, title, help, 1, 2)
832 g.add(t, 0, 0, padding = (0, 0, 0, 1))
833 g.add(bb, 0, 1, growx = 1)
834 return bb.buttonPressed(g.runOnce(x, y))
835
836 def EntryWindow(screen, title, text, prompts, allowCancel = 1, width = 40,
837 entryWidth = 20, buttons = [ 'Ok', 'Cancel' ], help = None):
838 """
839 EntryWindow(screen, title, text, prompts, allowCancel = 1, width = 40,
840 entryWidth = 20, buttons = [ 'Ok', 'Cancel' ], help = None):
841 """
842 bb = ButtonBar(screen, buttons);
843 t = TextboxReflowed(width, text)
844
845 count = 0
846 for n in prompts:
847 count = count + 1
848
849 sg = Grid(2, count)
850
851 count = 0
852 entryList = []
853 for n in prompts:
854 if type(n) == tuple:
855 (n, e) = n
856 if isinstance(e, str if sys.version >= '3' else basestring):
857 e = Entry(entryWidth, e)
858 else:
859 e = Entry(entryWidth)
860
861 sg.setField(Label(n), 0, count, padding = (0, 0, 1, 0), anchorLeft = 1)
862 sg.setField(e, 1, count, anchorLeft = 1)
863 count = count + 1
864 entryList.append(e)
865
866 g = GridFormHelp(screen, title, help, 1, 3)
867
868 g.add(t, 0, 0, padding = (0, 0, 0, 1))
869 g.add(sg, 0, 1, padding = (0, 0, 0, 1))
870 g.add(bb, 0, 2, growx = 1)
871
872 result = g.runOnce()
873
874 entryValues = []
875 count = 0
876 for n in prompts:
877 entryValues.append(entryList[count].value())
878 count = count + 1
879
880 return (bb.buttonPressed(result), tuple(entryValues))
881
882 class CListbox(Grid):
883 """Clistbox convenience class.
884
885 methods:
886
887 - Clistbox(self, height, cols, cols_widths, scroll = 0) : constructor
888 - colFormText(self, col_text, align = None, adjust_width = 0) : column text.
889 - append(self, col_text, item, col_text_align = None) :
890 - insert(self, col_text, item, before, col_text_align = None)
891 - delete(self, item)
892 - replace(self, col_text, item, col_text_align = None)
893 - current(self) : returns current item
894 - setCurrent(self, item): sets an item as current
895 - clear(self): clear the listbox
896
897 Alignments may be LEFT, RIGHT, CENTER, None
898 """
899 def __init__(self, height, cols, col_widths, scroll = 0,
900 returnExit = 0, width = 0, col_pad = 1,
901 col_text_align = None, col_labels = None,
902 col_label_align = None, adjust_width=0):
903
904 self.cols = cols
905 self.col_widths = col_widths[:]
906 self.col_pad = col_pad
907 self.col_text_align = col_text_align
908
909 if col_labels != None:
910 Grid.__init__(self, 1, 2)
911 box_y = 1
912
913 lstr = self.colFormText(col_labels, col_label_align,
914 adjust_width=adjust_width)
915 self.label = Label(lstr)
916 self.setField(self.label, 0, 0, anchorLeft=1)
917
918 else:
919 Grid.__init__(self, 1, 1)
920 box_y = 0
921
922
923 self.listbox = Listbox(height, scroll, returnExit, width)
924 self.setField(self.listbox, 0, box_y, anchorRight=1)
925
926 def colFormText(self, col_text, align = None, adjust_width=0):
927 i = 0
928 str = ""
929 c_len = len(col_text)
930 while (i < self.cols) and (i < c_len):
931
932 cstr = col_text[i]
933 cstrlen = _snack.wstrlen(cstr)
934 if self.col_widths[i] < cstrlen:
935 if adjust_width:
936 self.col_widths[i] = cstrlen
937 else:
938 cstr = cstr[:self.col_widths[i]]
939
940 delta = self.col_widths[i] - _snack.wstrlen(cstr)
941
942 if delta > 0:
943 if align == None:
944 a = LEFT
945 else:
946 a = align[i]
947
948 if a == LEFT:
949 cstr = cstr + (" " * delta)
950 if a == CENTER:
951 cstr = (" " * (delta / 2)) + cstr + \
952 (" " * ((delta + 1) / 2))
953 if a == RIGHT:
954 cstr = (" " * delta) + cstr
955
956 if i != c_len - 1:
957 pstr = (" " * self.col_pad)
958 else:
959 pstr = ""
960
961 str = str + cstr + pstr
962
963 i = i + 1
964
965 return str
966
967 def append(self, col_text, item, col_text_align = None):
968 if col_text_align == None:
969 col_text_align = self.col_text_align
970 text = self.colFormText(col_text, col_text_align)
971 self.listbox.append(text, item)
972
973 def insert(self, col_text, item, before, col_text_align = None):
974 if col_text_align == None:
975 col_text_align = self.col_text_align
976 text = self.colFormText(col_text, col_text_align)
977 self.listbox.insert(text, item, before)
978
979 def delete(self, item):
980 self.listbox.delete(item)
981
982 def replace(self, col_text, item, col_text_align = None):
983 if col_text_align == None:
984 col_text_align = self.col_text_align
985 text = self.colFormText(col_text, col_text_align)
986 self.listbox.replace(text, item)
987
988 def current(self):
989 return self.listbox.current()
990
991 def setCurrent(self, item):
992 self.listbox.setCurrent(item)
993
994 def clear(self):
995 self.listbox.clear()
996
997 def customColorset(x):
998 return 30 + x