home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Mobile / Chip_Mobile_2001.iso / palm / business / printcar / printcar.exe / src / UI / ListChoices.cc < prev    next >
C/C++ Source or Header  |  2000-06-03  |  4KB  |  169 lines

  1. //
  2. //  $Id: ListChoices.cc,v 1.1.1.1 2000/06/02 22:23:00 sergey Exp $
  3. //
  4.  
  5. #include <Pilot.h>
  6. #include "ListChoices.h"
  7. #include "Util/Assert.h"
  8. #include "Util/Error.h"
  9.  
  10.  
  11. namespace UI
  12. {
  13.     ListChoices::ListChoices():
  14.         _count(0), _bufferHandle(0), _items(0)
  15.     {}
  16.  
  17.     ListChoices::~ListChoices()
  18.     {
  19.         cleanUp();
  20.     }
  21.  
  22.     ListChoices::ListChoices(const ListChoices& other):
  23.         _count(0), _bufferHandle(0), _items(0)
  24.     {
  25.         operator =(other);
  26.     }
  27.  
  28.     ListChoices& ListChoices::operator =(const ListChoices& other)
  29.     {
  30.         if (this != &other)
  31.         {
  32.             cleanUp();
  33.  
  34.             _count = other._count;
  35.             _bufferHandle = other._bufferHandle;
  36.             _items = other._items;
  37.  
  38.             // Prevent buffers from being deleted during the destruction of the "other" object.
  39.             const_cast<ListChoices&>(other).release();
  40.         }
  41.  
  42.         return *this;
  43.     }
  44.  
  45.     // operations
  46.  
  47.     void ListChoices::add(const char* text)
  48.     {
  49.         assert(text != 0);
  50.  
  51.         deleteItems();     // delete previously created list of items
  52.  
  53.         int offset = bufferSize();
  54.  
  55.         if (ensureBufferSize(offset+StrLen(text)+1))
  56.         {
  57.             copyTextToBuffer(offset, text);
  58.             ++_count;
  59.         }
  60.     }
  61.  
  62.     // attributes
  63.  
  64.     const char* ListChoices::textItem(int index) const
  65.     {
  66.         return textItems() == 0? 0 : textItems()[index];
  67.     }
  68.  
  69.     const char** ListChoices::textItems() const
  70.     {
  71.         if (_items == 0)
  72.             const_cast<ListChoices*>(this)->createItems();
  73.  
  74.         return _items;
  75.     }
  76.  
  77.     // implementation
  78.  
  79.     void ListChoices::createItems()
  80.     {
  81.         if (_count > 0)
  82.         {
  83.             assert(_items == 0);
  84.             assert(_bufferHandle != 0);
  85.  
  86.             _items = (const char**)MemPtrNew(_count*sizeof(char*));
  87.             if (_items != 0)
  88.                 initItems(_items, (const char*)MemHandleLock(_bufferHandle), _count);
  89.         }
  90.     }
  91.  
  92.     void ListChoices::initItems(const char** items, const char* buffer, int count)
  93.     {
  94.         assert(items != 0);
  95.         assert(buffer != 0);
  96.  
  97.         for (int i = 0; i < count; ++i)
  98.         {
  99.             items[i] = buffer;
  100.             buffer += StrLen(buffer)+1;
  101.         }
  102.     }
  103.  
  104.     void ListChoices::deleteItems()
  105.     {
  106.         if (_items != 0)
  107.         {
  108.             MemPtrFree(_items);
  109.             _items = 0;
  110.  
  111.             MemHandleUnlock(_bufferHandle);     // existence of the items assumes lock on the buffer
  112.         }
  113.     }
  114.  
  115.     bool ListChoices::ensureBufferSize(int size)
  116.     {
  117.         if (_bufferHandle == 0)
  118.         {
  119.             if ((_bufferHandle = MemHandleNew(size)) != 0)
  120.                 return true;
  121.         }
  122.         else
  123.         {
  124.             if (MemHandleResize(_bufferHandle, size) == 0)
  125.                 return true;
  126.         }
  127.  
  128.         Util::Error::memoryAllocationError(__FILE__, __LINE__);
  129.         return false;
  130.     }
  131.  
  132.     void ListChoices::deleteBuffer()
  133.     {
  134.         if (_bufferHandle != 0)
  135.         {
  136.             MemHandleFree(_bufferHandle);
  137.             _bufferHandle = 0;
  138.         }
  139.     }
  140.  
  141.     int ListChoices::bufferSize() const
  142.     {
  143.         return _bufferHandle != 0? MemHandleSize(_bufferHandle) : 0;
  144.     }
  145.  
  146.     void ListChoices::copyTextToBuffer(int offset, const char* text)
  147.     {
  148.         assert(_bufferHandle != 0);
  149.  
  150.         StrCopy((char*)MemHandleLock(_bufferHandle)+offset, text);
  151.         MemHandleUnlock(_bufferHandle);
  152.     }
  153.  
  154.     void ListChoices::cleanUp()
  155.     {
  156.         deleteItems();
  157.         deleteBuffer();
  158.         _count = 0;
  159.     }
  160.  
  161.     void ListChoices::release()
  162.     {
  163.         _items = 0;
  164.         _bufferHandle = 0;
  165.         _count = 0;
  166.     }
  167. }
  168. // namespace UI
  169.