X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/7680e9feeb6cd9e2fd1d5c70fde45c178b71ee75..9e4fdfe4455bace71d5b7cceb3988b8dfc48964a:/src/clipboard.c diff --git a/src/clipboard.c b/src/clipboard.c index a4bea180..61c7c852 100644 --- a/src/clipboard.c +++ b/src/clipboard.c @@ -2,6 +2,7 @@ * viking -- GPS Data and Topo Analyzer, Explorer, and Manager * * Copyright (C) 2003-2005, Evan Battaglia + * Copyright (C) 2005, Alex Foobarian * * 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 @@ -19,21 +20,33 @@ * */ -#include +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include #include -#include "viking.h" +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif + +#include -#define DATA_NONE 0 -#define DATA_LAYER 1 -#define DATA_SUBLAYER 2 +#include "viking.h" typedef struct { gpointer clipboard; - guint8 type; + gint pid; + VikClipboardDataType type; gint subtype; guint16 layer_type; + guint len; + gchar *text; + guint8 data[0]; } vik_clipboard_t; static GtkTargetEntry target_table[] = { @@ -48,29 +61,24 @@ 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) { - gtk_selection_data_set ( selection_data, selection_data->target, 8, (void *)vc, sizeof(*vc) ); + 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, gtk_selection_data_get_target(selection_data), 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 ) { - vik_clipboard_t *vc = p; - // g_print("clip_clear\n"); - - if ( vc->clipboard && vc->type == DATA_LAYER ) - g_object_unref ( G_OBJECT(vc->clipboard) ); - 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); - vc->clipboard = NULL; - vc->type = DATA_NONE; + vik_clipboard_t* vc = (vik_clipboard_t*)p; + g_free(vc->text); + g_free(vc); } @@ -83,55 +91,58 @@ 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"); + if (gtk_selection_data_get_length(sd) == -1) { + 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_assert(sd->length == sizeof(*vc)); + // 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); - if ( vc->clipboard && vc->type == DATA_LAYER ) + if (gtk_selection_data_get_length(sd) != sizeof(*vc) + vc->len) { + g_warning ( _("wrong clipboard data size") ); + return; + } + + if ( vc->type == VIK_CLIPBOARD_DATA_LAYER ) { - /* oooh, _private_ ... so sue me, there's no other way to do this. */ - if ( G_OBJECT(vc->clipboard)->ref_count == 1 ) - { /* optimization -- if layer has been deleted, don't copy the layer. */ - g_object_ref ( G_OBJECT(vc->clipboard) ); - vik_layers_panel_add_layer ( vlp, VIK_LAYER(vc->clipboard) ); - } - else - { - VikLayer *new_layer = vik_layer_copy ( VIK_LAYER(vc->clipboard), vik_layers_panel_get_viewport(vlp) ); - if ( new_layer ) - { - 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->clipboard && 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) { 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 %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 ) { - gint latdeg, londeg; + gint latdeg, londeg, latmi, lonmi; + gdouble lats, lons; gdouble latm, lonm; gdouble lat, lon; gchar *cand; @@ -149,7 +160,7 @@ static gboolean clip_parse_latlon ( const gchar *text, struct LatLon *coord ) if (g_ascii_isdigit (s[i]) && s[i+1] != '.' && !g_ascii_isdigit (s[i+1])) { s[i+1] = ' '; if (!g_ascii_isalnum (s[i+2]) && s[i+2] != '-') { - s[i+2] = ' '; + s[i+2] = ' '; } } } @@ -162,44 +173,63 @@ static gboolean clip_parse_latlon ( const gchar *text, struct LatLon *coord ) gint j, k; cand = s+i; - // printf("Trying >>>>> %s\n", cand); - + // First try matching strings containing the cardinal directions for (j=0; j<2; j++) { - for (k=0; k<2; k++) { - gchar fmt1[] = "N %d%*[ ]%lf W %d%*[ ]%lf"; - gchar fmt2[] = "%d%*[ ]%lf N %d%*[ ]%lf W"; - gchar fmt3[] = "N %lf W %lf"; - gchar fmt4[] = "%lf N %lf W"; - - fmt1[0] = latc[j]; fmt1[13] = lonc[k]; - fmt2[11] = latc[j]; fmt2[24] = lonc[k]; - fmt3[0] = latc[j]; fmt3[6] = lonc[k]; - fmt4[4] = latc[j]; fmt4[10] = lonc[k]; - - if (sscanf(cand, fmt1, &latdeg, &latm, &londeg, &lonm) == 4 || - sscanf(cand, fmt2, &latdeg, &latm, &londeg, &lonm) == 4) { - lat = (j*2-1) * (latdeg + latm / 60); - lon = (k*2-1) * (londeg + lonm / 60); - break; - } - if (sscanf(cand, fmt3, &lat, &lon) == 2 || - sscanf(cand, fmt4, &lat, &lon) == 2) { - lat *= (j*2-1); - lon *= (k*2-1); - break; - } - } - if (k!=2) break; + for (k=0; k<2; k++) { + // DMM + gchar fmt1[] = "N %d%*[ ]%lf W %d%*[ ]%lf"; + gchar fmt2[] = "%d%*[ ]%lf N %d%*[ ]%lf W"; + // DDD + gchar fmt3[] = "N %lf W %lf"; + gchar fmt4[] = "%lf N %lf W"; + // DMS + gchar fmt5[] = "N%d%*[ ]%d%*[ ]%lf%*[ ]W%d%*[ ]%d%*[ ]%lf"; + + // Substitute in 'N','E','S' or 'W' values for each attempt + fmt1[0] = latc[j]; fmt1[13] = lonc[k]; + fmt2[11] = latc[j]; fmt2[24] = lonc[k]; + fmt3[0] = latc[j]; fmt3[6] = lonc[k]; + fmt4[4] = latc[j]; fmt4[10] = lonc[k]; + fmt5[0] = latc[j]; fmt5[23] = lonc[k]; + + if (sscanf(cand, fmt1, &latdeg, &latm, &londeg, &lonm) == 4 || + sscanf(cand, fmt2, &latdeg, &latm, &londeg, &lonm) == 4) { + lat = (j*2-1) * (latdeg + latm / 60); + lon = (k*2-1) * (londeg + lonm / 60); + break; + } + if (sscanf(cand, fmt3, &lat, &lon) == 2 || + sscanf(cand, fmt4, &lat, &lon) == 2) { + lat *= (j*2-1); + lon *= (k*2-1); + break; + } + gint am = sscanf(cand, fmt5, &latdeg, &latmi, &lats, &londeg, &lonmi, &lons); + if ( am == 6 ) { + lat = (j*2-1) * (latdeg + latmi / 60.0 + lats / 3600.0); + lon = (k*2-1) * (londeg + lonmi / 60.0 + lons / 3600.0); + break; + } + } + if (k!=2) break; } if (j!=2) break; - if (sscanf(cand, "%d%*[ ]%lf %d%*[ ]%lf", &latdeg, &latm, &londeg, &lonm) == 4) { - lat = latdeg/abs(latdeg) * (abs(latdeg) + latm / 60); - lon = londeg/abs(londeg) * (abs(londeg) + lonm / 60); - break; + // DMM without Cardinal directions + if (sscanf(cand, "%d%*[ ]%lf*[ ]%d%*[ ]%lf", &latdeg, &latm, &londeg, &lonm) == 4) { + lat = latdeg/abs(latdeg) * (abs(latdeg) + latm / 60); + lon = londeg/abs(londeg) * (abs(londeg) + lonm / 60); + break; + } + // DMS without Cardinal directions + if (sscanf(cand, "%d%*[ ]%d%*[ ]%lf%*[ ]%d%*[ ]%d%*[ ]%lf", &latdeg, &latmi, &lats, &londeg, &lonmi, &lons) == 6) { + lat = latdeg/abs(latdeg) * (abs(latdeg) + latm / 60.0 + lats / 3600.0); + lon = londeg/abs(londeg) * (abs(londeg) + lonm / 60.0 + lons / 3600.0); + break; } + // Raw values if (sscanf(cand, "%lf %lf", &lat, &lon) == 2) { - break; + break; } } } @@ -224,16 +254,33 @@ 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 ); + trw_layer_calculate_bounds_waypoints ( VIK_TRW_LAYER(sel) ); + vik_layer_emit_update ( VIK_LAYER(sel) ); } 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); } } 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); } @@ -242,21 +289,21 @@ 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; 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. */ - if (!(s = g_convert ( 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; } - // 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 @@ -265,14 +312,14 @@ static void clip_receive_html ( GtkClipboard *c, GtkSelectionData *sd, gpointer span = s; } for (i=0; i0) { span[i] = ' '; } - if (c == '>') { + if (ch == '>') { if (tag>0) tag--; } } @@ -283,29 +330,37 @@ static void clip_receive_html ( GtkClipboard *c, GtkSelectionData *sd, gpointer 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; - /* g_print("got targets\n"); */ for (i=0; ivt, &iter ); + if ( !vik_treeview_get_selected_iter ( sel->vt, &iter ) ) + return; + layer_type = sel->type; + + // 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 { + 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) +{ + vik_clipboard_t * vc = g_malloc(sizeof(*vc) + len); + GtkClipboard *c = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD ); - 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; - else - return; /* selected sublayer is uncopyable */ + vc->type = type; + vc->layer_type = layer_type; + vc->subtype = subtype; + vc->len = len; + vc->text = g_strdup (text); + if ( data ) { + memcpy(vc->data, data, len); + g_free(data); } + vc->pid = getpid(); + + // Simple clipboard copy when necessary + if ( type == VIK_CLIPBOARD_DATA_TEXT ) + gtk_clipboard_set_text ( c, text, -1 ); else - { - vc->clipboard = sel; - g_object_ref ( G_OBJECT(sel) ); - vc->type = DATA_LAYER; - } - gtk_clipboard_set_with_data ( c, target_table, G_N_ELEMENTS(target_table), clip_get, clip_clear, vc ); + 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 ) { @@ -357,3 +456,68 @@ gboolean a_clipboard_paste ( VikLayersPanel *vlp ) 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