1 package org.herac.tuxguitar.gui.editors; 2 3 import org.eclipse.swt.SWT; 4 import org.eclipse.swt.graphics.Color; 5 import org.eclipse.swt.graphics.Font; 6 import org.eclipse.swt.graphics.GC; 7 import org.eclipse.swt.graphics.Image; 8 import org.eclipse.swt.graphics.Path; 9 import org.eclipse.swt.graphics.Point; 10 import org.eclipse.swt.graphics.Rectangle; 11 12 public class TGPainter { 13 14 public static final int PATH_DRAW = 0x01; 15 16 public static final int PATH_FILL = 0x02; 17 18 private boolean pathEmpty; 19 20 private int style; 21 22 private GC gc; 23 24 private Path path; 25 TGPainter()26 public TGPainter(){ 27 super(); 28 } 29 TGPainter(GC gc)30 public TGPainter(GC gc){ 31 this.init(gc); 32 } 33 TGPainter(Image image)34 public TGPainter(Image image){ 35 this.init(image); 36 } 37 init(Image image)38 public void init(Image image){ 39 this.init(new GC(image)); 40 } 41 init(GC gc)42 public void init(GC gc){ 43 if(this.gc != null && !this.gc.isDisposed()){ 44 this.gc.dispose(); 45 } 46 this.gc = gc; 47 } 48 initPath(int style)49 public void initPath(int style){ 50 this.style = style; 51 this.path = new Path(this.gc.getDevice()); 52 this.pathEmpty = true; 53 this.setAntialias(true); 54 } 55 initPath()56 public void initPath(){ 57 this.initPath( PATH_DRAW ); 58 } 59 closePath()60 public void closePath(){ 61 if(! this.pathEmpty ){ 62 if( (this.style & PATH_DRAW) != 0){ 63 this.gc.drawPath(this.path); 64 } 65 if( (this.style & PATH_FILL) != 0){ 66 this.gc.fillPath(this.path); 67 } 68 } 69 this.style = 0; 70 this.path.dispose(); 71 this.pathEmpty = true; 72 this.setAntialias(false); 73 } 74 getGC()75 public GC getGC(){ 76 return this.gc; 77 } 78 dispose()79 public void dispose(){ 80 this.gc.dispose(); 81 } 82 copyArea(Image image, int x, int y)83 public void copyArea(Image image, int x, int y) { 84 this.gc.copyArea(image, x, y); 85 } 86 getStringExtent(String string)87 public Point getStringExtent(String string) { 88 this.setAdvanced(false); 89 return this.gc.stringExtent(string); 90 } 91 drawString(String string, int x, int y)92 public void drawString(String string, int x, int y) { 93 this.setAdvanced(false); 94 this.gc.drawString(string, x, y); 95 } 96 drawString(String string, int x, int y, boolean isTransparent)97 public void drawString(String string, int x, int y, boolean isTransparent) { 98 this.setAdvanced(false); 99 this.gc.drawString(string, x, y, isTransparent); 100 } 101 drawImage(Image image, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight)102 public void drawImage(Image image, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight) { 103 this.setAdvanced(false); 104 this.gc.drawImage(image, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight); 105 } 106 drawImage(Image image, int x, int y)107 public void drawImage(Image image, int x, int y) { 108 this.setAdvanced(false); 109 this.gc.drawImage(image, x, y); 110 } 111 drawPolygon(int[] arg0)112 public void drawPolygon(int[] arg0) { 113 this.gc.drawPolygon(arg0); 114 } 115 fillPolygon(int[] arg0)116 public void fillPolygon(int[] arg0) { 117 this.gc.fillPolygon(arg0); 118 } 119 cubicTo(float arg0, float arg1, float arg2, float arg3, float arg4, float arg5)120 public void cubicTo(float arg0, float arg1, float arg2, float arg3, float arg4, float arg5) { 121 this.path.cubicTo(arg0, arg1, arg2, arg3, arg4, arg5); 122 this.pathEmpty = false; 123 } 124 lineTo(float arg0, float arg1)125 public void lineTo(float arg0, float arg1) { 126 this.path.lineTo(arg0, arg1); 127 this.pathEmpty = false; 128 } 129 moveTo(float arg0, float arg1)130 public void moveTo(float arg0, float arg1) { 131 this.path.moveTo(arg0, arg1); 132 this.pathEmpty = false; 133 } 134 addString(String arg0, float arg1, float arg2, Font arg3)135 public void addString(String arg0, float arg1, float arg2, Font arg3) { 136 this.path.addString(arg0, arg1, arg2, arg3); 137 this.pathEmpty = false; 138 } 139 addArc(float arg0, float arg1, float arg2, float arg3, float arg4, float arg5)140 public void addArc(float arg0, float arg1, float arg2, float arg3, float arg4, float arg5) { 141 this.path.addArc(arg0, arg1, arg2, arg3, arg4, arg5); 142 this.pathEmpty = false; 143 } 144 addOval(float arg0, float arg1, float arg2, float arg3)145 public void addOval(float arg0, float arg1, float arg2, float arg3) { 146 this.path.addArc(arg0, arg1, arg2, arg3, 0, 360); 147 this.pathEmpty = false; 148 } 149 addRectangle(float x,float y,float width,float height)150 public void addRectangle(float x,float y,float width,float height) { 151 this.path.addRectangle(x, y, width, height); 152 this.pathEmpty = false; 153 } 154 addRectangle(Rectangle rectangle)155 public void addRectangle(Rectangle rectangle) { 156 this.path.addRectangle(rectangle.x,rectangle.y,rectangle.width,rectangle.height); 157 this.pathEmpty = false; 158 } 159 setBackground(Color arg0)160 public void setBackground(Color arg0) { 161 this.gc.setBackground(arg0); 162 } 163 setFont(Font arg0)164 public void setFont(Font arg0) { 165 this.gc.setFont(arg0); 166 } 167 setForeground(Color arg0)168 public void setForeground(Color arg0) { 169 this.gc.setForeground(arg0); 170 } 171 setLineStyle(int arg0)172 public void setLineStyle(int arg0) { 173 this.gc.setLineStyle(arg0); 174 } 175 setLineWidth(int arg0)176 public void setLineWidth(int arg0) { 177 this.gc.setLineWidth(arg0); 178 } 179 setAlpha(int alpha)180 public void setAlpha(int alpha) { 181 this.gc.setAlpha(alpha); 182 } 183 setAntialias(boolean enabled)184 public void setAntialias(boolean enabled){ 185 if( !TGPainterUtils.FORCE_OS_DEFAULTS ){ 186 this.gc.setAntialias(enabled ? SWT.ON : SWT.OFF ); 187 } 188 } 189 setAdvanced(boolean advanced)190 public void setAdvanced(boolean advanced){ 191 if( !TGPainterUtils.FORCE_OS_DEFAULTS ){ 192 this.gc.setAdvanced(advanced); 193 } 194 } 195 } 196