Package pyjamas :: Package ui :: Module Control
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.ui.Control

  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
47 - def isDragable(self):
48 return self.drag_enabled
49
50 - def setDragable(self, dragable):
51 if self.drag_enabled == dragable: 52 return 53 if self.drag_enabled: 54 self.removeKeyboardListener(self) 55 else: 56 self.addKeyboardListener(self) 57 self.drag_enabled = dragable
58
59 - def onFocus(self, sender):
60 pass
61
62 - def onLostFocus(self, sender):
63 pass
64
65 - def onClick(self, sender=None):
66 pass
67
68 - def processValue(self, value):
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
88 - def addControlValueListener(self, listener):
89 self.valuechange_listeners.append(listener)
90
91 - def removeControlValueListener(self, listener):
92 self.valuechange_listeners.remove(listener)
93
94 - def moveControl(self, x, y):
95 pass
96
97 - def onClick(self, sender=None):
98 self.setFocus(True) 99 # work out the relative position of cursor 100 event = DOM.eventGetCurrentEvent() 101 mouse_x = DOM.eventGetClientX(event) + Window.getScrollLeft() 102 mouse_y = DOM.eventGetClientY(event) + Window.getScrollTop() 103 self.moveControl(mouse_x - self.getAbsoluteLeft(), 104 mouse_y - self.getAbsoluteTop())
105
106 - def onMouseMove(self, sender, x, y):
107 if not self.dragging: 108 return 109 self.moveControl(x + Window.getScrollLeft(), y + Window.getScrollTop())
110
111 - def onLoseFocus(self, sender):
112 self.dragging = False 113 DOM.releaseCapture(self.getElement())
114 115
116 - def onMouseDown(self, sender, x, y):
117 # regardless of drag_enabled, onMouseDown must prevent 118 # default, in order to avoid losing focus. 119 DOM.eventPreventDefault(DOM.eventGetCurrentEvent()) 120 if not self.drag_enabled: 121 return 122 self.dragging = True 123 DOM.setCapture(self.getElement()) 124 self.moveControl(x + Window.getScrollLeft(), y + Window.getScrollTop())
125
126 - def onMouseUp(self, sender, x, y):
127 self.dragging = False 128 DOM.releaseCapture(self.getElement())
129
130 - def onMouseEnter(self, sender):
131 pass
132 - def onMouseLeave(self, sender):
133 pass
134
135 - def onKeyDown(self, sender, keycode, modifiers):
136 if keycode == KeyboardListener.KEY_UP: 137 DOM.eventPreventDefault(DOM.eventGetCurrentEvent()) 138 new_value = self.processValue(self.value + self.step) 139 self.setControlPos(new_value) 140 self.setValue(new_value) 141 elif keycode == KeyboardListener.KEY_DOWN: 142 DOM.eventPreventDefault(DOM.eventGetCurrentEvent()) 143 new_value = self.processValue(self.value - self.step) 144 self.setControlPos(new_value) 145 self.setValue(new_value)
146
147 - def onKeyUp(self, sender, keycode, modifiers):
148 pass
149
150 - def onKeyPress(self, sender, keycode, modifiers):
151 pass
152 153 Factory.registerClass('pyjamas.ui.Control', Control) 154