Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / native / jni / qt-peer / qttextareapeer.cpp
blob50f85b86101922bc1b6bb676fe15d49ce5f31a50
1 /* qttextareapeer.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 <time.h>
40 #include <QTextEdit>
41 #include <QTextCursor>
42 #include <gnu_java_awt_peer_qt_QtTextAreaPeer.h>
43 #include "mainthreadinterface.h"
44 #include "componentevent.h"
45 #include "slotcallbacks.h"
46 #include "qtcomponent.h"
47 #include "qtstrings.h"
49 class TASetText : public AWTEvent {
50 private:
51 QTextEdit *area;
52 QString *text;
54 public:
55 TASetText(QTextEdit *w, QString *t) : AWTEvent()
57 area = w;
58 text = t;
61 void runEvent()
63 area->setPlainText( *text );
64 delete text;
69 * Construct a QTextEdit object
71 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtTextAreaPeer_init
72 (JNIEnv *env, jobject obj)
74 QWidget *parentWidget = (QWidget *)getParentWidget( env, obj );
75 assert( parentWidget );
76 QTextEdit *editor = new QTextEdit( parentWidget );
77 editor->setGeometry( 0, 0, 400, 400 );
78 assert( editor );
80 // setLineWrapColumnOrWidth ( int w );
81 setNativeObject( env, obj, editor );
83 // Connect TextChanged events.
84 connectTextEdit(editor, env, obj);
88 * Returns the cursor position.
90 JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_qt_QtTextAreaPeer_getCaretPosition
91 (JNIEnv *env, jobject obj)
93 int index;
95 QTextEdit *editor = (QTextEdit *) getNativeObject( env, obj );
96 assert( editor );
98 index = editor->textCursor().position();;
100 return (jint)index;
104 * Returns the char index at a given screen point
106 JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_qt_QtTextAreaPeer_getIndexAtPoint
107 (JNIEnv *env, jobject obj, jint x, jint y)
109 QPoint *p = new QPoint(x,y);
111 QTextEdit *editor = (QTextEdit *) getNativeObject( env, obj );
112 assert( editor );
113 QTextCursor curs = editor->cursorForPosition( *p );
114 delete p;
116 return curs.position();
120 * Returns the start (start = true) or end (start = false) of the selection.
122 JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_qt_QtTextAreaPeer_getSelection
123 (JNIEnv *env, jobject obj, jboolean isStart)
125 int start, end;
127 QTextEdit *editor = (QTextEdit *) getNativeObject( env, obj );
128 assert( editor );
129 start = editor->textCursor().selectionStart();
130 end = editor->textCursor().selectionEnd();
132 return ((isStart == JNI_TRUE) ? start : end);
136 * Returns the text.
138 JNIEXPORT jstring JNICALL Java_gnu_java_awt_peer_qt_QtTextAreaPeer_getText
139 (JNIEnv *env, jobject obj)
141 QTextEdit *editor = (QTextEdit *) getNativeObject( env, obj );
142 assert( editor );
143 QString text = editor->toPlainText();
145 return getJavaString(env, &text);
149 * Sets the editor text.
151 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtTextAreaPeer_setText
152 (JNIEnv *env, jobject obj, jstring str)
154 QTextEdit *editor = (QTextEdit *) getNativeObject( env, obj );
155 assert( editor );
157 QString *qStr = getQString(env, str);
158 mainThread->postEventToMain( new TASetText( editor, qStr ) );
162 * Sets the selection.
164 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtTextAreaPeer_select
165 (JNIEnv *env, jobject obj, jint startpos, jint endpos)
167 QTextEdit *editor = (QTextEdit *) getNativeObject( env, obj );
168 assert( editor );
170 QTextCursor curs(editor->document());
171 curs.setPosition(startpos);
172 curs.setPosition(endpos, QTextCursor::KeepAnchor);
173 editor->setTextCursor( curs );
177 * Allow or disallow editing.
179 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtTextAreaPeer_setEditable
180 (JNIEnv *env, jobject obj, jboolean editable)
182 QTextEdit *editor = (QTextEdit *) getNativeObject( env, obj );
183 assert( editor );
184 editor->setReadOnly( (editable != JNI_TRUE) );
188 * Sets the cursor position
190 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtTextAreaPeer_setCaretPosition
191 (JNIEnv *env, jobject obj, jint index)
193 QTextEdit *editor = (QTextEdit *) getNativeObject( env, obj );
194 assert( editor );
196 editor->textCursor().setPosition( index );