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

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