Permalink
Browse files

add MouseInputControl

  • Loading branch information...
1 parent acfc093 commit c4433f79c76217108cecf6ad000dfef5e2b8c600 Luke Leighton committed Jul 28, 2010
@@ -14,6 +14,7 @@
from pyjamas.ui.Controls import AreaDemoSlider
from pyjamas.ui.Controls import AreaDemoSlider2
from pyjamas.ui.Controls import InputControl
+from pyjamas.ui.MouseInputControl import MouseInputControl
from pyjamas.ui.HorizontalPanel import HorizontalPanel
from pyjamas.ui.VerticalPanel import VerticalPanel
from pyjamas.ui import HasAlignment
@@ -82,22 +83,22 @@ def __init__(self, p2):
self.setSpacing(10)
if p2:
- self.b = AreaDemoSlider2([0,0], [100,100])
+ self.b = AreaDemoSlider2([0,0], [100,100], [0.2, 0.2])
else:
- self.b = AreaDemoSlider([0,0], [100,100])
+ self.b = AreaDemoSlider([0,0], [100,100], [0.2, 0.2])
self.add(self.b)
self.b.setHeight("100px")
self.b.setWidth("100px")
self.b.addControlValueListener(self)
- self.label_x = InputControl(0, 100)
+ self.label_x = MouseInputControl(0, 100, 0.2)
self.add(self.label_x)
self.label_x.addControlValueListener(self)
- self.label_y = InputControl(0, 100)
+ self.label_y = MouseInputControl(0, 100, 0.2)
self.add(self.label_y)
self.label_y.addControlValueListener(self)
@@ -125,6 +126,7 @@ def onControlValueChanged(self, sender, old_value_xy, new_value_xy):
self.label_y.setControlPos(new_value_y)
self.label_y.setValue(new_value_y, 0)
+
class ControlDemo:
def onModuleLoad(self):
@@ -1,3 +1,11 @@
+body {
+ width: 100%;
+ height: 100%;
+ padding: 0px;
+ margin: 0px;
+ background-color:#ffe;
+}
+
.gwt-VerticalSlider {
border:1px solid #aaaaaa;
background-color:#eeeeee;
@@ -18,3 +26,14 @@
border:1px solid #000000;
background-color:#cccccc;
}
+.gwt-MouseInputControl {
+ border:1px solid #aaaaaa;
+ background-color:#eeeeee;
+ width: 45px;
+}
+
+.gwt-MouseInputControl-focussed {
+ border:1px solid #000000;
+ background-color:#cccccc;
+}
+
@@ -110,25 +110,31 @@ def onMouseMove(self, sender, x, y):
def onLoseFocus(self, sender):
self.dragging = False
+ #print "lose focus"
+ #DOM.removeEventPreview(self)
DOM.releaseCapture(self.getElement())
def onMouseDown(self, sender, x, y):
# regardless of drag_enabled, onMouseDown must prevent
# default, in order to avoid losing focus.
+ self.setFocus(True)
DOM.eventPreventDefault(DOM.eventGetCurrentEvent())
if not self.drag_enabled:
return
+ #DOM.addEventPreview(self)
self.dragging = True
DOM.setCapture(self.getElement())
self.moveControl(x + Window.getScrollLeft(), y + Window.getScrollTop())
def onMouseUp(self, sender, x, y):
self.dragging = False
+ #DOM.removeEventPreview(self)
DOM.releaseCapture(self.getElement())
def onMouseEnter(self, sender):
pass
+
def onMouseLeave(self, sender):
pass
@@ -150,4 +156,37 @@ def onKeyUp(self, sender, keycode, modifiers):
def onKeyPress(self, sender, keycode, modifiers):
pass
+ def _event_targets_control(self, event):
+ target = DOM.eventGetTarget(event)
+ return target and DOM.isOrHasChild(self.getElement(), target)
+
+ def onEventPreview(self, event):
+ etype = DOM.eventGetType(event)
+ print "control preview", etype, self._event_targets_control(event), \
+ DOM.getCaptureElement() is not None
+ if etype == "keydown":
+ return self._event_targets_control(event)
+ elif etype == "keyup":
+ return self._event_targets_control(event)
+ elif etype == "keypress":
+ return self._event_targets_control(event)
+ elif ( etype == "mousedown"
+ or etype == "blur"
+ ):
+ if DOM.getCaptureElement() is not None:
+ return True
+ if not self._event_targets_control(event):
+ return True
+ elif ( etype == "mouseup"
+ or etype == "click"
+ or etype == "mousemove"
+ or etype == "dblclick"
+ ):
+ if DOM.getCaptureElement() is not None:
+ return True
+ elif etype == "mouseout":
+ if DOM.getCaptureElement() is not None:
+ return False
+ return self._event_targets_control(event)
+
Factory.registerClass('pyjamas.ui.Control', Control)
@@ -0,0 +1,61 @@
+""" Control Widgets. Presently comprises a Vertical Slider and derivatives.
+
+ HorizontalSlider and HorizontalSlider2 added by Bill Winder
+ AreaSlider and AreaSlider2 added by Bill Winder
+
+ Copyright (C) 2008, 2009, 2010 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
+ Copyright (C) 2010 - Cedric Gestes <gestes@aldebaran-robotics.com>
+ Copyright (C) 2009, 2010 - Bill Winder <wgwinder@gmail.com>
+
+
+ To do: All controls with draggable=True do not fire the OnFocus methon on single click.
+ the control does not activate the OnFocus method. Clicking the handle does fire OnFocus, however.
+
+"""
+
+from pyjamas import Factory
+from pyjamas import DOM
+from pyjamas.ui import MouseListener
+from InputControl import InputControl
+
+
+class MouseInputControl(InputControl):
+
+ def __init__(self, min_value, max_value, start_value=None, step=None,
+ **kwargs):
+
+ if not kwargs.has_key("StyleName"):
+ kwargs['StyleName'] = "gwt-MouseInputControl"
+ InputControl.__init__(self, min_value, max_value, start_value,
+ step, **kwargs)
+
+ self.addMouseListener(self)
+ self.setDragable(True)
+
+ def onFocus(self, sender):
+ self.addStyleName("gwt-MouseInputControl-focussed")
+
+ def onLostFocus(self, sender):
+ self.removeStyleName("gwt-MouseInputControl-focussed")
+ self.dragging = False
+ DOM.releaseCapture(self.getElement())
+
+ def moveControl(self, mouse_x, mouse_y):
+ height_range = 50
+ widget_height = self.getOffsetHeight()
+ relative_y = mouse_y - (widget_height / 2)
+ if relative_y < 0:
+ relative_y = 0
+ if relative_y >= height_range:
+ relative_y = height_range
+
+ relative_y = height_range - relative_y # turn round (bottom to top)
+
+ val_diff = self.max_value - self.min_value
+ new_value = ((val_diff * relative_y) / height_range) + self.min_value
+ new_value = self.processValue(new_value)
+
+ self.setControlPos(new_value)
+ self.setValue(new_value)
+
+Factory.registerClass('pyjamas.ui.MouseInputControl', InputControl)

0 comments on commit c4433f7

Please sign in to comment.