home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / cnr / dynobj / table.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-29  |  4.2 KB  |  158 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 does not allocate any storage, 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. // The objectCopy functions is required to support
  48. // direct manipulation copy operations.
  49. IContainerObject* TableObject::objectCopy()
  50. {
  51.    TableObject* pObject = new TableObject(*this);
  52.    return (IContainerObject*)pObject;
  53. }
  54.  
  55. // Operator new for variable number of fields.
  56. void* TableObject :: operator new(size_t        size)
  57. {
  58.   void* tableObject = IContainerObject::operator new( size +
  59.                      (size_t)fgFieldCount*sizeof(IString));
  60.   return(tableObject);
  61. }
  62.  
  63. // Destructor
  64. TableObject::~TableObject()
  65. {
  66.    // Because the first field is handled by destruction
  67.    // of the IString array, we delete from the
  68.    // second IString. Note that we are explicitly
  69.    // calling the destructor because we don't want any
  70.    // storage freed.
  71.    unsigned long s;
  72.    for(s=1; s<this->fgFieldCount; s++)
  73.   {
  74.       this->fFieldArray[s].~IString();
  75.   }
  76. }
  77.  
  78.  
  79. // Sets new text into a field at the specified index.
  80. TableObject& TableObject::setFieldText(const IString& strText,
  81.                                   unsigned long fieldIndex)
  82. {
  83.   IASSERTPARM(fieldIndex < fgFieldCount)
  84.   if (fieldIndex < fgFieldCount)
  85.   {
  86.     fFieldArray[fieldIndex] = strText;
  87.   }
  88.   return *this;
  89. }
  90.  
  91. // Returns the text at a field index.
  92. IString TableObject::fieldText( unsigned long fieldIndex) const
  93. {
  94.   return this->fFieldArray[fieldIndex];
  95. }
  96.  
  97.  
  98. void TableObject::setFieldCount( unsigned long fieldCount)
  99. {
  100.   fgFieldCount = fieldCount;
  101. }
  102.  
  103. unsigned long TableObject::fieldCount ( )
  104. {
  105.    return fgFieldCount;
  106. }
  107.  
  108. void TableObject::setHeadingText( const IString& headingText,
  109.                                   unsigned long  headingIndex)
  110. {
  111.   // For simplicity, we only support 50 Headings
  112.   if (TableObject__headingList == 0) 
  113.     TableObject__headingList = new IString[50];
  114.  
  115.   TableObject__headingList[headingIndex] = headingText;
  116. }
  117.  
  118. IString TableObject::headingText ( unsigned long  headingIndex)
  119. {
  120.    return TableObject__headingList[headingIndex];
  121. }
  122.  
  123.  
  124. void TableObject::createAllColumnsFor ( IContainerControl&  container)
  125. {
  126.  
  127.   for (unsigned long s=0; s<fgFieldCount; s++ ) {
  128.     createAndOrphanColumnFor( container, s);
  129.   }
  130.  
  131.   container.setDeleteColumnsOnClose();
  132. }
  133.  
  134.  
  135. IContainerColumn* TableObject::createAndOrphanColumnFor ( 
  136.                              IContainerControl& container,
  137.                              unsigned long      fieldNumber)
  138. {
  139.   // Set up a default column.
  140.   unsigned long offset = offsetof(TableObject,fFieldArray[0])
  141.      + fieldNumber*(sizeof(IString));
  142.  
  143.   IContainerColumn*  newColumn = new IContainerColumn(offset);
  144.  
  145.   // Ensure that a new column has been created.
  146.   IASSERTSTATE(newColumn != 0)
  147.  
  148.   // Put the text in the heading and make it editable.
  149.   newColumn->setHeadingText(headingText(fieldNumber));
  150.   newColumn->enableDataUpdate();
  151.  
  152.   // Add the column to the container.
  153.   container.addColumn( newColumn);
  154.  
  155.   return newColumn;
  156. }
  157.  
  158.