/*  *//* "Header Information" */
/*--------------------------------------------------------------------*/
/*                                                                    */
/* $VER: GfxCentreH 0.3 (23 May 1994)                                 */ 
/*                                                                    */
/* AUTHOR                                                             */
/*    P. Billing                                                      */
/*                                                                    */
/* NAME                                                               */
/*    GfxCentreH                                                      */
/*                                                                    */
/*                                                                    */
/* FUNCTION                                                           */
/*    Arexx-Script for Final Writer                                   */
/*                                                                    */
/*                                                                    */
/* SYNOPSIS                                                           */
/*                                                                    */
/*                                                                    */
/* INPUTS                                                             */
/*                                                                    */
/*                                                                    */
/*                                                                    */
/* $HISTORY:                                                          */
/*                                                                    */
/* 23 May 1994 : 0.3 :   Will now centre on page                      */
/* 08 May 1994 : 0.2 :   Made it DO Horizontal and Vertical           */
/* 08 May 1994 : 0.1 :   Code appears TO work                         */
/*                                                                    */
/*--------------------------------------------------------------------*/
/*  */

/*  *//* "General Options" */
Options Results

I = 0
X = 0


/*--------------------------------------------------------------------*/
/*              All measurement will be in micropoints.               */
/*--------------------------------------------------------------------*/

SetMeasure Ruler


/*--------------------------------------------------------------------*/
/*              Get the id of the first selected object.              */
/*            IF there are no objects selected THEN quit.             */
/*--------------------------------------------------------------------*/

FirstObject SELECTED
IF ( Result = 0 ) THEN
EXIT
/*  */

/*  *//* "Requesters" */
/*--------------------------------------------------------------------*/
/*            Collect the ids of all the selected objects             */
/*--------------------------------------------------------------------*/
I = 0
DO WHILE Result ~= 0 
  I = I + 1
  Objects.I = Result
  NextObject Objects.I SELECTED
END
GetPageSetup Width Height Top Bottom Left Right
Page_Coords = result
PARSE VAR Page_Coords p_wi p_he p_to p_bo p_le p_ri
GetSectionSetup Top Bottom Inside Outside
Section_Coords = result
PARSE VAR Section_Coords  s_to s_bo s_le s_ri
H_less = p_le+s_le+p_ri+s_ri
H_plus = p_le+s_le
V_less = p_to+s_to+p_bo+s_bo
V_plus = p_to+s_to

Page_Vertical_Centre = ((p_wi-H_less)/2)+H_plus
Page_Horizontal_Centre = ((p_he-V_less)/2)+V_plus

ShowMessage 1 1 '"Align Horizontally. Left - Right"
"Align Vertically.   Up   - Down" ""
"Horiz." "Vert." ""'

Align = result

ShowMessage 1 1 '"Centre Items" "Centre on Page" ""
"Items" "Page" ""'

Centre = result

/*  */

/*  *//* "Centre Horizontal" */
IF Align = 1 THEN DO
/*--------------------------------------------------------------------*/
/*              Loop through all of our objects finding               */
/*                 which one is closest to the right.                 */
/*--------------------------------------------------------------------*/
  
  X = 0
  R = 0
  L = 3000
  
  DO WHILE X < I
    X = X + 1
    GetObjectType Objects.X
    ObjType = Result
    GetObjectCoords Objects.X
    Coords = Result
    PARSE VAR Coords Page x1 y1 x2 y2
    
    IF (ObjType = 2) | (ObjType = 3) THEN  DO
      IF x1 > x2 THEN DO
        IF x1 > R THEN R = x1
      END
      ELSE
        IF x2 > R THEN R = x2
    END
    ELSE DO
      IF x1 + x2 > R THEN R = x1 + x2
    END
    
/*--------------------------------------------------------------------*/
/*               Now find which is closest TO the left                */
/*--------------------------------------------------------------------*/
    
    IF (ObjType = 2) | (ObjType = 3) THEN  DO
      IF x1 > x2 THEN DO
        IF x2 < L THEN L = x2
      END
      ELSE
        IF x1 < L THEN L = x1
    END
    ELSE DO
      IF x1 < L THEN L = x1
    END
  END
  
/*--------------------------------------------------------------------*/
/*                    Find the centre of the items                    */
/*--------------------------------------------------------------------*/
  IF Centre = 2 THEN 
    C = Page_Vertical_Centre
  ELSE
    C = (R-L)/2+L
  
/*--------------------------------------------------------------------*/
/*               Loop through all of our object moving                */
/*                       each one TO the Centre                       */
/*--------------------------------------------------------------------*/
  
  
  X = 0
  DO WHILE X < I
    X = X + 1
    GetObjectType Objects.X
    ObjType = Result
    GetObjectCoords Objects.X
    Coords = Result
    PARSE VAR Coords Page x1 y1 x2 y2
    IF (ObjType = 2) | (ObjType = 3) THEN  DO       /* Line or arrow */
      IF x1 = x2 THEN DO                            /* Vertical Line */
        x1 = C
        x2 = C
      END
      IF x1 > x2 THEN DO
        temp = (x1-x2)/2
        x1 = C+temp
        x2 = C-temp
      END
      ELSE DO
        temp = (x2-x1)/2
        x1 = C-temp
        x2 = C+temp
      END
    END
    ELSE DO
      x1 = C - x2/2
    END
    
    SetObjectCoords Objects.X Page x1 y1 x2 y2
  END
END
/*  */

/*  *//* "Centre Vertical" */
ELSE DO
/*--------------------------------------------------------------------*/
/*              Loop through all of our objects finding               */
/*                which one is closest to the Bottom.                 */
/*--------------------------------------------------------------------*/
  
  X = 0
  Bottom = 0
  Top    = 3000
  
  DO WHILE X < I
    X = X + 1
    GetObjectType Objects.X
    ObjType = Result
    GetObjectCoords Objects.X
    Coords = Result
    PARSE VAR Coords Page x1 y1 x2 y2
    IF (ObjType = 2) | (ObjType = 3) THEN  DO
      IF y1 > y2 THEN DO
        IF y1 > Bottom THEN Bottom = y1
      END
      ELSE DO
        IF y2 > Bottom THEN Bottom = y2
      END
    END
    ELSE DO
      IF y1 + y2 > Bottom THEN Bottom = y1 + y2
    END
    
    IF y1 < Top THEN Top = y1
    IF (ObjType = 2) | (ObjType = 3) THEN  DO
      IF y2 < Top THEN Top = y2
    END
  END
  
/*--------------------------------------------------------------------*/
/*                      Find the Vertical Centre                      */
/*--------------------------------------------------------------------*/
  IF Centre = 2 THEN
    C = Page_Horizontal_Centre
  ELSE
    C = (Bottom-Top)/2 + Top
  
/*--------------------------------------------------------------------*/
/*               Loop through all of our object moving                */
/*                      each one TO the Centre.                       */
/*--------------------------------------------------------------------*/
  
  X = 0
  DO WHILE X < I; X = X + 1
    GetObjectType Objects.X
    ObjType = Result
    GetObjectCoords Objects.X
    Coords = Result
    PARSE VAR Coords Page x1 y1 x2 y2
    
    IF (ObjType = 2) | (ObjType = 3) THEN  DO     /* Line or arrow   */
      IF y1 = y2 THEN DO                          /* Horizontal Line */
        y1 = C
        y2 = C
      END
      IF y1 > y2 THEN DO
        temp = (y1-y2)/2
        y2 = C-temp
        y1 = C+temp
      END
      ELSE DO
        temp = (y2-y1)/2
        y1 = C-temp
        y2 = C+temp
      END
    END
    ELSE DO
      y1 = C - y2/2
    END
    SetObjectCoords Objects.X Page x1 y1 x2 y2
  END
END
/*  */

/*  *//* "EXIT" */
/*--------------------------------------------------------------------*/
/*                 Reselect all of our objects and                    */
/*                 redraw to show our changes.                        */
/*--------------------------------------------------------------------*/

X = 0
DO WHILE X < I
  X = X + 1
  SelectObject Objects.X MULTIPLE
END

Redraw
SelectObject /* de SELECT objects */
EXIT
/*  */

print:
    type (arg(1))
    newparagraph
RETURN d

