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