set dialog not resizable or create another gtk window - c

I have a gtk_window which contain 4 buttons.
One of these buttons will open a file selection dialog (another function) which - when the file has been choosed - will show up a dialog with 3 gtk_entry (this function).
static void function_with_3_gtk_entry (gchar *message, gpointer mainwin){
GtkWidget *dialog, *label, *content_area, *entry1, *entry2, *entry3;
/* Create the widgets */
dialog = gtk_dialog_new_with_buttons ("Nome File", mainwin, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_OK, GTK_RESPONSE_NONE, NULL);
content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
entry1 = gtk_entry_new();
entry2 = gtk_entry_new();
entry3 = gtk_entry_new();
gtk_widget_set_size_request(dialog, 250, 200);
/* Ensure that the dialog box is destroyed when the user responds */
g_signal_connect_swapped (dialog, "response", G_CALLBACK (gtk_widget_destroy), dialog);
/* Add the label, and show everything we've added to the dialog */
gtk_container_add (GTK_CONTAINER (content_area), entry1);
gtk_container_add (GTK_CONTAINER (content_area), entry2);
gtk_container_add (GTK_CONTAINER (content_area), entry3);
gtk_widget_show_all (dialog);
}
My questions are:
can i use another gtk_window into this function instead of a gtk_dialog?
how can i set the dialog not resizable?

The code snippet below has two functions:
create_dialog() creates a dialog which cannot be resized, as requested.
create_window() creates a window, within a function, as requested.
Hope this helps.
#include <gtk/gtk.h>
/* Create a dialog, which cannot be resized by the user. */
void create_dialog(GtkWidget *button, gpointer window) {
GtkWidget *dialog, *label, *content_area;
/* New label for dialog content */
label = gtk_label_new("This is a dialog!");
/* Make a new dialog with an 'OK' button */
dialog = gtk_dialog_new_with_buttons("This is a dialog, which (shouldn't | can't) be resized!", window, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_OK, GTK_RESPONSE_NONE, NULL);
/* Add label to dialog */
content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
gtk_container_add(GTK_CONTAINER(content_area), label);
/* Destroy dialog properly */
g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog);
/* Set dialog to not resize. */
gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
gtk_widget_show_all(dialog);
}
/* Create a window, while in a function! */
void create_window(GtkWidget *button, gpointer window) {
GtkWidget *new_window, *label;
/*New label for dialog content */
label = gtk_label_new("This is a window, created in a function!");
/* Create new window */
new_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
/* Add label to window */
gtk_container_add(GTK_CONTAINER(new_window), label);
gtk_widget_show_all(new_window);
}
/* Boring implementation bits */
gint main(gint argc, char **argv) {
/* Initialise GTK+ */
gtk_init(&argc, &argv);
GtkWidget *main_win, *dialog_button, *window_button, *button_container;
/* Create a button, one for each function. */
dialog_button = gtk_button_new_with_label("Create dialog!");
window_button = gtk_button_new_with_label("Create window!");
/* Pack buttons into a box. */
button_container = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
gtk_box_pack_start(GTK_BOX(button_container), dialog_button, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(button_container), window_button, FALSE, FALSE, 0);
/* Connect signals to button callback functions */
g_signal_connect(dialog_button, "clicked", G_CALLBACK(create_dialog), main_win);
g_signal_connect(window_button, "clicked", G_CALLBACK(create_window), main_win);
/* Create a new window, show it, and run GTK+ */
main_win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(main_win, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_container_add(GTK_CONTAINER(main_win), button_container);
gtk_widget_show_all(main_win);
gtk_main();
return 0;
}

Related

GTK3 resize attempt grows window exponentially

I have a C GTK3 program that has a notebook with two images. I want to be able to grab the corner of the window and adjust the size of the image currently displayed. What I currently have is a program that once started, the window keeps growing until I kill it from the terminal using ctrl-c. I put a sleep call in the callback to slow it down, but it still grows. How do I stop the window from growing unless I "grab" a corner of the window and adjust it myself?
#include <stdio.h>
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
GtkWidget *notebook;
gboolean resize_image(GtkWidget *widget, GdkRectangle *allocation,
gpointer user_data)
{
int w,h, pagenum;
GdkPixbuf *pxbscaled;
GtkWidget *image;
GdkPixbuf *pixbuf;
pagenum = gtk_notebook_get_current_page (GTK_NOTEBOOK(notebook));
image = gtk_notebook_get_nth_page (GTK_NOTEBOOK(notebook), pagenum);
// GtkImageType image_type = gtk_image_get_storage_type
// (GTK_IMAGE(image));
pixbuf = gtk_image_get_pixbuf(GTK_IMAGE(image));
h = allocation->height;
w = (gdk_pixbuf_get_width(pixbuf) * h) / gdk_pixbuf_get_height(pixbuf);
pxbscaled = gdk_pixbuf_scale_simple(pixbuf, w, h, GDK_INTERP_BILINEAR);
printf("Allocation height %d width %d.\n", h, w);
gtk_image_set_from_pixbuf(GTK_IMAGE(image), pxbscaled);
g_object_unref (pxbscaled);
sleep(2);
return FALSE;
}
static gboolean delete( GtkWidget *widget,
GtkWidget *event,
gpointer data )
{
gtk_main_quit ();
return FALSE;
}
int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *button;
GtkWidget *table;
GtkWidget *label;
GtkWidget *image;
int i;
char bufferl[32];
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
// gtk_widget_set_size_request (GTK_WIDGET(window), 800, 480);
g_signal_connect (window, "delete-event",
G_CALLBACK (delete), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
table = gtk_grid_new ();
gtk_container_add (GTK_CONTAINER (window), table);
/* Create notebook, place position of tabs */
notebook = gtk_notebook_new ();
gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_TOP);
gtk_grid_attach (GTK_GRID (table), notebook, 0, 6, 3, 3);
gtk_widget_show (notebook);
/* Append pages to the notebook */
for (i = 0; i < 2; i++) {
sprintf(bufferl, "Page %d", i + 1);
if (i == 0) {
image = gtk_image_new_from_file("image1.jpg");
} else {
image = gtk_image_new_from_file("image2.jpg");
}
gtk_widget_set_halign(image, GTK_ALIGN_START);
gtk_widget_set_valign(image, GTK_ALIGN_START);
g_signal_connect(window, "size-allocate",
G_CALLBACK(resize_image), NULL);
label = gtk_label_new (bufferl);
gtk_notebook_append_page (GTK_NOTEBOOK(notebook),
image, label);
}
/* Create a close button */
button = gtk_button_new_with_label ("close");
g_signal_connect (button, "clicked",
G_CALLBACK (delete), NULL);
gtk_grid_attach (GTK_GRID (table), button, 0, 10, 1, 1);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
A user will need to provide image1.jpg and image2.jpg. Edit out the sleep call will result in the program filling the screen extremely quickly.
EDIT: I have also asked this question on the gtk mailing list.
The window with the image was growing because I was applying the size of the window to the image. Hence the image got larger and thus made the window get larger. Which continued in an endless progression, the "size-allocate" signal was constantly being called.
I fixed it by limiting the allocation height in the call back, by multiplying it by 0.75.
Now I can expand and contract the window with ease and it does not grow out of control.
The image does get ugly quite quickly, but that is another problem.

Getting an error Xlib: extension "RANDR" missing on display ":24.0". gtk

I am receiving this error and not sure why...Please take a look at my buttons array, maybe I messed smth there, I am not sure...
The error i get is: Xlib: extension "RANDR" missing on display ":24.0".
and nothing happens after that, meaning my program doesnt run at all....
#include <gtk/gtk.h>
/* Our new improved callback. The data passed to this function
* is printed to stdout. */
static void callback (GtkWidget *widget, gpointer data)
{
system ((gchar *) data);
}
/* another callback */
static gboolean delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
{
gtk_main_quit ();
return FALSE;
}
int main (int argc, char *argv[])
{
/* GtkWidget is the storage type for widgets */
GtkWidget *window;
GtkWidget *box1;
/* This is called in all GTK applications. Arguments are parsed
* from the command line and are returned to the application. */
gtk_init (&argc, &argv);
/* Create a new window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
/* This is a new call, which just sets the title of our
* new window to "My Assignments" */
gtk_window_set_title (GTK_WINDOW (window), "My Assignments");
/* Here we just set a handler for delete_event that immediately
* exits GTK. */
g_signal_connect (window, "delete-event",
G_CALLBACK (delete_event), NULL);
/* Sets the border width of the window. */
gtk_container_set_border_width (GTK_CONTAINER (window), 50);
/* We create a box to pack widgets into. This is described in detail
* in the "packing" section. The box is not really visible, it
* is just used as a tool to arrange widgets. */
box1 = gtk_vbox_new (FALSE,0);
/* Put the box into the main window. */
gtk_container_add (GTK_CONTAINER (window), box1);
/*array is here*/
GtkWidget *button[2];
int i;
for (i=0; i<2; i++)
{
button[i]=gtk_button_new();
}
button[0] = gtk_button_new_with_label ("Run shellscript");
g_signal_connect (button[0], "clicked",
G_CALLBACK (callback), "shellscript");
gtk_box_pack_start (GTK_BOX (box1), button[0], TRUE, TRUE, 0);
gtk_widget_show(button[0]);
button[1] = gtk_button_new_with_label ("Run program2 ");
g_signal_connect (button[1], "clicked",
G_CALLBACK (callback), "program");
gtk_box_pack_start (GTK_BOX (box1), button[1], TRUE, TRUE, 0);
gtk_widget_show(button[1]);
gtk_widget_show (box1);
/* Rest in gtk_main and wait for the fun to begin! */
gtk_main ();
return 0;
}

gtk displaying a dialog from within a dialog

I'm writing a gtk program,
at some point in my application, I call a gtkdialog to obtain input from the user.
what I want to do is close the current dialog and open another dialog when the OK button is clicked, I already have a function doing some other work, starting the dialog in that function would be great.
here's the parent dialog code:
GtkWidget * new_button_dialog = gtk_dialog_new_with_buttons("new button",(GtkWindow *)container,GTK_DIALOG_DESTROY_WITH_PARENT,"OK", GTK_RESPONSE_OK,NULL);
GtkWidget * content_area = gtk_dialog_get_content_area (GTK_DIALOG (new_button_dialog));
GtkWidget * button_name_label = gtk_label_new ("Press escape at any time to cancel.\ntype in the button name");
gtk_container_add (GTK_CONTAINER (content_area) , button_name_label);
GtkWidget * button_name_entry = gtk_entry_new();
gtk_entry_set_activates_default((GtkEntry*)button_name_entry,TRUE);
gtk_container_add(GTK_CONTAINER(content_area), button_name_entry);
GtkWidget * button_text_dialog = gtk_dialog_new_with_buttons("new button",(GtkWindow *)container,GTK_DIALOG_DESTROY_WITH_PARENT,"OK", GTK_RESPONSE_OK,NULL);
gtk_container_add (GTK_CONTAINER (container), button);
g_signal_connect(new_button_dialog,"response",G_CALLBACK (add_new_button),NULL);
gtk_signal_connect_object (GTK_OBJECT (button), "clicked",GTK_SIGNAL_FUNC (gtk_widget_show_all),(GtkWidget*)new_button_dialog);
gtk_widget_show_all((GtkWidget *)container);
The response of a dialog cannot be handled in the same function that the dialog is created. It means that in one function you cannot create a dialog, handle its input, close that dialog and open a new dialog. It cannot be done since when that function exits the dialog entry is empty and the first dialog is just shown.
It should be done as a chain of separate event handlers. All needed variables (data context) should be transferred between those separate handlers using a pointer to structure that holds that context.
The below full runnable example demonstrates such flow.
In the fist dialog handler we want to do something with the main window and some container object and maybe we want some other data that is shared between widgets of that main window. The structure window_ctx_t is declared for such context. There is a boolean flag in that context to lock creation on new dialog chains before closing active dialogs.
The function first_dialog_create creates the first dialog with text entry field. It receives window_ctx_t, where the main window pointer is stored that is used for the dialog creation. Now we need to attach to that dialog response handler and provide to the handler enough data for its functionality. In that example we provide pointer to text entry that is used to read text.
Using provided data the response handler add_new_button is able to read text entry, to create a new dialog attached to the main window and also to do something with provided container.
When the first dialog data context is not needed it is freed by calling g_free() in the dialog destroyed handler.
Build the example:
gcc -Wall -g dialogs.c -o dialogs `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0`
Example "dialogs.c"
#include <gtk/gtk.h>
typedef struct {
GtkWidget *container;
GtkWindow *window;
gboolean dialogs_created;
} window_ctx_t;
typedef struct {
GtkEntry *entry;
window_ctx_t *window_ctx;
} first_dialog_data_t;
static void first_dialog_create(gpointer context);
static void another_dialog_response(GtkWidget *dialog, gint response_id,
gpointer ctx)
{
window_ctx_t *win_ctx = ctx;
gtk_widget_destroy(dialog);
win_ctx->dialogs_created = FALSE;
}
static void cleanup_first_dialog(GtkWidget *widget, gpointer data)
{
g_free(data);
}
/* response callback of the first dialog */
static void add_new_button(GtkWidget *dialog, gint response_id,
gpointer ctx)
{
GtkWidget *button;
GtkWidget *button_text_dialog;
first_dialog_data_t *data = ctx;
if (response_id != GTK_RESPONSE_OK) {
data->window_ctx->dialogs_created = FALSE;
goto Exit;
}
/* Create a new button with the label from text entry */
button = gtk_button_new_with_label(gtk_entry_get_text(data->entry));
/* When the button receives the "clicked" signal, it will again open
* "new button" dialog */
g_signal_connect_swapped(button, "clicked", G_CALLBACK(first_dialog_create),
data->window_ctx);
/* add button to the container */
gtk_container_add(GTK_CONTAINER(data->window_ctx->container), button);
/* show button */
gtk_widget_show(button);
/* create another dialog */
button_text_dialog = gtk_dialog_new_with_buttons("new button",
data->window_ctx->window, GTK_DIALOG_DESTROY_WITH_PARENT, "OK",
GTK_RESPONSE_OK, NULL);
g_signal_connect(button_text_dialog, "response",
G_CALLBACK(another_dialog_response), (gpointer)data->window_ctx);
gtk_widget_show_all(button_text_dialog);
Exit:
gtk_widget_destroy(dialog);
}
static void first_dialog_create(gpointer context)
{
first_dialog_data_t *data;
window_ctx_t *win_ctx = context;
if (win_ctx->dialogs_created)
return;
/* lock buttons handler */
win_ctx->dialogs_created = TRUE;
/* allocate structure for dialog context */
data = g_new0(first_dialog_data_t, 1);
/* create first dialog, its label and text entry */
GtkWidget *new_button_dialog = gtk_dialog_new_with_buttons("new button",
win_ctx->window, GTK_DIALOG_DESTROY_WITH_PARENT,"OK",
GTK_RESPONSE_OK, NULL);
GtkWidget *content_area = gtk_dialog_get_content_area(
GTK_DIALOG(new_button_dialog));
GtkWidget *button_name_label = gtk_label_new("Press escape at any time to "
"cancel.\ntype in the button name");
gtk_container_add(GTK_CONTAINER(content_area), button_name_label);
GtkWidget * button_name_entry = gtk_entry_new();
gtk_entry_set_activates_default((GtkEntry*)button_name_entry,TRUE);
gtk_container_add(GTK_CONTAINER(content_area), button_name_entry);
/* provide data to response handler */
data->entry = (GtkEntry *)button_name_entry;
data->window_ctx = win_ctx;
g_signal_connect(new_button_dialog, "response", G_CALLBACK(add_new_button),
data);
/* handler to free dialog context 'data' when destroed */
g_signal_connect(new_button_dialog, "destroy",
G_CALLBACK(cleanup_first_dialog), data);
/* show first dialog */
gtk_widget_show_all(new_button_dialog);
}
static void destroy(GtkWidget *widget, gpointer data)
{
gtk_main_quit();
}
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *box1;
GtkWidget *button;
window_ctx_t win_ctx;
gtk_init (&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
box1 = gtk_hbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), box1);
/* set window context */
win_ctx.window = (GtkWindow *)window;
win_ctx.container = box1;
win_ctx.dialogs_created = FALSE;
g_signal_connect (window, "destroy", G_CALLBACK(destroy), NULL);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
button = gtk_button_new_with_label("Start");
g_signal_connect_swapped(button, "clicked", G_CALLBACK(first_dialog_create),
&win_ctx);
gtk_container_add(GTK_CONTAINER(box1), button);
gtk_widget_show_all(window);
gtk_main();
return 0;
}

pass string from inputfield to function fails - GTK c-language

I have one issue with gtk in c . I tried to follow the tutorial but I am not able to pass a text-entry to a function when one clicks on the button in the widget.
The code compiles fine but when I press the button I get several warnings and the string from text-entry that was supposed to be printed are null
What did I do wrong?
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
static GtkWidget *asset_label;
static GtkWidget *frame;
static GtkWidget *entry;
static void entry_Submit(GtkWidget *widget, GtkWidget *entry)
{
const gchar *text = gtk_entry_get_text(GTK_ENTRY (entry));
printf ("Result: %s\n", text);
gtk_widget_destroy(GTK_WIDGET(asset_label));
asset_label = gtk_label_new (text);
gtk_container_add (GTK_CONTAINER (frame), asset_label);
gtk_widget_show_all(frame);
}
static void destroy(GtkWidget *widget, gpointer data)
{
gtk_main_quit ();
}
static void initialize_window(GtkWidget* window)
{
gtk_window_set_title(GTK_WINDOW(window),"My Window"); //Set window title
gtk_window_set_default_size (GTK_WINDOW (window), 400, 200); //Set default size for the window
g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL); //End application when close button clicked
}
int main (int argc, char *argv[])
{
GtkWidget *window,*table,*label, *button;
gtk_init(&argc, &argv);
//Create the main window
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
initialize_window(window);
/* Create a 1x2 table */
table = gtk_table_new (3, 3, TRUE);
gtk_container_add (GTK_CONTAINER (window), table);
/* create a new label. */
label = gtk_label_new ("Enter some text:" );
//gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
gtk_table_set_homogeneous(GTK_TABLE (table), TRUE);
gtk_table_attach_defaults (GTK_TABLE (table), label, 1, 2, 0, 1);
//create a text box
entry = gtk_entry_new ();
//gtk_entry_set_text (GTK_ENTRY (entry), "");
gtk_entry_set_max_length (GTK_ENTRY (entry),0);
gtk_table_attach_defaults (GTK_TABLE (table), entry, 0, 1, 0, 1);
button = gtk_button_new_with_label("Calculate");
g_signal_connect_swapped (button, "clicked", G_CALLBACK (entry_Submit), entry);
gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 2, 1, 2);
//gtk_widget_show (button);
gtk_widget_show_all(window);
gtk_main ();
return 0;
}
And when the button is clicked I get this result:
Result: (null)
There is a problem in the callback or the way you are registering the callback (as fixing either one of them should fix your problem).
By default the clicked callback takes GtkButton as the first parameter and gpointer data as the second. By using g_signal_connect_swapped you are saying that in the callback function, the parameters will be swapped i.e., the first parameter will be gpointer data (GtkEntry in your code) and second GtkButton. But in your callback function you are treating second parameter entry which is in fact GtkButton as GtkEntry. Either use g_signal_connect instead of g_signal_connect_swapped or use the first parameter widget as GtkEntry in your callback function.
Side note: Regarding the warning, if the code which you have posted is the full code then in the callback function entry_Submit during first execution asset_label is null and thus gtk_widget_destroy(GTK_WIDGET(asset_label)); will throw a warning. Also, frame is unassigned before use in the callback function.
Hope this helps!

How to get the text of a button in GTK?

I am developing an application that has a numeric keypad and a text box when clicked on a button, text box shows the number.
I need to write a function to each button? Or you can pass a text and a widget as 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;
}
The answer to your question is yes, you can pass a widget in Gtk+ to a callback. Actually, the first parameter of the callback for the clicked signal is the button which received the signal (ie. usually the button that was clicked). As you can see in the sample code below, you can extract from the button its label 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'm using gtk_editable_insert_text because gtk_entry_append_text has been deprecated for a long time. Passing the "entry" parameter to the callback is possible by using the last parameter of g_signal_connect which allows you to specify some data that you need to access in your callback. This info is then made available to the callback in the "user_data" parameter.
Your exemple could also be improved by using gtk_widget_show_all, and I also don't see the point in calling g_object_set_data on the "char" property, as the text is already set in the label property (and retrieved with gtk_button_get_label).
Two options, that I can think of:
GTK widgets are really GObjects, so you can attach to them arbitrary pieces of data. See the functions g_object_set_data/g_object_set_data_full/g_object_get_data. So you can just add the text to the button as an attached data and retrieve it when needed.
You can pass any data you need to a callback by defining a struct with all the fields, and passing a pointer to it. If the struct cannot be declared statically, you can malloc it and use g_signal_connect_data to specify the function to release the data:
For example:
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);

Resources