Search code examples
cgtkgtk4

gtk_widget_set_size_request() not functioning as intended


I've created an action_bar widget with 3 children, a label and 2 buttons. I've tried multiple width and height dimensions in the gtk_widget_set_size_request() method. No matter the dimensions I put the action bar ALWAYS is the same height no matter the size of the window itself.

This is my code I have for the entire project. I'm sorry that all the code is squished together. I had some copy and pasting issues...

#include <gtk/gtk.h>
#include <pango/pango.h>
#include <stdio.h>
//READERS! This code is meant to be easily read for people who want to learn!
//Handles initializing the window
static void activateWindow(GtkApplication *app) {
        GtkWidget *window = gtk_application_window_new(app);
        //TopActionBar | Creation
        GtkWidget *topActionBar = gtk_action_bar_new();
        //This line is the issue.
        gtk_widget_set_size_request(GTK_WIDGET(topActionBar), -1, 17);
        //TopActionBar | TITLE
                GtkWidget *bideLabel = gtk_label_new(NULL);
                gchar *bideLabelText = g_markup_printf_escaped("<b>BIDE</b>");
                gtk_label_set_markup(GTK_LABEL(bideLabel), bideLabelText);
                gtk_action_bar_pack_start(GTK_ACTION_BAR(topActionBar), GTK_WIDGET(bideLabel));
                //TopActionBar | Open Folder Button
                GtkWidget *openFolder = gtk_button_new();
                gtk_button_set_label(GTK_BUTTON(openFolder), "Open Folder");
                gtk_action_bar_pack_start(GTK_ACTION_BAR(topActionBar), GTK_WIDGET(openFolder));
                //TopActionBar | Help Menu Button
                gchar *helpButtonText = g_markup_printf_escaped("<u>HELP</u>");
                GtkWidget *helpButton = gtk_button_new();
                GtkWidget *helpLabel = gtk_label_new("Help");
                gtk_label_set_markup(GTK_LABEL(helpLabel), helpButtonText);
                gtk_button_set_child(GTK_BUTTON(helpButton), GTK_WIDGET(helpLabel));
                gtk_action_bar_pack_start(GTK_ACTION_BAR(topActionBar), GTK_WIDGET(helpButton));
        gtk_window_set_child(GTK_WINDOW(window), GTK_WIDGET(topActionBar));
        gtk_window_present(GTK_WINDOW(window));

}
int main(int argc, char *argv[]){
        GtkApplication *app = gtk_application_new("me.lufthor.bide", G_APPLICATION_DEFAULT_FLAGS);
        g_signal_connect(app, "activate", G_CALLBACK(activateWindow), NULL);
        return g_application_run(G_APPLICATION(app), argc, argv);

}

I tried messing around with the height and width values and NOTHING. Nothing changed.


Solution

  • You are trying to set size to an action bar. While it is technically a widget, it is a container first. And as a container it does not have size on its own. Action bar size is calculated from the container it belongs to (application window) and from widgets inside the container (buttons).

    So, if you want to see a difference in action_bar size - you change size of buttons inside it. Just grow (or shrink) buttons - the action bar should accommodate it automatically.

    But this code, also has an "outside" problem. Your action bar is the only child of the window. So whatever size you define for the button - you will not see it, since the action bar in this case will be growing to the window's inner height. To see different size of action bar, we need to have an action bar and something else. The action bar always automatically grows horizontally, but has a fixed height (which depends on buttons inside the bar). So we need another widget below action bar, with vertical auto-size, so the action bar can retain its manually defined height.

    So in your code, instead of one line:

    gtk_window_set_child(GTK_WINDOW(window), GTK_WIDGET(topActionBar));
    

    You need to do this:

          // The box is a layout container which holds other widgets
          GtkWidget *box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
          // The child of a window must be a top layout widget 
          gtk_window_set_child (GTK_WINDOW (window), box);
    
          // Almost any widget is good for this example.
          // The scrolled window is good enough.
          GtkWidget *sw = gtk_scrolled_window_new ();
          gtk_widget_set_vexpand (sw, TRUE);
    
          // Now add both widgets into the layout container
          gtk_box_append (GTK_BOX (box), topActionBar);
          gtk_box_append (GTK_BOX (box), sw);
    
          // and the next line will do what you want
          gtk_widget_set_size_request(GTK_WIDGET(helpLabel), -1, 200);