~glitter-team/glitter/trunk

« back to all changes in this revision

Viewing changes to glitter/label.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__ = "Label widget" 
 
9
 
 
10
import gobject
 
11
import pango
 
12
import clutter
 
13
 
 
14
from container import Container
 
15
 
 
16
class Label(Container):
 
17
    """ 
 
18
    A label widget represents a text label which can be drawn in any pango 
 
19
    supported font, colored, ellipsized and line wrapped.
 
20
    
 
21
    """
 
22
    
 
23
    def __init__(self, text='Label', ellipsize=True, line_wrap=False):
 
24
        """ Initialize label """
 
25
        
 
26
        super(Label, self).__init__()
 
27
        
 
28
        self._text = text
 
29
        self._ellipsize = ellipsize
 
30
        self._line_wrap = line_wrap
 
31
        
 
32
        self._init_elements()
 
33
        self._update_style(self.style)
 
34
        
 
35
    
 
36
    def _init_elements(self):
 
37
        """ Initializes visual elements """
 
38
        
 
39
        # The clutter label
 
40
        self._label = clutter.Label()
 
41
        self._label.set_text(self.text)
 
42
        if self.ellipsize:
 
43
            self._label.set_ellipsize(pango.ELLIPSIZE_END)
 
44
        self._label.set_line_wrap(self.line_wrap)
 
45
        self.add(self._label)
 
46
        self._label.show()
 
47
    
 
48
    def _update_layout(self):
 
49
        """ Updates layout """
 
50
        
 
51
        super(Label, self)._update_layout()
 
52
        
 
53
        # Ajust text font size to height and calculate minimum width
 
54
        height = self.get_height()
 
55
        self._label.set_font_name(self.font + ' ' + str(height) + 'px')
 
56
        self.minimum_width = self._label.get_widthu()
 
57
        
 
58
        # Update the label to account for the new minimum width
 
59
        super(Label, self)._update_layout()
 
60
        
 
61
        # Clip the text
 
62
        width = self.get_width()
 
63
        self.set_clip(0, 0, width, height)
 
64
    
 
65
    def _update_style(self, props=None):
 
66
        """ Updates widget style """
 
67
        
 
68
        super(Label, self)._update_style(props)
 
69
        
 
70
        if not props:
 
71
            return
 
72
            
 
73
        for key, value in props:
 
74
            if key == 'font-name':
 
75
                self.font = value
 
76
            elif key == 'color':
 
77
                self.color = value
 
78
        
 
79
     # Properties
 
80
    
 
81
    def get_text(self):
 
82
        """ Retrieve label text
 
83
        
 
84
        return (str) -- label text
 
85
        """
 
86
        
 
87
        return self._text
 
88
        
 
89
    def set_text(self, text):
 
90
        """ Sets label text
 
91
        
 
92
        text (str) -- label text
 
93
        """
 
94
        
 
95
        self._text = text
 
96
        
 
97
    text = property(get_text, set_text)
 
98
    
 
99
    def get_font(self):
 
100
        """ Retrieve font
 
101
        
 
102
        return (str) -- font
 
103
        """
 
104
        
 
105
        return self._font
 
106
        
 
107
    def set_font(self, font):
 
108
        """ Sets font
 
109
        
 
110
        font (str) -- font name
 
111
        """
 
112
        
 
113
        self._font = font
 
114
        height = self.get_height()
 
115
        self._label.set_font_name(self._font + ' ' + str(height) + 'px')
 
116
        
 
117
    font = property(get_font, set_font)
 
118
    
 
119
    def get_color(self):
 
120
        """ Retrieve color
 
121
        
 
122
        return (int, int, int, int) -- color in rgba format
 
123
        """
 
124
        
 
125
        return self._color
 
126
        
 
127
    def set_color(self, color):
 
128
        """ Sets color
 
129
        
 
130
        color (int, int, int, int) -- color in rgba format
 
131
        """
 
132
        
 
133
        self._color = color
 
134
        self._label.set_color(self._color)
 
135
        
 
136
    color = property(get_color, set_color)
 
137
    
 
138
    def get_ellipsize(self):
 
139
        """ Retrieve whether text is to be ellipsized
 
140
        
 
141
        return (bool) -- ellipsize
 
142
        """
 
143
        
 
144
        return self._ellipsize
 
145
        
 
146
    def set_ellipsize(self, ellipsize):
 
147
        """ Sets whether text is to be ellipsized
 
148
        
 
149
        ellipsize (bool) -- ellipsize
 
150
        """
 
151
        
 
152
        self._ellipsize = ellipsize
 
153
        if ellipsize:
 
154
            self._label.set_ellipsize(pango.ELLIPSIZE_END)
 
155
        
 
156
    ellipsize = property(get_ellipsize, set_ellipsize)
 
157
 
 
158
    def get_line_wrap(self):
 
159
        """ Retrieve whether text is to be line wrapped
 
160
        
 
161
        return (bool) -- line wrap
 
162
        """
 
163
        
 
164
        return self._line_wrap
 
165
           
 
166
    def set_line_wrap(self, line_wrap):
 
167
        """ Sets whether text is to be line wrapped
 
168
        
 
169
        line_wrap (bool) -- line wrap
 
170
        """
 
171
        
 
172
        self._line_wrap = line_wrap
 
173
        self._label.set_line_wrap(line_wrap)
 
174
        
 
175
    line_wrap = property(get_line_wrap, set_line_wrap)       
 
176