#include <gtk/gtk.h>

static void
show_popover (GtkWidget *widget,
              gpointer   data)
{
  GtkWidget *popover;
  GtkWidget *label;
  GdkRectangle rect;

  popover = gtk_popover_new (widget);
  g_signal_connect (popover, "closed", G_CALLBACK (gtk_widget_destroy), NULL);

  /* I prefer left position */
  gtk_popover_set_position (GTK_POPOVER(popover), GTK_POS_LEFT);

  /* Popover wont fit using GTK_POS_LEFT, Gtk+ should
   * change the popover direction to right when rendering.
   * That doesn't happen in RTL, but yes in LTR .*/
  rect.x = 5;
  rect.y = 100;
  rect.width = 0;
  rect.height = 0;

  gtk_popover_set_pointing_to (GTK_POPOVER(popover), &rect);

  label = gtk_label_new ("Hello World");

  gtk_container_add (GTK_CONTAINER(popover), label);

  gtk_widget_show_all (GTK_WIDGET(popover));
}

int
main (int   argc,
      char *argv[])
{
  GtkWidget *window;
  GtkWidget *button;

  gtk_init (&argc, &argv);

  /* Force RTL to reproduce the bug */
  gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Hello");
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  gtk_window_set_default_size (GTK_WINDOW(window), 500, 500);

  button = gtk_button_new_with_label ("Hello World");
  g_signal_connect (button, "clicked", G_CALLBACK (show_popover), NULL);

  gtk_container_add (GTK_CONTAINER (window), button);

  gtk_widget_show (button);
  gtk_widget_show (window);

  gtk_main ();

  return 0;
}
