/* Test case for bug 671170 */

#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>

static void
text_grid (int text_width)
{
  const int N_ROWS = 25;
  const int N_COLS = 80;
  const int SCREEN_WIDTH = 640;
  const int SCREEN_HEIGHT = 400;

  const GdkRGBA black = { 0.0, 0.0, 0.0, 1.0 };
  const GdkRGBA white = { 1.0, 1.0, 1.0, 1.0 };

  GtkWidget* window;
  GtkWidget* grid;
  GtkWidget* dummy1;
  GtkWidget* dummy2;
  GtkWidget* textview1;
  GtkWidget* textview2;
  PangoFontDescription* font_description;

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Bug 671170 test case");

  /* the grid into which we will place labels */
  grid = gtk_grid_new ();
  gtk_grid_set_row_homogeneous(GTK_GRID(grid), TRUE);
  gtk_grid_set_column_homogeneous(GTK_GRID(grid), TRUE);

  /* set the main window to the desired size and make it non-resizable */
  gtk_widget_set_size_request(window, SCREEN_WIDTH, SCREEN_HEIGHT);
  gtk_window_set_resizable(GTK_WINDOW (window), FALSE);

  /* we want no space in the grid */
  gtk_grid_set_row_spacing(GTK_GRID(grid), 0);
  gtk_grid_set_column_spacing(GTK_GRID(grid), 0);

  /* add the grid to the window */
  gtk_container_add (GTK_CONTAINER (window), grid);

  /* Force the grid to contain N_ROWS x N_COLS cells */
  dummy1 = gtk_label_new(NULL);
  dummy2 = gtk_label_new(NULL);
  /* If the following two function calls are uncommented, the window width is correct. */
  /*
  gtk_widget_set_size_request(dummy1, SCREEN_WIDTH, SCREEN_HEIGHT/N_ROWS);
  gtk_widget_set_size_request(dummy2, SCREEN_WIDTH/N_COLS, SCREEN_HEIGHT - SCREEN_HEIGHT/N_ROWS);
  */
  gtk_grid_attach(GTK_GRID(grid), dummy1, 0, 0, N_COLS, 1);  /* left, top, width, height */
  gtk_grid_attach(GTK_GRID(grid), dummy2, 0, 1, 1, N_ROWS-1);
  
  /* create and populate two textviews */
  textview1 = gtk_text_view_new();
  textview2 = gtk_text_view_new();

  font_description = pango_font_description_from_string("Courier New, medium, 12");

  gtk_widget_override_font(textview1, font_description);
  gtk_widget_override_font(textview2, font_description);

  /* first textview -- width depends on width of second label */
  gtk_widget_override_color(textview1, GTK_STATE_FLAG_NORMAL, &white);
  gtk_widget_override_background_color(textview1, GTK_STATE_FLAG_NORMAL, &black);
  gtk_grid_attach(GTK_GRID(grid), textview1, 30, 3, 5, 1);
  gtk_text_view_set_editable(GTK_TEXT_VIEW(textview1), FALSE);
  gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview1), FALSE);
  gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview1)), "1234*", -1);

  /* second textview */
  gtk_widget_override_color(textview2, GTK_STATE_FLAG_NORMAL, &white);
  gtk_widget_override_background_color(textview2, GTK_STATE_FLAG_NORMAL, &black);
  gtk_grid_attach(GTK_GRID(grid), textview2, 20, 2, text_width, 1);
  gtk_text_view_set_editable(GTK_TEXT_VIEW(textview2), FALSE);
  gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview2), FALSE);
  gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview2)), "5678*", -1);

  /* When the window is hidden, quit the main loop. */
  g_signal_connect (window, "hide", gtk_main_quit, NULL);

  gtk_widget_show_all (window);
}

int
main (int argc, char *argv[])
{
  int text_width = 12;

  gtk_init (&argc, &argv);

  if (argc != 1 && argc != 2)
    {
      fprintf (stderr, "Usage: testgrid [text_width]\n");
      return 1;
    }

  if (argc == 2)
    {
      text_width = atoi(argv[1]);
      if (text_width < 1 || text_width > 50)
        {
           fprintf (stderr, "Invalid text_width '%s', must be between 1 and 50\n", argv[1]);
           return 1;
        }
    }

  text_grid (text_width);

  gtk_main ();

  return 0;
}
