Solving a regression
[viking.git] / src / clipboard.c
blobdc98a5883b47229711527738d0e11c13ef9f3c17
1 /*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include "viking.h"
28 typedef struct {
29 gpointer clipboard;
30 gint pid;
31 VikClipboardDataType type;
32 gint subtype;
33 guint16 layer_type;
34 guint len;
35 guint8 data[0];
36 } vik_clipboard_t;
38 static GtkTargetEntry target_table[] = {
39 { "application/viking", 0, 0 },
40 { "STRING", 0, 1 },
43 /*****************************************************************
44 ** functions which send to the clipboard client (we are owner) **
45 *****************************************************************/
47 static void clip_get ( GtkClipboard *c, GtkSelectionData *selection_data, guint info, gpointer p )
49 vik_clipboard_t *vc = p;
50 if (info==0) {
51 // g_print("clip_get: vc = %p, size = %d\n", vc, sizeof(*vc) + vc->len);
52 gtk_selection_data_set ( selection_data, selection_data->target, 8, (void *)vc, sizeof(*vc) + vc->len );
54 if (info==1) {
55 if (vc->type == VIK_CLIPBOARD_DATA_LAYER) {
56 gtk_selection_data_set_text ( selection_data, VIK_LAYER(vc->clipboard)->name, -1 );
61 static void clip_clear ( GtkClipboard *c, gpointer p )
63 g_free(p);
67 /**************************************************************************
68 ** functions which receive from the clipboard owner (we are the client) **
69 **************************************************************************/
71 /* our own data type */
72 static void clip_receive_viking ( GtkClipboard *c, GtkSelectionData *sd, gpointer p )
74 VikLayersPanel *vlp = p;
75 vik_clipboard_t *vc;
76 if (sd->length == -1) {
77 g_warning ( "paste failed" );
78 return;
80 // g_print("clip receive: target = %s, type = %s\n", gdk_atom_name(sd->target), gdk_atom_name(sd->type));
81 g_assert(!strcmp(gdk_atom_name(sd->target), target_table[0].target));
83 vc = (vik_clipboard_t *)sd->data;
84 // g_print(" sd->data = %p, sd->length = %d, vc->len = %d\n", sd->data, sd->length, vc->len);
86 if (sd->length != sizeof(*vc) + vc->len) {
87 g_warning ( "wrong clipboard data size" );
88 return;
91 if ( vc->type == VIK_CLIPBOARD_DATA_LAYER )
93 VikLayer *new_layer = vik_layer_unmarshall ( vc->data, vc->len, vik_layers_panel_get_viewport(vlp) );
94 vik_layers_panel_add_layer ( vlp, new_layer );
96 else if ( vc->type == VIK_CLIPBOARD_DATA_SUBLAYER )
98 VikLayer *sel = vik_layers_panel_get_selected ( vlp );
99 if ( sel && sel->type == vc->layer_type)
101 if ( vik_layer_get_interface(vc->layer_type)->paste_item )
102 vik_layer_get_interface(vc->layer_type)->paste_item ( sel, vc->subtype, vc->data, vc->len);
104 else
105 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 );
112 * utility func to handle pasted text:
113 * search for N dd.dddddd W dd.dddddd, N dd° dd.dddd W dd° dd.ddddd and so forth
115 static gboolean clip_parse_latlon ( const gchar *text, struct LatLon *coord )
117 gint latdeg, londeg;
118 gdouble latm, lonm;
119 gdouble lat, lon;
120 gchar *cand;
121 gint len, i;
122 gchar *s = g_strdup(text);
124 // g_print("parsing %s\n", s);
126 len = strlen(s);
128 /* remove non-digits following digits; gets rid of degree symbols or whatever people use, and
129 * punctuation
131 for (i=0; i<len-2; i++) {
132 if (g_ascii_isdigit (s[i]) && s[i+1] != '.' && !g_ascii_isdigit (s[i+1])) {
133 s[i+1] = ' ';
134 if (!g_ascii_isalnum (s[i+2]) && s[i+2] != '-') {
135 s[i+2] = ' ';
140 /* now try reading coordinates */
141 for (i=0; i<len; i++) {
142 if (s[i] == 'N' || s[i] == 'S' || g_ascii_isdigit (s[i])) {
143 gchar latc[2] = "SN";
144 gchar lonc[2] = "WE";
145 gint j, k;
146 cand = s+i;
148 for (j=0; j<2; j++) {
149 for (k=0; k<2; k++) {
150 gchar fmt1[] = "N %d%*[ ]%lf W %d%*[ ]%lf";
151 gchar fmt2[] = "%d%*[ ]%lf N %d%*[ ]%lf W";
152 gchar fmt3[] = "N %lf W %lf";
153 gchar fmt4[] = "%lf N %lf W";
155 fmt1[0] = latc[j]; fmt1[13] = lonc[k];
156 fmt2[11] = latc[j]; fmt2[24] = lonc[k];
157 fmt3[0] = latc[j]; fmt3[6] = lonc[k];
158 fmt4[4] = latc[j]; fmt4[10] = lonc[k];
160 if (sscanf(cand, fmt1, &latdeg, &latm, &londeg, &lonm) == 4 ||
161 sscanf(cand, fmt2, &latdeg, &latm, &londeg, &lonm) == 4) {
162 lat = (j*2-1) * (latdeg + latm / 60);
163 lon = (k*2-1) * (londeg + lonm / 60);
164 break;
166 if (sscanf(cand, fmt3, &lat, &lon) == 2 ||
167 sscanf(cand, fmt4, &lat, &lon) == 2) {
168 lat *= (j*2-1);
169 lon *= (k*2-1);
170 break;
173 if (k!=2) break;
175 if (j!=2) break;
177 if (sscanf(cand, "%d%*[ ]%lf %d%*[ ]%lf", &latdeg, &latm, &londeg, &lonm) == 4) {
178 lat = latdeg/abs(latdeg) * (abs(latdeg) + latm / 60);
179 lon = londeg/abs(londeg) * (abs(londeg) + lonm / 60);
180 break;
182 if (sscanf(cand, "%lf %lf", &lat, &lon) == 2) {
183 break;
187 g_free(s);
189 /* did we get to the end without actually finding a coordinate? */
190 if (i<len && lat >= -90.0 && lat <= 90.0 && lon >= -180.0 && lon <= 180.0) {
191 coord->lat = lat;
192 coord->lon = lon;
193 return TRUE;
195 return FALSE;
198 static void clip_add_wp(VikLayersPanel *vlp, struct LatLon *coord)
200 VikCoord vc;
201 VikLayer *sel = vik_layers_panel_get_selected ( vlp );
204 vik_coord_load_from_latlon ( &vc, VIK_COORD_LATLON, coord );
206 if (sel && sel->type == VIK_LAYER_TRW) {
207 vik_trw_layer_new_waypoint ( VIK_TRW_LAYER(sel), VIK_GTK_WINDOW_FROM_LAYER(sel), &vc );
208 } else {
209 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);
213 static void clip_receive_text (GtkClipboard *c, const gchar *text, gpointer p)
215 VikLayersPanel *vlp = p;
216 struct LatLon coord;
217 // g_print("got text: %s\n", text);
218 if (clip_parse_latlon(text, &coord)) {
219 clip_add_wp(vlp, &coord);
223 static void clip_receive_html ( GtkClipboard *c, GtkSelectionData *sd, gpointer p )
225 VikLayersPanel *vlp = p;
226 gsize r, w;
227 GError *err = NULL;
228 gchar *s, *span;
229 gint tag = 0, i;
230 struct LatLon coord;
232 if (sd->length == -1) {
233 return;
236 /* - copying from Mozilla seems to give html in UTF-16. */
237 if (!(s = g_convert ( (gchar *)sd->data, sd->length, "UTF-8", "UTF-16", &r, &w, &err))) {
238 return;
240 // g_print("html is %d bytes long: %s\n", sd->length, s);
242 /* scrape a coordinate pasted from a geocaching.com page: look for a
243 * telltale tag if possible, and then remove tags
245 if (!(span = g_strstr_len(s, w, "<span id=\"LatLon\">"))) {
246 span = s;
248 for (i=0; i<strlen(span); i++) {
249 gchar c = span[i];
250 if (c == '<') {
251 tag++;
253 if (tag>0) {
254 span[i] = ' ';
256 if (c == '>') {
257 if (tag>0) tag--;
260 if (clip_parse_latlon(span, &coord)) {
261 clip_add_wp(vlp, &coord);
264 g_free(s);
268 * deal with various data types a clipboard may hold
270 void clip_receive_targets ( GtkClipboard *c, GdkAtom *a, gint n, gpointer p )
272 VikLayersPanel *vlp = p;
273 gint i;
275 /* g_print("got targets\n"); */
276 for (i=0; i<n; i++) {
277 /* g_print(" ""%s""\n", gdk_atom_name(a[i])); */
278 if (!strcmp(gdk_atom_name(a[i]), "text/html")) {
279 gtk_clipboard_request_contents ( c, gdk_atom_intern("text/html", TRUE), clip_receive_html, vlp );
280 break;
282 if (a[i] == GDK_TARGET_STRING) {
283 gtk_clipboard_request_text ( c, clip_receive_text, vlp );
284 break;
286 if (!strcmp(gdk_atom_name(a[i]), "application/viking")) {
287 gtk_clipboard_request_contents ( c, gdk_atom_intern("application/viking", TRUE), clip_receive_viking, vlp );
288 break;
293 /*********************************************************************************
294 ** public functions **
295 *********************************************************************************/
298 * make a copy of selected object and associate ourselves with the clipboard
300 void a_clipboard_copy_selected ( VikLayersPanel *vlp )
302 VikLayer *sel = vik_layers_panel_get_selected ( vlp );
303 GtkTreeIter iter;
304 VikClipboardDataType type;
305 guint16 layer_type = 0;
306 gint subtype = 0;
307 guint8 *data = NULL;
308 guint len;
310 if ( ! sel )
311 return;
313 vik_treeview_get_selected_iter ( sel->vt, &iter );
315 if ( vik_treeview_item_get_type ( sel->vt, &iter ) == VIK_TREEVIEW_TYPE_SUBLAYER ) {
316 type = VIK_CLIPBOARD_DATA_SUBLAYER;
317 layer_type = sel->type;
318 if ( vik_layer_get_interface(layer_type)->copy_item) {
319 subtype = vik_treeview_item_get_data(sel->vt, &iter);
320 vik_layer_get_interface(layer_type)->copy_item(sel, subtype, vik_treeview_item_get_pointer(sel->vt, &iter), &data, &len );
323 else
325 type = VIK_CLIPBOARD_DATA_LAYER;
326 vik_layer_marshall ( sel, &data, &len );
329 if (data)
330 a_clipboard_copy( type, layer_type, subtype, len, data);
334 void a_clipboard_copy( VikClipboardDataType type, guint16 layer_type, gint subtype, guint len, guint8 * data)
336 vik_clipboard_t * vc = g_malloc(sizeof(*vc) + len);
337 GtkClipboard *c = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD );
339 vc->type = type;
340 vc->layer_type = layer_type;
341 vc->subtype = subtype;
342 vc->len = len;
343 memcpy(vc->data, data, len);
344 g_free(data);
345 vc->pid = getpid();
346 gtk_clipboard_set_with_data ( c, target_table, G_N_ELEMENTS(target_table), clip_get, clip_clear, vc );
350 * to deal with multiple data types, we first request the type of data on the clipboard,
351 * and handle them in the callback
353 gboolean a_clipboard_paste ( VikLayersPanel *vlp )
355 GtkClipboard *c = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD );
356 gtk_clipboard_request_targets ( c, clip_receive_targets, vlp );
357 return TRUE;