Michi
Michi

Reputation: 5297

How to switch between windows in GTK3 (C Language)

I have an App which as some point Opens a new Window and works fine.

enter image description here

Then After I'm done with it, I need to switch back to the main window.

I understood that I need the function:

gtk_widget_hide();

but I cant figure out how to hide the main Window, to print only the second one and again after I click the button in the second window to go back to the first one.

This is what I have so far:

#include <stdio.h>
#include <gtk/gtk.h>

static void crete_new_wind (GtkWidget *widget);
gboolean destroy (GtkWidget *window);

int main (int argc, char *argv[]){
    GtkWidget *window, *button, *box;

    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "First Window");
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);
    gtk_widget_set_size_request (window, 300, 300);
    gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);

    button = gtk_button_new_with_label ("Go to Window B");
    g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (crete_new_wind), (gpointer) window);

    box = gtk_box_new (TRUE, 1);
    gtk_box_pack_end (GTK_BOX (box), button, TRUE, TRUE, 1);
    gtk_container_add (GTK_CONTAINER (window), box);

    gtk_widget_show_all (window);
    gtk_main ();
    return 0;
}

void crete_new_wind (GtkWidget *widget){
    GtkWidget *window, *button, *box;

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "Second Window");
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);
    gtk_widget_set_size_request (window, 300, 300);
    gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);;
    g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy), widget);

    button = gtk_button_new_with_label ("Go back to Window A");
    g_signal_connect (G_OBJECT (button), "destroy", G_CALLBACK (destroy), NULL);
    gtk_widget_hide(widget);

    box = gtk_box_new (TRUE, 1);
    gtk_box_pack_end (GTK_BOX (box), button, TRUE, TRUE, 1);
    gtk_container_add (GTK_CONTAINER (window), box);
    gtk_widget_show_all (window);
}

gboolean destroy (GtkWidget *widget){
    gtk_widget_destroy (widget);
    return TRUE;
}

If I click on the button(Go to Window B) I have this:

enter image description here

But the main window is still there, avaible to the user which is not what I need.

Upvotes: 1

Views: 2368

Answers (1)

David Ranieri
David Ranieri

Reputation: 41017

Thats because the clicked callback wants two parameters

Change the prototype and the function to

static void crete_new_wind(GtkButton *dummy, gpointer widget);

static void crete_new_wind(GtkButton *dummy, gpointer widget) {

in order to use the second parameter in your function.

Upvotes: 2

Related Questions