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)
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
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
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. */
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
{
55 ListInsert(QListWidget
*w
, QString
*s
, int i
) : AWTEvent()
64 widget
->insertItem( index
, *string
);
69 class SelectEvent
: public AWTEvent
{
77 SelectEvent(QListWidget
*w
, int i
, bool s
) : AWTEvent()
86 widget
->setItemSelected ( widget
->item(index
), selected
);
90 class ListDelete
: public AWTEvent
{
94 int startIndex
, endIndex
;
97 ListDelete(QListWidget
*w
, int starti
, int endi
) : AWTEvent()
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
);
122 setNativeObject( env
, obj
, list
);
123 connectList(list
, env
, obj
);
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
);
134 QString
*qStr
= getQString(env
, str
);
135 mainThread
->postEventToMain( new ListInsert(list
, qStr
, index
) );
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
);
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
);
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
)
171 QListWidget
*list
= (QListWidget
*) getNativeObject( env
, obj
);
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 );
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
);
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
);
207 // FIXME: Multiple selection is buggy in Qt4. Workaround needed.
208 list
->setSelectionMode( ((allow
== JNI_TRUE
) ? QAbstractItemView::MultiSelection
: QAbstractItemView::SingleSelection
) );