]> git.street.me.uk Git - andy/viking.git/commitdiff
Allow opening of files via Drag and Drop onto the Viewport.
authorRob Norris <rw_norris@hotmail.com>
Wed, 22 May 2013 19:23:23 +0000 (20:23 +0100)
committerRob Norris <rw_norris@hotmail.com>
Thu, 23 May 2013 22:45:53 +0000 (23:45 +0100)
src/vikwindow.c

index 683e950d4c81a2097fa91d53113b7ab98a6f7f77..77324df3fd81dd1c9c491f81b2eba360b5db7a16 100644 (file)
@@ -479,6 +479,53 @@ static gint zoom_popup_handler (GtkWidget *widget)
   return TRUE;
 }
 
+enum {
+  TARGET_URIS,
+};
+
+static void drag_data_received_cb ( GtkWidget *widget,
+                                    GdkDragContext *context,
+                                    gint x,
+                                    gint y,
+                                    GtkSelectionData *selection_data,
+                                    guint target_type,
+                                    guint time,
+                                    gpointer data )
+{
+  gboolean success = FALSE;
+
+  if ( (selection_data != NULL) && (gtk_selection_data_get_length(selection_data) > 0) ) {
+    switch (target_type) {
+    case TARGET_URIS: {
+      gchar *str = (gchar*)gtk_selection_data_get_data(selection_data);
+      g_debug ("drag received string:%s \n", str);
+
+      // Convert string into GSList of individual entries for use with our open signal
+      gchar **entries = g_strsplit(str, "\r\n", 0);
+      GSList *filenames = NULL;
+      gint entry_runner = 0;
+      gchar *entry = entries[entry_runner];
+      while (entry) {
+        if ( g_strcmp0 ( entry, "" ) )
+          filenames = g_slist_append ( filenames, entry );
+        entry_runner++;
+        entry = entries[entry_runner];
+      }
+
+      if ( filenames )
+        g_signal_emit ( G_OBJECT(VIK_WINDOW_FROM_WIDGET(widget)), window_signals[VW_OPENWINDOW_SIGNAL], 0, filenames );
+        // NB: GSList & contents are freed by main.open_window
+
+      success = TRUE;
+      break;
+    }
+    default: break;
+    }
+  }
+
+  gtk_drag_finish ( context, success, FALSE, time );
+}
+
 static void vik_window_init ( VikWindow *vw )
 {
   GtkWidget *main_vbox;
@@ -562,6 +609,11 @@ static void vik_window_init ( VikWindow *vw )
   vw->save_img_dia = NULL;
   vw->save_img_dir_dia = NULL;
 
+  // Only accept Drag and Drop of files onto the viewport
+  gtk_drag_dest_set ( GTK_WIDGET(vw->viking_vvp), GTK_DEST_DEFAULT_ALL, NULL, 0, GDK_ACTION_COPY );
+  gtk_drag_dest_add_uri_targets ( GTK_WIDGET(vw->viking_vvp) );
+  g_signal_connect ( GTK_WIDGET(vw->viking_vvp), "drag-data-received", G_CALLBACK(drag_data_received_cb), NULL );
+
   // Store the thread value so comparisons can be made to determine the gdk update method
   // Hopefully we are storing the main thread value here :)
   //  [ATM any window initialization is always be performed by the main thread]