001package org.vishia.gral.swt;
002
003import org.eclipse.swt.events.PaintEvent;
004import org.eclipse.swt.events.PaintListener;
005import org.eclipse.swt.graphics.GC;
006import org.eclipse.swt.graphics.Image;
007import org.eclipse.swt.widgets.Canvas;
008import org.eclipse.swt.widgets.Composite;
009import org.vishia.gral.base.GralPanelContent;
010import org.vishia.gral.base.GralWidget;
011import org.vishia.gral.ifc.GralCanvasStorage;
012import org.vishia.gral.ifc.GralColor;
013import org.vishia.gral.widget.GralPlotArea;
014
015/**Implementation of GralPlotArea to SET
016 * @author Hartmut Schorrig
017 *
018 */
019public class SwtPlotArea extends GralPlotArea._GraphicImplAccess_
020{
021  /**Version, history and license.
022   * <ul>
023   * <li>2015-09-26 Hartmut creation.   
024   * </ul>
025   * <br><br>
026   * <b>Copyright/Copyleft</b>:
027   * For this source the LGPL Lesser General Public License,
028   * published by the Free Software Foundation is valid.
029   * It means:
030   * <ol>
031   * <li> You can use this source without any restriction for any desired purpose.
032   * <li> You can redistribute copies of this source to everybody.
033   * <li> Every user of this source, also the user of redistribute copies
034   *    with or without payment, must accept this license for further using.
035   * <li> But the LPGL is not appropriate for a whole software product,
036   *    if this source is only a part of them. It means, the user
037   *    must publish this part of source,
038   *    but don't need to publish the whole source of the own product.
039   * <li> You can study and modify (improve) this source
040   *    for own using or for redistribution, but you have to license the
041   *    modified sources likewise under this LGPL Lesser General Public License.
042   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
043   * </ol>
044   * If you are intent to use this sources without publishing its usage, you can get
045   * a second license subscribing a special contract with the author. 
046   * 
047   * @author Hartmut Schorrig = hartmut@vishia.org
048   * 
049   */
050  public static final String sVersion = "2015-09-26";
051
052  private final Canvas swtCanvas;
053  
054  private final SwtMng mng;
055  
056  protected SwtPlotArea(GralPlotArea gralPlotArea, SwtMng mng)
057  {
058    gralPlotArea.super(gralPlotArea);
059    this.mng = mng;
060    GralPanelContent panel = gralPlotArea.pos().panel;
061    Object swtPanel = panel._wdgImpl.getWidgetImplementation();
062    Composite panelSwt = (Composite) swtPanel; //mng.getCurrentPanel();
063    swtCanvas = new Canvas(panelSwt, 0);
064    swtCanvas.setBackground(mng.getColorImpl(GralColor.getColor("white")));
065    mng.setPosAndSizeSwt( gralPlotArea.pos(), swtCanvas, 800, 600);
066    swtCanvas.addPaintListener(paintListener);
067  }
068  
069  
070  private void paintRoutine(PaintEvent ev) {
071    GC g = ev.gc;  
072    for(GralCanvasStorage.PaintOrder order: super.canvasStore().paintOrders){
073        switch(order.paintWhat){
074          case GralCanvasStorage.paintLine: {
075            g.setForeground(mng.getColorImpl(order.color));
076            g.drawLine(order.x1, order.y1, order.x2, order.y2);
077          
078          } break;
079          case GralCanvasStorage.paintImage: {
080            GralCanvasStorage.PaintOrderImage orderImage = (GralCanvasStorage.PaintOrderImage) order;
081            Image image = (Image)orderImage.image.getImage();
082            //int dx1 = (int)(orderImage.zoom * order.x2);
083            //int dy1 = (int)(orderImage.zoom * order.y2);
084            g.drawImage(image, 0, 0, orderImage.dxImage, orderImage.dyImage, order.x1, order.y1, order.x2, order.y2);
085          } break;
086          case GralCanvasStorage.paintPolyline: {
087            if(order instanceof GralCanvasStorage.PolyLineFloatArray) {
088              GralCanvasStorage.PolyLineFloatArray line = (GralCanvasStorage.PolyLineFloatArray) order;
089              int[] points = ((GralCanvasStorage.PolyLineFloatArray) order).getImplStoreInt1Array();
090              g.setForeground(mng.getColorImpl(order.color));
091              g.drawPolyline(points);
092            } else {
093              GralCanvasStorage.PolyLine line = (GralCanvasStorage.PolyLine) order;
094              SwtCanvasStorePanel.SwtPolyLine swtLine;
095              { Object oImpl = line.getImplData();
096                if(oImpl == null){
097                  swtLine = new SwtCanvasStorePanel.SwtPolyLine(line, mng);
098                  line.setImplData(swtLine);
099                } else {
100                  swtLine = (SwtCanvasStorePanel.SwtPolyLine) oImpl;
101                }
102              }
103              g.drawPolyline(swtLine.points);
104            }
105          } break;
106          default: throw new IllegalArgumentException("unknown order");
107        }
108      }
109  
110  }
111  
112  
113  
114  @Override public void setVisibleGThread(boolean bVisible) { super.setVisibleState(bVisible); swtCanvas.setVisible(bVisible); }
115
116  
117  @SuppressWarnings("synthetic-access") 
118  PaintListener paintListener = new PaintListener(){
119    @Override public void paintControl(PaintEvent ev) {
120      SwtPlotArea.this.paintRoutine( ev);
121    }
122  };
123  
124
125}