Ajout du mode débutant.
[minwii.git] / src / pgu / gui / document.py
1 """
2 """
3 import pygame
4
5 import container
6 import layout
7
8 class _document_widget:
9 def __init__(self,w,align=None):
10 #w.rect.w,w.rect.h = w.resize()
11 #self.rect = w.rect
12 self.widget = w
13 if align != None: self.align = align
14
15 class Document(container.Container):
16 """A document container contains many widgets strung together in a document format. (How informative!)
17
18 <pre>Document()</pre>
19
20 """
21 def __init__(self,**params):
22 params.setdefault('cls','document')
23 container.Container.__init__(self,**params)
24 self.layout = layout.Layout(pygame.Rect(0,0,self.rect.w,self.rect.h))
25
26 def add(self,e,align=None):
27 """Add a widget.
28
29 <pre>Document.add(e,align=None)</pre>
30
31 <dl>
32 <dt>e<dd>widget
33 <dt>align<dd>alignment (None,-1,0,1)
34 </dl>
35 """
36 dw = _document_widget(e,align)
37 self.layout.add(dw)
38 e.container = self
39 e._c_dw = dw
40 self.widgets.append(e)
41 self.chsize()
42
43 def remove(self,e):
44 self.layout._widgets.remove(e._c_dw)
45 self.widgets.remove(e)
46 self.chsize()
47
48
49 def block(self,align):
50 """Start a new block.
51
52 <pre>Document.block(align)</pre>
53
54 <dl>
55 <dt>align<dd>alignment of block (-1,0,1)
56 </dl>
57 """
58 self.layout.add(align)
59
60 def space(self,e):
61 """Add a spacer.
62
63 <pre>Document.space(e)</pre>
64
65 <dl>
66 <dt>e<dd>a (w,h) size for the spacer
67 </dl>
68 """
69 self.layout.add(e)
70
71 def br(self,height):
72 """Add a line break.
73
74 <pre>Document.br(height)</pre>
75
76 <dl>
77 <dt>height<dd>height of line break
78 </dl>
79 """
80 self.layout.add((0,height))
81
82 def resize(self,width=None,height=None):
83 if self.style.width: width = self.style.width
84 if self.style.height: height = self.style.height
85
86 for w in self.widgets:
87 w.rect.w,w.rect.h = w.resize()
88
89 if (width != None and w.rect.w > width) or (height != None and w.rect.h > height):
90 w.rect.w,w.rect.h = w.resize(width,height)
91
92 dw = w._c_dw
93 dw.rect = pygame.Rect(0,0,w.rect.w,w.rect.h)
94
95 if width == None: width = 65535
96 self.layout.rect = pygame.Rect(0,0,width,0)
97 self.layout.resize()
98
99 _max_w = 0
100
101 for w in self.widgets:
102 #xt,xl,xb,xr = w.getspacing()
103 dw = w._c_dw
104 w.style.x,w.style.y,w.rect.w,w.rect.h = dw.rect.x,dw.rect.y,dw.rect.w,dw.rect.h
105 #w.resize()
106 w.rect.x,w.rect.y = w.style.x,w.style.y
107 _max_w = max(_max_w,w.rect.right)
108
109 #self.rect.w = _max_w #self.layout.rect.w
110 #self.rect.h = self.layout.rect.h
111 #print 'document',_max_w,self.layout.rect.h
112 return _max_w,self.layout.rect.h