home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / ui / strseq.cxx < prev    next >
C/C++ Source or Header  |  1995-04-09  |  5KB  |  252 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6. /*
  7.  *
  8.  *          Copyright (C) 1994, M. A. Sridhar
  9.  *  
  10.  *
  11.  *     This software is Copyright M. A. Sridhar, 1994. You are free
  12.  *     to copy, modify or distribute this software  as you see fit,
  13.  *     and to use  it  for  any  purpose, provided   this copyright
  14.  *     notice and the following   disclaimer are included  with all
  15.  *     copies.
  16.  *
  17.  *                        DISCLAIMER
  18.  *
  19.  *     The author makes no warranties, either expressed or implied,
  20.  *     with respect  to  this  software, its  quality, performance,
  21.  *     merchantability, or fitness for any particular purpose. This
  22.  *     software is distributed  AS IS.  The  user of this  software
  23.  *     assumes all risks  as to its quality  and performance. In no
  24.  *     event shall the author be liable for any direct, indirect or
  25.  *     consequential damages, even if the  author has been  advised
  26.  *     as to the possibility of such damages.
  27.  *
  28.  */
  29.  
  30.  
  31. #if defined(__GNUC__)
  32. #pragma implementation
  33. #endif
  34.  
  35.  
  36. #include "base/binding.h"
  37. #include "ui/strseq.h"
  38. #include "ui/strview.h"
  39.  
  40. #if defined(__GNUC__) && __GNUC_MINOR__ >= 6
  41. template class CL_Binding<UI_StringSequence>;
  42. #endif
  43.  
  44.  
  45. typedef CL_Binding<UI_StringSequence> SeqBind;
  46.  
  47. void UI_StringSequence::AddClient (UI_StringView* v)
  48. {
  49.     _clientSet.Add(v);
  50. }
  51.  
  52.  
  53.  
  54. void UI_StringSequence::RemoveClient (UI_StringView* v)
  55. {
  56.     _clientSet.Remove (v);
  57. }
  58.  
  59.  
  60.  
  61. bool UI_StringSequence::Update (CL_Object&, long index)
  62. {
  63.     _ChangeUpdate (index);
  64.     return TRUE;
  65. }
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. bool UI_StringSequence::Insert (const CL_String& s, long index)
  73. {
  74.     if (!CL_StringSequence::Insert (s, index))
  75.         return FALSE;
  76.     SeqBind b (this, &UI_StringSequence::Update);
  77.     (*this)[index+1].AddDependent (b, index+1);
  78.     _InsertionUpdate (index);
  79.     _ReassignCodes ();
  80.     return TRUE;
  81. }
  82.  
  83.  
  84. CL_String UI_StringSequence::Remove (long index)
  85. {
  86.     CL_String s = CL_StringSequence::Remove (index);
  87.     _RemovalUpdate (index);
  88.     _ReassignCodes ();
  89.     return s;
  90. }
  91.  
  92.  
  93. bool UI_StringSequence::ShiftLeftAt  (long pos, long amount)
  94. {
  95.     if (!CL_StringSequence::ShiftLeftAt (pos, amount))
  96.         return FALSE;
  97.     _ReassignCodes ();
  98.     return TRUE;
  99. }
  100.  
  101. bool UI_StringSequence::ShiftRightAt (long pos, long amount)
  102. {
  103.     if (!CL_StringSequence::ShiftRightAt (pos, amount))
  104.         return FALSE;
  105.     _ReassignCodes ();
  106.     return TRUE;
  107. }
  108.  
  109.  
  110.  
  111.  
  112. CL_Sequence<CL_String>& UI_StringSequence::operator=
  113.     (const CL_Sequence<CL_String>& s)
  114. {
  115.     CL_ObjectSetIterator itr (_clientSet);
  116.     while (itr.More())
  117.         ((UI_StringView*) itr.Next())->ModelEmptied ();
  118.     CL_Sequence<CL_String>::operator= (s);
  119.     long n = s.Size();
  120.     for (long i = 0; i < n; i++)
  121.         _InsertionUpdate (i-1);
  122.     _ReassignCodes();
  123.     return *this;
  124. }
  125.  
  126.  
  127. void UI_StringSequence::MakeEmpty ()
  128. {
  129.     CL_ObjectSetIterator itr (_clientSet);
  130.     while (itr.More())
  131.         ((UI_StringView*) itr.Next())->ModelEmptied ();
  132. }
  133.  
  134.  
  135. CL_Sequence<CL_String>& UI_StringSequence::operator+=
  136.     (const CL_Sequence<CL_String>& s)
  137. {
  138.     long n = Size();
  139.     CL_Sequence<CL_String>::operator+= (s);
  140.     long m = Size();
  141.     for (long i = n; i < m; i++) {
  142.         _InsertionUpdate (i-1);
  143.     }
  144.     _ReassignCodes();
  145.     return *this;
  146. }
  147.  
  148.  
  149. #ifndef __GNUC__ // Temporarily disable these methods to work around
  150.                  // the GCC bug
  151. void UI_StringSequence::operator-= (const CL_IntegerSet& s)
  152. {
  153.     CL_IntegerSetIterator itr (s);
  154.     long count = 0;
  155.     while (itr.More()) {
  156.         Remove (itr.Next() - count);
  157.         // ----------------^^^^^^^
  158.         // Needed to account for previously-deleted items
  159.         count++;
  160.     }
  161.     _ReassignCodes();
  162. }
  163.  
  164.  
  165. #endif    // __GNUC__
  166.  
  167.  
  168.  
  169. bool UI_StringSequence::ChangeSize (long new_size)
  170. {
  171.     long n = Size();
  172.     if (!CL_StringSequence::ChangeSize (new_size))
  173.         return FALSE;
  174.     if (new_size < n) {
  175.         for (long i = new_size; i < n; i++)
  176.             _RemovalUpdate (i);
  177.     }
  178.     else {
  179.         for (long i = n; i < new_size; i++)
  180.             _InsertionUpdate (i-1);
  181.     }
  182.     _ReassignCodes ();
  183.     return TRUE;
  184. }
  185.  
  186.  
  187. bool UI_StringSequence::Sort (long pos, long len)
  188. {
  189.     if (!CL_StringSequence::Sort(pos, len))
  190.         return FALSE;
  191.     CL_ObjectSetIterator itr (_clientSet);
  192.     while (itr.More())
  193.         ((UI_StringView*) itr.Next())->ModelEmptied ();
  194.     long n = Size();
  195.     for (long i = 0; i < n; i++) {
  196.         _InsertionUpdate (i-1);
  197.     }
  198.     _ReassignCodes();
  199.     return TRUE;
  200. }
  201.  
  202.  
  203.  
  204.  
  205. void UI_StringSequence::_ReassignCodes ()
  206. {
  207.     long n = Size();
  208.     SeqBind b (this, &UI_StringSequence::Update);
  209.     for (long i = 0; i < n; i++)
  210.         (*this)[i].PostChangeCode (b) = i;
  211. }
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219. void UI_StringSequence::_InsertionUpdate (long i)
  220. {
  221.     CL_ObjectSetIterator itr (_clientSet);
  222.  
  223.     itr.Reset();
  224.     while (itr.More())
  225.         ((UI_StringView*) itr.Next())->ItemInserted (i);
  226. }
  227.  
  228. void UI_StringSequence::_RemovalUpdate (long i)
  229. {
  230.     CL_ObjectSetIterator itr (_clientSet);
  231.  
  232.     itr.Reset();
  233.     while (itr.More())
  234.         ((UI_StringView*) itr.Next())->ItemRemoved (i);
  235. }
  236.  
  237. void UI_StringSequence::_ChangeUpdate (long i)
  238. {
  239.     CL_ObjectSetIterator itr (_clientSet);
  240.  
  241.     itr.Reset();
  242.     while (itr.More())
  243.         ((UI_StringView*) itr.Next())->ItemChanged(i);
  244. }
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.