home *** CD-ROM | disk | FTP | other *** search
/ Dream 57 / Amiga_Dream_57.iso / Amiga / Programmation / c / Extensions / APPSource.lha / APlusPlus / libsource / TextView.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-29  |  7.0 KB  |  260 lines

  1. /******************************************************************************
  2.  **
  3.  **   C++ Class Library for the Amiga⌐ system software.
  4.  **
  5.  **   Copyright (C) 1994 by Armin Vogt  **  EMail: armin@uni-paderborn.de
  6.  **   All Rights Reserved.
  7.  **
  8.  **   $Source: apphome:RCS/libsource/TextView.cxx,v $
  9.  **   $Revision: 1.10 $
  10.  **   $Date: 1994/07/27 11:52:18 $
  11.  **   $Author: Armin_Vogt $
  12.  **
  13.  ******************************************************************************/
  14.  
  15.  
  16. extern "C" {
  17. #ifdef __GNUG__
  18. #include <inline/graphics.h>
  19. #endif
  20.  
  21. #ifdef __SASC
  22. #include <proto/graphics.h>
  23. #endif
  24. }
  25.  
  26. #include <APlusPlus/graphics/TextView.h>
  27. #include <APlusPlus/intuition/IntuiMessageC.h>
  28.  
  29.  
  30. static const char rcs_id[] = "$Id: TextView.cxx,v 1.10 1994/07/27 11:52:18 Armin_Vogt Exp Armin_Vogt $";
  31.  
  32. //runtime type inquiry support
  33. intui_typeinfo(TextView, derived(from(Canvas)), rcs_id)
  34.  
  35.  
  36. TextView::TextView(GOB_OWNER, AttrList& attrs)
  37.    : Canvas(gob_owner,attrs), font((UBYTE*)intuiAttrs().getTagData(TXV_FontName),
  38.             (UWORD)intuiAttrs().getTagData(TXV_FontSize))
  39. {
  40.    if (Ok())
  41.    {
  42.       Canvas::setAttributes( AttrList(CNV_GranularityY,font.ySize(),
  43.             CNV_ScrollGratX,FALSE,CNV_ScrollGratY,TRUE,TAG_END) );
  44.       setFont(font);
  45.       crsrx = intuiAttrs().getTagData(TXV_CursorX,1);
  46.       crsrx = max(crsrx,1);
  47.       crsry = intuiAttrs().getTagData(TXV_CursorY,1);
  48.       crsry = max(crsry,1);
  49.       lineCount = intuiAttrs().getTagData(TXV_Lines,0);
  50.       crsrOn = (UBYTE)intuiAttrs().getTagData(TXV_CursorOn,FALSE);
  51.       cBoxLeft = cBoxRight = 0;
  52.       cviewx = 1; cviewy = 1;
  53.       setIOType(IOTYPE_TEXTVIEW);
  54.    }
  55. }
  56.  
  57. TextView::~TextView()
  58. {
  59.  
  60. }
  61.  
  62. ULONG TextView::setAttributes(AttrList& attrs)
  63. {
  64.    Canvas::setAttributes(attrs);
  65.  
  66.    AttrIterator next(attrs);
  67.    while (next())
  68.    {
  69.       switch (next.tag())
  70.       {
  71.          case TXV_CursorY : setCursor(next.data(),-1); break;
  72.          case TXV_CursorX : setCursor(-1,next.data()); break;
  73.          case TXV_Lines : lineCount = next.data(); break;
  74.          case TXV_CursorOn : crsrOn = (UBYTE)next.data(); break;
  75.       }
  76.    }
  77.    return 1L;
  78. }
  79.  
  80. ULONG TextView::getAttribute(Tag tag,ULONG& dataStore)
  81. {
  82.    return Canvas::getAttribute(tag,dataStore);
  83. }
  84.  
  85. void TextView::writeLine(LONG line)
  86. {
  87.    setStdClip();
  88.  
  89.    moveTx((line-1)*granularityY(),0);
  90.    UWORD len;
  91.    UBYTE *str = getLineString(line,len);
  92.    formatOutput(str,len);
  93.  
  94.    resetStdClip();
  95. }
  96.  
  97. void TextView::print(UBYTE *partString,UWORD length)
  98. {
  99.    text(partString,length);
  100. }
  101.  
  102. void TextView::formatOutput(UBYTE *textString, UWORD length)
  103. {
  104.    setAPen(1);setDrMd(JAM1);
  105.    text(textString,length);
  106. }
  107.  
  108. void TextView::drawSelf()
  109. {
  110.    _dprintf( ("TextView::drawSelf(viewY=%ld, visibleY=%ld, granularityY=%ld\n",viewY(),visibleY(),granularityY()) );
  111.    for (LONG ll=1,line=viewY()+1; ll<=visibleY() && line <= lines(); ll++,line++)
  112.    {
  113.       moveTx(0,(line-1)*granularityY());
  114.       UWORD len;
  115.       UBYTE *str = getLineString(line,len);
  116.       formatOutput(str,len);
  117.    }
  118.    // prevent erasing a cursor that is already drawn over.
  119.    cBoxRight = 0;
  120. }
  121.  
  122. void TextView::callback(const IntuiMessageC *imsg)
  123. {
  124.    Canvas::callback(imsg);
  125.  
  126.    switch (imsg->getClass())
  127.    {
  128.       case CLASS_MOUSEBUTTONS :
  129.          _dprintf("TV::MOUSEBUTTONS\n");
  130.          break;
  131.       case CLASS_MOUSEMOVE :
  132.          _dprintf("TV::MOUSEMOVE\n");
  133.          break;
  134.       case CLASS_GADGETDOWN :
  135.          _dprintf("TV:GADGETDOWN\n");
  136.          findCursor();
  137.          break;
  138.       case CLASS_GADGETUP :
  139.          _dprintf("TV:GADGETUP\n");
  140.          if (!forceActiveGadget(imsg))
  141.          {
  142.             _dprintf(" - loose active status..\n");
  143.             eraseCBox();
  144.          }
  145.          break;
  146.       case CLASS_VANILLAKEY :
  147.          break;
  148.       case CLASS_RAWKEY :
  149.          /* subclasses may already have decoded the rawkey into 'key'
  150.           * TextView is the last class to process the keycodes,
  151.           * therefore it clears the 'key' object after processing
  152.           */
  153.          if (key.isEmpty()) key.decode(imsg);
  154.          switch (key.key())
  155.          {
  156.             case CURSOR_UP :
  157.                setCursor(cursorY()-1,-1);
  158.                break;
  159.             case CURSOR_DOWN :
  160.                setCursor(cursorY()+1,-1);
  161.                break;
  162.             case CURSOR_RIGHT :
  163.                setCursor(-1,cursorX()+1);
  164.                break;
  165.             case CURSOR_LEFT :
  166.                setCursor(-1,cursorX()-1);
  167.                break;
  168.          }
  169.          key.clear();   // clear for next RAWKEY message
  170.          break;
  171.    }
  172. }
  173.  
  174. BOOL TextView::setCursor(LONG lineNr,LONG characterNr)
  175. {
  176.    if (characterNr == -1) characterNr = crsrx;
  177.    if (lineNr == -1) lineNr = crsry;
  178.  
  179.    if (lineNr >= 1 && lineNr <= lines() && characterNr >= 1)
  180.    {
  181.       eraseCBox();
  182.       // if new pos outside view move view so that cursor remains at his place within view
  183.       if (lineNr < topLine())
  184.       {
  185.          cviewy = crsry-topLine()+1;
  186.          if (lineNr < cviewy) cviewy = lineNr;
  187.          Canvas::setAttributes(AttrList(CNV_ViewY,lineNr-cviewy,TAG_END));
  188.       }
  189.       else if (lineNr > topLine()+visibleLines()-1)
  190.       {
  191.          cviewy = crsry-topLine()+1;
  192.          if (lines()-lineNr < visibleLines()-cviewy) cviewy = visibleLines()-(lines()-lineNr);
  193.          Canvas::setAttributes(AttrList(CNV_ViewY,lineNr-cviewy,TAG_END));
  194.       }
  195.       else cviewy = lineNr-topLine()+1;
  196.  
  197.       // if new
  198.       UWORD length;
  199.       UBYTE *lineString = getLineString(lineNr,length);
  200.       crsrx = min(characterNr,length); crsry = lineNr;
  201.  
  202.       XYVAL cpos = cBoxLeft/granularityX()-viewX();
  203.       cBoxRight = TextLength(rp(),(STRPTR)lineString,crsrx);
  204.       cBoxLeft = cBoxRight - TextLength(rp(),(STRPTR)&lineString[crsrx-1],1);
  205.  
  206.       XYVAL newViewX = cBoxLeft/granularityX();
  207.       XYVAL xOffset = viewX()*granularityX();
  208.       if (cBoxLeft < xOffset || cBoxRight > xOffset+visibleX()*granularityX())
  209.       {
  210.          if (newViewX <= cpos)
  211.          {
  212.             newViewX = 0;
  213.          }
  214.          else
  215.             newViewX -= cpos;
  216.          Canvas::setAttributes(AttrList(CNV_ViewX,newViewX,TAG_END));
  217.          cviewx = newViewX;
  218.       }
  219.  
  220.       drawCBox();
  221.       return TRUE;
  222.    }
  223.    else return TRUE;
  224. }
  225.  
  226. void TextView::findCursor()
  227. {
  228.    crsry = topLine()+cviewy-1;
  229.    // find character nearest to the cursor position relative to view boundaries
  230.    struct TextExtent textExtent;
  231.    UWORD length;
  232.    UBYTE *lineString = getLineString(crsry,length);
  233.    crsrx = (LONG)1+TextFit(rp(),(STRPTR)lineString,length,&textExtent,NULL,1,
  234.             (LONG)cBoxLeft+(-((LONG)cviewx)+viewX())*granularityX(),(LONG)font.ySize()+1);
  235.  
  236.    setCursor(crsry,crsrx);
  237. }
  238. /*
  239. void TextView::cursorPos(UWORD mx,UWORD my)
  240. {
  241.    struct TextExtent textExtent;
  242.    crsry = my+1;
  243.  
  244.    UWORD length;
  245.    UBYTE *lineString = getLineString(crsry,length);
  246.    cColumn = TextFit(rp(),lineString,length,&textExtent,NULL,1,mx,font.ySize()+1);
  247.  
  248.    if (cColumn == length){}
  249. }
  250. */
  251. void TextView::toggleCBox()
  252. {
  253.    // if cursor is on, erase cbox
  254.    adjustStdClip();
  255.    setStdClip();
  256.    setDrMd(COMPLEMENT);
  257.    rectFill(cBoxLeft,(crsry-1)*granularityY(),cBoxRight,crsry*granularityY());
  258.    resetStdClip();
  259. }
  260.