Mercurial > mamba
annotate mamba/widgets/messagebox.py @ 210:b92f705bd8ea
Modal dialogs, and focus stealing bugfixes
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Wed, 14 Sep 2011 20:49:48 +0200 |
parents | c9cee22934be |
children | c068190c0b1d |
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 fontcache = {} | |
10 | |
11 def __init__(self, rect, text, post_callback=None, color='white'): | |
12 super(MessageBox, self).__init__(rect) | |
13 self.text = text | |
14 self.post_callback = post_callback | |
15 self.color = color | |
16 self.prepare() | |
210
b92f705bd8ea
Modal dialogs, and focus stealing bugfixes
Stefano Rivera <stefano@rivera.za.net>
parents:
190
diff
changeset
|
17 self.modal = True |
190 | 18 |
19 def prepare(self): | |
20 cont = TextWidget((0, 0), "Press [OK] or Enter to continue") | |
21 message = TextWidget((0, 0), self.text, color=self.color) | |
22 width = max(cont.rect.width, message.rect.width) | |
23 if width > message.rect.width: | |
24 cont_pos = (self.rect.left + 5, | |
25 self.rect.top + 10 + message.rect.height) | |
26 message_pos = (self.rect.left + 5 + width / 2 | |
27 - message.rect.width / 2, self.rect.top + 5) | |
28 else: | |
29 message_pos = (self.rect.left + 5, self.rect.top + 5) | |
30 cont_pos = (self.rect.left + 5 + width / 2 | |
31 - message.rect.width / 2, | |
32 self.rect.top + 10 + message.rect.height) | |
33 message.rect.topleft = message_pos | |
34 cont.rect.topleft = cont_pos | |
35 self.add(message) | |
36 self.add(cont) | |
210
b92f705bd8ea
Modal dialogs, and focus stealing bugfixes
Stefano Rivera <stefano@rivera.za.net>
parents:
190
diff
changeset
|
37 self.ok_button = ok_button = TextButton((0, 0), 'OK') |
190 | 38 ok_pos = (self.rect.left + 5 + width / 2 - ok_button.rect.width / 2, |
39 cont_pos[1] + 5 + cont.rect.height) | |
40 ok_button.rect.topleft = ok_pos | |
41 ok_button.add_callback('clicked', self.close) | |
42 self.add(ok_button) | |
43 | |
44 def draw(self, surface): | |
45 background = pygame.Surface(self.rect.size, SRCALPHA) | |
46 background.fill(pygame.Color('gray')) | |
47 surface.blit(background, self.rect) | |
48 super(MessageBox, self).draw(surface) | |
49 | |
50 def close(self, ev, widget): | |
51 self.parent.remove(self) | |
52 if self.post_callback: | |
53 self.post_callback() | |
210
b92f705bd8ea
Modal dialogs, and focus stealing bugfixes
Stefano Rivera <stefano@rivera.za.net>
parents:
190
diff
changeset
|
54 |
b92f705bd8ea
Modal dialogs, and focus stealing bugfixes
Stefano Rivera <stefano@rivera.za.net>
parents:
190
diff
changeset
|
55 def grab_focus(self): |
b92f705bd8ea
Modal dialogs, and focus stealing bugfixes
Stefano Rivera <stefano@rivera.za.net>
parents:
190
diff
changeset
|
56 return self.ok_button.grab_focus() |