]> git.street.me.uk Git - andy/viking.git/blobdiff - src/clipboard.c
[DOC] Amend help to detail new merge, split and delete points track operations.
[andy/viking.git] / src / clipboard.c
index 161d132129c3553b23adfaefa3e575dfdd6c8ac4..fc601d3f4a1bb92dc84c965fef5aee0242265fc4 100644 (file)
@@ -2,6 +2,7 @@
  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
  *
  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
  *
  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
+ * Copyright (C) 2005, Alex Foobarian <foobarian@gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  */
 
  *
  */
 
-#include <stdio.h>
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include <string.h>
 #include <stdlib.h>
 #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;
 
 
 typedef struct {
   gpointer clipboard;
   gint pid;
-  guint8 type;
+  VikClipboardDataType type;
   gint subtype;
   guint16 layer_type;
   guint len;
   gint subtype;
   guint16 layer_type;
   guint len;
+  gchar *text;
   guint8 data[0];
 } vik_clipboard_t;
 
   guint8 data[0];
 } vik_clipboard_t;
 
@@ -51,20 +61,24 @@ static GtkTargetEntry target_table[] = {
 static void clip_get ( GtkClipboard *c, GtkSelectionData *selection_data, guint info, gpointer p ) 
 {
   vik_clipboard_t *vc = p;
 static void clip_get ( GtkClipboard *c, GtkSelectionData *selection_data, guint info, gpointer p ) 
 {
   vik_clipboard_t *vc = p;
-  if (info==0) {
+  if ( info == 0 ) {
+    // Viking Data Type
     //    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 );
   }
     //    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 );
-    } 
+  if ( info == 1 ) {
+    // Should be a string, but make sure it's something
+    if ( vc->text )
+      gtk_selection_data_set_text ( selection_data, vc->text, -1); // string text is null terminated
   }
   }
+
 }
 
 static void clip_clear ( GtkClipboard *c, gpointer p )
 {
 }
 
 static void clip_clear ( GtkClipboard *c, gpointer p )
 {
-  g_free(p);
+  vik_clipboard_t* vc = (vik_clipboard_t*)p;
+  g_free(vc->text);
+  g_free(vc);
 }
 
 
 }
 
 
@@ -78,7 +92,7 @@ static void clip_receive_viking ( GtkClipboard *c, GtkSelectionData *sd, gpointe
   VikLayersPanel *vlp = p;
   vik_clipboard_t *vc;
   if (sd->length == -1) {
   VikLayersPanel *vlp = p;
   vik_clipboard_t *vc;
   if (sd->length == -1) {
-    g_warning ( "paste failed" );
+    g_warning ( _("paste failed") );
     return;
   } 
   //  g_print("clip receive: target = %s, type = %s\n", gdk_atom_name(sd->target), gdk_atom_name(sd->type));
     return;
   } 
   //  g_print("clip receive: target = %s, type = %s\n", gdk_atom_name(sd->target), gdk_atom_name(sd->type));
@@ -88,16 +102,16 @@ static void clip_receive_viking ( GtkClipboard *c, GtkSelectionData *sd, gpointe
   //  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_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" );
+    g_warning ( _("wrong clipboard data size") );
     return;
   }
 
     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 );
   }
   {
     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->type == DATA_SUBLAYER )
+  else if ( vc->type == VIK_CLIPBOARD_DATA_SUBLAYER )
   {
     VikLayer *sel = vik_layers_panel_get_selected ( vlp );
     if ( sel && sel->type == vc->layer_type)
   {
     VikLayer *sel = vik_layers_panel_get_selected ( vlp );
     if ( sel && sel->type == vc->layer_type)
@@ -106,15 +120,24 @@ static void clip_receive_viking ( GtkClipboard *c, GtkSelectionData *sd, gpointe
         vik_layer_get_interface(vc->layer_type)->paste_item ( sel, vc->subtype, vc->data, vc->len);
     }
     else
         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 %s layers. "
+                                  "You must select a layer of this type to paste the clipboard data."),
+                                vik_layer_get_interface(vc->layer_type)->name );
   }
 }
 
 
 
   }
 }
 
 
 
-/*
- * utility func to handle pasted text:
- * search for N dd.dddddd W dd.dddddd, N dd° dd.dddd W dd° dd.ddddd and so forth
+/**
+ * clip_parse_latlon:
+ * @text: text containing LatLon data.
+ * @coord: computed coordinates.
+ *
+ * Utility func to handle pasted text:
+ * search for N dd.dddddd W dd.dddddd, N dd° dd.dddd W dd° dd.ddddd and so forth.
+ *
+ * Returns: TRUE if coordinates are set.
  */
 static gboolean clip_parse_latlon ( const gchar *text, struct LatLon *coord ) 
 {
  */
 static gboolean clip_parse_latlon ( const gchar *text, struct LatLon *coord ) 
 {
@@ -149,8 +172,6 @@ static gboolean clip_parse_latlon ( const gchar *text, struct LatLon *coord )
       gint j, k;
       cand = s+i;
 
       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";
       for (j=0; j<2; j++) {
        for (k=0; k<2; k++) {
          gchar fmt1[] = "N %d%*[ ]%lf W %d%*[ ]%lf";
@@ -212,7 +233,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 {
   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);
   }
 }
 
   }
 }
 
@@ -229,7 +250,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;
 static void clip_receive_html ( GtkClipboard *c, GtkSelectionData *sd, gpointer p ) 
 {
   VikLayersPanel *vlp = p;
-  guint r, w;
+  gsize r, w;
   GError *err = NULL;
   gchar *s, *span;
   gint tag = 0, i;
   GError *err = NULL;
   gchar *s, *span;
   gint tag = 0, i;
@@ -270,17 +291,18 @@ static void clip_receive_html ( GtkClipboard *c, GtkSelectionData *sd, gpointer
   g_free(s);
 }
 
   g_free(s);
 }
 
-/*
- * deal with various data types a clipboard may hold 
+/**
+ * clip_receive_targets:
+ *
+ * Deal with various data types a clipboard may hold.
  */
 void clip_receive_targets ( GtkClipboard *c, GdkAtom *a, gint n, gpointer p )
 {
   VikLayersPanel *vlp = p;
   gint i;
 
  */
 void clip_receive_targets ( GtkClipboard *c, GdkAtom *a, gint n, gpointer p )
 {
   VikLayersPanel *vlp = p;
   gint i;
 
-  /*  g_print("got targets\n"); */
   for (i=0; i<n; i++) {
   for (i=0; i<n; i++) {
-    /* g_print("  ""%s""\n", gdk_atom_name(a[i])); */
+    // g_print("  ""%s""\n", gdk_atom_name(a[i]));
     if (!strcmp(gdk_atom_name(a[i]), "text/html")) {
       gtk_clipboard_request_contents ( c, gdk_atom_intern("text/html", TRUE), clip_receive_html, vlp );
       break;
     if (!strcmp(gdk_atom_name(a[i]), "text/html")) {
       gtk_clipboard_request_contents ( c, gdk_atom_intern("text/html", TRUE), clip_receive_html, vlp );
       break;
@@ -300,61 +322,72 @@ void clip_receive_targets ( GtkClipboard *c, GdkAtom *a, gint n, gpointer p )
  ** public functions                                                            **
  *********************************************************************************/
 
  ** public functions                                                            **
  *********************************************************************************/
 
-/* 
- * make a copy of selected object and associate ourselves with the clipboard
+/**
+ * a_clipboard_copy_selected:
+ *
+ * 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;
 {
   VikLayer *sel = vik_layers_panel_get_selected ( vlp );
   GtkTreeIter iter;
-  vik_clipboard_t *vc = g_malloc(sizeof(*vc));
-  vik_clipboard_t *vc2;
-  GtkClipboard *c = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD );
+  VikClipboardDataType type;
+  guint16 layer_type = 0;
+  gint subtype = 0;
   guint8 *data = NULL;
   guint len;
   guint8 *data = NULL;
   guint len;
+  const gchar *name = NULL;
 
   if ( ! sel )
     return;
 
   vik_treeview_get_selected_iter ( sel->vt, &iter );
 
   if ( ! sel )
     return;
 
   vik_treeview_get_selected_iter ( sel->vt, &iter );
+  layer_type = sel->type;
 
   if ( vik_treeview_item_get_type ( sel->vt, &iter ) == VIK_TREEVIEW_TYPE_SUBLAYER ) {
 
   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->subtype = vik_treeview_item_get_data(sel->vt, &iter);
-      vik_layer_get_interface(vc->layer_type)->copy_item(sel, vc->subtype, vik_treeview_item_get_pointer(sel->vt, &iter), &data, &len );
-      vc->type = DATA_SUBLAYER;
-    }    
+    type = VIK_CLIPBOARD_DATA_SUBLAYER;
+    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 );
+      // This name is used in setting the text representation of the item on the clipboard.
+      name = vik_treeview_item_get_name(sel->vt, &iter);
+    }
   }
   else
   {
   }
   else
   {
-    vc->type = DATA_LAYER;
-    vik_layer_marshall ( sel, &data, &len );
+    gint ilen;
+    type = VIK_CLIPBOARD_DATA_LAYER;
+    vik_layer_marshall ( sel, &data, &ilen );
+    len = ilen;
+    name = vik_layer_get_name ( vik_treeview_item_get_pointer(sel->vt, &iter) );
   }
 
   }
 
-
-  /* if one of the above copy operations worked, put the data after a vc header and kill the old one */
   if (data) {
   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;
-
-    vc->pid = getpid();
-    gtk_clipboard_set_with_data ( c, target_table, G_N_ELEMENTS(target_table), clip_get, clip_clear, vc );
-
-  } else {
-    g_free(vc);
+    a_clipboard_copy( type, layer_type, subtype, len, name, data);
   }
   }
+}
+
+void a_clipboard_copy( VikClipboardDataType type, guint16 layer_type, gint subtype, guint len, const gchar* text, 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;
+  vc->text = g_strdup (text);
+  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 );
 }
 
 }
 
-/*
- * to deal with multiple data types, we first request the type of data on the clipboard,
- * and handle them in the callback
+/**
+ * a_clipboard_paste:
+ *
+ * To deal with multiple data types, we first request the type of data on the clipboard,
+ * and handle them in the callback.
  */
 gboolean a_clipboard_paste ( VikLayersPanel *vlp )
 {
  */
 gboolean a_clipboard_paste ( VikLayersPanel *vlp )
 {