1 11 package org.eclipse.swt.graphics; 12 13 import org.eclipse.swt.*; 14 import org.eclipse.swt.internal.gdip.*; 15 import org.eclipse.swt.internal.win32.*; 16 17 34 public class Path extends Resource { 35 36 46 public int handle; 47 48 PointF currentPoint = new PointF(), startPoint = new PointF(); 49 50 72 public Path (Device device) { 73 if (device == null) device = Device.getDevice(); 74 if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 75 this.device = device; 76 device.checkGDIP(); 77 handle = Gdip.GraphicsPath_new(Gdip.FillModeAlternate); 78 if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); 79 if (device.tracking) device.new_Object(this); 80 } 81 82 111 public void addArc(float x, float y, float width, float height, float startAngle, float arcAngle) { 112 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 113 if (width < 0) { 114 x = x + width; 115 width = -width; 116 } 117 if (height < 0) { 118 y = y + height; 119 height = -height; 120 } 121 if (width == 0 || height == 0 || arcAngle == 0) return; 122 if (width == height) { 123 Gdip.GraphicsPath_AddArc(handle, x, y, width, height, -startAngle, -arcAngle); 124 } else { 125 int path = Gdip.GraphicsPath_new(Gdip.FillModeAlternate); 126 if (path == 0) SWT.error(SWT.ERROR_NO_HANDLES); 127 int matrix = Gdip.Matrix_new(width, 0, 0, height, x, y); 128 if (matrix == 0) SWT.error(SWT.ERROR_NO_HANDLES); 129 Gdip.GraphicsPath_AddArc(path, 0, 0, 1, 1, -startAngle, -arcAngle); 130 Gdip.GraphicsPath_Transform(path, matrix); 131 Gdip.GraphicsPath_AddPath(handle, path, true); 132 Gdip.Matrix_delete(matrix); 133 Gdip.GraphicsPath_delete(path); 134 } 135 Gdip.GraphicsPath_GetLastPoint(handle, currentPoint); 136 } 137 138 151 public void addPath(Path path) { 152 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 153 if (path == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 154 if (path.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 155 Gdip.GraphicsPath_AddPath(handle, path.handle, false); 157 currentPoint.X = path.currentPoint.X; 158 currentPoint.Y = path.currentPoint.Y; 159 } 160 161 173 public void addRectangle(float x, float y, float width, float height) { 174 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 175 RectF rect = new RectF(); 176 rect.X = x; 177 rect.Y = y; 178 rect.Width = width; 179 rect.Height = height; 180 Gdip.GraphicsPath_AddRectangle(handle, rect); 181 currentPoint.X = x; 182 currentPoint.Y = y; 183 } 184 185 202 public void addString(String string, float x, float y, Font font) { 203 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 204 if (font == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 205 if (font.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 206 int length = string.length(); 207 char[] buffer = new char[length]; 208 string.getChars(0, length, buffer, 0); 209 int hDC = device.internal_new_GC(null); 210 int gdipFont = GC.createGdipFont(hDC, font.handle); 211 PointF point = new PointF(); 212 point.X = x - (Gdip.Font_GetSize(gdipFont) / 6); 213 point.Y = y; 214 int family = Gdip.FontFamily_new(); 215 Gdip.Font_GetFamily(gdipFont, family); 216 int style = Gdip.Font_GetStyle(gdipFont); 217 float size = Gdip.Font_GetSize(gdipFont); 218 Gdip.GraphicsPath_AddString(handle, buffer, length, family, style, size, point, 0); 219 Gdip.GraphicsPath_GetLastPoint(handle, currentPoint); 220 Gdip.FontFamily_delete(family); 221 Gdip.Font_delete(gdipFont); 222 device.internal_dispose_GC(hDC, null); 223 } 224 225 234 public void close() { 235 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 236 Gdip.GraphicsPath_CloseFigure(handle); 237 244 currentPoint.X = startPoint.X; 245 currentPoint.Y = startPoint.Y; 246 } 247 248 271 public boolean contains(float x, float y, GC gc, boolean outline) { 272 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 273 if (gc == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 274 if (gc.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 275 gc.initGdip(); 277 gc.checkGC(GC.LINE_CAP | GC.LINE_JOIN | GC.LINE_STYLE | GC.LINE_WIDTH); 278 int mode = OS.GetPolyFillMode(gc.handle) == OS.WINDING ? Gdip.FillModeWinding : Gdip.FillModeAlternate; 279 Gdip.GraphicsPath_SetFillMode(handle, mode); 280 if (outline) { 281 return Gdip.GraphicsPath_IsOutlineVisible(handle, x, y, gc.data.gdipPen, gc.data.gdipGraphics); 282 } else { 283 return Gdip.GraphicsPath_IsVisible(handle, x, y, gc.data.gdipGraphics); 284 } 285 } 286 287 301 public void cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) { 302 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 303 Gdip.GraphicsPath_AddBezier(handle, currentPoint.X, currentPoint.Y, cx1, cy1, cx2, cy2, x, y); 304 Gdip.GraphicsPath_GetLastPoint(handle, currentPoint); 305 } 306 307 312 public void dispose() { 313 if (handle == 0) return; 314 if (device.isDisposed()) return; 315 Gdip.GraphicsPath_delete(handle); 316 handle = 0; 317 if (device.tracking) device.dispose_Object(this); 318 device = null; 319 } 320 321 336 public void getBounds(float[] bounds) { 337 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 338 if (bounds == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 339 if (bounds.length < 4) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 340 RectF rect = new RectF(); 341 Gdip.GraphicsPath_GetBounds(handle, rect, 0, 0); 342 bounds[0] = rect.X; 343 bounds[1] = rect.Y; 344 bounds[2] = rect.Width; 345 bounds[3] = rect.Height; 346 } 347 348 362 public void getCurrentPoint(float[] point) { 363 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 364 if (point == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 365 if (point.length < 2) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 366 point[0] = currentPoint.X; 367 point[1] = currentPoint.Y; 368 } 369 370 381 public PathData getPathData() { 382 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 383 int count = Gdip.GraphicsPath_GetPointCount(handle); 384 byte[] gdipTypes = new byte[count]; 385 float[] points = new float[count * 2]; 386 Gdip.GraphicsPath_GetPathTypes(handle, gdipTypes, count); 387 Gdip.GraphicsPath_GetPathPoints(handle, points, count); 388 byte[] types = new byte[count * 2]; 389 int index = 0, typesIndex = 0; 390 while (index < count) { 391 byte type = gdipTypes[index]; 392 boolean close = false; 393 switch (type & Gdip.PathPointTypePathTypeMask) { 394 case Gdip.PathPointTypeStart: 395 types[typesIndex++] = SWT.PATH_MOVE_TO; 396 close = (type & Gdip.PathPointTypeCloseSubpath) != 0; 397 index += 1; 398 break; 399 case Gdip.PathPointTypeLine: 400 types[typesIndex++] = SWT.PATH_LINE_TO; 401 close = (type & Gdip.PathPointTypeCloseSubpath) != 0; 402 index += 1; 403 break; 404 case Gdip.PathPointTypeBezier: 405 types[typesIndex++] = SWT.PATH_CUBIC_TO; 406 close = (gdipTypes[index + 2] & Gdip.PathPointTypeCloseSubpath) != 0; 407 index += 3; 408 break; 409 default: 410 index++; 411 } 412 if (close) { 413 types[typesIndex++] = SWT.PATH_CLOSE; 414 } 415 } 416 if (typesIndex != types.length) { 417 byte[] newTypes = new byte[typesIndex]; 418 System.arraycopy(types, 0, newTypes, 0, typesIndex); 419 types = newTypes; 420 } 421 PathData result = new PathData(); 422 result.types = types; 423 result.points = points; 424 return result; 425 } 426 427 438 public void lineTo(float x, float y) { 439 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 440 Gdip.GraphicsPath_AddLine(handle, currentPoint.X, currentPoint.Y, x, y); 441 Gdip.GraphicsPath_GetLastPoint(handle, currentPoint); 442 } 443 444 454 public boolean isDisposed() { 455 return handle == 0; 456 } 457 458 470 public void moveTo(float x, float y) { 471 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 472 Gdip.GraphicsPath_StartFigure(handle); 473 currentPoint.X = startPoint.X = x; 474 currentPoint.Y = startPoint.Y = y; 475 } 476 477 489 public void quadTo(float cx, float cy, float x, float y) { 490 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 491 float cx1 = currentPoint.X + 2 * (cx - currentPoint.X) / 3; 492 float cy1 = currentPoint.Y + 2 * (cy - currentPoint.Y) / 3; 493 float cx2 = cx1 + (x - currentPoint.X) / 3; 494 float cy2 = cy1 + (y - currentPoint.Y) / 3; 495 Gdip.GraphicsPath_AddBezier(handle, currentPoint.X, currentPoint.Y, cx1, cy1, cx2, cy2, x, y); 496 Gdip.GraphicsPath_GetLastPoint(handle, currentPoint); 497 } 498 499 505 public String toString() { 506 if (isDisposed()) return "Path {*DISPOSED*}"; 507 return "Path {" + handle + "}"; 508 } 509 510 } 511 | Popular Tags |