home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / cnr / spreadsh / table.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-29  |  4.1 KB  |  159 lines

  1. //************************************************************
  2. // Container Control - Dynamic Creation Of Objects
  3. //
  4. // Copyright (C) 1994, Law, Leong, Love, Olson, Tsuji.
  5. // Copyright (c) 1997 John Wiley & Sons, Inc. 
  6. // All Rights Reserved.
  7. //************************************************************
  8. #include <ipoint.hpp>
  9. #include <istring.hpp>
  10. #include <iexcept.hpp>
  11. #include <icnrcol.hpp>
  12. #include <icnrctl.hpp>
  13. #include <new.h>
  14. #include "table.hpp"
  15.  
  16. unsigned long TableObject::fgFieldCount = 0;
  17. IString* TableObject__headingList = 0;
  18.  
  19. // Primary Constructor
  20. TableObject::TableObject( )
  21.              : IContainerObject(0)
  22. {
  23.   IASSERTSTATE(TableObject::fgFieldCount != 0)
  24.    // Call global operator new using placement syntax.
  25.    // This will result in no storage being allocated but
  26.    // the constructor gets called.
  27.    for(unsigned long s=1; s<fgFieldCount; s++)
  28.    {
  29.         new(&fFieldArray[s]) IString();
  30.    }
  31. }
  32.  
  33. // Copy Constructor
  34. TableObject::TableObject(TableObject& oldObject)
  35.                   : IContainerObject (oldObject)
  36. {
  37.    fgFieldCount = oldObject.fgFieldCount;
  38.    unsigned long s;
  39.    // Call global operator new using placement syntax.
  40.    for(s=1; s<fgFieldCount; s++)
  41.    {
  42.        new (this->fFieldArray+s) IString(oldObject.fFieldArray[s]);
  43.    }
  44.  
  45. }
  46.  
  47. // Derived class required function to make a copy.
  48. IContainerObject* TableObject::objectCopy()
  49. {
  50.    TableObject* pObject = new TableObject(*this);
  51.    return (IContainerObject*)pObject;
  52. }
  53.  
  54. // Operator new for variable number of fields.
  55. void* TableObject :: operator new(size_t        size)
  56. {
  57.  
  58.  
  59.   void* pobject = IContainerObject::operator new( size +
  60.                      (size_t)fgFieldCount*sizeof(IString));
  61.   return(pobject);
  62. }
  63.  
  64. // Destructor
  65. TableObject::~TableObject()
  66. {
  67.    // Since the first field is handled by destruction
  68.    // of the IString Array, we delete here from the
  69.    // second IString. Note that we are explicitly
  70.    // calling the destructor since we don't want any
  71.    // storage freed.
  72.    unsigned long s;
  73.    for(s=1; s<this->fgFieldCount; s++)
  74.   {
  75.       this->fFieldArray[s].~IString();
  76.   }
  77. }
  78.  
  79.  
  80. // Function to replace the text in a field.
  81. TableObject& TableObject::setFieldText(const IString& strText,
  82.                                   unsigned long fieldIndex)
  83. {
  84.   IASSERTPARM(fieldIndex < fgFieldCount)
  85.   if (fieldIndex < fgFieldCount)
  86.   {
  87.     fFieldArray[fieldIndex] = strText;
  88.   }
  89.   return *this;
  90. }
  91.  
  92. // Return the text at a field index
  93. IString TableObject::fieldText( unsigned long fieldIndex) const
  94. {
  95.   return this->fFieldArray[fieldIndex];
  96. }
  97.  
  98.  
  99. void TableObject::setFieldCount( unsigned long fieldCount)
  100. {
  101.   fgFieldCount = fieldCount;
  102. }
  103.  
  104. unsigned long TableObject::fieldCount ( )
  105. {
  106.    return fgFieldCount;
  107. }
  108.  
  109. void TableObject::setHeadingText( const IString& headingText,
  110.                                   unsigned long  headingIndex)
  111. {
  112.   // For simplicity, only support 50 Headings
  113.   if (TableObject__headingList == 0) 
  114.     TableObject__headingList = new IString[50];
  115.  
  116.   TableObject__headingList[headingIndex] = headingText;
  117. }
  118.  
  119. IString TableObject::headingText ( unsigned long  headingIndex)
  120. {
  121.    return TableObject__headingList[headingIndex];
  122. }
  123.  
  124.  
  125. void TableObject::createAllColumnsFor ( IContainerControl&  container)
  126. {
  127.  
  128.   for (unsigned long s=0; s<fgFieldCount; s++ ) {
  129.     createAndOrphanColumnFor( container, s);
  130.   }
  131.  
  132.   container.setDeleteColumnsOnClose();
  133. }
  134.  
  135.  
  136. IContainerColumn* TableObject::createAndOrphanColumnFor ( 
  137.                              IContainerControl& container,
  138.                              unsigned long      fieldNumber)
  139. {
  140.   // Setup a default Column.
  141.   unsigned long offset = offsetof(TableObject,fFieldArray[0])
  142.      + fieldNumber*(sizeof(IString));
  143.  
  144.   IContainerColumn*  newColumn = new IContainerColumn(offset);
  145.  
  146.   // Insure that a new column has been created.
  147.   IASSERTSTATE(newColumn != 0)
  148.  
  149.   // Put the text in the heading and make it editable.
  150.   newColumn->setHeadingText(headingText(fieldNumber));
  151.   newColumn->enableDataUpdate();
  152.  
  153.   // Add the column to the container.
  154.   container.addColumn( newColumn);
  155.  
  156.   return newColumn;
  157. }
  158.  
  159.