]> git.street.me.uk Git - andy/viking.git/blobdiff - src/clipboard.c
Relocate current data field
[andy/viking.git] / src / clipboard.c
index 83a1fd81be8ae9b91d95abba1c05dfb2e6590eca..8b2b3f26d1a4f4bacae25ccc04465b2a7c46f668 100644 (file)
  *
  */
 
-#include <stdio.h>
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include <string.h>
 #include <stdlib.h>
-#include "viking.h"
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#include <glib/gi18n.h>
 
-#define DATA_NONE 0
-#define DATA_LAYER 1
-#define DATA_SUBLAYER 2
+#include "viking.h"
 
 
 typedef struct {
   gpointer clipboard;
   gint pid;
-  guint8 type;
+  VikClipboardDataType type;
   gint subtype;
   guint16 layer_type;
   guint len;
@@ -51,28 +59,15 @@ static GtkTargetEntry target_table[] = {
 static void clip_get ( GtkClipboard *c, GtkSelectionData *selection_data, guint info, gpointer p ) 
 {
   vik_clipboard_t *vc = p;
-  //  g_print("clip_get\n");
   if (info==0) {
+    //    g_print("clip_get: vc = %p, size = %d\n", vc, sizeof(*vc) + vc->len);
     gtk_selection_data_set ( selection_data, selection_data->target, 8, (void *)vc, sizeof(*vc) + vc->len );
   }
-  if (info==1) {
-    if (vc->type == DATA_LAYER) {
-      gtk_selection_data_set_text ( selection_data, VIK_LAYER(vc->clipboard)->name, -1 );
-    } 
-  }
 }
 
 static void clip_clear ( GtkClipboard *c, gpointer p )
 {
-  vik_clipboard_t *vc = p;
-  //  g_print("clip_clear\n");
-
-  if ( vc->type == DATA_LAYER ) {
-    g_free(vc);
-  }
-  else if ( vc->clipboard && vc->type == DATA_SUBLAYER )
-    if ( vik_layer_get_interface(vc->layer_type)->free_copied_item )
-      vik_layer_get_interface(vc->layer_type)->free_copied_item(vc->subtype,vc->clipboard);
+  g_free(p);
 }
 
 
@@ -86,30 +81,38 @@ static void clip_receive_viking ( GtkClipboard *c, GtkSelectionData *sd, gpointe
   VikLayersPanel *vlp = p;
   vik_clipboard_t *vc;
   if (sd->length == -1) {
-    g_print("receive failed\n");
+    g_warning ( _("paste failed") );
     return;
   } 
   //  g_print("clip receive: target = %s, type = %s\n", gdk_atom_name(sd->target), gdk_atom_name(sd->type));
   g_assert(!strcmp(gdk_atom_name(sd->target), target_table[0].target));
 
   vc = (vik_clipboard_t *)sd->data;
-  g_assert(sd->length == sizeof(*vc) + vc->len);
+  //  g_print("  sd->data = %p, sd->length = %d, vc->len = %d\n", sd->data, sd->length, vc->len);
+
+  if (sd->length != sizeof(*vc) + vc->len) {
+    g_warning ( _("wrong clipboard data size") );
+    return;
+  }
 
-  if ( vc->type == DATA_LAYER )
+  if ( vc->type == VIK_CLIPBOARD_DATA_LAYER )
   {
     VikLayer *new_layer = vik_layer_unmarshall ( vc->data, vc->len, vik_layers_panel_get_viewport(vlp) );
     vik_layers_panel_add_layer ( vlp, new_layer );
   }
-  else if ( vc->clipboard && vc->type == DATA_SUBLAYER && vc->pid == getpid())
+  else if ( vc->type == VIK_CLIPBOARD_DATA_SUBLAYER )
   {
     VikLayer *sel = vik_layers_panel_get_selected ( vlp );
     if ( sel && sel->type == vc->layer_type)
     {
       if ( vik_layer_get_interface(vc->layer_type)->paste_item )
-        vik_layer_get_interface(vc->layer_type)->paste_item ( sel, vc->subtype, vc->clipboard );
+        vik_layer_get_interface(vc->layer_type)->paste_item ( sel, vc->subtype, vc->data, vc->len);
     }
     else
-      a_dialog_error_msg_extra ( VIK_GTK_WINDOW_FROM_WIDGET(GTK_WIDGET(vlp)), "The clipboard contains sublayer data for a %s layers. You must select a layer of this type to paste the clipboard data.", vik_layer_get_interface(vc->layer_type)->name );
+      a_dialog_error_msg_extra ( VIK_GTK_WINDOW_FROM_WIDGET(GTK_WIDGET(vlp)),
+                                _("The clipboard contains sublayer data for a %s layers."
+                                  "You must select a layer of this type to paste the clipboard data."),
+                                vik_layer_get_interface(vc->layer_type)->name );
   }
 }
 
@@ -152,8 +155,6 @@ static gboolean clip_parse_latlon ( const gchar *text, struct LatLon *coord )
       gint j, k;
       cand = s+i;
 
-      //      printf("Trying >>>>> %s\n", cand);
-
       for (j=0; j<2; j++) {
        for (k=0; k<2; k++) {
          gchar fmt1[] = "N %d%*[ ]%lf W %d%*[ ]%lf";
@@ -215,7 +216,7 @@ static void clip_add_wp(VikLayersPanel *vlp, struct LatLon *coord)
   if (sel && sel->type == VIK_LAYER_TRW) {
     vik_trw_layer_new_waypoint ( VIK_TRW_LAYER(sel), VIK_GTK_WINDOW_FROM_LAYER(sel), &vc );
   } else {
-    a_dialog_error_msg_extra ( VIK_GTK_WINDOW_FROM_WIDGET(GTK_WIDGET(vlp)), "In order to paste a waypoint, please select an appropriate layer to paste into.", NULL);
+    a_dialog_error_msg_extra ( VIK_GTK_WINDOW_FROM_WIDGET(GTK_WIDGET(vlp)), _("In order to paste a waypoint, please select an appropriate layer to paste into."), NULL);
   }
 }
 
@@ -232,7 +233,7 @@ static void clip_receive_text (GtkClipboard *c, const gchar *text, gpointer p)
 static void clip_receive_html ( GtkClipboard *c, GtkSelectionData *sd, gpointer p ) 
 {
   VikLayersPanel *vlp = p;
-  gint r, w;
+  gsize r, w;
   GError *err = NULL;
   gchar *s, *span;
   gint tag = 0, i;
@@ -243,7 +244,7 @@ static void clip_receive_html ( GtkClipboard *c, GtkSelectionData *sd, gpointer
   } 
 
   /* - copying from Mozilla seems to give html in UTF-16. */
-  if (!(s =  g_convert ( sd->data, sd->length, "UTF-8", "UTF-16", &r, &w, &err))) {
+  if (!(s =  g_convert ( (gchar *)sd->data, sd->length, "UTF-8", "UTF-16", &r, &w, &err))) {
     return;
   }
   //  g_print("html is %d bytes long: %s\n", sd->length, s);
@@ -306,13 +307,14 @@ void clip_receive_targets ( GtkClipboard *c, GdkAtom *a, gint n, gpointer p )
 /* 
  * make a copy of selected object and associate ourselves with the clipboard
  */
-void a_clipboard_copy ( VikLayersPanel *vlp )
+void a_clipboard_copy_selected ( VikLayersPanel *vlp )
 {
   VikLayer *sel = vik_layers_panel_get_selected ( vlp );
   GtkTreeIter iter;
-  vik_clipboard_t *vc = g_malloc(sizeof(*vc));
-  GtkClipboard *c = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD );
-  guint8 *data;
+  VikClipboardDataType type;
+  guint16 layer_type = 0;
+  gint subtype = 0;
+  guint8 *data = NULL;
   guint len;
 
   if ( ! sel )
@@ -320,39 +322,38 @@ void a_clipboard_copy ( VikLayersPanel *vlp )
 
   vik_treeview_get_selected_iter ( sel->vt, &iter );
 
-  if ( vik_treeview_item_get_type ( sel->vt, &iter ) == VIK_TREEVIEW_TYPE_SUBLAYER )
-  {
-    vc->layer_type = sel->type;
-    if ( vik_layer_get_interface(vc->layer_type)->copy_item && (vc->clipboard = vik_layer_get_interface(vc->layer_type)->
-        copy_item(sel,vc->subtype=vik_treeview_item_get_data(sel->vt,&iter),vik_treeview_item_get_pointer(sel->vt,&iter)) ))
-    {
-      vc->type = DATA_SUBLAYER;
-      vc->len = 0;
+  if ( vik_treeview_item_get_type ( sel->vt, &iter ) == VIK_TREEVIEW_TYPE_SUBLAYER ) {
+    type = VIK_CLIPBOARD_DATA_SUBLAYER;
+    layer_type = sel->type;
+    if ( vik_layer_get_interface(layer_type)->copy_item) {
+      subtype = vik_treeview_item_get_data(sel->vt, &iter);
+      vik_layer_get_interface(layer_type)->copy_item(sel, subtype, vik_treeview_item_get_pointer(sel->vt, &iter), &data, &len );
     }    
-    else {
-      g_free(vc);
-      return; /* selected sublayer is uncopyable */
-    }
   }
   else
   {
-    vik_clipboard_t *vc2;
-    vc->type = DATA_LAYER;
-    
-    vik_layer_marshall ( sel, &data, &len );
-    if (data) {
-      vc2 = g_malloc(sizeof(*vc2) + len);
-      *vc2 = *vc;
-      vc2->len = len;
-      memcpy(vc2->data, data, len);
-      g_free(data);
-      g_free(vc);
-      vc = vc2;
-    } else {
-      g_free(vc);
-      return;
-    }
+    gint ilen;
+    type = VIK_CLIPBOARD_DATA_LAYER;
+    vik_layer_marshall ( sel, &data, &ilen );
+    len = ilen;
   }
+
+  if (data)
+    a_clipboard_copy( type, layer_type, subtype, len, data);
+
+}
+
+void a_clipboard_copy( VikClipboardDataType type, guint16 layer_type, gint subtype, guint len, guint8 * data)
+{
+  vik_clipboard_t * vc = g_malloc(sizeof(*vc) + len);
+  GtkClipboard *c = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD );
+
+  vc->type = type;
+  vc->layer_type = layer_type;
+  vc->subtype = subtype;
+  vc->len = len;
+  memcpy(vc->data, data, len);
+  g_free(data);
   vc->pid = getpid();
   gtk_clipboard_set_with_data ( c, target_table, G_N_ELEMENTS(target_table), clip_get, clip_clear, vc );
 }