~ubuntu-branches/ubuntu/karmic/sugar-web-activity/karmic

« back to all changes in this revision

Viewing changes to Web.activity/linkbutton.py

  • Committer: Bazaar Package Importer
  • Author(s): Jani Monoses
  • Date: 2008-01-31 15:17:08 UTC
  • Revision ID: james.westby@ubuntu.com-20080131151708-4tegqfae3vdhiylk
Tags: upstream-85
ImportĀ upstreamĀ versionĀ 85

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007, One Laptop Per Child
 
2
#
 
3
# This library is free software; you can redistribute it and/or
 
4
# modify it under the terms of the GNU Lesser General Public
 
5
# License as published by the Free Software Foundation; either
 
6
# version 2 of the License, or (at your option) any later version.
 
7
#
 
8
# This library is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
# Lesser General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU Lesser General Public
 
14
# License along with this library; if not, write to the
 
15
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
16
# Boston, MA 02111-1307, USA.
 
17
 
 
18
import gtk
 
19
import os
 
20
import gobject
 
21
from gettext import gettext as _
 
22
import rsvg
 
23
import re
 
24
import gc
 
25
import pango
 
26
 
 
27
from sugar.graphics.palette import Palette
 
28
from sugar.graphics.tray import TrayButton
 
29
from sugar.graphics.icon import Icon
 
30
from sugar.graphics import style
 
31
 
 
32
 
 
33
class LinkButton(TrayButton, gobject.GObject):
 
34
    __gtype_name__ = 'LinkButton'
 
35
    __gsignals__ = {
 
36
        'remove_link': (gobject.SIGNAL_RUN_FIRST,
 
37
                        gobject.TYPE_NONE, ([str]))
 
38
        }
 
39
    def __init__(self, url, buffer, color, title, owner, index, hash):
 
40
        TrayButton.__init__(self)
 
41
        self.set_image(buffer, color.split(',')[1], color.split(',')[0])
 
42
 
 
43
        self.hash = hash
 
44
        info = title +'\n'+ owner     
 
45
        self.setup_rollover_options(info)        
 
46
        
 
47
    def set_image(self, buffer, fill='#0000ff', stroke='#4d4c4f'):
 
48
        img = gtk.Image()                    
 
49
        loader = gtk.gdk.PixbufLoader()
 
50
        loader.write(buffer)
 
51
        loader.close()
 
52
        pixbuf = loader.get_pixbuf()
 
53
        del loader            
 
54
 
 
55
        xo_buddy = os.path.join(os.path.dirname(__file__), "icons/link.svg")
 
56
        pixbuf_bg = self._read_link_background(xo_buddy, fill, stroke)
 
57
        pixbuf_bg = pixbuf_bg.scale_simple(style.zoom(120),
 
58
                                           style.zoom(110),
 
59
                                           gtk.gdk.INTERP_BILINEAR)        
 
60
        dest_x = style.zoom(10) 
 
61
        dest_y = style.zoom(20) 
 
62
        w = pixbuf.get_width()
 
63
        h = pixbuf.get_height() 
 
64
        scale_x = 1
 
65
        scale_y = 1
 
66
        
 
67
        pixbuf.composite(pixbuf_bg, dest_x, dest_y, w, h, dest_x, dest_y,
 
68
                         scale_x, scale_y, gtk.gdk.INTERP_BILINEAR, 255)
 
69
        img.set_from_pixbuf(pixbuf_bg)
 
70
        self.set_icon_widget(img)
 
71
        img.show()
 
72
        del pixbuf
 
73
        del pixbuf_bg
 
74
        gc.collect()
 
75
 
 
76
    def _read_link_background(self, filename, fill_color, stroke_color):
 
77
        icon_file = open(filename, 'r')
 
78
        data = icon_file.read()
 
79
        icon_file.close()
 
80
    
 
81
        if fill_color:
 
82
            entity = '<!ENTITY fill_color "%s">' % fill_color
 
83
            data = re.sub('<!ENTITY fill_color .*>', entity, data)
 
84
        
 
85
        if stroke_color:
 
86
            entity = '<!ENTITY stroke_color "%s">' % stroke_color
 
87
            data = re.sub('<!ENTITY stroke_color .*>', entity, data)
 
88
 
 
89
        data_size = len(data)
 
90
        return rsvg.Handle(data=data).get_pixbuf()
 
91
 
 
92
    def setup_rollover_options(self, info):
 
93
        palette = Palette(info, text_maxlen=50)
 
94
        self.set_palette(palette)
 
95
 
 
96
        menu_item = gtk.MenuItem(_('Remove'))
 
97
        menu_item.connect('activate', self.item_remove_cb)
 
98
        palette.menu.append(menu_item)
 
99
        menu_item.show()
 
100
 
 
101
    def item_remove_cb(self, widget):
 
102
        self.emit('remove_link', self.hash)