001package org.vishia.gral.swt;
002
003import java.util.List;
004
005import org.eclipse.swt.events.MouseEvent;
006import org.eclipse.swt.events.MouseListener;
007import org.eclipse.swt.events.PaintEvent;
008import org.eclipse.swt.events.PaintListener;
009import org.eclipse.swt.graphics.Color;
010import org.eclipse.swt.graphics.Font;
011import org.eclipse.swt.graphics.GC;
012import org.eclipse.swt.graphics.Point;
013import org.eclipse.swt.graphics.Rectangle;
014import org.eclipse.swt.widgets.Canvas;
015import org.eclipse.swt.widgets.Composite;
016import org.eclipse.swt.widgets.Control;
017import org.vishia.gral.base.GralWidgImpl_ifc;
018import org.vishia.gral.ifc.GralColor;
019import org.vishia.gral.ifc.GralRectangle;
020import org.vishia.gral.widget.GralHorizontalSelector;
021
022/**This class is a selector in one line. You can set the cursor into the field.
023 * @author Hartmut Schorrig
024 *
025 */
026public class SwtHorizontalSelector extends GralHorizontalSelector.GraphicImplAccess implements GralWidgImpl_ifc
027{
028  /**Version, history and copyright/copyleft.
029   * <ul>
030   * <li>2013-06-18 Hartmut created, new idea.
031   * </ul>
032   * 
033   * <b>Copyright/Copyleft</b>:<br>
034   * For this source the LGPL Lesser General Public License,
035   * published by the Free Software Foundation is valid.
036   * It means:
037   * <ol>
038   * <li> You can use this source without any restriction for any desired purpose.
039   * <li> You can redistribute copies of this source to everybody.
040   * <li> Every user of this source, also the user of redistribute copies
041   *    with or without payment, must accept this license for further using.
042   * <li> But the LPGL is not appropriate for a whole software product,
043   *    if this source is only a part of them. It means, the user
044   *    must publish this part of source,
045   *    but doesn't need to publish the whole source of the own product.
046   * <li> You can study and modify (improve) this source
047   *    for own using or for redistribution, but you have to license the
048   *    modified sources likewise under this LGPL Lesser General Public License.
049   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
050   * </ol>
051   * If you intent to use this source without publishing its usage, you can get
052   * a second license subscribing a special contract with the author. 
053   * 
054   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
055   */
056  //@SuppressWarnings("hiding")
057  public static final int version = 20130618;
058
059  //private final GralHorizontalSelector<?> wdgGral;
060  
061  //public final GralWidgetAccess wdgGralAccess;
062  
063  
064  /**It contains the association to the swt widget (Control) and the {@link SwtMng}
065   * and implements some methods of {@link GralWidgImpl_ifc} which are delegate from this.
066   */
067  private final SwtWidgetHelper swtWdgW;
068
069  
070  //protected Canvas widgetSwt;
071
072  //protected final SwtMng mng;
073  
074
075  
076  private Font fontText;
077
078  
079  
080  public SwtHorizontalSelector(SwtMng mng, GralHorizontalSelector<?> wdgGral)
081  { super(wdgGral, mng.mng);  //Invoke constructor of the super class, with knowledge of its outer class.
082    //this.mng = mng;
083    Composite panel = (Composite)outer.pos().panel.getWidgetImplementation();
084    //widgetSwt = new Canvas(panel,0);
085    super.wdgimpl = this.swtWdgW = new SwtWidgetHelper(new Canvas(panel,0), mng);
086    swtWdgW.widgetSwt.setData(wdgGral);
087    swtWdgW.widgetSwt.addPaintListener(paintListener);
088    swtWdgW.widgetSwt.addMouseListener(mouseListener);
089    mng.setBounds_(wdgGral.pos(), swtWdgW.widgetSwt);
090    float ySize = outer.pos().height();
091    char size1 = ySize > 3? 'B' : 'A';
092    switch(size1){ 
093      case 'A': fontText = mng.propertiesGuiSwt.stdInputFont; break;
094      case 'B': fontText = mng.propertiesGuiSwt.stdButtonFont; break;
095      default: throw new IllegalArgumentException("param size must be A or B");
096    }
097    super.execAfterCreationImplWidget();
098
099  }
100
101  
102  
103  @Override public void repaintGthread(){ swtWdgW.swtUpdateRedraw(); }
104
105  
106  @Override public Object getWidgetImplementation(){ return swtWdgW.widgetSwt; }
107  
108  @Override public boolean setFocusGThread(){ return swtWdgW.setFocusGThread(); }
109
110  @Override public void setVisibleGThread(boolean bVisible) { super.setVisibleState(bVisible); swtWdgW.setVisibleGThread(bVisible); }
111  
112  @Override public void removeWidgetImplementation(){ swtWdgW.removeWidgetImplementation(); }
113
114  @Override public void setBoundsPixel(int x, int y, int dx, int dy){ swtWdgW.setBoundsPixel(x, y, dx, dy); }
115
116  
117  @Override public GralRectangle getPixelPositionSize(){ return swtWdgW.getPixelPositionSize(); }
118
119  
120  //@Override public boolean setVisible(boolean visible){ return swtWidgWrapper.setVisible(visible); }
121  
122  
123
124  
125  
126  @SuppressWarnings("unchecked")
127  protected void paintControl(Canvas swt, PaintEvent e){
128    GC gc = e.gc;
129    //gc.d
130    Rectangle dim = swt.getBounds();
131    GralHorizontalSelector.Item<?> actItem = super.actItem();
132    int nrActItem = super.nrItem();
133    Color swtColorBack = swtWdgW.mng.getColorImpl(super.outer.colorBack);
134    Color swtColorText = swtWdgW.mng.getColorImpl(super.outer.colorText);
135    Color swtColorSelect = swtWdgW.mng.getColorImpl(super.outer.colorSelect);
136    gc.setBackground(swtColorBack);
137    swt.drawBackground(e.gc, dim.x+1, dim.y+1, dim.width-1, dim.height-1);
138    gc.setFont(fontText);
139    //FontData fontData = mng.propertiesGui.stdButtonFont.getFontData();
140    //fontData.
141    gc.setForeground(swtColorBack);
142    gc.fillRectangle(1,0,dim.width-1, dim.height);
143    int xArrow = 20;
144    super.calcLeftTab(dim.width, xArrow);
145    int xText = 2;
146    int yText = 0;
147    int ixLeftItem = super.nrLeftTab();
148    if(ixLeftItem >0){
149      gc.setForeground(swtColorText);
150      gc.drawString("<<", xText+4, yText); 
151      xText += 20;
152    }
153    //
154    //paint tabs
155    //
156    if(ixLeftItem >=0){
157      int ixItem = ixLeftItem;
158      int zItem = super.nrofTabs();
159      if(ixItem >= zItem){ 
160        ixItem = zItem -1; 
161      }
162      do {
163        GralHorizontalSelector.Item item = super.tab(ixItem); 
164        if(item.xSize == 0){
165          Point size = gc.stringExtent(item.text);
166          if(size.x < 150 - 10){ item.xSize = size.x + 10;}
167          else { item.xSize = 150; }
168        }
169        int xEnd = xText + item.xSize;
170        if(xEnd < (dim.width - (ixItem == (zItem-1) ? xArrow +4 : 4))){
171          if(ixItem == nrActItem){
172            gc.setForeground(swtColorSelect);  //black
173            gc.setLineWidth(2);
174          } else {
175            gc.setForeground(swtColorText);
176            gc.setLineWidth(1);
177          }
178          gc.drawString(item.text, xText+4, yText);
179          gc.drawLine(xText+1, 3, xText+1, dim.height);
180          gc.drawLine(xText+1, 3, xText+4, 0);
181          gc.drawLine(xEnd-1, 3, xEnd-1, dim.height);
182          gc.drawLine(xEnd-1, 3, xEnd-4, 0);
183          gc.drawLine(xEnd-4, 0, xEnd-item.xSize+4, 0);
184          xText = xEnd;
185          ixItem +=1;
186        } else break;
187      } while(ixItem < zItem);
188      if(ixItem < zItem){
189        gc.setForeground(swtColorText);
190        gc.drawString(">>", dim.width-16, yText); 
191      }
192    }    
193  }
194
195  
196  
197  protected void mouseDown(MouseEvent e)
198  {
199    super.findTab(e.x);
200    swtWdgW.widgetSwt.redraw();
201    
202  }
203
204  
205  public void mouseUp(MouseEvent e)
206  {
207    Rectangle dim = swtWdgW.widgetSwt.getBounds();
208    if(e.button == 1){
209      if(e.y >0 && e.y < dim.height){
210        super.setDstToActItem();
211      } else {
212        super.clearDstItem();
213      }
214      swtWdgW.widgetSwt.redraw();  //because selection is changed.
215    }
216    //else: content menu does the action.
217  }
218
219  
220  
221  PaintListener paintListener = new PaintListener(){
222    @Override public void paintControl(PaintEvent e) {
223      SwtHorizontalSelector.this.paintControl((Canvas)swtWdgW.widgetSwt, e);
224    }
225  };
226  
227  
228  
229  MouseListener mouseListener = new MouseListener(){
230
231    @Override
232    public void mouseDoubleClick(MouseEvent e)
233    {
234      // TODO Auto-generated method stub
235      
236    }
237
238    @Override
239    public void mouseDown(MouseEvent e)
240    { SwtHorizontalSelector.this.mouseDown(e);
241    }
242
243    @Override
244    public void mouseUp(MouseEvent e)
245    { SwtHorizontalSelector.this.mouseUp(e);
246    }
247    
248  };
249
250  
251  
252
253  
254}