~glitter-team/glitter/trunk

« back to all changes in this revision

Viewing changes to glitter/widget.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__ = "Base class for widgets" 
 
9
 
 
10
import gobject
 
11
import clutter
 
12
 
 
13
from theme import Theme
 
14
from style import Style
 
15
 
 
16
STATE_NORMAL = 'STATE_NORMAL'
 
17
STATE_HIGHLIGHT = 'STATE_HIGHLIGHT'
 
18
STATE_ACTIVE = 'STATE_ACTIVE'
 
19
STATE_SELECTED = 'STATE_SELECTED'
 
20
STATE_INSENSITIVE = 'STATE_INSENSITIVE'
 
21
 
 
22
class Widget(clutter.Group):
 
23
    """
 
24
    Base class for Glitter widgets.
 
25
    
 
26
    A widget inherits from a clutter group actor, which means each and every 
 
27
    widget can have children added or removed, working as a scenegraph. 
 
28
    Moreover, all the remaining underlying clutter API is exposed.
 
29
    
 
30
    The widget object also holds widget specific properties like current state 
 
31
    and styles.
 
32
    
 
33
    """
 
34
 
 
35
    # Signals    
 
36
    __gsignals__ = {
 
37
        'state-changed': (gobject.SIGNAL_RUN_LAST,
 
38
                          gobject.TYPE_BOOLEAN,
 
39
                          (gobject.TYPE_PYOBJECT,)),
 
40
        'style-changed': (gobject.SIGNAL_RUN_LAST,
 
41
                          gobject.TYPE_BOOLEAN,
 
42
                          (gobject.TYPE_PYOBJECT,))
 
43
    }
 
44
    
 
45
    def __init__(self):
 
46
        """ Initialize widget """
 
47
        
 
48
        super(Widget, self).__init__()
 
49
        
 
50
        self._state = STATE_NORMAL
 
51
        self._style = None
 
52
        self._styles = {}
 
53
        
 
54
        self._init_style()
 
55
        
 
56
    def _init_style(self):
 
57
        """ Initialize widget style """
 
58
                
 
59
        theme = Theme.get_default()
 
60
                  
 
61
        self._styles = {}
 
62
                     
 
63
        # Widget styles are a merge of the respective state styles for the 
 
64
        # widget and all its subclasses             
 
65
        base = self.__class__
 
66
        while issubclass(base, Widget): 
 
67
            module_name = base.__name__
 
68
            for state in (STATE_NORMAL, STATE_HIGHLIGHT, STATE_ACTIVE, 
 
69
                          STATE_SELECTED, STATE_INSENSITIVE):
 
70
                style = theme.get_style(module_name, state)
 
71
                if state in self._styles and style:
 
72
                    self._styles[state] = self._styles[state].merge(style)
 
73
                elif style:
 
74
                    #self._styles[state] = style
 
75
                    if state != STATE_NORMAL:
 
76
                        self._styles[state] = self._styles[STATE_NORMAL].merge(style)
 
77
                    else:
 
78
                        self._styles[state] = style
 
79
            base = base.__base__ 
 
80
        
 
81
        self._style = self._styles[self.state]
 
82
 
 
83
    def _update_style(self, props=None):
 
84
        """ Metaclass for style updates """
 
85
 
 
86
    # Properties
 
87
    
 
88
    def get_state(self):
 
89
        """ Retrieve widget state """
 
90
        
 
91
        return self._state
 
92
        
 
93
    def set_state(self, state):
 
94
        """ Sets the state for the widget and emits a 'state-changed' signal 
 
95
        case necessary
 
96
        
 
97
        state -- enum(STATE_NORMAL, STATE_HIGHLIGHT, STATE_ACTIVE, 
 
98
                 STATE_SELECTED, STATE_INSENSITIVE)
 
99
        """
 
100
        
 
101
        if self._state != state:
 
102
            self._state = state
 
103
            self.emit('state-changed', state)
 
104
            
 
105
    state = property(get_state, set_state)
 
106
    
 
107
    def get_style(self):
 
108
        """ Retrieve widget style """
 
109
        
 
110
        return self._style
 
111
        
 
112
    def set_style(self, style):
 
113
        """ Sets the style for the widget and emits a 'style-changed' signal 
 
114
        case necessary
 
115
        
 
116
        style -- (glitter.Style) 
 
117
        """
 
118
        
 
119
        if self._style != style:
 
120
            self._style = style
 
121
            self.emit('style-changed', style)
 
122
            
 
123
    style = property(get_style, set_style)
 
124
 
 
125
    def do_state_changed(self, previous_state):
 
126
        """ Widget state changed """
 
127
        
 
128
        self.style = self._styles[self.state]
 
129
        
 
130
    def do_style_changed(self, previous_style):
 
131
        """ Widget style changed """
 
132
        
 
133
        self._update_style(self.style)
 
134
        
 
135