/* ============================================================================= */
/* Final Data ARexx Macro.                                                       */
/* FDListMerge - Perform a list merge to a FinalWriter or FinalCopy_II document. */
/* $VER: FDListMerge 1.2 (31.01.99)                                               */
/* © 1995 SoftWood, Inc.                                                         */
/* Use of this macro is strictly at the user's risk.                             */
/*                                                                               */
/* This ARexx macro is to be run from FinalData. It merges a list from a Final   */
/* Data database to a FinalWriter or FinalCopy_II document. To run the macro you */
/* need to have your FC or FW document and your database file both open.         */
/*                                                                               */
/* Before running the macro, place the insertion point in the FinalCopy_II or    */
/* FinalWriter document where you want the list to begin. (Note that this macro  */
/* will use the first FinalWriter or FinalCopy_II port that it can find, so it   */
/* is wise to have only one document window open when you run this macro.        */
/* Otherwise, the wrong document may be used).                                   */
/*                                                                               */
/* Next determine which rows and columns of the database will be put into the    */
/* list. If nothing is selected or a cell is selected, then all rows and columns */
/* will appear in the list. If rows are selected then only those selected rows   */
/* with all the columns will appear. If columns are selected, then all rows but  */
/* only the selected columns will appear in the list. You are now ready to run   */
/* the macro.                                                                    */
/*                                                                               */
/* The macro will ask if you want it to try to set up tab stops. The stops will  */
/* be determined by the column justification and column width. If you choose no, */
/* the macro will use any tabs that might be on the current line. You will also  */
/* be asked if you want to include the column names in the list. If you choose   */
/* yes, then the column names will appear in the first row.                      */
/*                                                                               */
/* Changes:                                                                      */
/*  V1.1   Initial version for Final Data.                                       */
/*  V1.2   Modified for Final Data R2 to support non-contiguous row and column   */
/*         selection.                                                            */
/* ============================================================================= */
OPTIONS RESULTS

/* ---- Load the rexxsupport library ---- */
IF ( ADDLIB("rexxsupport.library", 0, -30, 0) = FALSE) THEN EXIT 20

/* ---- Get the address of the port we were called from. ---- */
FDPort = ADDRESS()

/* ---- Find the address of the word processing port. ------------------- */
/* ---- This will use the first port it can find with the base name. ---- */
/* ---- First try to get a FinalWriter port. If we can't find one, ------ */
/* ---- then try to get a FinalCopy port. ------------------------------- */
WPPortBase = "FINALW."
found = 0
DO p = 1 TO 50
   IF ( SHOWLIST('P', WPPortBase || p) ) THEN DO
      WPPort = WPPortBase || p
      found = 1
      LEAVE
   END
END

/* ---- Try FinalCopy port ---- */
IF ( ~ found ) THEN DO
   WPPortBase = "FINALC."
   found = 0
   DO p = 1 TO 50
      IF ( SHOWLIST('P', WPPortBase || p) ) THEN DO
         WPPort = WPPortBase || p
         found = 1
         LEAVE
      END
   END
END

IF ( ~ found ) THEN DO
   ShowMessage 1 1 '"Can not find word processing port!" "" "" "OK" "" ""'
   EXIT 10
END

DROP p
DROP found

/* ---- Determine the number of columns and rows in the database. ---- */
NumRows
nrows = RESULT
NumColumns
ncols = RESULT
IF ( nrows = 0 ) THEN DO
   ShowMessage 1 1 '"No rows of data in database!" "" "" "OK" "" ""'
   EXIT 10
END

IF ( ncols = 0 ) THEN DO
   ShowMessage 1 1 '"No columns defined in database!" "" "" "OK" "" ""'
   EXIT 10
END

/* ---- Determine which rows and columns will get transferred. ----------- */
/* ---- If selection is OFF or CELL then use all the columns and rows ---- */
firstRow = 1
firstCol = 1
lastRow = nrows
lastCol = ncols

DO c = firstCol TO lastCol
   useCol.c = 1;
END

DO r = firstRow TO lastRow
   useRow.r = 1;
END

SelectionInfo
PARSE VAR RESULT selType selFrom selTo
SELECT
   WHEN ( selType = "COLUMNS" ) THEN DO
      firstCol = selFrom
      lastCol = selTo

		DO c = firstCol TO lastCol
			IsColumnSelected c
			useCol.c = RESULT
		END
   END

   WHEN ( selType = "ROWS" ) THEN DO
      firstRow = selFrom
      lastRow = selTo

		DO r = firstRow TO lastRow
			IsRowSelected r
			useRow.r = RESULT
		END
   END

   OTHERWISE DO   /* OFF or CELL */
   END
END /* end Select */



/* ---- Ask if tabs should be created. If so then, create tab stops. ---- */
tabB4FirstCol = 0
ShowMessage 1 0 '"Attempt to set up tab stops?" "" "" "Yes" "No" ""'
IF ( RESULT = 1 ) THEN DO
   tabPos = 0
   DO c = firstCol TO lastCol
		IF ( useCol.c ) THEN DO
     	   ADDRESS VALUE FDPort
         GetColumnID POSITION c
         GetColumnDef RESULT ALIGNMENT WIDTH
         PARSE VAR RESULT calign cwidth

         SELECT
            WHEN ( calign = "Left" ) THEN DO
               inc = cwidth * 9
               END
            WHEN ( calign = "Right" ) THEN DO
               tabpos = tabpos + (cwidth * 9)
               inc = 0
               END
            WHEN ( calign = "Center") THEN DO
               tabpos = tabpos + ((cwidth * 9) % 2)
               inc = ((cwidth * 9) % 2)
               END
            OTHERWISE DO
               END
         END   /* end select */

         IF ( c = firstCol & calign ~= "Left" ) THEN
            tabB4FirstCol = 1

         IF ( c ~= firstCol | tabB4FirstCol ) THEN DO
            ADDRESS VALUE WPPort
            SetMeasure Micropoints
            SetTab tabpos calign
         END

         tabpos = tabpos + inc + 45
      END /* end if useCol */

   END   /* end column loop */
END

/* ---- Ask if column names are wanted. If they are, then put them in. ---- */
ADDRESS VALUE FDPort
ShowMessage 1 0 '"Include column names?" "" "" "Yes" "No" ""'
IF ( RESULT = 1 ) THEN DO
   ADDRESS VALUE FDPort
   data = ""
   DO c = firstCol TO lastCol
		IF	( useCol.c ) THEN DO
         IF ( c = firstCol & tabB4FirstCol ) THEN
            data = data || D2C(9)

         GetColumnName POSITION c
         data = data || RESULT
         IF ( c ~= lastCol ) THEN
            data = data || D2C(9)
      END /* end if usecol */
   END   /* end column */

   ADDRESS VALUE WPPort
   Type data
   NewParagraph
END

/* ---- For each row of the database, collect the data in each column. ---- */
/* ---- Then type the combined data for the row to the word processor. ---- */
ADDRESS VALUE FDPort
DO r = firstRow TO lastRow
	IF ( useRow.r ) THEN DO
      data = ""

      ADDRESS VALUE FDPort
      DO c = firstCol TO lastCol
		   IF ( useCol.c ) THEN DO
            IF ( c = firstCol & tabB4FirstCol ) THEN
               data = data || D2C(9)

            CellData c r
            data = data || RESULT
            IF ( c ~= lastCol ) THEN
               data = data || D2C(9)
         END /* end if usecol */
      END   /* end column */

      ADDRESS VALUE WPPort
      Type data
      IF ( r ~= lastRow ) THEN
         NewParagraph
   END /* end userow */
END   /* end row */

ADDRESS VALUE FDPort

/* END OF MACRO FILE */