    001package org.vishia.gral.swt;
    002
    003
    004import org.vishia.gral.base.GralMng;
    005import org.vishia.gral.base.GralPanelContent;
    006import org.vishia.gral.ifc.GralCanvasStorage;
    007import org.vishia.gral.ifc.GralColor;
    008import org.vishia.gral.ifc.GralPoint;
    009import org.vishia.gral.ifc.GralRectangle;
    010import org.eclipse.swt.events.PaintEvent;
    011import org.eclipse.swt.events.PaintListener;
    012import org.eclipse.swt.graphics.Color;
    013import org.eclipse.swt.graphics.GC;
    014import org.eclipse.swt.graphics.Image;
    015import org.eclipse.swt.widgets.Canvas;
    016import org.eclipse.swt.widgets.Composite;
    017import org.eclipse.swt.widgets.Control;
    018
    019/**Class to store some graphical figures to draw it in its {@link SwtCanvas#drawBackground(GC, int, int, int, int)}-routine.
    020 * The figures are stored with its coordinates and it are drawn if necessary.
    021 * <br><br>
    022 * It can contain some GUI-Elements like Button, Text, Label, Table etc from org.eclipse.swt.widgets.
    023 * The graphical figures are shown as background than.
    024 *
    025 * @author Hartmut Schorrig
    026 *
    027 */
    028public class SwtCanvasStorePanel extends SwtPanel  //CanvasStorePanel //
    029{
    030
    031  protected SwtCanvas swtCanvas;
    032
    033        /**The storage for the Canvas content. */
    034        //GralCanvasStorage store = new GralCanvasStorage();
    035
    036
    037
    038
    039        //class MyCanvas extends Canvas{
    040
    041        private static final long serialVersionUID = 6448419343757106982L;
    042
    043  protected Color currColor;
    044
    045  /**Constructs the instance with a SWT-Canvas Panel.
    046   * @param parent
    047   * @param style
    048   * @param backGround
    049   */
    050  public SwtCanvasStorePanel(GralPanelContent panelg, Composite parent, int style, Color backGround, GralMng gralMng)
    051  { super(panelg, null);
    052    gralPanel().canvas = new GralCanvasStorage();
    053    swtCanvas = new SwtCanvas(this,parent, style);
    054    super.panelComposite = swtCanvas;
    055    swtCanvas.addControlListener(resizeItemListener);
    056    swtCanvas.setData(this);
    057    swtCanvas.setLayout(null);
    058    currColor = swtCanvas.getForeground();
    059    swtCanvas.addPaintListener(swtCanvas.paintListener);
    060    swtCanvas.setBackground(backGround);
    061  }
    062
    063  /**Constructor called in derived classes. The derived class have to be instantiate the Canvas
    064   * maybe with other draw routines.
    065   */
    066  protected SwtCanvasStorePanel(GralPanelContent panelg)
    067  {
    068    super(panelg, null);
    069    gralPanel().canvas = new GralCanvasStorage();
    070  }
    071
    072
    073  public void xxxsetForeground(Color color){
    074    currColor = color;
    075        }
    076
    077
    078
    079        /**Implementation class for Canvas for Swt
    080         * This class is a org.eclipse.swt.widgets.Composite.
    081         */
    082        protected static class SwtCanvas extends Canvas
    083        {
    084          private final SwtCanvasStorePanel storeMng;
    085
    086          SwtCanvas(SwtCanvasStorePanel storeMng, Composite parent, int style)
    087          {
    088            super(parent, style);
    089            this.storeMng = storeMng;
    090          }
    091
    092    @Override
    093    public void drawBackground(GC g, int x, int y, int dx, int dy) {
    094        //NOTE: forces stack overflow because calling of this routine recursively: super.paint(g);
    095
    096      if(storeMng.gralPanel().canvas == null){
    097        stop();
    098      } else
    099        for(GralCanvasStorage.PaintOrder order: storeMng.gralPanel().canvas.paintOrders){
    100                switch(order.paintWhat){
    101                case GralCanvasStorage.paintLine: {
    102                        g.setForeground(((SwtMng)storeMng.gralPanel().gralMng().impl).getColorImpl(order.color));
    103                g.drawLine(order.x1, order.y1, order.x2, order.y2);
    104
    105          } break;
    106                case GralCanvasStorage.paintImage: {
    107                  GralCanvasStorage.PaintOrderImage orderImage = (GralCanvasStorage.PaintOrderImage) order;
    108                  Image image = (Image)orderImage.image.getImage();
    109                  //int dx1 = (int)(orderImage.zoom * order.x2);
    110                  //int dy1 = (int)(orderImage.zoom * order.y2);
    111            g.drawImage(image, 0, 0, orderImage.dxImage, orderImage.dyImage, order.x1, order.y1, order.x2, order.y2);
    112                } break;
    113                case GralCanvasStorage.paintPolyline: {
    114                  GralCanvasStorage.PolyLine line = (GralCanvasStorage.PolyLine) order;
    115                  SwtPolyLine swtLine;
    116            { Object oImpl = line.getImplData();
    117                          if(oImpl == null){
    118                swtLine = new SwtPolyLine(line, ((SwtMng)storeMng.gralPanel().gralMng().impl));
    119                line.setImplData(swtLine);
    120              } else {
    121                swtLine = (SwtPolyLine) oImpl;
    122              }
    123                  }
    124            g.drawPolyline(swtLine.points);
    125                } break;
    126                default: throw new IllegalArgumentException("unknown order");
    127                }
    128        }
    129    }
    130
    131    /**The listener for paint events. It is called whenever the window is shown newly. */
    132    protected PaintListener paintListener = new PaintListener()
    133    {
    134
    135      @Override
    136      public void paintControl(PaintEvent e) {
    137        // TODO Auto-generated method stub
    138        GC gc = e.gc;
    139        drawBackground(e.gc, e.x, e.y, e.width, e.height);
    140        //stop();
    141      }
    142
    143    };
    144
    145    void stop(){}
    146
    147        }
    148
    149  @Override public Control getWidgetImplementation(){ return swtCanvas; }
    150
    151  @Override public boolean setFocusGThread()
    152  {
    153    if(!super.setFocusGThread()){
    154      return swtCanvas.setFocus();
    155    } else return true;
    156  }
    157
    158
    159  void stop(){} //debug
    160
    161  public static class SwtPolyLine // extends GralCanvasStorage.PolyLine
    162  {
    163    int[] points;
    164    int nrofPoints;
    165    Color color;
    166
    167    SwtPolyLine(GralCanvasStorage.PolyLine line, SwtMng gralMng){
    168      nrofPoints = line.points.size();
    169      points = new int[2 * nrofPoints];
    170      GralRectangle rr = line.pos.calcWidgetPosAndSize(gralMng.mng.propertiesGui, 0, 0, 800, 600);
    171      int ix = -1;
    172      int xf, yf;
    173      if(line.bPointsAreGralPosUnits){
    174        xf = gralMng.mng.propertiesGui.xPixelUnit();  //1.0 is one GralPos unit
    175        yf = gralMng.mng.propertiesGui.xPixelUnit();
    176      } else {
    177        xf = rr.dx;  //0.0..1.0 is size of line.pos
    178        yf = rr.dy;
    179      }
    180      for(GralPoint point: line.points){
    181        int x = rr.x + (int)(point.x * xf + 0.5f);
    182        int y = rr.y - (int)(point.y * xf + 0.5f);
    183        points[++ix] = x;
    184        points[++ix] = y;
    185      }
    186      color = gralMng.getColorImpl(line.color);
    187    }
    188  }
    189
    190
    191}
    192