Web lists-archives.com

Re: gtk-3.16: GtkStackSideBar & GtkStack - Change page from code (No user interaction)




Hi Ben,

Thank you. This works for me.
I attach  two examples (gtk & gtkmm) with a button that do this.




2015-07-30 16:13 GMT+02:00 Ben <iofelben@xxxxxxxxx>:
>
> You can use gtk_stack_set_visible_child()
>
> https://developer.gnome.org/gtk3/stable/GtkStack.html#gtk-stack-set-vis
> ible-child
>
> On Wed, 2015-07-29 at 19:43 +0200, Alfredo Pons wrote:
> > The transtions between GtkStacks are very beautifuls.
> > Can I change pages from the source code without user interaction?
> >
> > The idea is set hide the GtkStackSideBar and change pages from source
> > code when some conditions are met.
> >
> > Than you.
> > _______________________________________________
> > gtk-list mailing list
> > gtk-list@xxxxxxxxx
> > https://mail.gnome.org/mailman/listinfo/gtk-list
/* Stack Sidebar
 *
 * GtkStackSidebar provides an automatic sidebar widget to control
 * navigation of a GtkStack object. This widget automatically updates it
 * content based on what is presently available in the GtkStack object,
 * and using the "title" child property to set the display labels.
 */

#include <glib/gi18n.h>
#include <gtk/gtk.h>

GtkWidget *stack_example;
GtkWidget *example = NULL;

static void
change_child (GtkWidget *widget,
              gpointer   data)
{
    g_print ("Hello World\n");

    gtk_stack_set_visible_child(stack_example, example);
}


int main (int argc, char *argv[])
{

    GtkWidget *window = NULL;
    GtkWidget *sidebar;
    GtkWidget *stack;
    GtkWidget *box;
    GtkWidget *widget;
    GtkWidget *header;
    GtkWidget *button;

    const gchar* pages[] = {
        "Welcome to GTK+",
        "GtkStackSidebar Widget",
        "Automatic navigation",
        "Consistent appearance",
        "Scrolling",
        "Page 6",
        "Page 7",
        "Page 8",
        "Page 9",
        NULL
    };



    const gchar *c = NULL;
    guint i;

    gtk_init (&argc, &argv);
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
    gtk_widget_set_size_request (window, 500, 350);

    header = gtk_header_bar_new ();
    gtk_header_bar_set_show_close_button (GTK_HEADER_BAR(header), TRUE);
    gtk_window_set_titlebar (GTK_WINDOW(window), header);
    gtk_window_set_title (GTK_WINDOW(window), "Stack Sidebar");

    g_signal_connect (window, "destroy",
                      G_CALLBACK (gtk_widget_destroyed), &window);



    sidebar = gtk_stack_sidebar_new ();
    gtk_box_pack_start (GTK_BOX (box), sidebar, FALSE, FALSE, 0);



    box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);

    stack = gtk_stack_new ();
    stack_example = stack;
    gtk_stack_set_transition_type (GTK_STACK (stack), GTK_STACK_TRANSITION_TYPE_SLIDE_UP_DOWN);
    gtk_stack_sidebar_set_stack (GTK_STACK_SIDEBAR (sidebar), GTK_STACK (stack));

    /* Separator between sidebar and stack */
    widget = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
    gtk_box_pack_start (GTK_BOX(box), widget, FALSE, FALSE, 0);

    gtk_box_pack_start (GTK_BOX (box), stack, TRUE, TRUE, 0);



    for (i=0; (c = *(pages+i)) != NULL; i++ )
    {
        if (i == 0)
        {
            widget = gtk_image_new_from_icon_name ("help-about", GTK_ICON_SIZE_MENU);
            gtk_image_set_pixel_size (GTK_IMAGE (widget), 256);
        }
        else
        {
            widget = gtk_label_new (c);
            if (i == 3)
                example = widget;
        }

        gtk_stack_add_named (GTK_STACK (stack), widget, c);
        gtk_container_child_set (GTK_CONTAINER (stack), widget, "title", c, NULL);
    }


    /* Creates a toggle button */
    button=gtk_button_new_with_label("Test button");
    g_signal_connect (button, "clicked",
                      G_CALLBACK (change_child), NULL);
    /* Add the button to window */
    gtk_container_add(GTK_BOX(box),button);
    gtk_widget_show(button);
    gtk_container_add (GTK_CONTAINER (window), box);



    /* make sure that everything, window and label, are visible */
    gtk_widget_show_all (window);

    gtk_widget_hide(sidebar);
    // gtk_stack_set_visible_child(stack, example);

    /* start the main loop, and let it rest there until the application is closed */
    gtk_main ();

    return 0;
}
/* Stack Sidebar
 *
 * Gtk::StackSidebar provides an automatic sidebar widget to control
 * navigation of a Gtk::Stack object. This widget automatically updates its
 * contents based on what is presently available in the Gtk::Stack object,
 * and using the "title" child property to set the display labels.
 */

#include <gtkmm.h>
#include <vector>

class Example_StackSidebar : public Gtk::Window
{
public:
    Example_StackSidebar();
    virtual ~Example_StackSidebar();

protected:
    // Container:
    Gtk::Box m_Box;

    // Widgets:
    Gtk::HeaderBar m_HeaderBar;
    Gtk::StackSidebar m_StackSidebar;
    Gtk::Separator m_Separator;
    Gtk::Stack m_Stack;
    Gtk::Button m_button;
    Gtk::Widget *m_example;

private:

    void fill_page_names();
    std::vector<Glib::ustring> m_page_names;

    void on_button_click();
};


void
Example_StackSidebar::on_button_click()
{
    m_Stack.set_visible_child(*m_example);
}


Example_StackSidebar::Example_StackSidebar()
    :
    m_Box(Gtk::ORIENTATION_HORIZONTAL),
    m_HeaderBar(),
    m_StackSidebar(),
    m_Separator(Gtk::ORIENTATION_VERTICAL),
    m_Stack(),
    m_button()
{
    m_HeaderBar.set_show_close_button(true);
    m_button.signal_clicked().connect (sigc::mem_fun (*this, &Example_StackSidebar::on_button_click));


    set_titlebar(m_HeaderBar);
    set_title("Stack Sidebar demo");
    set_default_size(500, 350);

    add(m_Box);

    m_Box.pack_start(m_StackSidebar, Gtk::PACK_SHRINK);
    m_Box.pack_start(m_Separator, Gtk::PACK_SHRINK);
    m_Box.pack_start(m_Stack, Gtk::PACK_EXPAND_WIDGET);
    m_button.set_label("Test button");
    m_Box.add(m_button);


    m_Stack.set_transition_type(Gtk::STACK_TRANSITION_TYPE_SLIDE_UP_DOWN);
    m_StackSidebar.set_stack(m_Stack);

    // Stack pages
    fill_page_names();
    for (std::size_t i = 0; i != m_page_names.size(); ++i)
    {
        Gtk::Widget* widget = 0;
        if (i == 0)
        {
            Gtk::Image* image = Gtk::manage(new Gtk::Image());
            image->set_from_icon_name("help-about", Gtk::ICON_SIZE_MENU);
            image->set_pixel_size(256);
            widget = image;
        }
        else
        {

            widget = Gtk::manage(new Gtk::Label(m_page_names[i]));
            if (i== 3)
                m_example = widget;
        }
        m_Stack.add(*widget, m_page_names[i], m_page_names[i]);
    }

    show_all();
    m_StackSidebar.hide();


}

Example_StackSidebar::~Example_StackSidebar()
{
}

void Example_StackSidebar::fill_page_names()
{
    m_page_names.push_back("Welcome to gtkmm");
    m_page_names.push_back("Gtk::StackSidebar Widget");
    m_page_names.push_back("Automatic navigation");
    m_page_names.push_back("Consistent appearance");
    m_page_names.push_back("Scrolling");
    m_page_names.push_back("Page 6");
    m_page_names.push_back("Page 7");
    m_page_names.push_back("Page 8");
    m_page_names.push_back("Page 9");
}

int main (int argc, char *argv[])
{
    Gtk::Main kit(argc, argv);

    Example_StackSidebar example;
    //Shows the window and returns when it is closed.
    Gtk::Main::run(example);

    return 0;
}
_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
https://mail.gnome.org/mailman/listinfo/gtk-list