home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-09-12 | 4.6 KB | 241 lines | [TEXT/CWIE] |
- /*
- CEntryList.cp
-
- Copyright © 1995 Alastair Rankine.
- All Rights Reserved.
- */
-
- #include "CEntryList.h"
- #include "CEntry.h"
- #include "CDeleteEntryAction.h"
- #include "MessageIDs.h"
- #include "CLookupDocument.h"
-
- CEntryList::CEntryList(LStream * inStream)
- : CListBox(inStream)
- , mDocument(0)
- {
- Assert_(mMacListH);
-
- (**mMacListH).selFlags = lOnlyOne;
- }
-
- void CEntryList::LDEFDraw( Boolean lSelect, Rect *lRect, Cell lCell, short /*lDataOffset*/,
- short /*lDataLen*/)
- {
- ::EraseRect( lRect );
-
- const CEntry & rec = GetEntryAt(lCell);
-
- ::MoveTo( lRect->left+2, lRect->top + (**mMacListH).indent.v );
- ::DrawString(rec.GetField(0).ToPString());
-
- if ( lSelect ) {
- ::LMSetHiliteMode( LMGetHiliteMode() & ~(1 << hiliteBit) );
- ::InvertRect( lRect );
- }
- }
-
- void CEntryList::LDEFHilite( Boolean lSelect, Rect *lRect, Cell lCell,
- short /*lDataOffset*/, short /*lDataLen*/)
- {
- ::LMSetHiliteMode( LMGetHiliteMode() & ~(1 << hiliteBit) );
- ::InvertRect( lRect );
-
- if(lSelect) {
- const CEntry * rec = &GetEntryAt(lCell);
- BroadcastMessage(msg_CellSelected, &rec);
- }
- else
- BroadcastMessage(msg_CellDeselected, nil);
- }
-
- void CEntryList::LDEFInitialize()
- {
- FontInfo fInfo;
- ::GetFontInfo( &fInfo );
- (**mMacListH).indent.h = 2;
- (**mMacListH).indent.v = ((**mMacListH).cellSize.v -
- (fInfo.ascent+fInfo.descent)) / 2 + fInfo.ascent;
- }
-
- Boolean CEntryList::ObeyCommand(CommandT inCommand, void * ioParam)
- {
- Boolean commandHandled = false;
-
- switch(inCommand)
- {
- case cmd_Clear:
- Assert_(mDocument);
- PostAction(new CDeleteEntryAction(*mDocument, GetSelection()));
- break;
- default:
- commandHandled = CListBox::ObeyCommand(inCommand, ioParam);
- break;
- }
-
- return commandHandled;
- }
-
- void CEntryList::FindCommandStatus(CommandT inCommand, Boolean & outEnabled,
- Boolean & outUsesMark, Char16 & outMark, Str255 outName)
- {
- switch (inCommand)
- {
- case cmd_Clear:
- outEnabled = (GetSelection() != kEmptySelection);
- break;
- default:
- CListBox::FindCommandStatus(inCommand, outEnabled, outUsesMark, outMark, outName);
- break;
- }
- }
-
- Int16 CEntryList::GetSelection() const
- {
- Cell cell = {0, 0};
-
- if(::LGetSelect(true, &cell, mMacListH))
- return cell.v;
- else
- return kEmptySelection;
- }
-
- void CEntryList::FinishCreateSelf()
- {
- CListBox::FinishCreateSelf();
-
- ::LAddColumn(1, 0, mMacListH);
- }
-
- void CEntryList::SetLookupDocument(CLookupDocument * inDocument)
- {
- Assert_(!mDocument);
- Assert_(inDocument);
-
- mDocument = inDocument;
- mDocument->AddListener(this);
- }
-
- UInt16 CEntryList::InsertRow(UInt16 row, UInt16 count)
- {
- return ::LAddRow(count, row, mMacListH);
- }
-
- void CEntryList::SetSelection(UInt16 row, Boolean select)
- {
- Cell cell;
- cell.h = 0;
- cell.v = 0;
-
- // Deselect all the preceeding cells:
- while(::LGetSelect(true, &cell, mMacListH))
- {
- if(cell.v >= row)
- break;
- ::LSetSelect(false, cell, mMacListH);
- }
-
- // Select our cell:
- cell.v = row;
- ::LSetSelect(select, cell, mMacListH);
-
- // Deselect all the following cells:
- if(::LNextCell(false, true, &cell, mMacListH))
- while(::LGetSelect(true, &cell, mMacListH))
- ::LSetSelect(false, cell, mMacListH);
- }
-
- void CEntryList::DeleteRow(Uint16 row, UInt16 count)
- {
- ::LDelRow(count, row, mMacListH);
- }
-
- void CEntryList::InvalidateRow(UInt16 row)
- {
- Cell cell;
- cell.h = 0;
- cell.v = row;
-
- if(::PtInRect(cell, &(**mMacListH).visible))
- {
- // Invalidate the cell's rectangle...
- Rect r;
- ::LRect(&r, cell, mMacListH);
- ::InvalRect(&r);
- }
- }
-
- void CEntryList::ListenToMessage(MessageT inMessage, void * ioParam)
- {
- switch(inMessage)
- {
- case msg_Reverted:
- {
- UInt16 recs = mDocument->GetEntryCount();
- UInt16 rows = GetRowCount();
-
- if(rows < recs)
- ::LAddRow(recs - rows, 0, mMacListH);
- else if(rows > recs)
- ::LDelRow(rows - recs, 0, mMacListH);
-
- if(rows != recs)
- Refresh();
-
- break;
- }
- case msg_EntryInserted:
- {
- EntryIndex idx = *(EntryIndex *)ioParam;
-
- idx = InsertRow(idx);
- SetSelection(idx, true);
- break;
- }
- case msg_EntryDeleted:
- {
- EntryIndex idx = *(EntryIndex *)ioParam;
-
- DeleteRow(idx);
- SetSelection(idx, false);
- break;
- }
- case msg_EntryChanged:
- {
- EntryIndex idx = *(EntryIndex *)ioParam;
-
- InvalidateRow(idx);
- SetSelection(idx, true);
- break;
- }
- }
- }
-
- UInt16 CEntryList::GetRowCount() const
- {
- return (**mMacListH).dataBounds.bottom;
- }
-
-
- const CEntry & CEntryList::GetEntryAt(Cell cell) const
- {
- Assert_(mDocument);
- return mDocument->GetEntryAt(cell.v);
- }
-
- void CEntryList::Register()
- {
- URegistrar::RegisterClass(kClassID, (ClassCreatorFunc)CreateFromStream);
- }
-
- CEntryList * CEntryList::CreateFromStream(LStream * inStream)
- {
- return new CEntryList(inStream);
- }
-
-
-
-
-
-