qtmenupeer.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* qtmenupeer.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 <gnu_java_awt_peer_qt_QtMenuPeer.h>
  34. #include "nativewrapper.h"
  35. #include "qtstrings.h"
  36. #include "mainthreadinterface.h"
  37. #include "slotcallbacks.h"
  38. #include "componentevent.h"
  39. typedef enum ActionType {
  40. ActionMenu,
  41. ActionItem,
  42. ActionSeparator
  43. } ActionType;
  44. // Sets the title, but also tear-off.
  45. class MenuTitleEvent : public AWTEvent {
  46. private:
  47. QMenu *widget;
  48. QString *string;
  49. bool tearOff;
  50. public:
  51. MenuTitleEvent(QMenu *w, QString *s, bool tear) : AWTEvent()
  52. {
  53. widget = w;
  54. string = s;
  55. tearOff = tear;
  56. }
  57. void runEvent()
  58. {
  59. if (tearOff)
  60. widget->setTearOffEnabled( true );
  61. else
  62. {
  63. widget->setTitle( *string );
  64. delete string;
  65. }
  66. }
  67. };
  68. class MenuAction : public AWTEvent {
  69. private:
  70. QMenu *menu;
  71. QAction *action;
  72. ActionType actionType; // type of action to add
  73. JavaVM *vm;
  74. jobject menuPeer;
  75. jobject itemPeer;
  76. public:
  77. MenuAction(JNIEnv *env, jobject mp, jobject ip, QMenu *m, QAction *a,
  78. ActionType actionType) : AWTEvent()
  79. {
  80. menu = m;
  81. action = a;
  82. this->actionType = actionType;
  83. env->GetJavaVM( &vm );
  84. menuPeer = env->NewGlobalRef( mp );
  85. if( ip != NULL )
  86. itemPeer = env->NewGlobalRef( ip );
  87. else
  88. itemPeer = NULL;
  89. }
  90. void runEvent()
  91. {
  92. JNIEnv *env;
  93. vm->GetEnv((void **)&env, JNI_VERSION_1_1);
  94. if (actionType == ActionMenu)
  95. menu->addMenu ((QMenu *) action);
  96. else
  97. menu->addAction (action);
  98. jclass menuCls = env->GetObjectClass( menuPeer );
  99. jmethodID mid = env->GetMethodID(menuCls, "add", "(J)V");
  100. env->DeleteLocalRef(menuCls);
  101. env->CallVoidMethod( menuPeer, mid, (jlong)action );
  102. env->DeleteGlobalRef( menuPeer );
  103. if( itemPeer != NULL )
  104. {
  105. setNativeObject( env, itemPeer, action );
  106. connectAction(action, env, itemPeer);
  107. env->DeleteGlobalRef( itemPeer );
  108. }
  109. }
  110. };
  111. class MenuRemoveAction : public AWTEvent {
  112. private:
  113. QMenu *menu;
  114. QAction *action;
  115. public:
  116. MenuRemoveAction(QMenu *m, QAction *a) : AWTEvent()
  117. {
  118. menu = m;
  119. action = a;
  120. }
  121. void runEvent()
  122. {
  123. if (action)
  124. menu->removeAction(action);
  125. }
  126. };
  127. /*
  128. * Constructs a QMenu item
  129. */
  130. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtMenuPeer_init
  131. (JNIEnv *env, jobject obj)
  132. {
  133. QMenu *menu = new QMenu();
  134. assert( menu );
  135. setNativeObject( env, obj, menu );
  136. }
  137. /**
  138. * Allows tear-off: Only called once, if ever.
  139. */
  140. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtMenuPeer_allowTearOff
  141. (JNIEnv *env, jobject obj)
  142. {
  143. QMenu *menu = (QMenu *)getNativeObject( env, obj );
  144. assert( menu );
  145. mainThread->postEventToMain( new MenuTitleEvent( menu, NULL, true ) );
  146. }
  147. /*
  148. * Inserts a seperator.
  149. */
  150. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtMenuPeer_insertSeperator
  151. (JNIEnv *env, jobject obj)
  152. {
  153. QMenu *menu = (QMenu *)getNativeObject( env, obj );
  154. assert( menu );
  155. mainThread->postEventToMain( new MenuAction( env, obj, NULL,
  156. menu, NULL, ActionSeparator ) );
  157. }
  158. /*
  159. * Inserts an item.
  160. */
  161. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtMenuPeer_insertItem
  162. (JNIEnv *env, jobject obj, jobject item)
  163. {
  164. QMenu *menu = (QMenu *)getNativeObject( env, obj );
  165. assert( menu );
  166. QAction *action = (QAction *)getNativeObject( env, item );
  167. assert( action );
  168. mainThread->postEventToMain( new MenuAction( env, obj, item, menu, action, ActionItem ));
  169. }
  170. /*
  171. * Inserts a sub-menu
  172. */
  173. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtMenuPeer_insertMenu
  174. (JNIEnv *env, jobject obj, jobject menu)
  175. {
  176. assert( menu );
  177. QMenu *thisMenu = (QMenu *)getNativeObject( env, obj );
  178. assert( thisMenu );
  179. QMenu *insMenu = (QMenu *)getNativeObject(env, menu);
  180. assert( insMenu );
  181. mainThread->postEventToMain( new MenuAction( env, obj, menu, thisMenu, (QAction *)insMenu, ActionMenu ) );
  182. }
  183. /*
  184. * Removes an item at index.
  185. */
  186. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtMenuPeer_delItem
  187. (JNIEnv *env, jobject obj, jlong ptr)
  188. {
  189. QMenu *menu = (QMenu *)getNativeObject( env, obj );
  190. assert( menu );
  191. QAction *action = (QAction *)ptr;
  192. mainThread->postEventToMain( new MenuRemoveAction( menu, action ) );
  193. }
  194. /*
  195. * Enables/Disables the menu.
  196. */
  197. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtMenuPeer_setEnabled
  198. (JNIEnv *env, jobject obj, jboolean enabled)
  199. {
  200. QMenu *menu = (QMenu *)getNativeObject( env, obj );
  201. assert( menu );
  202. mainThread->postEventToMain(new AWTEnableEvent(menu, (enabled == JNI_TRUE)));
  203. }
  204. /*
  205. * Sets the menu title.
  206. */
  207. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtMenuPeer_setLabel
  208. (JNIEnv *env, jobject obj, jstring label)
  209. {
  210. if(label == NULL)
  211. return;
  212. QMenu *menu = (QMenu *)getNativeObject( env, obj );
  213. assert( menu );
  214. QString *qStr = getQString(env, label);
  215. mainThread->postEventToMain( new MenuTitleEvent( menu, qStr, false ) );
  216. }