    001package org.vishia.gral.swt;
    002
    003import org.eclipse.swt.graphics.Rectangle;
    004import org.eclipse.swt.widgets.Composite;
    005import org.eclipse.swt.widgets.Control;
    006import org.eclipse.swt.widgets.Shell;
    007import org.vishia.gral.base.GralWidgImpl_ifc;
    008import org.vishia.gral.ifc.GralRectangle;
    009
    010/**This class wraps a SWT widget. In this form it is able to reference in the SWT-independent GRAL
    011 * @author Hartmut Schorrig
    012 *
    013 */
    014public class SwtWidgetSimpleWrapper implements GralWidgImpl_ifc
    015{
    016  protected Control widgetSwt;
    017
    018  protected final SwtMng mng;
    019
    020  public SwtWidgetSimpleWrapper(Control widgetSwt, SwtMng mng)
    021  { this.mng = mng;
    022    this.widgetSwt = widgetSwt;
    023  }
    024
    025
    026  @Override public void repaintGthread(){
    027    widgetSwt.redraw();
    028  }
    029
    030
    031  public void swtUpdateRedraw(){
    032    widgetSwt.update();
    033    widgetSwt.redraw();
    034  }
    035
    036
    037
    038  @Override public Object getWidgetImplementation()
    039  { return widgetSwt;
    040  }
    041
    042  @Override public boolean setFocusGThread(){ return widgetSwt.setFocus(); }
    043
    044  @Override public void setVisibleGThread(boolean bVisible) { widgetSwt.setVisible(bVisible); }
    045
    046
    047  @Override public void removeWidgetImplementation()
    048  {
    049    if(widgetSwt !=null){
    050      widgetSwt.dispose();
    051      widgetSwt = null;
    052    }
    053  }
    054
    055
    056  @Override public void setBoundsPixel(int x, int y, int dx, int dy)
    057  { widgetSwt.setBounds(x,y,dx,dy);
    058  }
    059
    060
    061  @Override public GralRectangle getPixelPositionSize(){
    062    int posx = 0, posy = 0;
    063    Rectangle r = widgetSwt.getBounds();
    064    Composite parent;
    065    if(widgetSwt instanceof Composite){
    066      parent = (Composite) widgetSwt; //start with them, maybe the shell itself
    067    } else {
    068      parent = widgetSwt.getParent();
    069    }
    070    Rectangle pos;
    071    while( !( parent instanceof Shell ) ){
    072      pos = parent.getBounds();
    073      posx += pos.x; posy += pos.y;
    074      parent = parent.getParent();
    075    }
    076    assert(parent instanceof Shell);
    077    Rectangle s = parent.getClientArea();
    078    pos = parent.getBounds();
    079    int dframe = (pos.width - s.width) /2;   //width of the frame line.
    080    posx += r.x + dframe;               //absolute position of the client area!
    081    posy += r.y + (pos.height - s.height) - dframe;
    082    int dx, dy;
    083    if(parent == widgetSwt){
    084      dx = s.width;
    085      dy = s.height;
    086    } else {
    087      dx = r.width;
    088      dy = r.height;
    089    }
    090    GralRectangle posSize = new GralRectangle(posx, posy, dx, dy);
    091    return posSize;
    092  }
    093
    094
    095
    096
    097
    098}