simple implementation of PopupMenu using GtkPopoverMenu

This commit is contained in:
Julian Winkler
2024-03-16 22:56:40 +01:00
parent 6513195b9e
commit 14217e8724
4 changed files with 230 additions and 2 deletions

View File

@@ -0,0 +1,45 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class android_widget_PopupMenu */
#ifndef _Included_android_widget_PopupMenu
#define _Included_android_widget_PopupMenu
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: android_widget_PopupMenu
* Method: native_init
* Signature: ()J
*/
JNIEXPORT jlong JNICALL Java_android_widget_PopupMenu_native_1init
(JNIEnv *, jobject);
/*
* Class: android_widget_PopupMenu
* Method: native_appendItem
* Signature: (JLjava/lang/String;I)V
*/
JNIEXPORT void JNICALL Java_android_widget_PopupMenu_native_1appendItem
(JNIEnv *, jobject, jlong, jstring, jint);
/*
* Class: android_widget_PopupMenu
* Method: native_buildPopover
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_android_widget_PopupMenu_native_1buildPopover
(JNIEnv *, jobject, jlong);
/*
* Class: android_widget_PopupMenu
* Method: native_show
* Signature: (JJ)V
*/
JNIEXPORT void JNICALL Java_android_widget_PopupMenu_native_1show
(JNIEnv *, jobject, jlong, jlong);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,50 @@
#include <gtk/gtk.h>
#include "../defines.h"
#include "../util.h"
#include "../generated_headers/android_widget_PopupMenu.h"
JNIEXPORT jlong JNICALL Java_android_widget_PopupMenu_native_1init(JNIEnv *env, jobject this)
{
return _INTPTR(g_menu_new());
}
JNIEXPORT void JNICALL Java_android_widget_PopupMenu_native_1appendItem(JNIEnv *env, jobject this, jlong menu_ptr, jstring title_jstr, jint id)
{
const gchar *title = (*env)->GetStringUTFChars(env, title_jstr, NULL);
GMenuItem *item = g_menu_item_new(title, NULL);
(*env)->ReleaseStringUTFChars(env, title_jstr, title);
g_menu_item_set_action_and_target(item, "popupmenu.clicked", "i", id);
g_menu_append_item(G_MENU(_PTR(menu_ptr)), item);
}
static void popupmenu_activated(GSimpleAction *action, GVariant *parameter, gpointer user_data) {
int id = g_variant_get_int32(parameter);
JNIEnv *env = get_jni_env();
jobject this = (jobject)user_data;
jmethodID onMenuItemClick = _METHOD(_CLASS(this), "menuItemClickCallback", "(I)V");
(*env)->CallVoidMethod(env, this, onMenuItemClick, id);
if ((*env)->ExceptionCheck(env))
(*env)->ExceptionDescribe(env);
}
static const GActionEntry action_entry = { "clicked", popupmenu_activated, "i", NULL, NULL };
JNIEXPORT jlong JNICALL Java_android_widget_PopupMenu_native_1buildPopover(JNIEnv *env, jobject this, jlong menu_ptr)
{
GtkWidget *popover = gtk_popover_menu_new_from_model(G_MENU_MODEL(_PTR(menu_ptr)));
GSimpleActionGroup *group = g_simple_action_group_new();
g_action_map_add_action_entries(G_ACTION_MAP(group), &action_entry, 1, _REF(this));
gtk_widget_insert_action_group(popover, "popupmenu", G_ACTION_GROUP(group));
return _INTPTR(popover);
}
JNIEXPORT void JNICALL Java_android_widget_PopupMenu_native_1show(JNIEnv *env, jobject this, jlong popover_ptr, jlong anchor_ptr)
{
GtkWidget *anchor = gtk_widget_get_parent(GTK_WIDGET(_PTR(anchor_ptr)));
GtkPopover *popover = GTK_POPOVER(_PTR(popover_ptr));
gtk_widget_insert_before(GTK_WIDGET(popover), GTK_WIDGET(anchor), NULL);
gtk_popover_present(popover);
gtk_popover_popup(popover);
}