How to get button text in GTK?

I am developing an application with a numeric keypad and a text box when I click a button, a number is displayed in the text box. Do I need to write a function for each button? Or can you pass the text and widget as a parameter?

#include <stdio.h> #include <stdlib.h> #include <gtk/gtk.h> void callback( GtkWidget *widget, gpointer data ) { gtk_entry_append_text(entry, text); } void create_button(GtkWidget* table,GtkWidget* entry, int start_r,int end_r, int start_c,int end_c, char* label) { GtkWidget *button; button = gtk_button_new_with_label (label); g_object_set_data( G_OBJECT( button ), "char", (gpointer)label ); g_signal_connect (button, "clicked", G_CALLBACK (callback), entry); gtk_table_attach_defaults (GTK_TABLE(table), button, start_c, end_c, start_r, end_r); gtk_widget_show (button); } gint delete_event( GtkWidget *widget, GdkEvent *event, gpointer data ) { gtk_main_quit (); return(FALSE); } int main(int argc,char* argv[]){ GtkWidget *window; //GtkWidget *button; GtkWidget *table; GtkWidget *entry; //GtkWidget *label; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_title (GTK_WINDOW (window), "Armario"); g_signal_connect (GTK_OBJECT (window), "delete_event", G_CALLBACK (delete_event), NULL); gtk_container_set_border_width (GTK_CONTAINER (window), 20); table = gtk_table_new (2, 2, TRUE); gtk_container_add (GTK_CONTAINER (window), table); entry = gtk_entry_new(); gtk_entry_set_max_length(GTK_ENTRY(entry),10); gtk_entry_set_placeholder_text(GTK_ENTRY(entry),"Teste"); gtk_table_attach_defaults (GTK_TABLE(table), entry, 0, 2, 1, 2); gtk_widget_show(entry); create_button(table,entry,0,1,0,1,"Botao"); gtk_widget_show (table); gtk_widget_show (window); gtk_main (); return 0; } 
+4
source share
2 answers

The answer to your question: yes, you can pass the widget to Gtk + for a callback. In fact, the first callback parameter for the clicked signal is the button that received the signal (that is, usually the button pressed). As you can see in the code example below, you can extract your label from the button and use it as text.

 [...] /* In create_button... */ /* Make your buttons be notified when they are clicked */ g_signal_connect (button, "clicked", G_CALLBACK (on_button_clicked), entry); [...] /* Append the text in the button to the text entry */ void on_button_clicked (GtkButton *button, gpointer user_data) { GtkEntry *entry = user_data; const gchar *text = gtk_button_get_label (button); gint position = 0; gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &position); } 

I use because I have been out of date for a long time. Passing the write parameter to the callback is possible using the last parameter g_signal_connect , which allows you to specify some data that you need to receive in your callback. This information is then made available for callback in the user_data parameter.

Your example can also be improved with gtk_widget_show_all , and I also don't see the point of calling g_object_set_data on "char", since the text is already set in the label property (and obtained with gtk_button_get_label ).

+8
source

Two options that I can think of:

  • GTK widgets are truly GObjects, so you can attach arbitrary pieces of data to them. See g_object_set_data/g_object_set_data_full/g_object_get_data . Thus, you can simply add text to the button as attached data and get it when necessary.

  • You can transfer any data needed for a callback by defining a structure with all fields and passing a pointer to it. If the structure cannot be declared statically, you can malloc it and use g_signal_connect_data to specify the function to release the data:

For instance:

 struct entry_and_text; { GtkWidget *w; char *text; }; void free_data(gpointer data, GClosure *closure) { free(data); } entry_and_text *data = (entry_and_text *)malloc(sizeof(entry_and_text)); data->w = entry; data->text = label; g_signal_connect_data (button, "clicked", G_CALLBACK (callback), data, free_data, 0); 
+1
source

Source: https://habr.com/ru/post/1396946/


All Articles