#include <gtk/gtk.h>

static void
create_box (GtkOrientation orientation)
{
  GtkWidget *box = gtk_box_new (orientation, 0);
  gtk_style_context_add_class (gtk_widget_get_style_context (box), "linked");

  for (int i = 0; i < 4; ++i)
    {
      char *text = g_strdup_printf ("Button #%i", i);
      gtk_container_add (GTK_CONTAINER (box), gtk_button_new_with_label (text));
      g_free (text);
    }

  GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Box");
  gtk_container_add (GTK_CONTAINER (window), box);

  gtk_widget_show_all (window);
}

static void
create_grid (GtkOrientation orientation)
{
  GtkWidget *grid = gtk_grid_new ();
  gtk_orientable_set_orientation (GTK_ORIENTABLE (grid), orientation);
  gtk_style_context_add_class (gtk_widget_get_style_context (grid), "linked");

  for (int i = 0; i < 4; ++i)
    {
      char *text = g_strdup_printf ("Button #%i", i);
      gtk_container_add (GTK_CONTAINER (grid), gtk_button_new_with_label (text));
      g_free (text);
    }

  GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Grid");
  gtk_container_add (GTK_CONTAINER (window), grid);

  gtk_widget_show_all (window);
}

int
main (int argc, char **argv)
{
  gtk_init (&argc, &argv);

  GtkCssProvider *css_provider = gtk_css_provider_get_default ();
  gtk_css_provider_load_from_data (css_provider,
                                   ".linked > button:last-child { background: red; }",
                                   -1, NULL);
  gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
                                             GTK_STYLE_PROVIDER (css_provider),
                                             GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

  create_box (GTK_ORIENTATION_HORIZONTAL);
  create_box (GTK_ORIENTATION_VERTICAL);
  create_grid (GTK_ORIENTATION_HORIZONTAL);
  create_grid (GTK_ORIENTATION_VERTICAL);

  gtk_main ();
}
