summaryrefslogtreecommitdiffstats
path: root/qtjava/javalib/qtjava/QtSupport.h
blob: 1f7e7b38596ebbc9e650d99075db22da1d4468a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/***************************************************************************
                            QtSupport.h -  description
                             -------------------
    begin                : Fri Oct 20 13:46:55 2000
    copyright            : (C) 2000 Lost Highway Ltd
    email                : Richard_Dale@tipitina.demon.co.uk
    generated by         : duke@tipitina on Fri Oct 20 13:46:55 2000, using kdoc $.
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef _QT_SUPPORT_H_
#define _QT_SUPPORT_H_

#include <jni.h>

#ifndef JNIEXPORT
#define JNIEXPORT
#endif

#ifndef JNIIMPORT
#define JNIIMPORT
#endif

#ifndef JNICALL
#define JNICALL
#endif

#include <tqstrlist.h>
#include <tqstringlist.h>
#include <tqwidgetlist.h>
#include <tqdom.h>
#include <tqobjectlist.h>
#include <tqdatetime.h>
#include <tqevent.h>
#include <tqvalidator.h>
#include <tqcanvas.h>
#include <tqlistview.h>
#include <tqiconview.h>
#include <tqurlinfo.h>

class JavaSlot;
class JavaSignal;


/** A class with various static utility methods to run the C++ side
	of the Qt Java library. The functions cooperate with the Java
	methods in 'qtjava.java' and 'Invocation.java'.

	@author Richard Dale */
class QtSupport
{
public:
	QtSupport() {}
	~QtSupport() {}

	/** Save the current JavaVM pointer to make it easy to obtain the current environment later */
	static void registerJVM(JNIEnv * env);
	/** Obtain the current JNIEnv */
	static JNIEnv * GetEnv();

	/** Converts from the enum TTQEvent::Type to the Java class name of the corresponding event */
	static const char * eventTypeToEventClassName(TTQEvent::Type eventType);
	/** Event filter handling callback function */
	static bool eventFilterDelegate(TTQObject * object, const char * objectType, TTQObject * filterTarget, TTQEvent * event);
	/** Event handling callback function */
	static bool eventDelegate(TTQObject * object, const char * eventType, void * event, const char * eventName);
	/** Void method with no parameters. FALSE if method is implemented in Java,
	    and returns TRUE if method is not implemented in Java */
	static bool voidDelegate(void * object, const char * className, const char * methodName);
	/** Boolean method with no parameters. FALSE iff method is implemented in Java
	    and also returns FALSE. Returns TRUE if method is not implemented in Java */
	static bool booleanDelegate(TTQObject * object, const char * methodName);

	/** TTQValidator callbacks for validate() and fixup() */
	static int validateDelegate(TTQValidator * object, TTQString & input, int & pos);
	static void fixupDelegate(TTQValidator * object, TTQString & input);

	/** C++ instances are deleted in a finalize method iff 'allocatedInJavaWorld' is true. */
	static bool allocatedInJavaWorld(JNIEnv * env, jobject obj);
	static void setAllocatedInJavaWorld(JNIEnv * env, jobject obj, bool yn);

	/** Obtain the wrapped C++ instance for a given Java instance */
	static void * getQt(JNIEnv * env, jobject obj);
	/** Set the wrapped C++ instance for a given Java instance */
	static void setQt(JNIEnv * env, jobject obj, void * qt);

	/** Add a 'C++ instance key/Java instance value' pair to the qtKeyToJavaMap */
	static void setObjectForQtKey(JNIEnv * env, jobject obj, void * qt);
	
	/** Obtain the Jave instance for the given C++ instance, instantiating a new
		instance of the given class name if needed */
	static jobject objectForQtKey(JNIEnv * env, void * qt, const char * className, const bool allocatedInJavaWorld = FALSE);

	/** A 'C++ instance has been deleted. Set '_allocatedInJavaWorld' to false in the corresponding java instance */
	static void qtKeyDeleted(void * qt);

	/** Connect a C++ or Java sender to a Java receiver */
	static jboolean connect(JNIEnv * env, jobject sender, jstring signal, jobject receiver, jstring slot);

	/** Disconnect a C++ or Java sender from a Java receiver */
	static jboolean disconnect(JNIEnv * env, jobject sender, jstring signal, jobject receiver, jstring slot);

	/** Invoke a Java slot with arguments 'args'. */
	static void emitJavaSignal(JNIEnv * env, jobject sender, jstring signal, jobjectArray args);

	/** Return a JavaSignal proxy instance for a Java signal. Creates a new one if needed. */
	static JavaSignal * signalForSender(JNIEnv * env, void * qt, jstring signal);
	/** Return a JavaSlot proxy instance for a Java slot. Creates a new one if needed. */
	static JavaSlot * slotForReceiver(JNIEnv * env, jobject receiver, jstring slot);

	/** Casts a 'TTQWidget *', 'TTQPixmap *' or subclass of TTQPaintDevice correctly to a 'TTQPaintDevice *'.
	 	Needed because of C++ multiple inheritance complications. */
	static TTQPaintDevice * paintDevice(JNIEnv * env, jobject obj);
	/** Casts a 'TTQDragObject *', 'TTQDropEvent *' or subclass of TTQDropEvent correctly to a 'TTQMimeSource *'.
	 	Needed because of C++ multiple inheritance complications. */
	static TTQMimeSource * mimeSource(JNIEnv * env, jobject obj);

	/** Which byte order are Java strings */
	static bool bigEndianUnicode();

	/** Convert from Java primitive array types to C++ primitive array */
	static bool * toBooleanPtr(JNIEnv * env, jbooleanArray obj);
	static int * toIntPtr(JNIEnv * env, jintArray obj);
	static double * toDoublePtr(JNIEnv * env, jdoubleArray obj);
	static short * toShortPtr(JNIEnv * env, jshortArray obj);

	/** Convert from C++ primitive array types to Java primitive arrays */
	static jbooleanArray fromBooleanPtr(JNIEnv * env, bool * arg);
	static jintArray fromIntPtr(JNIEnv * env, int * arg);
	static jdoubleArray fromDoublePtr(JNIEnv * env, double * arg);
	static jshortArray fromShortPtr(JNIEnv * env, short * arg);

	/** Convert from Qt dates and times to Java Date and Time */
	static jobject fromTQDateTime(JNIEnv * env, TTQDateTime* qdate);
	static jobject fromTQDate(JNIEnv * env, TTQDate* qdate);
	static jobject fromTQTime(JNIEnv * env, TTQTime* qtime);

	/** Convert from Java Date and Time to Qt dates and times */
	static TTQDateTime * toTQDateTime(JNIEnv * env, jobject jdate, TTQDateTime** qdate);
	static TTQDate * toTQDate(JNIEnv * env, jobject jdate, TTQDate** qdate);
	static TTQTime * toTQTime(JNIEnv * env, jobject jtime, TTQTime** qtime);

	/** Convert from TTQString to Java String */
	static jstring fromTQString(JNIEnv * env, TTQString * qstring);
	/** Convert from TTQCString to Java String */
	static jstring fromTQCString(JNIEnv * env, TTQCString * qcstring);
	/** Convert from a char * to Java String */
	static jstring fromCharString(JNIEnv * env, char * qcstring);

	/** Convert from Java String to TTQString */
	static TTQString * toTQString(JNIEnv * env, jstring str, TTQString ** qstring);
	/** Convert from Java String to TTQCString */
	static TTQCString * toTQCString(JNIEnv * env, jstring str, TTQCString ** qcstring);
	/** Convert from Java String to a char * */
	static char * toCharString(JNIEnv * env, jstring str, TTQCString ** qcstring);

	/** Convert from TTQString to Java StringBuffer */
	static void fromTQStringToStringBuffer(JNIEnv * env, TTQString * qstring, jobject buffer);
	/** Convert from Java StringBuffer to TTQString */
	static TTQString * toTQStringFromStringBuffer(JNIEnv * env, jobject buffer, TTQString ** qstring);
	/** Convert from TTQCString to Java StringBuffer */
	static void fromTQCStringToStringBuffer(JNIEnv * env, TTQCString * qcstring, jobject buffer);
	
	/** Convert from TTQChar to Java char */
	static jchar fromTQChar(JNIEnv * env, TTQChar * qchar);
	/** Convert from Java char to TTQChar */
	static TTQChar * toTQChar(JNIEnv * env, jchar unichar, TTQChar ** qchar);

	/** Convert from TTQByteArray to Java byte array */
	static jbyteArray fromTQByteArray(JNIEnv * env, TTQByteArray * qbyteArray);
	/** Convert from Java byte array to TTQByteArray */
	static TTQByteArray * toTQByteArray(JNIEnv * env, jbyteArray bytes, TTQByteArray ** qbyteArray);
	/** Convert from Java 'char[]' to 'uchar *' */
	static uchar * toUcharArray(JNIEnv * env, jcharArray bytes, TTQByteArray ** qbyteArray);

	/** Convert from Java 'int[]' to TTQValueList<int> */
	static TTQValueList<int> toTQIntValueList(JNIEnv * env, jintArray ints, TTQValueList<int> ** qintArray);
	/** Convert from TTQValueList<int> to Java 'int[]' */
	static jintArray fromTQIntValueList(JNIEnv * env, TTQValueList<int> * qintArray);
	
	/** Convert from a String[] to char * argv[], adding a dummy argv[0] argument */
	static char ** toArgv(JNIEnv * env, jobjectArray stringList);
	/** Convert from a String[] to char * argv[] */
	static char ** toStringArray(JNIEnv * env, jobjectArray stringList);
	/** Convert from String[] to TTQStrList */
	static TTQStrList * toTQStrList(JNIEnv * env, jobjectArray stringList, TTQStrList ** qstringList);
	/** Convert from String[] to TTQStringList */
	static TTQStringList * toTQStringList(JNIEnv * env, jobjectArray stringList, TTQStringList ** qstringList);

	/** Returns a Java ArrayList for the various types of Qt List */
	static jobject arrayWithTQStrList(JNIEnv * env, TTQStrList * strList, jobject arrayList = 0);
	static jobject arrayWithTQStringList(JNIEnv * env, TTQStringList * stringList, jobject arrayList = 0);
	static jobject arrayWithTQWidgetList(JNIEnv * env, TTQWidgetList * widgetList, jobject arrayList = 0);
	static jobject arrayWithTQDomNodeList(JNIEnv * env, TTQDomNodeList * domNodeList, jobject arrayList = 0);
	static jobject arrayWithTQObjectList(JNIEnv * env, TTQObjectList * objectList, jobject arrayList = 0);
	static jobject arrayWithTQCanvasItemList(JNIEnv * env, TTQCanvasItemList * itemList, jobject arrayList = 0);
	static jobject arrayWithTQListViewItemList(JNIEnv * env, TTQListViewItemIterator * iterator, jobject arrayList = 0);
	static jobject arrayWithTQRectList(JNIEnv * env, TTQMemArray<TTQRect> * rectList, jobject arrayList = 0);
	static jobject arrayWithTQIconDragItemList(JNIEnv * env, TTQValueList<TTQIconDragItem> * dragItemList, jobject arrayList = 0);
	static jobject arrayWithTQUrlInfoList(JNIEnv * env, TTQValueList<TTQUrlInfo> * infoList, jobject arrayList = 0);

protected:
	/** Cache String constructor from bytes, and getBytes() method info */
	static jmethodID MID_String_init;
	static jmethodID MID_String_getBytes;

	/** Result of the test for the endianess of the unicode string returned by GetStringChars() */
	static bool _bigEndianUnicode;
};

#endif