001package org.vishia.gral.swt;
002
003import java.util.LinkedList;
004import java.util.List;
005
006import org.eclipse.swt.graphics.Color;
007import org.eclipse.swt.graphics.Point;
008import org.eclipse.swt.graphics.Rectangle;
009import org.eclipse.swt.widgets.Composite;
010import org.eclipse.swt.widgets.Control;
011import org.eclipse.swt.widgets.Menu;
012import org.eclipse.swt.widgets.Scrollable;
013import org.eclipse.swt.widgets.Shell;
014import org.eclipse.swt.widgets.TabFolder;
015import org.eclipse.swt.widgets.TabItem;
016import org.vishia.gral.base.GralWidget;
017import org.vishia.gral.base.GralWidgetHelper;
018import org.vishia.gral.base.GralMenu;
019import org.vishia.gral.base.GralMng;
020import org.vishia.gral.ifc.GralColor;
021import org.vishia.gral.ifc.GralRectangle;
022import org.vishia.gral.impl_ifc.GralWidgetImpl_ifc;
023import org.vishia.util.Debugutil;
024
025/**The static methods of this class are called in some situations, where same functionality is need in some classes.
026 * @author Hartmut Schorrig
027 *
028 */
029public class SwtWidgetHelper implements GralWidgetImpl_ifc
030{
031
032  /**Version and history
033   * <ul>
034   * <li>2011-11-18 Hartmut chg: {@link #setFocusOfTabSwt(Control)} is the implementation of all 
035   *   {@link org.vishia.gral.base.GralWidget#setFocusGThread()} implementations. It regards that a tab in a TabFolder
036   *   should be selected if any widget inside the tab-panel is focused. Used extensive in JavaCommander.
037   * </ul>
038   * 
039   */
040  public final static int version = 0x20111119;
041  
042  
043  private static SwtMng mngs;
044  
045  public final SwtMng mng;
046  
047  public Control widgetSwt;
048
049
050
051  public SwtWidgetHelper(Control widgetSwt, SwtMng mng)
052  { if(SwtWidgetHelper.mngs !=null){ assert(SwtWidgetHelper.mngs == mng); }
053    else {SwtWidgetHelper.mngs = mng; }
054    this.widgetSwt = widgetSwt;
055    this.mng = mng;
056  }
057
058
059
060
061  public static GralColor getColor(Color swtColor)
062  {
063    int colorValue = swtColor.getBlue() << 16 + swtColor.getGreen() << 8 + swtColor.getRed();
064    return GralColor.getColor(colorValue);
065  }
066  
067  
068  public static Color getColor(GralColor color){ return mngs.propertiesGuiSwt.colorSwt(color); }
069  
070  public static GralColor setBackgroundColor(GralColor color, Control swtWidget)
071  { Color colorSwt = getColor(color);
072    Color colorSwtOld = swtWidget.getBackground();
073    swtWidget.setBackground(colorSwt);
074    return getColor(colorSwtOld);
075  }
076
077  
078  public static GralColor setForegroundColor(GralColor color, Control swtWidget)
079  { 
080    Color colorSwt = (Color)color.colorGuimpl;
081    Color colorSwtOld = swtWidget.getForeground();
082    swtWidget.setForeground(colorSwt);
083    return getColor(colorSwtOld);
084  }
085
086
087  /**Sets the correct TabItem if any widget at this TabItem is focused. That is not done by swt graphic
088   * on Control.setFocus().
089   * @param control
090   */
091  public static boolean setFocusOfTabSwt(Control control)
092  {
093    List<Control> parents = new LinkedList<Control>();
094    Control parent = control;
095    while( parent !=null && (parent = parent.getParent())!=null){
096      parents.add(parent);
097    }
098    for(Control parent1: parents){
099      Object gralObj = parent1.getData();
100      if(gralObj !=null && gralObj instanceof SwtPanel){
101        SwtPanel gralPanel = (SwtPanel) gralObj;
102        TabItem tabitem = gralPanel.itsTabSwt;
103        if(tabitem !=null){
104          tabitem.getParent().setSelection(tabitem);
105        }
106      }
107      if(parent1 instanceof TabFolder){
108        TabFolder tf = (TabFolder)parent1;
109        tf.setFocus();
110      }
111    }
112    if(control == null){
113      return false;         //TODO should not be.
114    }
115    control.forceFocus();
116    return control.setFocus();
117
118    
119  }
120
121  
122  
123  public void swtUpdateRedraw(){
124    widgetSwt.update();
125    widgetSwt.redraw();
126  }
127  
128  
129  
130  public boolean setFocusGThread(){ return widgetSwt.setFocus(); }
131
132  /**Sets the implementation widget vible or not.
133   * @see org.vishia.gral.base.GralWidgImpl_ifc#setVisibleGThread(boolean)
134   */
135  public void setVisibleGThread(boolean bVisible){ 
136    widgetSwt.setVisible(bVisible);
137  }
138
139
140  public void removeWidgetImplementation()
141  {
142    if(widgetSwt !=null){ 
143      widgetSwt.dispose();
144      widgetSwt = null;
145    }
146  }
147
148
149  public void setBoundsPixel(int x, int y, int dx, int dy)
150  { widgetSwt.setBounds(x,y,dx,dy);
151  }
152  
153  
154  public GralRectangle getPixelPositionSize(){
155    int posx = 0, posy = 0;
156    Rectangle r = widgetSwt.getBounds();
157    Composite parent;
158    if(widgetSwt instanceof Composite){
159      parent = (Composite) widgetSwt; //start with them, maybe the shell itself
160    } else {
161      parent = widgetSwt.getParent();
162    }
163    Rectangle pos;
164    while( !( parent instanceof Shell ) ){
165      pos = parent.getBounds();
166      posx += pos.x; posy += pos.y;
167      parent = parent.getParent();
168    }
169    assert(parent instanceof Shell);
170    Rectangle s = parent.getClientArea();
171    pos = parent.getBounds();
172    int dframe = (pos.width - s.width) /2;   //width of the frame line.
173    posx += r.x + dframe;               //absolute position of the client area!
174    posy += r.y + (pos.height - s.height) - dframe;
175    int dx, dy;
176    if(parent == widgetSwt){
177      dx = s.width;
178      dy = s.height;
179    } else {
180      dx = r.width;
181      dy = r.height;
182    }
183    GralRectangle posSize = new GralRectangle(posx, posy, dx, dy);
184    return posSize;
185  }
186
187
188  
189  /**
190   * @param widg
191   * @return
192   * @deprecated this routine is implemented in {@link SwtWidgetHelper}
193   *   which can be used for all widgets (new concept).
194   */
195  @Deprecated
196  public static GralRectangle getPixelPositionSize(Control widg){
197    int posx = 0, posy = 0;
198    Rectangle r = widg.getBounds();
199    Composite parent;
200    if(widg instanceof Composite){
201      parent = (Composite) widg; //start with them, maybe the shell itself
202    } else {
203      parent = widg.getParent();
204    }
205    Rectangle pos;
206    while( !( parent instanceof Shell ) ){
207      pos = parent.getBounds();
208      posx += pos.x; posy += pos.y;
209      parent = parent.getParent();
210    }
211    assert(parent instanceof Shell);
212    Rectangle s = parent.getClientArea();
213    pos = parent.getBounds();
214    int dframe = (pos.width - s.width) /2;   //width of the frame line.
215    posx += r.x + dframe;               //absolute position of the client area!
216    posy += r.y + (pos.height - s.height) - dframe;
217    int dx, dy;
218    if(parent == widg){
219      dx = s.width;
220      dy = s.height;
221    } else {
222      dx = r.width;
223      dy = r.height;
224    }
225    GralRectangle posSize = new GralRectangle(posx, posy, dx, dy);
226    return posSize;
227  }
228
229
230
231
232  @Override
233  public void specifyContextMenu(GralMenu menu)
234  {
235    Menu swtMenu = (Menu)menu.getMenuImpl();
236    if(swtMenu == null) {
237      //GralMenu._GraphicImpl implMenu = new SwtMenu(menu, widgetSwt);
238      //implMenu._implMenu();
239      swtMenu = (Menu)menu.getMenuImpl();
240    }
241    try{
242      //widgetSwt.setMenu(swtMenu);
243    } catch(IllegalArgumentException exc){
244      Debugutil.stop();
245    }
246    
247  }
248
249
250  
251  
252}