| 1 | |
package net.miginfocom.swt; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
import net.miginfocom.layout.ComponentWrapper; |
| 37 | |
import net.miginfocom.layout.ContainerWrapper; |
| 38 | |
import net.miginfocom.layout.PlatformDefaults; |
| 39 | |
import org.eclipse.swt.SWT; |
| 40 | |
import org.eclipse.swt.custom.StyledText; |
| 41 | |
import org.eclipse.swt.graphics.*; |
| 42 | |
import org.eclipse.swt.widgets.*; |
| 43 | |
|
| 44 | |
import java.util.Random; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public class SwtComponentWrapper implements ComponentWrapper |
| 49 | |
{ |
| 50 | |
|
| 51 | |
|
| 52 | |
private static Color DB_COMP_OUTLINE = new Color(Display.getCurrent(), 0, 0, 200); |
| 53 | |
|
| 54 | |
private static boolean vp = false; |
| 55 | |
private static boolean mz = false; |
| 56 | |
|
| 57 | |
private final Control c; |
| 58 | |
private int compType = TYPE_UNSET; |
| 59 | |
|
| 60 | |
public SwtComponentWrapper(Control c) |
| 61 | |
{ |
| 62 | |
this.c = c; |
| 63 | |
} |
| 64 | |
|
| 65 | |
public final int getBaseline(int width, int height) |
| 66 | |
{ |
| 67 | |
return -1; |
| 68 | |
} |
| 69 | |
|
| 70 | |
public final Object getComponent() |
| 71 | |
{ |
| 72 | |
return c; |
| 73 | |
} |
| 74 | |
|
| 75 | |
public final float getPixelUnitFactor(boolean isHor) |
| 76 | |
{ |
| 77 | |
switch (PlatformDefaults.getLogicalPixelBase()) { |
| 78 | |
case PlatformDefaults.BASE_FONT_SIZE: |
| 79 | |
GC gc = new GC(c); |
| 80 | |
FontMetrics fm = gc.getFontMetrics(); |
| 81 | |
float f = isHor ? fm.getAverageCharWidth() / 5f : fm.getHeight() / 13f; |
| 82 | |
gc.dispose(); |
| 83 | |
return f; |
| 84 | |
|
| 85 | |
case PlatformDefaults.BASE_SCALE_FACTOR: |
| 86 | |
Float s = isHor ? PlatformDefaults.getHorizontalScaleFactor() : PlatformDefaults.getVerticalScaleFactor(); |
| 87 | |
if (s != null) |
| 88 | |
return s.floatValue(); |
| 89 | |
return (isHor ? getHorizontalScreenDPI() : getVerticalScreenDPI()) / (float) PlatformDefaults.getDefaultDPI(); |
| 90 | |
|
| 91 | |
default: |
| 92 | |
return 1f; |
| 93 | |
} |
| 94 | |
} |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
public final int getX() |
| 118 | |
{ |
| 119 | |
return c.getLocation().x; |
| 120 | |
} |
| 121 | |
|
| 122 | |
public final int getY() |
| 123 | |
{ |
| 124 | |
return c.getLocation().y; |
| 125 | |
} |
| 126 | |
|
| 127 | |
public final int getWidth() |
| 128 | |
{ |
| 129 | |
return c.getSize().x; |
| 130 | |
} |
| 131 | |
|
| 132 | |
public final int getHeight() |
| 133 | |
{ |
| 134 | |
return c.getSize().y; |
| 135 | |
} |
| 136 | |
|
| 137 | |
public final int getScreenLocationX() |
| 138 | |
{ |
| 139 | |
return c.toDisplay(0, 0).x; |
| 140 | |
} |
| 141 | |
|
| 142 | |
public final int getScreenLocationY() |
| 143 | |
{ |
| 144 | |
return c.toDisplay(0, 0).y; |
| 145 | |
} |
| 146 | |
|
| 147 | |
public final int getMinimumHeight(int sz) |
| 148 | |
{ |
| 149 | |
return mz ? 0 : computeSize(false, sz).y; |
| 150 | |
} |
| 151 | |
|
| 152 | |
public final int getMinimumWidth(int sz) |
| 153 | |
{ |
| 154 | |
return mz ? 0 : computeSize(true, sz).x; |
| 155 | |
} |
| 156 | |
|
| 157 | |
public final int getPreferredHeight(int sz) |
| 158 | |
{ |
| 159 | |
return computeSize(false, sz).y; |
| 160 | |
} |
| 161 | |
|
| 162 | |
public final int getPreferredWidth(int sz) |
| 163 | |
{ |
| 164 | |
return computeSize(true, sz).x; |
| 165 | |
} |
| 166 | |
|
| 167 | |
public final int getMaximumHeight(int sz) |
| 168 | |
{ |
| 169 | |
return Short.MAX_VALUE; |
| 170 | |
} |
| 171 | |
|
| 172 | |
public final int getMaximumWidth(int sz) |
| 173 | |
{ |
| 174 | |
return Short.MAX_VALUE; |
| 175 | |
} |
| 176 | |
|
| 177 | |
private Point computeSize(boolean hor, int sz) |
| 178 | |
{ |
| 179 | |
|
| 180 | |
int wHint = hor ? SWT.DEFAULT : sz; |
| 181 | |
int hHint = !hor ? SWT.DEFAULT : sz; |
| 182 | |
|
| 183 | |
if (wHint != SWT.DEFAULT || hHint != SWT.DEFAULT) { |
| 184 | |
int trim = 0; |
| 185 | |
if (c instanceof Scrollable) { |
| 186 | |
Rectangle rect = ((Scrollable) c).computeTrim(0, 0, 0, 0); |
| 187 | |
trim = hor ? rect.width : rect.height; |
| 188 | |
} else { |
| 189 | |
trim = (c.getBorderWidth() << 1); |
| 190 | |
} |
| 191 | |
if (wHint == SWT.DEFAULT) { |
| 192 | |
hHint = Math.max(0, hHint - trim); |
| 193 | |
} else { |
| 194 | |
wHint = Math.max(0, wHint - trim); |
| 195 | |
} |
| 196 | |
} |
| 197 | |
|
| 198 | |
return c.computeSize(wHint, hHint); |
| 199 | |
} |
| 200 | |
|
| 201 | |
public final ContainerWrapper getParent() |
| 202 | |
{ |
| 203 | |
return new SwtContainerWrapper(c.getParent()); |
| 204 | |
} |
| 205 | |
|
| 206 | |
public int getHorizontalScreenDPI() |
| 207 | |
{ |
| 208 | |
return c.getDisplay().getDPI().x; |
| 209 | |
} |
| 210 | |
|
| 211 | |
public int getVerticalScreenDPI() |
| 212 | |
{ |
| 213 | |
return c.getDisplay().getDPI().y; |
| 214 | |
} |
| 215 | |
|
| 216 | |
public final int getScreenWidth() |
| 217 | |
{ |
| 218 | |
return c.getDisplay().getBounds().width; |
| 219 | |
} |
| 220 | |
|
| 221 | |
public final int getScreenHeight() |
| 222 | |
{ |
| 223 | |
return c.getDisplay().getBounds().height; |
| 224 | |
} |
| 225 | |
|
| 226 | |
public final boolean hasBaseline() |
| 227 | |
{ |
| 228 | |
return false; |
| 229 | |
} |
| 230 | |
|
| 231 | |
public final String getLinkId() |
| 232 | |
{ |
| 233 | |
return null; |
| 234 | |
} |
| 235 | |
|
| 236 | |
public final void setBounds(int x, int y, int width, int height) |
| 237 | |
{ |
| 238 | |
c.setBounds(x, y, width, height); |
| 239 | |
} |
| 240 | |
|
| 241 | |
public boolean isVisible() |
| 242 | |
{ |
| 243 | |
return c.getVisible(); |
| 244 | |
} |
| 245 | |
|
| 246 | |
public final int[] getVisualPadding() |
| 247 | |
{ |
| 248 | |
return null; |
| 249 | |
} |
| 250 | |
|
| 251 | |
public static boolean isUseVisualPadding() |
| 252 | |
{ |
| 253 | |
return vp; |
| 254 | |
} |
| 255 | |
|
| 256 | |
public static void setUseVisualPadding(boolean b) |
| 257 | |
{ |
| 258 | |
vp = b; |
| 259 | |
} |
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
public static boolean isMinimumSizeZero() |
| 265 | |
{ |
| 266 | |
return mz; |
| 267 | |
} |
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
public static void setMinimumSizeZero(boolean b) |
| 273 | |
{ |
| 274 | |
mz = b; |
| 275 | |
} |
| 276 | |
|
| 277 | |
public int getLayoutHashCode() |
| 278 | |
{ |
| 279 | |
if (c.isDisposed()) |
| 280 | |
return -1; |
| 281 | |
|
| 282 | |
Point sz = c.getSize(); |
| 283 | |
Point p = c.computeSize(SWT.DEFAULT, SWT.DEFAULT, false); |
| 284 | |
int h = p.x + (p.y << 12) + (sz.x << 22) + (sz.y << 16); |
| 285 | |
|
| 286 | |
if (c.isVisible()) |
| 287 | |
h += 1324511; |
| 288 | |
|
| 289 | |
String id = getLinkId(); |
| 290 | |
if (id != null) |
| 291 | |
h += id.hashCode(); |
| 292 | |
return h; |
| 293 | |
} |
| 294 | |
|
| 295 | |
public final void paintDebugOutline() |
| 296 | |
{ |
| 297 | |
if (c.isDisposed()) |
| 298 | |
return; |
| 299 | |
|
| 300 | |
GC gc = new GC(c); |
| 301 | |
|
| 302 | |
gc.setLineJoin(SWT.JOIN_MITER); |
| 303 | |
gc.setLineCap(SWT.CAP_SQUARE); |
| 304 | |
gc.setLineStyle(SWT.LINE_DOT); |
| 305 | |
|
| 306 | |
gc.setForeground(DB_COMP_OUTLINE); |
| 307 | |
gc.drawRectangle(0, 0, getWidth() - 1, getHeight() - 1); |
| 308 | |
|
| 309 | |
gc.dispose(); |
| 310 | |
} |
| 311 | |
|
| 312 | |
public int getComponetType(boolean disregardScrollPane) |
| 313 | |
{ |
| 314 | |
if (compType == TYPE_UNSET) |
| 315 | |
compType = checkType(); |
| 316 | |
|
| 317 | |
return compType; |
| 318 | |
} |
| 319 | |
|
| 320 | |
private int checkType() |
| 321 | |
{ |
| 322 | |
int s = c.getStyle(); |
| 323 | |
|
| 324 | |
if (c instanceof Text || c instanceof StyledText) { |
| 325 | |
return (s & SWT.MULTI) > 0 ? TYPE_TEXT_AREA : TYPE_TEXT_FIELD; |
| 326 | |
} else if (c instanceof Label) { |
| 327 | |
return (s & SWT.SEPARATOR) > 0 ? TYPE_SEPARATOR : TYPE_LABEL; |
| 328 | |
} else if (c instanceof Button) { |
| 329 | |
if ((s & SWT.CHECK) > 0 || (s & SWT.RADIO) > 0) |
| 330 | |
return TYPE_CHECK_BOX; |
| 331 | |
return TYPE_BUTTON; |
| 332 | |
} else if (c instanceof Canvas) { |
| 333 | |
return TYPE_PANEL; |
| 334 | |
} else if (c instanceof List) { |
| 335 | |
return TYPE_LIST; |
| 336 | |
} else if (c instanceof Table) { |
| 337 | |
return TYPE_TABLE; |
| 338 | |
} else if (c instanceof Spinner) { |
| 339 | |
return TYPE_SPINNER; |
| 340 | |
} else if (c instanceof ProgressBar) { |
| 341 | |
return TYPE_PROGRESS_BAR; |
| 342 | |
} else if (c instanceof Slider) { |
| 343 | |
return TYPE_SLIDER; |
| 344 | |
} else if (c instanceof Composite) { |
| 345 | |
return TYPE_CONTAINER; |
| 346 | |
} |
| 347 | |
return TYPE_UNKNOWN; |
| 348 | |
} |
| 349 | |
|
| 350 | |
public final int hashCode() |
| 351 | |
{ |
| 352 | |
return c.hashCode(); |
| 353 | |
} |
| 354 | |
|
| 355 | |
public final boolean equals(Object o) |
| 356 | |
{ |
| 357 | |
if (o == null || o instanceof ComponentWrapper == false) |
| 358 | |
return false; |
| 359 | |
|
| 360 | |
return getComponent().equals(((ComponentWrapper) o).getComponent()); |
| 361 | |
} |
| 362 | |
} |