Mercurial > urwid-satext
annotate frontends/primitivus/custom_widgets.py @ 6:d586d06a9d8f
primitivus group chat & misc
primitivus: new widget: SurroundedText (text with a character repeated around it)
primitivus: new decorator LabelLine (like lineBox, but with a label on the top line)
wix & primitivus & quick_app: group chat method move to quick_chat
wix: when new message, window is not raised anymore, but RequestUserAttention is called instead
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 08 Jul 2010 14:12:18 +0800 |
parents | 592cd64933dd |
children | 94868f58850b |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 Primitivus: a SAT frontend | |
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 import urwid | |
6 | 23 from urwid.escape import utf8decode |
0 | 24 |
25 class Password(urwid.Edit): | |
26 | |
27 def __init__(self, *args, **kwargs): | |
28 self.hidden_char=kwargs['hidden_char'] if kwargs.has_key('hidden_char') else '*' | |
29 self.__real_text='' | |
30 super(Password, self).__init__(*args, **kwargs) | |
31 | |
32 def set_edit_text(self, text): | |
33 self.__real_text = text | |
34 hidden_txt = len(text)*'*' | |
35 super(Password, self).set_edit_text(hidden_txt) | |
36 | |
37 def get_edit_text(self): | |
38 return self.__real_text | |
39 | |
4
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
40 class AdvancedEdit(urwid.Edit): |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
41 """Edit box with some custom improvments""" |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
42 signals = urwid.Edit.signals + ['click'] |
4
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
43 |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
44 def keypress(self, size, key): |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
45 #TODO: insert mode is not managed yet |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
46 if key == 'ctrl a': |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
47 key = 'home' |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
48 elif key == 'ctrl e': |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
49 key = 'end' |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
50 elif key == 'ctrl k': |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
51 self._delete_highlighted() |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
52 self.set_edit_text(self.edit_text[:self.edit_pos]) |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
53 elif key == 'ctrl w': |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
54 before = self.edit_text[:self.edit_pos] |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
55 pos = before.rstrip().rfind(" ")+1 |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
56 self.set_edit_text(before[:pos] + self.edit_text[self.edit_pos:]) |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
57 self.set_edit_pos(pos) |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
58 elif key == 'enter': |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
59 self._emit('click') |
4
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
60 return super(AdvancedEdit, self).keypress(size, key) |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
61 |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
62 |
6 | 63 class SurroundedText(urwid.FlowWidget): |
64 | |
65 def __init__(self,text,car=utf8decode('─')): | |
66 self.text=text | |
67 self.car=car | |
68 | |
69 def rows(self,size,focus=False): | |
70 return self.display_widget(size, focus).rows(size, focus) | |
71 | |
72 def render(self, size, focus=False): | |
73 return self.display_widget(size, focus).render(size, focus) | |
74 | |
75 def display_widget(self, size, focus): | |
76 (maxcol,) = size | |
77 middle = (maxcol-len(self.text))/2 | |
78 render_text = middle * self.car + self.text + (maxcol - len(self.text) - middle) * self.car | |
79 return urwid.Text(render_text) | |
80 | |
81 | |
82 | |
0 | 83 class SelectableText(urwid.FlowWidget): |
6 | 84 """Text which can be selected with space""" |
0 | 85 signals = ['change'] |
86 | |
87 def __init__(self, text, align='left'): | |
88 self.text=unicode(text) | |
89 self.align = align | |
90 self.__selected=False | |
91 | |
92 def getValue(self): | |
93 return self.text | |
94 | |
95 def setState(self, selected, invisible=False): | |
96 """Change state | |
97 @param selected: boolean state value | |
98 @param invisible: don't emit change signal if True""" | |
99 assert(type(selected)==bool) | |
100 self.__selected=selected | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
101 self._invalidate() |
0 | 102 if not invisible: |
103 self._emit("change", self.__selected) | |
104 | |
105 def getState(self): | |
106 return self.__selected | |
107 | |
108 def selectable(self): | |
109 return True | |
110 | |
111 def keypress(self, size, key): | |
112 if key==' ' or key=='enter': | |
113 self.setState(not self.__selected) | |
114 else: | |
115 return key | |
116 | |
117 def rows(self,size,focus=False): | |
118 return self.display_widget(size, focus).rows(size, focus) | |
119 | |
120 def render(self, size, focus=False): | |
121 return self.display_widget(size, focus).render(size, focus) | |
122 | |
123 def display_widget(self, size, focus): | |
124 attr = 'selected' if self.__selected else 'default' | |
125 if focus: | |
126 attr+="_focus" | |
127 return urwid.Text((attr,self.text), align=self.align) | |
128 | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
129 class GenericList(urwid.WidgetWrap): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
130 signals = ['click','change'] |
0 | 131 |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
132 def __init__(self, options, style=[], align='left', on_click=None, on_change=None, user_data=None): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
133 """ |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
134 Widget managing list of string and their selection |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
135 @param options: list of strings used for options |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
136 @param style: list of string: |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
137 - 'single' if only one must be selected |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
138 - 'no_first_select' nothing selected when list is first displayed |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
139 - 'can_select_none' if we can select nothing |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
140 @param align: alignement of text inside the list |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
141 @param on_click: method called when click signal is emited |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
142 @param user_data: data sent to the callback for click signal |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
143 """ |
0 | 144 self.single = 'single' in style |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
145 self.no_first_select = 'no_first_select' in style |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
146 self.can_select_none = 'can_select_none' in style |
0 | 147 self.align = align |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
148 self.first_display = True |
0 | 149 |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
150 if on_click: |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
151 urwid.connect_signal(self, 'click', on_click, user_data) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
152 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
153 if on_change: |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
154 urwid.connect_signal(self, 'change', on_change, user_data) |
0 | 155 |
1 | 156 self.content = urwid.SimpleListWalker([]) |
0 | 157 self.list_box = urwid.ListBox(self.content) |
4
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
158 urwid.WidgetWrap.__init__(self, self.list_box) |
1 | 159 self.changeValues(options) |
0 | 160 |
161 def __onStateChange(self, widget, selected): | |
162 if self.single: | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
163 if not selected and not self.can_select_none: |
0 | 164 #if in single mode, it's forbidden to unselect a value |
165 widget.setState(True, invisible=True) | |
166 return | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
167 if selected: |
0 | 168 self.unselectAll(invisible=True) |
169 widget.setState(True, invisible=True) | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
170 self._emit("click") |
0 | 171 |
172 | |
173 def unselectAll(self, invisible=False): | |
174 for widget in self.content: | |
175 if widget.getState(): | |
176 widget.setState(False, invisible) | |
177 widget._invalidate() | |
178 | |
2
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
179 def deleteValue(self, value): |
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
180 """Delete the first value equal to the param given""" |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
181 for widget in self.content: |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
182 if widget.getValue() == value: |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
183 self.content.remove(widget) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
184 self._emit('change') |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
185 return |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
186 raise ValueError("%s ==> %s" % (str(value),str(self.content))) |
2
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
187 |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
188 def getSelectedValue(self): |
1 | 189 """Convenience method to get the value selected as a string in single mode, or None""" |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
190 values = self.getSelectedValues() |
1 | 191 return values[0] if values else None |
192 | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
193 def getAllValues(self): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
194 """Return values of all items""" |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
195 return [widget.getValue() for widget in self.content] |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
196 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
197 def getSelectedValues(self): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
198 """Return values of selected items""" |
0 | 199 result = [] |
200 for widget in self.content: | |
201 if widget.getState(): | |
202 result.append(widget.getValue()) | |
203 return result | |
204 | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
205 def getDisplayWidget(self): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
206 return self.list_box |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
207 |
0 | 208 def changeValues(self, new_values): |
2
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
209 """Change all value in one shot""" |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
210 if not self.first_display: |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
211 old_selected = self.getSelectedValues() |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
212 widgets = [] |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
213 for option in new_values: |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
214 widget = SelectableText(option, self.align) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
215 if not self.first_display and option in old_selected: |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
216 widget.setState(True) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
217 widgets.append(widget) |
1 | 218 urwid.connect_signal(widget, 'change', self.__onStateChange) |
0 | 219 self.content[:] = widgets |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
220 if self.first_display and self.single and new_values and not self.no_first_select: |
1 | 221 self.content[0].setState(True) |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
222 display_widget = self.getDisplayWidget() |
4
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
223 self._set_w(display_widget) |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
224 self._emit('change') |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
225 self.first_display = False |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
226 |
0 | 227 def selectValue(self, value): |
228 self.unselectAll() | |
229 idx = 0 | |
230 for widget in self.content: | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
231 if widget.getSelectedValue() == value: |
0 | 232 widget.setState(True) |
233 self.list_box.set_focus(idx) | |
234 return | |
235 idx+=1 | |
236 | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
237 class List(urwid.FlowWidget): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
238 """FlowWidget list, same arguments as GenericList, with an additional one 'max_height'""" |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
239 signals = ['click','change'] |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
240 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
241 def __init__(self, options, style=[], max_height=5, align='left', on_click=None, on_change=None, user_data=None): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
242 self.genericList = GenericList(options, style, align, on_click, on_change, user_data) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
243 self.max_height = max_height |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
244 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
245 def selectable(self): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
246 return True |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
247 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
248 def keypress(self, size, key): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
249 return self.displayWidget(size,True).keypress(size, key) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
250 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
251 def unselectAll(self, invisible=False): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
252 return self.genericList.unselectAll(invisible) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
253 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
254 def deleteValue(self, value): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
255 return self.genericList.deleteValue(value) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
256 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
257 def getSelectedValue(self): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
258 return self.genericList.getSelectedValue() |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
259 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
260 def getAllValues(self): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
261 return self.genericList.getAllValues() |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
262 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
263 def getSelectedValues(self): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
264 return self.genericList.getSelectedValues() |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
265 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
266 def changeValues(self, new_values): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
267 return self.genericList.changeValues(new_values) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
268 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
269 def selectValue(self, value): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
270 return self.genericList.selectValue(value) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
271 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
272 def render(self, size, focus=False): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
273 return self.displayWidget(size, focus).render(size, focus) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
274 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
275 def rows(self, size, focus=False): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
276 return self.displayWidget(size, focus).rows(size, focus) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
277 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
278 def displayWidget(self, size, focus): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
279 list_size = sum([wid.rows(size, focus) for wid in self.genericList.content]) |
6 | 280 height = min(list_size,self.max_height) or 1 |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
281 return urwid.BoxAdapter(self.genericList, height) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
282 |
6 | 283 ## DIALOGS ## |
284 | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
285 class GenericDialog(urwid.WidgetWrap): |
0 | 286 |
287 def __init__(self, widgets_lst, title, style=[], **kwargs): | |
288 frame_header = urwid.AttrMap(urwid.Text(title,'center'),'title') | |
289 | |
290 buttons = None | |
291 | |
292 if "OK/CANCEL" in style: | |
293 buttons = [urwid.Button(_("Cancel"), kwargs['cancel_cb']), | |
294 urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] | |
295 elif "YES/NO" in style: | |
296 buttons = [urwid.Button(_("Yes"), kwargs['yes_cb']), | |
297 urwid.Button(_("No"), kwargs['no_cb'], kwargs['yes_value'])] | |
1 | 298 if "OK" in style: |
299 buttons = [urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] | |
0 | 300 if buttons: |
301 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center') | |
302 widgets_lst.append(buttons_flow) | |
303 body_content = urwid.SimpleListWalker(widgets_lst) | |
304 frame_body = urwid.ListBox(body_content) | |
305 frame = urwid.Frame(frame_body, frame_header) | |
306 decorated_frame = urwid.LineBox(frame) | |
307 urwid.WidgetWrap.__init__(self, decorated_frame) | |
308 | |
309 | |
310 | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
311 class InputDialog(GenericDialog): |
0 | 312 |
6 | 313 def __init__(self, title, instrucions, style=['OK/CANCEL'], default_txt = '', **kwargs): |
0 | 314 instr_wid = urwid.Text(instrucions+':') |
6 | 315 edit_box = urwid.Edit(edit_text=default_txt) |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
316 GenericDialog.__init__(self, [instr_wid,edit_box], title, style, ok_value=edit_box, **kwargs) |
0 | 317 |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
318 class ConfirmDialog(GenericDialog): |
0 | 319 |
320 def __init__(self, title, style=['YES/NO'], **kwargs): | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
321 GenericDialog.__init__(self, [], title, style, yes_value=None, **kwargs) |
1 | 322 |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
323 class Alert(GenericDialog): |
1 | 324 |
325 def __init__(self, title, message, style=['OK'], **kwargs): | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
326 GenericDialog.__init__(self, [urwid.Text(message, 'center')], title, style, ok_value=None, **kwargs) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
327 |
6 | 328 ## CONTAINERS ## |
329 | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
330 class FocusFrame(urwid.Frame): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
331 """Frame which manage "tab" key""" |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
332 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
333 def keypress(self, size, key): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
334 if key == 'tab': |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
335 focus_list = ('header','body','footer') |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
336 focus_idx = focus_list.index(self.focus_part) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
337 for i in range(2): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
338 focus_idx = (focus_idx + 1) % len(focus_list) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
339 focus_name = focus_list[focus_idx] |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
340 widget = getattr(self,'_'+focus_name) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
341 if widget!=None and widget.selectable(): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
342 self.set_focus(focus_name) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
343 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
344 return urwid.Frame.keypress(self, size, key) |
6 | 345 |
346 ## DECORATORS ## | |
347 class LabelLine(urwid.LineBox): | |
348 | |
349 def __init__(self, original_widget, label_widget): | |
350 urwid.LineBox.__init__(self, original_widget) | |
351 top_columns = self._w.widget_list[0] | |
352 top_columns.widget_list[1] = label_widget | |
353 |