]> git.ipfire.org Git - ipfire-3.x.git/blob - src/pomona/text.py
Daily checkin.
[ipfire-3.x.git] / src / pomona / text.py
1 #!/usr/bin/python
2
3 import string
4
5 from snack import *
6 from constants import *
7
8 import gettext
9 _ = lambda x: gettext.ldgettext("pomona", x)
10
11 class TextWindow:
12 def __init__(self, screen):
13 self.screen = screen
14
15 def pop(self):
16 self.screen.popWindow()
17 self.screen.refresh()
18
19 def refresh(self):
20 self.screen.refresh()
21
22
23 class WaitWindow(TextWindow):
24 def setText(self, text):
25 self.t.setText(text)
26 self.g.draw()
27 self.screen.refresh()
28
29 def __init__(self, screen, title, text, width):
30 TextWindow.__init__(self, screen)
31
32 if width is None:
33 width = 40
34 if (len(text) < width):
35 width = len(text)
36
37 self.t = TextboxReflowed(width, text)
38
39 self.g = GridForm(self.screen, title, 1, 1)
40 self.g.add(self.t, 0, 0)
41 self.g.draw()
42 self.screen.refresh()
43
44
45 class OkCancelWindow:
46 def getrc(self):
47 return self.rc
48
49 def __init__(self, screen, title, text):
50 rc = ButtonChoiceWindow(screen, title, text, buttons=[TEXT_OK_BUTTON, _("Cancel")])
51 if rc == string.lower(_("Cancel")):
52 self.rc = 1
53 else:
54 self.rc = 0
55
56
57 class ExceptionWindow(TextWindow):
58 def __init__ (self, short, long=None, screen=None):
59 TextWindow.__init__(self, screen)
60 self.text = "%s\n\n" % short
61 self.buttons=[TEXT_OK_BUTTON]
62
63 def run(self):
64 self.rc = ButtonChoiceWindow(self.screen, _("Exception Occurred"),
65 self.text, self.buttons, width=60)
66
67 def getrc(self):
68 return 0
69
70
71 class TextInterface:
72 def __init__(self, log):
73 self.log = log
74 self.screen = SnackScreen()
75
76 self.setRootline(SCREEN_ROOTLINE)
77 self.setHelpline(SCREEN_HELPLINE)
78
79 def __del__(self):
80 if self.screen:
81 self.screen.finish()
82
83 def setRootline(self, msg):
84 self.screen.drawRootText (0, 0, string.center(msg, self.screen.width))
85 self.log.debug("Set rootline text: %s" % msg)
86
87 def setHelpline(self, msg):
88 self.screen.pushHelpLine(string.center(msg, self.screen.width))
89 self.log.debug("Set helpline text: %s" % msg)
90
91
92 ### WINDOW DEFINITIONS ###
93
94 def waitWindow(self, title, text, width=None):
95 return WaitWindow(self.screen, title, text, width)
96
97 def exceptionWindow(self, short, long):
98 self.log.critical(short)
99 return ExceptionWindow(short, long, self.screen)
100
101 def messageWindow(self, title, text, type="ok", default = None,
102 custom_icon=None, custom_buttons=[]):
103 if type == "ok":
104 ButtonChoiceWindow(self.screen, title, text, buttons=[TEXT_OK_BUTTON])
105
106 elif type == "yesno":
107 if default and default == "no":
108 btnlist = [TEXT_NO_BUTTON, TEXT_YES_BUTTON]
109 else:
110 btnlist = [TEXT_YES_BUTTON, TEXT_NO_BUTTON]
111 rc = ButtonChoiceWindow(self.screen, title, text, buttons=btnlist)
112 if rc == "yes":
113 return 1
114 else:
115 return 0
116
117 elif type == "custom":
118 tmpbut = []
119 for but in custom_buttons:
120 tmpbut.append(string.replace(but,"_",""))
121 rc = ButtonChoiceWindow(self.screen, title, text, width=60, buttons=tmpbut)
122
123 idx = 0
124 for b in tmpbut:
125 if string.lower(b) == rc:
126 return idx
127 idx = idx + 1
128 return 0
129
130 else:
131 return OkCancelWindow(self.screen, title, text)