qtmenuitempeer.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* qtmenuitempeer.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 <QMenu>
  33. #include <QAction>
  34. #include <QThread>
  35. #include <gnu_java_awt_peer_qt_QtMenuItemPeer.h>
  36. #include "nativewrapper.h"
  37. #include "qtstrings.h"
  38. #include "mainthreadinterface.h"
  39. class MenuItemDestroyEvent : public AWTEvent {
  40. private:
  41. QAction *widget;
  42. public:
  43. MenuItemDestroyEvent(QAction *w)
  44. {
  45. widget = w;
  46. }
  47. void runEvent()
  48. {
  49. delete widget;
  50. }
  51. };
  52. class MenuItemLabelEvent : public AWTEvent {
  53. private:
  54. QAction *widget;
  55. QString *string;
  56. public:
  57. MenuItemLabelEvent(QAction *w, QString *s) : AWTEvent()
  58. {
  59. widget = w;
  60. string = s;
  61. }
  62. void runEvent()
  63. {
  64. widget->setText( *string );
  65. delete string;
  66. }
  67. };
  68. class MenuItemStatusEvent : public AWTEvent {
  69. private:
  70. QAction *widget;
  71. bool enabled;
  72. bool value;
  73. public:
  74. MenuItemStatusEvent(QAction *w, bool e, bool v) : AWTEvent()
  75. {
  76. widget = w;
  77. enabled = e;
  78. value = v;
  79. }
  80. void runEvent()
  81. {
  82. if( enabled )
  83. widget->setEnabled( value );
  84. else
  85. widget->setChecked( value );
  86. }
  87. };
  88. /*
  89. * Creates a QAction object
  90. */
  91. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtMenuItemPeer_create
  92. (JNIEnv *env, jobject obj, jstring label, jboolean isSeperator, jboolean isCheckable)
  93. {
  94. QAction *action;
  95. if(label == NULL || isSeperator == JNI_TRUE)
  96. {
  97. action = new QAction(NULL);
  98. action->setSeparator(true);
  99. assert( action );
  100. }
  101. else
  102. {
  103. QString *qStr = getQString(env, label);
  104. action = new QAction(*qStr, NULL);
  105. delete qStr;
  106. assert( action );
  107. action->setCheckable( (isCheckable == JNI_TRUE) );
  108. }
  109. setNativeObject( env, obj, action );
  110. }
  111. /**
  112. * Disposal.
  113. */
  114. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtMenuItemPeer_dispose
  115. (JNIEnv *env, jobject obj)
  116. {
  117. QAction *action = (QAction *)getNativeObject( env, obj );
  118. assert( action );
  119. mainThread->postEventToMain( new MenuItemDestroyEvent( action ) );
  120. }
  121. /*
  122. * Enables/disables the item
  123. */
  124. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtMenuItemPeer_setEnabled
  125. (JNIEnv *env, jobject obj, jboolean enabled)
  126. {
  127. QAction *action = (QAction *)getNativeObject( env, obj );
  128. assert( action );
  129. mainThread->postEventToMain(new MenuItemStatusEvent(action, true,
  130. (enabled == JNI_TRUE)));
  131. }
  132. /*
  133. * Sets the item label.
  134. */
  135. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtMenuItemPeer_setLabel
  136. (JNIEnv *env, jobject obj, jstring label)
  137. {
  138. QAction *action = (QAction *)getNativeObject( env, obj );
  139. assert( action );
  140. QString *qStr = getQString(env, label);
  141. mainThread->postEventToMain( new MenuItemLabelEvent( action, qStr ) );
  142. }
  143. /*
  144. * Sets the checkbox state.
  145. */
  146. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtMenuItemPeer_setState
  147. (JNIEnv *env, jobject obj, jboolean state)
  148. {
  149. QAction *action = (QAction *)getNativeObject( env, obj );
  150. assert( action );
  151. mainThread->postEventToMain(new MenuItemStatusEvent(action, false,
  152. (state == JNI_TRUE)));
  153. }