-1

I am creating a Menu GUI for a game using glade and GTK in C. I have a text entry box and a button. When the button is pressed, the value inside the text entry needs to be saved as a global variable. I don't know how to get the value inside the text entry so I'm a bit stuck. Any help would be great.

Function

void on_okButton_clicked (GtkButton *object, gpointer user_data)
{
//???????
}

EDIT:

This is what my current code looks like

#include <stdio.h>
#include <gtk/gtk.h>
#include <python2.7/Python.h>


void on_window1_destroy (GtkObject *object, gpointer user_data)
{
gtk_main_quit();
}

void on_okButton_clicked (GtkButton *object, gpointer user_data)
{

gchar *entry_value;//this can be a global variable, too, of course
entry_value = gtk_entry_get_text(//get text function
    GTK_ENTRY(//use GTK_ENTRY widget
        (GtkWidget *) user_data //cast to GtkWidget pointer
    )
);

}


int main(int argc, char *argv[])
{
    GtkBuilder *gtkBuilder;
    GtkWidget *window;
    GtkWidget *Quit;
    GtkWidget *OK;
    GtkWidget *entry = gtk_entry_new();
    gtk_init(&argc, &argv);

    gtkBuilder = gtk_builder_new();
    gtk_builder_add_from_file(gtkBuilder, "IPenter.glade", NULL);
    window = GTK_WIDGET(gtk_builder_get_object(gtkBuilder, "window1"));

    OK = GTK_WIDGET(gtk_builder_get_object(gtkBuilder, "okButton"));
    g_signal_connect(G_OBJECT(OK),"clicked",G_CALLBACK(on_okButton_clicked),entry);

    g_object_unref(G_OBJECT(gtkBuilder));
    gtk_widget_show(window);
    gtk_main();

    return 0;
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322

1 Answers1

1

Assuming you connected the signal correctly, this ought to work just fine:

gchar *entry_value;//this can be a global variable, too, of course
entry_value = gtk_entry_get_text(//get text function
    GTK_ENTRY(//use GTK_ENTRY widget
        (GtkWidget *) user_data //cast to GtkWidget pointer
    )
);

Just to be sure, this is how you should connect the signal:

g_signal_connect(
    G_OBJECT(your_btn),
    "clicked",
    G_CALLBACK(
        on_okButton_clicked
    ),
    txt_entry_pointer
);

Where txt_entry_pointer is, of course, a pointer to the entry widget. Do not, however, make the surprisingly common mistake of passing the address of a local pointer here. If you create the entry widget like so:

GtkWidget *entry = gtk_entry_new();

And then attempt to connect the signal like so:

g_signal_connect(
    G_OBJECT(your_btn),
    "clicked",
    G_CALLBACK(
        on_okButton_clicked
    ),
    &entry// <=== WRONG!!
);

You are passing a pointer to a stack pointer, very likely resulting in undefined behaviour. Just drop the ampersand from the code above.

Also worth mentioning is that gchar is just a typedef for the char type. That means that:

gchar *foobar;

is the same as writing

char *foobar;

As you can see here.

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • Thank You for this but I am not very good with it and I am very unsure as to what is wrong now. I have edited my original question to show what I have, if you could help with why it isn’t working that would be great. – William Goodwin Apr 24 '14 at 13:03
  • @WilliamGoodwin: What errors are you getting? What are you expecting to see? You're getting the input from the entry widget, but at no point are you using the value/variable... so of course there's very little to see... check if you get the correct input simply by writing it to an output stream (`fprintf(stderr, "%s\n", (char *) entry_value);`) and start the program from the command line, it should print out the input when the callback gets called – Elias Van Ootegem Apr 24 '14 at 13:12
  • Just a suggestion: Why are you calling `gtk_builder_new`, and then call `gtk_builder_add_from_file`, when you can simply call `gtk_builder_new_from_file("gladeFile.Glade");`? – Elias Van Ootegem Apr 24 '14 at 13:18
  • At this line (GtkWidget *entry = gtk_entry_new();) i get a lot of these kind of errors when stepping through the program: (process:2511): GLib-GObject-CRITICAL **: /build/buildd/glib2.0-2.32.4/./gobject/gtype.c:2722: You forgot to call g_type_init() (process:2511): GLib-CRITICAL **: g_once_init_leave: assertion `result != 0' failed (process:2511): GLib-GObject-CRITICAL **: /build/buildd/glib2.0-2.32.4/./gobject/gtype.c:2722: You forgot to call g_type_init() (process:2511): GLib-GObject-CRITICAL **: /build/buildd/glib2.0-2.32.4/./gobject/gtype.c:2722: You forgot to call g_type_init() – William Goodwin Apr 24 '14 at 13:19
  • @WilliamGoodwin: Oh, now I see: you're creating a new gtk widget, before having called `gtk_init`. The widget, created by `gtk_entry_new` is also not part of the form that the builder creates. You should work on your glade file a bit more first – Elias Van Ootegem Apr 24 '14 at 13:30
  • I am very new to this and im not sure what that means and dont know what else to do to glade. I have made my glade file with a builder and drawn the gui if that helps but i dont know what `gtk_init` is. can you help by going through each step to completing this please? – William Goodwin Apr 24 '14 at 13:34
  • @WilliamGoodwin: Nope. That sort of thing goes way beyond the scope of this site. But there are many tutorials out there, here's a [youtube video tut](https://www.youtube.com/watch?v=vOGK3TveDDk), for example. – Elias Van Ootegem Apr 24 '14 at 13:36
  • can you explain to me what 'gtk_init' is and what i must do to the glade file to initiate this? – William Goodwin Apr 24 '14 at 14:57
  • 1
    @WilliamGoodwin: look at your bloody code! it's a function call, that initiates GTK. you call it passing it a pointer to both `argc` and `argv`: `gtk_init(&argc, &argv);` <-- don't be lazy, read the documentation – Elias Van Ootegem Apr 24 '14 at 15:07