Milo Hartikainen
Milo Hartikainen

Reputation: 49

how to close GTK window with C

i was doing a small project where i had a a fullscreen GUI (Glade, GTK and C) and needed to give the user a way to close the screen (obviously the window manager was not available due to the window being fullscreen).

problem rices when i attempt to compile this piece of code

//close sidebar1
 void on_window_sidebar1_back_clicked()
 {
    gtk_window_close (GtkWindow *window);
 }

i receive the following error

    /usr/bin/arm-linux-gnueabihf-gcc -c -g -O0 -Wall -pthread -pipe src/main.c `pkg-config --cflags --libs gtk+-3.0` -o main.o
src/main.c: In function 'on_window_sidebar1_back_clicked':
src/main.c:61:2: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'gtk_window_close'
  gtk_window_close (GtkWindow *window);
  ^
src/main.c:61:20: error: expected expression before 'GtkWindow'
  gtk_window_close (GtkWindow *window);
                    ^
makefile:30: recipe for target 'main.o' failed
make: *** [main.o] Error 1

I tried to follow the documentation here, but seem to have failed badly

Does anyone have any ideas?

PS.

here is the full main.c if someone is intrested. Its my first attempt at C with gtk, so dont be too harsh, Thank you (=

#include <gtk/gtk.h>

GtkWidget *g_lbl_test;
GtkWidget *g_lbl_count;

int main(int argc, char *argv[])
{
    GtkBuilder      *builder; 
    GtkWidget       *window;

    gtk_init(&argc, &argv);

    builder = gtk_builder_new();
    gtk_builder_add_from_file (builder, "glade/window_main.glade", NULL);

    window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));
    gtk_builder_connect_signals(builder, NULL);

    //pointers for the labels (...used in button press)
    g_lbl_test = GTK_WIDGET(gtk_builder_get_object(builder, "lbl_test"));
    g_lbl_count = GTK_WIDGET(gtk_builder_get_object(builder, "lbl_count"));

    g_object_unref(builder);

    gtk_widget_show(window);
    //gtk_window_fullscreen (GtkWindow *window);  //dont know how to use this, same with gtk_window_close
    gtk_main();

    return 0;
}

//button press (just a test to make sure the window didnt freeze)
void on_btn_count_clicked()
{
        static unsigned int count= 0;
        char str_count[30] = {0};

        gtk_label_set_text(GTK_LABEL(g_lbl_test), "Test success!");
        count++;
        sprintf(str_count, "%d", count);
        gtk_label_set_text(GTK_LABEL(g_lbl_count), str_count);
}

//sidebar1
void on_sidebar_1_clicked()
{
    GtkBuilder      *builder; 
    GtkWidget       *window;

    builder = gtk_builder_new();
    gtk_builder_add_from_file (builder, "glade/window_main.glade", NULL);

    window = GTK_WIDGET(gtk_builder_get_object(builder, "window_sidebar1"));

    gtk_widget_show(window); 
}
//close sidebar1
 void on_window_sidebar1_back_clicked()
 {
    gtk_window_close (GtkWindow *window);
 }


// called on closing window
void on_window_main_destroy()
{
    gtk_main_quit();
}

Upvotes: 0

Views: 8734

Answers (2)

Milo Hartikainen
Milo Hartikainen

Reputation: 49

Attention! This does not follow any industrial standards and there are multiple flaws in this. I don't make any money or fame out of this so my only requirement is that it works. Take this into consideration when reading this!

I made edits to my code with what I learned and this seems to work:

void on_sidebar_1_clicked(int argc, char *argv[])
{
    GtkBuilder      *builder; 

    gtk_init(&argc, &argv);

    builder = gtk_builder_new();
    gtk_builder_add_from_file (builder, "glade/window_sidebar1.glade", NULL);  //a whole own file for the second window

    secondWindow = GTK_WIDGET(gtk_builder_get_object(builder, "window_sidebar1")); //secondWindow is defined at the start of main.c like this: "GtkWidget *secondWindow = NULL;" so that both callbacks can use it.
    gtk_builder_connect_signals(builder, NULL);


    gtk_widget_show(secondWindow);
    gtk_main();

}


void on_sidebar1_back_clicked()
 {
    gtk_widget_destroy(GTK_WIDGET(secondWindow)); //here we call the widget to be closed instead of the window. Seems to work a lot better than the gtk_window_close, dunno why.
 }

This code was written fast (and furious) so it's a little crude. I made some changes to my glade files as well, if someone needs a reference to those, please ask. As far as I care, this was the answer for my question. Also, I would recommend learning C and GTK (like Basile Starynkevitch said) before you start doing this kind of stuff, I didn't have the time nor motivation to do so.

Upvotes: 0

You probably should call

 gtk_close_window(window);

But that window should come from somewhere.

(you may use a Gtk closure with a connection, or a callback with a client data, etc. or store somehow that window in a global variable, etc...)

Read more about C programming, then read a bit about callbacks and closures, then read more about GTK, and the signal mechanism of Gobject.

(your code shows a lot of confusion; I suggest to read the Getting started with GTK chapter after having read a good C programming book; BTW, I suggest to first write something which is not fullscreen, and only later improve it)

Don't forget to enable all warnings and debug info when compiling, that is compile with
gcc -Wall -Wextra -g (and other arguments, perhaps using $(pkg-config --cflags gtk+-3.0) and also $(pkg-config --libs gtk+-3.0) for linking).

Upvotes: 3

Related Questions