qtlistpeer.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* qtlistpeer.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 <QWidget>
  33. #include <QListWidget>
  34. #include <gnu_java_awt_peer_qt_QtListPeer.h>
  35. #include "qtcomponent.h"
  36. #include "qtstrings.h"
  37. #include "mainthreadinterface.h"
  38. #include "slotcallbacks.h"
  39. class ListInsert : public AWTEvent {
  40. private:
  41. QListWidget *widget;
  42. QString *string;
  43. int index;
  44. public:
  45. ListInsert(QListWidget *w, QString *s, int i) : AWTEvent()
  46. {
  47. widget = w;
  48. string = s;
  49. index = i;
  50. }
  51. void runEvent()
  52. {
  53. widget->insertItem( index, *string );
  54. delete string;
  55. }
  56. };
  57. class SelectEvent : public AWTEvent {
  58. private:
  59. QListWidget *widget;
  60. int index;
  61. bool selected;
  62. public:
  63. SelectEvent(QListWidget *w, int i, bool s) : AWTEvent()
  64. {
  65. widget = w;
  66. index = i;
  67. selected = s;
  68. }
  69. void runEvent()
  70. {
  71. widget->setItemSelected ( widget->item(index), selected );
  72. }
  73. };
  74. class ListDelete : public AWTEvent {
  75. private:
  76. QListWidget *widget;
  77. int startIndex, endIndex;
  78. public:
  79. ListDelete(QListWidget *w, int starti, int endi) : AWTEvent()
  80. {
  81. widget = w;
  82. startIndex = starti;
  83. endIndex = endi;
  84. }
  85. void runEvent()
  86. {
  87. for (int i = endIndex; i >= startIndex; i--)
  88. delete widget->takeItem(i);
  89. }
  90. };
  91. /*
  92. * Construct a QListWidget object
  93. */
  94. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtListPeer_init
  95. (JNIEnv *env, jobject obj)
  96. {
  97. QWidget *parentWidget = (QWidget *)getParentWidget(env, obj);
  98. assert( parentWidget );
  99. QListWidget *list = new QListWidget( parentWidget );
  100. assert( list );
  101. setNativeObject( env, obj, list );
  102. connectList(list, env, obj);
  103. }
  104. /*
  105. * Adds an element.
  106. */
  107. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtListPeer_add
  108. (JNIEnv *env, jobject obj, jstring str, jint index)
  109. {
  110. QListWidget *list = (QListWidget *) getNativeObject( env, obj );
  111. assert( list );
  112. QString *qStr = getQString(env, str);
  113. mainThread->postEventToMain( new ListInsert(list, qStr, index) );
  114. }
  115. /*
  116. * Delete items
  117. */
  118. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtListPeer_delItems
  119. (JNIEnv *env, jobject obj, jint startindex, jint endindex)
  120. {
  121. QListWidget *list = (QListWidget *) getNativeObject( env, obj );
  122. assert( list );
  123. mainThread->postEventToMain( new ListDelete(list, startindex, endindex) );
  124. }
  125. /*
  126. * (De)select an element.
  127. */
  128. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtListPeer_select
  129. (JNIEnv *env, jobject obj, jint index, jboolean sel)
  130. {
  131. QListWidget *list = (QListWidget *) getNativeObject( env, obj );
  132. assert( list );
  133. mainThread->postEventToMain( new SelectEvent(list, index,
  134. (sel == JNI_TRUE)) );
  135. }
  136. /**
  137. * Returns the indices of the selected items.
  138. */
  139. JNIEXPORT jintArray JNICALL Java_gnu_java_awt_peer_qt_QtListPeer_getSelectedIndexes
  140. (JNIEnv *env, jobject obj)
  141. {
  142. jintArray retArray;
  143. jint *arr;
  144. QListWidget *list = (QListWidget *) getNativeObject( env, obj );
  145. assert( list );
  146. QList<QListWidgetItem *> items = list->selectedItems();
  147. retArray = env->NewIntArray( items.count() );
  148. arr = env->GetIntArrayElements( retArray, NULL );
  149. for(int i = 0; i < items.count(); i++)
  150. arr[i] = list->row(items.at(i));
  151. env->ReleaseIntArrayElements( retArray, arr, 0 );
  152. return retArray;
  153. }
  154. /*
  155. * Sets the current item and makes it visible.
  156. */
  157. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtListPeer_makeVisible
  158. (JNIEnv *env, jobject obj, jint index)
  159. {
  160. QListWidget *list = (QListWidget *) getNativeObject( env, obj );
  161. assert( list );
  162. list->scrollToItem( list->item(index) );
  163. }
  164. /*
  165. * Set multiple selection mode.
  166. */
  167. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtListPeer_setMultipleMode
  168. (JNIEnv *env, jobject obj, jboolean allow)
  169. {
  170. QListWidget *list = (QListWidget *) getNativeObject( env, obj );
  171. assert( list );
  172. // FIXME: Multiple selection is buggy in Qt4. Workaround needed.
  173. list->setSelectionMode( ((allow == JNI_TRUE) ? QAbstractItemView::MultiSelection : QAbstractItemView::SingleSelection) );
  174. }