home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / opendc12.zip / od124os2.exe / od12osp1.exe / src / text / paragrph.cpp < prev    next >
Text File  |  1997-04-02  |  7KB  |  226 lines

  1. //====START_GENERATED_PROLOG======================================
  2. //
  3. //
  4. //   COMPONENT_NAME: odtextpart
  5. //
  6. //   CLASSES: none
  7. //
  8. //   ORIGINS: 82,27
  9. //
  10. //
  11. //   (C) COPYRIGHT International Business Machines Corp. 1995,1996
  12. //   All Rights Reserved
  13. //   Licensed Materials - Property of IBM
  14. //   US Government Users Restricted Rights - Use, duplication or
  15. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  16. //       
  17. //   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18. //   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. //   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  20. //   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  21. //   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  22. //   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  23. //   OR PERFORMANCE OF THIS SOFTWARE.
  24. //
  25. //====END_GENERATED_PROLOG========================================
  26. //
  27. // @(#) 1.9 com/src/samples/text/paragrph.cpp, odtextpart, od96os2, odos29712d 2/21/97 13:40:42 [3/21/97 17:48:38]
  28. #include "paragrph.hpp"
  29. #include <string.h>
  30.  
  31. Paragraph::Paragraph(int org, char* chars, int numChars)
  32. {
  33.    if (numChars == 0) {
  34.      textBuffer = NULL;
  35.      textBufferSize = 0;
  36.    } else {
  37.      textBufferSize = numChars;
  38.      textBuffer = new char[textBufferSize];
  39.      memcpy(textBuffer, chars, numChars);  // Copy input text
  40.    }
  41.    mem_allocated = textBufferSize;
  42.    origin = org;
  43.  
  44. }
  45.  
  46. Paragraph::Paragraph(int org)
  47. {
  48.    textBufferSize = 0;
  49.    textBuffer = NULL;
  50.    mem_allocated = 0;
  51.    origin = org;
  52. }
  53.  
  54. Paragraph::~Paragraph()
  55. {
  56.    if (textBuffer != NULL)
  57.       delete textBuffer;
  58. }
  59.  
  60. void Paragraph::InsertText(int insPos, char* newChars, int numNewChars)
  61. {
  62.    // Check for just a plain newLine- no other characters.
  63.    if (numNewChars == 0)
  64.       return;
  65.  
  66.    // If the paragraph has been created but not had any text input yet,
  67.    // just copy the incoming text directly into a new buffer.
  68.    if (textBuffer == NULL) {
  69.       textBufferSize = numNewChars + insPos;
  70.       textBuffer = new char [textBufferSize];
  71.  
  72.       // If we have a non-Null insert position, we need to insert spaces
  73.       // before the first character.  This could happen if the user uses
  74.       // the right arrow key instead of the space bar to move the cursor.
  75.       origin = insPos;  // In case we add indentation function later...
  76.       for (int i=0; i < insPos; i++)  // Add spaces for indentation
  77.          textBuffer[i] = ' ';
  78.  
  79.       // now copy the input text
  80.       strncpy(textBuffer+insPos, newChars, numNewChars);
  81.  
  82.    // Otherwise, the paragraph already had some text, so insert the
  83.    // new text at the input column.
  84.    } else {
  85.       int charsBefore;
  86.       char* oldChars = textBuffer;
  87.       textBuffer = new char[textBufferSize + numNewChars];
  88.  
  89.       // Copy the old and inserted text into the new buffer
  90.       if (insPos != 0) {
  91.          charsBefore = insPos;
  92.          strncpy(textBuffer, oldChars, charsBefore);
  93.       } else {
  94.          charsBefore = 0;
  95.       } /* endif */
  96.       strncpy(textBuffer + charsBefore, newChars, numNewChars);
  97.       strncpy(textBuffer + charsBefore + numNewChars,
  98.               oldChars + charsBefore,
  99.               textBufferSize - charsBefore);
  100.  
  101.       textBufferSize += numNewChars;
  102.       delete oldChars;
  103.    } /* endif */
  104. }
  105.  
  106. void Paragraph::AppendText(char* newChars, int numNewChars)
  107. {
  108.    // Check for just a plain newLine- no other characters.
  109.    if (numNewChars == 0)
  110.       return;
  111.  
  112.    // If the paragraph has been created but not had any text input yet,
  113.    // just copy the incoming text directly into a new buffer.
  114.    if (textBuffer == NULL) {
  115.       textBuffer = new char [numNewChars];
  116.       strncpy(textBuffer, newChars, numNewChars);
  117.       textBufferSize = numNewChars;
  118.  
  119.    // Otherwise, the paragraph already had some text, so append the new text.
  120.    } else {
  121.       char* oldChars = textBuffer;
  122.       textBuffer = new char[textBufferSize + numNewChars];
  123.  
  124.       // Copy the old and appended text into the new buffer
  125.       strncpy(textBuffer, oldChars, textBufferSize);
  126.       strncpy(textBuffer+textBufferSize, newChars, numNewChars);
  127.  
  128.       textBufferSize += numNewChars;
  129.       delete oldChars;
  130.    } /* end if */
  131. }
  132.  
  133. void Paragraph::DeleteText(int from, int numChars)
  134. {
  135.    char* oldBuffer = textBuffer;
  136.  
  137.    if (textBuffer != NULL) {
  138.       numChars = min(numChars, textBufferSize - from + 1);
  139.       textBuffer = new char [textBufferSize - numChars];
  140.       memcpy(textBuffer,oldBuffer,from);  // Copy up to "from"
  141.       memcpy(textBuffer + from,
  142.              oldBuffer + from + numChars, // skip the deleted chars
  143.              textBufferSize - from - numChars);
  144.       textBufferSize -= numChars;
  145.       delete oldBuffer;
  146.    }
  147. }
  148.  
  149. void Paragraph::ReplaceText(int from, int to, char* newChars, int numNewChars)
  150. {
  151.    if (textBuffer == NULL) // No text to replace- just insert the new text.
  152.       InsertText(from, newChars, numNewChars);
  153.    else {
  154.       int deletedChars = to - from + 1;
  155.  
  156.       // If we have a one-for-one character replacement, just copy the
  157.       // new text over the old text.
  158.       if (deletedChars == numNewChars)
  159.          memcpy(textBuffer + from, newChars, numNewChars);
  160.  
  161.       // Otherwise, we are adding a different number of chars than we're
  162.       // deleting, so we need to allocate a new buffer.
  163.       else {
  164.          char* oldBuffer = textBuffer;
  165.          textBuffer = new char [textBufferSize - deletedChars + numNewChars];
  166.          memcpy(textBuffer, oldBuffer, from);  // Copy up to "from"
  167.          memcpy(textBuffer + from, newChars, numNewChars);  // Copy new chars
  168.          memcpy(textBuffer + from + numNewChars,
  169.                 oldBuffer + from + deletedChars, // Skip the replaced characters
  170.                 textBufferSize - from - deletedChars);
  171.          textBufferSize += (numNewChars - deletedChars);
  172.          delete oldBuffer;
  173.       }
  174.    }
  175. }
  176.  
  177. void Paragraph::Join(Paragraph* appendingPara)
  178. {
  179.    if (appendingPara != NULL) {
  180.       this->AppendText(appendingPara->textBuffer, appendingPara->textBufferSize);
  181.       delete appendingPara;
  182.    }
  183. }
  184.  
  185.  
  186. Paragraph* Paragraph::Split(int from)
  187. {
  188.    Paragraph* newPara;
  189.  
  190.    if (textBuffer == NULL) {
  191.       newPara = new Paragraph(0);
  192.    } else {
  193.       // Create a new paragraph with the text after the split.
  194.       char* newText = textBuffer + from;
  195.       newPara = new Paragraph(0, newText, textBufferSize - from);
  196.  
  197.       // Create a new buffer to hold the remaining text.
  198.       char* oldBuffer = textBuffer;
  199.       textBuffer = new char[from];
  200.       memcpy(textBuffer, oldBuffer, from);
  201.       textBufferSize = from;
  202.       delete oldBuffer;
  203.    }
  204.  
  205.    return newPara;
  206. }
  207.  
  208. void Paragraph::CopyText(int from, int to, char* outBuffer)
  209. {
  210.    if (textBuffer != NULL)
  211.       memcpy(outBuffer, textBuffer+from, to-from+1);
  212. }
  213.  
  214. // void Paragraph::DrawText(HDC hdc, RECT frameRect, int fontWidth, int fontAsc)
  215. // See textplat.cpp for definition
  216.  
  217. int Paragraph::GetOrigin()
  218. {
  219.    return origin;
  220. }
  221.  
  222. void Paragraph::ChangeOrigin(int orgPos)
  223. {
  224.    origin = orgPos;
  225. }
  226.