I am using GtkStackSwitcher and GtkStack widget in gtk4, trying to add window close icon/button and label together in GtkStackSwitcher (page). I tried the demo script below. It is adding the page with a title, but I am not able to add the label and close button at the same time. Can anyone tell me how to do it?
#include <gtk/gtk.h>
static void
activate (GtkApplication* app,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *switcher;
GtkWidget *stack;
GtkWidget *box;
GtkWidget *widget;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 100, 200);
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
switcher = gtk_stack_switcher_new ();
gtk_box_append (GTK_BOX (box), switcher);
stack = gtk_stack_new ();
gtk_stack_set_transition_type (GTK_STACK (stack), GTK_STACK_TRANSITION_TYPE_CROSSFADE);
gtk_stack_switcher_set_stack ((GtkStackSwitcher *)switcher, GTK_STACK (stack));
gtk_widget_set_hexpand (stack, TRUE);
gtk_box_append (GTK_BOX (box), stack);
/**make box with label and close icon or exit button**/
GtkWidget *pBox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
GtkWidget *label = gtk_label_new ("Unlimited");
GtkWidget *c_btn = gtk_image_new_from_icon_name ("window-close-symbolic");
gtk_image_set_icon_size (GTK_IMAGE (c_btn), GTK_ICON_SIZE_INHERIT);
/*or use button */
//GtkWidget *c_btn = gtk_button_new_with_label("X");
gtk_box_append ((GtkBox *)pBox, label);
gtk_box_append ((GtkBox *)pBox, c_btn);
/**add page**/
widget = gtk_image_new_from_icon_name ("org.gtk.Demo4");
gtk_widget_add_css_class (widget, "icon-dropshadow");
gtk_image_set_pixel_size (GTK_IMAGE (widget), 256);
gtk_stack_add_named (GTK_STACK (stack), widget, "page1");
g_object_set (gtk_stack_get_page (GTK_STACK (stack), widget), "title", "page1", NULL);
widget = gtk_label_new ("Welcome");
gtk_stack_add_named (GTK_STACK (stack), widget, "page2");
g_object_set (gtk_stack_get_page (GTK_STACK (stack), widget), "title", "page2", NULL);
/*HOW TO ADD the pBox Widget on GtkStackPage? */
gtk_window_set_child (GTK_WINDOW (window), box);
gtk_window_present (GTK_WINDOW (window));
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
#if GLIB_CHECK_VERSION(2, 74, 0)
app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
#else
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
#endif
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
