File: SmileyRenderer.py

package info (click to toggle)
emesene 1.0-dist-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 4,596 kB
  • ctags: 3,006
  • sloc: python: 25,171; makefile: 14; sh: 1
file content (456 lines) | stat: -rw-r--r-- 18,262 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# -*- coding: utf-8 -*-

#   This file is part of emesene.
#
#    Emesene is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    emesene is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with emesene; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import sys

import gtk
import pango
import cairo
import gtk.gdk
import gobject
from gobject import PARAM_READWRITE, TYPE_PYOBJECT

from Parser import Smiley
from emesenelib.ContactData import Contact, Group


class SmileyRenderer(gtk.GenericCellRenderer):
    '''Cellrenderer with smiley support. '''

    __gproperties__ = {
        'markup': (TYPE_PYOBJECT, 'Smiley markup',
        'A list with strs, Smileys, and \ns', PARAM_READWRITE),
        'obj': (TYPE_PYOBJECT, 'Smiley markup',
        'A list with strs, Smileys, and \ns', PARAM_READWRITE),
        'ellipsize': (bool, 'Ellipsize True/False', 
        '',False, gobject.PARAM_READWRITE),
        'wrap': (bool, 'Wrap True/False', '',False, gobject.PARAM_READWRITE),
    }

    def __init__(self, cache = {}):
        self.__gobject_init__()
        self._markup = ['']
        self._ellipsize = True
        self._wrap = True
        self._cached_markup = {}
        self._cached_layout = {}
        self._style_handler_id = None
        self.xpad = 2
        self.ypad = 1
        self._selected_flgs = (int(gtk.CELL_RENDERER_SELECTED), \
                int(gtk.CELL_RENDERER_SELECTED) + int(gtk.CELL_RENDERER_PRELIT))
        
    def do_get_property(self, prop):
        return getattr(self, prop.name)

    def do_set_property(self, prop, value):
        setattr(self, prop.name, value)

    def on_render(self, win, widget, bgnd_area, cell_area, expose_area, flags):
        '''Called by gtk to render the cell.'''
        x, y, width, height = cell_area
        x += self.xpad
        y += self.ypad
        width -= self.xpad
        ctx = win.cairo_create()        
        layout = self._get_layout(widget)
        layout.set_width(width  * pango.SCALE)
        layout.set_in_color_override_mode(flags in self._selected_flgs)            
        layout.draw(ctx, (x, y, width, height))
        
    def on_get_size(self, widget, cell_area):
        '''Returns the size of the cellrenderer'''
        if not self._style_handler_id:
            self._style_handler_id = widget.connect('style-set', self._style_set)
        layout = self._get_layout(widget)
        width, height = layout.get_pixel_size()
        return (0,0, -1, height + (self.ypad * 2))

    def _get_layout(self, widget):
        if type(self.obj) == Contact: 
            obj_id = self.obj.email
        elif type(self.obj) == Group: 
            obj_id = self.obj.id
        else: 
            obj_id = ''    
        if obj_id in self._cached_markup and \
                    self._cached_markup[obj_id] == self.markup:
            layout = self._cached_layout[obj_id]
        else:
            layout = SmileyLayout(widget.create_pango_context(), 
                                  self.markup,
                                  widget.style.text[gtk.STATE_NORMAL],
                                  widget.style.text[gtk.STATE_SELECTED]) 
            layout.set_ellipsize(pango.ELLIPSIZE_END)
            self._cached_markup[obj_id] = self.markup
            self._cached_layout[obj_id] = layout
        return layout

    def _style_set(self, widget, previous_style):
        self._cached_markup = {}
        self._cached_layout = {}        
        widget.queue_resize()



class SmileyLabel(gtk.Widget):
    '''Label with smiley support. '''

    __gsignals__ = { 'size_request' : 'override',
                     'size-allocate' : 'override', 
                     'expose-event' : 'override'}
            
    def __init__(self, smiley_theme, style):
        gtk.Widget.__init__(self)
        self._text = ['']     
        self._ellipsize = True
        self._wrap = True
        self._smiley_layout = None            
        self.set_flags(self.flags() | gtk.NO_WINDOW)
        self._smiley_layout = SmileyLayout(self.create_pango_context())

    def set_ellipsize(self, ellipsize):
        ''' Sets the ellipsize behavior '''
        self._ellipsize = ellipsize   
        self.queue_resize()

    def set_wrap(self, wrap):
        ''' Sets the wrap behavior '''
        self._wrap = wrap
        self.queue_resize()

    def set_markup(self, text=['']):
        self.set_text(text)

    def set_text(self, text=['']):
        ''' Sets widget text '''
        self._text = text
        self.setup_smiley_layout()
        self.queue_resize()
                
    def set_smiley_scaling(self, smiley_scaling):
        self._smiley_layout.set_smiley_scaling(smiley_scaling)
        self.queue_resize()
        
    def setup_smiley_layout(self):
        self._smiley_layout.set_element_list(self._text)

    def do_realize(self):
        gtk.Widget.do_realize(self)
        self.set_flags(self.flags() | gtk.REALIZED)
        self.window = self.get_parent().window        

    def do_style_set(self, prev_style):
        self._smiley_layout.set_colors(self.style.text[gtk.STATE_NORMAL])
        self.queue_draw()

    def do_size_request(self, requisition):             
        self._smiley_layout.set_width(-1)
        width, height = self._smiley_layout.get_pixel_size()
        requisition.height = height
        if self._ellipsize or self._wrap:
            requisition.width = 0
        else: 
            requisition.width = width

    def do_size_allocate(self, allocation):
        if not (self._ellipsize or self._wrap):
            self._smiley_layout.set_width(-1)
            width, height = self._smiley_layout.get_pixel_size()
            self.set_size_request(width, height)
        else:
            if self._ellipsize: 
                self._smiley_layout.set_ellipsize(pango.ELLIPSIZE_END)
            else: 
                self._smiley_layout.set_ellipsize(pango.ELLIPSIZE_NONE)
            self._smiley_layout.set_width(allocation.width * pango.SCALE)
            self.set_size_request(-1, self._smiley_layout.get_pixel_size()[1])
        gtk.Widget.do_size_allocate(self, allocation)
                        
    def do_expose_event(self, event):
        area = self.get_allocation()
        ctx = event.window.cairo_create()    
        self._smiley_layout.draw(ctx, area)
gobject.type_register(SmileyLabel)



class SmileyLayout(pango.Layout):

    def __init__(self, context, 
                 parsed_elements_list = [''], 
                 color = gtk.gdk.Color(), 
                 override_color = gtk.gdk.Color(),
                 scaling=1.0):                    
        pango.Layout.__init__(self, context)
        self._width = -1
        self._ellipsize = True
        self._elayout = pango.Layout(context)
        self._elayout.set_text('___') #…
        self._elayout.set_ellipsize(pango.ELLIPSIZE_END)
        self._elayout.set_width(0)
        self._in_override = False # color override mode, used for selected text
        self._base_to_center = 0
        self._text_height = 0
        self._smilies = {} # key: (index_pos), value(pixbuf)
        self._base_attrlist = None # no color             
        self._attrlist = None # with color
        self._override_attrlist = None # with override color        
        self._color = color
        self._override_color = override_color
        self._smilies_scaled = {} # key: (index_pos), value(pixbuf)
        self._scaling = scaling # relative to ascent + desent, -1 for natural
        self._is_rtl = False
                
        self.set_element_list(parsed_elements_list)
        self._update_layout()

    def set_element_list(self, parsed_elements_list=['']):
        ''' Sets Layout Text based on parsed elements '''
        self._update_base(parsed_elements_list)
    
    def set_text(self, text):
        ''' Sets Layout Text '''
        self.set_element_list(text)
    
    def set_markup(self, markup):
        ''' Same as set_text() '''
        self.set_element_list(text)
        
    def set_width(self, width):
        ''' Set width of layout in pixels, -1 for natural width '''
        self._width = width
        self._update_layout()
        
    def get_width(self):
        return self._width 
           
    def set_ellipsize(self, value):
        ''' Turns Ellipsize ON/OFF '''
        if value == pango.ELLIPSIZE_END:
            self._ellipsize = True
        else:
            self._ellipsize = False
        self._update_layout()
        
    def set_smiley_scaling(self, smiley_scaling):
        '''
        Set smiley scalling relative to ascent + desent, 
        -1 for natural size
        '''
        self._scaling = smiley_scaling
        self._update_smilies()
        
    def set_in_color_override_mode(self, in_override):
        if not in_override == self._in_override:
            self._in_override = in_override
            self._update_attributes()

    def set_colors(self, color = gtk.gdk.Color(), 
                 override_color = gtk.gdk.Color()):
        self._color = color
        self._override_color = override_color
        self._update_attrlists()

    def _update_base(self, elements_list=['']):
        self._smilies = {}
        self._base_attrlist = pango.AttrList()
        text = ''
        if type(elements_list) in (str, unicode): 
            elements_list = [elements_list]
        for element in elements_list:
            if type(element) in (str, unicode):
                try:
                    attrl, ptxt, ac = pango.parse_markup(str(element), u'\x00')
                except:
                    attrl, ptxt = pango.AttrList(), str(element)
                #append attribute list
                shift = len(text)
                itter = attrl.get_iterator()
                while True:
                    attrs = itter.get_attrs()
                    for attr in attrs:
                        attr.end_index += shift
                        attr.start_index += shift
                        self._base_attrlist.insert(attr)
                    if not itter.next(): break
                text += ptxt
            elif type(element) == Smiley:
                self._smilies[len(text)] = element.getPixbuf(animated=False)                    
                text += '_'

        pango.Layout.set_text(self, text)

        if hasattr(pango, 'find_base_dir'):
            for line in text.splitlines():
                if (pango.find_base_dir(line,-1) == pango.DIRECTION_RTL):
                    self._is_rtl = True
                    break
        else:
            self._is_rtl = False

        logical = self.get_line(0).get_pixel_extents()[1]
        ascent = pango.ASCENT(logical)
        decent = pango.DESCENT(logical)
        self._text_height =  ascent + decent
        self._base_to_center = (self._text_height / 2) - decent
        self._update_smilies()
        
    def _update_smilies(self):    
        self._base_attrlist.filter(lambda attr: attr.type == pango.ATTR_SHAPE)
        self._smilies_scaled = {}
        #set max height of a pixbuf
        if self._scaling >= 0: 
            max_height = self._text_height * self._scaling
        else: 
            max_height = sys.maxint                
        for index, pixbuf in self._smilies.iteritems():
            height, width = pixbuf.get_height(), pixbuf.get_width()
            npix = pixbuf.copy()
            if height > max_height:
                cairo_scale = float(max_height) / float(height)
                height = int(height * cairo_scale)
                width = int(width * cairo_scale)
                npix = npix.scale_simple(width, height, gtk.gdk.INTERP_BILINEAR)
            self._smilies_scaled[index] = npix
            rect = (0, -1 * (self._base_to_center + (height /2)) * pango.SCALE,\
                                     width * pango.SCALE, height * pango.SCALE)
            self._base_attrlist.insert(pango.AttrShape((0,0,0,0), rect, index, index + 1))
        self._update_attrlists()
        
    def _update_attrlists(self):
        clr = self._color
        oclr = self._override_color
        norm_forground = pango.AttrForeground( clr.red, 
                clr.green, clr.blue, 0, len(self.get_text()))
        override_forground = pango.AttrForeground( oclr.red, 
                oclr.green, oclr.blue, 0, len(self.get_text()))
        self._attrlist = pango.AttrList()
        self._attrlist.insert(norm_forground)
        self._override_attrlist = pango.AttrList()
        self._override_attrlist.insert(override_forground)                                
        itter = self._base_attrlist.get_iterator()
        while True:
            attrs = itter.get_attrs()
            for attr in attrs:
                self._attrlist.insert(attr.copy())
                if not (attr.type in (pango.ATTR_FOREGROUND, pango.ATTR_BACKGROUND)):
                    self._override_attrlist.insert(attr.copy())
            if not itter.next(): break
        self._update_attributes()            

    def _update_attributes(self):
        if self._in_override: 
            self.set_attributes(self._override_attrlist)
        else: 
            self.set_attributes(self._attrlist)
    
    def _update_layout(self):
        if self._width >= 0 and self._ellipsize == False: # if true, then wrap
            pango.Layout.set_width(self, self._width)
        else:
            pango.Layout.set_width(self, -1)
   
    def get_size(self):
        natural_width, natural_height = pango.Layout.get_size(self)        
        if self._width >= 0 and self._ellipsize : # if ellipsize
            return self._width, natural_height
        else: 
            return natural_width, natural_height   
   
    def get_pixel_size(self):
        natural_width, natural_height = pango.Layout.get_pixel_size(self)        
        if self._width >= 0 and self._ellipsize : # if ellipsize
            return pango.PIXELS(self._width), natural_height
        else: 
            return natural_width, natural_height     
   
   
    def draw(self, ctx, area):
        x, y, width, height = area
        pxls = pango.PIXELS
        ctx.rectangle(x, y , width, height)
        ctx.clip()
        if self._is_rtl:
            layout_width = pango.Layout.get_pixel_size(self)[0]
            ctx.translate(x + width - layout_width, y)
        else:        
            ctx.translate(x,y)        
            #Clipping and ellipsation
            if self._width >= 0:        
                INLINE, BYTE, GRAPH = 0, 1, 2 
                X, Y, W, H = 0, 1, 2, 3       
                layout_width = self._width
                lst = self.get_attributes()
                e_ascent = pango.ASCENT(self._elayout.get_line(0).get_pixel_extents()[1])    
                coords = [] # of path in px
                for i in range(self.get_line_count()):
                    line = self.get_line(i)
                    edge = line.x_to_index(layout_width)                                        
                    if edge[INLINE]: 
                        #create ellipsize layout with the style of the char
                        attrlist = pango.AttrList()
                        itter = lst.get_iterator()
                        while True:
                            attrs = itter.get_attrs()
                            for attr in attrs:
                                if not attr.type == pango.ATTR_SHAPE:
                                    start, end = itter.range()
                                    if start <= edge[BYTE] < end:
                                        n_attr = attr.copy()
                                        n_attr.start_index = 0                
                                        n_attr.end_index = 3
                                        attrlist.insert(n_attr)
                            if not itter.next(): break
                        self._elayout.set_attributes(attrlist)
                        ellipsize_width = self._elayout.get_size()[0]          
                        edge = line.x_to_index(layout_width - ellipsize_width)                        
                        char = self.index_to_pos(edge[BYTE])
                        char_x, char_y, char_h = pxls(char[X]), pxls(char[Y]), pxls(char[H])
                        y1, y2 = char_y, char_y + char_h
                        if edge[INLINE]:
                            x1 = char_x
                        else:
                            x1 = 0         
                        coords.append((x1, y1))
                        coords.append((x1, y2))                 
                        line_ascent = pango.ASCENT(line.get_pixel_extents()[1])                                      
                        ctx.move_to(x1, y1 + line_ascent - e_ascent)
                        ctx.show_layout(self._elayout)
                    else:
                        char = self.index_to_pos(edge[BYTE])
                        coords.append((pxls(char[X] + char[W]), pxls(char[Y])))
                        coords.append((pxls(char[X] + char[W]), pxls(char[Y] + char[H])))
                if coords:
                    ctx.move_to(0, 0)
                    for x, y in coords:
                        ctx.line_to(x, y)
                    ctx.line_to(0, coords[-1][1])
                    ctx.close_path()
                    ctx.clip()
        #layout
        ctx.move_to(0,0)
        ctx.show_layout(self)
        #smilies
        for index  in self._smilies.keys():
            x, y, width, height = self.index_to_pos(index)                        
            pixbuf = self._smilies_scaled[index] 
            tx = pxls(x) 
            ty = pxls(y) + (pxls(height)/2) - (pixbuf.get_height()/2)
            ctx.set_source_pixbuf(pixbuf, tx, ty)
            ctx.paint()