/* gcc `pkg-config --cflags gtk+-3.0` -o gtk-bug-709491-testcase
 gtk-bug-709491-testcase.c `pkg-config --libs gtk+-3.0` */

#include <gtk/gtk.h>

GtkWidget *spinbutton1;
GtkWidget *spinbutton2;
GtkSettings *settings;

void
toggle_dark_theme (GtkButton *button1)
{
  gint old_value;

  g_object_get (G_OBJECT (settings),
		"gtk-application-prefer-dark-theme",
		&old_value,
		NULL);

  g_object_set (G_OBJECT (settings),
		"gtk-application-prefer-dark-theme",
		! old_value,
		NULL);
}

void
toggle_orientations (GtkWidget *button1)
{
  GtkOrientation orientation;
  GtkOrientation new_orientation;

  orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (spinbutton1));
  new_orientation = (orientation == GTK_ORIENTATION_VERTICAL ?
		     GTK_ORIENTATION_HORIZONTAL : GTK_ORIENTATION_VERTICAL);
  gtk_orientable_set_orientation (GTK_ORIENTABLE (spinbutton1), new_orientation);
  gtk_orientable_set_orientation (GTK_ORIENTABLE (spinbutton2), orientation);
}

int
main (int argc,
      char *argv[])
{
  GtkWidget *window;
  GtkWidget *box;
  GtkWidget *hbox;
  GtkWidget *spinner_hbox;

  GtkWidget *button1;
  GtkWidget *button2;

  gboolean start_with_dark_theme;
  gtk_init (&argc, &argv);

  start_with_dark_theme = FALSE;
  start_with_dark_theme = (g_strcmp0 (argv[1], "--dark") == 0);

  settings = gtk_settings_get_default ();

  g_object_set (G_OBJECT (settings),
  		"gtk-application-prefer-dark-theme", start_with_dark_theme,
  		NULL);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "GtkSpinner Test");
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);


  /* Spinbutton 1, starts out with horizontal orientation */
  spinbutton1 = gtk_spin_button_new_with_range (-10.0, 10.0, 1);
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton1), 1.0);
  gtk_widget_set_hexpand (spinbutton1, TRUE);
  gtk_widget_set_vexpand (spinbutton1, TRUE);
  gtk_widget_set_halign (spinbutton1, GTK_ALIGN_CENTER);
  gtk_widget_set_valign (spinbutton1, GTK_ALIGN_CENTER);

  /* Spinbutton 2, starts out with vertical orientation */
  spinbutton2 = gtk_spin_button_new_with_range (-10.0, 10.0, 1);
  gtk_orientable_set_orientation (GTK_ORIENTABLE (spinbutton2),
				  GTK_ORIENTATION_VERTICAL);
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton2), 2.0);
  gtk_widget_set_hexpand (spinbutton2, TRUE);
  gtk_widget_set_vexpand (spinbutton2, TRUE);
  gtk_widget_set_halign (spinbutton2, GTK_ALIGN_CENTER);
  gtk_widget_set_valign (spinbutton2, GTK_ALIGN_CENTER);


  /* Buttons to toggle theme and orientation. */
  button1 = gtk_button_new_with_label ("Toggle Dark Theme");
  button2 = gtk_button_new_with_label ("Toggle Orientation");
  g_signal_connect (button1, "clicked", G_CALLBACK (toggle_dark_theme), NULL);
  g_signal_connect (button2, "clicked", G_CALLBACK (toggle_orientations), NULL);

  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
  gtk_box_set_homogeneous (GTK_BOX (hbox), TRUE);
  gtk_container_add (GTK_CONTAINER (hbox), spinbutton1);
  gtk_container_add (GTK_CONTAINER (hbox), spinbutton2);

  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_container_add (GTK_CONTAINER (box), hbox);
  gtk_container_add (GTK_CONTAINER (box), button1);
  gtk_container_add (GTK_CONTAINER (box), button2);

  gtk_container_add (GTK_CONTAINER (window), box);
  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}
