]> git.street.me.uk Git - andy/viking.git/blobdiff - src/clipboard.c
[QA] Remove spurious commented out code.
[andy/viking.git] / src / clipboard.c
index 81b778cbafed0e61c8241a3490d1a036eefc0bae..9538eadba2ca947911b772a84b1ae61730b3b11a 100644 (file)
@@ -45,6 +45,7 @@ typedef struct {
   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;
 
@@ -60,15 +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);
     //    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 );
+    gtk_selection_data_set ( selection_data, gtk_selection_data_get_target(selection_data), 8, (void *)vc, sizeof(*vc) + vc->len );
   }
   }
+  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);
 }
 
 
 }
 
 
@@ -81,17 +91,17 @@ static void clip_receive_viking ( GtkClipboard *c, GtkSelectionData *sd, gpointe
 {
   VikLayersPanel *vlp = p;
   vik_clipboard_t *vc;
 {
   VikLayersPanel *vlp = p;
   vik_clipboard_t *vc;
-  if (sd->length == -1) {
+  if (gtk_selection_data_get_length(sd) == -1) {
     g_warning ( _("paste failed") );
     return;
   } 
     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));
+  //  g_print("clip receive: target = %s, type = %s\n", gdk_atom_name(gtk_selection_data_get_target(sd), gdk_atom_name(sd->type));
+  g_assert(!strcmp(gdk_atom_name(gtk_selection_data_get_target(sd)), target_table[0].target));
 
 
-  vc = (vik_clipboard_t *)sd->data;
+  vc = (vik_clipboard_t *)gtk_selection_data_get_data(sd);
   //  g_print("  sd->data = %p, sd->length = %d, vc->len = %d\n", sd->data, sd->length, 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) {
+  if (gtk_selection_data_get_length(sd) != sizeof(*vc) + vc->len) {
     g_warning ( _("wrong clipboard data size") );
     return;
   }
     g_warning ( _("wrong clipboard data size") );
     return;
   }
@@ -119,9 +129,15 @@ static void clip_receive_viking ( GtkClipboard *c, GtkSelectionData *sd, gpointe
 
 
 
 
 
 
-/*
- * 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 ) 
 {
@@ -224,8 +240,23 @@ static void clip_add_wp(VikLayersPanel *vlp, struct LatLon *coord)
 static void clip_receive_text (GtkClipboard *c, const gchar *text, gpointer p)
 {
   VikLayersPanel *vlp = p;
 static void clip_receive_text (GtkClipboard *c, const gchar *text, gpointer p)
 {
   VikLayersPanel *vlp = p;
+
+  g_debug ( "got text: %s", text );
+
+  VikLayer *sel = vik_layers_panel_get_selected ( vlp );
+  if ( sel && vik_treeview_get_editing ( sel->vt ) ) {
+    GtkTreeIter iter;
+    if ( vik_treeview_get_selected_iter ( sel->vt, &iter ) ) {
+      // Try to sanitize input:
+      gchar *name = g_strescape ( text, NULL );
+      vik_layer_rename ( sel, name );
+      vik_treeview_item_set_name ( sel->vt, &iter, name );
+      g_free ( name );
+    }
+    return;
+  }
+
   struct LatLon coord;
   struct LatLon coord;
-  //  g_print("got text: %s\n", text);
   if (clip_parse_latlon(text, &coord)) {
     clip_add_wp(vlp, &coord);
   }
   if (clip_parse_latlon(text, &coord)) {
     clip_add_wp(vlp, &coord);
   }
@@ -240,15 +271,15 @@ static void clip_receive_html ( GtkClipboard *c, GtkSelectionData *sd, gpointer
   gint tag = 0, i;
   struct LatLon coord;
 
   gint tag = 0, i;
   struct LatLon coord;
 
-  if (sd->length == -1) {
+  if (gtk_selection_data_get_length(sd) == -1) {
     return;
   } 
 
   /* - copying from Mozilla seems to give html in UTF-16. */
     return;
   } 
 
   /* - copying from Mozilla seems to give html in UTF-16. */
-  if (!(s =  g_convert ( (gchar *)sd->data, sd->length, "UTF-8", "UTF-16", &r, &w, &err))) {
+  if (!(s =  g_convert ( (gchar *)gtk_selection_data_get_data(sd), gtk_selection_data_get_length(sd), "UTF-8", "UTF-16", &r, &w, &err))) {
     return;
   }
     return;
   }
-  //  g_print("html is %d bytes long: %s\n", sd->length, s);
+  //  g_print("html is %d bytes long: %s\n", gtk_selection_data_get_length(sd), s);
 
   /* scrape a coordinate pasted from a geocaching.com page: look for a 
    * telltale tag if possible, and then remove tags 
 
   /* scrape a coordinate pasted from a geocaching.com page: look for a 
    * telltale tag if possible, and then remove tags 
@@ -275,17 +306,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;
@@ -305,8 +337,10 @@ 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_selected ( VikLayersPanel *vlp )
 {
  */
 void a_clipboard_copy_selected ( VikLayersPanel *vlp )
 {
@@ -317,6 +351,7 @@ void a_clipboard_copy_selected ( VikLayersPanel *vlp )
   gint subtype = 0;
   guint8 *data = NULL;
   guint len;
   gint subtype = 0;
   guint8 *data = NULL;
   guint len;
+  const gchar *name = NULL;
 
   if ( ! sel )
     return;
 
   if ( ! sel )
     return;
@@ -324,27 +359,36 @@ void a_clipboard_copy_selected ( VikLayersPanel *vlp )
   vik_treeview_get_selected_iter ( sel->vt, &iter );
   layer_type = sel->type;
 
   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 ) {
-    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 );
-    }    
+  // Since we intercept copy and paste keyboard operations, this is called even when a cell is being edited
+  if ( vik_treeview_get_editing ( sel->vt ) ) {
+    type = VIK_CLIPBOARD_DATA_TEXT;
+    //  I don't think we can access what is actually selected (internal to GTK) so we go for the name of the item
+    // At least this is better than copying the layer data - which is even further away from what the user would be expecting...
+    name = vik_treeview_item_get_name ( sel->vt, &iter );
+    len = 0;
   }
   }
-  else
-  {
-    gint ilen;
-    type = VIK_CLIPBOARD_DATA_LAYER;
-    vik_layer_marshall ( sel, &data, &ilen );
-    len = ilen;
+  else {
+    if ( vik_treeview_item_get_type ( sel->vt, &iter ) == VIK_TREEVIEW_TYPE_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 {
+      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 (data)
-    a_clipboard_copy( type, layer_type, subtype, len, data);
-
+  a_clipboard_copy ( type, layer_type, subtype, len, name, data );
 }
 
 }
 
-void a_clipboard_copy( VikClipboardDataType type, guint16 layer_type, gint subtype, guint len, guint8 * 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 );
 {
   vik_clipboard_t * vc = g_malloc(sizeof(*vc) + len);
   GtkClipboard *c = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD );
@@ -353,15 +397,25 @@ void a_clipboard_copy( VikClipboardDataType type, guint16 layer_type, gint subty
   vc->layer_type = layer_type;
   vc->subtype = subtype;
   vc->len = len;
   vc->layer_type = layer_type;
   vc->subtype = subtype;
   vc->len = len;
-  memcpy(vc->data, data, len);
-  g_free(data);
+  vc->text = g_strdup (text);
+  if ( data ) {
+    memcpy(vc->data, data, len);
+    g_free(data);
+  }
   vc->pid = getpid();
   vc->pid = getpid();
-  gtk_clipboard_set_with_data ( c, target_table, G_N_ELEMENTS(target_table), clip_get, clip_clear, vc );
+
+  // Simple clipboard copy when necessary
+  if ( type == VIK_CLIPBOARD_DATA_TEXT )
+    gtk_clipboard_set_text ( c, text, -1 );
+  else
+    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 )
 {
@@ -370,3 +424,61 @@ gboolean a_clipboard_paste ( VikLayersPanel *vlp )
   return TRUE;
 }
 
   return TRUE;
 }
 
+/**
+ *
+ * Detect our own data types
+ */
+static void clip_determine_viking_type ( GtkClipboard *c, GtkSelectionData *sd, gpointer p )
+{
+  VikClipboardDataType *vdct = p;
+  // Default value
+  *vdct = VIK_CLIPBOARD_DATA_NONE;
+  vik_clipboard_t *vc;
+  if (gtk_selection_data_get_length(sd) == -1) {
+    g_warning ("DETERMINING TYPE: length failure");
+    return;
+  }
+
+  vc = (vik_clipboard_t *)gtk_selection_data_get_data(sd);
+
+  if ( !vc->type )
+    return;
+
+  if ( vc->type == VIK_CLIPBOARD_DATA_LAYER ) {
+    *vdct = VIK_CLIPBOARD_DATA_LAYER;
+  }
+  else if ( vc->type == VIK_CLIPBOARD_DATA_SUBLAYER ) {
+    *vdct = VIK_CLIPBOARD_DATA_SUBLAYER;
+  }
+  else {
+    g_warning ("DETERMINING TYPE: THIS SHOULD NEVER HAPPEN");
+  }
+}
+
+static void clip_determine_type ( GtkClipboard *c, GdkAtom *a, gint n, gpointer p )
+{
+  gint i;
+  for (i=0; i<n; i++) {
+    // g_print("  ""%s""\n", gdk_atom_name(a[i]));
+    if (!strcmp(gdk_atom_name(a[i]), "application/viking")) {
+      gtk_clipboard_request_contents ( c, gdk_atom_intern("application/viking", TRUE), clip_determine_viking_type, p );
+      break;
+    }
+  }
+}
+
+/**
+ * a_clipboard_type:
+ *
+ * Return the type of data held in the clipboard if any
+ */
+VikClipboardDataType a_clipboard_type ( )
+{
+  GtkClipboard *c = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD );
+  VikClipboardDataType *vcdt = g_malloc ( sizeof (VikClipboardDataType) );
+
+  gtk_clipboard_request_targets ( c, clip_determine_type, vcdt );
+  gint answer = *vcdt;
+  g_free ( vcdt );
+  return answer;
+}