2007-01-17 Jack Howarth <howarth@bromo.med.uc.edu>
[official-gcc.git] / libjava / classpath / native / jni / qt-peer / qtcomponentpeer.cpp
blob0399dd8fd639e07f491bb42cb6462e8444e048a1
1 /* qtcomponentpeer.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 <QApplication>
40 #include <QDesktopWidget>
41 #include <QShowEvent>
42 #include <QHideEvent>
43 #include <QColor>
44 #include <QCursor>
45 #include <QWidget>
46 #include <gnu_java_awt_peer_qt_QtComponentPeer.h>
47 #include "qtcomponent.h"
48 #include "componentevent.h"
49 #include "qtfont.h"
51 extern QApplication *qApplication;
53 // Java Cursor types.
54 #define DEFAULT_CURSOR 0
55 #define CROSSHAIR_CURSOR 1
56 #define TEXT_CURSOR 2
57 #define WAIT_CURSOR 3
58 #define SW_RESIZE_CURSOR 4
59 #define SE_RESIZE_CURSOR 5
60 #define NW_RESIZE_CURSOR 6
61 #define NE_RESIZE_CURSOR 7
62 #define N_RESIZE_CURSOR 8
63 #define S_RESIZE_CURSOR 9
64 #define W_RESIZE_CURSOR 10
65 #define E_RESIZE_CURSOR 11
66 #define HAND_CURSOR 12
67 #define MOVE_CURSOR 13
69 /**
70 * Call back the init() method from the main thread.
72 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_callInit
73 (JNIEnv *env, jobject obj)
75 mainThread->postEventToMain( new AWTInitEvent( env, obj ) );
79 * Generic disposal.
81 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_disposeNative
82 (JNIEnv *env, jobject obj)
84 QWidget *widget = (QWidget *) getNativeObject( env, obj );
85 setNativeObject(env, obj, NULL);
86 mainThread->postEventToMain( new AWTDestroyEvent( widget ) );
89 /**
90 * Returns the on-screen location of the component.
92 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_getLocationOnScreenNative
93 (JNIEnv *env, jobject obj, jobject point)
95 QWidget *widget = (QWidget *) getNativeObject( env, obj );
96 assert( widget );
97 mainThread->postEventToMain( new AWTGetOriginEvent( widget, env, point) );
101 * Get the preferred/minimum size of the widget
103 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_getSizeNative
104 (JNIEnv *env, jobject obj, jobject size, jboolean preferred)
106 QWidget *widget = (QWidget *) getNativeObject( env, obj );
107 assert( widget );
109 mainThread->postEventToMain
110 (new GetSizeEvent( widget, env, size, (preferred == JNI_TRUE)));
115 JNIEXPORT jboolean JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_isObscured
116 (JNIEnv *env, jobject obj)
118 jboolean retVal;
120 QWidget *widget = (QWidget *) getNativeObject( env, obj );
121 assert( widget );
123 retVal = (widget->isVisible() == TRUE) ? JNI_TRUE : JNI_FALSE;
125 return retVal;
129 * Returns whether the widget is focusable or not.
131 JNIEXPORT jboolean JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_isFocusable
132 (JNIEnv *env, jobject obj)
134 jboolean retVal;
136 QWidget *widget = (QWidget *) getNativeObject( env, obj );
137 assert( widget );
139 retVal = (widget->focusPolicy() != Qt::NoFocus) ? JNI_TRUE : JNI_FALSE;
141 return retVal;
145 * Requests the focus
147 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_requestFocus
148 (JNIEnv *env, jobject obj)
150 QWidget *widget = (QWidget *) getNativeObject( env, obj );
151 assert( widget );
152 mainThread->postEventToMain( new AWTReqFocusEvent( widget ) );
156 * Sets the size and position. Important.
158 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_setBoundsNative
159 (JNIEnv *env, jobject obj, jint x, jint y, jint width, jint height)
161 QWidget *widget = (QWidget *) getNativeObject( env, obj );
162 assert( widget );
163 mainThread->postEventToMain
164 (new AWTResizeEvent( widget, x, y, width, height ) );
168 * Sets the mouse cursor
170 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_setCursor
171 (JNIEnv *env, jobject obj, jint cursortype)
173 QWidget *widget = (QWidget *) getNativeObject( env, obj );
174 assert( widget );
176 Qt::CursorShape shape;
177 switch(cursortype)
179 case CROSSHAIR_CURSOR:
180 shape = Qt::CrossCursor;
181 break;
183 case W_RESIZE_CURSOR:
184 case E_RESIZE_CURSOR:
185 shape = Qt::SizeHorCursor;
186 break;
187 case N_RESIZE_CURSOR:
188 case S_RESIZE_CURSOR:
189 shape = Qt::SizeVerCursor;
190 break;
191 case HAND_CURSOR:
192 shape = Qt::PointingHandCursor;
193 break;
194 case MOVE_CURSOR:
195 shape = Qt::SizeAllCursor;
196 break;
198 case NE_RESIZE_CURSOR:
199 case SW_RESIZE_CURSOR:
200 shape = Qt::SizeBDiagCursor;
201 break;
202 case NW_RESIZE_CURSOR:
203 case SE_RESIZE_CURSOR:
204 shape = Qt::SizeFDiagCursor;
205 break;
206 case TEXT_CURSOR:
207 shape = Qt::IBeamCursor;
208 break;
209 case WAIT_CURSOR:
210 shape = Qt::WaitCursor;
211 break;
213 case DEFAULT_CURSOR:
214 default:
215 shape = Qt::ArrowCursor;
216 break;
219 mainThread->postEventToMain( new AWTCursorEvent( widget, shape ) );
223 * Enable, disable.
225 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_setEnabled
226 (JNIEnv *env, jobject obj, jboolean state)
228 QWidget *widget = (QWidget *) getNativeObject( env, obj );
229 assert(widget != NULL);
231 mainThread->postEventToMain( new AWTEnableEvent( widget, (state == JNI_TRUE) ) );
235 * Set the font
237 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_setFontNative
238 (JNIEnv *env, jobject obj, jobject fontpeer)
240 QWidget *widget = (QWidget *) getNativeObject( env, obj );
241 assert( widget );
242 QFont *font = (QFont *) getFont( env, fontpeer );
243 assert( font );
245 mainThread->postEventToMain( new AWTFontEvent(widget, font) );
249 * Sets the back- or foreground color.
251 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_setGround
252 (JNIEnv *env, jobject obj, jint r, jint g, jint b, jboolean isForeground)
254 QColor *color = new QColor(r, g, b);
256 QWidget *widget = (QWidget *) getNativeObject( env, obj );
257 assert(widget);
258 mainThread->postEventToMain( new AWTBackgroundEvent(widget,
259 (isForeground == JNI_TRUE),
260 color) );
264 * Sets the visibility.
266 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_setVisible
267 (JNIEnv *env, jobject obj, jboolean state)
269 QWidget *widget = (QWidget *) getNativeObject( env, obj );
270 assert(widget != NULL);
271 mainThread->postEventToMain( new AWTShowEvent( widget, (state == JNI_TRUE) ) );
275 * Returns whether the widget handles wheel scrolling.
277 JNIEXPORT jboolean JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_handlesWheelScrolling
278 (JNIEnv *env, jobject obj)
280 jboolean handles = JNI_FALSE;
282 QWidget *cb = (QWidget *) getNativeObject( env, obj );
283 if( cb )
284 if( cb->focusPolicy() & Qt::WheelFocus )
285 handles = JNI_TRUE;
287 return handles;
291 * calls qwidget::update on the compnent.
293 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_QtUpdateArea
294 (JNIEnv *env, jobject obj, jint x, jint y, jint w, jint h)
296 QWidget *cb = (QWidget *) getNativeObject( env, obj );
297 if( cb )
298 mainThread->postEventToMain( new AWTUpdateEvent
299 (cb, false, x, y, w, h ) );
303 * calls qwidget::update on the compnent.
305 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_QtUpdate
306 (JNIEnv *env, jobject obj)
308 QWidget *cb = (QWidget *) getNativeObject( env, obj );
309 if( cb )
310 mainThread->postEventToMain( new AWTUpdateEvent
311 ( cb, true, 0, 0, 0, 0 ) );
315 * Returns the native background color.
317 JNIEXPORT jobject JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_getNativeBackground
318 (JNIEnv *env, jobject obj)
320 QWidget *cb = (QWidget *) getNativeObject( env, obj );
321 assert(cb);
322 QColor c = cb->palette().background().color().toRgb();
324 jclass cls = env->FindClass("java/awt/Color");
325 jmethodID mid = env->GetMethodID(cls, "<init>", "(III)V");
326 jvalue values[3];
328 values[0].i = (jint) c.red();
329 values[1].i = (jint) c.green();
330 values[2].i = (jint) c.blue();
332 return env->NewObjectA(cls, mid, values);
336 * Returns which screen the component is on.
338 JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_whichScreen
339 (JNIEnv *env, jobject obj)
341 QWidget *widget = (QWidget *) getNativeObject( env, obj );
342 assert( widget );
343 return (jint) qApplication->desktop()->screenNumber( widget );
347 * Reparents the widget.
349 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_reparentNative
350 (JNIEnv *env, jobject obj, jobject newparent)
352 QWidget *widget = (QWidget *) getNativeObject( env, obj );
353 assert( widget );
354 QWidget *parentWidget = (QWidget *) getNativeObject( env, newparent );
355 assert( parentWidget );
356 mainThread->postEventToMain( new AWTReparent(widget, parentWidget ) );
360 * Get the preferred size of the widget
362 JNIEXPORT jobject JNICALL Java_gnu_java_awt_peer_qt_QtComponentPeer_getBounds
364 (JNIEnv *env, jobject obj)
366 QWidget *widget = (QWidget *) getNativeObject( env, obj );
367 assert( widget );
369 int x, y, w, h;
370 widget->geometry().getRect(&x, &y, &w, &h);
372 jclass cls = env->FindClass("java/awt/Rectangle");
373 assert( cls != NULL);
374 jmethodID mid = env->GetMethodID(cls, "<init>", "(IIII)V");
375 assert( mid != NULL);
376 jvalue values[4];
378 values[0].i = (jint) x;
379 values[1].i = (jint) y;
380 values[2].i = (jint) w;
381 values[3].i = (jint) h;
383 return env->NewObjectA(cls, mid, values);