qttextfieldpeer.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* qttextfieldpeer.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 <QLineEdit>
  33. #include <QWidget>
  34. #include <gnu_java_awt_peer_qt_QtTextFieldPeer.h>
  35. #include "qtcomponent.h"
  36. #include "qtstrings.h"
  37. #include "slotcallbacks.h"
  38. #include "mainthreadinterface.h"
  39. class TFEchoChar : public AWTEvent {
  40. private:
  41. QLineEdit *line;
  42. jchar c;
  43. public:
  44. TFEchoChar(QLineEdit *w, jchar ch) : AWTEvent()
  45. {
  46. line = w;
  47. c = ch;
  48. }
  49. void runEvent()
  50. {
  51. line->setEchoMode( (c) ? QLineEdit::Password : QLineEdit::Normal );
  52. }
  53. };
  54. class TFEditable : public AWTEvent {
  55. private:
  56. QLineEdit *line;
  57. bool editable;
  58. public:
  59. TFEditable(QLineEdit *w, bool e) : AWTEvent()
  60. {
  61. line = w;
  62. editable = e;
  63. }
  64. void runEvent()
  65. {
  66. line->setReadOnly( editable );
  67. }
  68. };
  69. class TFSetText : public AWTEvent {
  70. private:
  71. QLineEdit *line;
  72. QString *text;
  73. public:
  74. TFSetText(QLineEdit *w, QString *t) : AWTEvent()
  75. {
  76. line = w;
  77. text = t;
  78. }
  79. void runEvent()
  80. {
  81. line->setText( *text );
  82. delete text;
  83. }
  84. };
  85. class TFSetCursorPos : public AWTEvent {
  86. private:
  87. QLineEdit *line;
  88. int pos;
  89. public:
  90. TFSetCursorPos(QLineEdit *w, int p) : AWTEvent()
  91. {
  92. line = w;
  93. pos = p;
  94. }
  95. void runEvent()
  96. {
  97. line->setCursorPosition(pos);
  98. }
  99. };
  100. class TFSelect : public AWTEvent {
  101. private:
  102. QLineEdit *line;
  103. int start,end;
  104. public:
  105. TFSelect(QLineEdit *w, int s, int e) : AWTEvent()
  106. {
  107. line = w;
  108. start = s;
  109. end = e;
  110. }
  111. void runEvent()
  112. {
  113. line->setSelection(start, end - start);
  114. }
  115. };
  116. /*
  117. */
  118. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtTextFieldPeer_init
  119. (JNIEnv *env, jobject obj)
  120. {
  121. QWidget *parentWidget = (QWidget *)getParentWidget(env, obj);
  122. assert( parentWidget );
  123. QLineEdit *line = new QLineEdit( parentWidget );
  124. assert( line );
  125. setNativeObject( env, obj, line );
  126. connectLineEdit(line, env, obj);
  127. }
  128. /*
  129. * Sets the echo char.
  130. */
  131. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtTextFieldPeer_setEchoChar
  132. (JNIEnv *env, jobject obj, jchar echo)
  133. {
  134. QLineEdit *line = (QLineEdit *) getNativeObject( env, obj );
  135. assert( line );
  136. mainThread->postEventToMain( new TFEchoChar( line, echo ) );
  137. }
  138. /*
  139. */
  140. JNIEXPORT jobject JNICALL Java_gnu_java_awt_peer_qt_QtTextFieldPeer_getMinimumSizeNative
  141. (JNIEnv *env, jobject obj, jint columns)
  142. {
  143. QLineEdit *line = (QLineEdit *) getNativeObject( env, obj );
  144. assert( line );
  145. // FIXME does this work?
  146. int old = line->maxLength();
  147. line->setMaxLength(columns);
  148. QSize size = line->minimumSizeHint();
  149. line->setMaxLength(old);
  150. return makeDimension(env, &size);
  151. }
  152. /*
  153. */
  154. JNIEXPORT jobject JNICALL Java_gnu_java_awt_peer_qt_QtTextFieldPeer_getPreferredSizeNative
  155. (JNIEnv *env, jobject obj, jint columns)
  156. {
  157. QLineEdit *line = (QLineEdit *) getNativeObject( env, obj );
  158. assert( line );
  159. int old = line->maxLength();
  160. line->setMaxLength(columns);
  161. QSize size = line->sizeHint();
  162. line->setMaxLength(old);
  163. return makeDimension(env, &size);
  164. }
  165. /*
  166. */
  167. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtTextFieldPeer_setEditable
  168. (JNIEnv *env, jobject obj, jboolean edit)
  169. {
  170. QLineEdit *line = (QLineEdit *) getNativeObject( env, obj );
  171. assert( line );
  172. mainThread->postEventToMain( new TFEditable( line, (edit != JNI_TRUE) ) );
  173. }
  174. /*
  175. * Gets the text.
  176. */
  177. JNIEXPORT jstring JNICALL Java_gnu_java_awt_peer_qt_QtTextFieldPeer_getText
  178. (JNIEnv *env, jobject obj)
  179. {
  180. QLineEdit *line = (QLineEdit *) getNativeObject( env, obj );
  181. assert( line );
  182. QString text = line->text();
  183. return getJavaString(env, &text);
  184. }
  185. /*
  186. * Sets the text
  187. */
  188. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtTextFieldPeer_setText
  189. (JNIEnv *env, jobject obj, jstring text)
  190. {
  191. QLineEdit *line = (QLineEdit *) getNativeObject( env, obj );
  192. assert( line );
  193. QString *qStr = getQString(env, text);
  194. mainThread->postEventToMain( new TFSetText( line, qStr ) );
  195. }
  196. /*
  197. * Returns the start (start = true) or end (start = false) of the selection.
  198. */
  199. JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_qt_QtTextFieldPeer_getSelection
  200. (JNIEnv *env, jobject obj, jboolean start)
  201. {
  202. int index, length;
  203. QLineEdit *line = (QLineEdit *) getNativeObject( env, obj );
  204. assert( line );
  205. index = line->selectionStart();
  206. if(start == JNI_TRUE)
  207. return index;
  208. length = (line->selectedText()).length();
  209. return index + length;
  210. }
  211. /*
  212. * Sets the selection.
  213. */
  214. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtTextFieldPeer_select
  215. (JNIEnv *env, jobject obj, jint start, jint end)
  216. {
  217. QLineEdit *line = (QLineEdit *) getNativeObject( env, obj );
  218. assert( line );
  219. mainThread->postEventToMain( new TFSelect( line, start, end ) );
  220. }
  221. /*
  222. * Sets the cursor position.
  223. */
  224. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtTextFieldPeer_setCaretPosition
  225. (JNIEnv *env, jobject obj, jint pos)
  226. {
  227. QLineEdit *line = (QLineEdit *) getNativeObject( env, obj );
  228. assert( line );
  229. mainThread->postEventToMain( new TFSetCursorPos( line, (int)pos ) );
  230. }
  231. /*
  232. * Returns the caret position.
  233. */
  234. JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_qt_QtTextFieldPeer_getCaretPosition
  235. (JNIEnv *env, jobject obj)
  236. {
  237. QLineEdit *line = (QLineEdit *) getNativeObject( env, obj );
  238. assert( line );
  239. return line->cursorPosition();
  240. }