home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / ADDON.PAK / EDITTST2.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  9.9 KB  |  296 lines

  1. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  2.  
  3.   edittst2.cpp
  4.   Created: 03/11/95
  5.   Copyright (c) 1996, Borland International
  6.   $Revision:   1.2  $
  7.  
  8. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
  9. #ifndef __AOEXPCH_H
  10.   #include "aoexpch.h"
  11. #endif
  12. #pragma hdrstop
  13.  
  14. #include "edittst2.h"
  15.  
  16. //.............................................................................
  17. EditTestC::EditTestC()
  18. {
  19.   _editServer = NULL;
  20.  
  21. }
  22. //.............................................................................
  23. EditTestC::~EditTestC()
  24.   {
  25.   UnInit();
  26.   }
  27. //.............................................................................
  28. BOOL EditTestC::Init()
  29. {
  30.   if ( !_editServer )
  31.   {
  32.    OutStr( "EditTestC::Init()" );
  33.    _editServer = GET_INTERFACE(IEditorServer);
  34.    if ( _editServer == NULL )
  35.    {
  36.     return FALSE;
  37.    }
  38.   }
  39.   bid = -1;
  40.   return TRUE;
  41. }
  42. //.............................................................................
  43. void EditTestC::UnInit()
  44.   {
  45.   OutStr( "EditTestC::UnInit()" );
  46.   if ( _editServer )
  47.    {
  48.    _editServer->Release();
  49.    _editServer = NULL;
  50.    }
  51.   }
  52. //.............................................................................
  53. const char * EditTestC::GetName()
  54.   {
  55.   return "Editor Test A";
  56.   }
  57. //.............................................................................
  58. const char * EditTestC::GetTestDescription( int testNum )
  59.   {
  60.  
  61.   switch ( testNum )
  62.    {
  63.    case 1:
  64.     return "Insert some lines into a buffer";
  65.    case 2:
  66.     return "Insert a new line of text below current cursor position";
  67.    case 3:
  68.     return "Move first line of buffer to the end of the buffer";
  69.    case 4:
  70.     return "Insert a new line of text 3 lines below current cursor";
  71.    default:
  72.     return "This test not implemented.";
  73.    }
  74.   }
  75. //.............................................................................
  76. void EditTestC::DoTest( int testNum )
  77.   {
  78.   if ( !_editServer )
  79.    {
  80.    OutStr( "Editor Server not initialized!" );
  81.    return;
  82.    }
  83.   
  84.   switch ( testNum )
  85.   {
  86.    case 1:
  87.    {
  88.     /* This test uses an existing edit buffer or, if one doesn't exist,
  89.       it creates one.  The buffer to be used is given focus using the
  90.       set_buffer and show_buffer methods and then a line of text and a
  91.       carriage return/newline are inserted at the beginning of the buffer.
  92.       The cursor is then moved back up to the beginning of the inserted line
  93.       of text.  At the end of this test the cursor is on the first column of
  94.       the line in which the "This line has been..." has just been inserted.
  95.      
  96.      Methods used: begin_edit   end_edit   inq_buffer     create_buffer
  97.               set_buffer    show_buffer beginning_of_line
  98.               insert      up
  99.     */ 
  100.     
  101.     IPolyString *insertString = ::MakePolyString( "This line has been inserted using the insert method.\r\n" );
  102.     if (insertString != NULL)
  103.     {
  104.       _editServer->begin_edit();
  105.       bid = _editServer->inq_buffer();
  106.       if ( bid > 0 )
  107.       {
  108.         _editServer->beginning_of_line();
  109.         _editServer->insert(insertString);
  110.         _editServer->up();
  111.         OutStr( FormatStr( "Text insert suceeded." ));
  112.       }                  
  113.       else
  114.       {
  115.         // There is not an edit buffer available so attempt to create one
  116.         IPolyString *newEditFile = ::MakePolyString( "edittst2.out" );
  117.         if (newEditFile != NULL)
  118.         { 
  119.           bid = _editServer->create_buffer( newEditFile, 0 );
  120.           if ( bid > 0)
  121.           {
  122.             OutStr( FormatStr( "Created a Buffer.  Bid is %d", bid ));
  123.             _editServer->set_buffer( bid );
  124.             _editServer->show_buffer();
  125.             _editServer->beginning_of_line();
  126.             _editServer->insert(insertString);
  127.             _editServer->up();
  128.             OutStr( FormatStr( "Text insert suceeded." ));
  129.           }
  130.           else
  131.             OutStr( FormatStr( "Failure to find or create an edit buffer." ));
  132.         }
  133.         else
  134.           OutStr( FormatStr( "Failure to make polystr" ));
  135.       }
  136.       _editServer->end_edit();
  137.     }
  138.     else
  139.       OutStr( FormatStr("Failure to make a polystr."));
  140.     break;
  141.    }
  142.  
  143.    case 2:
  144.     /* This test uses the edit buffer that was used in Test 1. The buffer to
  145.       be used is given focus using the set_buffer and show_buffer methods
  146.       and then a line of text is inserted on the line immediately below
  147.       the current cursor location and the cursor is set to the end of the
  148.       line that has just been insterted. In order for the line of text to be
  149.       inserted a line must already exist below the current cursor.  Otherwise
  150.       the message "Could not go down a line." appears in the Output box of
  151.       the BCW Add-on Tester window.
  152.      
  153.      Methods used: begin_edit   end_edit   set_buffer   show_buffer
  154.               beginning_of_line      insert     down
  155.     */ 
  156.    {
  157.     _editServer->begin_edit();
  158.     if ( bid > 0 )
  159.     {
  160.       OutStr( FormatStr( "Test 2:  Bid is: %d.", bid) );
  161.       _editServer->set_buffer( bid );
  162.       _editServer->show_buffer();
  163.       if ( _editServer->down() )
  164.       {
  165.         IPolyString *insertString = ::MakePolyString( 
  166.           "This line has been inserted below the (formerly) current cursor.\r\n" );
  167.         if (insertString != NULL)
  168.         {
  169.           _editServer->beginning_of_line();
  170.           _editServer->insert(insertString);
  171.           OutStr( FormatStr( "Text insert suceeded." ));
  172.         }                  
  173.         else
  174.           OutStr( FormatStr( "Failure to make a polystr." ));
  175.       }
  176.       else
  177.         OutStr( FormatStr( "Could not go down a line." ) );
  178.     }
  179.     else
  180.       OutStr( FormatStr( "Failure to find the edit buffer used in Test 1." ));
  181.     _editServer->end_edit();
  182.    }
  183.    break;
  184.  
  185.    case 3:
  186.    {
  187.     /* This test uses the edit buffer that was used in Test 1. The buffer to
  188.       be used is given focus using the set_buffer and show_buffer methods
  189.       and then the first line of text in the buffer is deleted and then
  190.       inserted as the last line of the buffer. there must be at least two
  191.       lines of text in the buffer for this test to be effective.  If there
  192.       isn't the message "Must be at least two lines in buffer." appears in
  193.       the Output box of the BCW Add-on Tester window.
  194.      
  195.      Methods used: begin_edit       end_edit         set_buffer
  196.               show_buffer      top_of_buffer      end_of_buffer
  197.               inq_line_length   beginning_of_line  end_of_line
  198.               up             down           read
  199.               delete_line      insert
  200.     */ 
  201.     _editServer->begin_edit();        
  202.     if ( bid > 0 )
  203.     {
  204.       _editServer->set_buffer( bid );
  205.       _editServer->show_buffer();
  206.       _editServer->top_of_buffer();
  207.       // We're on the first line.  Is there another line?
  208.       if (_editServer->down())
  209.       {
  210.         _editServer->up();
  211.  
  212.         /* 
  213.           The default behavior of the read method is to read up to and
  214.           including the end-of-line characters if the number of characters
  215.           to be read is to provided.  In this case we don't what to have
  216.           a new line added to the end of the buffer so to get around this,
  217.           use inq_line_length.  This method will return the number of 
  218.           characters in the give line NOT INCLUDING the end-of-line
  219.           characters.  Then use the value returned by inq_line_length in
  220.           the call to the read method and you have the line without the
  221.           end-of-line stuff.
  222.         */
  223.         int nLineLength = _editServer->inq_line_length();
  224.         OutStr( FormatStr ( "nLineLength is %d.", nLineLength ));
  225.         IPolyString *chars = _editServer->read(nLineLength);
  226.         
  227.         _editServer->delete_line();
  228.         _editServer->end_of_buffer();
  229.         // if the last line in the buffer is not blank...
  230.         if (_editServer->inq_line_length())
  231.         {
  232.           _editServer->end_of_line();
  233.           IPolyString *crNl = ::MakePolyString("\r\n");
  234.           _editServer->insert( crNl );
  235.         }
  236.         _editServer->insert( chars );
  237.       }
  238.       else
  239.         OutStr( FormatStr ( "Must be at lest two lines in buffer." ));
  240.     }
  241.     else
  242.       OutStr( FormatStr( "Failure to find the edit buffer used in Test 1." ));
  243.     _editServer->end_edit();
  244.    }
  245.    break;
  246.  
  247.  
  248.    case 4:
  249.    {
  250.     /* This test uses the edit buffer that was used in Test 1. The buffer to
  251.       be used is given focus using the set_buffer and show_buffer methods.
  252.       Once the buffer has focus then the cursor is moved three lines down
  253.       from it's current position and a new line of text is entered.
  254.      
  255.      Methods used: begin_edit       end_edit         set_buffer
  256.               show_buffer      top_of_buffer      end_of_buffer
  257.               inq_position      goto_line
  258.     */ 
  259.  
  260.     long line;
  261.     
  262.     _editServer->begin_edit();
  263.  
  264.     /* Insert a new line of text 3 lines below current cursor */
  265.     if ( bid > 0 )
  266.     {
  267.       _editServer->set_buffer( bid );
  268.       _editServer->show_buffer();
  269.       _editServer->inq_position( &line );
  270.       // goto the beginning of the third line down
  271.       if ( _editServer->goto_line( line + 3 ) )
  272.       {
  273.         IPolyString *insertString = ::MakePolyString( "This line has been inserted three lines below the (formerly) current cursor.\r\n" );
  274.         if (insertString != NULL)
  275.         {
  276.           _editServer->insert(insertString);
  277.           OutStr( FormatStr( "Text insert suceeded." ));
  278.         }
  279.         else
  280.           OutStr( FormatStr( "Failure to make polystr" ));
  281.       }
  282.       else
  283.         OutStr( FormatStr( "Too close to the end of the file.") );
  284.     }
  285.     else
  286.       OutStr( FormatStr( "Failure to find the edit buffer used in Test 1." ));
  287.     
  288.     _editServer->end_edit();
  289.     break;
  290.    }
  291.     
  292.    default:
  293.     OutStr( FormatStr( "Test #%d Not Implemented!", testNum ) );
  294.   }
  295. }
  296.