qttextareapeer.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* qttextareapeer.cpp --
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. #include <assert.h>
  32. #include <time.h>
  33. #include <QTextEdit>
  34. #include <QTextCursor>
  35. #include <gnu_java_awt_peer_qt_QtTextAreaPeer.h>
  36. #include "mainthreadinterface.h"
  37. #include "componentevent.h"
  38. #include "slotcallbacks.h"
  39. #include "qtcomponent.h"
  40. #include "qtstrings.h"
  41. class TASetText : public AWTEvent {
  42. private:
  43. QTextEdit *area;
  44. QString *text;
  45. public:
  46. TASetText(QTextEdit *w, QString *t) : AWTEvent()
  47. {
  48. area = w;
  49. text = t;
  50. }
  51. void runEvent()
  52. {
  53. area->setPlainText( *text );
  54. delete text;
  55. }
  56. };
  57. /*
  58. * Construct a QTextEdit object
  59. */
  60. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtTextAreaPeer_init
  61. (JNIEnv *env, jobject obj)
  62. {
  63. QWidget *parentWidget = (QWidget *)getParentWidget( env, obj );
  64. assert( parentWidget );
  65. QTextEdit *editor = new QTextEdit( parentWidget );
  66. editor->setGeometry( 0, 0, 400, 400 );
  67. assert( editor );
  68. // setLineWrapColumnOrWidth ( int w );
  69. setNativeObject( env, obj, editor );
  70. // Connect TextChanged events.
  71. connectTextEdit(editor, env, obj);
  72. }
  73. /*
  74. * Returns the cursor position.
  75. */
  76. JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_qt_QtTextAreaPeer_getCaretPosition
  77. (JNIEnv *env, jobject obj)
  78. {
  79. int index;
  80. QTextEdit *editor = (QTextEdit *) getNativeObject( env, obj );
  81. assert( editor );
  82. index = editor->textCursor().position();;
  83. return (jint)index;
  84. }
  85. /*
  86. * Returns the char index at a given screen point
  87. */
  88. JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_qt_QtTextAreaPeer_getIndexAtPoint
  89. (JNIEnv *env, jobject obj, jint x, jint y)
  90. {
  91. QPoint *p = new QPoint(x,y);
  92. QTextEdit *editor = (QTextEdit *) getNativeObject( env, obj );
  93. assert( editor );
  94. QTextCursor curs = editor->cursorForPosition( *p );
  95. delete p;
  96. return curs.position();
  97. }
  98. /*
  99. * Returns the start (start = true) or end (start = false) of the selection.
  100. */
  101. JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_qt_QtTextAreaPeer_getSelection
  102. (JNIEnv *env, jobject obj, jboolean isStart)
  103. {
  104. int start, end;
  105. QTextEdit *editor = (QTextEdit *) getNativeObject( env, obj );
  106. assert( editor );
  107. start = editor->textCursor().selectionStart();
  108. end = editor->textCursor().selectionEnd();
  109. return ((isStart == JNI_TRUE) ? start : end);
  110. }
  111. /*
  112. * Returns the text.
  113. */
  114. JNIEXPORT jstring JNICALL Java_gnu_java_awt_peer_qt_QtTextAreaPeer_getText
  115. (JNIEnv *env, jobject obj)
  116. {
  117. QTextEdit *editor = (QTextEdit *) getNativeObject( env, obj );
  118. assert( editor );
  119. QString text = editor->toPlainText();
  120. return getJavaString(env, &text);
  121. }
  122. /*
  123. * Sets the editor text.
  124. */
  125. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtTextAreaPeer_setText
  126. (JNIEnv *env, jobject obj, jstring str)
  127. {
  128. QTextEdit *editor = (QTextEdit *) getNativeObject( env, obj );
  129. assert( editor );
  130. QString *qStr = getQString(env, str);
  131. mainThread->postEventToMain( new TASetText( editor, qStr ) );
  132. }
  133. /*
  134. * Sets the selection.
  135. */
  136. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtTextAreaPeer_select
  137. (JNIEnv *env, jobject obj, jint startpos, jint endpos)
  138. {
  139. QTextEdit *editor = (QTextEdit *) getNativeObject( env, obj );
  140. assert( editor );
  141. QTextCursor curs(editor->document());
  142. curs.setPosition(startpos);
  143. curs.setPosition(endpos, QTextCursor::KeepAnchor);
  144. editor->setTextCursor( curs );
  145. }
  146. /*
  147. * Allow or disallow editing.
  148. */
  149. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtTextAreaPeer_setEditable
  150. (JNIEnv *env, jobject obj, jboolean editable)
  151. {
  152. QTextEdit *editor = (QTextEdit *) getNativeObject( env, obj );
  153. assert( editor );
  154. editor->setReadOnly( (editable != JNI_TRUE) );
  155. }
  156. /*
  157. * Sets the cursor position
  158. */
  159. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtTextAreaPeer_setCaretPosition
  160. (JNIEnv *env, jobject obj, jint index)
  161. {
  162. QTextEdit *editor = (QTextEdit *) getNativeObject( env, obj );
  163. assert( editor );
  164. editor->textCursor().setPosition( index );
  165. }