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 FocusWidget import FocusWidget
21 from MouseListener import MouseHandler
22 from pyjamas.ui import KeyboardListener
23
24
25 -class Control(FocusWidget, MouseHandler):
26
27 - def __init__(self, element, min_value, max_value,
28 start_value=None, step=None,
29 **kwargs):
30
31 self.min_value = min_value
32 self.max_value = max_value
33 if start_value is None:
34 start_value = min_value
35 if step is None:
36 step = (self.max_value - self.min_value) / 20
37 self.step = step
38 self.value = start_value
39 self.valuechange_listeners = []
40 self.dragging = False
41 self.drag_enabled = False
42
43 if not kwargs.has_key("TabIndex"): kwargs['TabIndex'] = 0
44 FocusWidget.__init__(self, element, **kwargs)
45 MouseHandler.__init__(self)
46
48 return self.drag_enabled
49
58
61
64
67
69 """ rounds and limits the value to acceptable range
70 """
71 value = math.floor((value - self.min_value) / self.step)
72 value = (value * self.step) + self.min_value
73 value = max(value, self.min_value)
74 value = min(value, self.max_value)
75 return value
76
77 - def setValue(self, new_value, notify=1):
78
79 old_value = self.value
80 self.value = new_value
81
82 if not notify:
83 return
84
85 for listener in self.valuechange_listeners:
86 listener.onControlValueChanged(self, old_value, new_value)
87
89 self.valuechange_listeners.append(listener)
90
92 self.valuechange_listeners.remove(listener)
93
96
105
110
114
115
125
129
134
135 - def onKeyDown(self, sender, keycode, modifiers):
146
147 - def onKeyUp(self, sender, keycode, modifiers):
149
150 - def onKeyPress(self, sender, keycode, modifiers):
152
153 Factory.registerClass('pyjamas.ui.Control', Control)
154