~glitter-team/glitter/trunk

« back to all changes in this revision

Viewing changes to glitter/scroll_area.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__ = "Scroll area widget"
 
9
 
 
10
import gobject
 
11
import clutter
 
12
 
 
13
from widget import *
 
14
from container import Container
 
15
from image import Image
 
16
 
 
17
class ScrollArea(Container):
 
18
    """
 
19
    A scroll area offers a scrollable container area which can be horizontal, 
 
20
    vertical, or both. It supports both standard and touch based scrolling.
 
21
    
 
22
    """
 
23
    
 
24
    def __init__(self, area_height=1.0):
 
25
        """ Initialize scroll area """
 
26
        
 
27
        super(ScrollArea, self).__init__()
 
28
        
 
29
        self.area_height = area_height
 
30
        
 
31
        self.set_reactive(True)
 
32
        self.connect('scroll-event', self.on_scroll_event)
 
33
        
 
34
        self._init_elements()
 
35
        self._init_animations()
 
36
        
 
37
        self._update_style(self.style)
 
38
 
 
39
    def _init_elements(self):
 
40
        """ Initializes graphical elements """
 
41
        
 
42
        self._background_image = Image()
 
43
        self.add(self._background_image)
 
44
        
 
45
        self._scroll_image = Image()
 
46
        self.add(self._scroll_image)
 
47
        self._scroll_image.natural_height = self.area_height
 
48
    
 
49
    def _init_animations(self):
 
50
    
 
51
        self._timeline = None
 
52
        
 
53
    def _update_style(self, props=None):
 
54
        """ Updates style """
 
55
        
 
56
        super(ScrollArea, self)._update_style(props)
 
57
        
 
58
        for key, value in props:
 
59
            if key == 'background-image':
 
60
                self.background_image = value
 
61
                self._background_image.set_source(self.background_image)
 
62
                self._background_image._update_layout()
 
63
            elif key == 'scroll-image':
 
64
                self.scroll_image = value
 
65
                self._scroll_image.set_source(self.scroll_image)
 
66
                self._scroll_image._update_layout()        
 
67
 
 
68
    def _update_layout(self):
 
69
        """ Updates layout """
 
70
        
 
71
        super(ScrollArea, self)._update_layout()
 
72
        
 
73
        self._background_image._update_layout()
 
74
        self._scroll_image._update_layout()      
 
75
 
 
76
    def _update_layout(self):
 
77
        """ Updates layout """
 
78
        
 
79
        super(ScrollArea, self)._update_layout()
 
80
        
 
81
        for child in self.get_children():
 
82
            child._update_layout()
 
83
        
 
84
    def _scroll_up(self):
 
85
        """ Scrolls up """
 
86
        
 
87
        target_y = \
 
88
          self._scroll_image.get_y() - self._scroll_image.get_height() * 0.2
 
89
        if not self._timeline or not self._timeline.is_playing():
 
90
            if target_y < self.get_parent().get_height() - self._scroll_image.get_height():
 
91
                self._timeline = clutter.Timeline(fps=60, duration=200)
 
92
                alpha = clutter.Alpha(self._timeline, clutter.sine_inc_func)
 
93
                self._behaviour = clutter.BehaviourPath(alpha)
 
94
                self._behaviour.append_knots(
 
95
                  (0, self.get_parent().get_height() - self._scroll_image.get_height()), 
 
96
                  (0, target_y),
 
97
                  (0, self.get_parent().get_height() - self._scroll_image.get_height())
 
98
                )
 
99
            else:
 
100
                self._timeline = clutter.Timeline(fps=60, duration=100)
 
101
                alpha = clutter.Alpha(self._timeline, clutter.smoothstep_inc_func)
 
102
                self._behaviour = clutter.BehaviourPath(alpha)
 
103
                self._behaviour.append_knots(
 
104
                  (0, self._scroll_image.get_y()), 
 
105
                  (0, target_y)
 
106
                )
 
107
            self._behaviour.apply(self._scroll_image)
 
108
            self._timeline.start()
 
109
            
 
110
 
 
111
    def _scroll_down(self):
 
112
        """ Scrolls up """
 
113
        
 
114
        target_y = \
 
115
          self._scroll_image.get_y() + self._scroll_image.get_height() * 0.2
 
116
 
 
117
        if not self._timeline or not self._timeline.is_playing():
 
118
            if target_y > 0:
 
119
                self._timeline = clutter.Timeline(fps=60, duration=200)
 
120
                alpha = clutter.Alpha(self._timeline, clutter.sine_inc_func)
 
121
                self._behaviour = clutter.BehaviourPath(alpha)
 
122
                self._behaviour.append_knots(
 
123
                  (0, 0), 
 
124
                  (0, target_y),
 
125
                  (0, 0)
 
126
                )
 
127
            else:
 
128
                self._timeline = clutter.Timeline(fps=60, duration=100)
 
129
                alpha = clutter.Alpha(self._timeline, clutter.smoothstep_inc_func)
 
130
                self._behaviour = clutter.BehaviourPath(alpha)
 
131
                self._behaviour.append_knots(
 
132
                  (0, self._scroll_image.get_y()), 
 
133
                  (0, target_y)
 
134
                )
 
135
            self._behaviour.apply(self._scroll_image)
 
136
            self._timeline.start()
 
137
 
 
138
    def pack(self, widget):
 
139
        """ Packs a widget in the scroll area """
 
140
        
 
141
        self._scroll_image.add(widget)  
 
142
        
 
143
        self._update_layout()
 
144
 
 
145
    def unpack(self, widget):
 
146
        """ Unpacks a widget in the scroll widget """
 
147
        
 
148
        self._scroll_image.remove(widget)  
 
149
        
 
150
        self._update_layout()
 
151
        
 
152
    def on_scroll_event(self, widget, event):
 
153
        """ Scroll event """
 
154
    
 
155
        if event.direction == clutter.SCROLL_UP:
 
156
            self._scroll_up()
 
157
        elif event.direction == clutter.SCROLL_DOWN:
 
158
            self._scroll_down()
 
159
        elif event.direction == clutter.SCROLL_LEFT:
 
160
            pass
 
161
        elif event.direction == clutter.SCROLL_RIGHT:
 
162
            pass
 
163
            
 
164