home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / Digest Browser 1.6 / Views / CSelectLine.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-30  |  5.0 KB  |  216 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2.     CSelectLine.c
  3.     
  4.     Methods for a text list with selectable lines.
  5.         
  6.     Copyright © 1989 Symantec Corporation. All rights reserved.
  7.     Copyright © 1991 Manuel A. Pérez.  All rights reserved.
  8.  
  9.  ******************************************************************************/
  10.  
  11.  
  12. #include "CSelectLine.h"
  13. #include <Commands.h>
  14. #include <CDocument.h>
  15. #include <CBartender.h>
  16. #include <Constants.h>
  17. #include "Global.h"
  18. #include <string.h>
  19.  
  20. /*** Global Variables ***/
  21. extern    CBartender    *gBartender;
  22. extern EventRecord  gLastMouseUp;
  23.  
  24. void CSelectLine::ISelectLine(CView *anEnclosure, CBureaucrat *aSupervisor,
  25.         short vLoc, short vHeight)
  26. {
  27. Rect    margin;
  28.  
  29.     selLine = -1;
  30.     IStyleText(anEnclosure, aSupervisor,
  31.         1, vHeight,                     // aWidth, aHeight
  32.         0, vLoc,                    // aHEncl, aVEncl
  33.         sizELASTIC, sizELASTIC,        // aHSizing, aVSizing
  34.         -1);                        // lineWidth
  35.     FitToEnclosure(TRUE, FALSE);        // fit horiz, and vert
  36.  
  37.         /**
  38.          **    Give the edit pane a little margin.
  39.          **    Each element of the margin rectangle
  40.          **    specifies by how much to change that
  41.          **    edge. Positive values are down and to
  42.          **    right, negative values are up and to
  43.          **    the left.
  44.          **
  45.          **/
  46.  
  47.     (*macTE)->crOnly = -1;            // do not wrap around
  48.     SetRect(&margin, 2, 2, -2, -2);
  49.     ChangeSize(&margin, FALSE);
  50.     Specify(false, true, false);    // edit, select, style
  51.     SetCanBeGopher(true);
  52.  
  53. }
  54.  
  55. /***
  56.  *
  57.  * FindEnds
  58.  *
  59.  ***/
  60.  
  61. void    CSelectLine::FindEnds(long line, long *lineStart, long *lineEnd)
  62. {
  63.     register TEPtr        t;
  64.     long                lineNum;
  65.     
  66.     t = *macTE;
  67.                 
  68.     if (line < 0)                        // avoid problems with negative line #
  69.         line = 0;
  70.  
  71.     *lineStart = t->lineStarts[line];
  72.     if (line > t->nLines)
  73.         *lineEnd = t->teLength;            // special case, past end
  74.     else
  75. //        *lineEnd = t->lineStarts[line+1];
  76. // JRB change - to get ScrollToSelection to work, lineEnd must point to
  77. //                last char of line, not first char of next line
  78.         *lineEnd = t->lineStarts[line+1]-1;
  79. }
  80.  
  81. /***
  82.  *
  83.  * DoClick {OVERRIDE}
  84.  *
  85.  *    Respond to a mouse click within the EditText
  86.  ***/
  87.  
  88. void    CSelectLine::DoClick(
  89.     Point        hitPt,                    /* Mouse location in Frame coords    */
  90.     short        modifierKeys,            /* State of modifier keys            */
  91.     long        when)                    /* Tick time of mouse click            */
  92. {     
  93. long offset;
  94. LongPt lHitPt;
  95.  
  96.     QDToLongPt(hitPt, &lHitPt);
  97.     SetSelectedLine(FindLine(GetCharOffset(&lHitPt)), true);
  98.     
  99.         // CSwitchboard will never see the mouse up that ended
  100.         // the drag, so we stuff gLastMouseUp here to allow
  101.         // multi-click counting to work.
  102.         
  103.     gLastMouseUp.what = mouseUp;
  104.     gLastMouseUp.when = TickCount();
  105.     gLastMouseUp.where = hitPt;
  106.     LocalToGlobal( &gLastMouseUp.where);
  107.     gLastMouseUp.modifiers = modifierKeys;
  108.  
  109. }
  110.  
  111. /***
  112.  *
  113.  * DoKeyDown {OVERRIDE}
  114.  *
  115.  *    Respond to a key down event
  116.  ***/
  117.  
  118. void CSelectLine::DoKeyDown(char theChar, Byte keyCode, EventRecord *macEvent)
  119. {
  120. // JRB addition - allow arrow keys to change selection
  121. // MAP modified to use keyCodes whenever possible.  Also checked
  122. // for invalid moves.  It was giving an error before.
  123.  
  124. long selectedLine;
  125.  
  126.     selectedLine = GetSelectedLine();
  127.  
  128.     switch (keyCode) {
  129.         case KeyHome:
  130.         case KeyPageUp:
  131.             SetSelectedLine(0, true);
  132.             ScrollToSelection();
  133.             break;
  134.              
  135.         case KeyPageDown:
  136.         case KeyEnd:
  137.             SetSelectedLine(GetNumLines()-1, true);
  138.             ScrollToSelection();
  139.             break;
  140.             
  141.         case KeyUpCursor:
  142.             if (selectedLine > 0)     {    
  143.                 SetSelectedLine(selectedLine - 1, true);
  144.                 ScrollToSelection();
  145.             }
  146.             break;
  147.  
  148.         case KeyDownCursor:
  149.             if (selectedLine < GetNumLines()-1) {
  150.                 SetSelectedLine(selectedLine+1, true);
  151.                 ScrollToSelection();
  152.             }
  153.             break;
  154.  
  155.         default:
  156.             if ((theChar == kEnterKey) || (theChar == '\r')) {
  157.                 SetSelectedLine(selectedLine+1, true);
  158.                 ScrollToSelection();
  159.             }
  160.  
  161.             // Move selection to the top
  162.             else if (theChar == '<')     {
  163.                 SetSelectedLine(0, true);
  164.                 ScrollToSelection();
  165.             }
  166.         
  167.             // Move selection to the bottom
  168.             else if (theChar == '>')     {
  169.                 SetSelectedLine(GetNumLines()-1, true);
  170.                 ScrollToSelection();
  171.             }
  172.             break;
  173.     }
  174. // end JRB addition
  175. }
  176.  
  177. /******************************************************************************
  178.  SetSelectedLine
  179.  
  180.      Sets the selected text to the range corresponding to character positions
  181.      selStart through selEnd.
  182. ******************************************************************************/
  183.  
  184. void CSelectLine::SetSelectedLine( long line, Boolean fRedraw)
  185. {
  186. long selStart, selEnd;
  187.  
  188.     FindEnds(line, &selStart, &selEnd);
  189.     SetSelection(selStart, selEnd, true);
  190.  
  191.     if (line != selLine) {
  192.         selLine = line;
  193.         if (fRedraw)
  194.             SelectionChanged();
  195.     }
  196. }    /* CSelectLine::SetSelection */
  197.  
  198. long CSelectLine::GetSelectedLine(void)
  199. {
  200.     return selLine;
  201. }
  202.  
  203. /******************************************************************************
  204.  AdjustCursor {OVERRIDE}
  205.  
  206.         Mouse is inside the CSelectLine area, set the cursor to the arrow
  207.  ******************************************************************************/
  208.  
  209. void    CSelectLine::AdjustCursor(
  210.     Point        where,                    /* Mouse location in Window coords    */
  211.     RgnHandle    mouseRgn)
  212. {
  213.     SetCursor(&arrow);                    /* Use the standard arrow cursor    */
  214. }
  215.  
  216.