slotcallbacks.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* slotcallbacks.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 <QObject>
  32. #include <QAbstractButton>
  33. #include <QAbstractSlider>
  34. #include <QAction>
  35. #include <QComboBox>
  36. #include <QListWidget>
  37. #include <QLineEdit>
  38. #include <QPushButton>
  39. #include <QTextEdit>
  40. #include <gnu_java_awt_peer_qt_QtButtonPeer.h>
  41. #include "qtcomponent.h"
  42. #include "qtstrings.h"
  43. #include "keybindings.h"
  44. #include "buttonevent.h"
  45. #include "slotcallbacks.h"
  46. // AdjustmentEvent constants
  47. #define UNIT_INCREMENT 1
  48. #define UNIT_DECREMENT 2
  49. #define BLOCK_DECREMENT 3
  50. #define BLOCK_INCREMENT 4
  51. #define TRACK 5
  52. class SlotCallback : public QObject {
  53. Q_OBJECT;
  54. private:
  55. JavaVM* vm;
  56. jobject target;
  57. jclass componentCls;
  58. jmethodID fireEventID;
  59. public:
  60. QScrollBar *sb; // used only by the scrollbar method.
  61. QListWidget *lw; // used only by the listitemclicked method
  62. SlotCallback(JNIEnv *env, jobject t)
  63. {
  64. env->GetJavaVM(&vm);
  65. target = t;
  66. target = env->NewGlobalRef(t);
  67. }
  68. ~SlotCallback()
  69. {
  70. JNIEnv *env;
  71. vm->GetEnv((void **)&env, JNI_VERSION_1_1);
  72. env->DeleteGlobalRef(target);
  73. }
  74. public slots:
  75. void buttonClicked()
  76. {
  77. JNIEnv *env;
  78. vm->GetEnv((void **)&env, JNI_VERSION_1_1);
  79. componentCls = env->GetObjectClass( target );
  80. fireEventID = env->GetMethodID( componentCls,
  81. "fireClick",
  82. "(I)V" );
  83. int modifiers = getAEKeyModifiers( QApplication::keyboardModifiers() );
  84. env->CallVoidMethod( target, fireEventID, modifiers );
  85. env->DeleteLocalRef( componentCls );
  86. }
  87. void buttonToggled(bool checked)
  88. {
  89. JNIEnv *env;
  90. vm->GetEnv((void **)&env, JNI_VERSION_1_1);
  91. componentCls = env->GetObjectClass( target );
  92. fireEventID = env->GetMethodID( componentCls,
  93. "fireToggle",
  94. "(Z)V" );
  95. if(checked)
  96. env->CallVoidMethod( target, fireEventID, JNI_TRUE );
  97. else
  98. env->CallVoidMethod( target, fireEventID, JNI_FALSE );
  99. env->DeleteLocalRef( componentCls );
  100. }
  101. // Used for List and Choice
  102. void choiceActivated( int index )
  103. {
  104. JNIEnv *env;
  105. vm->GetEnv((void **)&env, JNI_VERSION_1_1);
  106. componentCls = env->GetObjectClass( target );
  107. fireEventID = env->GetMethodID( componentCls,
  108. "fireChoice",
  109. "(I)V" );
  110. env->CallVoidMethod( target, fireEventID, (jint)index );
  111. env->DeleteLocalRef( componentCls );
  112. }
  113. void textChanged()
  114. {
  115. JNIEnv *env;
  116. vm->GetEnv((void **)&env, JNI_VERSION_1_1);
  117. componentCls = env->GetObjectClass( target );
  118. fireEventID = env->GetMethodID( componentCls,
  119. "textChanged",
  120. "()V" );
  121. env->CallVoidMethod( target, fireEventID );
  122. env->DeleteLocalRef( componentCls );
  123. }
  124. void scrollBarAction( int action )
  125. {
  126. JNIEnv *env;
  127. int type;
  128. int index;
  129. switch(action)
  130. {
  131. case QAbstractSlider::SliderNoAction:
  132. return;
  133. case QAbstractSlider::SliderSingleStepAdd:
  134. type = UNIT_INCREMENT;
  135. break;
  136. case QAbstractSlider::SliderSingleStepSub:
  137. type = UNIT_DECREMENT;
  138. break;
  139. case QAbstractSlider::SliderPageStepAdd:
  140. type = BLOCK_INCREMENT;
  141. break;
  142. case QAbstractSlider::SliderPageStepSub:
  143. type = BLOCK_DECREMENT;
  144. break;
  145. case QAbstractSlider::SliderToMinimum:
  146. type = TRACK;
  147. break;
  148. case QAbstractSlider::SliderToMaximum:
  149. type = TRACK;
  150. break;
  151. case QAbstractSlider::SliderMove:
  152. type = TRACK;
  153. break;
  154. }
  155. index = sb->value();
  156. vm->GetEnv((void **)&env, JNI_VERSION_1_1);
  157. componentCls = env->GetObjectClass( target );
  158. fireEventID = env->GetMethodID( componentCls,
  159. "fireMoved",
  160. "(II)V" );
  161. env->CallVoidMethod( target, fireEventID, (jint)type, (jint)index );
  162. env->DeleteLocalRef( componentCls );
  163. }
  164. void listItemClicked( QListWidgetItem * item )
  165. {
  166. int index = lw->row( item );
  167. JNIEnv *env;
  168. vm->GetEnv((void **)&env, JNI_VERSION_1_1);
  169. componentCls = env->GetObjectClass( target );
  170. fireEventID = env->GetMethodID( componentCls,
  171. "itemDoubleClicked",
  172. "(II)V" );
  173. int modifiers = getAEKeyModifiers( QApplication::keyboardModifiers() );
  174. env->CallVoidMethod( target, fireEventID, index, modifiers );
  175. env->DeleteLocalRef( componentCls );
  176. }
  177. };
  178. #include "slotcallbacks.moc.h"
  179. void connectButton(QPushButton *button, JNIEnv *env, jobject buttonobj)
  180. {
  181. SlotCallback *scb = new SlotCallback(env, buttonobj);
  182. QObject::connect( button, SIGNAL( clicked() ), scb, SLOT( buttonClicked() ) );
  183. }
  184. void connectChoice(QComboBox *choice, JNIEnv *env, jobject choiceobj)
  185. {
  186. SlotCallback *scb = new SlotCallback(env, choiceobj);
  187. QObject::connect( choice, SIGNAL( activated(int) ), scb, SLOT( choiceActivated(int) ) );
  188. }
  189. void connectList(QListWidget *list, JNIEnv *env, jobject listobj)
  190. {
  191. SlotCallback *scb = new SlotCallback(env, listobj);
  192. scb->lw = list;
  193. QObject::connect( list, SIGNAL( currentRowChanged(int) ),
  194. scb, SLOT( choiceActivated(int) ) );
  195. QObject::connect( list, SIGNAL( itemDoubleClicked( QListWidgetItem * )),
  196. scb, SLOT( listItemClicked( QListWidgetItem * )));
  197. }
  198. void connectAction(QAction *action, JNIEnv *env, jobject obj)
  199. {
  200. SlotCallback *scb = new SlotCallback(env, obj);
  201. QObject::connect( action, SIGNAL( triggered() ), scb, SLOT( buttonClicked() ) );
  202. }
  203. void connectToggle(QAbstractButton *action, JNIEnv *env, jobject obj)
  204. {
  205. SlotCallback *scb = new SlotCallback(env, obj);
  206. QObject::connect( action, SIGNAL( toggled(bool) ), scb, SLOT( buttonToggled(bool) ) );
  207. }
  208. void connectScrollBar(QScrollBar *scroll, JNIEnv *env, jobject obj)
  209. {
  210. SlotCallback *scb = new SlotCallback(env, obj);
  211. scb->sb = scroll;
  212. QObject::connect( scroll, SIGNAL( actionTriggered(int) ), scb, SLOT( scrollBarAction(int) ) );
  213. }
  214. void connectTextEdit(QTextEdit *edit, JNIEnv *env, jobject obj)
  215. {
  216. SlotCallback *scb = new SlotCallback(env, obj);
  217. QObject::connect( edit, SIGNAL( textChanged() ),
  218. scb, SLOT( textChanged() ) );
  219. }
  220. void connectLineEdit(QLineEdit *edit, JNIEnv *env, jobject obj)
  221. {
  222. SlotCallback *scb = new SlotCallback(env, obj);
  223. QObject::connect( edit, SIGNAL(textChanged( QString ) ),
  224. scb, SLOT( textChanged() ) );
  225. }