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