def setDragStartPosition(event): #, undo): dt = event.dataTransfer target = DOM.eventGetTarget(event) clientX, clientY = event.clientX, event.clientY absx, absy = clientX + Window.getScrollLeft( ), clientY + Window.getScrollTop() # para arrastar um component ou bloco entre abas from edu.uca.renderable.block.Block import Block from edu.uca.util.Serializable import saveStackBlock from edu.uca.Workspace import getWorkspace from edu.uca.renderable.hardware.ComponentBlock import ComponentBlock from edu.uca.util.Serializable import saveComponent if (isinstance(getWorkspace().getMovingBlock(), ComponentBlock)): package = json.dumps({ "offsetX": absx - DOM.getAbsoluteLeft(target), # armagenar a origem para undo "offsetY": absy - DOM.getAbsoluteTop(target), "componetHardware": saveComponent(getWorkspace().getMovingBlock()) }) elif (isinstance(getWorkspace().getMovingBlock(), Block)): package = json.dumps({ "offsetX": absx - DOM.getAbsoluteLeft(target), # armagenar a origem para undo "offsetY": absy - DOM.getAbsoluteTop(target), "stackBlock": saveStackBlock(getWorkspace().getMovingBlock()) }) dt.setData('text', package) dt.allowedEffects = 'copy' # using "copy" here because Windows Chrome does not like "move"
def fireMouseEvent(listeners, sender, event): x = DOM.eventGetClientX(event) - DOM.getAbsoluteLeft(sender.getElement()) y = DOM.eventGetClientY(event) - DOM.getAbsoluteTop(sender.getElement()) etype = DOM.eventGetType(event) if etype == "mousedown": for listener in listeners: listener.onMouseDown(sender, x, y) return True elif etype == "mouseup": for listener in listeners: listener.onMouseUp(sender, x, y) return True elif etype == "mousemove": for listener in listeners: listener.onMouseMove(sender, x, y) return True elif etype == "mouseover": from_element = DOM.eventGetFromElement(event) if not DOM.isOrHasChild(sender.getElement(), from_element): for listener in listeners: listener.onMouseEnter(sender) return True elif etype == "mouseout": to_element = DOM.eventGetToElement(event) if not DOM.isOrHasChild(sender.getElement(), to_element): for listener in listeners: listener.onMouseLeave(sender) return True return False
def onElementContextmenu(self, sender,event): x = DOM.eventGetClientX(event) - DOM.getAbsoluteLeft(self.canvas.getElement()) y = DOM.eventGetClientY(event) - DOM.getAbsoluteTop(self.canvas.getElement()) s = self.desc[self.elements.index(sender)] self.set_status('Contextmenue on Element '+s+' at '+str(x)+', '+str(y)) DOM.eventCancelBubble(event,True) DOM.eventPreventDefault(event)
def getCurrentOffsetPosition(event): target = DOM.eventGetTarget(event) clientX, clientY = event.clientX, event.clientY # pega pos absoluta do ponteiro do mouse absx, absy = clientX + Window.getScrollLeft( ), clientY + Window.getScrollTop() # ? - sempre 0,0 return absx - DOM.getAbsoluteLeft(target), absy - DOM.getAbsoluteTop( target) # subtrai pos xy do blockspad
def onCanvasContextmenu(self, sender, event): x = DOM.eventGetClientX(event) - DOM.getAbsoluteLeft( self.canvas.getElement()) y = DOM.eventGetClientY(event) - DOM.getAbsoluteTop( self.canvas.getElement()) self.set_status('Contextmenue on Canvas at ' + str(x) + ', ' + str(y)) DOM.eventPreventDefault(event)
def onElementDblClick(self, sender, event): x = DOM.eventGetClientX(event) - DOM.getAbsoluteLeft( self.canvas.getElement()) y = DOM.eventGetClientY(event) - DOM.getAbsoluteTop( self.canvas.getElement()) s = self.desc[self.elements.index(sender)] self.set_status('Doubleclick on Element ' + s + ' at ' + str(x) + ', ' + str(y)) DOM.eventCancelBubble(event, True)
def __init__(self): """ Standard initialiser. """ VerticalPanel.__init__(self) self.canvas = Raphael(900,700) self.add(self.canvas) x = DOM.getAbsoluteLeft(self.canvas.getElement()) y = DOM.getAbsoluteTop(self.canvas.getElement()) self.offset = (x,y)
def onElementContextmenu(self, sender, event): x = DOM.eventGetClientX(event) - DOM.getAbsoluteLeft( self.canvas.getElement()) y = DOM.eventGetClientY(event) - DOM.getAbsoluteTop( self.canvas.getElement()) s = self.desc[self.elements.index(sender)] self.set_status('Contextmenue on Element ' + s + ' at ' + str(x) + ', ' + str(y)) DOM.eventCancelBubble(event, True) DOM.eventPreventDefault(event)
def isIn(element, x, y): """ Given an element and an absolute x and y, return True if the (x,y) coordinate is within the element. Otherwise, return False. """ top = DOM.getAbsoluteTop(element) height = DOM.getIntAttribute(element, 'offsetHeight') if y >= top and y <= top + height: left = DOM.getAbsoluteLeft(element) width = DOM.getIntAttribute(element, 'offsetWidth') if x >= left and x <= left + width: return True return False
def onBrowserEvent(self, event): kind = DOM.eventGetType(event) x = DOM.eventGetClientX(event) - DOM.getAbsoluteLeft(self.getElement()) y = DOM.eventGetClientY(event) - DOM.getAbsoluteTop(self.getElement()) y = HEIGHT - y if kind == "mousedown": self.onMouseDown(x,y) self._dragging = True elif kind == "mousemove" and self._dragging: self.onMouseDrag(x,y) elif (kind == "mouseup" or kind == "mouseout") and self._dragging: self._dragging = False self.onMouseUp(x,y)
def _move_cursor(self, col): """ moves the css-styled cursor """ #old style (useful for insert mode - don't delete this line for now!) #self.cf.setStyleName(0, col, "inputbox-square-word-cursor", highlight) el = self.cf.getRawElement(0, col+1) w = self.getWidget() px = self.tp.getAbsoluteLeft() py = self.tp.getAbsoluteTop() width = DOM.getOffsetWidth(el) px = DOM.getAbsoluteLeft(el) - px py = DOM.getAbsoluteTop(el) - py w.setWidgetPosition(self.cursor, px, py)
def onBrowserEvent(self, event): print "Event happened" kind = DOM.eventGetType(event) x = DOM.eventGetClientX(event) - DOM.getAbsoluteLeft(self.canvas.getElement()) y = DOM.eventGetClientY(event) - DOM.getAbsoluteTop(self.canvas.getElement()) print "Type of event: %s" % (kind) if kind == "mousedown": self._dragging = True self.onMouseDown(x,y) elif kind == "mousemove" and self._dragging: self.onMouseDrag(x,y) elif (kind == "mouseup" or kind == "mouseout") and self._dragging: self._dragging = False self.onMouseUp(x,y)
def setDragImage(self, element, x, y): position_absolute = DOM.getStyleAttribute(element, 'position') == 'absolute' if position_absolute: self.dragLeftOffset = x + DOM.getAbsoluteLeft(element.offsetParent) self.dragTopOffset = y + DOM.getAbsoluteTop(element.offsetParent) else: self.dragLeftOffset = x self.dragTopOffset = y if element.tagName.lower().endswith('img'): src = DOM.getAttribute(element, 'src') element = DOM.createElement('img') DOM.setAttribute(element, 'src', src) if not self.draggingImage: self.createDraggingImage(element) else: self.draggingImage.setImage(element)
def onDragStart(self, event): dt = event.dataTransfer target = DOM.eventGetTarget(event) clientX = event.clientX clientY = event.clientY absx = clientX + Window.getScrollLeft() absy = clientY + Window.getScrollTop() package = json.encode({"text": DOM.getInnerText(target), "offsetX": absx - DOM.getAbsoluteLeft(target), "offsetY": absy - DOM.getAbsoluteTop(target)}) dt.setData('text', package) # using "copy" here because Windows Chrome does not like "move" dt.allowedEffects = 'copy' self.movingWidget = None for widget in self.children: if target == widget.getElement(): self.movingWidget = widget
def setDragImage(self, element, x, y): position_absolute = DOM.getStyleAttribute(element, 'position') == 'absolute' if position_absolute: self.dragLeftOffset = x + DOM.getAbsoluteLeft( element.offsetParent) self.dragTopOffset = y + DOM.getAbsoluteTop( element.offsetParent) else: self.dragLeftOffset = x self.dragTopOffset = y if element.tagName.lower().endswith('img'): src = DOM.getAttribute(element,'src') element = DOM.createElement('img') DOM.setAttribute(element, 'src', src) if not self.draggingImage: self.createDraggingImage(element) else: self.draggingImage.setImage(element)
def onDragStart(self, event): dt = event.dataTransfer target = DOM.eventGetTarget(event) clientX = event.clientX clientY = event.clientY absx = clientX + Window.getScrollLeft() absy = clientY + Window.getScrollTop() package = json.encode({ "text": DOM.getInnerText(target), "offsetX": absx - DOM.getAbsoluteLeft(target), "offsetY": absy - DOM.getAbsoluteTop(target) }) dt.setData('text', package) # using "copy" here because Windows Chrome does not like "move" dt.allowedEffects = 'copy' self.movingWidget = None for widget in self.children: if target == widget.getElement(): self.movingWidget = widget
def onClick(self, sender, evt): x = DOM.eventGetClientX(evt) - DOM.getAbsoluteLeft(self.getElement()) y = DOM.eventGetClientY(evt) - DOM.getAbsoluteTop(self.getElement()) xth = int(x / uwidth) yth = int(y / uheight) box = self.map[yth][xth] if not box: return box.toggleSelect() if not box.selected: self.selected_box = None elif self.selected_box == None: self.selected_box = box elif box.img != self.selected_box.img: # change selection self.selected_box.unSelect() self.selected_box = box else: sbox = self.selected_box print 'check' result = False if box.xth < sbox.xth: print(box.yth, box.xth), (sbox.yth, sbox.xth) result = checkllk(self.map, (box.yth, box.xth), (sbox.yth, sbox.xth)) else: print(sbox.yth, sbox.xth), (box.yth, box.xth) result = checkllk(self.map, (sbox.yth, sbox.xth), (box.yth, box.xth)) if result: print 'path:', result sbox.dead() self.map[sbox.yth][sbox.xth] = 0 box.dead() self.map[box.yth][box.xth] = 0 self.selected_box = None else: print 'fail' self.selected_box.unSelect() self.selected_box = box
def moveFocus(self, selection): focusableWidget = selection.getFocusableWidget() if focusableWidget is not None: focusableWidget.setFocus(True) DOM.scrollIntoView(focusableWidget.getElement()) else: selectedElem = selection.getContentElem() containerLeft = self.getAbsoluteLeft() containerTop = self.getAbsoluteTop() left = DOM.getAbsoluteLeft(selectedElem) - containerLeft top = DOM.getAbsoluteTop(selectedElem) - containerTop width = DOM.getIntAttribute(selectedElem, "offsetWidth") height = DOM.getIntAttribute(selectedElem, "offsetHeight") DOM.setIntStyleAttribute(self.focusable, "left", "%spx" % left) DOM.setIntStyleAttribute(self.focusable, "top", "%spx" % top) DOM.setIntStyleAttribute(self.focusable, "width", "%spx" % width) DOM.setIntStyleAttribute(self.focusable, "height", "%spx" % height) DOM.scrollIntoView(self.focusable) Focus.focus(self.focusable)
def moveFocus(self, selection): focusableWidget = selection.getFocusableWidget() if focusableWidget is not None: focusableWidget.setFocus(True) DOM.scrollIntoView(focusableWidget.getElement()) else: selectedElem = selection.getContentElem() containerLeft = self.getAbsoluteLeft() containerTop = self.getAbsoluteTop() left = DOM.getAbsoluteLeft(selectedElem) - containerLeft top = DOM.getAbsoluteTop(selectedElem) - containerTop width = DOM.getIntAttribute(selectedElem, "offsetWidth") height = DOM.getIntAttribute(selectedElem, "offsetHeight") DOM.setIntStyleAttribute(self.focusable, "left", left) DOM.setIntStyleAttribute(self.focusable, "top", top) DOM.setIntStyleAttribute(self.focusable, "width", width) DOM.setIntStyleAttribute(self.focusable, "height", height) DOM.scrollIntoView(self.focusable) Focus.focus(self.focusable)
def onClick(self, sender, evt): x = DOM.eventGetClientX(evt)-DOM.getAbsoluteLeft(self.getElement()) y = DOM.eventGetClientY(evt)-DOM.getAbsoluteTop(self.getElement()) xth = int(x/uwidth) yth = int(y/uheight) box = self.map[yth][xth] if not box: return box.toggleSelect() if not box.selected: self.selected_box = None elif self.selected_box==None: self.selected_box = box elif box.img != self.selected_box.img: # change selection self.selected_box.unSelect() self.selected_box = box else: sbox = self.selected_box print 'check' result = False if box.xth<sbox.xth: print (box.yth,box.xth), (sbox.yth,sbox.xth) result = checkllk(self.map, (box.yth,box.xth), (sbox.yth,sbox.xth)) else: print (sbox.yth,sbox.xth), (box.yth,box.xth) result = checkllk(self.map, (sbox.yth,sbox.xth), (box.yth,box.xth)) if result: print 'path:', result sbox.dead() self.map[sbox.yth][sbox.xth] = 0 box.dead() self.map[box.yth][box.xth] = 0 self.selected_box = None else: print 'fail' self.selected_box.unSelect() self.selected_box = box
def __init__(self): """ Standard initialiser. """ VerticalPanel.__init__(self) GUI_Event.__init__(self) self.canvas = Raphael(900,700) #self.canvas.addListener('mousedown',self.test) #self.canvas.addListener('contextmenu',self.test) #self.tb = TextArea() #self.tb.setName("textBoxFormElement") #self.tb.setStyleAttribute({'position':'absolute', # 'top':'175px','left':'158px' # ,'color':COLOR['maroon'] # ,'background':COLOR['peachpuff'] # ,'background-color':COLOR['peachpuff']}) #self.tb.setSize(450,100) #self.tb.setReadonly('readonly') self.add(self.canvas) #self.add(self.tb) x = DOM.getAbsoluteLeft(self.canvas.getElement()) y = DOM.getAbsoluteTop(self.canvas.getElement()) self.offset = (x,y) '''
def onCanvasMouseup(self,obj,event): x = DOM.eventGetClientX(event) - DOM.getAbsoluteLeft(self.canvas.getElement()) y = DOM.eventGetClientY(event) - DOM.getAbsoluteTop(self.canvas.getElement()) self.set_status('Mousemove on Canvas at '+str(x)+', '+str(y))
def onCanvasDblClick(self, sender, event): x = DOM.eventGetClientX(event) - DOM.getAbsoluteLeft( self.canvas.getElement()) y = DOM.eventGetClientY(event) - DOM.getAbsoluteTop( self.canvas.getElement()) self.set_status('Doubleclick on Canvas at ' + str(x) + ', ' + str(y))
def onElementDblClick(self,sender,event): x = DOM.eventGetClientX(event) - DOM.getAbsoluteLeft(self.canvas.getElement()) y = DOM.eventGetClientY(event) - DOM.getAbsoluteTop(self.canvas.getElement()) s = self.desc[self.elements.index(sender)] self.set_status('Doubleclick on Element '+s+' at '+str(x)+', '+str(y)) DOM.eventCancelBubble(event,True)
def new_mouse_pos(self, x, y): el = self.canvas.getElement() self.mouse_pos_x = x - DOM.getAbsoluteLeft(el) self.mouse_pos_y = y - DOM.getAbsoluteTop(el)
def onCanvasDblClick(self,sender,event): x = DOM.eventGetClientX(event) - DOM.getAbsoluteLeft(self.canvas.getElement()) y = DOM.eventGetClientY(event) - DOM.getAbsoluteTop(self.canvas.getElement()) self.set_status('Doubleclick on Canvas at '+str(x)+', '+str(y))
def onCanvasContextmenu(self, sender,event): x = DOM.eventGetClientX(event) - DOM.getAbsoluteLeft(self.canvas.getElement()) y = DOM.eventGetClientY(event) - DOM.getAbsoluteTop(self.canvas.getElement()) self.set_status('Contextmenue on Canvas at '+str(x)+', '+str(y)) DOM.eventPreventDefault(event)
def addTo(self, panel): panel.add(self) self.top = DOM.getAbsoluteTop(self.getElement()) self.left = DOM.getAbsoluteLeft(self.getElement())
def onElementMousemove(self, sender, event): x = DOM.eventGetClientX(event) - DOM.getAbsoluteLeft(self.canvas.getElement()) y = DOM.eventGetClientY(event) - DOM.getAbsoluteTop(self.canvas.getElement()) s = self.desc[self.elements.index(sender)] self.set_status("Mousemove on Element " + s + " at " + str(x) + ", " + str(y)) DOM.eventCancelBubble(event, True)
def getAbsoluteLeft(self): return DOM.getAbsoluteLeft(self.getElement())
def onCanvasMousedown(self, sender, event): x = DOM.eventGetClientX(event) - DOM.getAbsoluteLeft( self.canvas.getElement()) y = DOM.eventGetClientY(event) - DOM.getAbsoluteTop( self.canvas.getElement()) self.set_status('Mousedown on Canvas at ' + str(x) + ', ' + str(y))
def onCanvasMousedown(self, sender, event): x = DOM.eventGetClientX(event) - DOM.getAbsoluteLeft(self.canvas.getElement()) y = DOM.eventGetClientY(event) - DOM.getAbsoluteTop(self.canvas.getElement()) self.set_status("Mousedown on Canvas at " + str(x) + ", " + str(y))
def onMouseMove(self, sender, x, y): event = DOM.eventGetCurrentEvent() self.mouseEvent = event button = DOM.eventGetButton(event) if not button == Event.BUTTON_LEFT: return ## The following commented code lets the native dnd happen in IE. sucks. ## But it may enable dragging our widgets out of IE into other apps. # else: # try: # self.dragWidget.getElement().dragDrop() # return # except: # pass # Adjust x and y to absolute coordinates. x, y = eventCoordinates(event) if self.dragging == DRAGGING_NO_MOVEMENT_YET: self.origMouseX = x self.origMouseY = y self.currentDragOperation = 'none' fromElement = self.dragWidget.getElement() # Is the widget itself draggable? try: draggable = fromElement.draggable except: draggable = False # if not, find the draggable element at (x, y) in the widget if not draggable: fromElement = findDraggable(sender.getElement(), self.origMouseX, self.origMouseY) # Nothing draggable found. return. if fromElement is None: self.dragging = NOT_DRAGGING return # Get the location for the dragging widget #self.absParent = None #self.absParent = self.dragWidget.getParent() #self.absLeft = DOM.getStyleAttribute(fromElement, 'left') #print self.absLeft #self.absTop = DOM.getStyleAttribute(fromElement, 'top') #print self.absTop #self.origTop = DOM.getAbsoluteTop(fromElement) + parent.getAbsoluteTop() #self.origLeft = DOM.getAbsoluteLeft(fromElement) + parent.getAbsoluteLeft() self.origTop = DOM.getAbsoluteTop(fromElement) self.origLeft = DOM.getAbsoluteLeft(fromElement) #self.glassTop = DOM.getAbsoluteTop(fromElement.offsetParent) #self.glassLeft = DOM.getAbsoluteTop(fromElement.offsetParent) position_absolute = DOM.getStyleAttribute(fromElement, 'position') == 'absolute' if position_absolute: self.dragLeftOffset = (self.origMouseX - DOM.getAbsoluteLeft(fromElement.offsetParent)) self.dragTopOffset = (self.origMouseY - DOM.getAbsoluteTop(fromElement.offsetParent)) else: self.dragLeftOffset = self.origMouseX - self.origLeft self.dragTopOffset = self.origMouseY - self.origTop # self.setDragImage(fromElement, # self.origMouseX - self.origLeft, # self.origMouseY - self.origTop) self.dragDataStore.elements = [fromElement] dragStartEvent = self.fireDNDEvent('dragstart', None, self.dragWidget) if not isCanceled(dragStartEvent): self.initFeedbackImage() RootPanel().add(self.draggingImage) self.setDragImageLocation(x, y) self.dragging = ACTIVELY_DRAGGING GlassWidget.show(self) elif self.dragging == ACTIVELY_DRAGGING: try: doc().selection.empty() except: wnd().getSelection().removeAllRanges() self.setDragImageLocation(x, y) # If we are still working on the previous iteration, or if we have # done this recently, we'll wait for the next event. if self.dragBusy or time.time() - self.drag_time < 0.25: return self.doDrag(event, x, y) self.drag_time = time.time()
def onMouseMove(self, sender, x, y): event = DOM.eventGetCurrentEvent() self.mouseEvent = event button = DOM.eventGetButton(event) if not button == Event.BUTTON_LEFT: return ## The following commented code lets the native dnd happen in IE. sucks. ## But it may enable dragging our widgets out of IE into other apps. # else: # try: # self.dragWidget.getElement().dragDrop() # return # except: # pass # Adjust x and y to absolute coordinates. x, y = eventCoordinates(event) if self.dragging == DRAGGING_NO_MOVEMENT_YET: self.origMouseX = x self.origMouseY = y self.currentDragOperation = 'none' fromElement = self.dragWidget.getElement() # Is the widget itself draggable? try: draggable = fromElement.draggable except: draggable = False # if not, find the draggable element at (x, y) in the widget if not draggable: fromElement = findDraggable(sender.getElement(), self.origMouseX, self.origMouseY) # Nothing draggable found. return. if fromElement is None: self.dragging = NOT_DRAGGING return # Get the location for the dragging widget #self.absParent = None #self.absParent = self.dragWidget.getParent() #self.absLeft = DOM.getStyleAttribute(fromElement, 'left') #print self.absLeft #self.absTop = DOM.getStyleAttribute(fromElement, 'top') #print self.absTop #self.origTop = DOM.getAbsoluteTop(fromElement) + parent.getAbsoluteTop() #self.origLeft = DOM.getAbsoluteLeft(fromElement) + parent.getAbsoluteLeft() self.origTop = DOM.getAbsoluteTop(fromElement) self.origLeft = DOM.getAbsoluteLeft(fromElement) #self.glassTop = DOM.getAbsoluteTop(fromElement.offsetParent) #self.glassLeft = DOM.getAbsoluteTop(fromElement.offsetParent) position_absolute = DOM.getStyleAttribute(fromElement, 'position') == 'absolute' if position_absolute: self.dragLeftOffset = ( self.origMouseX - DOM.getAbsoluteLeft(fromElement.offsetParent)) self.dragTopOffset = ( self.origMouseY - DOM.getAbsoluteTop(fromElement.offsetParent)) else: self.dragLeftOffset = self.origMouseX - self.origLeft self.dragTopOffset = self.origMouseY - self.origTop # self.setDragImage(fromElement, # self.origMouseX - self.origLeft, # self.origMouseY - self.origTop) self.dragDataStore.elements = [fromElement] dragStartEvent = self.fireDNDEvent('dragstart', None, self.dragWidget) if not isCanceled(dragStartEvent): self.initFeedbackImage() RootPanel().add(self.draggingImage) self.setDragImageLocation(x, y) self.dragging = ACTIVELY_DRAGGING GlassWidget.show(self) elif self.dragging == ACTIVELY_DRAGGING: try: doc().selection.empty() except: wnd().getSelection().removeAllRanges() self.setDragImageLocation(x, y) # If we are still working on the previous iteration, or if we have # done this recently, we'll wait for the next event. if self.dragBusy or time.time() - self.drag_time < 0.25: return self.doDrag(event, x, y) self.drag_time = time.time()