home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / FaceLift / FaceLift Folder / FLMapOps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-16  |  5.6 KB  |  303 lines  |  [TEXT/KAHL]

  1. # include    "ListEdit.h"
  2. # include    "FLMaca.h"
  3. # include    "FLMapInfo.h"
  4. # include    "FaceLift.h"
  5.  
  6.  
  7. /*
  8.  * Check two MapSpecs for equality.
  9.  */
  10.  
  11. Boolean
  12. EqualMSpec (MapSpec *m1, MapSpec *m2)
  13. {
  14.     return (CompareCSpec (&m1->inFmt, &m2->inFmt) == 0
  15.         && CompareCSpec (&m1->outFmt, &m2->outFmt) == 0);
  16. }
  17.  
  18.  
  19. /*
  20.  * Check whether a MapSpec already exists in the map
  21.  */
  22.  
  23. Boolean
  24. StatMSpec (MapSpec *mSpec)
  25. {
  26. int    i;
  27.  
  28.     for (i = 0; i < mapList->nLines; ++i)
  29.     {
  30.         if (EqualMSpec (mSpec, &mapSpec[i]))
  31.             return (true);
  32.     }
  33.     return (false);
  34. }
  35.  
  36.  
  37.  
  38. /*
  39.  * Insert the text of all the fields for the given map list line.
  40.  *
  41.  * This routines relies implicitly on the ordering of fields
  42.  * within a map line.
  43.  */
  44.  
  45. void
  46. SetMapFields (LineHandle hLine, MapStr *mStr)
  47. {
  48. FieldHandle    hField;
  49.  
  50.     hField = hLine;
  51.     SetFieldStr (hField, mStr->inStr.markStr);
  52.     hField = NextField (hField);
  53.     SetFieldStr (hField, mStr->inStr.fontStr);
  54.     hField = NextField (hField);
  55.     SetFieldStr (hField, mStr->inStr.sizeStr);
  56.     hField = NextField (hField);
  57.     SetFieldStr (hField, mStr->inStr.styleStr);
  58.     hField = NextField (hField);
  59.     SetFieldStr (hField, mStr->outStr.markStr);
  60.     hField = NextField (hField);
  61.     SetFieldStr (hField, mStr->outStr.fontStr);
  62.     hField = NextField (hField);
  63.     SetFieldStr (hField, mStr->outStr.sizeStr);
  64.     hField = NextField (hField);
  65.     SetFieldStr (hField, mStr->outStr.styleStr);
  66. }
  67.  
  68.  
  69. /*
  70.  * Select a mapping
  71.  */
  72.  
  73. void
  74. SelectMapping (int lineNo)
  75. {
  76.     SelectLine (mapList, lineNo);    /* sets mapList->curLine */
  77.     if (lineNo != noLine)
  78.         ScrollToLine (mapList, lineNo);
  79.     SetSelectors (lineNo);
  80. }
  81.  
  82.  
  83. /*
  84.  * Insert a new mapping, making it mapping n.  This takes care
  85.  * of both the array of mappings, and the list of text representations
  86.  * of the mappings.  It also sets up lots of pointers to various
  87.  * parts of the current mapping.
  88.  *
  89.  * Note that there's no explicit count of mappings.  Use the count
  90.  * in the LineList corresponding to the mapping array is used.
  91.  */
  92.  
  93. Boolean
  94. InsertMapping (MapSpec *mSpec, int n)
  95. {
  96. MapStr                mStr;
  97. LineHandle    hLine;
  98. int        i, nLines;
  99. Str255                s;
  100.  
  101.     if (mapList->nLines < mapList->maxLines)
  102.     {
  103.  
  104.         /*
  105.          * Convert mapping specification into string form and put the strings
  106.          * into a new line to be inserted into the line list.
  107.          *
  108.          * Then make room for the new mapping in spec array and insert it.
  109.          */
  110.  
  111.         nLines = mapList->nLines;
  112.         MSpecToMStr (mSpec, &mStr);            /* convert specs to strings */
  113.         hLine = NewLine (mapList->nFields);
  114.         SetMapFields (hLine, &mStr);
  115.         if (InsertLine (mapList, hLine, n) == false)
  116.         {
  117.             /*DisposeLine (hLine);    /* THIS IS WRONG (won't have been attached!) */
  118.         }
  119.         else
  120.         {
  121.             for (i = nLines; i > n; --i)
  122.             {
  123.                 mapSpec[i] = mapSpec[i-1];    /* move specs to make room */
  124.             }
  125.             mapSpec[n] = *mSpec;            /* add new one */
  126.         
  127.             SelectMapping (n);
  128.             return (true);
  129.         }
  130.     }
  131.     NumToString ((long) mapList->maxLines, s);
  132.     Message3 ("\pMap is full (", s, "\p lines).  Can't add anything to it.");
  133.     return (false);
  134. }
  135.  
  136.  
  137. /*
  138.  * Add a new, blank, mapping to the end of the list.
  139.  */
  140.  
  141. void
  142. NewMapping (void)
  143. {
  144. MapSpec    mSpec;
  145.  
  146.     ClearMSpec (&mSpec);
  147.     (void) InsertMapping (&mSpec, mapList->nLines);
  148. }
  149.  
  150.  
  151. /*
  152.  * Duplicate the currently selected mapping and add right after the
  153.  * selection.
  154.  */
  155.  
  156. void
  157. DupMapping (int n)
  158. {
  159.     (void) InsertMapping (&mapSpec[n], n+1);
  160. }
  161.  
  162.  
  163. /*
  164.  * Replace the currently selected mapping with the given one.
  165.  * (It's assumed that there *is* one selected.)
  166.  */
  167.  
  168. void
  169. PasteMapping (MapSpec *mSpec, int n)
  170. {
  171. MapStr            mStr;
  172. int    curLine;
  173.  
  174.     curLine = mapList->curLine;
  175.     mapSpec[curLine] = *mSpec;
  176.     MSpecToMStr (mSpec, &mStr);
  177.     SetMapFields (ListLine (mapList, curLine), &mStr);
  178.     DrawLine (mapList, curLine);
  179.     SetSelectors (curLine);                /* set selection controls */
  180. }
  181.  
  182.  
  183. /*
  184.  * Clobber the n-th mapping.
  185.  */
  186.  
  187. void
  188. DeleteMapping (int n)
  189. {
  190. int    i;
  191.  
  192.     for (i = n; i < mapList->nLines; ++i)
  193.     {
  194.         mapSpec[i] = mapSpec[i+1];                    /* copy specs */
  195.     }
  196.     DeleteLine (mapList, n);
  197.     SelectMapping (noLine);
  198.     ScrollToLine (mapList, n);
  199. }
  200.  
  201.  
  202. /*
  203.  * Clobber entire map.
  204.  */
  205.  
  206. void
  207. ClobberMap (void)
  208. {
  209.     SelectMapping (noLine);
  210.     ResetList (mapList);
  211. }
  212.  
  213.  
  214.  
  215. /*
  216.  * Sort the map specifications on the input format values.  Make
  217.  * sure to keep track of the current spec and redraw the text 
  218.  * and controls properly when done.
  219.  */
  220.  
  221. void
  222. SortMap (void)
  223. {
  224. int        i, j, curLine;
  225. MapSpec    tmpSpec;
  226. LineHandle    tmpLine;
  227.  
  228.     curLine = mapList->curLine;
  229.     for (i = 0; i < mapList->nLines - 1; ++i)
  230.     {
  231.         for (j = i; j < mapList->nLines; ++j)
  232.         {
  233.             if (CompareCSpec (&mapSpec[i].inFmt, &mapSpec[j].inFmt) > 0)
  234.             {
  235.                 tmpSpec = mapSpec[i];
  236.                 mapSpec[i] = mapSpec[j];
  237.                 mapSpec[j] = tmpSpec;
  238.                 tmpLine = ListLine (mapList, i);
  239.                 ListLine (mapList, i) = ListLine (mapList, j);
  240.                 ListLine (mapList, j) = tmpLine;
  241.                 if (curLine == i)
  242.                     curLine = j;
  243.                 else if (curLine == j)
  244.                     curLine = i;
  245.             }
  246.         }
  247.     }
  248.     DrawListText (mapList);
  249.     SelectMapping (curLine);
  250. }
  251.  
  252.  
  253. /*
  254.  * Eliminate duplicates in the map.  The loops must explicitly
  255.  * test mapList->nLines, not a variable set equal to that before
  256.  * the loops, since DeleteMapping causes mapList->nLines to change.
  257.  */
  258.  
  259. void
  260. SquishMap (void)
  261. {
  262. int    i, j;
  263.  
  264.     for (i = 0; i < mapList->nLines; ++i)
  265.     {
  266.         j = i + 1;
  267.         while (j < mapList->nLines)
  268.         {
  269.             if (EqualMSpec (&mapSpec[i], &mapSpec[j]))
  270.                 DeleteMapping (j);
  271.             else
  272.                 ++j;
  273.         }
  274.     }
  275. }
  276.  
  277.  
  278. /*
  279.  * Reverse input and output sides of map.
  280.  */
  281.  
  282. void
  283. ReverseMap (void)
  284. {
  285. short        i;
  286. ConvSpec    cSpec;
  287. MapSpec        *mSpec;
  288. MapStr        mStr;
  289.  
  290.     for (i = 0; i < mapList->nLines; ++i)
  291.     {
  292.         mSpec = &mapSpec[i];
  293.         cSpec = mSpec->inFmt;
  294.         mSpec->inFmt = mSpec->outFmt;
  295.         mSpec->outFmt = cSpec;
  296.         mSpec->isInput = !mSpec->isInput;        
  297.         MSpecToMStr (mSpec, &mStr);    /* convert specs to strings */
  298.         SetMapFields (ListLine (mapList, i), &mStr);
  299.     }
  300.     DrawListText (mapList);
  301.     SetSelectors (mapList->curLine);
  302. }
  303.