Reordering the rows or lines in a list field

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Reordering the rows or lines in a list field

Post by Simon Knight » Mon May 28, 2018 12:42 pm

Hi,

I'm a little stuck!

For reasons that I won't go into I wish to use a list field to present a list of thumb nail images for the user to interact with. One action I am trying to implement is using the mouse to change the order of the rows by select, hold and drag then release.

To do this I believe that I need to identify the selected row then detect where the user wishes the selected row moved to. This last part is proving difficult. I am trying to use the mouseloc at the time of mouse release as a means of calculating which row is under the mouse when it is released. The calculation is fairly simple if the lines all have the same height but in my field they don't because the thumbnails vary in size. Does anyone know of a method of getting the row heights from a list field or a better method of determining the row under the mouse?

My next step is to obtain and store list of heights of the images displayed in each row of the field but I wonder if there is a simpler way.
best wishes
Skids

Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Reordering the rows or lines in a list field

Post by Simon Knight » Mon May 28, 2018 1:18 pm

Answering my own question - this shows promise but there may well be a better way.

Code: Select all

on mouseMove pNewMouseH, pNewMouseV
   put mouseCharChunk()   into tCharChunk
   put  "Line No. " & GetLineNumber (tCharChunk) into field "debug2" 
end mouseMove

Function GetLineNumber pCharChunk
   put word 2 of pCharChunk into tPosnFirstChar  -- a number
   put field "imageList" into tFieldText  -- the plain text from the field
   put 1 into tLineCounter  -- line counts start at one
   repeat with count = 1 to tPosnFirstChar
      if char count of tFieldText is cr then
         add one to tLineCounter
      end if
   end repeat
   return tLinecounter
end GetLineNumber
best wishes
Skids

Klaus
Posts: 13793
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Reordering the rows or lines in a list field

Post by Klaus » Mon May 28, 2018 1:28 pm

Hi Keith,

you can have that much shorter:

Code: Select all

on mouseMove
   put  "Line No. " & the lineindex of the mouseCharChunk into field "debug2"  
end mouseMove
:D


Best

Klaus

Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Reordering the rows or lines in a list field

Post by Simon Knight » Mon May 28, 2018 2:28 pm

Thanks and also Doh!!! :roll:
best wishes
Skids

Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Reordering the rows or lines in a list field

Post by Simon Knight » Mon May 28, 2018 7:38 pm

I have managed to get to a stage where my list field is fit for others to see. I have attached a stack file, all the code is stored in the single field's object script. I have never tried stretching the "simple" controls before and am quite surprised how effective a list field can be.

Thanks to all who offered help here and to Scott Rossi who published an example list field with bells and whistles back in 2007. I have removed some of the functions that Scott included which simplifies my code and helps my understanding. I have probably added to many comments for most peoples taste but I know they will help me in six months/weeks/hours time when I come to read the scripts

My field is populated with image thumb nails and may be reordered by dragging a row to a new location.

The next step will be to enable text entry and editing.
Attachments
DemoListControl.livecode.zip
Zip of demo stack and list field
(4.4 KiB) Downloaded 228 times
best wishes
Skids

trinitylogics
Posts: 7
Joined: Mon Jul 02, 2018 4:17 am

Re: Reordering the rows or lines in a list field

Post by trinitylogics » Fri Jul 06, 2018 10:43 pm

Nice work Simon.

I like the idea of using a single field for some complex tasks.

Here is an example of a group list (using graphics as the list items for demo) where the drag and drop to reorder the list is implemented. Probably needs more comments, but maybe you will find it useful.
DragOrderList.livecode.zip
(5.93 KiB) Downloaded 217 times

Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Reordering the rows or lines in a list field

Post by Simon Knight » Wed Jul 11, 2018 1:00 pm

Thanks for the example. I like the way the your list adjusts to provide space for the dragged item ready for a drop. I have had a quick look at your code and will have a more in depth look when I have more time. Even my quick look has taught me something as I had not thought of using a static array to provide access to variables without declaring them at the start of the code.

Another excellent example of what may be achieved using "simple" controls without having to use a datagrid. Thanks,

Simon K.
best wishes
Skids

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am
Location: London, UK

Re: Reordering the rows or lines in a list field

Post by marksmithhfx » Fri Jul 01, 2022 3:14 pm

Simon Knight wrote:
Mon May 28, 2018 7:38 pm
Thanks to all who offered help here and to Scott Rossi who published an example list field with bells and whistles back in 2007. I have removed some of the functions that Scott included which simplifies my code and helps my understanding. I have probably added to many comments for most peoples taste but I know they will help me in six months/weeks/hours time when I come to read the scripts

My field is populated with image thumb nails and may be reordered by dragging a row to a new location.
Thanks for posting this terrific example Simon. I dropped it into a project I was working on and it worked perfectly (after a few tweaks to make it more closely fit the style I was looking for). One problem I did run into is that when built it causes macOS to pop-up an alert asking for permission to "copy the screen". This is probably a fairly recent addition to the OS. Following some advice from Mark Waddington, I eliminated the section titled "convert from screen to window coordinates - using globalLoc" and added "of me" to the export snapshot command and between the two of them (I did not test them separately) this got rid of the permissions pop-up dialog.

The sections I modified are in the handler "buildImageToDrag"

Hope this is useful to anyone else who might be interested in using your code.

best,
Mark
Attachments
DemoListControl5.livecode.zip
(7.71 KiB) Downloaded 125 times
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

Klaus
Posts: 13793
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Reordering the rows or lines in a list field

Post by Klaus » Fri Jul 01, 2022 5:34 pm

Hi Mark,

I usually (mis-)use a datagrid with the new features to provide a "text list" to the user that can be reordered by dragging.
All build-in nowadays, please check this lesson, which got me started:
https://lessons.livecode.com/m/datagrid ... -data-grid

Best

Klaus

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am
Location: London, UK

Re: Reordering the rows or lines in a list field

Post by marksmithhfx » Fri Jul 01, 2022 5:37 pm

Klaus wrote:
Fri Jul 01, 2022 5:34 pm
I usually (mis-)use a datagrid with the new features to provide a "text list" to the user that can be reordered by dragging.
All build-in nowadays, please check this lesson, which got me started:
https://lessons.livecode.com/m/datagrid ... -data-grid
Good suggestion Klaus, thanks
Mark
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

Post Reply

Return to “Talking LiveCode”