1 """ Control Widgets. Presently comprises a Vertical Slider Demo and derivatives.
2
3 HorizontalDemoSlider and HorizontalDemoSlider2 added by Bill Winder
4 AreaDemoSlider and AreaDemoSlider2 added by Bill Winder
5
6 Copyright (C) 2008, 2009, 2010 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
7 Copyright (C) 2010 - Cedric Gestes <gestes@aldebaran-robotics.com>
8 Copyright (C) 2009, 2010 - Bill Winder <wgwinder@gmail.com>
9
10
11 To do: All controls with draggable=True do not fire the OnFocus methon on single click.
12 the control does not activate the OnFocus method. Clicking the handle does fire OnFocus, however.
13
14 """
15
16 from pyjamas import Factory
17 import math
18 from pyjamas import DOM
19 from pyjamas import Window
20 from pyjamas.ui.FocusWidget import FocusWidget
21 from pyjamas.ui.MouseListener import MouseHandler, MouseWheelHandler
22 from pyjamas.ui import KeyboardListener
23 from pyjamas.ui import GlassWidget
24
25
26 -class Control(FocusWidget, MouseHandler, MouseWheelHandler):
27
28 - def __init__(self, element, min_value, max_value,
29 start_value=None, step=None,
30 **kwargs):
31
32 self.min_value = min_value
33 self.max_value = max_value
34 if start_value is None:
35 start_value = min_value
36 if step is None:
37 step = (self.max_value - self.min_value) / 20
38 self.step = step
39 self.value = start_value
40 self.valuechange_listeners = []
41 self.dragging = False
42 self.drag_enabled = False
43
44 kwargs['TabIndex'] = kwargs.get("TabIndex", 0)
45
46 FocusWidget.__init__(self, element, **kwargs)
47 MouseHandler.__init__(self)
48 MouseWheelHandler.__init__(self, True)
49 self.addMouseWheelListener(self)
50
52 return self.drag_enabled
53
62
65
68
71
73 """ rounds and limits the value to acceptable range
74 """
75 value = math.floor((value - self.min_value) / self.step)
76 value = (value * self.step) + self.min_value
77 value = max(value, self.min_value)
78 value = min(value, self.max_value)
79 return value
80
81 - def setValue(self, new_value, notify=1):
82
83 old_value = self.value
84 self.value = new_value
85
86 if not notify:
87 return
88
89 for listener in self.valuechange_listeners:
90 listener.onControlValueChanged(self, old_value, new_value)
91
93 self.valuechange_listeners.append(listener)
94
96 self.valuechange_listeners.remove(listener)
97
100
109
111 print "mouse wheel", sender, velocity
112 if self.dragging:
113 return
114 new_value = self.processValue(self.value - self.step * velocity)
115 self.setControlPos(new_value)
116 self.setValue(new_value)
117
122
125
137
140
143
146
149
152
154 if not self.dragging:
155 return
156 self.dragging = False
157 GlassWidget.hide()
158
159 - def onKeyDown(self, sender, keycode, modifiers):
170
171 - def onKeyUp(self, sender, keycode, modifiers):
173
174 - def onKeyPress(self, sender, keycode, modifiers):
176
180
209
210 Factory.registerClass('pyjamas.ui.Control', 'Control', Control)
211