~glitter-team/glitter/trunk

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
# !/usr/bin/python
# -*- coding: utf-8 -*-

# Glitter Toolkit

__authors__ = ["Jan Jokela <janjokela@gmail.com>"]
__licenses__ = ["LICENSE.LGPL"]
__description__ = "Button widget"

import gobject

from widget import *
from container import Container
from box import HBox
from label import Label
from image import Image

class Button(Container):
    """ 
    A button widget features icon and label, signals for press and release.
    
    """
    
    __gsignals__ = {
        'clicked': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_BOOLEAN, 
                    (gobject.TYPE_PYOBJECT,))
    }
        
    def __init__(self, icon=None, label=None):
        """ Initialize button widget """
        
        super(Button, self).__init__()
        
        self.icon = icon
        self.label = label
        
        self.set_reactive(True) 
        self.connect('motion-event', self.on_motion_event) 
        self.connect('leave-event', self.on_leave_event) 
        self.connect('button-press-event', self.on_press_event) 
        self.connect('button-release-event', self.on_release_event)
        
        self._init_elements()
        
        self._update_style(self.style)
    
    def _init_elements(self):
        """ Initializes graphical elements """
        
        self._background_image = Image()
        self.add(self._background_image)
        
        self._hbox = HBox()
        self.add(self._hbox)
        self._update_layout()
        
        if self.icon:
            self._icon = Image(self.icon)
            self._icon.size_ratio = 1.0
            self._hbox.pack(self._icon)
        if self.label:
            self._label = Label(self.label)
            self._hbox.pack(self._label)
        
    def _update_style(self, props=None):
        """ Updates style """
        
        super(Button, self)._update_style(props)
        
        for key, value in props:
            if key == 'background-image':
                self.background_image = value
                self._background_image.hide()
                self._background_image.set_source(self.background_image)
                self._background_image._update_layout()
                self._background_image.show()
                
        
    def _update_layout(self):
        """ Updates layout """
        
        super(Button, self)._update_layout()
                
        self._background_image._update_layout()
        self._hbox._update_layout()

    def on_motion_event(self, widget, event):
        """ Cursor motion over widget """
        
        if not self.state == STATE_SELECTED:
            self.state = STATE_HIGHLIGHT
        
    def on_leave_event(self, widget, event):
        """ Cursor leave from widget """
        
        self.state = STATE_NORMAL
        
    def on_press_event(self, widget, event):
        
        self.state = STATE_SELECTED
        self.emit("clicked", self)
        
    def on_release_event(self, widget, event):
        
        self.state = STATE_HIGHLIGHT