1# Copyright (c) 2005 VMware, Inc.
2#
3# Permission is hereby granted, free of charge, to any person obtaining a copy
4# of this software and associated documentation files (the "Software"), to deal
5# in the Software without restriction, including without limitation the rights
6# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7# copies of the Software, and to permit persons to whom the Software is
8# furnished to do so, subject to the following conditions:
9#
10# The above copyright notice and this permission notice shall be included in
11# all copies or substantial portions of the Software.
12#
13# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19# SOFTWARE.
20
21# Python translation from wrapLabel.{cc|h} by Gian Mario Tagliaretti
22
23import gtk
24import pango
25
26
27class WrapLabel(gtk.Label):
28    __gtype_name__ = 'WrapLabel'
29
30    def __init__(self, text=None):
31        gtk.Label.__init__(self)
32
33        self.__wrap_width = 0
34        self.layout = self.get_layout()
35        self.layout.set_wrap(pango.WRAP_WORD_CHAR)
36
37        if text is not None:
38            self.set_text(text)
39
40        self.set_alignment(0.0, 0.0)
41
42    def do_size_request(self, requisition):
43        layout = self.get_layout()
44        width, height = layout.get_pixel_size()
45        requisition.width = 0
46        requisition.height = height
47
48    def do_size_allocate(self, allocation):
49        gtk.Label.do_size_allocate(self, allocation)
50        self.__set_wrap_width(allocation.width)
51
52    def set_text(self, text):
53        gtk.Label.set_text(self, text)
54        self.__set_wrap_width(self.__wrap_width)
55
56    def set_markup(self, text):
57        gtk.Label.set_markup(self, text)
58        self.__set_wrap_width(self.__wrap_width)
59
60    def __set_wrap_width(self, width):
61        if width == 0:
62            return
63        layout = self.get_layout()
64        layout.set_width(width * pango.SCALE)
65        if self.__wrap_width != width:
66            self.__wrap_width = width
67            self.queue_resize()
68