#include <gtk/gtk.h>
//gcc dragdrop.c `pkg-config --cflags --libs gtk+-3.0`
//under X we get GdkEventConfigure events which we can
//use to know where the window has been dragged to so
//we can dock a floating toolbar if its in proximity
//to a docking site, we get nothing under wayland

gboolean on_mouse_move(GtkWidget* widget,
  GdkEventMotion *event, void* foo) {
  
  fprintf(stderr, "mouse move %d\n", event->time);
  
  return TRUE;
}

GtkWidget *window;

gboolean on_configure(GtkWidget* widget,
  GdkEventConfigure *event, void* foo){

  int x, y;
  GString *buf;

  x = event->x;
  y = event->y;

  buf = g_string_new(NULL);   
  g_string_printf(buf, "%d, %d", x, y);

  gtk_window_set_title(GTK_WINDOW(window), buf->str);

  fprintf(stderr, "got %s\n", buf->str);

  g_string_free(buf, TRUE);

  return TRUE;
}

gboolean on_button_press(GtkWidget* widget,
  GdkEventButton *event, GdkWindowEdge edge) {
      
  if (event->type == GDK_BUTTON_PRESS) {
      
    if (event->button == 1) {
      gtk_window_begin_move_drag(GTK_WINDOW(gtk_widget_get_toplevel(widget)),
          event->button,
          event->x_root,
          event->y_root,
          event->time);
    }
  }
  
  return TRUE;
}

int main(int argc, char *argv[]) {

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size(GTK_WINDOW(window), 250, 200);
  gtk_window_set_title(GTK_WINDOW(window), "Drag & drop");
  gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
  gtk_widget_add_events(window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
                           GDK_POINTER_MOTION_MASK |
                           GDK_VISIBILITY_NOTIFY_MASK | GDK_SCROLL_MASK | GDK_STRUCTURE_MASK);

  g_signal_connect(G_OBJECT(window), "button-press-event",
      G_CALLBACK(on_button_press), NULL);

  g_signal_connect(G_OBJECT(window), "motion-notify-event",
      G_CALLBACK(on_mouse_move), NULL);

  g_signal_connect(G_OBJECT(window), "configure-event",
      G_CALLBACK(on_configure), NULL);

  g_signal_connect(G_OBJECT(window), "destroy",
        G_CALLBACK(gtk_main_quit), G_OBJECT(window));

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}
