Page 1 of 1

xbrowse drag / drop (to Mr. Rao)

Posted: Mon Nov 04, 2024 10:17 pm
by ralph
I asked Copilot to build a program to allow reorder rows in a list using drag & drop and xBrowse
This is the result:

Code: Select all | Expand

#include "FiveWin.ch"
function Main()
    local oDlg, oBrw, aData

    aData := { ;
        { "Item 1", "Codigo 1", "Producto 1", 10, "Uni 1", 100, 5, 95 }, ;
        { "Item 2", "Codigo 2", "Producto 2", 20, "Uni 2", 200, 10, 190 }, ;
        { "Item 3", "Codigo 3", "Producto 3", 30, "Uni 3", 300, 15, 285 } ;
    }

    DEFINE DIALOG oDlg TITLE "Reordenar filas con Drag & Drop" SIZE 800, 400

    @ 10, 10 XBROWSE oBrw ARRAY aData OF oDlg ;
        COLUMNS { 1, 2, 3, 4, 5, 6, 7, 8 } ;
        HEADERS { "Item", "Codigo", "Nombre del producto", "Cantid", "Uni", "Pr.Unit", "Dscto", "Importe" } ;
        COLSIZES { 100, 100, 200, 100, 50, 100, 100, 100 } ;
        CELL LINES NOBORDER

    oBrw:nMarqueeStyle := MARQSTYLE_HIGHLROW
    oBrw:nColDividerStyle := LINESTYLE_BLACK
    oBrw:nRowDividerStyle := LINESTYLE_BLACK
    oBrw:lAllowRowSizing := .F.
    oBrw:lAllowColSwapping := .F.

    // Habilitar Drag & Drop
    oBrw:lDragDrop := .T.
    oBrw:bDragBegin := { |nRow| oBrw:nDragRow := nRow }
    oBrw:bDragEnd := { |nRow| ReorderRows( oBrw, nRow ) }

    ACTIVATE DIALOG oDlg CENTERED

return nil

function ReorderRows( oBrw, nRow )
    local aTemp

    if oBrw:nDragRow != nRow
        aTemp := oBrw:aArrayData[ oBrw:nDragRow ]
        ADel( oBrw:aArrayData, oBrw:nDragRow )
        AIns( oBrw:aArrayData, nRow, aTemp )
        oBrw:Refresh()
    endif

return nil

 
It uses some methods that do not exist (unless the IA read the future)

Code: Select all | Expand

    // Habilitar Drag & Drop
    oBrw:lDragDrop := .T.
    oBrw:bDragBegin := { |nRow| oBrw:nDragRow := nRow }
    oBrw:bDragEnd := { |nRow| ReorderRows( oBrw, nRow ) }
 
To Mr. Rao: Is there a way to get this functionality ?

Re: xbrowse drag / drop (to Mr. Rao)

Posted: Tue Nov 05, 2024 1:53 pm
by nageswaragunupudi
FWH already had full support for Drag and Drop feature, not only inside Xbrowse but across all controls.
This feature has been there from the time of creation of FiveWin.
So, nothing futuristic.

Simple sample:

Code: Select all | Expand

#include "fivewin.ch"

function Main()

   local aData, oCur

   USE STATES SHARED NEW
   aData := FW_DbfToArray()
   CLOSE STATES

   DEFINE CURSOR oCur HAND
   XBROWSER aData SETUP xSetup1( oBrw, oCur )
   RELEASE CURSOR oCur

return nil

function xSetup1( oBrw, oCur )

   oBrw:oDragCursor  := oCur

   oBrw:bDragBegin   := { |r,c,f| SetDropInfo( oBrw:nArrayAt ) }

   oBrw:bDropOver    := < |u,r,c,f|
      oBrw:SetPos( r, c, .t. )
      StackPush( oBrw:aRow )
      oBrw:aArrayData[ oBrw:nArrayAt ] := oBrw:aArrayData[ u ]
      oBrw:aArrayData[ u ] := StackPop()
      oBrw:Refresh()
      return nil
      >

return nil
Image

Re: xbrowse drag / drop (to Mr. Rao)

Posted: Tue Nov 05, 2024 4:24 pm
by Marc Venken
Mr. Rao,

To insert the dropped item between the 2 target items where you want to drop means that we have to sort the items again ?

1
2
3
4
5
6

drop 6 on 3

1
2
3
6
4
5

How to do that ?

Re: xbrowse drag / drop (to Mr. Rao)

Posted: Tue Nov 05, 2024 5:21 pm
by nageswaragunupudi
In case of arrays, if we sort again the very purpose of dropping is lost. Right?

Re: xbrowse drag / drop (to Mr. Rao)

Posted: Tue Nov 05, 2024 7:12 pm
by Marc Venken
I would think (because of less experience) that

If I take 4 and drop on 2, the position in 4 is empty so resort would put the empty cell on top and make a new array starting from pos 2.

But I know that you have a much more elegant solution

Re: xbrowse drag / drop (to Mr. Rao)

Posted: Tue Nov 05, 2024 8:16 pm
by Marc Venken
I found a sample from Maurizio :
https://forums.fivetechsupport.com/view ... =3&t=39249

Code: Select all | Expand

#include "fivewin.ch"
#include "xbrowse.ch"

function main()

   local oDlg, oBrw, oFont, oCur, aData[ 12 ]

   AEval( aData, { |u,i| aData[ i ] := NtoCMonth( i ) } )

   DEFINE CURSOR oCur DRAG
   DEFINE FONT oFont NAME "VERDANA" SIZE 0,-16
   DEFINE DIALOG oDlg SIZE 250,500 PIXEL FONT oFont
   @ 10,10 XBROWSE oBrw SIZE -10,-10 PIXEL OF oDlg ;
      DATASOURCE aData COLUMNS 1 CELL LINES NOBORDER

   WITH OBJECT oBrw
      :nStretchCol      := 1
      :oDragCursor      := oCur
      :bDragBegin       := { |r,c,f,o| SetDropInfo( { oBrw:nArrayAt, oBrw:aRow } ) }
      :bDropOver        := { |u,r,c,f| oBrw:LButtonDown( r,c ), ;
                                       oBrw:LButtonUp( r,c ), ;
                                       aDel( aData, u[ 1 ] ), ;
                                       AIns( aData, oBrw:nArrayAt, u[ 2 ] ), ;
                                       oBrw:Refresh() }
      //
      :CreateFromCode()
   END

   ACTIVATE DIALOG oDlg CENTERED
   RELEASE FONT oFont
   RELEASE CURSOR oCur

return nil

 

Re: xbrowse drag / drop (to Mr. Rao)

Posted: Wed Nov 06, 2024 3:32 am
by nageswaragunupudi
This also works
Btw

Code: Select all | Expand

oBrw:SetPos( r, c, .t. )
is a short-cut for

Code: Select all | Expand

oBrw:LButtonDown( r,c )
oBrw:LButtonUp( r,c )
This short-cut is more optimal.

Also swapping arrays (actually pointers) is more optimal than ADel() and AIns()

Re: xbrowse drag / drop (to Mr. Rao)

Posted: Wed Nov 06, 2024 3:57 am
by ralph
Thanks! Mr Rao. It works perfect!.

Re: xbrowse drag / drop (to Mr. Rao)

Posted: Wed Nov 06, 2024 9:10 am
by Marc Venken
Swapping Array .....

What I found looks like this :

@ 20,10 BTNBMP PROMPT "ToExcel" SIZE 100,30 FLAT PIXEL OF oDlg ;
ACTION ( aCols := {}, ;
AEval( oBrw:aArrayData, { |a| If( a[ 1 ], AAdd( aCols, oBrowse:aCols[ a[ 2 ] ] ), nil ) } ), ;
oBrowse:ToExcel( nil, nil, aCols ) ;
)
______
XBROWSER aData TITLE "Array : Ctrl-Up/Dn for Swap Up/Dn"

AEval( aData, { |a,i| a[ 4 ] := i } )
_______

Re: xbrowse drag / drop (to Mr. Rao)

Posted: Thu Nov 07, 2024 4:25 am
by nageswaragunupudi
The response to the main post of Mr.Ralph is that Drag and Drop feature is fully supported by FWin and no further changes are required.
This drag and drop support is not only inside xbrowse but also in between different controls/dialogs/windows.