cc24089a90963d5e906235689e30e06f5a318b81
[minwii.git] / src / pgu / gui / keysym.py
1 """
2 """
3 import pygame
4 from pygame.locals import *
5
6 from const import *
7 import widget
8
9 class Keysym(widget.Widget):
10 """A keysym input.
11
12 <p>This widget records the keysym of the key pressed while this widget is in focus.</p>
13
14 <pre>Keysym(value=None)</pre>
15
16 <dl>
17 <dt>value<dd>initial keysym, see <a href="http://www.pygame.org/docs/ref/key.html">pygame keysyms</a> </dl>
18
19 <strong>Example</strong>
20 <code>
21 w = Input(value=pygame.locals.K_g)
22
23 w = Input(pygame.locals.K_g)
24
25 w = Input()
26 </code>
27
28 """
29
30 def __init__(self,value=None,**params):
31 params.setdefault('cls','keysym')
32 widget.Widget.__init__(self,**params)
33 self.value = value
34
35 self.font = self.style.font
36 w,h = self.font.size("Right Super") #"Right Shift")
37 self.style.width,self.style.height = w,h
38 #self.rect.w=w+self.style.padding_left+self.style.padding_right
39 #self.rect.h=h+self.style.padding_top+self.style.padding_bottom
40
41 def event(self,e):
42 used = None
43 if e.type == FOCUS or e.type == BLUR: self.repaint()
44 elif e.type == KEYDOWN:
45 if e.key != K_TAB:
46 self.value = e.key
47 self.repaint()
48 self.send(CHANGE)
49 used = True
50 self.next()
51 self.pcls = ""
52 if self.container.myfocus is self: self.pcls = "focus"
53 return used
54
55 def paint(self,s):
56 r = pygame.rect.Rect(0,0,self.rect.w,self.rect.h)
57 #render_box(s,self.style.background,r)
58 if self.value == None: return
59 name = ""
60 for p in pygame.key.name(self.value).split(): name += p.capitalize()+" "
61 #r.x = self.style.padding_left;
62 #r.y = self.style.padding_bottom;
63 s.blit(self.style.font.render(name, 1, self.style.color), r)
64
65 def __setattr__(self,k,v):
66 if k == 'value' and v != None:
67 v = int(v)
68 _v = self.__dict__.get(k,NOATTR)
69 self.__dict__[k]=v
70 if k == 'value' and _v != NOATTR and _v != v:
71 self.send(CHANGE)
72 self.repaint()