qtcomponentpeer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* qtcomponentpeer.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 <QApplication>
  33. #include <QDesktopWidget>
  34. #include <QShowEvent>
  35. #include <QHideEvent>
  36. #include <QColor>
  37. #include <QCursor>
  38. #include <QWidget>
  39. #include <gnu_java_awt_peer_qt_QtComponentPeer.h>
  40. #include "qtcomponent.h"
  41. #include "componentevent.h"
  42. #include "qtfont.h"
  43. extern QApplication *qApplication;
  44. // Java Cursor types.
  45. #define DEFAULT_CURSOR 0
  46. #define CROSSHAIR_CURSOR 1
  47. #define TEXT_CURSOR 2
  48. #define WAIT_CURSOR 3
  49. #define SW_RESIZE_CURSOR 4
  50. #define SE_RESIZE_CURSOR 5
  51. #define NW_RESIZE_CURSOR 6
  52. #define NE_RESIZE_CURSOR 7
  53. #define N_RESIZE_CURSOR 8
  54. #define S_RESIZE_CURSOR 9
  55. #define W_RESIZE_CURSOR 10
  56. #define E_RESIZE_CURSOR 11
  57. #define HAND_CURSOR 12
  58. #define MOVE_CURSOR 13
  59. /**
  60. * Call back the init() method from the main thread.
  61. */
  62. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_callInit
  63. (JNIEnv *env, jobject obj)
  64. {
  65. mainThread->postEventToMain( new AWTInitEvent( env, obj ) );
  66. }
  67. /*
  68. * Generic disposal.
  69. */
  70. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_disposeNative
  71. (JNIEnv *env, jobject obj)
  72. {
  73. QWidget *widget = (QWidget *) getNativeObject( env, obj );
  74. setNativeObject(env, obj, NULL);
  75. mainThread->postEventToMain( new AWTDestroyEvent( widget ) );
  76. }
  77. /**
  78. * Returns the on-screen location of the component.
  79. */
  80. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_getLocationOnScreenNative
  81. (JNIEnv *env, jobject obj, jobject point)
  82. {
  83. QWidget *widget = (QWidget *) getNativeObject( env, obj );
  84. assert( widget );
  85. mainThread->postEventToMain( new AWTGetOriginEvent( widget, env, point) );
  86. }
  87. /*
  88. * Get the preferred/minimum size of the widget
  89. */
  90. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_getSizeNative
  91. (JNIEnv *env, jobject obj, jobject size, jboolean preferred)
  92. {
  93. QWidget *widget = (QWidget *) getNativeObject( env, obj );
  94. assert( widget );
  95. mainThread->postEventToMain
  96. (new GetSizeEvent( widget, env, size, (preferred == JNI_TRUE)));
  97. }
  98. /*
  99. */
  100. JNIEXPORT jboolean JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_isObscured
  101. (JNIEnv *env, jobject obj)
  102. {
  103. jboolean retVal;
  104. QWidget *widget = (QWidget *) getNativeObject( env, obj );
  105. assert( widget );
  106. retVal = (widget->isVisible() == TRUE) ? JNI_TRUE : JNI_FALSE;
  107. return retVal;
  108. }
  109. /*
  110. * Returns whether the widget is focusable or not.
  111. */
  112. JNIEXPORT jboolean JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_isFocusable
  113. (JNIEnv *env, jobject obj)
  114. {
  115. jboolean retVal;
  116. QWidget *widget = (QWidget *) getNativeObject( env, obj );
  117. assert( widget );
  118. retVal = (widget->focusPolicy() != Qt::NoFocus) ? JNI_TRUE : JNI_FALSE;
  119. return retVal;
  120. }
  121. /**
  122. * Requests the focus
  123. */
  124. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_requestFocus
  125. (JNIEnv *env, jobject obj)
  126. {
  127. QWidget *widget = (QWidget *) getNativeObject( env, obj );
  128. assert( widget );
  129. mainThread->postEventToMain( new AWTReqFocusEvent( widget ) );
  130. }
  131. /*
  132. * Sets the size and position. Important.
  133. */
  134. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_setBoundsNative
  135. (JNIEnv *env, jobject obj, jint x, jint y, jint width, jint height)
  136. {
  137. QWidget *widget = (QWidget *) getNativeObject( env, obj );
  138. assert( widget );
  139. mainThread->postEventToMain
  140. (new AWTResizeEvent( widget, x, y, width, height ) );
  141. }
  142. /*
  143. * Sets the mouse cursor
  144. */
  145. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_setCursor
  146. (JNIEnv *env, jobject obj, jint cursortype)
  147. {
  148. QWidget *widget = (QWidget *) getNativeObject( env, obj );
  149. assert( widget );
  150. Qt::CursorShape shape;
  151. switch(cursortype)
  152. {
  153. case CROSSHAIR_CURSOR:
  154. shape = Qt::CrossCursor;
  155. break;
  156. case W_RESIZE_CURSOR:
  157. case E_RESIZE_CURSOR:
  158. shape = Qt::SizeHorCursor;
  159. break;
  160. case N_RESIZE_CURSOR:
  161. case S_RESIZE_CURSOR:
  162. shape = Qt::SizeVerCursor;
  163. break;
  164. case HAND_CURSOR:
  165. shape = Qt::PointingHandCursor;
  166. break;
  167. case MOVE_CURSOR:
  168. shape = Qt::SizeAllCursor;
  169. break;
  170. case NE_RESIZE_CURSOR:
  171. case SW_RESIZE_CURSOR:
  172. shape = Qt::SizeBDiagCursor;
  173. break;
  174. case NW_RESIZE_CURSOR:
  175. case SE_RESIZE_CURSOR:
  176. shape = Qt::SizeFDiagCursor;
  177. break;
  178. case TEXT_CURSOR:
  179. shape = Qt::IBeamCursor;
  180. break;
  181. case WAIT_CURSOR:
  182. shape = Qt::WaitCursor;
  183. break;
  184. case DEFAULT_CURSOR:
  185. default:
  186. shape = Qt::ArrowCursor;
  187. break;
  188. }
  189. mainThread->postEventToMain( new AWTCursorEvent( widget, shape ) );
  190. }
  191. /*
  192. * Enable, disable.
  193. */
  194. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_setEnabled
  195. (JNIEnv *env, jobject obj, jboolean state)
  196. {
  197. QWidget *widget = (QWidget *) getNativeObject( env, obj );
  198. assert(widget != NULL);
  199. mainThread->postEventToMain( new AWTEnableEvent( widget, (state == JNI_TRUE) ) );
  200. }
  201. /**
  202. * Set the font
  203. */
  204. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_setFontNative
  205. (JNIEnv *env, jobject obj, jobject fontpeer)
  206. {
  207. QWidget *widget = (QWidget *) getNativeObject( env, obj );
  208. assert( widget );
  209. QFont *font = (QFont *) getFont( env, fontpeer );
  210. assert( font );
  211. mainThread->postEventToMain( new AWTFontEvent(widget, font) );
  212. }
  213. /*
  214. * Sets the back- or foreground color.
  215. */
  216. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_setGround
  217. (JNIEnv *env, jobject obj, jint r, jint g, jint b, jboolean isForeground)
  218. {
  219. QColor *color = new QColor(r, g, b);
  220. QWidget *widget = (QWidget *) getNativeObject( env, obj );
  221. assert(widget);
  222. mainThread->postEventToMain( new AWTBackgroundEvent(widget,
  223. (isForeground == JNI_TRUE),
  224. color) );
  225. }
  226. /*
  227. * Sets the visibility.
  228. */
  229. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_setVisible
  230. (JNIEnv *env, jobject obj, jboolean state)
  231. {
  232. QWidget *widget = (QWidget *) getNativeObject( env, obj );
  233. assert(widget != NULL);
  234. mainThread->postEventToMain( new AWTShowEvent( widget, (state == JNI_TRUE) ) );
  235. }
  236. /*
  237. * Returns whether the widget handles wheel scrolling.
  238. */
  239. JNIEXPORT jboolean JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_handlesWheelScrolling
  240. (JNIEnv *env, jobject obj)
  241. {
  242. jboolean handles = JNI_FALSE;
  243. QWidget *cb = (QWidget *) getNativeObject( env, obj );
  244. if( cb )
  245. if( cb->focusPolicy() & Qt::WheelFocus )
  246. handles = JNI_TRUE;
  247. return handles;
  248. }
  249. /**
  250. * calls qwidget::update on the compnent.
  251. */
  252. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_QtUpdateArea
  253. (JNIEnv *env, jobject obj, jint x, jint y, jint w, jint h)
  254. {
  255. QWidget *cb = (QWidget *) getNativeObject( env, obj );
  256. if( cb )
  257. mainThread->postEventToMain( new AWTUpdateEvent
  258. (cb, false, x, y, w, h ) );
  259. }
  260. /*
  261. * calls qwidget::update on the compnent.
  262. */
  263. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_QtUpdate
  264. (JNIEnv *env, jobject obj)
  265. {
  266. QWidget *cb = (QWidget *) getNativeObject( env, obj );
  267. if( cb )
  268. mainThread->postEventToMain( new AWTUpdateEvent
  269. ( cb, true, 0, 0, 0, 0 ) );
  270. }
  271. /*
  272. * Returns the native background color.
  273. */
  274. JNIEXPORT jobject JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_getNativeBackground
  275. (JNIEnv *env, jobject obj)
  276. {
  277. QWidget *cb = (QWidget *) getNativeObject( env, obj );
  278. assert(cb);
  279. QColor c = cb->palette().background().color().toRgb();
  280. jclass cls = env->FindClass("java/awt/Color");
  281. jmethodID mid = env->GetMethodID(cls, "<init>", "(III)V");
  282. jvalue values[3];
  283. values[0].i = (jint) c.red();
  284. values[1].i = (jint) c.green();
  285. values[2].i = (jint) c.blue();
  286. return env->NewObjectA(cls, mid, values);
  287. }
  288. /*
  289. * Returns which screen the component is on.
  290. */
  291. JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_whichScreen
  292. (JNIEnv *env, jobject obj)
  293. {
  294. QWidget *widget = (QWidget *) getNativeObject( env, obj );
  295. assert( widget );
  296. return (jint) qApplication->desktop()->screenNumber( widget );
  297. }
  298. /*
  299. * Reparents the widget.
  300. */
  301. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_reparentNative
  302. (JNIEnv *env, jobject obj, jobject newparent)
  303. {
  304. QWidget *widget = (QWidget *) getNativeObject( env, obj );
  305. assert( widget );
  306. QWidget *parentWidget = (QWidget *) getNativeObject( env, newparent );
  307. assert( parentWidget );
  308. mainThread->postEventToMain( new AWTReparent(widget, parentWidget ) );
  309. }
  310. /*
  311. * Get the preferred size of the widget
  312. */
  313. JNIEXPORT jobject JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_getBounds
  314. (JNIEnv *env, jobject obj)
  315. {
  316. QWidget *widget = (QWidget *) getNativeObject( env, obj );
  317. assert( widget );
  318. int x, y, w, h;
  319. widget->geometry().getRect(&x, &y, &w, &h);
  320. jclass cls = env->FindClass("java/awt/Rectangle");
  321. assert( cls != NULL);
  322. jmethodID mid = env->GetMethodID(cls, "<init>", "(IIII)V");
  323. assert( mid != NULL);
  324. jvalue values[4];
  325. values[0].i = (jint) x;
  326. values[1].i = (jint) y;
  327. values[2].i = (jint) w;
  328. values[3].i = (jint) h;
  329. return env->NewObjectA(cls, mid, values);
  330. }