home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / dev / oberon-a-1.4ß.lha / Oberon-A / source / amigautil / IntuiUtil.mod < prev    next >
Text File  |  1994-08-08  |  17KB  |  658 lines

  1. (***************************************************************************
  2.  
  3.      $RCSfile: IntuiUtil.mod $
  4.   Description: Support for clients of intuition.library
  5.  
  6.    Created by: fjc (Frank Copeland)
  7.     $Revision: 3.2 $
  8.       $Author: fjc $
  9.         $Date: 1994/08/08 16:09:54 $
  10.  
  11.   Copyright © 1994, Frank Copeland.
  12.   This file is part of the Oberon-A Library.
  13.   See Oberon-A.doc for conditions of use and distribution.
  14.  
  15. ***************************************************************************)
  16.  
  17. MODULE IntuiUtil;
  18.  
  19. (*
  20. ** $C- CaseChk       $I- IndexChk  $L+ LongAdr   $N- NilChk
  21. ** $P- PortableCode  $R- RangeChk  $S- StackChk  $T- TypeChk
  22. ** $V- OvflChk       $Z- ZeroVars
  23. *)
  24.  
  25. IMPORT
  26.   E := Exec, G := Graphics, L := Layers, I := Intuition,
  27.   U := Util, SYS := SYSTEM;
  28.  
  29.  
  30. (* Passed as a parameter to GetMenuChoice () *)
  31.  
  32. TYPE
  33.  
  34.   Choice * = RECORD
  35.     menuChosen *    : INTEGER;
  36.     itemChosen *    : INTEGER;
  37.     subItemChosen * : INTEGER;
  38.     pointer *       : I.MenuItemPtr;
  39.   END; (* ChoiceType *)
  40.  
  41. CONST
  42.  
  43.   halfPot  = I.maxPot DIV 2;
  44.   halfBody = I.maxBody DIV 2;
  45.  
  46. VAR
  47.   autoIntuiText : I.IntuiText;
  48.  
  49.  
  50. (* ===== Preferences ===== *)
  51.  
  52.  
  53. (*------------------------------------*)
  54. PROCEDURE PrefsFontHeight * () : SHORTINT;
  55. (*
  56.   Returns the height of the default system font.
  57. *)
  58.  
  59. VAR
  60.   prefsBuffer : I.Preferences;
  61.  
  62. BEGIN
  63.   SYS.PUTREG (0, I.base.GetPrefs (prefsBuffer, SIZE(I.Preferences)));
  64.   RETURN prefsBuffer.fontHeight;
  65. END PrefsFontHeight;
  66.  
  67.  
  68. (* ===== Gadget ===== *)
  69.  
  70.  
  71. (*------------------------------------*)
  72. PROCEDURE CentreGadget *
  73.   ( VAR gadget : I.Gadget; left, top, width, height : INTEGER );
  74. (*
  75.   Adjusts the gadget's position to centre it within a rectangle defined by
  76.   (left, top, width, height).
  77. *)
  78.  
  79. BEGIN (* CentreGadget *)
  80.   gadget.leftEdge := ( ( width - gadget.width ) DIV 2 ) + left;
  81.   gadget.topEdge := ( ( height - gadget.height ) DIV 2 ) + top;
  82. END CentreGadget;
  83.  
  84.  
  85. (*------------------------------------*)
  86. PROCEDURE ConvertPot *
  87.   ( potValue, totalUnits, visibleUnits : INTEGER )
  88.   : INTEGER;
  89.  
  90. VAR
  91.   value, hidden : LONGINT;
  92.  
  93. BEGIN (* ConvertPot *)
  94.   IF (potValue = 0) THEN
  95.     RETURN 0
  96.   ELSE
  97.     IF (visibleUnits >= totalUnits) THEN
  98.       RETURN 0
  99.     ELSE
  100.       IF potValue < 0 THEN value := potValue + 010000H
  101.       ELSE value := potValue
  102.       END;
  103.       hidden := totalUnits - visibleUnits;
  104.       RETURN SHORT ((hidden * value + halfPot) DIV I.maxPot)
  105.     END; (* ELSE *)
  106.   END; (* ELSE *)
  107. END ConvertPot;
  108.  
  109.  
  110. (*------------------------------------*)
  111. PROCEDURE ConvertToPot *
  112.   ( units, totalUnits, visibleUnits : INTEGER )
  113.   : INTEGER;
  114.  
  115. VAR
  116.   hidden, lUnits : LONGINT;
  117.  
  118. BEGIN (* ConvertToPot *)
  119.   IF units = 0 THEN
  120.     RETURN 0
  121.   ELSE
  122.     IF visibleUnits >= totalUnits THEN
  123.       RETURN 0
  124.     ELSE
  125.       IF units < 0 THEN lUnits := units + 010000H
  126.       ELSE lUnits := units
  127.       END;
  128.       hidden := totalUnits - visibleUnits;
  129.       IF lUnits >= hidden THEN
  130.         RETURN (*I.maxPot*) -1
  131.       ELSE
  132.         RETURN SHORT ((I.maxPot * lUnits) DIV hidden)
  133.       END; (* ELSE *)
  134.     END; (* ELSE *)
  135.   END; (* ELSE *)
  136. END ConvertToPot;
  137.  
  138.  
  139. (*------------------------------------*)
  140. PROCEDURE ConvertBody * (bodyValue, totalUnits : INTEGER) : INTEGER;
  141.  
  142.   VAR value : LONGINT;
  143.  
  144. BEGIN (* ConvertBody *)
  145.   IF bodyValue = 0 THEN
  146.     RETURN 0
  147.   ELSIF (bodyValue = I.maxBody) OR (totalUnits < 2) THEN
  148.     RETURN totalUnits
  149.   ELSE
  150.     IF bodyValue < 0 THEN value := bodyValue + 010000H
  151.     ELSE value := bodyValue
  152.     END;
  153.     RETURN SHORT ((totalUnits * value) DIV I.maxBody);
  154.   END
  155. END ConvertBody;
  156.  
  157.  
  158. (*------------------------------------*)
  159. PROCEDURE ConvertToBody * ( totalUnits, visibleUnits : INTEGER ) : INTEGER;
  160.  
  161. BEGIN (* ConvertToBody *)
  162.   IF visibleUnits = 0 THEN
  163.     RETURN 0
  164.   ELSIF visibleUnits >= totalUnits THEN
  165.     RETURN (*I.maxBody*) -1
  166.   ELSE
  167.     RETURN SHORT ((I.maxBody * visibleUnits) DIV totalUnits)
  168.   END; (* ELSE *)
  169. END ConvertToBody;
  170.  
  171.  
  172. (*------------------------------------*)
  173. (* $D- *)
  174. PROCEDURE SetString * (VAR gadget : I.Gadget; string : ARRAY OF CHAR);
  175.  
  176. VAR
  177.   stringInfo : I.StringInfoPtr;
  178.  
  179. BEGIN (* SetString *)
  180.   stringInfo := SYS.VAL (I.StringInfoPtr, gadget.specialInfo);
  181.   SYS.MOVE
  182.     ( SYS.ADR (string), stringInfo.buffer,
  183.       U.MaxInt
  184.         ( SHORT (SYS.STRLEN (string) + 1), stringInfo.maxChars - 1 ) );
  185.   stringInfo.buffer [stringInfo.maxChars] := 0X
  186. END SetString;
  187.  
  188.  
  189. (*------------------------------------*)
  190. PROCEDURE GetString *  (VAR gadget : I.Gadget; VAR string : ARRAY OF CHAR);
  191.  
  192. VAR
  193.   stringInfo : I.StringInfoPtr;
  194.  
  195. BEGIN (* SetString *)
  196.   stringInfo := SYS.VAL (I.StringInfoPtr, gadget.specialInfo);
  197.   COPY (stringInfo.buffer^, string)
  198. END GetString;
  199.  
  200.  
  201. (*------------------------------------*)
  202. PROCEDURE SetInteger * ( VAR gadget : I.Gadget; integer : LONGINT );
  203.  
  204.   VAR
  205.     stringInfo : I.StringInfoPtr;
  206.     buffer : ARRAY 12 OF CHAR;
  207.     index : INTEGER;
  208.     negative : BOOLEAN;
  209.  
  210.   (*------------------------------------*)
  211.   PROCEDURE Digits ( integer : LONGINT ) : INTEGER;
  212.  
  213.   VAR
  214.     digits : INTEGER;
  215.  
  216.   BEGIN (* Digits *)
  217.     digits := 0;
  218.     WHILE ( integer > 0 ) DO
  219.       INC( digits );
  220.       integer := integer DIV 10;
  221.     END; (* WHILE *)
  222.     RETURN digits;
  223.   END Digits;
  224.  
  225. BEGIN (* SetInteger *)
  226.   stringInfo := SYS.VAL (I.StringInfoPtr, gadget.specialInfo);
  227.   stringInfo.longInt := integer;
  228.   negative := (integer < 0); integer := ABS(integer);
  229.   index := Digits( integer );
  230.   IF negative THEN INC(index) END;
  231.   buffer [index] := 0X;
  232.   WHILE integer > 0 DO
  233.     DEC (index);
  234.     buffer[index] := CHR (integer MOD 10 + ORD ("0"));
  235.     integer := integer DIV 10;
  236.   END; (* WHILE *)
  237.   IF negative THEN buffer [0] := "-" END;
  238.   SetString (gadget, buffer);
  239. END SetInteger;
  240.  
  241.  
  242. (*------------------------------------*)
  243. PROCEDURE GetInteger * ( VAR gadget : I.Gadget ) : LONGINT;
  244.  
  245. VAR
  246.   stringInfo : I.StringInfoPtr;
  247.  
  248. BEGIN (* GetInteger *)
  249.   stringInfo := SYS.VAL (I.StringInfoPtr, gadget.specialInfo);
  250.   RETURN stringInfo^.longInt;
  251. END GetInteger;
  252.  
  253.  
  254. (* ===== IntuiText ===== *)
  255.  
  256.  
  257. (*------------------------------------*)
  258. PROCEDURE IntuiTextHeight * ( VAR intuiText : I.IntuiText ) : INTEGER;
  259. (*
  260.   Returns the height in scan lines of the text held in intuiText.
  261. *)
  262.  
  263. BEGIN (* IntuiTextHeight *)
  264.   IF intuiText.iTextFont = NIL THEN
  265.     RETURN PrefsFontHeight()
  266.   ELSE
  267.     RETURN intuiText.iTextFont.ySize
  268.   END; (* ELSE *)
  269. END IntuiTextHeight;
  270.  
  271.  
  272. (*------------------------------------*)
  273. PROCEDURE CentreIntuiText * (
  274.   VAR intuiText : I.IntuiText;
  275.   left, top, width, height : INTEGER );
  276. (*
  277.   Adjusts the text's position to centre it within a rectangle defined by
  278.   (left, top, width, height).
  279. *)
  280.  
  281. BEGIN (* CentreIntuiText *)
  282.   intuiText.leftEdge :=
  283.     ( ( width - SHORT (I.base.IntuiTextLength(intuiText)) ) DIV 2 ) + left;
  284.   intuiText.topEdge :=
  285.     ( ( height - IntuiTextHeight(intuiText) ) DIV 2 ) + top;
  286. END CentreIntuiText;
  287.  
  288.  
  289. (*------------------------------------*)
  290. (* $D- *)
  291. PROCEDURE CalcTextBox *
  292.   ( text              : ARRAY OF CHAR;
  293.     font              : G.TextAttrPtr;
  294.     VAR width, height : INTEGER );
  295. (*
  296.   Returns the minimum size of the rectangle that will enclose the given text
  297.   if rendered in the given font.
  298. *)
  299.  
  300. VAR
  301.   intuiText : I.IntuiText;
  302.  
  303. BEGIN (* CalcTextBox *)
  304.   intuiText.iText := SYS.ADR (text);
  305.   intuiText.iTextFont := font;
  306.   width := SHORT (I.base.IntuiTextLength (intuiText));
  307.   height := IntuiTextHeight (intuiText);
  308. END CalcTextBox;
  309.  
  310.  
  311. (* ===== Window ===== *)
  312.  
  313.  
  314. (*------------------------------------*)
  315. PROCEDURE ClipWindow *
  316.   ( window                 : I.WindowPtr;
  317.     minX, minY, maxX, maxY : INTEGER;
  318.     VAR oldRegion          : G.RegionPtr )
  319.   : BOOLEAN;
  320. (*
  321.   Sets up the window's clipping region to permit drawing only inside the
  322.   rectangle defined by (minX, minY, maxX, maxY). It returns FALSE if the
  323.   attempt fails and puts the existing clipping region in oldRegion.  It
  324.   should immediately be followed by drawing routines, then
  325.   UnclipWindow( window, oldRegion ).
  326. *)
  327.  
  328. VAR
  329.   newRegion : G.RegionPtr; myRectangle : G.Rectangle;
  330.  
  331. BEGIN (* ClipWindow *)
  332.   myRectangle.minX := minX;
  333.   myRectangle.minY := minY;
  334.   myRectangle.maxX := maxX;
  335.   myRectangle.maxY := maxY;
  336.   newRegion := G.base.NewRegion();
  337.   IF newRegion # NIL THEN
  338.     IF G.base.OrRectRegion (newRegion, m