home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Applications / Lookup 1.0d2 / Sources / CEntryList.cp < prev    next >
Encoding:
Text File  |  1995-09-12  |  4.6 KB  |  241 lines  |  [TEXT/CWIE]

  1. /*
  2.     CEntryList.cp
  3.     
  4.     Copyright © 1995 Alastair Rankine.
  5.     All Rights Reserved.
  6. */
  7.  
  8. #include "CEntryList.h"
  9. #include "CEntry.h"
  10. #include "CDeleteEntryAction.h"
  11. #include "MessageIDs.h"
  12. #include "CLookupDocument.h"
  13.  
  14. CEntryList::CEntryList(LStream * inStream)
  15.     : CListBox(inStream)
  16.     , mDocument(0)
  17. {
  18.     Assert_(mMacListH);
  19.  
  20.     (**mMacListH).selFlags = lOnlyOne;
  21. }
  22.  
  23. void CEntryList::LDEFDraw( Boolean lSelect, Rect *lRect, Cell lCell, short /*lDataOffset*/,
  24.     short /*lDataLen*/)
  25. {
  26.     ::EraseRect( lRect );
  27.     
  28.     const CEntry & rec = GetEntryAt(lCell);
  29.  
  30.     ::MoveTo( lRect->left+2, lRect->top + (**mMacListH).indent.v );
  31.     ::DrawString(rec.GetField(0).ToPString());
  32.  
  33.     if ( lSelect ) {
  34.         ::LMSetHiliteMode( LMGetHiliteMode() & ~(1 << hiliteBit) );
  35.         ::InvertRect( lRect );
  36.     }
  37. }
  38.  
  39. void CEntryList::LDEFHilite( Boolean lSelect, Rect *lRect, Cell lCell,
  40.     short /*lDataOffset*/, short /*lDataLen*/)
  41. {
  42.     ::LMSetHiliteMode( LMGetHiliteMode() & ~(1 << hiliteBit) );
  43.     ::InvertRect( lRect );
  44.     
  45.     if(lSelect) {
  46.         const CEntry * rec = &GetEntryAt(lCell);
  47.         BroadcastMessage(msg_CellSelected, &rec);
  48.     }
  49.     else
  50.         BroadcastMessage(msg_CellDeselected, nil);
  51. }
  52.  
  53. void CEntryList::LDEFInitialize()
  54. {
  55.     FontInfo    fInfo;
  56.     ::GetFontInfo( &fInfo );
  57.     (**mMacListH).indent.h = 2;
  58.     (**mMacListH).indent.v = ((**mMacListH).cellSize.v -
  59.         (fInfo.ascent+fInfo.descent)) / 2 + fInfo.ascent;
  60. }
  61.  
  62. Boolean CEntryList::ObeyCommand(CommandT inCommand, void * ioParam)
  63. {
  64.     Boolean commandHandled = false;
  65.     
  66.     switch(inCommand)
  67.     {
  68.         case cmd_Clear:
  69.             Assert_(mDocument);
  70.             PostAction(new CDeleteEntryAction(*mDocument, GetSelection()));
  71.             break;
  72.         default:
  73.             commandHandled = CListBox::ObeyCommand(inCommand, ioParam);
  74.             break;
  75.     }
  76.  
  77.     return commandHandled;
  78. }
  79.  
  80. void CEntryList::FindCommandStatus(CommandT inCommand, Boolean & outEnabled,
  81.     Boolean & outUsesMark, Char16 & outMark, Str255 outName)
  82. {
  83.     switch (inCommand)
  84.     {
  85.         case cmd_Clear:
  86.             outEnabled = (GetSelection() != kEmptySelection);
  87.             break;
  88.         default:
  89.             CListBox::FindCommandStatus(inCommand, outEnabled, outUsesMark, outMark, outName);
  90.             break;
  91.     }
  92. }
  93.  
  94. Int16 CEntryList::GetSelection() const
  95. {
  96.     Cell cell = {0, 0};
  97.     
  98.     if(::LGetSelect(true, &cell, mMacListH))
  99.         return cell.v;
  100.     else
  101.         return kEmptySelection;
  102. }
  103.  
  104. void CEntryList::FinishCreateSelf()
  105. {
  106.     CListBox::FinishCreateSelf();
  107.     
  108.     ::LAddColumn(1, 0, mMacListH);
  109. }
  110.  
  111. void CEntryList::SetLookupDocument(CLookupDocument * inDocument)
  112. {
  113.     Assert_(!mDocument);
  114.     Assert_(inDocument);
  115.     
  116.     mDocument = inDocument;
  117.     mDocument->AddListener(this);
  118. }
  119.  
  120. UInt16 CEntryList::InsertRow(UInt16 row, UInt16 count)
  121. {
  122.     return ::LAddRow(count, row, mMacListH);
  123. }
  124.  
  125. void CEntryList::SetSelection(UInt16 row, Boolean select)
  126. {
  127.     Cell cell;
  128.     cell.h = 0;
  129.     cell.v = 0;
  130.  
  131.     // Deselect all the preceeding cells:
  132.     while(::LGetSelect(true, &cell, mMacListH))
  133.     {
  134.         if(cell.v >= row)
  135.             break;
  136.         ::LSetSelect(false, cell, mMacListH);
  137.     }
  138.  
  139.     // Select our cell:
  140.     cell.v = row;
  141.     ::LSetSelect(select, cell, mMacListH);
  142.     
  143.     // Deselect all the following cells:
  144.     if(::LNextCell(false, true, &cell, mMacListH))
  145.         while(::LGetSelect(true, &cell, mMacListH))
  146.             ::LSetSelect(false, cell, mMacListH);
  147. }
  148.  
  149. void CEntryList::DeleteRow(Uint16 row, UInt16 count)
  150. {
  151.     ::LDelRow(count, row, mMacListH);
  152. }
  153.  
  154. void CEntryList::InvalidateRow(UInt16 row)
  155. {
  156.     Cell cell;
  157.     cell.h = 0;
  158.     cell.v = row;
  159.  
  160.     if(::PtInRect(cell, &(**mMacListH).visible))
  161.     {
  162.         // Invalidate the cell's rectangle...
  163.         Rect r;
  164.         ::LRect(&r, cell, mMacListH);
  165.         ::InvalRect(&r);
  166.     }
  167. }
  168.  
  169. void CEntryList::ListenToMessage(MessageT inMessage, void * ioParam)
  170. {
  171.     switch(inMessage)
  172.     {
  173.         case msg_Reverted:
  174.         {
  175.             UInt16 recs = mDocument->GetEntryCount();
  176.             UInt16 rows = GetRowCount();
  177.     
  178.             if(rows < recs)
  179.                 ::LAddRow(recs - rows, 0, mMacListH);
  180.             else if(rows > recs)
  181.                 ::LDelRow(rows - recs, 0, mMacListH);
  182.             
  183.             if(rows != recs)
  184.                 Refresh();
  185.             
  186.             break;
  187.         }
  188.         case msg_EntryInserted:
  189.         {
  190.             EntryIndex idx = *(EntryIndex *)ioParam;
  191.             
  192.             idx = InsertRow(idx);
  193.             SetSelection(idx, true);
  194.             break;
  195.         }
  196.         case msg_EntryDeleted:
  197.         {
  198.             EntryIndex idx = *(EntryIndex *)ioParam;
  199.             
  200.             DeleteRow(idx);
  201.             SetSelection(idx, false);
  202.             break;
  203.         }
  204.         case msg_EntryChanged:
  205.         {
  206.             EntryIndex idx = *(EntryIndex *)ioParam;
  207.             
  208.             InvalidateRow(idx);
  209.             SetSelection(idx, true);
  210.             break;
  211.         }
  212.     }
  213. }
  214.  
  215. UInt16 CEntryList::GetRowCount() const
  216. {
  217.     return (**mMacListH).dataBounds.bottom;
  218. }
  219.  
  220.  
  221. const CEntry & CEntryList::GetEntryAt(Cell cell) const
  222. {
  223.     Assert_(mDocument);
  224.     return mDocument->GetEntryAt(cell.v);
  225. }
  226.  
  227. void CEntryList::Register()
  228. {
  229.     URegistrar::RegisterClass(kClassID, (ClassCreatorFunc)CreateFromStream);
  230. }
  231.  
  232. CEntryList * CEntryList::CreateFromStream(LStream * inStream)
  233. {
  234.     return new CEntryList(inStream);
  235. }
  236.  
  237.  
  238.  
  239.  
  240.  
  241.