
! script name: datatable.d/moverows
! topic: move rows in a table

!A
!A NAME:
!A
!A 	Move Rows
!A
!A DESCRIPTION:
!A
!A	Move a row in a table from one location to another.
!A
!B INSTRUCTIONS:
!B
!B 	PICK ORIGIN ROW: Pick the source row.
!B
!B 	PICK ROW TO BE INSERTED BEFORE: Pick the reference row.
!B
!B NOTES:
!B
!B 	The origin table and the destination table may be the same.
!B 	In this case the copied data must not overlap.
!B
!B 	When tables are used in variational design the variational
!B 	attachments are not copied.
!B
!B
!# see also - htdocs/help.d/datatables.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: move row ")

DIM Answer$[256]
DIM A$[256]
DIM Destination$[256]
DEG

ActivateUndo
Start:!
Table1=PickEntity("PICK ORIGIN ROW")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(Table1) THEN Start
T1= LastPickPara
R1=TableParaRow(Table1,T1)

Table2:Stat= SetInputFormat("a1",",E,")
Table2=PickEntity("PICK ROW TO BE INSERTED BEFORE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Use_end= 0
IF Answer$= "E" THEN
	Table2=PickEntity("PICK TABLE TO PLACE ROW")
	IF (ExitScript(Destination$)) THEN Leave
	IF NOT ValidGeometry(Table2) THEN Start
	Use_end= 1
END IF
IF NOT ValidGeometry(Table2) THEN Start
T2= LastPickPara

R2=TableParaRow(Table2,T2)
IF (R1=R2 OR R1=R2-1) AND AreEntitiesSame(Table1,Table2) THEN Start

Pick_loop:!
Stat= PlotDataTable(Table1,R1,1,Highlight_pen)

Nc= NumberTableCols(Table2)

ActivateUndo
Stat= AdvanceUndoNumber("move rows")
Stat= AddItemToUndo(WsUBModifyMode,Table1)
Stat= AddItemToUndo(WsUBModifyMode,Table2)

Nrows= 1
Stat= EraseEntity(Table1)
Stat= EraseEntity(Table2)
Stat= InsertTableRows(Table2,R2,Nrows)
IF R1>R2 AND AreEntitiesSame(Table1,Table2) THEN R1= R1+1
Stat= CopyTableRows(Table1,Table2,R1,1,R1,Nc,R2,1)
Stat= DeleteTableRow(Table1,R1)
Stat= PlotEntity(Table1)
Stat= PlotEntity(Table2)
Stat= PlotDataTable(Table2,R1,1,R1,Nc,0)

GOTO Start

Leave:!
stat= SetInputFormat("n4","")
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
