cgtkgtk2

how i write a code for delete from cursor in gtk entry


how i write a code for delete from cursor in gtk entry

i write a code When I enter a text I delete it from button image of entry button.

i try to delete only one character from entry cursor

i use g_signal_connect(): delete-from-cursor

but in callback (function) I do not know what to do

i use this code from source https://docs.gtk.org/gtk3/signal.Entry.delete-from-cursor.html

the function void clicked_callback(GtkEntry* entry,GtkDeleteType type,gint count,gpointer user_data) {

the code of application :

#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include<stdio.h>
void clicked_callback(GtkEntry* entry,GtkDeleteType type,gint count,gpointer user_data)
{
    g_print("pressed");
}
int main(int argc, char **argv)
{
    gtk_init(&argc,&argv);
    GtkWidget *window;
    GtkWidget *entry;
    GtkWidget *grid;
    GtkWidget *button;
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window),200,200);
    grid = gtk_grid_new();
    gtk_container_add(GTK_CONTAINER(window),grid);
    entry = gtk_entry_new();
    gtk_widget_set_name(entry,"newname");
    gtk_container_add(GTK_CONTAINER(window),entry);
    gtk_grid_attach(GTK_GRID(grid), entry, 0, 0, 5, 1);
    button = gtk_button_new_with_label("I");
    gtk_grid_attach(GTK_GRID(grid),button, 0, 1, 1, 1);
    g_signal_connect_swapped(button, "delete_from_cursor", G_CALLBACK(clicked_callback), entry);
    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}

Solution

  • I found the answer. The code here will be in Python, but I'll explain the principles, so hopefully it won't be to hard to translate to C.

    What you need to do first is connect a function to the button's clicked signal. In Python, it looks like this:

    button = Gtk.Button.new_with_label("I")
    button.connect("clicked", button_clicked_function)
    

    In that function, you need to call the entry's delete_from_cursor() method, with GtkDeleteType of CHARS for the type argument, and a -1 for the count argument. This -1 tells it to delete 1 letter to the left of the cursor, instead of the right. Here is what this looks like in Python:

    def button_clicked_function(button):
        entry.do_delete_from_cursor(entry, Gtk.DeleteType.CHARS, -1)