Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pgu/gui/document.py
blob: 1bd96df20b700866e5f8c3304fbd41341b216904 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
"""
import pygame

import container
import layout

class _document_widget:
    def __init__(self,w,align=None):
        #w.rect.w,w.rect.h = w.resize()
        #self.rect = w.rect
        self.widget = w
        if align != None: self.align = align

class Document(container.Container):
    """A document container contains many widgets strung together in a document format.  (How informative!)
    
    <pre>Document()</pre>
    
    """
    def __init__(self,**params):
        params.setdefault('cls','document')
        container.Container.__init__(self,**params)
        self.layout =  layout.Layout(pygame.Rect(0,0,self.rect.w,self.rect.h))
    
    def add(self,e,align=None):
        """Add a widget.
        
        <pre>Document.add(e,align=None)</pre>
        
        <dl>
        <dt>e<dd>widget
        <dt>align<dd>alignment (None,-1,0,1)
        </dl>
        """
        dw = _document_widget(e,align)
        self.layout.add(dw)
        e.container = self
        e._c_dw = dw
        self.widgets.append(e)
        self.chsize()
        
    def remove(self,e):
        self.layout._widgets.remove(e._c_dw)
        self.widgets.remove(e)
        self.chsize()
        
    
    def block(self,align):
        """Start a new block.
        
        <pre>Document.block(align)</pre>
        
        <dl>
        <dt>align<dd>alignment of block (-1,0,1)
        </dl>
        """
        self.layout.add(align)
    
    def space(self,e):
        """Add a spacer.
        
        <pre>Document.space(e)</pre>
        
        <dl>
        <dt>e<dd>a (w,h) size for the spacer
        </dl>
        """
        self.layout.add(e)
    
    def br(self,height):
        """Add a line break.
        
        <pre>Document.br(height)</pre>
        
        <dl>
        <dt>height<dd>height of line break
        </dl>
        """
        self.layout.add((0,height))
    
    def resize(self,width=None,height=None):
        if self.style.width: width = self.style.width
        if self.style.height: height = self.style.height
        
        for w in self.widgets:
            w.rect.w,w.rect.h = w.resize()
            
            if (width != None and w.rect.w > width) or (height != None and w.rect.h > height):
                w.rect.w,w.rect.h = w.resize(width,height)
            
            dw = w._c_dw
            dw.rect = pygame.Rect(0,0,w.rect.w,w.rect.h)
        
        if width == None: width = 65535
        self.layout.rect = pygame.Rect(0,0,width,0)
        self.layout.resize()
        
        _max_w = 0
        
        for w in self.widgets:
            #xt,xl,xb,xr = w.getspacing()
            dw = w._c_dw
            w.style.x,w.style.y,w.rect.w,w.rect.h = dw.rect.x,dw.rect.y,dw.rect.w,dw.rect.h
            #w.resize()
            w.rect.x,w.rect.y = w.style.x,w.style.y
            _max_w = max(_max_w,w.rect.right)
        
        #self.rect.w = _max_w #self.layout.rect.w
        #self.rect.h = self.layout.rect.h
        #print 'document',_max_w,self.layout.rect.h
        return _max_w,self.layout.rect.h