~glitter-team/glitter/trunk

« back to all changes in this revision

Viewing changes to glitter/box.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__ = "Horizontal and vertical box widgets" 
 
9
 
 
10
import gobject
 
11
import pango
 
12
import clutter
 
13
 
 
14
from container import Container
 
15
 
 
16
class Box(Container):
 
17
    """ 
 
18
    A box is a special type of container for displaying multiple children in 
 
19
    an horizontal or vertical layout.
 
20
    
 
21
    """
 
22
    
 
23
    def __init__(self):
 
24
        """ Initialize box """
 
25
        
 
26
        super(Box, self).__init__()
 
27
        
 
28
        self._spacing = 0.0
 
29
        self._alignment = 1
 
30
        self.packed_children = []
 
31
        
 
32
        self._update_style(self.style)
 
33
        
 
34
    def _update_style(self, props=None):
 
35
        """ Updates style """
 
36
        
 
37
        super(Box, self)._update_style(props)
 
38
        
 
39
        for key, value in props:
 
40
            if key == 'spacing':
 
41
                self.spacing = value
 
42
            elif key == 'alignment':
 
43
                self.alignment = value
 
44
                
 
45
    def _update_layout(self):
 
46
        """ Update layout """
 
47
        
 
48
        super(Box, self)._update_layout()          
 
49
                   
 
50
    def pack(self, child):
 
51
        """ Packs a child into the box """
 
52
        
 
53
        self.packed_children.append(child)
 
54
        self.add(child)
 
55
                            
 
56
    def get_spacing(self):
 
57
        """ Retrieve child spacing """
 
58
        
 
59
        return self._spacing
 
60
        
 
61
    def set_spacing(self, value):
 
62
        """ Sets child spacing
 
63
        
 
64
        value -- (float) Spacing between children
 
65
        """
 
66
        
 
67
        self._spacing = value
 
68
                
 
69
    spacing = property(get_spacing, set_spacing)
 
70
        
 
71
    def get_alignment(self):
 
72
        """ Retrieve alignment """
 
73
        
 
74
        return self._alignment
 
75
        
 
76
    def set_alignment(self, value):
 
77
        """ Sets children alignment 
 
78
        
 
79
        value -- (str) [ALINGMENT.START, ALIGNMENT.END, ALIGNMENT.CENTER]
 
80
        """
 
81
        
 
82
        self._alignment = value
 
83
        
 
84
    alignment = property(get_alignment, set_alignment)
 
85
        
 
86
           
 
87
class HBox(Box):
 
88
    """ An HBox layouts children in an horizontal fashion """
 
89
    
 
90
    def __init__(self):
 
91
        """ Initialize horizontal box """
 
92
        
 
93
        super(HBox, self).__init__()
 
94
        
 
95
        self._update_style(self.style)
 
96
        
 
97
    def _update_style(self, props=None):
 
98
        """ Updates style """
 
99
        
 
100
        super(HBox, self)._update_style(props)
 
101
        
 
102
    def _update_layout(self):
 
103
        """ Updates layout """
 
104
        
 
105
        super(HBox, self)._update_layout()
 
106
        
 
107
        fixed_width_children = []
 
108
        
 
109
        for child in self.packed_children:
 
110
            if child.size_ratio > 0:
 
111
                fixed_width_children.append(child)
 
112
        
 
113
        current_x = 0.0
 
114
        for child in self.packed_children:         
 
115
            child.set_natural_x(current_x)
 
116
            if child not in fixed_width_children:
 
117
                child_width = 1.0 / len(self.packed_children) - \
 
118
                  len(fixed_width_children)
 
119
                child.set_natural_width(child_width)
 
120
            child._update_layout()    
 
121
            current_x += child.get_widthu() * 1.0 / self.get_widthu()
 
122
            current_x += self.spacing
 
123
            
 
124