Ralph St.Albord
Ralph St.Albord

Reputation: 1

Simple Example of AdwViewSwitcher from libadwaita

I can't get AdwViewSwitcher to show anything. I don't get any sort of errors, but nothing shows. This is my code:

PRIVATE GtkWidget *window = NULL;
PRIVATE GtkWidget *pref   = NULL;

PRIVATE GtkWidget *theme = NULL;

PRIVATE GtkFontDialog *font_dialog = NULL;
PRIVATE GtkWidget     *font        = NULL;

PRIVATE GtkWidget *about = NULL;

PRIVATE GtkWidget *view   = NULL;
PRIVATE GtkWidget *stack1 = NULL;
PRIVATE GtkWidget *stack2 = NULL;
PRIVATE GtkWidget *button = NULL;

PRIVATE
void theme_ev(GtkWidget *widget, GParamSpec *param_spec, gpointer user_data) {
        SDL_Log("Theme");
}

PRIVATE
void font_ev(GtkWidget *widget, GParamSpec *param_spec, gpointer user_data) {
        SDL_Log("Font");
}

PRIVATE
void about_ev(GtkButton *button, gpointer user_data) {
        adw_show_about_window(GTK_WINDOW(window), 
                              "application-name", "Editor Example",
                              NULL);
}

API
SDL_bool SetupEditor(GtkApplication *app, gpointer *unused) {
        SDL_DisplayMode *mode = GetDisplayMode();

        window = gtk_application_window_new(app);
        gtk_window_set_title(GTK_WINDOW(window), "GameEngine Editor");
        gtk_window_set_default_size(GTK_WINDOW(window), mode->w, mode->h);

        pref = adw_header_bar_new();
        gtk_window_set_titlebar(GTK_WINDOW(window), pref);

        theme = gtk_source_style_scheme_chooser_button_new();
        adw_header_bar_pack_start(ADW_HEADER_BAR(pref), theme);
        g_signal_connect(theme, "notify::style-scheme", G_CALLBACK(theme_ev), NULL);

        font_dialog = gtk_font_dialog_new();
        font        = gtk_font_dialog_button_new(font_dialog);
        adw_header_bar_pack_start(ADW_HEADER_BAR(pref), font);
        g_signal_connect(font, "notify::font-desc", G_CALLBACK(font_ev), NULL);

        about = gtk_button_new_with_label("About");
        adw_header_bar_pack_end(ADW_HEADER_BAR(pref), about);
        g_signal_connect(about, "clicked", G_CALLBACK(about_ev), NULL);

        view   = adw_view_switcher_new();
        stack1 = adw_view_stack_new();
        stack2 = adw_view_stack_new();
        button = gtk_button_new_with_label("Game Editor");
        adw_view_stack_add(ADW_VIEW_STACK(stack1), button);
        button = gtk_button_new_with_label("Script Editor");
        adw_view_stack_add(ADW_VIEW_STACK(stack2), button);
        adw_view_switcher_set_stack(ADW_VIEW_SWITCHER(view), ADW_VIEW_STACK(stack1));
        adw_view_switcher_set_stack(ADW_VIEW_SWITCHER(view), ADW_VIEW_STACK(stack2));
        adw_header_bar_pack_end(ADW_HEADER_BAR(pref), view);


        gtk_window_present(GTK_WINDOW(window));

        return SDL_TRUE;
}

I want to see a couple of tabs indicating two view. There no examples in the refernce documentation or in the demos. It doesn't have to look like this I just want a simple example of AdwViewSwitcher. Prefeably one that displays like the image here.

Upvotes: 0

Views: 257

Answers (1)

Kripto
Kripto

Reputation: 474

The simplest way to build graphical interfaces in GTK is using xml instead of creating widgets directly by code. Workbench has a good example of Adw.ViewSwitcher, as well as several other examples. That said, you need at least the following widgets:

  • A Window
  • A Box as window content
  • A Adw.HeaderBar as a Box child
  • A AdwViewStack as a Box child
  • Adw.ViewSwitcher as title-widget of Adw.HeaderBar

And, if you want the interface to be adaptive

  • An AdwViewSwitcherBar as a child of Box
  • An AdwBreakpoint as a Window child to hide the Adw.ViewSwitcher and reveal the AdwViewSwitcherBar depending on the size of the window.

Upvotes: -1

Related Questions