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

# Glitter Toolkit

__authors__ = ["Jan Jokela <janjokela@gmail.com>"]
__licenses__ = ["LICENSE.LGPL"]
__description__ = "Base class for widgets" 

import gobject
import clutter

from theme import Theme
from style import Style

STATE_NORMAL = 'STATE_NORMAL'
STATE_HIGHLIGHT = 'STATE_HIGHLIGHT'
STATE_ACTIVE = 'STATE_ACTIVE'
STATE_SELECTED = 'STATE_SELECTED'
STATE_INSENSITIVE = 'STATE_INSENSITIVE'

class Widget(clutter.Group):
    """
    Base class for Glitter widgets.
    
    A widget inherits from a clutter group actor, which means each and every 
    widget can have children added or removed, working as a scenegraph. 
    Moreover, all the remaining underlying clutter API is exposed.
    
    The widget object also holds widget specific properties like current state 
    and styles.
    
    """

    # Signals    
    __gsignals__ = {
        'state-changed': (gobject.SIGNAL_RUN_LAST,
                          gobject.TYPE_BOOLEAN,
                          (gobject.TYPE_PYOBJECT,)),
        'style-changed': (gobject.SIGNAL_RUN_LAST,
                          gobject.TYPE_BOOLEAN,
                          (gobject.TYPE_PYOBJECT,))
    }
    
    def __init__(self):
        """ Initialize widget """
        
        super(Widget, self).__init__()
        
        self._state = STATE_NORMAL
        self._style = None
        self._styles = {}
        
        self._init_style()
        
    def _init_style(self):
        """ Initialize widget style """
                
        theme = Theme.get_default()
                  
        self._styles = {}
                     
        # Widget styles are a merge of the respective state styles for the 
        # widget and all its subclasses             
        base = self.__class__
        while issubclass(base, Widget): 
            module_name = base.__name__
            for state in (STATE_NORMAL, STATE_HIGHLIGHT, STATE_ACTIVE, 
                          STATE_SELECTED, STATE_INSENSITIVE):
                style = theme.get_style(module_name, state)
                if state in self._styles and style:
                    self._styles[state] = self._styles[state].merge(style)
                elif style:
                    self._styles[state] = style
                    """if state != STATE_NORMAL:
                        self._styles[state] = self._styles[STATE_NORMAL].merge(style)
                    else:
                        self._styles[state] = style"""
            base = base.__base__ 
        
        self._style = self._styles[self.state]

    def _update_style(self, props=None):
        """ Metaclass for style updates """

    # Properties
    
    def get_state(self):
        """ Retrieve widget state """
        
        return self._state
        
    def set_state(self, state):
        """ Sets the state for the widget and emits a 'state-changed' signal 
        case necessary
        
        state -- enum(STATE_NORMAL, STATE_HIGHLIGHT, STATE_ACTIVE, 
                 STATE_SELECTED, STATE_INSENSITIVE)
        """
        
        if self._state != state:
            self._state = state
            self.emit('state-changed', state)
            
    state = property(get_state, set_state)
    
    def get_style(self):
        """ Retrieve widget style """
        
        return self._style
        
    def set_style(self, style):
        """ Sets the style for the widget and emits a 'style-changed' signal 
        case necessary
        
        style -- (glitter.Style) 
        """
        
        if self._style != style:
            self._style = style
            self.emit('style-changed', style)
            
    style = property(get_style, set_style)

    def do_state_changed(self, previous_state):
        """ Widget state changed """
        
        self.style = self._styles[self.state]
        
    def do_style_changed(self, previous_style):
        """ Widget style changed """
        
        self._update_style(self.style)