~glitter-team/glitter/trunk

« back to all changes in this revision

Viewing changes to glitter/container.py

  • Committer: Jan Jokela
  • Date: 2008-12-10 22:18:59 UTC
  • Revision ID: janjokela@gmail.com-20081210221859-zxr2ut255a7xu15x
Hi, Glitter here

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# !/usr/bin/python
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
# Glitter Toolkit
 
5
 
 
6
__authors__ = ["Jan Jokela <janjokela@gmail.com>"]
 
7
__licenses__ = ["LICENSE.LGPL"]
 
8
__description__ = "Container widget"
 
9
 
 
10
from widget import Widget
 
11
 
 
12
POLICY_NONE = 'POLICY_NONE'
 
13
POLICY_EXPAND = 'POLICY_EXPAND'
 
14
POLICY_MINIMUM = 'POLICY_MINIMUM'
 
15
 
 
16
class Container(Widget):
 
17
    """
 
18
    A container widget provides a representation of a graphical  container 
 
19
    that not only is intended as a placeholder for children but also as a base 
 
20
    class for other container-type widgets.
 
21
    
 
22
    It offers powerfull layout capabilities through the usage of minimum and 
 
23
    natural sizes and positions. It also features resolution independence 
 
24
    through the use of relative values, millimeters and clutter units.
 
25
    
 
26
    """ 
 
27
    
 
28
    def __init__(self):
 
29
        """ Initialize the container """
 
30
        
 
31
        super(Container, self).__init__()
 
32
        
 
33
        self._natural_width = 0.0
 
34
        self._natural_height = 0.0
 
35
        self._minimum_width = 0.0
 
36
        self._minimum_height = 0.0
 
37
        self._natural_x = 0.0
 
38
        self._natural_y = 0.0
 
39
        self._minimum_x = 0
 
40
        self._minimum_y = 0
 
41
        
 
42
        self._size_ratio = 0.0
 
43
        
 
44
    def _update_style(self, props=None):
 
45
        """ Update style """
 
46
        
 
47
        super(Container, self)._update_style(props)
 
48
        
 
49
        if not props:
 
50
            return
 
51
        
 
52
        for key, value in props:
 
53
            if key == 'natural-width':
 
54
                self.natural_width = value
 
55
            elif key == 'natural-height':
 
56
                self.natural_height = value
 
57
            elif key == 'natural-width':
 
58
                self.natural_width = value
 
59
            elif key == 'natural-height':
 
60
                self.natural_height = value
 
61
            elif key == 'natural-x': 
 
62
                self.natural_x = value
 
63
            elif key == 'natural-y':
 
64
                self.natural_y = value
 
65
            elif key == 'minimum-x':
 
66
                self.minimum_x = value
 
67
            elif key == 'minimum-y':
 
68
                self.minimum_y = value
 
69
                
 
70
        
 
71
    def _update_layout(self):
 
72
        """ Updates layout """
 
73
        
 
74
        # Natural an minimum sizes
 
75
        n_width = int(self.natural_width * self.get_parent().get_widthu())
 
76
        n_height = int(self.natural_height * self.get_parent().get_heightu())
 
77
        
 
78
        m_width = self.minimum_width
 
79
        m_height = self.minimum_height
 
80
        
 
81
        # Preferred size is the natural one, except if lower than minimum
 
82
        if n_width >= m_width:
 
83
            tmp_width = n_width
 
84
        else:
 
85
            tmp_width = m_width
 
86
        if n_height >= m_height:
 
87
            tmp_height = n_height
 
88
        else:
 
89
            tmp_height = m_height
 
90
            
 
91
        # Account for size ratios
 
92
        if self.size_ratio > 0:
 
93
            tmp_width = int(self.size_ratio * tmp_height)
 
94
        elif self.size_ratio < 0:
 
95
            tmp_height = int(self.size_ratio * tmp_width)
 
96
        
 
97
        # Set size
 
98
        self.set_widthu(int(tmp_width))
 
99
        self.set_heightu(int(tmp_height))
 
100
 
 
101
        # Natural and minimum positions
 
102
        n_x = int(self.natural_x * self.get_parent().get_widthu())
 
103
        n_y = int(self.natural_y * self.get_parent().get_heightu())
 
104
        
 
105
        m_x = self.minimum_x
 
106
        m_y = self.minimum_y
 
107
        
 
108
        if n_x >= m_x:
 
109
            tmp_x = n_x
 
110
        else:
 
111
            tmp_x = m_x
 
112
        if n_y >= m_y:
 
113
            tmp_y = n_y
 
114
        else:
 
115
            tmp_y = m_y
 
116
            
 
117
        self.set_xu(tmp_x)
 
118
        self.set_yu(tmp_y)
 
119
        
 
120
    def get_natural_width(self):
 
121
        """ Retrieve natural width """
 
122
        
 
123
        return self._natural_width
 
124
        
 
125
    def set_natural_width(self, value):
 
126
        """ Sets natural width
 
127
        
 
128
        value -- (float) Width as a factor of parent width
 
129
        """
 
130
    
 
131
        self._natural_width = value
 
132
      
 
133
    natural_width = property(get_natural_width, set_natural_width)
 
134
    
 
135
    def get_natural_height(self):
 
136
        """ Retrieve natural height """
 
137
        
 
138
        return self._natural_height
 
139
 
 
140
    def set_natural_height(self, value):
 
141
        """ Sets natural height
 
142
        
 
143
        value -- (float) Height as a factor of parent height
 
144
        """
 
145
        
 
146
        self._natural_height = value
 
147
        
 
148
    natural_height = property(get_natural_height, set_natural_height)
 
149
 
 
150
    def get_minimum_width(self):
 
151
        """ Retrieve minimum width """
 
152
    
 
153
        return self._minimum_width
 
154
        
 
155
    def set_minimum_width(self, value):
 
156
        """ Sets minimum width 
 
157
        
 
158
        value -- (int) Minimum width in clutter units
 
159
        """
 
160
    
 
161
        self._minimum_width = value
 
162
        
 
163
    minimum_width = property(get_minimum_width, set_minimum_width)
 
164
        
 
165
    def get_minimum_height(self):
 
166
        """ Retrieve minimum height """
 
167
    
 
168
        return self._minimum_height
 
169
        
 
170
    def set_minimum_height(self, value):
 
171
        """ Sets minimum height 
 
172
        
 
173
        value -- (int) Minimum height in clutter units
 
174
        """
 
175
    
 
176
        self._minimum_height = value
 
177
        
 
178
    minimum_height = property(get_minimum_height, set_minimum_height)
 
179
 
 
180
    def get_size_ratio(self):
 
181
        """ Retrieve size ratio """
 
182
        
 
183
        return self._size_ratio
 
184
        
 
185
    def set_size_ratio(self, value):
 
186
        """ Sets size ratio
 
187
        
 
188
        value -- (float) Size ratio. Positive values for width as a factor of 
 
189
                 height. Negative values for height as a factor of width.
 
190
        """
 
191
    
 
192
        self._size_ratio = value
 
193
        
 
194
    size_ratio = property(get_size_ratio, set_size_ratio)
 
195
 
 
196
    def get_width_policy(self):
 
197
        """ Retrieve width policy """
 
198
        
 
199
        return self._width_policy
 
200
        
 
201
    def set_width_policy(self, value):
 
202
        """ Sets width policy 
 
203
        
 
204
        value -- (str) NATURAL or MINIMUM width
 
205
        """
 
206
        
 
207
        self._width_policy = value
 
208
        
 
209
    width_policy = property(get_width_policy, set_width_policy)
 
210
    
 
211
    def get_height_policy(self):
 
212
        """ Retrieve height policy """
 
213
        
 
214
        return self._height_policy
 
215
        
 
216
    def set_height_policy(self, value):
 
217
        """ Sets height policy
 
218
        
 
219
        value -- (str) NATURAL or MINIMUM height
 
220
        """
 
221
        
 
222
        self._height_policy = value
 
223
        
 
224
    height_policy = property(get_height_policy, set_height_policy)    
 
225
    
 
226
    def get_natural_x(self):
 
227
        """ Retrieve minimum x """
 
228
        
 
229
        return self._natural_x
 
230
        
 
231
    def set_natural_x(self, value):
 
232
        """ Sets natural x
 
233
        
 
234
        value -- (float) X position as a factor of parent width
 
235
        """
 
236
        
 
237
        self._natural_x = value
 
238
    
 
239
    natural_x = property(get_natural_x, set_natural_x)
 
240
    
 
241
    def get_natural_y(self):
 
242
        """ Retrieve natural y """
 
243
        
 
244
        return self._natural_y
 
245
        
 
246
    def set_natural_y(self, value):
 
247
        """ Sets natural y
 
248
        
 
249
        value -- (float) Y as a factor of parent height
 
250
        """
 
251
        
 
252
        self._natural_y = value 
 
253
        
 
254
    natural_y = property(get_natural_y, set_natural_y)
 
255
    
 
256
    def get_minimum_x(self):
 
257
        """ Retrieve minimum y """
 
258
        
 
259
        return self._minimum_y
 
260
        
 
261
    def set_minimum_x(self, value):
 
262
        """ Sets minimum x
 
263
        
 
264
        value -- (float) Minimum x in clutter units
 
265
        """
 
266
        
 
267
        self._minimum_x = value
 
268
        
 
269
    minimum_x = property(get_minimum_x, set_minimum_x)
 
270
    
 
271
    def get_minimum_y(self):
 
272
        """ Retrieve minimum y """
 
273
        
 
274
        return self._minimum_y
 
275
        
 
276
    def set_minimum_y(self, value):
 
277
        """ Sets minimum y
 
278
        
 
279
        value -- (float) Minimum y in clutter units
 
280
        """
 
281
        
 
282
        self._minimum_y = value
 
283
        
 
284
    minimum_y = property(get_minimum_y, set_minimum_y)