1

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;
}

1 Answer 1

0

My solution proposal is to add a GtkBox to the GtkStack as a child.

After that, this GtkBox can then be used to place more children.

 
#include<gtk/gtk.h>

static void 
activate (GtkApplication *app, gpointer user_data)
{
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *switcher;
    GtkWidget *stack;
    GtkWidget *widget;
    GtkWidget *pbox;

    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);

    pbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    GtkWidget *label = gtk_label_new("Unlimited");

    gtk_box_append(GTK_BOX(pbox), label);

    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);
    gtk_box_append(GTK_BOX(pbox),c_btn);

    /**add page1**/
        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);
        gtk_stack_page_set_name (gtk_stack_get_page(GTK_STACK(stack), widget),"page1");

    /**add page2**/
        widget = gtk_label_new("Welcom");
    gtk_box_append(GTK_BOX(pbox),widget );
    gtk_stack_add_child(GTK_STACK(stack),pbox);
        gtk_stack_page_set_title (gtk_stack_get_page(GTK_STACK(stack), pbox),"page2");

    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;
}

enter image description here

Have fun testing.

Supplement:

As far as I understand the documentation, GtkStackPage can only be added properties.

for example:

 g_object_set((GObject*)gtk_stack_get_page(GTK_STACK(stack),pbox),"title","page2","icon-name","window-close",NULL);

This will preclude a GtkButton.

In addition, only the icon is then displayed at all.

This can also be recognized from the following source code:

https://gitlab.gnome.org/GNOME/gtk/-/blob/main/gtk/gtkstackswitcher.c#L142

So you would have to write your own GtkStackSwitcher to change that.

The GtkButten can therefore be implemented within "pbox".

This should not be a problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, but I don't want it like this. I want to have a close button on every page (tab). I know you understand what I'm trying to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.