Mercurial > urwid-satext
annotate frontends/primitivus/custom_widgets.py @ 21:96a2c5904e35
Primitivus: send_file first draft
- a new dialog for choosing a file is work in progress
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 12 Aug 2010 23:09:31 +0800 |
parents | 333a62fab0e3 |
children | dfabea6f73b5 |
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): | |
7 | 26 """Edit box which doesn't show what is entered (show '*' or other char instead)""" |
0 | 27 |
28 def __init__(self, *args, **kwargs): | |
7 | 29 """Same args than Edit.__init__ with an additional keyword arg 'hidden_char' |
30 @param hidden_char: char to show instead of what is actually entered: default '*' | |
31 """ | |
0 | 32 self.hidden_char=kwargs['hidden_char'] if kwargs.has_key('hidden_char') else '*' |
33 self.__real_text='' | |
34 super(Password, self).__init__(*args, **kwargs) | |
35 | |
36 def set_edit_text(self, text): | |
37 self.__real_text = text | |
38 hidden_txt = len(text)*'*' | |
39 super(Password, self).set_edit_text(hidden_txt) | |
40 | |
41 def get_edit_text(self): | |
42 return self.__real_text | |
43 | |
10
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
44 def insert_text(self, text): |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
45 self._edit_text = self.__real_text |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
46 super(Password,self).insert_text(text) |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
47 |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
48 def render(self, size, focus=False): |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
49 return super(Password, self).render(size, focus) |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
50 |
4
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
51 class AdvancedEdit(urwid.Edit): |
7 | 52 """Edit box with some custom improvments |
53 new chars: | |
54 - C-a: like 'home' | |
55 - C-e: like 'end' | |
56 - C-k: remove everything on the right of the cursor | |
57 - C-w: remove the word on the back | |
58 new behaviour: emit a 'click' signal when enter is pressed""" | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
59 signals = urwid.Edit.signals + ['click'] |
4
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
60 |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
61 def keypress(self, size, key): |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
62 #TODO: insert mode is not managed yet |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
63 if key == 'ctrl a': |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
64 key = 'home' |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
65 elif key == 'ctrl e': |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
66 key = 'end' |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
67 elif key == 'ctrl k': |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
68 self._delete_highlighted() |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
69 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
|
70 elif key == 'ctrl w': |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
71 before = self.edit_text[:self.edit_pos] |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
72 pos = before.rstrip().rfind(" ")+1 |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
73 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
|
74 self.set_edit_pos(pos) |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
75 elif key == 'enter': |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
76 self._emit('click') |
4
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
77 return super(AdvancedEdit, self).keypress(size, key) |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
78 |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
79 |
6 | 80 class SurroundedText(urwid.FlowWidget): |
7 | 81 """Text centered on a repeated character (like a Divider, but with a text in the center)""" |
6 | 82 |
83 def __init__(self,text,car=utf8decode('─')): | |
84 self.text=text | |
85 self.car=car | |
86 | |
87 def rows(self,size,focus=False): | |
88 return self.display_widget(size, focus).rows(size, focus) | |
89 | |
90 def render(self, size, focus=False): | |
91 return self.display_widget(size, focus).render(size, focus) | |
92 | |
93 def display_widget(self, size, focus): | |
94 (maxcol,) = size | |
95 middle = (maxcol-len(self.text))/2 | |
96 render_text = middle * self.car + self.text + (maxcol - len(self.text) - middle) * self.car | |
97 return urwid.Text(render_text) | |
98 | |
0 | 99 class SelectableText(urwid.FlowWidget): |
6 | 100 """Text which can be selected with space""" |
0 | 101 signals = ['change'] |
102 | |
9 | 103 def __init__(self, text, align='left', header='', select_attr=None, default_attr=None, selected = False, data=None): |
0 | 104 self.text=unicode(text) |
9 | 105 self.header=header |
106 if data: | |
107 self.data=data | |
108 if select_attr: | |
109 self.selected = select_attr | |
110 if default_attr: | |
111 self.default = default_attr | |
0 | 112 self.align = align |
9 | 113 self.__selected=selected |
0 | 114 |
115 def getValue(self): | |
116 return self.text | |
9 | 117 |
16
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
118 def get_text(self): |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
119 """for compatibility with urwid.Text""" |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
120 return self.getValue() |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
121 |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
122 def set_text(self, text): |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
123 self.text=unicode(text) |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
124 self._invalidate() |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
125 |
9 | 126 def setAttribute(self, name, value): |
127 """Change attribut used for rendering widget | |
128 @param name: one of | |
129 -default: when not selected | |
130 -selected: when selected | |
131 @param value: name of the attribute | |
132 /!\ the attribute name followed by _focus is used when widget has focus""" | |
133 assert name in ['default', 'selected'] | |
134 self.__setattr__(name,value) | |
135 self._invalidate() | |
0 | 136 |
137 def setState(self, selected, invisible=False): | |
138 """Change state | |
139 @param selected: boolean state value | |
140 @param invisible: don't emit change signal if True""" | |
141 assert(type(selected)==bool) | |
142 self.__selected=selected | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
143 self._invalidate() |
0 | 144 if not invisible: |
145 self._emit("change", self.__selected) | |
146 | |
147 def getState(self): | |
148 return self.__selected | |
149 | |
150 def selectable(self): | |
151 return True | |
152 | |
153 def keypress(self, size, key): | |
154 if key==' ' or key=='enter': | |
155 self.setState(not self.__selected) | |
156 else: | |
157 return key | |
158 | |
15
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
159 def mouse_event(self, size, event, button, x, y, focus): |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
160 if urwid.is_mouse_press(event) and button == 1: |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
161 self.setState(not self.__selected) |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
162 return True |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
163 |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
164 return False |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
165 |
0 | 166 def rows(self,size,focus=False): |
167 return self.display_widget(size, focus).rows(size, focus) | |
168 | |
169 def render(self, size, focus=False): | |
170 return self.display_widget(size, focus).render(size, focus) | |
171 | |
172 def display_widget(self, size, focus): | |
9 | 173 try: |
174 select_attr = self.selected | |
175 except AttributeError: | |
176 select_attr = 'selected' | |
177 try: | |
178 default_attr = self.default | |
179 except AttributeError: | |
180 default_attr = 'default' | |
181 attr = select_attr if self.__selected else default_attr | |
0 | 182 if focus: |
183 attr+="_focus" | |
9 | 184 return urwid.Text((attr,self.header+self.text), align=self.align) |
0 | 185 |
9 | 186 class ClickableText(SelectableText): |
187 signals = SelectableText.signals + ['click'] | |
8 | 188 |
189 def setState(self, selected, invisible=False): | |
9 | 190 self._emit('click') |
8 | 191 |
15
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
192 class CustomButton(ClickableText): |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
193 |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
194 def __init__(self, label, on_press=None, user_data=None, left_border = "[ ", right_border = " ]"): |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
195 self.label = label |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
196 render_txt = "%s%s%s" % (left_border, label, right_border) |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
197 self.size = len(render_txt) |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
198 super(CustomButton, self).__init__(render_txt) |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
199 if on_press: |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
200 urwid.connect_signal(self, 'click', on_press, user_data) |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
201 |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
202 def getSize(self): |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
203 """Return representation size of the button""" |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
204 return self.size |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
205 |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
206 def get_label(self): |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
207 return self.label |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
208 |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
209 class GenericList(urwid.WidgetWrap): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
210 signals = ['click','change'] |
0 | 211 |
8 | 212 def __init__(self, options, style=[], align='left', option_type = SelectableText, on_click=None, on_change=None, user_data=None): |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
213 """ |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
214 Widget managing list of string and their selection |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
215 @param options: list of strings used for options |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
216 @param style: list of string: |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
217 - 'single' if only one must be selected |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
218 - 'no_first_select' nothing selected when list is first displayed |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
219 - 'can_select_none' if we can select nothing |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
220 @param align: alignement of text inside the list |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
221 @param on_click: method called when click signal is emited |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
222 @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
|
223 """ |
0 | 224 self.single = 'single' in style |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
225 self.no_first_select = 'no_first_select' in style |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
226 self.can_select_none = 'can_select_none' in style |
0 | 227 self.align = align |
8 | 228 self.option_type = option_type |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
229 self.first_display = True |
0 | 230 |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
231 if on_click: |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
232 urwid.connect_signal(self, 'click', on_click, user_data) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
233 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
234 if on_change: |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
235 urwid.connect_signal(self, 'change', on_change, user_data) |
0 | 236 |
1 | 237 self.content = urwid.SimpleListWalker([]) |
0 | 238 self.list_box = urwid.ListBox(self.content) |
4
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
239 urwid.WidgetWrap.__init__(self, self.list_box) |
1 | 240 self.changeValues(options) |
0 | 241 |
242 def __onStateChange(self, widget, selected): | |
243 if self.single: | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
244 if not selected and not self.can_select_none: |
0 | 245 #if in single mode, it's forbidden to unselect a value |
246 widget.setState(True, invisible=True) | |
247 return | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
248 if selected: |
0 | 249 self.unselectAll(invisible=True) |
250 widget.setState(True, invisible=True) | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
251 self._emit("click") |
0 | 252 |
253 | |
254 def unselectAll(self, invisible=False): | |
255 for widget in self.content: | |
256 if widget.getState(): | |
257 widget.setState(False, invisible) | |
258 widget._invalidate() | |
259 | |
2
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
260 def deleteValue(self, value): |
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
261 """Delete the first value equal to the param given""" |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
262 for widget in self.content: |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
263 if widget.getValue() == value: |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
264 self.content.remove(widget) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
265 self._emit('change') |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
266 return |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
267 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
|
268 |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
269 def getSelectedValue(self): |
1 | 270 """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
|
271 values = self.getSelectedValues() |
1 | 272 return values[0] if values else None |
273 | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
274 def getAllValues(self): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
275 """Return values of all items""" |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
276 return [widget.getValue() for widget in self.content] |
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 getSelectedValues(self): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
279 """Return values of selected items""" |
0 | 280 result = [] |
281 for widget in self.content: | |
282 if widget.getState(): | |
283 result.append(widget.getValue()) | |
284 return result | |
285 | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
286 def getDisplayWidget(self): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
287 return self.list_box |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
288 |
0 | 289 def changeValues(self, new_values): |
2
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
290 """Change all value in one shot""" |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
291 if not self.first_display: |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
292 old_selected = self.getSelectedValues() |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
293 widgets = [] |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
294 for option in new_values: |
8 | 295 widget = self.option_type(option, self.align) |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
296 if not self.first_display and option in old_selected: |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
297 widget.setState(True) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
298 widgets.append(widget) |
8 | 299 try: |
300 urwid.connect_signal(widget, 'change', self.__onStateChange) | |
301 except NameError: | |
302 pass #the widget given doesn't support 'change' signal | |
0 | 303 self.content[:] = widgets |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
304 if self.first_display and self.single and new_values and not self.no_first_select: |
1 | 305 self.content[0].setState(True) |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
306 display_widget = self.getDisplayWidget() |
4
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
307 self._set_w(display_widget) |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
308 self._emit('change') |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
309 self.first_display = False |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
310 |
0 | 311 def selectValue(self, value): |
312 self.unselectAll() | |
313 idx = 0 | |
314 for widget in self.content: | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
315 if widget.getSelectedValue() == value: |
0 | 316 widget.setState(True) |
317 self.list_box.set_focus(idx) | |
318 return | |
319 idx+=1 | |
320 | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
321 class List(urwid.FlowWidget): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
322 """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
|
323 signals = ['click','change'] |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
324 |
8 | 325 def __init__(self, options, style=[], max_height=5, align='left', option_type = SelectableText, on_click=None, on_change=None, user_data=None): |
326 self.genericList = GenericList(options, style, align, option_type, on_click, on_change, user_data) | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
327 self.max_height = max_height |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
328 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
329 def selectable(self): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
330 return True |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
331 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
332 def keypress(self, size, key): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
333 return self.displayWidget(size,True).keypress(size, key) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
334 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
335 def unselectAll(self, invisible=False): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
336 return self.genericList.unselectAll(invisible) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
337 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
338 def deleteValue(self, value): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
339 return self.genericList.deleteValue(value) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
340 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
341 def getSelectedValue(self): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
342 return self.genericList.getSelectedValue() |
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 def getAllValues(self): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
345 return self.genericList.getAllValues() |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
346 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
347 def getSelectedValues(self): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
348 return self.genericList.getSelectedValues() |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
349 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
350 def changeValues(self, new_values): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
351 return self.genericList.changeValues(new_values) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
352 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
353 def selectValue(self, value): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
354 return self.genericList.selectValue(value) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
355 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
356 def render(self, size, focus=False): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
357 return self.displayWidget(size, focus).render(size, focus) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
358 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
359 def rows(self, size, focus=False): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
360 return self.displayWidget(size, focus).rows(size, focus) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
361 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
362 def displayWidget(self, size, focus): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
363 list_size = sum([wid.rows(size, focus) for wid in self.genericList.content]) |
6 | 364 height = min(list_size,self.max_height) or 1 |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
365 return urwid.BoxAdapter(self.genericList, height) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
366 |
10
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
367 ## MISC ## |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
368 |
16
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
369 class NotificationBar(urwid.WidgetWrap): |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
370 """Bar used to show misc information to user""" |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
371 signals = ['change'] |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
372 |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
373 def __init__(self): |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
374 self.waitNotifs = urwid.Text('') |
17 | 375 self.message = ClickableText('', default_attr='notifs', select_attr='notifs') |
16
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
376 urwid.connect_signal(self.message, 'click', lambda wid: self.showNext()) |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
377 self.columns = urwid.Columns([('fixed',6,self.waitNotifs),self.message]) |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
378 urwid.WidgetWrap.__init__(self, urwid.AttrMap(self.columns,'notifs')) |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
379 self.notifs = [] |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
380 |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
381 def __modQueue(self): |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
382 """must be called each time the notifications queue is changed""" |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
383 self.waitNotifs.set_text("(%i)" % len(self.notifs) if self.notifs else '') |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
384 self._emit('change') |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
385 |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
386 def addPopUp(self, pop_up_widget): |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
387 """Add a popup to the waiting queue""" |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
388 self.notifs.append(('popup',pop_up_widget)) |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
389 self.__modQueue() |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
390 |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
391 def addMessage(self, message): |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
392 "Add a message to the notificatio bar" |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
393 if not self.message.get_text(): |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
394 self.message.set_text(message) |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
395 self._invalidate() |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
396 self._emit('change') |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
397 else: |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
398 self.notifs.append(('message',message)) |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
399 self.__modQueue() |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
400 |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
401 def showNext(self): |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
402 """Show next message if any, else delete current message""" |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
403 found = None |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
404 for notif in self.notifs: |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
405 if notif[0] == "message": |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
406 found = notif |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
407 break |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
408 if found: |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
409 self.notifs.remove(found) |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
410 self.message.set_text(found[1]) |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
411 self.__modQueue() |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
412 else: |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
413 self.message.set_text('') |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
414 self._emit('change') |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
415 |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
416 def getNextPopup(self): |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
417 """Return next pop-up and remove it from the queue |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
418 @return: pop-up or None if there is no more in the queue""" |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
419 ret = None |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
420 for notif in self.notifs: |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
421 if notif[0] == 'popup': |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
422 ret = notif[1] |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
423 break |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
424 if ret: |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
425 self.notifs.remove(notif) |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
426 self.__modQueue() |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
427 return ret |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
428 |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
429 def isQueueEmpty(self): |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
430 return not bool(self.notifs) |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
431 |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
432 def canHide(self): |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
433 """Return True if there is now important information to show""" |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
434 return self.isQueueEmpty() and not self.message.get_text() |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
435 |
263fe4d067ad
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
436 |
11 | 437 class MenuBox(urwid.WidgetWrap): |
15
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
438 """Show menu items of a category in a box""" |
11 | 439 signals = ['click'] |
440 | |
441 def __init__(self,parent,items): | |
442 self.parent = parent | |
443 self.selected = None | |
444 content = urwid.SimpleListWalker([ClickableText(text,default_attr='menuitem') for text in items]) | |
445 for wid in content: | |
446 urwid.connect_signal(wid, 'click', self.onClick) | |
447 | |
448 self.listBox = urwid.ListBox(content) | |
449 menubox = urwid.LineBox(urwid.BoxAdapter(self.listBox,len(items))) | |
450 urwid.WidgetWrap.__init__(self,menubox) | |
451 | |
452 def getValue(self): | |
453 return self.selected | |
454 | |
455 def keypress(self, size, key): | |
456 if key=='up': | |
457 if self.listBox.get_focus()[1] == 0: | |
458 self.parent.keypress(size, key) | |
459 elif key=='left' or key=='right': | |
460 self.parent.keypress(size,'up') | |
461 self.parent.keypress(size,key) | |
462 return super(MenuBox,self).keypress(size,key) | |
463 | |
464 def onClick(self, wid): | |
465 self.selected = wid.getValue() | |
466 self._emit('click') | |
467 | |
10
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
468 class Menu(urwid.FlowWidget): |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
469 |
12
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
470 def __init__(self,loop, x_orig=0): |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
471 """Menu widget |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
472 @param loop: main loop of urwid |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
473 @param x_orig: absolute start of the abscissa |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
474 """ |
10
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
475 super(Menu, self).__init__() |
11 | 476 self.loop = loop |
477 self.menu_keys = [] | |
478 self.menu = {} | |
12
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
479 self.x_orig = x_orig |
10
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
480 self.shortcuts = {} #keyboard shortcuts |
11 | 481 self.focus_menu = 0 |
482 self.save_bottom = None | |
483 | |
484 def selectable(self): | |
485 return True | |
13
8cccbaadb9c5
Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
486 |
8cccbaadb9c5
Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
487 def getMenuSize(self): |
8cccbaadb9c5
Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
488 """return the current number of categories in this menu""" |
8cccbaadb9c5
Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
489 return len(self.menu_keys) |
12
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
490 |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
491 def setOrigX(self, orig_x): |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
492 self.x_orig = orig_x |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
493 |
11 | 494 def __buildOverlay(self,menu_key,columns): |
495 """Build the overlay menu which show menuitems | |
496 @param menu_key: name of the category | |
497 @colums: column number where the menubox must be displayed""" | |
498 max_len = 0 | |
499 for item in self.menu[menu_key]: | |
500 if len(item[0]) > max_len: | |
501 max_len = len(item[0]) | |
502 | |
503 self.save_bottom = self.loop.widget | |
504 menu_box = MenuBox(self,[item[0] for item in self.menu[menu_key]]) | |
505 urwid.connect_signal(menu_box, 'click', self.onClick) | |
506 | |
507 self.loop.widget = urwid.Overlay(urwid.AttrMap(menu_box,'menubar'),self.save_bottom,('fixed left', columns),max_len+2,('fixed top',1),None) | |
508 | |
509 def keypress(self, size, key): | |
510 if key == 'right' and self.focus_menu < len(self.menu)-1: | |
511 self.focus_menu += 1 | |
512 self._invalidate() | |
513 elif key == 'left' and self.focus_menu > 0: | |
514 self.focus_menu -= 1 | |
515 self._invalidate() | |
12
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
516 return |
11 | 517 elif key == 'down': |
12
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
518 if self.menu_keys and not self.save_bottom: |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
519 column = sum([len(menu)+4 for menu in self.menu_keys[0:self.focus_menu]],self.focus_menu+self.x_orig) |
11 | 520 self.__buildOverlay(self.menu_keys[self.focus_menu],column) |
521 elif key == 'up': | |
522 if self.save_bottom: | |
523 self.loop.widget = self.save_bottom | |
524 self.save_bottom = None | |
525 | |
526 return key | |
10
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
527 |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
528 def checkShortcuts(self, key): |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
529 for shortcut in self.shortcuts.keys(): |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
530 if key == shortcut: |
11 | 531 category, item, callback = self.shortcuts[shortcut] |
10
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
532 callback((category, item)) |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
533 return key |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
534 |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
535 def addMenu(self, category, item, callback, shortcut=None): |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
536 """Add a menu item, create the category if new |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
537 @param category: category of the menu (e.g. File/Edit) |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
538 @param item: menu item (e.g. new/close/about) |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
539 @callback: method to call when item is selected""" |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
540 if not category in self.menu.keys(): |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
541 self.menu_keys.append(category) |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
542 self.menu[category] = [] |
11 | 543 self.menu[category].append((item, callback)) |
10
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
544 if shortcut: |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
545 assert(shortcut not in self.shortcuts.keys()) |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
546 self.shortcuts[shortcut] = (category, item, callback) |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
547 |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
548 def rows(self,size,focus=False): |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
549 return self.display_widget(size, focus).rows(size, focus) |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
550 |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
551 def render(self, size, focus=False): |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
552 return self.display_widget(size, focus).render(size, focus) |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
553 |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
554 def display_widget(self, size, focus): |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
555 render_txt = [] |
11 | 556 idx = 0 |
10
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
557 for menu in self.menu_keys: |
11 | 558 if focus and idx == self.focus_menu: |
559 render_txt.append(('selected_menu', '[ %s ]' % menu)) | |
560 render_txt.append(' ') | |
561 else: | |
562 render_txt.append('[ %s ] ' % menu) | |
563 idx += 1 | |
10
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
564 return urwid.AttrMap(urwid.Text(render_txt), 'menubar') |
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
565 |
11 | 566 def onClick(self, widget): |
567 category = self.menu_keys[self.focus_menu] | |
568 item = widget.getValue() | |
569 for menu_item in self.menu[category]: | |
570 if item == menu_item[0]: | |
571 callback = menu_item[1] | |
572 break | |
573 if callback: | |
574 self.keypress(None,'up') | |
575 callback((category, item)) | |
576 | |
12
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
577 class MenuRoller(urwid.WidgetWrap): |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
578 |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
579 def __init__(self,menus_list): |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
580 """Create a MenuRoller |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
581 @param menus_list: list of tuple with (name, Menu_instance), name can be None |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
582 """ |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
583 assert (menus_list) |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
584 self.selected = 0 |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
585 self.name_list = [] |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
586 self.menus = {} |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
587 |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
588 self.columns = urwid.Columns([urwid.Text(''),urwid.Text('')]) |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
589 urwid.WidgetWrap.__init__(self, self.columns) |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
590 |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
591 for menu_tuple in menus_list: |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
592 name,menu = menu_tuple |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
593 self.addMenu(name, menu) |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
594 |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
595 def __showSelected(self): |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
596 """show menu selected""" |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
597 name_txt = u'\u21c9 '+self.name_list[self.selected]+u' \u21c7 ' |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
598 current_name = ClickableText(name_txt) |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
599 name_len = len(name_txt) |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
600 current_menu = self.menus[self.name_list[self.selected]] |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
601 current_menu.setOrigX(name_len) |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
602 self.columns.widget_list[0] = current_name |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
603 self.columns.column_types[0]=('fixed', name_len) |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
604 self.columns.widget_list[1] = current_menu |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
605 |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
606 def keypress(self, size, key): |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
607 if key=='up': |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
608 if self.columns.get_focus_column()==0 and self.selected > 0: |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
609 self.selected -= 1 |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
610 self.__showSelected() |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
611 elif key=='down': |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
612 if self.columns.get_focus_column()==0 and self.selected < len(self.name_list)-1: |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
613 self.selected += 1 |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
614 self.__showSelected() |
13
8cccbaadb9c5
Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
615 elif key=='right': |
8cccbaadb9c5
Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
616 if self.columns.get_focus_column()==0 and \ |
15
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
617 (isinstance(self.columns.widget_list[1], urwid.Text) or \ |
13
8cccbaadb9c5
Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
618 self.menus[self.name_list[self.selected]].getMenuSize()==0): |
8cccbaadb9c5
Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
619 return #if we have no menu or the menu is empty, we don't go the right column |
12
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
620 |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
621 return super(MenuRoller, self).keypress(size, key) |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
622 |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
623 def addMenu(self, name_param, menu): |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
624 name = name_param or '' |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
625 if name not in self.name_list: |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
626 self.name_list.append(name) |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
627 self.menus[name] = menu |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
628 if self.name_list[self.selected] == name: |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
629 self.__showSelected() #if we are on the menu, we update it |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
630 |
15
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
631 def removeMenu(self, name): |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
632 if name in self.name_list: |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
633 self.name_list.remove(name) |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
634 if name in self.menus.keys(): |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
635 del self.menus[name] |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
636 self.selected = 0 |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
637 self.__showSelected() |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
638 |
12
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
639 def checkShortcuts(self, key): |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
640 for menu in self.name_list: |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
641 key = self.menus[menu].checkShortcuts(key) |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
642 return key |
7e63429cc929
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
643 |
10
024b79b61a31
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
644 |
6 | 645 ## DIALOGS ## |
646 | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
647 class GenericDialog(urwid.WidgetWrap): |
0 | 648 |
649 def __init__(self, widgets_lst, title, style=[], **kwargs): | |
650 frame_header = urwid.AttrMap(urwid.Text(title,'center'),'title') | |
651 | |
652 buttons = None | |
653 | |
654 if "OK/CANCEL" in style: | |
655 buttons = [urwid.Button(_("Cancel"), kwargs['cancel_cb']), | |
656 urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] | |
657 elif "YES/NO" in style: | |
658 buttons = [urwid.Button(_("Yes"), kwargs['yes_cb']), | |
659 urwid.Button(_("No"), kwargs['no_cb'], kwargs['yes_value'])] | |
1 | 660 if "OK" in style: |
661 buttons = [urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] | |
0 | 662 if buttons: |
663 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center') | |
664 body_content = urwid.SimpleListWalker(widgets_lst) | |
665 frame_body = urwid.ListBox(body_content) | |
20
333a62fab0e3
Primitivus: buttons are now in the bottom of Dialogs
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
666 frame = urwid.Frame(frame_body, frame_header, buttons_flow if buttons else None) |
0 | 667 decorated_frame = urwid.LineBox(frame) |
668 urwid.WidgetWrap.__init__(self, decorated_frame) | |
669 | |
670 | |
671 | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
672 class InputDialog(GenericDialog): |
7 | 673 """Dialog with an edit box""" |
0 | 674 |
6 | 675 def __init__(self, title, instrucions, style=['OK/CANCEL'], default_txt = '', **kwargs): |
0 | 676 instr_wid = urwid.Text(instrucions+':') |
6 | 677 edit_box = urwid.Edit(edit_text=default_txt) |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
678 GenericDialog.__init__(self, [instr_wid,edit_box], title, style, ok_value=edit_box, **kwargs) |
0 | 679 |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
680 class ConfirmDialog(GenericDialog): |
7 | 681 """Dialog with buttons for confirm or cancel an action""" |
0 | 682 |
683 def __init__(self, title, style=['YES/NO'], **kwargs): | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
684 GenericDialog.__init__(self, [], title, style, yes_value=None, **kwargs) |
1 | 685 |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
686 class Alert(GenericDialog): |
7 | 687 """Dialog with just a message and a OK button""" |
1 | 688 |
689 def __init__(self, title, message, style=['OK'], **kwargs): | |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
690 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
|
691 |
6 | 692 ## CONTAINERS ## |
693 | |
18
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
694 class ColumnsRoller(urwid.FlowWidget): |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
695 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
696 def __init__(self, widget_list = None, focus_column=0): |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
697 self.widget_list = widget_list or [] |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
698 self.focus_column = focus_column |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
699 self.__start = 0 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
700 self.__next = False |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
701 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
702 def addWidget(self, widget, width): |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
703 self.widget_list.append((width,widget)) |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
704 if len(self.widget_list) == 1: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
705 self.set_focus(0) |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
706 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
707 def selectable(self): |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
708 try: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
709 return self.widget_list[self.focus_column][1].selectable() |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
710 except IndexError: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
711 return False |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
712 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
713 def keypress(self, size, key): |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
714 if key=='left': |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
715 if self.focus_column>0: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
716 self.focus_column-=1 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
717 self._invalidate() |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
718 return |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
719 if key=='right': |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
720 if self.focus_column<len(self.widget_list)-1: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
721 self.focus_column+=1 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
722 self._invalidate() |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
723 return |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
724 if self.focus_column<len(self.widget_list): |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
725 return self.widget_list[self.focus_column][1].keypress(size,key) |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
726 return key |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
727 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
728 def set_focus(self, idx): |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
729 if idx>len(self.widget_list)-1: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
730 idx = len(self.widget_list)-1 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
731 self.focus_column = idx |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
732 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
733 def rows(self,size,focus=False): |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
734 return 1 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
735 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
736 def __calculate_limits(self, size): |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
737 (maxcol,) = size |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
738 _prev = _next = False |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
739 start_wid = 0 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
740 end_wid = len(self.widget_list)-1 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
741 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
742 total_wid = sum([w[0] for w in self.widget_list]) |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
743 while total_wid > maxcol: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
744 if self.focus_column == end_wid: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
745 if not _prev: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
746 total_wid+=1 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
747 _prev = True |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
748 total_wid-=self.widget_list[start_wid][0] |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
749 start_wid+=1 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
750 else: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
751 if not _next: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
752 total_wid+=1 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
753 _next = True |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
754 total_wid-=self.widget_list[end_wid][0] |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
755 end_wid-=1 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
756 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
757 cols_left = maxcol - total_wid |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
758 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
759 return _prev,_next,start_wid,end_wid,cols_left |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
760 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
761 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
762 def mouse_event(self, size, event, button, x, y, focus): |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
763 (maxcol,)=size |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
764 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
765 if urwid.is_mouse_press(event) and button == 1: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
766 _prev,_next,start_wid,end_wid,cols_left = self.__calculate_limits(size) |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
767 if x==0 and _prev: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
768 self.keypress(size,'left') |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
769 return True |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
770 if x==maxcol-1 and _next: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
771 self.keypress(size,'right') |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
772 return True |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
773 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
774 current_pos = 1 if _prev else 0 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
775 idx = 0 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
776 while current_pos<x and idx<len(self.widget_list): |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
777 width,widget = self.widget_list[idx] |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
778 if x<=current_pos+width: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
779 self.focus_column = idx |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
780 self._invalidate() |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
781 if not hasattr(widget,'mouse_event'): |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
782 return False |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
783 return widget.mouse_event((width,0), event, button, |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
784 x-current_pos, 0, focus) |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
785 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
786 current_pos+=self.widget_list[idx][0] |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
787 idx+=1 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
788 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
789 return False |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
790 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
791 def render(self, size, focus=False): |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
792 if not self.widget_list: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
793 return SolidCanvas(" ", size[0], 1) |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
794 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
795 _prev,_next,start_wid,end_wid,cols_left = self.__calculate_limits(size) |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
796 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
797 idx=start_wid |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
798 render = [] |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
799 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
800 for width,widget in self.widget_list[start_wid:end_wid+1]: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
801 _focus = idx == self.focus_column and focus |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
802 render.append((widget.render((width,),_focus),False,_focus,width)) |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
803 idx+=1 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
804 if _prev: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
805 render.insert(0,(urwid.Text([u"◀"]).render((1,),False),False,False,1)) |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
806 if _next: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
807 render.append((urwid.Text([u"▶"],align='right').render((1+cols_left,),False),False,False,1+cols_left)) |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
808 else: |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
809 render.append((urwid.SolidCanvas(" "*cols_left, size[0], 1),False,False,cols_left)) |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
810 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
811 return urwid.CanvasJoin(render) |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
812 |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
813 |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
814 class FocusFrame(urwid.Frame): |
7 | 815 """Frame which manage 'tab' key""" |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
816 |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
817 def keypress(self, size, key): |
19
0b83dd2b15d1
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
818 ret = urwid.Frame.keypress(self, size, key) |
0b83dd2b15d1
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
819 if not ret: |
0b83dd2b15d1
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
820 return |
0b83dd2b15d1
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
821 |
5
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
822 if key == 'tab': |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
823 focus_list = ('header','body','footer') |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
824 focus_idx = focus_list.index(self.focus_part) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
825 for i in range(2): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
826 focus_idx = (focus_idx + 1) % len(focus_list) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
827 focus_name = focus_list[focus_idx] |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
828 widget = getattr(self,'_'+focus_name) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
829 if widget!=None and widget.selectable(): |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
830 self.set_focus(focus_name) |
592cd64933dd
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
831 |
19
0b83dd2b15d1
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
832 return ret |
6 | 833 |
15
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
834 class TabsContainer(urwid.WidgetWrap): |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
835 signals = ['click'] |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
836 |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
837 def __init__(self): |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
838 #self._current_tab = 0 |
18
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
839 self._buttons_cont = ColumnsRoller() |
15
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
840 self.tabs = [] |
19
0b83dd2b15d1
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
841 self.__frame = FocusFrame(urwid.Filler(urwid.Text('')),urwid.Pile([self._buttons_cont,urwid.Divider(u"─")])) |
15
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
842 urwid.WidgetWrap.__init__(self, self.__frame) |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
843 |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
844 def keypress(self, size, key): |
19
0b83dd2b15d1
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
845 if key=='tab': |
0b83dd2b15d1
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
846 self._w.keypress(size,key) |
0b83dd2b15d1
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
847 return |
0b83dd2b15d1
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
848 return self._w.keypress(size,key) |
15
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
849 |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
850 def __buttonClicked(self, button, invisible=False): |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
851 """Called when a button on the tab is changed, |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
852 change the page |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
853 @param button: button clicked |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
854 @param invisible: emit signal only if False""" |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
855 tab_name = button.get_label() |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
856 for tab in self.tabs: |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
857 if tab[0] == tab_name: |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
858 break |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
859 if tab[0] != tab_name: |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
860 error(_("INTERNAL ERROR: Tab not found")) |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
861 assert(False) |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
862 self.__frame.body = tab[1] |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
863 if not invisible: |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
864 self._emit('click') |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
865 |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
866 def __appendButton(self, name): |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
867 """Append a button to the frame header, |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
868 and link it to the page change method""" |
18
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
869 button = CustomButton(name, self.__buttonClicked, left_border = '', right_border=' | ') |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
870 self._buttons_cont.addWidget(button, button.getSize()) |
bdc83e857093
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
871 if len(self._buttons_cont.widget_list) == 1: |
15
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
872 #first button: we set the focus and the body |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
873 self._buttons_cont.set_focus(0) |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
874 self.__buttonClicked(button,True) |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
875 |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
876 def addTab(self,name,content=[]): |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
877 """Add a page to the container |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
878 @param name: name of the page (what appear on the tab) |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
879 @param content: content of the page |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
880 @return: ListBox (content of the page)""" |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
881 listbox = urwid.ListBox(urwid.SimpleListWalker(content)) |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
882 self.tabs.append([name,listbox]) |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
883 self.__appendButton(name) |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
884 return listbox |
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
885 |
19
0b83dd2b15d1
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
886 def addFooter(self, widget): |
0b83dd2b15d1
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
887 """Add a widget on the bottom of the tab (will be displayed on all pages) |
0b83dd2b15d1
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
888 @param widget: FlowWidget""" |
0b83dd2b15d1
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
889 self._w.footer = widget |
0b83dd2b15d1
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
890 |
0b83dd2b15d1
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
891 |
6 | 892 ## DECORATORS ## |
893 class LabelLine(urwid.LineBox): | |
7 | 894 """Like LineBox, but with a Label centered in the top line""" |
6 | 895 |
896 def __init__(self, original_widget, label_widget): | |
897 urwid.LineBox.__init__(self, original_widget) | |
898 top_columns = self._w.widget_list[0] | |
899 top_columns.widget_list[1] = label_widget | |
8 | 900 |
901 class VerticalSeparator(urwid.WidgetDecoration, urwid.WidgetWrap): | |
21 | 902 def __init__(self, original_widget, left_char = u"│", right_char = ''): |
15
8241b3157699
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
903 """Draw a separator on left and/or right of original_widget.""" |
6 | 904 |
8 | 905 widgets = [original_widget] |
906 if left_char: | |
907 widgets.insert(0, ('fixed', 1, urwid.SolidFill(left_char))) | |
908 if right_char: | |
909 widgets.append(('fixed', 1, urwid.SolidFill(right_char))) | |
910 columns = urwid.Columns(widgets, box_columns = [0,2], focus_column = 1) | |
911 urwid.WidgetDecoration.__init__(self, original_widget) | |
912 urwid.WidgetWrap.__init__(self, columns) | |
913 | |
914 |