Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / native / jni / qt-peer / qtlistpeer.cpp
blob027d47880908d712bf48007c40d5993d72f0105a
1 /* qtlistpeer.cpp --
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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. */
38 #include <assert.h>
39 #include <QWidget>
40 #include <QListWidget>
41 #include <gnu_java_awt_peer_qt_QtListPeer.h>
42 #include "qtcomponent.h"
43 #include "qtstrings.h"
44 #include "mainthreadinterface.h"
45 #include "slotcallbacks.h"
47 class ListInsert : public AWTEvent {
49 private:
50 QListWidget *widget;
51 QString *string;
52 int index;
54 public:
55 ListInsert(QListWidget *w, QString *s, int i) : AWTEvent()
57 widget = w;
58 string = s;
59 index = i;
62 void runEvent()
64 widget->insertItem( index, *string );
65 delete string;
69 class SelectEvent : public AWTEvent {
71 private:
72 QListWidget *widget;
73 int index;
74 bool selected;
76 public:
77 SelectEvent(QListWidget *w, int i, bool s) : AWTEvent()
79 widget = w;
80 index = i;
81 selected = s;
84 void runEvent()
86 widget->setItemSelected ( widget->item(index), selected );
90 class ListDelete : public AWTEvent {
92 private:
93 QListWidget *widget;
94 int startIndex, endIndex;
96 public:
97 ListDelete(QListWidget *w, int starti, int endi) : AWTEvent()
99 widget = w;
100 startIndex = starti;
101 endIndex = endi;
104 void runEvent()
106 for (int i = endIndex; i >= startIndex; i--)
107 delete widget->takeItem(i);
112 * Construct a QListWidget object
114 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtListPeer_init
115 (JNIEnv *env, jobject obj)
117 QWidget *parentWidget = (QWidget *)getParentWidget(env, obj);
118 assert( parentWidget );
119 QListWidget *list = new QListWidget( parentWidget );
120 assert( list );
122 setNativeObject( env, obj, list );
123 connectList(list, env, obj);
127 * Adds an element.
129 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtListPeer_add
130 (JNIEnv *env, jobject obj, jstring str, jint index)
132 QListWidget *list = (QListWidget *) getNativeObject( env, obj );
133 assert( list );
134 QString *qStr = getQString(env, str);
135 mainThread->postEventToMain( new ListInsert(list, qStr, index) );
139 * Delete items
141 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtListPeer_delItems
142 (JNIEnv *env, jobject obj, jint startindex, jint endindex)
144 QListWidget *list = (QListWidget *) getNativeObject( env, obj );
145 assert( list );
146 mainThread->postEventToMain( new ListDelete(list, startindex, endindex) );
150 * (De)select an element.
152 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtListPeer_select
153 (JNIEnv *env, jobject obj, jint index, jboolean sel)
155 QListWidget *list = (QListWidget *) getNativeObject( env, obj );
156 assert( list );
158 mainThread->postEventToMain( new SelectEvent(list, index,
159 (sel == JNI_TRUE)) );
163 * Returns the indices of the selected items.
165 JNIEXPORT jintArray JNICALL Java_gnu_java_awt_peer_qt_QtListPeer_getSelectedIndexes
166 (JNIEnv *env, jobject obj)
168 jintArray retArray;
169 jint *arr;
171 QListWidget *list = (QListWidget *) getNativeObject( env, obj );
172 assert( list );
174 QList<QListWidgetItem *> items = list->selectedItems();
175 retArray = env->NewIntArray( items.count() );
176 arr = env->GetIntArrayElements( retArray, NULL );
178 for(int i = 0; i < items.count(); i++)
179 arr[i] = list->row(items.at(i));
181 env->ReleaseIntArrayElements( retArray, arr, 0 );
182 return retArray;
186 * Sets the current item and makes it visible.
188 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtListPeer_makeVisible
189 (JNIEnv *env, jobject obj, jint index)
192 QListWidget *list = (QListWidget *) getNativeObject( env, obj );
193 assert( list );
195 list->scrollToItem( list->item(index) );
199 * Set multiple selection mode.
201 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtListPeer_setMultipleMode
202 (JNIEnv *env, jobject obj, jboolean allow)
204 QListWidget *list = (QListWidget *) getNativeObject( env, obj );
205 assert( list );
207 // FIXME: Multiple selection is buggy in Qt4. Workaround needed.
208 list->setSelectionMode( ((allow == JNI_TRUE) ? QAbstractItemView::MultiSelection : QAbstractItemView::SingleSelection) );