javauis/lcdui_qt/src/javax/microedition/lcdui/Graphics.java
changeset 26 dc7c549001d5
parent 23 98ccebc37403
child 35 85266cc22c7f
equal deleted inserted replaced
23:98ccebc37403 26:dc7c549001d5
    14 * Description:
    14 * Description:
    15 *
    15 *
    16 */
    16 */
    17 package javax.microedition.lcdui;
    17 package javax.microedition.lcdui;
    18 
    18 
    19 import org.eclipse.swt.graphics.GC;
    19 
    20 import org.eclipse.swt.graphics.Internal_GfxPackageSupport;
    20 import org.eclipse.swt.graphics.Internal_GfxPackageSupport;
    21 import org.eclipse.swt.graphics.Rectangle;
    21 import org.eclipse.swt.graphics.Rectangle;
    22 import org.eclipse.swt.internal.qt.graphics.GraphicsContext;
    22 import org.eclipse.swt.internal.qt.graphics.GraphicsContext;
    23 import org.eclipse.swt.internal.qt.graphics.JavaCommandBuffer;
    23 
    24 import org.eclipse.swt.internal.qt.graphics.FontUtils;
    24 import org.eclipse.swt.internal.qt.graphics.FontUtils;
    25 
    25 
    26 import com.nokia.mid.ui.DirectGraphics;
    26 import com.nokia.mid.ui.DirectGraphics;
    27 
    27 
    28 /**
    28 /**
    74     /**
    74     /**
    75      * Constant for dotted stroke style.
    75      * Constant for dotted stroke style.
    76      */
    76      */
    77     public static final int DOTTED = 1;
    77     public static final int DOTTED = 1;
    78 
    78 
       
    79     static final int INVALID_STROKE_STYLE = -1;
       
    80 
    79     static final int RGB_MASK = 0x00FFFFFF;
    81     static final int RGB_MASK = 0x00FFFFFF;
    80 
    82 
       
    83     static final int OPAQUE_ALPHA = 0xff000000;
       
    84 
    81     static final int COMPONENT_MASK = 0xFF;
    85     static final int COMPONENT_MASK = 0xFF;
    82 
    86 
    83     private DirectGraphics directGraphics;
    87     private DirectGraphics directGraphics;
    84 
    88     private Buffer graphicsBuffer;
    85     // Graphics rectangle (off-screen buffer rectangle).
       
    86     // private Rectangle graphicsClientArea;
       
    87 
       
    88     // Graphics context for drawing to off-screen buffer.
       
    89     private GraphicsContext gc;
       
    90 
    89 
    91     // Off-screen buffer.
    90     // Off-screen buffer.
    92     //private org.eclipse.swt.internal.qt.graphics.Image frameBuffer;
    91     //private org.eclipse.swt.internal.qt.graphics.Image frameBuffer;
    93 
    92 
    94     // Current font for rendering texts.
    93     // Current font for rendering texts.
    95     private Font currentFont;
    94     Font currentFont;
    96     private int currentColor;
    95     int currentColor;
    97     private int translateX;
    96     int translateX;
    98     private int translateY;
    97     int translateY;
    99     private int[] currentClip = new int[4];
    98     int[] currentClip = new int[4];
   100     private int currentStrokeSyle;
    99     int currentStrokeSyle;
   101 
   100 
   102     // Image owning the Graphics if the Graphics object belongs to an Image.
       
   103     private Image parentImage;
       
   104 
       
   105     // Flag indicating that we are in Canvas.paint() callback,
       
   106     // thus inside UI thread
       
   107     private boolean paintCallBack;
       
   108     private boolean buffered;
       
   109     private JavaCommandBuffer cmdBuffer;
       
   110     private com.nokia.mj.impl.rt.support.Finalizer finalizer;
   101     private com.nokia.mj.impl.rt.support.Finalizer finalizer;
   111     // serialization lock for command buffering and flush
   102     // serialization lock for command buffering and flush
   112     private final Object flushLock = new Object();
   103     private final Object flushLock = new Object();
   113     private Canvas canvasParent;
   104     private Canvas canvasParent;
   114     private CustomItem customItemParent;
   105     private CustomItem customItemParent;
   115 
   106 
   116     //Constructor
   107     //Constructor
   117     Graphics()
   108     Graphics(Buffer buffer)
   118     {
   109     {
   119         finalizer = ((finalizer != null) ? finalizer
   110         finalizer = ((finalizer != null) ? finalizer
   120                      : new com.nokia.mj.impl.rt.support.Finalizer()
   111                      : new com.nokia.mj.impl.rt.support.Finalizer()
   121         {
   112         {
   122             public void finalizeImpl()
   113             public void finalizeImpl()
   136 
   127 
   137                     }
   128                     }
   138                 }
   129                 }
   139             }
   130             }
   140         });
   131         });
   141     }
   132         graphicsBuffer = buffer;
   142     /**
       
   143      * Set the parent image of this Graphics.
       
   144      *
       
   145      * @param image an image
       
   146      */
       
   147     void eswtSetParentImage(final Image image)
       
   148     {
       
   149         if(buffered)
       
   150         {
       
   151             return;
       
   152         }
       
   153         if(parentImage != image)
       
   154         {
       
   155             parentImage = image;
       
   156 
       
   157             if(gc == null)
       
   158             {
       
   159                 gc = new GraphicsContext();
       
   160             }
       
   161             else
       
   162             {
       
   163                 gc.releaseTarget();
       
   164             }
       
   165             ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
   166             {
       
   167                 public void run()
       
   168                 {
       
   169                     org.eclipse.swt.graphics.Image eswtImage = Image.getESWTImage(image);
       
   170 
       
   171                     if(eswtImage != null)
       
   172                     {
       
   173                         gc.bindTarget(Internal_GfxPackageSupport.getImage(eswtImage));
       
   174                         Rectangle clipRect = eswtImage.getBounds();
       
   175                         setClip(0, 0, clipRect.width, clipRect.height);
       
   176                     }
       
   177                     reset();
       
   178                 }
       
   179             });
       
   180         }
       
   181     }
       
   182 
       
   183     /**
       
   184      * Initializes this instance of Graphics to use command buffer.
       
   185      *
       
   186      * Can be called in a non-UI thread. Not thread-safe.
       
   187      *
       
   188      * @param x The x-coordinate of clip
       
   189      * @param y The y-coordinate of clip
       
   190      * @param width The width of clip
       
   191      * @param height The height of clip
       
   192      */
       
   193     void initBuffered(final Canvas parent, final int x, final int y, final int width, final int height)
       
   194     {
       
   195         ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
   196         {
       
   197             public void run()
       
   198             {
       
   199                 cmdBuffer = new JavaCommandBuffer();
       
   200                 if(gc == null)
       
   201                 {
       
   202                     gc = new GraphicsContext();
       
   203                 }
       
   204                 gc.bindTarget(cmdBuffer);
       
   205                 setClip(x, y, width, height);
       
   206                 reset();
       
   207                 buffered = true;
       
   208                 javax.microedition.lcdui.Graphics.this.canvasParent = parent;
       
   209             }
       
   210         });
       
   211     }
       
   212 
       
   213     /**
       
   214      * Initializes this instance of Graphics to use command buffer.
       
   215      *
       
   216      * Can be called in a non-UI thread. Not thread-safe.
       
   217      *
       
   218      * @param x The x-coordinate of clip
       
   219      * @param y The y-coordinate of clip
       
   220      * @param width The width of clip
       
   221      * @param height The height of clip
       
   222      */
       
   223     void initBuffered(CustomItem parent, int x, int y, int width, int height)
       
   224     {
       
   225         cmdBuffer = new JavaCommandBuffer();
       
   226         if(gc == null)
       
   227         {
       
   228             gc = new GraphicsContext();
       
   229         }
       
   230         gc.bindTarget(cmdBuffer);
       
   231         setClip(x, y, width, height);
       
   232         reset();
   133         reset();
   233         buffered = true;
   134     }
   234         this.customItemParent = parent;
   135 
   235     }
   136 
   236 
   137 
   237     /**
   138     /**
   238      * Resets the command buffer contents.
   139      * Disposes objects with native counterparts
   239      *
       
   240      * This is safe to call only in the UI thread.
       
   241      */
       
   242     void resetCommandBuffer()
       
   243     {
       
   244         gc.releaseTarget();
       
   245         cmdBuffer.reset();
       
   246         gc.bindTarget(cmdBuffer);
       
   247         // write settings which were active before flush
       
   248         // if they are not the same as defaults
       
   249         gc.setFont(Font.getESWTFont(currentFont).handle);
       
   250         gc.setBackgroundColor(currentColor, false);
       
   251         gc.setForegroundColor(currentColor, false);
       
   252         if((translateX != 0) || (translateY != 0))
       
   253         {
       
   254             gc.translate(translateX, translateY);
       
   255         }
       
   256 
       
   257         // Note that if called in a non-UI thread then the size of the
       
   258         // Canvas or CustomItem can change between getting the height and
       
   259         // getting the width. Those are modified by the UI thread and there
       
   260         // is no synchronization.
       
   261         int w = 0;
       
   262         int h = 0;
       
   263         if(canvasParent != null)
       
   264         {
       
   265             w = canvasParent.getWidth();
       
   266             h = canvasParent.getHeight();
       
   267         }
       
   268         else
       
   269         {
       
   270             w = customItemParent.getContentWidth();
       
   271             h = customItemParent.getContentHeight();
       
   272         }
       
   273 
       
   274         if((currentClip[0] != 0) && (currentClip[1] != 0) &&
       
   275                 (currentClip[2] != w) && (currentClip[2] != h))
       
   276         {
       
   277             gc.setClip(currentClip[0], currentClip[1], currentClip[2], currentClip[3], false);
       
   278         }
       
   279         if(currentStrokeSyle != SOLID)
       
   280         {
       
   281             gc.setStrokeStyle(GraphicsContext.STROKE_DOT);
       
   282         }
       
   283     }
       
   284 
       
   285     /**
       
   286      * Returns the current command buffer or null.
       
   287      *
       
   288      * This method is thread-safe.
       
   289      */
       
   290     JavaCommandBuffer getCommandBuffer()
       
   291     {
       
   292         return cmdBuffer;
       
   293     }
       
   294 
       
   295     /**
       
   296      * Get the parent image of this Graphics.
       
   297      */
       
   298     Image getParentImage()
       
   299     {
       
   300         return parentImage;
       
   301     }
       
   302 
       
   303     /**
       
   304      * Set Graphics context to a target - the GC is coming from paint event.
       
   305      *
       
   306      * @param eswtGC
       
   307      */
       
   308     void eswtSetGC(final GC eswtGC)
       
   309     {
       
   310         if(buffered)
       
   311         {
       
   312             return;
       
   313         }
       
   314         gc = eswtGC.getGCData().internalGc;
       
   315         ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
   316         {
       
   317             public void run()
       
   318             {
       
   319                 Rectangle clipRect = eswtGC.getClipping();
       
   320                 setClip(clipRect.x, clipRect.y, clipRect.width, clipRect.height);
       
   321                 reset();
       
   322             }
       
   323         });
       
   324     }
       
   325 
       
   326     /**
       
   327      * Disposes Graphics context resources.
       
   328      */
   140      */
   329     void dispose()
   141     void dispose()
   330     {
   142     {
   331         if(parentImage != null)
   143         synchronized(graphicsBuffer)
   332         {
   144         {
   333             parentImage = null;
   145             graphicsBuffer.removeRef();
   334         }
       
   335         if(gc != null)
       
   336         {
       
   337             ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
   338             {
       
   339                 public void run()
       
   340                 {
       
   341                     gc.dispose();
       
   342                 }
       
   343             });
       
   344             gc = null;
       
   345         }
       
   346         if(cmdBuffer != null)
       
   347         {
       
   348             cmdBuffer = null;
       
   349         }
   146         }
   350     }
   147     }
   351 
   148 
   352     /**
   149     /**
   353      * Resets Graphics state to initial.
   150      * Resets Graphics state to initial.
   381         setColor(255, 255, 255);
   178         setColor(255, 255, 255);
   382         fillRect(x, y, w, h);
   179         fillRect(x, y, w, h);
   383         setColor(savedColor);
   180         setColor(savedColor);
   384     }
   181     }
   385 
   182 
   386     /**
   183 
   387      * Sets flag indicating that we are in Canvas.paint() callback
       
   388      */
       
   389     void beginPaint()
       
   390     {
       
   391         paintCallBack = true;
       
   392     }
       
   393 
       
   394     /**
       
   395      * Sets flag indicating that we are exiting Canvas.paint() callback
       
   396      */
       
   397     void endPaint()
       
   398     {
       
   399         paintCallBack = false;
       
   400     }
       
   401 
       
   402     /**
       
   403      * Provides the serialization lock for buffer writing and flushing
       
   404      * @return lock used for synchronizing command buffer access
       
   405      */
       
   406     Object getLock()
       
   407     {
       
   408         return flushLock;
       
   409     }
       
   410 
   184 
   411     /**
   185     /**
   412      * Sets coordinate translation. Translations are cumulative.
   186      * Sets coordinate translation. Translations are cumulative.
   413      *
   187      *
   414      * @param xDelta x-shift for coordinates.
   188      * @param xDelta x-shift for coordinates.
   415      * @param yDelta y-shift for coordinates.
   189      * @param yDelta y-shift for coordinates.
   416      */
   190      */
   417     public void translate(int xDelta, int yDelta)
   191     public void translate(int xDelta, int yDelta)
   418     {
   192     {
   419         synchronized(flushLock)
   193         synchronized(graphicsBuffer)
   420         {
   194         {
   421             if(!buffered)
   195             graphicsBuffer.translate(xDelta, yDelta, this);
   422             {
   196             translateX += xDelta;
   423                 final int xDelta_ = xDelta;
   197             translateY += yDelta;
   424                 final int yDelta_ = yDelta;
   198         }
   425                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
   426                 {
       
   427                     public void run()
       
   428                     {
       
   429                         gc.translate(xDelta_, yDelta_);
       
   430                     }
       
   431                 });
       
   432             }
       
   433             else
       
   434             {
       
   435                 gc.translate(xDelta, yDelta);
       
   436             }
       
   437         }
       
   438         translateX += xDelta;
       
   439         translateY += yDelta;
       
   440     }
   199     }
   441 
   200 
   442     /**
   201     /**
   443      * Returns current X-shift of coordinate translation.
   202      * Returns current X-shift of coordinate translation.
   444      *
   203      *
   445      * @return Current X-shift of coordinate translation.
   204      * @return Current X-shift of coordinate translation.
   446      */
   205      */
   447     public int getTranslateX()
   206     public int getTranslateX()
   448     {
   207     {
   449         return translateX;
   208         synchronized(graphicsBuffer)
       
   209         {
       
   210             return translateX;
       
   211         }
   450     }
   212     }
   451 
   213 
   452     /**
   214     /**
   453      * Returns current Y-shift of coordinate translation.
   215      * Returns current Y-shift of coordinate translation.
   454      *
   216      *
   455      * @return current Y-shift of coordinate translation.
   217      * @return current Y-shift of coordinate translation.
   456      */
   218      */
   457     public int getTranslateY()
   219     public int getTranslateY()
   458     {
   220     {
   459         return translateY;
   221         synchronized(graphicsBuffer)
       
   222         {
       
   223             return translateY;
       
   224         }
   460     }
   225     }
   461 
   226 
   462     /**
   227     /**
   463      * Returns current color in 0x00RRGGBB format.
   228      * Returns current color in 0x00RRGGBB format.
   464      *
   229      *
   465      * @return Current color in 0x00RRGGBB format.
   230      * @return Current color in 0x00RRGGBB format.
   466      */
   231      */
   467     public int getColor()
   232     public int getColor()
   468     {
   233     {
   469         return currentColor;
   234         synchronized(graphicsBuffer)
       
   235         {
       
   236             return (currentColor & RGB_MASK);
       
   237         }
   470     }
   238     }
   471 
   239 
   472     /**
   240     /**
   473      * Returns red component of current color.
   241      * Returns red component of current color.
   474      *
   242      *
   475      * @return Red component of current color in the range of 0-255.
   243      * @return Red component of current color in the range of 0-255.
   476      */
   244      */
   477     public int getRedComponent()
   245     public int getRedComponent()
   478     {
   246     {
   479         return currentColor >> 16;
   247         synchronized(graphicsBuffer)
       
   248         {
       
   249             return currentColor >> 16;
       
   250         }
   480     }
   251     }
   481 
   252 
   482     /**
   253     /**
   483      * Returns green component of current color.
   254      * Returns green component of current color.
   484      *
   255      *
   485      * @return Green component of current color in the range of 0-255.
   256      * @return Green component of current color in the range of 0-255.
   486      */
   257      */
   487     public int getGreenComponent()
   258     public int getGreenComponent()
   488     {
   259     {
   489         return (currentColor >> 8) & COMPONENT_MASK;
   260         synchronized(graphicsBuffer)
       
   261         {
       
   262             return (currentColor >> 8) & COMPONENT_MASK;
       
   263         }
   490     }
   264     }
   491 
   265 
   492     /**
   266     /**
   493      * Returns blue component of current color.
   267      * Returns blue component of current color.
   494      *
   268      *
   495      * @return Blue component of current color in the range of 0-255.
   269      * @return Blue component of current color in the range of 0-255.
   496      */
   270      */
   497     public int getBlueComponent()
   271     public int getBlueComponent()
   498     {
   272     {
   499         return currentColor & COMPONENT_MASK;
   273         synchronized(graphicsBuffer)
       
   274         {
       
   275             return currentColor & COMPONENT_MASK;
       
   276         }
   500     }
   277     }
   501 
   278 
   502     /**
   279     /**
   503      * Returns current grayscale color.
   280      * Returns current grayscale color.
   504      *
   281      *
   505      * @return Returns current grayscale color in the range 0-255.
   282      * @return Returns current grayscale color in the range 0-255.
   506      */
   283      */
   507     public int getGrayScale()
   284     public int getGrayScale()
   508     {
   285     {
   509         return (getRedComponent() + getGreenComponent() + getBlueComponent()) / 3;
   286         synchronized(graphicsBuffer)
       
   287         {
       
   288             return (getRedComponent() + getGreenComponent() + getBlueComponent()) / 3;
       
   289         }
   510     }
   290     }
   511 
   291 
   512     /**
   292     /**
   513      * Sets the current color by color components for red, green and blue.
   293      * Sets the current color by color components for red, green and blue.
   514      *
   294      *
   522                 g < 0 || g > 255 ||
   302                 g < 0 || g > 255 ||
   523                 b < 0 || b > 255)
   303                 b < 0 || b > 255)
   524         {
   304         {
   525             throw new IllegalArgumentException();
   305             throw new IllegalArgumentException();
   526         }
   306         }
   527         synchronized(flushLock)
   307         synchronized(graphicsBuffer)
   528         {
   308         {
   529             if(!buffered)
   309             graphicsBuffer.setColor(r, g, b, this);
   530             {
   310             currentColor = (OPAQUE_ALPHA | (r << 16) | (g << 8) | b);
   531                 final int r_ = r;
   311         }
   532                 final int g_ = g;
   312 
   533                 final int b_ = b;
       
   534                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
   535                 {
       
   536                     public void run()
       
   537                     {
       
   538                         gc.setForegroundColor(r_, g_, b_);
       
   539                         gc.setBackgroundColor(r_, g_, b_);
       
   540                     }
       
   541                 });
       
   542             }
       
   543             else
       
   544             {
       
   545                 gc.setForegroundColor(r, g, b);
       
   546                 gc.setBackgroundColor(r, g, b);
       
   547             }
       
   548         }
       
   549         currentColor = ((r << 16) | (g << 8) | b) & RGB_MASK;
       
   550     }
   313     }
   551 
   314 
   552     /**
   315     /**
   553      * Sets the current color.
   316      * Sets the current color.
   554      *
   317      *
   558     {
   321     {
   559         int maskedRGB = RGB & RGB_MASK;
   322         int maskedRGB = RGB & RGB_MASK;
   560         final int r = maskedRGB >> 16;
   323         final int r = maskedRGB >> 16;
   561         final int g = (maskedRGB >> 8) & COMPONENT_MASK;
   324         final int g = (maskedRGB >> 8) & COMPONENT_MASK;
   562         final int b = maskedRGB & COMPONENT_MASK;
   325         final int b = maskedRGB & COMPONENT_MASK;
   563         synchronized(flushLock)
   326         synchronized(graphicsBuffer)
   564         {
   327         {
   565             if(!buffered)
   328             graphicsBuffer.setColor(r, g, b, this);
   566             {
   329             currentColor = (OPAQUE_ALPHA | maskedRGB);
   567                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
   330         }
   568                 {
   331 
   569                     public void run()
       
   570                     {
       
   571                         gc.setForegroundColor(r, g, b);
       
   572                         gc.setBackgroundColor(r, g, b);
       
   573                     }
       
   574                 });
       
   575             }
       
   576             else
       
   577             {
       
   578                 gc.setForegroundColor(r, g, b);
       
   579                 gc.setBackgroundColor(r, g, b);
       
   580             }
       
   581         }
       
   582         currentColor = maskedRGB;
       
   583     }
   332     }
   584 
   333 
   585     /**
   334     /**
   586      * Sets gray-scale color.
   335      * Sets gray-scale color.
   587      *
   336      *
   592         if((val < 0) || (val > 255))
   341         if((val < 0) || (val > 255))
   593         {
   342         {
   594             throw new IllegalArgumentException();
   343             throw new IllegalArgumentException();
   595         }
   344         }
   596         final int col = val & COMPONENT_MASK;
   345         final int col = val & COMPONENT_MASK;
   597         synchronized(flushLock)
   346         synchronized(graphicsBuffer)
   598         {
   347         {
   599             if(!buffered)
   348             graphicsBuffer.setColor(col, col, col, this);
   600             {
   349             currentColor = (OPAQUE_ALPHA | (col << 16) | (col << 8) | col);
   601                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
   350         }
   602                 {
   351 
   603                     public void run()
       
   604                     {
       
   605                         gc.setForegroundColor(col, col, col);
       
   606                         gc.setBackgroundColor(col, col, col);
       
   607                     }
       
   608                 });
       
   609             }
       
   610             else
       
   611             {
       
   612                 gc.setForegroundColor(col, col, col);
       
   613                 gc.setBackgroundColor(col, col, col);
       
   614             }
       
   615         }
       
   616         currentColor = ((col << 16) | (col << 8) | col) & RGB_MASK;
       
   617     }
   352     }
   618 
   353 
   619     /**
   354     /**
   620      * Returns the current font.
   355      * Returns the current font.
   621      *
   356      *
   622      * @return Current font.
   357      * @return Current font.
   623      */
   358      */
   624     public Font getFont()
   359     public Font getFont()
   625     {
   360     {
   626         return currentFont;
   361         synchronized(graphicsBuffer)
       
   362         {
       
   363             return currentFont;
       
   364         }
   627     }
   365     }
   628 
   366 
   629     /**
   367     /**
   630      * Sets the font to be used for string rendering.
   368      * Sets the font to be used for string rendering.
   631      *
   369      *
   632      * @param newFont - the font to be used for string rendering.
   370      * @param newFont - the font to be used for string rendering.
   633      */
   371      */
   634     public void setFont(Font newFont)
   372     public void setFont(Font newFont)
   635     {
   373     {
   636         synchronized(flushLock)
   374         synchronized(graphicsBuffer)
   637         {
   375         {
   638             if(!buffered)
   376             if(newFont == null)
   639             {
   377             {
   640                 final Font newFont_ = newFont;
   378                 newFont = Font.getDefaultFont();
   641                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
   379             }
   642                 {
   380             graphicsBuffer.setFont(Font.getESWTFont(newFont).handle, this);
   643                     public void run()
   381             currentFont = newFont;
   644                     {
   382         }
   645                         gc.setFont(Font.getESWTFont(newFont_).handle);
       
   646                     }
       
   647                 });
       
   648             }
       
   649             else
       
   650             {
       
   651                 gc.setFont(Font.getESWTFont(newFont).handle);
       
   652             }
       
   653         }
       
   654         currentFont = newFont;
       
   655     }
   383     }
   656 
   384 
   657     /**
   385     /**
   658      * Returns left bound of clip rectangle.
   386      * Returns left bound of clip rectangle.
   659      *
   387      *
   660      * @return Left bound of clip rectangle.
   388      * @return Left bound of clip rectangle.
   661      */
   389      */
   662     public int getClipX()
   390     public int getClipX()
   663     {
   391     {
   664         return currentClip[0];
   392         synchronized(graphicsBuffer)
       
   393         {
       
   394             return currentClip[0];
       
   395         }
   665     }
   396     }
   666 
   397 
   667     /**
   398     /**
   668      * Returns top bound of clip rectangle.
   399      * Returns top bound of clip rectangle.
   669      *
   400      *
   670      * @return Top bound of clip rectangle.
   401      * @return Top bound of clip rectangle.
   671      */
   402      */
   672     public int getClipY()
   403     public int getClipY()
   673     {
   404     {
   674         return currentClip[1];
   405         synchronized(graphicsBuffer)
       
   406         {
       
   407             return currentClip[1];
       
   408         }
   675     }
   409     }
   676 
   410 
   677     /**
   411     /**
   678      * Returns width of clip rectangle.
   412      * Returns width of clip rectangle.
   679      *
   413      *
   680      * @return Width of clip rectangle.
   414      * @return Width of clip rectangle.
   681      */
   415      */
   682     public int getClipWidth()
   416     public int getClipWidth()
   683     {
   417     {
   684         return currentClip[2];
   418         synchronized(graphicsBuffer)
       
   419         {
       
   420             return currentClip[2];
       
   421         }
   685     }
   422     }
   686 
   423 
   687     /**
   424     /**
   688      * Returns height of clip rectangle.
   425      * Returns height of clip rectangle.
   689      *
   426      *
   690      * @return Height of clip rectangle.
   427      * @return Height of clip rectangle.
   691      */
   428      */
   692     public int getClipHeight()
   429     public int getClipHeight()
   693     {
   430     {
   694         return currentClip[3];
   431         synchronized(graphicsBuffer)
       
   432         {
       
   433             return currentClip[3];
       
   434         }
   695     }
   435     }
   696 
   436 
   697     /**
   437     /**
   698      * Intersects current clip rectangle with the specified one.
   438      * Intersects current clip rectangle with the specified one.
   699      *
   439      *
   705      * @param h - height of the clip rectangle to intersect with the current
   445      * @param h - height of the clip rectangle to intersect with the current
   706      *            one.
   446      *            one.
   707      */
   447      */
   708     public void clipRect(int x, int y, int w, int h)
   448     public void clipRect(int x, int y, int w, int h)
   709     {
   449     {
   710         final int cx2 = Math.min(currentClip[0] + currentClip[2], x + w);
   450         synchronized(graphicsBuffer)
   711         final int cy2 = Math.min(currentClip[1] + currentClip[3], y + h);
   451         {
   712         // setting of clip to Java Graphics
   452             final int cx2 = Math.min(currentClip[0] + currentClip[2], x + w);
   713         currentClip[0] = Math.max(x, currentClip[0]);
   453             final int cy2 = Math.min(currentClip[1] + currentClip[3], y + h);
   714         currentClip[1] = Math.max(y, currentClip[1]);
   454             // setting of clip to Java Graphics
   715         currentClip[2] = cx2 - currentClip[0];
   455             currentClip[0] = Math.max(x, currentClip[0]);
   716         currentClip[3] = cy2 - currentClip[1];
   456             currentClip[1] = Math.max(y, currentClip[1]);
   717         synchronized(flushLock)
   457             currentClip[2] = cx2 - currentClip[0];
   718         {
   458             currentClip[3] = cy2 - currentClip[1];
   719             if(!buffered)
   459             graphicsBuffer.setClip(currentClip[0], currentClip[1], currentClip[2], currentClip[3], this);
   720             {
       
   721                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
   722                 {
       
   723                     public void run()
       
   724                     {
       
   725                         gc.setClip(currentClip[0], currentClip[1], currentClip[2], currentClip[3], false);
       
   726                     }
       
   727                 });
       
   728             }
       
   729             else
       
   730             {
       
   731                 gc.setClip(currentClip[0], currentClip[1], currentClip[2], currentClip[3], false);
       
   732             }
       
   733         }
   460         }
   734     }
   461     }
   735 
   462 
   736     /**
   463     /**
   737      * Sets the clip rectangle.
   464      * Sets the clip rectangle.
   741      * @param w - width of the new clip rectangle.
   468      * @param w - width of the new clip rectangle.
   742      * @param h - height of the new clip rectangle.
   469      * @param h - height of the new clip rectangle.
   743      */
   470      */
   744     public void setClip(int x, int y, int w, int h)
   471     public void setClip(int x, int y, int w, int h)
   745     {
   472     {
   746         synchronized(flushLock)
   473         synchronized(graphicsBuffer)
   747         {
   474         {
   748             if(!buffered)
   475             currentClip[0] = x;
   749             {
   476             currentClip[1] = y;
   750                 final int x_ = x;
   477             currentClip[2] = w;
   751                 final int y_ = y;
   478             currentClip[3] = h;
   752                 final int w_ = w;
   479             graphicsBuffer.setClip(currentClip[0], currentClip[1], currentClip[2], currentClip[3], this);
   753                 final int h_ = h;
   480         }
   754 
       
   755                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
   756                 {
       
   757                     public void run()
       
   758                     {
       
   759                         gc.setClip(x_, y_, w_, h_, false);
       
   760                     }
       
   761                 });
       
   762             }
       
   763             else
       
   764             {
       
   765                 gc.setClip(x, y, w, h, false);
       
   766             }
       
   767         }
       
   768         currentClip[0] = x;
       
   769         currentClip[1] = y;
       
   770         currentClip[2] = w;
       
   771         currentClip[3] = h;
       
   772     }
   481     }
   773 
   482 
   774     /**
   483     /**
   775      * Draws a line with current color and current stroke style.
   484      * Draws a line with current color and current stroke style.
   776      *
   485      *
   779      * @param xEnd - X-coordinate of line end point.
   488      * @param xEnd - X-coordinate of line end point.
   780      * @param yEnd - Y-coordinate of line end point.
   489      * @param yEnd - Y-coordinate of line end point.
   781      */
   490      */
   782     public void drawLine(int xStart, int yStart, int xEnd, int yEnd)
   491     public void drawLine(int xStart, int yStart, int xEnd, int yEnd)
   783     {
   492     {
   784         synchronized(flushLock)
   493         synchronized(graphicsBuffer)
   785         {
   494         {
   786             if(!buffered)
   495             graphicsBuffer.drawLine(xStart, yStart, xEnd, yEnd, this);
   787             {
       
   788                 final int xs_ = xStart;
       
   789                 final int ys_ = yStart;
       
   790                 final int xe_ = xEnd;
       
   791                 final int ye_ = yEnd;
       
   792 
       
   793                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
   794                 {
       
   795                     public void run()
       
   796                     {
       
   797                         gc.drawLine(xs_, ys_, xe_, ye_);
       
   798                     }
       
   799                 });
       
   800             }
       
   801             else
       
   802             {
       
   803                 gc.drawLine(xStart, yStart, xEnd, yEnd);
       
   804             }
       
   805         }
   496         }
   806     }
   497     }
   807 
   498 
   808     /**
   499     /**
   809      * Fills a rectangle with current color.
   500      * Fills a rectangle with current color.
   817     {
   508     {
   818         if((w < 0) || (h < 0))
   509         if((w < 0) || (h < 0))
   819         {
   510         {
   820             return;
   511             return;
   821         }
   512         }
   822         synchronized(flushLock)
   513         synchronized(graphicsBuffer)
   823         {
   514         {
   824             if(!buffered)
   515             graphicsBuffer.fillRect(x, y, w, h, this);
   825             {
       
   826                 final int x_ = x;
       
   827                 final int y_ = y;
       
   828                 final int w_ = w;
       
   829                 final int h_ = h;
       
   830 
       
   831                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
   832                 {
       
   833                     public void run()
       
   834                     {
       
   835                         gc.fillRect(x_, y_, w_, h_);
       
   836                     }
       
   837                 });
       
   838             }
       
   839             else
       
   840             {
       
   841                 gc.fillRect(x, y, w, h);
       
   842             }
       
   843         }
   516         }
   844     }
   517     }
   845 
   518 
   846     /**
   519     /**
   847      * Draws a rectangle with specified color and stroke style.
   520      * Draws a rectangle with specified color and stroke style.
   855     {
   528     {
   856         if((w < 0) || (h < 0))
   529         if((w < 0) || (h < 0))
   857         {
   530         {
   858             return;
   531             return;
   859         }
   532         }
   860         synchronized(flushLock)
   533         synchronized(graphicsBuffer)
   861         {
   534         {
   862             if(!buffered)
   535             graphicsBuffer.drawRect(x, y, w, h, this);
   863             {
       
   864                 final int x_ = x;
       
   865                 final int y_ = y;
       
   866                 final int w_ = w;
       
   867                 final int h_ = h;
       
   868                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
   869                 {
       
   870                     public void run()
       
   871                     {
       
   872                         gc.drawRect(x_, y_, w_, h_);
       
   873                     }
       
   874                 });
       
   875             }
       
   876             else
       
   877             {
       
   878                 gc.drawRect(x, y, w, h);
       
   879             }
       
   880         }
   536         }
   881     }
   537     }
   882 
   538 
   883     /**
   539     /**
   884      * Draws a rounded rectangle with specified color and stroke style.
   540      * Draws a rounded rectangle with specified color and stroke style.
   894     {
   550     {
   895         if((w < 0) || (h < 0) || (arcW < 0) || (arcH < 0))
   551         if((w < 0) || (h < 0) || (arcW < 0) || (arcH < 0))
   896         {
   552         {
   897             return;
   553             return;
   898         }
   554         }
   899         synchronized(flushLock)
   555         synchronized(graphicsBuffer)
   900         {
   556         {
   901             if(!buffered)
   557             graphicsBuffer.drawRoundRect(x, y, w, h, arcW, arcH, this);
   902             {
       
   903                 final int x_ = x;
       
   904                 final int y_ = y;
       
   905                 final int w_ = w;
       
   906                 final int h_ = h;
       
   907                 final int arcW_ = arcW;
       
   908                 final int arcH_ = arcH;
       
   909 
       
   910                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
   911                 {
       
   912                     public void run()
       
   913                     {
       
   914                         gc.drawRoundRect(x_, y_, w_, h_, arcW_, arcH_);
       
   915                     }
       
   916                 });
       
   917             }
       
   918             else
       
   919             {
       
   920                 gc.drawRoundRect(x, y, w, h, arcW, arcH);
       
   921             }
       
   922         }
   558         }
   923     }
   559     }
   924 
   560 
   925     /**
   561     /**
   926      * Fills a rounded rectangle with specified color.
   562      * Fills a rounded rectangle with specified color.
   936     {
   572     {
   937         if((w < 0) || (h < 0) || (arcW < 0) || (arcH < 0))
   573         if((w < 0) || (h < 0) || (arcW < 0) || (arcH < 0))
   938         {
   574         {
   939             return;
   575             return;
   940         }
   576         }
   941         synchronized(flushLock)
   577         synchronized(graphicsBuffer)
   942         {
   578         {
   943             if(!buffered)
   579             graphicsBuffer.fillRoundRect(x, y, w, h, arcW, arcH, this);
   944             {
       
   945                 final int x_ = x;
       
   946                 final int y_ = y;
       
   947                 final int w_ = w;
       
   948                 final int h_ = h;
       
   949                 final int arcW_ = arcW;
       
   950                 final int arcH_ = arcH;
       
   951 
       
   952                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
   953                 {
       
   954                     public void run()
       
   955                     {
       
   956                         gc.fillRoundRect(x_, y_, w_, h_, arcW_, arcH_);
       
   957                     }
       
   958                 });
       
   959             }
       
   960             else
       
   961             {
       
   962                 gc.fillRoundRect(x, y, w, h, arcW, arcH);
       
   963             }
       
   964         }
   580         }
   965     }
   581     }
   966 
   582 
   967     /**
   583     /**
   968      * Fills an arc with specified color.
   584      * Fills an arc with specified color.
   980     {
   596     {
   981         if((w < 0) || (h < 0))
   597         if((w < 0) || (h < 0))
   982         {
   598         {
   983             return;
   599             return;
   984         }
   600         }
   985         synchronized(flushLock)
   601         synchronized(graphicsBuffer)
   986         {
   602         {
   987             if(!buffered)
   603             graphicsBuffer.fillArc(x, y, w, h, startAngle, arcAngle, this);
   988             {
       
   989                 final int x_ = x;
       
   990                 final int y_ = y;
       
   991                 final int w_ = w;
       
   992                 final int h_ = h;
       
   993                 final int startAngle_ = startAngle;
       
   994                 final int arcAngle_ = arcAngle;
       
   995 
       
   996                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
   997                 {
       
   998                     public void run()
       
   999                     {
       
  1000                         gc.fillArc(x_, y_, w_, h_, startAngle_, arcAngle_);
       
  1001                     }
       
  1002                 });
       
  1003             }
       
  1004             else
       
  1005             {
       
  1006                 gc.fillArc(x, y, w, h, startAngle, arcAngle);
       
  1007             }
       
  1008         }
   604         }
  1009     }
   605     }
  1010 
   606 
  1011     /**
   607     /**
  1012      * Draws an arc with specified color and stroke style.
   608      * Draws an arc with specified color and stroke style.
  1024     {
   620     {
  1025         if((w < 0) || (h < 0))
   621         if((w < 0) || (h < 0))
  1026         {
   622         {
  1027             return;
   623             return;
  1028         }
   624         }
  1029         synchronized(flushLock)
   625         synchronized(graphicsBuffer)
  1030         {
   626         {
  1031             if(!buffered)
   627             graphicsBuffer.drawArc(x, y, w, h, startAngle, arcAngle, this);
  1032             {
       
  1033                 final int x_ = x;
       
  1034                 final int y_ = y;
       
  1035                 final int w_ = w;
       
  1036                 final int h_ = h;
       
  1037                 final int startAngle_ = startAngle;
       
  1038                 final int arcAngle_ = arcAngle;
       
  1039 
       
  1040                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
  1041                 {
       
  1042                     public void run()
       
  1043                     {
       
  1044                         gc.drawArc(x_, y_, w_, h_, startAngle_, arcAngle_);
       
  1045                     }
       
  1046                 });
       
  1047             }
       
  1048             else
       
  1049             {
       
  1050                 gc.drawArc(x, y, w, h, startAngle, arcAngle);
       
  1051             }
       
  1052         }
   628         }
  1053     }
   629     }
  1054 
   630 
  1055     /**
   631     /**
  1056      * Draws a string with specified color and font.
   632      * Draws a string with specified color and font.
  1075         {
   651         {
  1076             throw new IllegalArgumentException(
   652             throw new IllegalArgumentException(
  1077                 MsgRepository.GRAPHICS_EXCEPTION_INVALID_ANCHOR);
   653                 MsgRepository.GRAPHICS_EXCEPTION_INVALID_ANCHOR);
  1078         }
   654         }
  1079 
   655 
  1080         final int alignments = GraphicsContext.ALIGNMENT_TOP | GraphicsContext.ALIGNMENT_LEFT;
   656         synchronized(graphicsBuffer)
  1081         final int[] boundingBox = new int[4];
   657         {
  1082         final String localStr = string;
   658             final int alignments = GraphicsContext.ALIGNMENT_TOP | GraphicsContext.ALIGNMENT_LEFT;
  1083 
   659             final int[] boundingBox = new int[4];
  1084         if(paintCallBack)
   660             final String localStr = string;
  1085         {
   661 
  1086             gc.getTextBoundingBox(boundingBox, string, alignments, 0);
       
  1087         }
       
  1088         else
       
  1089         {
       
  1090             ESWTUIThreadRunner.safeSyncExec(new Runnable()
   662             ESWTUIThreadRunner.safeSyncExec(new Runnable()
  1091             {
   663             {
  1092                 public void run()
   664                 public void run()
  1093                 {
   665                 {
  1094                     FontUtils fu = new FontUtils(Font.getESWTFont(currentFont).handle);
   666                     FontUtils fu = new FontUtils(Font.getESWTFont(currentFont).handle);
  1095                     fu.getBoundingRect(boundingBox, localStr);
   667                     fu.getBoundingRect(boundingBox, localStr);
  1096                 }
   668                 }
  1097             });
   669             });
  1098         }
   670 
  1099 
   671 
  1100         // boundingBox[RECT_X] - top-left x of the text bounding box
   672             // Arrange vertical alignments
  1101         // boundingBox[RECT_Y] - top-left y of the text bounding box
   673             int y = yPos;
  1102         // boundingBox[RECT_WIDTH] - the width of the text bounding box
   674             if(isFlag(anch, Graphics.BOTTOM))
  1103         // boundingBox[RECT_HEIGHT] - the height of the text bounding box
   675             {
  1104 
   676                 y = yPos - boundingBox[GraphicsContext.RECT_HEIGHT];
  1105         // Arrange vertical alignments
   677             }
  1106         int y = yPos;
   678             if(isFlag(anch, Graphics.BASELINE))
  1107         if(isFlag(anch, Graphics.BOTTOM))
   679             {
  1108         {
   680                 y = yPos - currentFont.getBaselinePosition();
  1109             y = yPos - boundingBox[GraphicsContext.RECT_HEIGHT];
   681             }
  1110         }
   682             // Arrange horizontal alignments
  1111         if(isFlag(anch, Graphics.BASELINE))
   683             int x = xPos;
  1112         {
   684             if(isFlag(anch, Graphics.RIGHT))
  1113             y = yPos - currentFont.getBaselinePosition();
   685             {
  1114         }
   686                 x = xPos - boundingBox[GraphicsContext.RECT_WIDTH];
  1115 
   687             }
  1116         // Arrange horizontal alignments
   688             if(isFlag(anch, Graphics.HCENTER))
  1117         int x = xPos;
   689             {
  1118         if(isFlag(anch, Graphics.RIGHT))
   690                 x = xPos - boundingBox[GraphicsContext.RECT_WIDTH] / 2;
  1119         {
   691             }
  1120             x = xPos - boundingBox[GraphicsContext.RECT_WIDTH];
   692 
  1121         }
   693             graphicsBuffer.drawString(localStr, x, y, this);
  1122         if(isFlag(anch, Graphics.HCENTER))
       
  1123         {
       
  1124             x = xPos - boundingBox[GraphicsContext.RECT_WIDTH] / 2;
       
  1125         }
       
  1126         synchronized(flushLock)
       
  1127         {
       
  1128             if(!buffered)
       
  1129             {
       
  1130                 final int x_ = x;
       
  1131                 final int y_ = y;
       
  1132                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
  1133                 {
       
  1134                     public void run()
       
  1135                     {
       
  1136                         gc.drawString(localStr, x_, y_, true);
       
  1137                     }
       
  1138                 });
       
  1139             }
       
  1140             else
       
  1141             {
       
  1142                 gc.drawString(localStr, x, y, true);
       
  1143             }
       
  1144         }
   694         }
  1145     }
   695     }
  1146 
   696 
  1147     private static boolean isFlag(int anchor, int flag)
   697     private static boolean isFlag(int anchor, int flag)
  1148     {
   698     {
  1279         if(!checkImageAnchors(anch))
   829         if(!checkImageAnchors(anch))
  1280         {
   830         {
  1281             throw new IllegalArgumentException(
   831             throw new IllegalArgumentException(
  1282                 MsgRepository.GRAPHICS_EXCEPTION_INVALID_ANCHOR);
   832                 MsgRepository.GRAPHICS_EXCEPTION_INVALID_ANCHOR);
  1283         }
   833         }
  1284 
   834         synchronized(graphicsBuffer)
  1285         int y = yPos;
   835         {
  1286         if(isFlag(anch, Graphics.VCENTER))
   836             int y = yPos;
  1287         {
   837             if(isFlag(anch, Graphics.VCENTER))
  1288             y = yPos - image.getHeight() / 2;
   838             {
  1289         }
   839                 y = yPos - image.getHeight() / 2;
  1290         if(isFlag(anch, Graphics.BOTTOM))
   840             }
  1291         {
   841             if(isFlag(anch, Graphics.BOTTOM))
  1292             y = yPos - image.getHeight();
   842             {
  1293         }
   843                 y = yPos - image.getHeight();
  1294 
   844             }
  1295         int x = xPos;
   845 
  1296         if(isFlag(anch, Graphics.HCENTER))
   846             int x = xPos;
  1297         {
   847             if(isFlag(anch, Graphics.HCENTER))
  1298             x = xPos - image.getWidth() / 2;
   848             {
  1299         }
   849                 x = xPos - image.getWidth() / 2;
  1300         if(isFlag(anch, Graphics.RIGHT))
   850             }
  1301         {
   851             if(isFlag(anch, Graphics.RIGHT))
  1302             x = xPos - image.getWidth();
   852             {
  1303         }
   853                 x = xPos - image.getWidth();
  1304 
   854             }
  1305         synchronized(flushLock)
   855 
  1306         {
   856             synchronized(image.graphicsBuffer)
  1307             if(!buffered)
   857             {
  1308             {
   858                 image.sync();
  1309                 final int x_ = x;
   859                 graphicsBuffer.drawImage(Internal_GfxPackageSupport.getImage(Image.getESWTImage(image)), x, y, this);
  1310                 final int y_ = y;
       
  1311                 final org.eclipse.swt.internal.qt.graphics.Image image_ =
       
  1312                     Internal_GfxPackageSupport.getImage(Image.getESWTImage(image));
       
  1313                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
  1314                 {
       
  1315                     public void run()
       
  1316                     {
       
  1317                         gc.drawImage(image_, x_, y_);
       
  1318                     }
       
  1319                 });
       
  1320             }
       
  1321             else
       
  1322             {
       
  1323                 gc.drawImage(Internal_GfxPackageSupport.getImage(Image.getESWTImage(image)), x, y);
       
  1324             }
   860             }
  1325         }
   861         }
  1326     }
   862     }
  1327 
   863 
  1328     /**
   864     /**
  1376     {
   912     {
  1377         if(newStyle == currentStrokeSyle)
   913         if(newStyle == currentStrokeSyle)
  1378         {
   914         {
  1379             return;
   915             return;
  1380         }
   916         }
  1381         if(newStyle == SOLID)
   917         synchronized(graphicsBuffer)
  1382         {
   918         {
  1383             synchronized(flushLock)
   919             int styleToApply = mapStrokeStyle(newStyle);
  1384             {
   920             if(styleToApply == INVALID_STROKE_STYLE)
  1385                 if(!buffered)
       
  1386                 {
       
  1387                     ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
  1388                     {
       
  1389                         public void run()
       
  1390                         {
       
  1391                             gc.setStrokeStyle(GraphicsContext.STROKE_SOLID);
       
  1392                         }
       
  1393                     });
       
  1394                 }
       
  1395                 else
       
  1396                 {
       
  1397                     gc.setStrokeStyle(GraphicsContext.STROKE_SOLID);
       
  1398                 }
       
  1399             }
       
  1400         }
       
  1401         else
       
  1402         {
       
  1403             if(newStyle == DOTTED)
       
  1404             {
       
  1405                 synchronized(flushLock)
       
  1406                 {
       
  1407                     if(!buffered)
       
  1408                     {
       
  1409                         ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
  1410                         {
       
  1411                             public void run()
       
  1412                             {
       
  1413                                 gc.setStrokeStyle(GraphicsContext.STROKE_DOT);
       
  1414                             }
       
  1415                         });
       
  1416                     }
       
  1417                     else
       
  1418                     {
       
  1419                         gc.setStrokeStyle(GraphicsContext.STROKE_DOT);
       
  1420                     }
       
  1421                 }
       
  1422             }
       
  1423             else
       
  1424             {
   921             {
  1425                 throw new IllegalArgumentException(
   922                 throw new IllegalArgumentException(
  1426                     MsgRepository.GRAPHICS_EXCEPTION_ILLEGAL_STROKE_STYLE);
   923                     MsgRepository.GRAPHICS_EXCEPTION_ILLEGAL_STROKE_STYLE);
  1427             }
   924             }
  1428         }
   925             graphicsBuffer.setStrokeStyle(styleToApply, newStyle, this);
  1429         currentStrokeSyle = newStyle;
   926             currentStrokeSyle = newStyle;
       
   927         }
  1430     }
   928     }
  1431 
   929 
  1432     /**
   930     /**
  1433      * Returns current stroke style.
   931      * Returns current stroke style.
  1434      *
   932      *
  1435      * @return Current stroke style.
   933      * @return Current stroke style.
  1436      */
   934      */
  1437     public int getStrokeStyle()
   935     public int getStrokeStyle()
  1438     {
   936     {
  1439         return currentStrokeSyle;
   937         synchronized(graphicsBuffer)
       
   938         {
       
   939             return currentStrokeSyle;
       
   940         }
  1440     }
   941     }
  1441 
   942 
  1442     /**
   943     /**
  1443      * Returns the color that will be used if the specified color is requested.
   944      * Returns the color that will be used if the specified color is requested.
  1444      *
   945      *
  1478         if(rgb == null)
   979         if(rgb == null)
  1479         {
   980         {
  1480             throw new NullPointerException(
   981             throw new NullPointerException(
  1481                 MsgRepository.IMAGE_EXCEPTION_DATA_IS_NULL);
   982                 MsgRepository.IMAGE_EXCEPTION_DATA_IS_NULL);
  1482         }
   983         }
  1483         synchronized(flushLock)
   984         synchronized(graphicsBuffer)
  1484         {
   985         {
  1485             if(!buffered)
   986             graphicsBuffer.drawRGB(rgb, offset, scanlength, x, y, w, h, alpha, this);
  1486             {
       
  1487                 final int[] rgb_ = rgb;
       
  1488                 final int offset_ = offset;
       
  1489                 final int scanlength_ = scanlength;
       
  1490                 final int x_ = x;
       
  1491                 final int y_ = y;
       
  1492                 final int w_ = w;
       
  1493                 final int h_ = h;
       
  1494                 final boolean alpha_ = alpha;
       
  1495 
       
  1496                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
  1497                 {
       
  1498                     public void run()
       
  1499                     {
       
  1500                         gc.drawRGB(rgb_, offset_, scanlength_, x_, y_, w_, h_, alpha_);
       
  1501                     }
       
  1502                 });
       
  1503             }
       
  1504             else
       
  1505             {
       
  1506                 gc.drawRGB(rgb, offset, scanlength, x, y, w, h, alpha);
       
  1507             }
       
  1508         }
   987         }
  1509     }
   988     }
  1510 
   989 
  1511     /**
   990     /**
  1512      * Fills the specified triangle with current color.
   991      * Fills the specified triangle with current color.
  1523                              int xPos2,
  1002                              int xPos2,
  1524                              int yPos2,
  1003                              int yPos2,
  1525                              int xPos3,
  1004                              int xPos3,
  1526                              int yPos3)
  1005                              int yPos3)
  1527     {
  1006     {
  1528         final int[] points = {xPos1, yPos1, xPos2, yPos2, xPos3, yPos3};
  1007         synchronized(graphicsBuffer)
  1529         synchronized(flushLock)
  1008         {
  1530         {
  1009             final int[] points = {xPos1, yPos1, xPos2, yPos2, xPos3, yPos3};
  1531             if(!buffered)
  1010             graphicsBuffer.fillTriangle(points, this);
  1532             {
       
  1533                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
  1534                 {
       
  1535                     public void run()
       
  1536                     {
       
  1537                         gc.fillPolygon(points);
       
  1538                     }
       
  1539                 });
       
  1540             }
       
  1541             else
       
  1542             {
       
  1543                 gc.fillPolygon(points);
       
  1544             }
       
  1545         }
  1011         }
  1546     }
  1012     }
  1547 
  1013 
  1548     /**
  1014     /**
  1549      * Copies the specified area.
  1015      * Copies the specified area.
  1563                          int xTo,
  1029                          int xTo,
  1564                          int yTo,
  1030                          int yTo,
  1565                          int anch)
  1031                          int anch)
  1566     {
  1032     {
  1567 
  1033 
  1568         if(this.parentImage == null)
  1034         if(graphicsBuffer.getHostType() != Buffer.HOST_TYPE_IMAGE)
  1569         {
  1035         {
  1570             // this Graphics belongs to a screen device.
  1036             // this Graphics belongs to a screen device.
  1571             throw new IllegalStateException(
  1037             throw new IllegalStateException(
  1572                 MsgRepository.GRAPHICS_EXCEPTION_DESTINATION_IS_SCREEN);
  1038                 MsgRepository.GRAPHICS_EXCEPTION_DESTINATION_IS_SCREEN);
  1573         }
  1039         }
  1574 
  1040         synchronized(graphicsBuffer)
  1575         if(!javax.microedition.lcdui.Image.validateRegion(parentImage
  1041         {
  1576                 .getWidth(), parentImage.getHeight(), xFrom, yFrom, w, h))
  1042             Image image = (Image)graphicsBuffer.getHost();
  1577         {
  1043             if(!javax.microedition.lcdui.Image.validateRegion(image
  1578             throw new IllegalArgumentException(
  1044                     .getWidth(), image.getHeight(), xFrom, yFrom, w, h))
  1579                 MsgRepository.IMAGE_EXCEPTION_INVALID_REGION);
  1045             {
  1580         }
  1046                 throw new IllegalArgumentException(
  1581 
  1047                     MsgRepository.IMAGE_EXCEPTION_INVALID_REGION);
  1582         // Arrange vertical alignments
  1048             }
  1583         int destY = yTo;
  1049 
  1584         if(isFlag(anch, Graphics.BOTTOM))
  1050             // Arrange vertical alignments
  1585         {
  1051             int destY = yTo;
  1586             destY = yTo - h;
  1052             if(isFlag(anch, Graphics.BOTTOM))
  1587         }
  1053             {
  1588         if(isFlag(anch, Graphics.VCENTER))
  1054                 destY = yTo - h;
  1589         {
  1055             }
  1590             destY = yTo - h / 2;
  1056             if(isFlag(anch, Graphics.VCENTER))
  1591         }
  1057             {
  1592 
  1058                 destY = yTo - h / 2;
  1593         // Arrange horizontal alignments
  1059             }
  1594         int destX = xTo;
  1060 
  1595         if(isFlag(anch, Graphics.RIGHT))
  1061             // Arrange horizontal alignments
  1596         {
  1062             int destX = xTo;
  1597             destX = xTo - w;
  1063             if(isFlag(anch, Graphics.RIGHT))
  1598         }
  1064             {
  1599         if(isFlag(anch, Graphics.HCENTER))
  1065                 destX = xTo - w;
  1600         {
  1066             }
  1601             destX = xTo - w / 2;
  1067             if(isFlag(anch, Graphics.HCENTER))
  1602         }
  1068             {
  1603 
  1069                 destX = xTo - w / 2;
  1604         synchronized(flushLock)
  1070             }
  1605         {
  1071             graphicsBuffer.copyArea(xFrom, yFrom, w, h, destX, destY, this);
  1606             if(!buffered)
       
  1607             {
       
  1608                 final int x1 = xFrom;
       
  1609                 final int y1 = yFrom;
       
  1610                 final int width = w;
       
  1611                 final int height = h;
       
  1612                 final int x2 = destX;
       
  1613                 final int y2 = destY;
       
  1614 
       
  1615                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
  1616                 {
       
  1617                     public void run()
       
  1618                     {
       
  1619                         gc.copyArea(x1, y1, width, height, x2, y2);
       
  1620                     }
       
  1621                 });
       
  1622             }
       
  1623             else
       
  1624             {
       
  1625                 gc.copyArea(xFrom, yFrom, w, h, destX, destY);
       
  1626             }
       
  1627         }
  1072         }
  1628     }
  1073     }
  1629 
  1074 
  1630     /**
  1075     /**
  1631      * Renders a portion of source image with possible transforms.
  1076      * Renders a portion of source image with possible transforms.
  1656         if(srcImage == null)
  1101         if(srcImage == null)
  1657         {
  1102         {
  1658             throw new NullPointerException(
  1103             throw new NullPointerException(
  1659                 MsgRepository.IMAGE_EXCEPTION_IS_NULL);
  1104                 MsgRepository.IMAGE_EXCEPTION_IS_NULL);
  1660         }
  1105         }
  1661         if(srcImage == parentImage)
  1106         if(srcImage == graphicsBuffer.getHost())
  1662         {
  1107         {
  1663             throw new IllegalArgumentException(
  1108             throw new IllegalArgumentException(
  1664                 MsgRepository.GRAPHICS_EXCEPTION_SAME_SOURCE_AND_DESTINATION);
  1109                 MsgRepository.GRAPHICS_EXCEPTION_SAME_SOURCE_AND_DESTINATION);
  1665         }
  1110         }
  1666         if(!javax.microedition.lcdui.Image.validateTransform(transform))
  1111         if(!javax.microedition.lcdui.Image.validateTransform(transform))
  1677                 srcImage.getHeight(), xSrc, ySrc, width, height))
  1122                 srcImage.getHeight(), xSrc, ySrc, width, height))
  1678         {
  1123         {
  1679             throw new IllegalArgumentException(
  1124             throw new IllegalArgumentException(
  1680                 MsgRepository.IMAGE_EXCEPTION_INVALID_REGION);
  1125                 MsgRepository.IMAGE_EXCEPTION_INVALID_REGION);
  1681         }
  1126         }
  1682 
  1127         synchronized(graphicsBuffer)
  1683         // Arrange vertical alignments
  1128         {
  1684         int y = yDst;
  1129             // Arrange vertical alignments
  1685         if(isFlag(anch, Graphics.VCENTER))
  1130             int y = yDst;
  1686         {
  1131             if(isFlag(anch, Graphics.VCENTER))
  1687             y = yDst - srcImage.getHeight() / 2;
  1132             {
  1688         }
  1133                 y = yDst - srcImage.getHeight() / 2;
  1689         if(isFlag(anch, Graphics.BOTTOM))
  1134             }
  1690         {
  1135             if(isFlag(anch, Graphics.BOTTOM))
  1691             y = yDst - srcImage.getHeight();
  1136             {
  1692         }
  1137                 y = yDst - srcImage.getHeight();
  1693 
  1138             }
  1694         // Arrange horizontal alignments
  1139 
  1695         int x = xDst;
  1140             // Arrange horizontal alignments
  1696         if(isFlag(anch, Graphics.HCENTER))
  1141             int x = xDst;
  1697         {
  1142             if(isFlag(anch, Graphics.HCENTER))
  1698             x = xDst - srcImage.getWidth() / 2;
  1143             {
  1699         }
  1144                 x = xDst - srcImage.getWidth() / 2;
  1700         if(isFlag(anch, Graphics.RIGHT))
  1145             }
  1701         {
  1146             if(isFlag(anch, Graphics.RIGHT))
  1702             x = xDst - srcImage.getWidth();
  1147             {
  1703         }
  1148                 x = xDst - srcImage.getWidth();
  1704 
  1149             }
  1705         final int gcTransform = Image.getCgTransformValue(transform);
  1150             final int gcTransform = Image.getCgTransformValue(transform);
  1706         synchronized(flushLock)
  1151             synchronized(srcImage.graphicsBuffer)
  1707         {
  1152             {
  1708             if(!buffered)
  1153                 srcImage.sync();
  1709             {
  1154                 graphicsBuffer.drawImage(Internal_GfxPackageSupport.getImage(Image.getESWTImage(srcImage)),
  1710                 final int localXDst = x;
  1155                                          x, y, width, height, xSrc, ySrc, width, height, gcTransform, this);
  1711                 final int localYDst = y;
       
  1712                 final int localW = width;
       
  1713                 final int localH = height;
       
  1714                 final int localXSrc = xSrc;
       
  1715                 final int localYSrc = ySrc;
       
  1716                 final org.eclipse.swt.internal.qt.graphics.Image localImage =
       
  1717                     Internal_GfxPackageSupport.getImage(Image.getESWTImage(srcImage));
       
  1718                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
  1719                 {
       
  1720                     public void run()
       
  1721                     {
       
  1722                         gc.drawImage(localImage, localXDst, localYDst, localW, localH, localXSrc, localYSrc, localW, localH, gcTransform);
       
  1723                     }
       
  1724                 });
       
  1725             }
       
  1726             else
       
  1727             {
       
  1728                 gc.drawImage(Internal_GfxPackageSupport.getImage(Image.getESWTImage(srcImage)),
       
  1729                              x, y, width, height, xSrc, ySrc, width, height, gcTransform);
       
  1730             }
  1156             }
  1731         }
  1157         }
  1732     }
  1158     }
  1733 
  1159 
  1734     /**
  1160     /**
  1742         }
  1168         }
  1743         return directGraphics;
  1169         return directGraphics;
  1744     }
  1170     }
  1745 
  1171 
  1746     /**
  1172     /**
  1747      * Return native graphic context.
  1173      * Maps stroke style constant from values used by
  1748      */
  1174      * Graphics to values defined in GraphicsContext
  1749     GraphicsContext getGc()
  1175      */
  1750     {
  1176     static int mapStrokeStyle(int strokeStyle)
  1751         return gc;
  1177     {
       
  1178         if(strokeStyle == SOLID)
       
  1179         {
       
  1180             return GraphicsContext.STROKE_SOLID;
       
  1181         }
       
  1182         else if(strokeStyle == DOTTED)
       
  1183         {
       
  1184             return GraphicsContext.STROKE_DOT;
       
  1185         }
       
  1186         return INVALID_STROKE_STYLE;
       
  1187     }
       
  1188 
       
  1189     //
       
  1190     // Nokia UI API support
       
  1191     //
       
  1192     void drawRGB(int[] rgb, int offset, int scanlength, int x, int y, int w,
       
  1193                  int h, boolean processAlpha, int manipulation)
       
  1194     {
       
  1195         synchronized(graphicsBuffer)
       
  1196         {
       
  1197             graphicsBuffer.drawRGB(rgb, offset, scanlength, x, y, w, h, processAlpha, manipulation, this);
       
  1198         }
       
  1199     }
       
  1200 
       
  1201     void drawRGB(byte[] rgb, byte[] transparencyMask, int offset, int scanlength, int x, int y, int w,
       
  1202                  int h, int manipulation, int format)
       
  1203     {
       
  1204         synchronized(graphicsBuffer)
       
  1205         {
       
  1206             graphicsBuffer.drawRGB(rgb, transparencyMask, offset, scanlength, x, y, w, h, manipulation, format, this);
       
  1207         }
       
  1208     }
       
  1209 
       
  1210     void drawRGB(short[] rgb, int offset, int scanlength, int x, int y, int w,
       
  1211                  int h, boolean processAlpha, int manipulation, int format)
       
  1212     {
       
  1213         synchronized(graphicsBuffer)
       
  1214         {
       
  1215             graphicsBuffer.drawRGB(rgb, offset, scanlength, x, y, w, h, processAlpha, manipulation, format, this);
       
  1216         }
       
  1217     }
       
  1218 
       
  1219     void drawPolygon(int[] points)
       
  1220     {
       
  1221         synchronized(graphicsBuffer)
       
  1222         {
       
  1223             graphicsBuffer.drawPolygon(points, this);
       
  1224         }
       
  1225     }
       
  1226 
       
  1227     void fillPolygon(int[] points)
       
  1228     {
       
  1229         synchronized(graphicsBuffer)
       
  1230         {
       
  1231             graphicsBuffer.fillPolygon(points, this);
       
  1232         }
       
  1233     }
       
  1234 
       
  1235     void setARGBColor(int argb)
       
  1236     {
       
  1237         synchronized(graphicsBuffer)
       
  1238         {
       
  1239             graphicsBuffer.setARGBColor(argb, this);
       
  1240             currentColor = argb;
       
  1241         }
       
  1242 
  1752     }
  1243     }
  1753 
  1244 
  1754 }
  1245 }