0

The following code creates a grid of cat icons, using the cat.jpg file below.

static void make_toggles(GtkWidget * window) {
    GtkWidget *stop_grid, vpaned;
    int i;
    stop_grid = gtk_grid_new();
    vpaned = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
    int num_stops = 10;
    char label[7]; label[0] = '\0';
    for (i = 0; i < num_stops; i++) {
        sprintf(label, "%d", i);
        stop = gtk_toggle_button_new_with_label(label);
        image = gtk_image_new_from_file("cat.jpg");
        gtk_button_set_child(GTK_BUTTON(stop), image);
        gtk_grid_attach(GTK_GRID (stop_grid), stop, i % 5 , 1 + (i/5), 1, 1); 
        g_signal_connect(stop, "toggled", G_CALLBACK(stop_toggle), NULL);
    }
    gtk_grid_set_column_spacing(GTK_GRID(stop_grid), 10);
    gtk_grid_set_row_spacing(GTK_GRID(stop_grid), 10);
    gtk_paned_set_start_child(GTK_PANED(vpaned), stop_grid);
    gtk_paned_set_resize_start_child(GTK_PANED(vpaned), TRUE);
    gtk_window_set_child (GTK_WINDOW (window), vpaned);
}

This is called from the application activate handler:

static void activate (GtkApplication* app, gpointer user_data) {
    GtkWidget *window;

    window = gtk_application_window_new (app);
    gtk_window_set_title (GTK_WINDOW (window), "Left Panel");
    gtk_window_set_default_size (GTK_WINDOW (window), 700, 500);
    make_toggles(window);
    gtk_widget_show (window);
}

However, the icons are really tiny. Is there a way to increase the size of each individual cell so the cat can be seen more legibly? enter image description here enter image description here

Cheetaiean
  • 901
  • 1
  • 12
  • 26

2 Answers2

0

While not really an answer, the Gtk FlowBox is a bit more flexible, as its children naturally fill out the existing space.

Cheetaiean
  • 901
  • 1
  • 12
  • 26
0

The cells in a GtkGrid will adjust to the size of their contents. So rather than fiddling around in GtkGrid, you want to take a look at those images.

In other words, if you want to make the images bigger, you want to use something like gtk_image_set_pixel_size(). Note that the default behavior of GtkImage is to have an icon-like size, since that's what its intended to be used for. If you're really more interested in showing more general images, you might find the GtkPicture class to better suit your needs (for example, it will use the natural size of the image you're loading)

nielsdg
  • 2,318
  • 1
  • 13
  • 22