~glitter-team/glitter/trunk

« back to all changes in this revision

Viewing changes to glitter/button.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__ = "Button widget"
 
9
 
 
10
import gobject
 
11
 
 
12
from widget import *
 
13
from container import Container
 
14
from box import HBox
 
15
from label import Label
 
16
from image import Image
 
17
 
 
18
class Button(Container):
 
19
    """ 
 
20
    A button widget features icon and label, signals for press and release.
 
21
    
 
22
    """
 
23
    
 
24
    __gsignals__ = {
 
25
        'clicked': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_BOOLEAN, 
 
26
                    (gobject.TYPE_PYOBJECT,))
 
27
    }
 
28
        
 
29
    def __init__(self, icon=None, label=None):
 
30
        """ Initialize button widget """
 
31
        
 
32
        super(Button, self).__init__()
 
33
        
 
34
        self.icon = icon
 
35
        self.label = label
 
36
        
 
37
        self.set_reactive(True) 
 
38
        self.connect('motion-event', self.on_motion_event) 
 
39
        self.connect('leave-event', self.on_leave_event) 
 
40
        self.connect('button-press-event', self.on_press_event) 
 
41
        self.connect('button-release-event', self.on_release_event)
 
42
        
 
43
        self._init_elements()
 
44
        
 
45
        self._update_style(self.style)
 
46
    
 
47
    def _init_elements(self):
 
48
        """ Initializes graphical elements """
 
49
        
 
50
        self._background_image = Image()
 
51
        self.add(self._background_image)
 
52
        
 
53
        self._hbox = HBox()
 
54
        self.add(self._hbox)
 
55
        
 
56
        if self.icon:
 
57
            self._icon = Image(self.icon)
 
58
            self._icon.size_ratio = 1.0
 
59
            self._hbox.pack(self._icon)
 
60
        if self.label:
 
61
            self._label = Label(self.label)
 
62
            self._hbox.pack(self._label)
 
63
        
 
64
    def _update_style(self, props=None):
 
65
        """ Updates style """
 
66
        
 
67
        super(Button, self)._update_style(props)
 
68
        
 
69
        for key, value in props:
 
70
            if key == 'background-image':
 
71
                self.background_image = value
 
72
                self._background_image.hide()
 
73
                self._background_image.set_source(self.background_image)
 
74
                self._background_image._update_layout()
 
75
                self._background_image.show()
 
76
        
 
77
    def _update_layout(self):
 
78
        """ Updates layout """
 
79
        
 
80
        super(Button, self)._update_layout()
 
81
                
 
82
        self._background_image._update_layout()
 
83
        self._hbox._update_layout()
 
84
    
 
85
    def on_motion_event(self, widget, event):
 
86
        """ Cursor motion over widget """
 
87
        
 
88
        if not self.state == STATE_SELECTED:
 
89
            self.state = STATE_HIGHLIGHT
 
90
        
 
91
    def on_leave_event(self, widget, event):
 
92
        """ Cursor leave from widget """
 
93
        
 
94
        self.state = STATE_NORMAL
 
95
        
 
96
    def on_press_event(self, widget, event):
 
97
        
 
98
        self.state = STATE_SELECTED
 
99
        self.emit("clicked", self)
 
100
        
 
101
    def on_release_event(self, widget, event):
 
102
        
 
103
        self.state = STATE_HIGHLIGHT
 
104
        
 
105
        
 
106
        
 
107