qtgraphics.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /* qtgraphics.cpp --
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. #include <assert.h>
  32. #include <jni.h>
  33. #include <QPainter>
  34. #include <QBrush>
  35. #include <QLinearGradient>
  36. #include <QPen>
  37. #include <QPaintDevice>
  38. #include <QPainterPath>
  39. #include <QImage>
  40. #include <QColor>
  41. #include <gnu_java_awt_peer_qt_QtGraphics.h>
  42. #include "nativewrapper.h"
  43. #include "qtimage.h"
  44. #include "qtstrings.h"
  45. #include "qtcomponent.h"
  46. #include "qtgraphics.h"
  47. #include "qtfont.h"
  48. // Constants from java.awt.AlphaComposite
  49. #define CLEAR 1
  50. #define SRC 2
  51. #define DST 9
  52. #define SRC_OVER 3
  53. #define DST_OVER 4
  54. #define SRC_IN 5
  55. #define DST_IN 6
  56. #define SRC_OUT 7
  57. #define DST_OUT 8
  58. #define SRC_ATOP 10
  59. #define DST_ATOP 11
  60. #define XOR 12
  61. GraphicsPainter *getPainter( JNIEnv *env, jobject obj )
  62. {
  63. jclass cls = env->GetObjectClass( obj );
  64. jfieldID field = env->GetFieldID( cls, "nativeObject", "J" );
  65. return (GraphicsPainter *)env->GetLongField( obj, field );
  66. }
  67. static void setNativePtr( JNIEnv *env, jobject obj, void *value )
  68. {
  69. jlong longValue = (jlong) value;
  70. jclass cls = env->GetObjectClass( obj );
  71. jfieldID field = env->GetFieldID( cls, "nativeObject", "J" );
  72. env->SetLongField( obj, field, longValue );
  73. }
  74. static jobject getToolkit( JNIEnv *env, jobject obj )
  75. {
  76. jclass cls = env->FindClass( "gnu/java/awt/peer/qt/QtGraphics" );
  77. jfieldID field = env->GetFieldID( cls, "toolkit",
  78. "Lgnu/java/awt/peer/qt/QtToolkit;" );
  79. return env->GetObjectField( obj, field );
  80. }
  81. ///////////////////////// JNI methods ////////////////////////////////
  82. /**
  83. * Clones the parent QPainter object.
  84. */
  85. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_cloneNativeContext
  86. (JNIEnv *env, jobject obj, jobject parent)
  87. {
  88. GraphicsPainter *painter = (GraphicsPainter *)getPainter( env, parent );
  89. assert( painter );
  90. QPainter *newPainter = new GraphicsPainter( painter->device() );
  91. assert( newPainter );
  92. setNativePtr(env, obj, newPainter);
  93. }
  94. /*
  95. * Start of JNI methods
  96. */
  97. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_initImage
  98. (JNIEnv *env, jobject obj, jobject image)
  99. {
  100. QImage *im = getQtImage( env, image );
  101. assert( im );
  102. QPainter *painter = new GraphicsPainter( im );
  103. assert( painter );
  104. setNativePtr(env, obj, painter);
  105. painter->setRenderHint(QPainter::TextAntialiasing);
  106. painter->setRenderHint(QPainter::Antialiasing);
  107. painter->setRenderHint(QPainter::SmoothPixmapTransform);
  108. }
  109. /*
  110. * Start of JNI methods
  111. */
  112. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_initVolatileImage
  113. (JNIEnv *env, jobject obj, jobject image)
  114. {
  115. QPixmap *im = getQtVolatileImage( env, image );
  116. assert( im );
  117. QPainter *painter = new GraphicsPainter( im );
  118. assert( painter );
  119. setNativePtr(env, obj, painter);
  120. painter->setRenderHint(QPainter::TextAntialiasing);
  121. painter->setRenderHint(QPainter::Antialiasing);
  122. painter->setRenderHint(QPainter::SmoothPixmapTransform);
  123. }
  124. /**
  125. * Deletes the QPainter
  126. */
  127. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_delete
  128. (JNIEnv *env, jobject obj)
  129. {
  130. GraphicsPainter *painter = (GraphicsPainter *)getPainter( env, obj );
  131. setNativePtr( env, obj, NULL );
  132. if( painter )
  133. {
  134. if( painter->isActive() )
  135. painter->end();
  136. delete painter;
  137. }
  138. }
  139. ///////////////////////////////////////////////////////////
  140. /*
  141. * Sets the clip to a path.
  142. */
  143. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_setClipNative
  144. (JNIEnv *env, jobject obj, jobject path)
  145. {
  146. QPainter *painter = getPainter( env, obj );
  147. assert( painter );
  148. QPainterPath *pp = (QPainterPath *)getNativeObject( env, path );
  149. assert( pp );
  150. painter->setClipPath( *pp );
  151. }
  152. /*
  153. * Sets the clip to a rectangle.
  154. */
  155. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_setClipRectNative
  156. (JNIEnv *env, jobject obj, jint x, jint y, jint w, jint h)
  157. {
  158. QPainter *painter = getPainter( env, obj );
  159. assert( painter );
  160. painter->setClipRect( x, y, w, h );
  161. }
  162. /*
  163. * Intersects a shape with the current clip.
  164. */
  165. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_intersectClipNative
  166. (JNIEnv *env, jobject obj, jobject path)
  167. {
  168. QPainter *painter = getPainter( env, obj );
  169. assert( painter );
  170. QPainterPath *pp = (QPainterPath *)getNativeObject( env, path );
  171. assert( pp );
  172. painter->setClipPath( *pp, Qt::IntersectClip );
  173. }
  174. /*
  175. * Intersect a rectangle with the current clip.
  176. */
  177. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_intersectClipRectNative
  178. (JNIEnv *env, jobject obj, jint x, jint y, jint w, jint h)
  179. {
  180. QPainter *painter = getPainter( env, obj );
  181. assert( painter );
  182. painter->setClipRect( x, y, w, h, Qt::IntersectClip );
  183. }
  184. /*
  185. * Returns a QPainterPath object with the clip path of this painter.
  186. */
  187. JNIEXPORT jobject JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_getClipNative
  188. (JNIEnv *env, jobject obj)
  189. {
  190. QPainter *painter = getPainter( env, obj );
  191. assert( painter );
  192. jclass cls = env->FindClass("gnu/java/awt/peer/qt/QPainterPath");
  193. jmethodID method = env->GetMethodID(cls, "<init>", "()V");
  194. jobject ppo = env->NewObject(cls, method);
  195. QPainterPath qpp = painter->clipPath();
  196. setNativeObject(env, ppo, &qpp);
  197. env->DeleteLocalRef( cls );
  198. return ppo;
  199. }
  200. /*
  201. * Returns a Rectangle with the bounds of this painters clip path.
  202. */
  203. JNIEXPORT jobject JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_getClipBounds
  204. (JNIEnv *env, jobject obj)
  205. {
  206. QPainter *painter = getPainter( env, obj );
  207. assert( painter );
  208. qreal x, y, w, h;
  209. painter->clipPath().boundingRect().getRect(&x, &y, &w, &h);
  210. jclass cls = env->FindClass("java/awt/Rectangle");
  211. assert( cls != NULL);
  212. jmethodID mid = env->GetMethodID(cls, "<init>", "(IIII)V");
  213. assert( mid != NULL);
  214. jvalue values[4];
  215. values[0].i = (jint) x;
  216. values[1].i = (jint) y;
  217. values[2].i = (jint) w;
  218. values[3].i = (jint) h;
  219. return env->NewObjectA(cls, mid, values);
  220. }
  221. ///////////////////////// Color stuff ////////////////////////
  222. /**
  223. *
  224. */
  225. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_setColor
  226. (JNIEnv *env, jobject obj, jint r, jint g, jint b, jint alpha)
  227. {
  228. GraphicsPainter *painter = (GraphicsPainter *)getPainter( env, obj );
  229. assert( painter );
  230. painter->currentPen->setColor( QColor(r, g, b, alpha) );
  231. painter->setPen( *painter->currentPen );
  232. painter->currentBrush = new QBrush( QColor(r, g, b, alpha) );
  233. painter->setBrush( *painter->currentBrush );
  234. painter->currentColor = new QColor(r, g, b, alpha);
  235. }
  236. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_setAlphaNative
  237. (JNIEnv *env, jobject obj, jdouble alpha)
  238. {
  239. GraphicsPainter *painter = (GraphicsPainter *)getPainter( env, obj );
  240. assert( painter );
  241. QColor c = painter->currentPen->color();
  242. c.setAlphaF( (qreal)alpha );
  243. painter->currentPen->setColor(c);
  244. c = painter->currentBrush->color();
  245. c.setAlphaF( (qreal)alpha );
  246. painter->currentBrush->setColor( c );
  247. }
  248. /*
  249. * Class: gnu_java_awt_peer_qt_QtGraphics
  250. * Method: drawNative
  251. * Signature: (Lgnu/java/awt/peer/qt/QPainterPath;)V
  252. */
  253. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_drawNative
  254. (JNIEnv *env, jobject obj, jobject path)
  255. {
  256. GraphicsPainter *painter = (GraphicsPainter *)getPainter( env, obj );
  257. assert( painter );
  258. QPainterPath *pp = (QPainterPath *)getNativeObject( env, path );
  259. assert( pp );
  260. painter->setPen( *painter->currentPen );
  261. painter->setBrush( Qt::NoBrush );
  262. painter->drawPath( *pp );
  263. }
  264. /*
  265. * Class: gnu_java_awt_peer_qt_QtGraphics
  266. * Method: fillNative
  267. * Signature: (Lgnu/java/awt/peer/qt/QPainterPath;)V
  268. */
  269. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_fillNative
  270. (JNIEnv *env, jobject obj, jobject path)
  271. {
  272. GraphicsPainter *painter = (GraphicsPainter *)getPainter( env, obj );
  273. assert( painter );
  274. QPainterPath *pp = (QPainterPath *)getNativeObject( env, path );
  275. assert( pp );
  276. painter->setPen(Qt::NoPen);
  277. painter->setBrush( *painter->currentBrush );
  278. painter->drawPath( *pp );
  279. }
  280. /**
  281. * Draws a string.
  282. */
  283. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_drawStringNative
  284. (JNIEnv *env, jobject obj, jstring str, jdouble x, jdouble y)
  285. {
  286. GraphicsPainter *painter = getPainter( env, obj );
  287. assert( painter );
  288. QString *qStr = getQString(env, str);
  289. painter->setBrush( Qt::NoBrush );
  290. painter->setPen( *painter->currentPen );
  291. painter->drawText(QPointF( (qreal)x, (qreal)y ), *qStr);
  292. delete qStr;
  293. }
  294. /*
  295. * Sets the native stroke
  296. */
  297. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_setNativeStroke
  298. (JNIEnv *env, jobject obj, jobject stroke)
  299. {
  300. GraphicsPainter *painter = (GraphicsPainter *)getPainter( env, obj );
  301. assert( painter );
  302. QPen *pen = (QPen *)getNativeObject(env, stroke);
  303. assert( pen );
  304. painter->currentPen = new QPen( *pen );
  305. painter->setPen( *painter->currentPen );
  306. }
  307. /*
  308. * Sets the transform
  309. */
  310. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_setQtTransform
  311. (JNIEnv *env, jobject obj, jobject matrix)
  312. {
  313. QPainter *painter = getPainter( env, obj );
  314. assert( painter );
  315. QMatrix *m = (QMatrix *)getNativeObject( env, matrix );
  316. assert( m );
  317. painter->setMatrix( *m );
  318. }
  319. /**
  320. * Set the font
  321. */
  322. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_setFontNative
  323. (JNIEnv *env, jobject obj, jobject fontpeer)
  324. {
  325. QPainter *painter = getPainter( env, obj );
  326. assert( painter );
  327. QFont *font = (QFont *) getFont( env, fontpeer );
  328. assert( font );
  329. painter->setFont( *font );
  330. }
  331. /*
  332. * Sets Porter-Duff compositing.
  333. */
  334. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_setNativeComposite
  335. (JNIEnv *env, jobject obj, jint compositeMode)
  336. {
  337. QPainter *painter = getPainter( env, obj );
  338. assert( painter );
  339. QPainter::CompositionMode mode;
  340. switch( compositeMode )
  341. {
  342. case CLEAR:
  343. mode = QPainter::CompositionMode_Clear;
  344. break;
  345. case SRC:
  346. mode = QPainter::CompositionMode_Source;
  347. break;
  348. case DST:
  349. mode = QPainter::CompositionMode_Destination;
  350. break;
  351. case SRC_OVER:
  352. mode = QPainter::CompositionMode_SourceOver;
  353. break;
  354. case DST_OVER:
  355. mode = QPainter::CompositionMode_DestinationOver;
  356. break;
  357. case SRC_IN:
  358. mode = QPainter::CompositionMode_SourceIn;
  359. break;
  360. case DST_IN:
  361. mode = QPainter::CompositionMode_DestinationIn;
  362. break;
  363. case SRC_OUT:
  364. mode = QPainter::CompositionMode_SourceOut;
  365. break;
  366. case DST_OUT:
  367. mode = QPainter::CompositionMode_DestinationOut;
  368. break;
  369. case SRC_ATOP:
  370. mode = QPainter::CompositionMode_SourceAtop;
  371. break;
  372. case DST_ATOP:
  373. mode = QPainter::CompositionMode_DestinationAtop;
  374. break;
  375. case XOR:
  376. mode = QPainter::CompositionMode_Xor;
  377. break;
  378. }
  379. painter->setCompositionMode( mode );
  380. }
  381. /**
  382. * Sets the current brush to a linear gradient.
  383. */
  384. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_setLinearGradient
  385. (JNIEnv *env, jobject obj, jint r1, jint g1, jint b1, jint r2, jint g2,
  386. jint b2, jdouble x1, jdouble y1, jdouble x2, jdouble y2, jboolean cyclic)
  387. {
  388. GraphicsPainter *painter = getPainter( env, obj );
  389. assert( painter );
  390. QLinearGradient *lg = new QLinearGradient(QPointF( (qreal)x1, (qreal)y1 ),
  391. QPointF( (qreal)x2, (qreal)y2 ) );
  392. lg->setColorAt( (qreal)0.0, QColor(r1, g1, b1) );
  393. lg->setColorAt( (qreal)1.0, QColor(r2, g2, b2) );
  394. if( cyclic == JNI_TRUE )
  395. lg->setSpread( QGradient::ReflectSpread );
  396. else
  397. lg->setSpread( QGradient::PadSpread );
  398. painter->currentBrush = new QBrush( *lg );
  399. delete lg;
  400. }
  401. /*
  402. */
  403. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_fill3DRect
  404. (JNIEnv *env, jobject obj, jint x, jint y, jint w, jint h, jboolean raised)
  405. {
  406. GraphicsPainter *painter = getPainter( env, obj );
  407. assert( painter );
  408. // FIXME: Adjust colors
  409. painter->fillRect ( x, y, w, h, QBrush( *painter->currentColor) );
  410. QPen *p = new QPen( *painter->currentColor );
  411. p->setWidth( 1 );
  412. painter->setPen( *p );
  413. painter->drawLine( x + w, y, x + w, y + h);
  414. painter->drawLine( x, y + h, x + w, y + h);
  415. }
  416. /*
  417. */
  418. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtGraphics_draw3DRect
  419. (JNIEnv *env, jobject obj, jint x, jint y, jint w, jint h, jboolean raised)
  420. {
  421. GraphicsPainter *painter = getPainter( env, obj );
  422. assert( painter );
  423. // FIXME: Adjust colors
  424. QPen *p = new QPen( *painter->currentColor );
  425. p->setWidth( 1 );
  426. painter->setPen( *p );
  427. painter->drawLine( x, y, x + w, y );
  428. painter->drawLine( x, y, x, y + h);
  429. painter->drawLine( x + w, y, x + w, y + h);
  430. painter->drawLine( x, y + h, x + w, y + h);
  431. }