/* gcc -o test-timed-drawing test-timed-drawing.c `pkg-config --cflags --libs gtk+-3.0` */
#include <gtk/gtk.h>

GtkAdjustment *adjustment;

static void
on_draw (GtkWidget *widget,
         cairo_t   *cr)
{
  GtkAllocation allocation;
  float displaying_ms = gtk_adjustment_get_value (adjustment);

  g_usleep (displaying_ms * 1000);

  gtk_widget_get_allocation (widget, &allocation);

  cairo_rectangle (cr, 20, 20, allocation.width - 40, allocation.height - 40);
  cairo_set_source_rgb (cr, 1, 1, 0);
  cairo_fill (cr);
}

int
main (int argc, char **argv)
{
  GtkWidget *window, *vbox, *label, *scale;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_size_request (window, 500, 500);
  gtk_widget_set_app_paintable (window, TRUE);

  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_container_add (GTK_CONTAINER (window), vbox);

  adjustment = gtk_adjustment_new (20, 0, 2000, 1, 10, 0);
  scale = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, adjustment);
  gtk_box_pack_end (GTK_BOX (vbox), scale, FALSE, FALSE, 0);

  label = gtk_label_new ("Screen update time (ms):");
  gtk_box_pack_end (GTK_BOX (vbox), label, FALSE, FALSE, 0);

  g_signal_connect (window, "draw",
                    G_CALLBACK (on_draw), NULL);
  g_signal_connect (window, "delete-event",
                    G_CALLBACK (gtk_main_quit), NULL);

  gtk_widget_show_all (window);
  gtk_main ();

  return 0;
}
