/*
 * Demonstrate the difference in stacking between X11 and Wayland.
 *
 * Build with:
 * $ gcc -o testdialog-mutter testdialog-mutter.c `pkg-config --libs --cflags gtk+-3.0`
 *
 */

#include <gtk/gtk.h>

void
focus_in_handler (GtkWindow *window, GdkEvent *event, gpointer user_data)
{
  GList *tmp_list, *l;

  tmp_list = gdk_screen_get_toplevel_windows (gtk_window_get_screen (window));
  for (l = tmp_list; l; l = l->next)
    {
       GdkWindow *w = l->data;

       if (w == gtk_widget_get_window (GTK_WIDGET (window)))
         continue;
       if (gdk_window_get_type_hint (w) != GDK_WINDOW_TYPE_HINT_DIALOG)
         continue;

       gdk_window_set_transient_for (w, gtk_widget_get_window (GTK_WIDGET (window)));
    }
  g_list_free (tmp_list);

}

int
main (int argc, char *argv[])
{
  GtkWidget *window1;
  GtkWidget *window2;
  GtkWidget *window3;
  GtkWidget *window4;

  gtk_init (NULL, NULL);

  window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW (window1), 600, 400);
  gtk_window_set_title (GTK_WINDOW (window1), "A toplevel window");
  g_signal_connect (window1, "destroy", gtk_main_quit, NULL);
  g_signal_connect (GTK_WINDOW (window1), "focus-in-event", G_CALLBACK (focus_in_handler), NULL);

  window2 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW (window2), 600, 400);
  gtk_window_set_title (GTK_WINDOW (window2), "Another toplevel window");
  g_signal_connect (window2, "destroy", gtk_main_quit, NULL);
  g_signal_connect (GTK_WINDOW (window2), "focus-in-event", G_CALLBACK (focus_in_handler), NULL);

  window3 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW (window3), 300, 200);
  gtk_window_set_type_hint (GTK_WINDOW (window3), GDK_WINDOW_TYPE_HINT_DIALOG);
  gtk_window_set_title (GTK_WINDOW (window3), "And a dialog type window");
  g_signal_connect (window3, "destroy", gtk_main_quit, NULL);

  window4 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW (window4), 300, 200);
  gtk_window_set_type_hint (GTK_WINDOW (window4), GDK_WINDOW_TYPE_HINT_DIALOG);
  gtk_window_set_title (GTK_WINDOW (window4), "And another dialog type window");
  g_signal_connect (window4, "destroy", gtk_main_quit, NULL);

  gtk_widget_show (window1);
  gtk_widget_show (window2);
  gtk_widget_show (window3);
  gtk_widget_show (window4);

  gtk_main ();
}
