TVision
view release on metacpan or search on metacpan
tvision.git/examples/tvforms/listdlg.cpp view on Meta::CPAN
void TListKeyBox::getText( char *dest, short item, short maxLen )
{
switch (list()->keyType)
{
case stringKey:
TSortedListBox::getText(dest, item, maxLen);
break;
case longIntKey:
ltoa(*(int32_t *)list()->keyOf(list()->at(item)), dest, 10);
break;
}
}
// TListDialog
TListDialog::TListDialog( char *rezName, char *title) :
TWindowInit(&TListDialog::initFrame),
TDialog(TRect( 2, 2, 32, 15 ), title),
dataCollection(0),
fileName(newStr(rezName)),
isValid(False),
modified(False)
{
const short
buttonCt = 4,
listX = 2,
listY = 3,
formWd = 30,
formHt = 13,
defaultListWd = 12,
listHt = buttonCt * 2,
buttonWd = 12,
buttonY = listY;
TScrollBar *sb;
short y;
TForm *f;
short listWd;
short buttonX;
// Read data off resource stream
if (openDataFile(fileName, formDataFile, ios::in) == True)
{
// Get horizontal size of key field
f = (TForm *)formDataFile->get("FormDialog");
if (f == NULL)
{
messageBox("Error accessing file data.", mfError | mfOKButton);
return;
}
// Base listbox width on key width. Grow entire dialog if required
if (f->keyWidth > defaultListWd)
{
listWd = f->keyWidth;
growTo((short)(formWd + listWd - defaultListWd), (short)formHt);
}
else
listWd = defaultListWd;
// Move to upper right corner of desktop
TRect r (TProgram::deskTop->getExtent()); // Desktop coordinates
moveTo((short)(r.b.x - size.x), 1);
destroy(f);
// Read data collection into memory
dataCollection = (TDataCollection *)formDataFile->get("FormData");
if (dataCollection != NULL)
{
// Loaded successfully: build ListDialog dialog
// Scrollbar
sb = new TScrollBar( TRect(listX + listWd, listY,
listX + listWd + 1, listY + listHt));
insert(sb);
// List box
list = new TListKeyBox( TRect(listX, listY, listX + listWd,
listY + listHt), 1, sb);
list->newList(dataCollection);
insert(list);
// Label
insert(new TLabel ( TRect(listX, listY - 1,
listX + 10, listY), "~K~eys", list));
// Buttons
buttonX = listX + listWd + 2;
y = buttonY;
insert(new TButton (TRect(buttonX, y, buttonX + buttonWd,
y + 2), "~E~dit", cmFormEdit, bfDefault));
y += 2;
insert(new TButton (TRect(buttonX, y, buttonX + buttonWd,
y + 2), "~N~ew", cmFormNew, bfNormal));
y += 2;
insert(new TButton (TRect(buttonX, y, buttonX + buttonWd,
y + 2), "~D~elete", cmFormDel, bfNormal));
y += 2;
insert(new TButton (TRect(buttonX, y, buttonX + buttonWd,
y + 2), "~S~ave", cmListSave, bfNormal));
selectNext(False); // Select first field
isValid = True;
}
}
}
TListDialog::~TListDialog(void)
{
if (dataCollection)
destroy(dataCollection);
if (formDataFile != NULL)
destroy(formDataFile);
if (fileName != NULL)
delete[] fileName;
}
void TListDialog::close(void)
{
if (valid(cmClose))
{
// Stop desktop video update in case there are scores of attached forms
TProgram::deskTop->lock();
message(TProgram::deskTop, evBroadcast, cmCloseForm, this);
TProgram::deskTop->unlock();
destroy(this);
}
}
TForm *TListDialog::editingForm()
{
// Return pointer to the form that is editing the current selection
return (TForm *)message(TProgram::deskTop, evBroadcast, cmEditingForm,
dataCollection->at(list->focused));
}
void TListDialog::formOpen(Boolean newForm)
{
TForm *f;
if (!newForm)
{
// Empty collection?
if (dataCollection->getCount() == 0)
return;
// If selection is being edited, then bring its form to top
f = editingForm();
if (f != NULL)
{
f->select();
return;
}
}
// Selection is not being edited: open new form from the resource file
f = (TForm *) formDataFile->get("FormDialog");
if (f == NULL)
messageBox("Error opening form.", mfError | mfOKButton);
else
{
f->listDialog = this; // Form points back to List
if (newForm)
f->prevData = NULL; // Adding new form
else
{
// Edit data from collection
f->prevData = dataCollection->at(list->focused);
f->setData(f->prevData);
}
if (TApplication::application->validView(f) != NULL)
{
stackOnPrev(f);
TProgram::deskTop->insert(f); // Insert & select
}
}
}
void TListDialog::deleteSelection()
{
TForm *f;
tvision.git/examples/tvforms/listdlg.cpp view on Meta::CPAN
}
memset(p, 0, dataCollection->itemSize);
f->getData(p);
// If no duplicates, make sure not attempting to add duplicate key
if ( (!(dataCollection->duplicates) && dataCollection->search(dataCollection->keyOf(p), i)) )
if ( (((TForm*)f)->prevData == NULL) || (((TForm *)f)->prevData != dataCollection->at(i)) )
{
free(p);
messageBox("Duplicate keys are not allowed in this database. "
"Delete duplicate record before saving this form.",
mfError | mfOKButton);
return False;
}
// Free previous data.
if (((TForm *)f)->prevData != NULL)
dataCollection->free(((TForm*)f)->prevData);
// TDataCollection.Insert may fail because it doesn't use
// the safety pool. Check status field after insert and cleanup
// if necessary.
dataCollection->insert(p);
if (dataCollection->status != 0)
{
free(p);
TApplication::application->outOfMemory();
return False;
}
// Success: store off original data pointer
((TForm *)f)->prevData = p;
// Redraw list
list->setRange(dataCollection->getCount());
list->drawView();
modified = True;
return True;
}
void TListDialog::stackOnPrev(TDialog *f)
{
TForm *topForm;
// Stack on top topmost form or on top list if first form
topForm = (TForm *)message(owner, evBroadcast, cmTopForm, this);
if (topForm != NULL)
// Stack on top previous topmost form
f->moveTo(topForm->origin.x + 1, topForm->origin.y + 1);
else
{
// Stack right or left of ListDialog
if (origin.x > f->size.x)
f->moveTo(0, origin.y);
else
f->moveTo(origin.x + size.x + 1, origin.y);
}
// Visible on desktop? Make sure at least half of form is visible
TRect r = owner->getExtent(); // Desktop coordinates
if (f->origin.x + size.x / 2 > r.b.x)
f->moveTo(0, 1);
if (f->origin.y + size.y / 2 > r.b.y)
f->moveTo(f->origin.x, 1);
}
Boolean TListDialog::valid(ushort command)
{
Boolean ok;
ushort reply;
ok = True;
switch (command)
{
case cmValid:
ok = isValid;
if (!ok)
messageBox(mfError | mfOKButton, "Error opening file (%s).", fileName);
break;
case cmQuit:
case cmClose:
// Any forms open that cannot close?
if (message(TProgram::deskTop, evBroadcast, cmCanCloseForm, this) == NULL)
ok = True;
else
ok = False;
// Any data modified?
if (ok && modified)
{
select();
reply = messageBox("Database has been modified. Save? ",
mfYesNoCancel);
switch (reply)
{
case cmYes:
ok = saveList();
break;
case cmNo:
modified = False; // abandon changes
break;
default:
ok = False; // cancel close request
break;
}
}
break;
}
if (ok)
return TDialog::valid(command);
else
return False;
}
( run in 1.204 second using v1.01-cache-2.11-cpan-5623c5533a1 )