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 >
Wrap
C/C++ Source or Header
|
2000-06-03
|
4KB
|
169 lines
//
// $Id: ListChoices.cc,v 1.1.1.1 2000/06/02 22:23:00 sergey Exp $
//
#include <Pilot.h>
#include "ListChoices.h"
#include "Util/Assert.h"
#include "Util/Error.h"
namespace UI
{
ListChoices::ListChoices():
_count(0), _bufferHandle(0), _items(0)
{}
ListChoices::~ListChoices()
{
cleanUp();
}
ListChoices::ListChoices(const ListChoices& other):
_count(0), _bufferHandle(0), _items(0)
{
operator =(other);
}
ListChoices& ListChoices::operator =(const ListChoices& other)
{
if (this != &other)
{
cleanUp();
_count = other._count;
_bufferHandle = other._bufferHandle;
_items = other._items;
// Prevent buffers from being deleted during the destruction of the "other" object.
const_cast<ListChoices&>(other).release();
}
return *this;
}
// operations
void ListChoices::add(const char* text)
{
assert(text != 0);
deleteItems(); // delete previously created list of items
int offset = bufferSize();
if (ensureBufferSize(offset+StrLen(text)+1))
{
copyTextToBuffer(offset, text);
++_count;
}
}
// attributes
const char* ListChoices::textItem(int index) const
{
return textItems() == 0? 0 : textItems()[index];
}
const char** ListChoices::textItems() const
{
if (_items == 0)
const_cast<ListChoices*>(this)->createItems();
return _items;
}
// implementation
void ListChoices::createItems()
{
if (_count > 0)
{
assert(_items == 0);
assert(_bufferHandle != 0);
_items = (const char**)MemPtrNew(_count*sizeof(char*));
if (_items != 0)
initItems(_items, (const char*)MemHandleLock(_bufferHandle), _count);
}
}
void ListChoices::initItems(const char** items, const char* buffer, int count)
{
assert(items != 0);
assert(buffer != 0);
for (int i = 0; i < count; ++i)
{
items[i] = buffer;
buffer += StrLen(buffer)+1;
}
}
void ListChoices::deleteItems()
{
if (_items != 0)
{
MemPtrFree(_items);
_items = 0;
MemHandleUnlock(_bufferHandle); // existence of the items assumes lock on the buffer
}
}
bool ListChoices::ensureBufferSize(int size)
{
if (_bufferHandle == 0)
{
if ((_bufferHandle = MemHandleNew(size)) != 0)
return true;
}
else
{
if (MemHandleResize(_bufferHandle, size) == 0)
return true;
}
Util::Error::memoryAllocationError(__FILE__, __LINE__);
return false;
}
void ListChoices::deleteBuffer()
{
if (_bufferHandle != 0)
{
MemHandleFree(_bufferHandle);
_bufferHandle = 0;
}
}
int ListChoices::bufferSize() const
{
return _bufferHandle != 0? MemHandleSize(_bufferHandle) : 0;
}
void ListChoices::copyTextToBuffer(int offset, const char* text)
{
assert(_bufferHandle != 0);
StrCopy((char*)MemHandleLock(_bufferHandle)+offset, text);
MemHandleUnlock(_bufferHandle);
}
void ListChoices::cleanUp()
{
deleteItems();
deleteBuffer();
_count = 0;
}
void ListChoices::release()
{
_items = 0;
_bufferHandle = 0;
_count = 0;
}
}
// namespace UI