qtscrollpanepeer.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* qtscrollpanepeer.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 <QScrollArea>
  33. #include <QScrollBar>
  34. #include <gnu_java_awt_peer_qt_QtScrollPanePeer.h>
  35. #include "qtcomponent.h"
  36. #include "containers.h"
  37. #include "mainthreadinterface.h"
  38. #include "componentevent.h"
  39. #include "keybindings.h"
  40. // Constants in java.awt.ScrollPane
  41. #define SCROLLBARS_AS_NEEDED 0
  42. #define SCROLLBARS_ALWAYS 1
  43. #define SCROLLBARS_NEVER 2
  44. class MyScrollArea : public QScrollArea
  45. {
  46. public:
  47. MyScrollArea(JNIEnv *env, jobject obj, QWidget *parent) : QScrollArea( parent )
  48. {
  49. setup(env, obj);
  50. }
  51. ~MyScrollArea()
  52. {
  53. destroy();
  54. }
  55. #define I_KNOW_WHAT_IM_DOING
  56. #define PARENT QScrollArea
  57. #include "eventmethods.h"
  58. };
  59. class ScrollPanePolicy : public AWTEvent {
  60. private:
  61. QScrollArea *widget;
  62. Qt::ScrollBarPolicy policy;
  63. public:
  64. ScrollPanePolicy(QScrollArea *w, Qt::ScrollBarPolicy p) : AWTEvent()
  65. {
  66. widget = w;
  67. policy = p;
  68. }
  69. void runEvent()
  70. {
  71. widget->setHorizontalScrollBarPolicy(policy);
  72. widget->setVerticalScrollBarPolicy(policy);
  73. }
  74. };
  75. /**
  76. * Returns the child widget, given the owner Component.
  77. */
  78. QWidget *scrollPaneChildWidget( JNIEnv *env, jobject component )
  79. {
  80. jclass scrollpaneCls = env->FindClass( "java/awt/ScrollPane" );
  81. jmethodID getPeerMID = env->GetMethodID( scrollpaneCls,
  82. "getPeer",
  83. "()Ljava/awt/peer/ComponentPeer;");
  84. assert(getPeerMID != 0);
  85. jobject scrollpanepeerobj = env->CallObjectMethod( component, getPeerMID, NULL );
  86. QScrollArea *view = (QScrollArea *) getNativeObject( env, scrollpanepeerobj );
  87. assert(view != 0);
  88. return view->viewport();
  89. }
  90. /*
  91. * Creates a QScrollArea object.
  92. */
  93. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtScrollPanePeer_init
  94. (JNIEnv *env, jobject obj)
  95. {
  96. QWidget *parentWidget = (QWidget *) getParentWidget( env, obj );
  97. assert( parentWidget );
  98. // QScrollArea *pane = new MyScrollArea( env, obj, parentWidget );
  99. QScrollArea *pane = new QScrollArea( parentWidget );
  100. assert( pane );
  101. setNativeObject( env, obj, pane );
  102. }
  103. /*
  104. * Resize the child.
  105. */
  106. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtScrollPanePeer_childResized
  107. (JNIEnv *env, jobject obj, jint w, jint h)
  108. {
  109. QScrollArea *view = (QScrollArea *) getNativeObject( env, obj );
  110. assert( view );
  111. QWidget *child = view->viewport();
  112. assert( child );
  113. // child->setGeometry( 0, 0, w, h );
  114. // child->update();
  115. mainThread->postEventToMain( new AWTResizeEvent(child, 0, 0, w, h) );
  116. }
  117. /*
  118. * Returns the horizontal scrollbar height.
  119. */
  120. JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_qt_QtScrollPanePeer_getHScrollbarHeight
  121. (JNIEnv *env, jobject obj)
  122. {
  123. QScrollArea *pane = (QScrollArea *) getNativeObject( env, obj );
  124. assert( pane );
  125. QScrollBar *hbar = pane->horizontalScrollBar();
  126. if(hbar == NULL)
  127. return 0;
  128. if(!hbar->isVisible())
  129. return 0;
  130. int height = hbar->height();
  131. return height;
  132. }
  133. /*
  134. * Returns the vertical scrollbar width.
  135. */
  136. JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_qt_QtScrollPanePeer_getVScrollbarWidth
  137. (JNIEnv *env, jobject obj)
  138. {
  139. QScrollArea *pane = (QScrollArea *) getNativeObject( env, obj );
  140. assert( pane );
  141. QScrollBar *vbar = pane->verticalScrollBar();
  142. if(vbar == NULL)
  143. return 0;
  144. if(!vbar->isVisible())
  145. return 0;
  146. int width = vbar->width();
  147. return width;
  148. }
  149. /*
  150. * Sets the current upper-left corner to x, y.
  151. */
  152. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtScrollPanePeer_setScrollPosition
  153. (JNIEnv *env, jobject obj, jint x, jint y)
  154. {
  155. QScrollArea *pane = (QScrollArea *) getNativeObject( env, obj );
  156. assert( pane );
  157. // pane->scrollContentsBy( x, y );
  158. }
  159. /*
  160. * Sets the scrollbar policy
  161. */
  162. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtScrollPanePeer_setPolicy
  163. (JNIEnv *env, jobject obj, jint policy)
  164. {
  165. QScrollArea *pane = (QScrollArea *) getNativeObject( env, obj );
  166. assert( pane );
  167. Qt::ScrollBarPolicy qtpolicy;
  168. switch( policy )
  169. {
  170. case SCROLLBARS_ALWAYS:
  171. qtpolicy = Qt::ScrollBarAlwaysOn;
  172. break;
  173. case SCROLLBARS_NEVER:
  174. qtpolicy = Qt::ScrollBarAlwaysOff;
  175. break;
  176. default:
  177. case SCROLLBARS_AS_NEEDED:
  178. qtpolicy = Qt::ScrollBarAsNeeded;
  179. break;
  180. }
  181. mainThread->postEventToMain( new ScrollPanePolicy( pane, qtpolicy ) );
  182. }