comparison pyntnclick/widgets/messagebox.py @ 555:c0474fe18b96 pyntnclick

Copy in widgets from mamba (currently unused)
author Stefano Rivera <stefano@rivera.za.net>
date Sat, 11 Feb 2012 14:09:46 +0200
parents
children f9f04cb35697
comparison
equal deleted inserted replaced
554:99a1420097df 555:c0474fe18b96
1 from mamba.constants import FONT_SIZE
2 from mamba.widgets.base import Box
3 from mamba.widgets.text import TextWidget, TextButton
4
5
6 class MessageBox(Box):
7
8 def __init__(self, rect, text, post_callback=None, color='red',
9 fontsize=FONT_SIZE):
10 super(MessageBox, self).__init__(rect)
11 self.text = text
12 self.font_size = fontsize
13 self.post_callback = post_callback
14 self.color = color
15 self.prepare()
16 self.modal = True
17
18 def prepare(self):
19 cont = TextWidget((0, 0), "Press [OK] or Enter to continue",
20 fontsize=self.font_size)
21 widgets = []
22 width = cont.rect.width
23 for line in self.text.split('\n'):
24 message = TextWidget((0, 0), line, color=self.color,
25 fontsize=self.font_size)
26 widgets.append(message)
27 width = max(width, message.rect.width)
28 widgets.append(cont)
29 top = self.rect.top + 10
30 left = self.rect.left + 5
31 for widget in widgets:
32 pos = (left + width / 2 - widget.rect.width / 2, top)
33 widget.rect.topleft = pos
34 top += widget.rect.height + 5
35 self.add(widget)
36 self.ok_button = ok_button = TextButton((0, 0), 'OK')
37 ok_pos = (self.rect.left + 5 + width / 2 - ok_button.rect.width / 2,
38 top + 5)
39 ok_button.rect.topleft = ok_pos
40 ok_button.add_callback('clicked', self.close)
41 self.add(ok_button)
42 self.rect.height += 5
43
44 def close(self, ev, widget):
45 if hasattr(self.parent, 'paused'):
46 self.parent.paused = False
47 self.parent.remove(self)
48 if self.post_callback:
49 self.post_callback()
50 if getattr(self, 'parent_modal', False):
51 self.parent.modal = True
52
53 def grab_focus(self):
54 return self.ok_button.grab_focus()