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

# Glitter Toolkit

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

import gobject
import clutter

from widget import *
from container import Container
from image import Image
from scrollable import Scrollable

class ScrollArea(Container):
    """
    A scroll area offers a scrollable container area which can be horizontal, 
    vertical, or both. It supports both standard and touch based scrolling.
    
    """
    
    def __init__(self, area_height=1.0):
        """ Initialize scroll area """
        
        super(ScrollArea, self).__init__()
        
        self.area_height = area_height
        
        self._previous_scroll_image_y = None
        
        self._items = []
        self.set_reactive(True)
        self.connect('scroll-event', self.on_scroll_event)
        
        self._init_elements()
        self._init_animations()
        
        self._update_style(self.style)

    def _init_elements(self):
        """ Initializes graphical elements """
        
        self._background_image = Image()
        self.add(self._background_image)
        
        self._scroll_image = Image()
        self.add(self._scroll_image)
        self._scroll_image.natural_height = self.area_height
    
    def _init_animations(self):
    
        self._timeline = None
        
    def _update_style(self, props=None):
        """ Updates style """
        
        super(ScrollArea, self)._update_style(props)
        
        for key, value in props:
            if key == 'background-image':
                self.background_image = value
                self._background_image.set_source(self.background_image)
                self._background_image._update_layout()
            elif key == 'scroll-image':
                self.scroll_image = value
                self._scroll_image.set_source(self.scroll_image)
                self._scroll_image._update_layout()        

    def _update_layout(self):
        """ Updates layout """
        
        super(ScrollArea, self)._update_layout()
        
        self._background_image._update_layout()
        self._scroll_image._update_layout()      
        
        for item in self._items:
            item._update_layout()
            item.raise_top()
            #print self._scroll_image.get_height(), item.get_height()
        
        self.set_clipu(0, 0, self.get_widthu(), self.get_heightu())
        #self._scroll_image.set_clipu(0, 0, self._scroll_image.get_widthu(), self._scroll_image.get_heightu())
        
    def _scroll_up(self, factor=0.2, px=None):
        """ Scrolls up """
        
        if px:
            target_y = \
              self._scroll_image.get_y() - px
        else:
            target_y = \
              self._scroll_image.get_y() - self._scroll_image.get_height() * factor
              
        if not self._timeline or not self._timeline.is_playing():
            if target_y < self.get_height() - self._scroll_image.get_height():
                self._timeline = clutter.Timeline(fps=60, duration=200)
                alpha = clutter.Alpha(self._timeline, clutter.sine_inc_func)
                self._behaviour = clutter.BehaviourPath(alpha)
                self._behaviour.append_knots(
                  (0, self._scroll_image.get_y()), 
                  (0, target_y),
                  (0, -(self._scroll_image.get_height() - self.get_height()))
                )
            else:
                self._timeline = clutter.Timeline(fps=60, duration=100)
                alpha = clutter.Alpha(self._timeline, clutter.smoothstep_inc_func)
                self._behaviour = clutter.BehaviourPath(alpha)
                self._behaviour.append_knots(
                  (0, self._scroll_image.get_y()), 
                  (0, target_y)
                )
            self._behaviour.apply(self._scroll_image)
            self._timeline.start()
            
 
    def _scroll_down(self, factor=0.2, px=None):
        """ Scrolls up """
        
        if px:
            target_y = \
              self._scroll_image.get_y() + px
        else:
            target_y = \
              self._scroll_image.get_y() + self._scroll_image.get_height() * factor
        if not self._timeline or not self._timeline.is_playing():
            if target_y > 0:
                self._timeline = clutter.Timeline(fps=60, duration=200)
                alpha = clutter.Alpha(self._timeline, clutter.sine_inc_func)
                self._behaviour = clutter.BehaviourPath(alpha)
                self._behaviour.append_knots(
                  (0, 0), 
                  (0, target_y),
                  (0, 0)
                )
            else:
                self._timeline = clutter.Timeline(fps=60, duration=100)
                alpha = clutter.Alpha(self._timeline, clutter.smoothstep_inc_func)
                self._behaviour = clutter.BehaviourPath(alpha)
                self._behaviour.append_knots(
                  (0, self._scroll_image.get_y()), 
                  (0, target_y)
                )
            self._behaviour.apply(self._scroll_image)
            self._timeline.start()
            
    def _scroll_to(self, factor):
        """ Scrolls to a given position
        
        factor -- (float) position as a factor of the scroll area height
        """
        
        if self._previous_scroll_image_y:
            _scroll_image_y = self._previous_scroll_image_y
        else:
            _scroll_image_y = self._scroll_image.get_y()
        target_y = _scroll_image_y + self._scroll_image.get_height() * factor

        if self._timeline:
            self._timeline.rewind()
        else:
            self._timeline = clutter.Timeline(fps=60, duration=200)
        alpha = clutter.Alpha(self._timeline, clutter.sine_inc_func)
        self._behaviour = clutter.BehaviourPath(alpha)
        self._behaviour.append_knots(
          (0, self._scroll_image.get_y()), 
          (0, target_y)
        )
        self._previous_scroll_image_y = target_y
        self._behaviour.apply(self._scroll_image)
        self._timeline.start()
                      
        #self._scroll_image.set_y(int(target_y))

    def pack(self, widget):
        """ Packs a widget into the scroll area """
        
        self._scroll_image.add(widget)  
        self._items.append(widget)
        
        if isinstance(widget, Scrollable):
            widget.connect('view-changed', self.on_scrollable_view_changed)
            
        self._update_layout()

    def unpack(self, widget):
        """ Unpacks a widget in the scroll widget """
        
        self._scroll_image.remove(widget)  
        self._items.remove(widget)
        
        self._update_layout()
        
    def on_scroll_event(self, widget, event):
        """ Scroll event """

        if event.direction == clutter.SCROLL_UP:
            self._scroll_up()
        elif event.direction == clutter.SCROLL_DOWN:
            self._scroll_down()
        elif event.direction == clutter.SCROLL_LEFT:
            pass
        elif event.direction == clutter.SCROLL_RIGHT:
            pass
             
    def on_scrollable_view_changed(self, widget, event, offset):
        """ Scrollable view changed """
        
        # Get event widget allocation box (clutter units)
        xu, yu, widthu, heightu = event        
        
        # Update xu, yu for coords relative to self
        parent = widget
        while parent != self:
            yu += parent.get_yu()
            parent = parent.get_parent()
        
        scroll_delta = float(heightu) / float(self._scroll_image.get_heightu())
        if yu < 0:
            self._scroll_to(scroll_delta)
        elif yu + heightu > self.get_heightu():
            self._scroll_to(-scroll_delta)