Mercurial > mamba
annotate mamba/widgets/messagebox.py @ 303:af202e15167d
Multi-line messages
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Fri, 16 Sep 2011 17:35:50 +0200 |
parents | c068190c0b1d |
children | 170d4a43c00e |
rev | line source |
---|---|
190 | 1 import pygame |
210
b92f705bd8ea
Modal dialogs, and focus stealing bugfixes
Stefano Rivera <stefano@rivera.za.net>
parents:
190
diff
changeset
|
2 from pygame.constants import SRCALPHA |
190 | 3 |
4 from mamba.widgets.base import Container | |
5 from mamba.widgets.text import TextWidget, TextButton | |
6 | |
7 | |
8 class MessageBox(Container): | |
9 | |
10 def __init__(self, rect, text, post_callback=None, color='white'): | |
11 super(MessageBox, self).__init__(rect) | |
12 self.text = text | |
13 self.post_callback = post_callback | |
14 self.color = color | |
15 self.prepare() | |
210
b92f705bd8ea
Modal dialogs, and focus stealing bugfixes
Stefano Rivera <stefano@rivera.za.net>
parents:
190
diff
changeset
|
16 self.modal = True |
190 | 17 |
18 def prepare(self): | |
19 cont = TextWidget((0, 0), "Press [OK] or Enter to continue") | |
303 | 20 widgets = [] |
21 width = cont.rect.width | |
22 for line in self.text.split('\n'): | |
23 message = TextWidget((0, 0), line, color=self.color) | |
24 widgets.append(message) | |
25 width = max(width, message.rect.width) | |
26 widgets.append(cont) | |
27 top = self.rect.top + 10 | |
28 left = self.rect.left + 5 | |
29 for widget in widgets: | |
30 pos = (left + width / 2 - widget.rect.width / 2, top) | |
31 widget.rect.topleft = pos | |
32 top += widget.rect.height + 5 | |
33 self.add(widget) | |
210
b92f705bd8ea
Modal dialogs, and focus stealing bugfixes
Stefano Rivera <stefano@rivera.za.net>
parents:
190
diff
changeset
|
34 self.ok_button = ok_button = TextButton((0, 0), 'OK') |
190 | 35 ok_pos = (self.rect.left + 5 + width / 2 - ok_button.rect.width / 2, |
303 | 36 top + 5) |
190 | 37 ok_button.rect.topleft = ok_pos |
38 ok_button.add_callback('clicked', self.close) | |
39 self.add(ok_button) | |
40 | |
41 def draw(self, surface): | |
42 background = pygame.Surface(self.rect.size, SRCALPHA) | |
43 background.fill(pygame.Color('gray')) | |
44 surface.blit(background, self.rect) | |
45 super(MessageBox, self).draw(surface) | |
46 | |
47 def close(self, ev, widget): | |
48 self.parent.remove(self) | |
49 if self.post_callback: | |
50 self.post_callback() | |
210
b92f705bd8ea
Modal dialogs, and focus stealing bugfixes
Stefano Rivera <stefano@rivera.za.net>
parents:
190
diff
changeset
|
51 |
b92f705bd8ea
Modal dialogs, and focus stealing bugfixes
Stefano Rivera <stefano@rivera.za.net>
parents:
190
diff
changeset
|
52 def grab_focus(self): |
b92f705bd8ea
Modal dialogs, and focus stealing bugfixes
Stefano Rivera <stefano@rivera.za.net>
parents:
190
diff
changeset
|
53 return self.ok_button.grab_focus() |