2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
42 #include <QLinearGradient>
44 #include <QPaintDevice>
45 #include <QPainterPath>
48 #include <gnu_java_awt_peer_qt_QtGraphics.h>
49 #include "nativewrapper.h"
51 #include "qtstrings.h"
52 #include "qtcomponent.h"
53 #include "qtgraphics.h"
56 // Constants from java.awt.AlphaComposite
70 GraphicsPainter
*getPainter( JNIEnv
*env
, jobject obj
)
72 jclass cls
= env
->GetObjectClass( obj
);
73 jfieldID field
= env
->GetFieldID( cls
, "nativeObject", "J" );
74 return (GraphicsPainter
*)env
->GetLongField( obj
, field
);
77 static void setNativePtr( JNIEnv
*env
, jobject obj
, void *value
)
79 jlong longValue
= (jlong
) value
;
80 jclass cls
= env
->GetObjectClass( obj
);
81 jfieldID field
= env
->GetFieldID( cls
, "nativeObject", "J" );
82 env
->SetLongField( obj
, field
, longValue
);
85 static jobject
getToolkit( JNIEnv
*env
, jobject obj
)
87 jclass cls
= env
->FindClass( "gnu/java/awt/peer/qt/QtGraphics" );
89 jfieldID field
= env
->GetFieldID( cls
, "toolkit",
90 "Lgnu/java/awt/peer/qt/QtToolkit;" );
91 return env
->GetObjectField( obj
, field
);
94 ///////////////////////// JNI methods ////////////////////////////////
97 * Clones the parent QPainter object.
99 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_cloneNativeContext
100 (JNIEnv
*env
, jobject obj
, jobject parent
)
102 GraphicsPainter
*painter
= (GraphicsPainter
*)getPainter( env
, parent
);
104 QPainter
*newPainter
= new GraphicsPainter( painter
->device() );
105 assert( newPainter
);
106 setNativePtr(env
, obj
, newPainter
);
110 * Start of JNI methods
112 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_initImage
113 (JNIEnv
*env
, jobject obj
, jobject image
)
115 QImage
*im
= getQtImage( env
, image
);
117 QPainter
*painter
= new GraphicsPainter( im
);
119 setNativePtr(env
, obj
, painter
);
120 painter
->setRenderHint(QPainter::TextAntialiasing
);
121 painter
->setRenderHint(QPainter::Antialiasing
);
122 painter
->setRenderHint(QPainter::SmoothPixmapTransform
);
126 * Start of JNI methods
128 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_initVolatileImage
129 (JNIEnv
*env
, jobject obj
, jobject image
)
131 QPixmap
*im
= getQtVolatileImage( env
, image
);
133 QPainter
*painter
= new GraphicsPainter( im
);
135 setNativePtr(env
, obj
, painter
);
136 painter
->setRenderHint(QPainter::TextAntialiasing
);
137 painter
->setRenderHint(QPainter::Antialiasing
);
138 painter
->setRenderHint(QPainter::SmoothPixmapTransform
);
142 * Deletes the QPainter
144 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_delete
145 (JNIEnv
*env
, jobject obj
)
147 GraphicsPainter
*painter
= (GraphicsPainter
*)getPainter( env
, obj
);
148 setNativePtr( env
, obj
, NULL
);
151 if( painter
->isActive() )
157 ///////////////////////////////////////////////////////////
159 * Sets the clip to a path.
161 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_setClipNative
162 (JNIEnv
*env
, jobject obj
, jobject path
)
164 QPainter
*painter
= getPainter( env
, obj
);
166 QPainterPath
*pp
= (QPainterPath
*)getNativeObject( env
, path
);
168 painter
->setClipPath( *pp
);
172 * Sets the clip to a rectangle.
174 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_setClipRectNative
175 (JNIEnv
*env
, jobject obj
, jint x
, jint y
, jint w
, jint h
)
177 QPainter
*painter
= getPainter( env
, obj
);
179 painter
->setClipRect( x
, y
, w
, h
);
183 * Intersects a shape with the current clip.
185 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_intersectClipNative
186 (JNIEnv
*env
, jobject obj
, jobject path
)
188 QPainter
*painter
= getPainter( env
, obj
);
190 QPainterPath
*pp
= (QPainterPath
*)getNativeObject( env
, path
);
192 painter
->setClipPath( *pp
, Qt::IntersectClip
);
196 * Intersect a rectangle with the current clip.
198 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_intersectClipRectNative
199 (JNIEnv
*env
, jobject obj
, jint x
, jint y
, jint w
, jint h
)
201 QPainter
*painter
= getPainter( env
, obj
);
203 painter
->setClipRect( x
, y
, w
, h
, Qt::IntersectClip
);
207 * Returns a QPainterPath object with the clip path of this painter.
209 JNIEXPORT jobject JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_getClipNative
210 (JNIEnv
*env
, jobject obj
)
212 QPainter
*painter
= getPainter( env
, obj
);
214 jclass cls
= env
->FindClass("gnu/java/awt/peer/qt/QPainterPath");
215 jmethodID method
= env
->GetMethodID(cls
, "<init>", "()V");
217 jobject ppo
= env
->NewObject(cls
, method
);
218 QPainterPath qpp
= painter
->clipPath();
219 setNativeObject(env
, ppo
, &qpp
);
221 env
->DeleteLocalRef( cls
);
226 * Returns a Rectangle with the bounds of this painters clip path.
228 JNIEXPORT jobject JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_getClipBounds
229 (JNIEnv
*env
, jobject obj
)
231 QPainter
*painter
= getPainter( env
, obj
);
234 painter
->clipPath().boundingRect().getRect(&x
, &y
, &w
, &h
);
236 jclass cls
= env
->FindClass("java/awt/Rectangle");
237 assert( cls
!= NULL
);
238 jmethodID mid
= env
->GetMethodID(cls
, "<init>", "(IIII)V");
239 assert( mid
!= NULL
);
242 values
[0].i
= (jint
) x
;
243 values
[1].i
= (jint
) y
;
244 values
[2].i
= (jint
) w
;
245 values
[3].i
= (jint
) h
;
247 return env
->NewObjectA(cls
, mid
, values
);
250 ///////////////////////// Color stuff ////////////////////////
254 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_setColor
255 (JNIEnv
*env
, jobject obj
, jint r
, jint g
, jint b
, jint alpha
)
257 GraphicsPainter
*painter
= (GraphicsPainter
*)getPainter( env
, obj
);
259 painter
->currentPen
->setColor( QColor(r
, g
, b
, alpha
) );
260 painter
->setPen( *painter
->currentPen
);
261 painter
->currentBrush
= new QBrush( QColor(r
, g
, b
, alpha
) );
262 painter
->setBrush( *painter
->currentBrush
);
263 painter
->currentColor
= new QColor(r
, g
, b
, alpha
);
266 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_setAlphaNative
267 (JNIEnv
*env
, jobject obj
, jdouble alpha
)
269 GraphicsPainter
*painter
= (GraphicsPainter
*)getPainter( env
, obj
);
272 QColor c
= painter
->currentPen
->color();
273 c
.setAlphaF( (qreal
)alpha
);
274 painter
->currentPen
->setColor(c
);
276 c
= painter
->currentBrush
->color();
277 c
.setAlphaF( (qreal
)alpha
);
278 painter
->currentBrush
->setColor( c
);
282 * Class: gnu_java_awt_peer_qt_QtGraphics
284 * Signature: (Lgnu/java/awt/peer/qt/QPainterPath;)V
286 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_drawNative
287 (JNIEnv
*env
, jobject obj
, jobject path
)
289 GraphicsPainter
*painter
= (GraphicsPainter
*)getPainter( env
, obj
);
291 QPainterPath
*pp
= (QPainterPath
*)getNativeObject( env
, path
);
293 painter
->setPen( *painter
->currentPen
);
294 painter
->setBrush( Qt::NoBrush
);
295 painter
->drawPath( *pp
);
299 * Class: gnu_java_awt_peer_qt_QtGraphics
301 * Signature: (Lgnu/java/awt/peer/qt/QPainterPath;)V
303 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_fillNative
304 (JNIEnv
*env
, jobject obj
, jobject path
)
306 GraphicsPainter
*painter
= (GraphicsPainter
*)getPainter( env
, obj
);
308 QPainterPath
*pp
= (QPainterPath
*)getNativeObject( env
, path
);
311 painter
->setPen(Qt::NoPen
);
312 painter
->setBrush( *painter
->currentBrush
);
313 painter
->drawPath( *pp
);
319 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_drawStringNative
320 (JNIEnv
*env
, jobject obj
, jstring str
, jdouble x
, jdouble y
)
322 GraphicsPainter
*painter
= getPainter( env
, obj
);
324 QString
*qStr
= getQString(env
, str
);
325 painter
->setBrush( Qt::NoBrush
);
326 painter
->setPen( *painter
->currentPen
);
327 painter
->drawText(QPointF( (qreal
)x
, (qreal
)y
), *qStr
);
332 * Sets the native stroke
334 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_setNativeStroke
335 (JNIEnv
*env
, jobject obj
, jobject stroke
)
337 GraphicsPainter
*painter
= (GraphicsPainter
*)getPainter( env
, obj
);
339 QPen
*pen
= (QPen
*)getNativeObject(env
, stroke
);
341 painter
->currentPen
= new QPen( *pen
);
342 painter
->setPen( *painter
->currentPen
);
348 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_setQtTransform
349 (JNIEnv
*env
, jobject obj
, jobject matrix
)
351 QPainter
*painter
= getPainter( env
, obj
);
353 QMatrix
*m
= (QMatrix
*)getNativeObject( env
, matrix
);
355 painter
->setMatrix( *m
);
361 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_setFontNative
362 (JNIEnv
*env
, jobject obj
, jobject fontpeer
)
364 QPainter
*painter
= getPainter( env
, obj
);
366 QFont
*font
= (QFont
*) getFont( env
, fontpeer
);
368 painter
->setFont( *font
);
372 * Sets Porter-Duff compositing.
374 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_setNativeComposite
375 (JNIEnv
*env
, jobject obj
, jint compositeMode
)
377 QPainter
*painter
= getPainter( env
, obj
);
379 QPainter::CompositionMode mode
;
381 switch( compositeMode
)
384 mode
= QPainter::CompositionMode_Clear
;
387 mode
= QPainter::CompositionMode_Source
;
390 mode
= QPainter::CompositionMode_Destination
;
393 mode
= QPainter::CompositionMode_SourceOver
;
396 mode
= QPainter::CompositionMode_DestinationOver
;
399 mode
= QPainter::CompositionMode_SourceIn
;
402 mode
= QPainter::CompositionMode_DestinationIn
;
405 mode
= QPainter::CompositionMode_SourceOut
;
408 mode
= QPainter::CompositionMode_DestinationOut
;
411 mode
= QPainter::CompositionMode_SourceAtop
;
414 mode
= QPainter::CompositionMode_DestinationAtop
;
417 mode
= QPainter::CompositionMode_Xor
;
420 painter
->setCompositionMode( mode
);
424 * Sets the current brush to a linear gradient.
426 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_setLinearGradient
427 (JNIEnv
*env
, jobject obj
, jint r1
, jint g1
, jint b1
, jint r2
, jint g2
,
428 jint b2
, jdouble x1
, jdouble y1
, jdouble x2
, jdouble y2
, jboolean cyclic
)
430 GraphicsPainter
*painter
= getPainter( env
, obj
);
432 QLinearGradient
*lg
= new QLinearGradient(QPointF( (qreal
)x1
, (qreal
)y1
),
433 QPointF( (qreal
)x2
, (qreal
)y2
) );
434 lg
->setColorAt( (qreal
)0.0, QColor(r1
, g1
, b1
) );
435 lg
->setColorAt( (qreal
)1.0, QColor(r2
, g2
, b2
) );
436 if( cyclic
== JNI_TRUE
)
437 lg
->setSpread( QGradient::ReflectSpread
);
439 lg
->setSpread( QGradient::PadSpread
);
440 painter
->currentBrush
= new QBrush( *lg
);
446 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_fill3DRect
447 (JNIEnv
*env
, jobject obj
, jint x
, jint y
, jint w
, jint h
, jboolean raised
)
449 GraphicsPainter
*painter
= getPainter( env
, obj
);
451 // FIXME: Adjust colors
452 painter
->fillRect ( x
, y
, w
, h
, QBrush( *painter
->currentColor
) );
453 QPen
*p
= new QPen( *painter
->currentColor
);
455 painter
->setPen( *p
);
456 painter
->drawLine( x
+ w
, y
, x
+ w
, y
+ h
);
457 painter
->drawLine( x
, y
+ h
, x
+ w
, y
+ h
);
462 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_draw3DRect
463 (JNIEnv
*env
, jobject obj
, jint x
, jint y
, jint w
, jint h
, jboolean raised
)
465 GraphicsPainter
*painter
= getPainter( env
, obj
);
467 // FIXME: Adjust colors
468 QPen
*p
= new QPen( *painter
->currentColor
);
470 painter
->setPen( *p
);
471 painter
->drawLine( x
, y
, x
+ w
, y
);
472 painter
->drawLine( x
, y
, x
, y
+ h
);
473 painter
->drawLine( x
+ w
, y
, x
+ w
, y
+ h
);
474 painter
->drawLine( x
, y
+ h
, x
+ w
, y
+ h
);