0

I have a sample GTK4 application where a list of entries is arranged vertically in a scrolled window. When I test the application using a mouse by continuously scrolling the window, the app does not crash. However, when I test it on a touch device and click on an entry, a text field cursor appears. This cursor remains visible even while scrolling the window, eventually causing the application to crash with a segmentation fault.

I tried disabling events for the entries using

gtk_widget_set_sensitive(entry, FALSE),

which made the application stable. However, this also disables all interactions with the entries, which is not ideal since I need to enter and retrieve data from them.

Could anyone suggest a way to resolve this issue?

#include <gtk/gtk.h>

GtkWidget *entry[15];
GtkEntryBuffer *buffer[15];


// Focus change handler
static void on_entry_focus_change(GtkWidget *widget, GParamSpec *pspec, gpointer user_data) {
    if (gtk_widget_has_focus(widget)) {
        g_print("Entry gained focus\n");
    } else {
        g_print("Entry lost focus\n");
    }
}


// Scroll handling
void on_scroll(GtkAdjustment *adjustment) {
    double value = gtk_adjustment_get_value(adjustment);
    g_print("Scrolled to: %.2f\n", value);
}

// Create multiple entries
static void activate(GtkApplication *app, gpointer user_data) {
    GtkWidget *window = gtk_application_window_new(app);
    gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
    gtk_window_set_default_size(GTK_WINDOW(window), 500, 800);


    GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
    gtk_window_set_child(GTK_WINDOW(window), box);

    GtkWidget *scrolled_window = gtk_scrolled_window_new();
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_NEVER, GTK_POLICY_EXTERNAL);
    gtk_scrolled_window_set_kinetic_scrolling(GTK_SCROLLED_WINDOW(scrolled_window), TRUE);
    gtk_widget_set_size_request(scrolled_window, 400, 750);
    gtk_widget_set_margin_start(scrolled_window, 40);
    gtk_widget_set_margin_end(scrolled_window, 40);
    gtk_box_append(GTK_BOX(box), scrolled_window);

    GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scrolled_window));
    GtkAdjustment *hadj = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(scrolled_window));

    g_signal_connect(vadj, "value-changed", G_CALLBACK(on_scroll), NULL);
    g_signal_connect(hadj, "value-changed", G_CALLBACK(on_scroll), NULL);

    GtkWidget *s_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
    gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scrolled_window), s_vbox);

    // Create entries
    for (int i = 0; i < 15; i++) {
        buffer[i] = gtk_entry_buffer_new("Initial Text", -1);
        if (buffer[i] == NULL) {
            g_print("Failed to create buffer for entry %d\n", i);
            continue;
        }

        entry[i] = gtk_entry_new_with_buffer(buffer[i]);
        if (entry[i] == NULL) {
            g_print("Failed to create entry %d\n", i);
            continue;
        }

        gtk_widget_set_size_request(entry[i], 396, 60);
        gtk_entry_set_input_hints(GTK_ENTRY(entry[i]), GTK_INPUT_HINT_NO_EMOJI);

        char *placeholder_text = g_strdup_printf("Entry %d", i + 1);
        gtk_entry_set_placeholder_text(GTK_ENTRY(entry[i]), placeholder_text);
        g_free(placeholder_text);

        // Connect focus change signal
        g_signal_connect(entry[i], "notify::has-focus", G_CALLBACK(on_entry_focus_change), NULL);


        gtk_box_append(GTK_BOX(s_vbox), entry[i]);
        g_print("Created entry %d\n", i);
    }

    gtk_window_present(GTK_WINDOW(window));
}

int main(int argc, char *argv[]) {
    GtkApplication *app = gtk_application_new("com.example.GtkMultipleEntries", G_APPLICATION_FLAGS_NONE);
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    int status = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref(app);
    return status;
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.