]> git.street.me.uk Git - andy/viking.git/blobdiff - src/clipboard.c
Add OSRM routing engine
[andy/viking.git] / src / clipboard.c
index 88545fa1e8c15dd3a32a49fb4133f9310de3c161..9538eadba2ca947911b772a84b1ae61730b3b11a 100644 (file)
@@ -240,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;
+
+  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;
-  //  g_print("got text: %s\n", text);
   if (clip_parse_latlon(text, &coord)) {
     clip_add_wp(vlp, &coord);
   }
@@ -302,7 +317,7 @@ void clip_receive_targets ( 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]));
+    //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;
@@ -344,27 +359,33 @@ void a_clipboard_copy_selected ( VikLayersPanel *vlp )
   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 );
-      // 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) );
+  // 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;
   }
-
-  if (data) {
-    a_clipboard_copy( type, layer_type, subtype, len, name, data);
+  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) );
+    }
   }
+  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)
@@ -377,10 +398,17 @@ void a_clipboard_copy( VikClipboardDataType type, guint16 layer_type, gint subty
   vc->subtype = subtype;
   vc->len = len;
   vc->text = g_strdup (text);
-  memcpy(vc->data, data, len);
-  g_free(data);
+  if ( data ) {
+    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 );
+
+  // 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 );
 }
 
 /**