Subversion Repositories wimsdev

Rev

Blame | Last modification | View Log | RSS feed

  1. package rene.viewer;
  2.  
  3. import java.awt.*;
  4.  
  5. public class Line
  6. {   TextDisplay TD;
  7.         boolean Chosen;
  8.         int Pos,Posend;
  9.         int Block; // block state
  10.         static final int NONE=0,START=1,END=2,FULL=4; // block states (logically or'ed)
  11.         Color C,IC; // Color of line and high key color of the line
  12.         char a[]; // Contains the characters of this line
  13.        
  14.         public Line (String s, TextDisplay td)
  15.         {       this(s,td,Color.black);
  16.         }
  17.        
  18.         /**
  19.          * Generate a line containing s in the textdisplay td.
  20.          * Display color is c.
  21.          * @param s
  22.          * @param td
  23.          * @param c
  24.          */
  25.         public Line (String s, TextDisplay td, Color c)
  26.     {   TD=td;
  27.         C=c;
  28.         // Create a color that is very bright, but resembles the line color
  29.         IC=new Color(C.getRed()/4+192,C.getGreen()/4+192,C.getBlue()/4+192);
  30.         Block=NONE;
  31.         a=s.toCharArray();
  32.     }
  33.    
  34.     public void expandTabs (int tabwidth)
  35.     {   int pos=0;
  36.         for (int i=0; i<a.length; i++)
  37.         {       pos++;
  38.                 if (a[i]=='\t') pos=(pos/tabwidth+1)*tabwidth;
  39.         }
  40.         char b[]=new char[pos];
  41.         pos=0;
  42.         for (int i=0; i<a.length; i++)
  43.         {       if (a[i]=='\t')
  44.                 {       int newpos=((pos+1)/tabwidth+1)*tabwidth;
  45.                         for (int k=pos; k<newpos; k++) b[k]=' ';
  46.                         pos=newpos;
  47.                 }
  48.                 else b[pos++]=a[i];
  49.         }
  50.         a=b;   
  51.     }
  52.     int length ()
  53.     {   return a.length;
  54.     }
  55.     int getpos (int x, int offset)
  56.     {   int l[]=TD.getwidth(a);
  57.         int h=offset-TD.Offset*TD.FM.charWidth(' ');
  58.         if (x<h) return 0;
  59.         int i=0;
  60.         while (x>h && i<a.length)
  61.         {       h+=l[i];
  62.                 i++;
  63.         }
  64.         return i;
  65.     }
  66.    
  67.     /**
  68.      * Draw a line.
  69.      * If the line is in a block, draw it white on black or on dark gray,
  70.      * depending on the focus.
  71.      * Drawing in blocks does not use antialias.
  72.          * @param g
  73.          * @param x
  74.          * @param y
  75.          */
  76.         public void draw (Graphics g, int x, int y)
  77.     {   int i1=0,i2,p=0;
  78.         x-=TD.Offset*TD.FM.charWidth(' ');
  79.         if (Chosen) // Complete line is chosen (in Lister)
  80.         {       // To see, if the display has the focus:
  81.                 if (TD.hasFocus()) g.setColor(Color.darkGray);
  82.                 else g.setColor(Color.gray);
  83.                 g.fillRect(0,y-TD.Ascent,TD.getSize().width,TD.Height);
  84.                 g.setColor(IC); // draw in light color
  85.                         TD.antialias(false);
  86.                 g.drawChars(a,0,a.length,x,y);
  87.                         TD.antialias(true);
  88.         }
  89.         else if ((Block&FULL)!=0) // Line in full block (in Viewer)
  90.         {       g.setColor(Color.darkGray);
  91.                 g.fillRect(x,y-TD.Ascent,TD.FM.charsWidth(a,0,a.length),TD.Height);
  92.                 g.setColor(Color.white);
  93.                 TD.antialias(false);
  94.                 g.drawChars(a,0,a.length,x,y);
  95.                 TD.antialias(true);
  96.         }
  97.         else if ((Block&START)!=0)
  98.         {       if (Pos>0) // Draw text before block
  99.                 {       g.setColor(C);
  100.                         g.drawChars(a,0,Pos,x,y);
  101.                         x+=TD.FM.charsWidth(a,0,Pos);
  102.                 }
  103.                 if ((Block&END)!=0) // draw text in block
  104.                 {       if (Posend>Pos)
  105.                         {       int h=TD.FM.charsWidth(a,Pos,Posend-Pos);
  106.                                 g.setColor(Color.darkGray);
  107.                                 g.fillRect(x,y-TD.Ascent,h,TD.Height);
  108.                                 g.setColor(Color.white);
  109.                                         TD.antialias(false);
  110.                                 g.drawChars(a,Pos,Posend-Pos,x,y);
  111.                                         TD.antialias(true);
  112.                                 g.setColor(C);
  113.                                 x+=h;
  114.                                 if (a.length>Posend)
  115.                                 {       g.drawChars(a,Posend,a.length-Posend,x,y);
  116.                                 }
  117.                         }
  118.                         else g.drawChars(a,Pos,a.length-Pos,x,y);
  119.                 }
  120.                 else // draw the rest of the line in block
  121.                 {       int h=TD.FM.charsWidth(a,Pos,a.length-Pos);
  122.                         g.setColor(Color.darkGray);
  123.                         g.fillRect(x,y-TD.Ascent,h,TD.Height);
  124.                         g.setColor(Color.white);
  125.                                 TD.antialias(false);
  126.                         g.drawChars(a,Pos,a.length-Pos,x,y);   
  127.                                 TD.antialias(true);
  128.                 }
  129.         }
  130.         else if ((Block&END)!=0)
  131.         {       int h=TD.FM.charsWidth(a,0,Posend);
  132.                 g.setColor(Color.darkGray);
  133.                         g.fillRect(x,y-TD.Ascent,h,TD.Height);
  134.                         g.setColor(Color.white);
  135.                         TD.antialias(false);
  136.                 g.drawChars(a,0,Posend,x,y);   
  137.                         TD.antialias(true);
  138.                 g.setColor(C);
  139.                 x+=h;
  140.                 if (a.length>Posend)
  141.                 {       g.drawChars(a,Posend,a.length-Posend,x,y);
  142.                 }
  143.         }
  144.         else
  145.         {       g.setColor(C);
  146.                 g.drawChars(a,0,a.length,x,y);
  147.         }
  148.     }
  149.     void append (String s)
  150.     {   a=(new String(a)+s).toCharArray();
  151.     }
  152.     void chosen (boolean f)
  153.     {   Chosen=f;
  154.     }
  155.     public boolean chosen () { return Chosen; }
  156.     void block (int pos, int mode)
  157.     {   switch (mode)
  158.         {       case NONE : Block=NONE; break;
  159.                 case FULL : Block=FULL; break;
  160.                 case START :
  161.                         Block|=START; Pos=pos; break;
  162.                 case END :
  163.                         Block|=END; Posend=pos; break;
  164.         }
  165.     }
  166.     String getblock ()
  167.     {   if (Block==FULL)
  168.         {       return new String(a,0,a.length);
  169.         }
  170.         else if ((Block&START)!=0)
  171.         {       if ((Block&END)!=0)
  172.                 {       return new String(a,Pos,Posend-Pos);
  173.                 }
  174.                 else return new String(a,Pos,a.length-Pos);
  175.         }
  176.         else if ((Block&END)!=0) return new String(a,0,Posend);
  177.         else return "";
  178.     }
  179. }
  180.