Drag and Drop in a locked field

André Bisseret andre.bisseret at wanadoo.fr
Fri Feb 10 04:13:30 EST 2012


Bonjour Bob,

In some apps I am using the following script.
It is an adaptation to drap and drop inside one field, of a script from Eric Chatonet he had written for drag and drop from one field to another.

Besides the field, this script uses an extra field name "tempo" which allows to see what line one is dragging.

A bug (# 6823) prevented this script to work well in a grouped field (but the Slug had writte a workaround).

Seems that now this bug has been fixed : I just tried the script on a grouped field and it works.

Best regards from Grenoble

André



----------- THE SCRIPT OF THE FIELD -----------------------

local lLineNo,tLaLigne,tStyle, -- script local variable
----------------------------------
on mouseEnter
    set the dragDelta to 5 -- instead of 10 seems to prevent to drag a wrong line too often
end mouseEnter
----------------------------------
on dragMove -- sent repeatedly when the dragdata is within the field
    local tContents,tHilitedText
    -----
    put (the mouseV - the top of the target) div the effective textHeight of the target into lLineNo -- line number
    if lLineNo > the number of lines of the target then put the number of lines of the target into lLineNo
    -----
    lock screen
    put StripCR(me) into tContents -- see below
    put the hilitedText of the target into tHilitedText -- stores highlighted line
    put cr after line lLineNo of tContents -- creates a blank line
    set the text of the target to tContents
    set the hilitedLine of the target to lineOffset(tHilitedText,tContents) -- restores highlighted line
    unlock screen
    delete line (the hilitedLine of the target) of the target
end dragMove

on DragLeave -- restores current contents
    local tHilitedLine,
    -----------
    lock screen
    put the hilitedLine of the target into tHilitedLine
    put StripCR(me) into the target --
    set the hilitedLine of the target to tHilitedLine
    unlock screen
end DragLeave
----------------------------------
on dragEnter -- NÉCESSAIRE DANS LE CONTROL DE DESTINATION
    set the dragAction to "Move"
end dragEnter
----------------------------------
on dragDrop
    local tContents
    -----
    put StripCR(me) into tContents -- see below
    if lLineNo = 0 then
        put the dragdata["text"] & cr before tContents
    else
        put cr & the dragdata["text"] after line lLineNo of tContents
    end if
    lock screen
    set the text of the target to tContents
    set the hilitedLine of the target to lLineNo + 1 -- sets highlighted line to dropped line
    unlock screen
end dragDrop
----------------------------------
function StripCR pText -- returns 'normal' field's contents
    if char 1 of pText = cr then delete char 1 of pText
    if char -1 of pText = cr then delete char -1 of pText
    replace cr & cr with cr in pText
    return pText
end StripCR
-- ---------------------------------
on dragStart --NÉCESSAIRE DANS LE CONTROL SOURCE
    set the dragData["text"] to the selectedText
    put the selectedText of the target into fld "Temp" -- in order to create the right image
    -- put tLaLigne into fld "Temp"
    set the dragImage to CreateDragImage() -- see below
    set the dragImageOffset to the width of last image div 2,the height of last image div 2 -- centered
end dragStart

on dragEnd
    delete last image -- we no longer need the drag image
end dragEnd
---------------------------------
function CreateDragImage -- creates drag image on-the-fly
    local tRect,tempImage
    -----
    put the rect of fld "Temp" into tRect
    -----
    export snapshot from rect tRect of this cd to tempImage as jpeg
    -- export image to a variable
    lock messages -- important to speed up the process
    create invisible image -- we don't want to see it
    set the text of last image to tempImage
    if there is an image ID 6001 then delete image ID 6001
    set the ID of last image to 6001 -- setting dimensions
    unlock messages
    return the ID of last image -- we need it to set the dragImage property
end CreateDragImage
------------------------------------------------------------------------------------------------


Le 10 févr. 2012 à 01:35, Bob Sneidar a écrit :

> Hi all. I use locked fields with multiple lines and listBehavior set to true as a kind of menu. Now I am trying to allow the user to rearrange the items in the list. I read in the Dictionary that LC handles drag and drop between *unlocked* fields automatically. Crud. So I tried unlocking the field on mouseDown and then sending mouseDown again in 0 seconds, but that was a bust. I tried just unlocking the field on mouseDown and locking it on mouseUp but that was problematic too. 
> 
> Any easy way to implement this? I could use a datagrid I suppose (grumble grumble) but it would be nice if I could just do this with a simple list field. I want to reorder entire lines by dragging and dropping them. 
> 
> Bob
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list