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

Source Code for Module pyjamas.ui.VerticalSlider

  1  """ Control Widgets.  Presently comprises a Vertical Slider and derivatives. 
  2   
  3      HorizontalSlider and HorizontalSlider2 added by Bill Winder 
  4      AreaSlider and AreaSlider2 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  from pyjamas import DOM 
 18  from pyjamas.ui import Focus 
 19  from Control import Control 
 20   
 21   
22 -class VerticalSlider(Control):
23
24 - def __init__(self, min_value, max_value, start_value=None, step=None, 25 **kwargs):
26 27 if not kwargs.has_key("StyleName"): 28 kwargs['StyleName'] = "gwt-VerticalSlider" 29 30 if kwargs.has_key('Element'): 31 # XXX FIXME: Focus.createFocusable is here for a reason... 32 element = kwargs.pop('Element') 33 else: 34 element = Focus.createFocusable() 35 DOM.setStyleAttribute(element, "position", "relative") 36 DOM.setStyleAttribute(element, "overflow", "hidden") 37 38 self.handle = DOM.createDiv() 39 DOM.appendChild(element, self.handle) 40 41 self.setHandleStyle("1px", "100%", "10px", "#808080") 42 43 Control.__init__(self, element, min_value, max_value, start_value, step, 44 **kwargs) 45 46 self.addClickListener(self) 47 self.addFocusListener(self) 48 self.addMouseListener(self)
49
50 - def setHandleStyle(self, border, width, height, backgroundColor):
51 if border is not None: 52 DOM.setStyleAttribute(self.handle, "border", border) 53 if width is not None: 54 DOM.setStyleAttribute(self.handle, "width", width) 55 if height is not None: 56 DOM.setStyleAttribute(self.handle, "height", height) 57 if backgroundColor is not None: 58 DOM.setStyleAttribute(self.handle, "backgroundColor", backgroundColor)
59
60 - def onFocus(self, sender):
61 self.addStyleName("gwt-VerticalSlider-focussed")
62
63 - def onLostFocus(self, sender):
64 self.removeStyleName("gwt-VerticalSlider-focussed") 65 self.dragging = False 66 DOM.releaseCapture(self.getElement())
67
68 - def moveControl(self, mouse_x, mouse_y):
69 handle_height = DOM.getIntAttribute(self.handle, "offsetHeight") 70 widget_height = self.getOffsetHeight() 71 height_range = widget_height - 10 # handle height is hard-coded 72 relative_y = mouse_y - (handle_height / 2) 73 if relative_y < 0: 74 relative_y = 0 75 if relative_y >= height_range: 76 relative_y = height_range 77 78 relative_y = height_range - relative_y # turn round (bottom to top) 79 80 val_diff = self.max_value - self.min_value 81 new_value = ((val_diff * relative_y) / height_range) + self.min_value 82 new_value = self.processValue(new_value) 83 84 self.setControlPos(new_value) 85 self.setValue(new_value)
86
87 - def setControlPos(self, value):
88 widget_height = self.getOffsetHeight() 89 height_range = widget_height - 10 # handle height is hard-coded 90 val_diff = self.max_value - self.min_value 91 relative_y = height_range * (value - self.min_value) / val_diff 92 93 # limit the position to be in the widget! 94 if relative_y < 0: 95 relative_y = 0 96 if relative_y >= height_range: 97 relative_y = height_range 98 99 relative_y = height_range - relative_y # turn round (bottom to top) 100 101 # move the handle 102 DOM.setStyleAttribute(self.handle, "top", "%dpx" % relative_y) 103 DOM.setStyleAttribute(self.handle, "position", "absolute")
104 105 Factory.registerClass('pyjamas.ui.VerticalSlider', VerticalSlider) 106