]> git.street.me.uk Git - andy/viking.git/blame - src/clipboard.c
[DOC] Improve reference documentation for babel.c
[andy/viking.git] / src / clipboard.c
CommitLineData
50a14534
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
a482007a 5 * Copyright (C) 2005, Alex Foobarian <foobarian@gmail.com>
50a14534
EB
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
45acf79e
MA
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
4df1abf8 27#include <string.h>
2f50c650 28#include <stdlib.h>
45acf79e
MA
29#ifdef HAVE_SYS_TYPES_H
30#include <sys/types.h>
31#endif
32#ifdef HAVE_UNISTD_H
33#include <unistd.h>
34#endif
4c77d5e0
GB
35
36#include <glib/gi18n.h>
37
50a14534
EB
38#include "viking.h"
39
2f50c650 40
4df1abf8
EB
41typedef struct {
42 gpointer clipboard;
c434d571 43 gint pid;
2cebc318 44 VikClipboardDataType type;
4df1abf8
EB
45 gint subtype;
46 guint16 layer_type;
c434d571
AF
47 guint len;
48 guint8 data[0];
4df1abf8 49} vik_clipboard_t;
50a14534 50
4df1abf8
EB
51static GtkTargetEntry target_table[] = {
52 { "application/viking", 0, 0 },
53 { "STRING", 0, 1 },
54};
50a14534 55
2f50c650
AF
56/*****************************************************************
57 ** functions which send to the clipboard client (we are owner) **
58 *****************************************************************/
50a14534 59
4df1abf8
EB
60static void clip_get ( GtkClipboard *c, GtkSelectionData *selection_data, guint info, gpointer p )
61{
62 vik_clipboard_t *vc = p;
4df1abf8 63 if (info==0) {
0a6cab71 64 // g_print("clip_get: vc = %p, size = %d\n", vc, sizeof(*vc) + vc->len);
c434d571 65 gtk_selection_data_set ( selection_data, selection_data->target, 8, (void *)vc, sizeof(*vc) + vc->len );
50a14534 66 }
50a14534
EB
67}
68
4df1abf8 69static void clip_clear ( GtkClipboard *c, gpointer p )
50a14534 70{
ddc47a46 71 g_free(p);
4df1abf8
EB
72}
73
74
2f50c650
AF
75/**************************************************************************
76 ** functions which receive from the clipboard owner (we are the client) **
77 **************************************************************************/
4df1abf8
EB
78
79/* our own data type */
80static void clip_receive_viking ( GtkClipboard *c, GtkSelectionData *sd, gpointer p )
81{
82 VikLayersPanel *vlp = p;
83 vik_clipboard_t *vc;
84 if (sd->length == -1) {
4c77d5e0 85 g_warning ( _("paste failed") );
4df1abf8
EB
86 return;
87 }
2f50c650 88 // g_print("clip receive: target = %s, type = %s\n", gdk_atom_name(sd->target), gdk_atom_name(sd->type));
4df1abf8 89 g_assert(!strcmp(gdk_atom_name(sd->target), target_table[0].target));
4df1abf8
EB
90
91 vc = (vik_clipboard_t *)sd->data;
0a6cab71
AF
92 // g_print(" sd->data = %p, sd->length = %d, vc->len = %d\n", sd->data, sd->length, vc->len);
93
94 if (sd->length != sizeof(*vc) + vc->len) {
4c77d5e0 95 g_warning ( _("wrong clipboard data size") );
0a6cab71
AF
96 return;
97 }
4df1abf8 98
2cebc318 99 if ( vc->type == VIK_CLIPBOARD_DATA_LAYER )
50a14534 100 {
c434d571
AF
101 VikLayer *new_layer = vik_layer_unmarshall ( vc->data, vc->len, vik_layers_panel_get_viewport(vlp) );
102 vik_layers_panel_add_layer ( vlp, new_layer );
50a14534 103 }
2cebc318 104 else if ( vc->type == VIK_CLIPBOARD_DATA_SUBLAYER )
50a14534
EB
105 {
106 VikLayer *sel = vik_layers_panel_get_selected ( vlp );
4df1abf8 107 if ( sel && sel->type == vc->layer_type)
50a14534 108 {
4df1abf8 109 if ( vik_layer_get_interface(vc->layer_type)->paste_item )
ddc47a46 110 vik_layer_get_interface(vc->layer_type)->paste_item ( sel, vc->subtype, vc->data, vc->len);
50a14534
EB
111 }
112 else
4c77d5e0 113 a_dialog_error_msg_extra ( VIK_GTK_WINDOW_FROM_WIDGET(GTK_WIDGET(vlp)),
f5f53833 114 _("The clipboard contains sublayer data for %s layers. "
4c77d5e0
GB
115 "You must select a layer of this type to paste the clipboard data."),
116 vik_layer_get_interface(vc->layer_type)->name );
4df1abf8
EB
117 }
118}
119
120
2f50c650 121
317cbf8a
GB
122/**
123 * clip_parse_latlon:
124 * @text: text containing LatLon data.
125 * @coord: computed coordinates.
126 *
127 * Utility func to handle pasted text:
128 * search for N dd.dddddd W dd.dddddd, N dd° dd.dddd W dd° dd.ddddd and so forth.
129 *
130 * Returns: TRUE if coordinates are set.
4df1abf8 131 */
2f50c650
AF
132static gboolean clip_parse_latlon ( const gchar *text, struct LatLon *coord )
133{
134 gint latdeg, londeg;
135 gdouble latm, lonm;
136 gdouble lat, lon;
137 gchar *cand;
138 gint len, i;
139 gchar *s = g_strdup(text);
140
141 // g_print("parsing %s\n", s);
142
143 len = strlen(s);
144
145 /* remove non-digits following digits; gets rid of degree symbols or whatever people use, and
146 * punctuation
147 */
148 for (i=0; i<len-2; i++) {
149 if (g_ascii_isdigit (s[i]) && s[i+1] != '.' && !g_ascii_isdigit (s[i+1])) {
150 s[i+1] = ' ';
e0fca367 151 if (!g_ascii_isalnum (s[i+2]) && s[i+2] != '-') {
2f50c650
AF
152 s[i+2] = ' ';
153 }
154 }
155 }
156
157 /* now try reading coordinates */
158 for (i=0; i<len; i++) {
159 if (s[i] == 'N' || s[i] == 'S' || g_ascii_isdigit (s[i])) {
160 gchar latc[2] = "SN";
161 gchar lonc[2] = "WE";
162 gint j, k;
163 cand = s+i;
164
2f50c650
AF
165 for (j=0; j<2; j++) {
166 for (k=0; k<2; k++) {
167 gchar fmt1[] = "N %d%*[ ]%lf W %d%*[ ]%lf";
168 gchar fmt2[] = "%d%*[ ]%lf N %d%*[ ]%lf W";
169 gchar fmt3[] = "N %lf W %lf";
170 gchar fmt4[] = "%lf N %lf W";
171
172 fmt1[0] = latc[j]; fmt1[13] = lonc[k];
173 fmt2[11] = latc[j]; fmt2[24] = lonc[k];
174 fmt3[0] = latc[j]; fmt3[6] = lonc[k];
175 fmt4[4] = latc[j]; fmt4[10] = lonc[k];
176
177 if (sscanf(cand, fmt1, &latdeg, &latm, &londeg, &lonm) == 4 ||
178 sscanf(cand, fmt2, &latdeg, &latm, &londeg, &lonm) == 4) {
179 lat = (j*2-1) * (latdeg + latm / 60);
180 lon = (k*2-1) * (londeg + lonm / 60);
181 break;
182 }
183 if (sscanf(cand, fmt3, &lat, &lon) == 2 ||
184 sscanf(cand, fmt4, &lat, &lon) == 2) {
185 lat *= (j*2-1);
186 lon *= (k*2-1);
187 break;
188 }
189 }
190 if (k!=2) break;
191 }
192 if (j!=2) break;
193
194 if (sscanf(cand, "%d%*[ ]%lf %d%*[ ]%lf", &latdeg, &latm, &londeg, &lonm) == 4) {
195 lat = latdeg/abs(latdeg) * (abs(latdeg) + latm / 60);
196 lon = londeg/abs(londeg) * (abs(londeg) + lonm / 60);
197 break;
198 }
199 if (sscanf(cand, "%lf %lf", &lat, &lon) == 2) {
200 break;
201 }
202 }
203 }
204 g_free(s);
205
7680e9fe
AF
206 /* did we get to the end without actually finding a coordinate? */
207 if (i<len && lat >= -90.0 && lat <= 90.0 && lon >= -180.0 && lon <= 180.0) {
2f50c650
AF
208 coord->lat = lat;
209 coord->lon = lon;
210 return TRUE;
2f50c650 211 }
7680e9fe 212 return FALSE;
2f50c650
AF
213}
214
215static void clip_add_wp(VikLayersPanel *vlp, struct LatLon *coord)
216{
217 VikCoord vc;
218 VikLayer *sel = vik_layers_panel_get_selected ( vlp );
219
220
221 vik_coord_load_from_latlon ( &vc, VIK_COORD_LATLON, coord );
222
223 if (sel && sel->type == VIK_LAYER_TRW) {
224 vik_trw_layer_new_waypoint ( VIK_TRW_LAYER(sel), VIK_GTK_WINDOW_FROM_LAYER(sel), &vc );
225 } else {
4c77d5e0 226 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);
2f50c650
AF
227 }
228}
229
4df1abf8
EB
230static void clip_receive_text (GtkClipboard *c, const gchar *text, gpointer p)
231{
232 VikLayersPanel *vlp = p;
2f50c650 233 struct LatLon coord;
e0fca367 234 // g_print("got text: %s\n", text);
2f50c650
AF
235 if (clip_parse_latlon(text, &coord)) {
236 clip_add_wp(vlp, &coord);
237 }
4df1abf8
EB
238}
239
4df1abf8
EB
240static void clip_receive_html ( GtkClipboard *c, GtkSelectionData *sd, gpointer p )
241{
242 VikLayersPanel *vlp = p;
00bb93da 243 gsize r, w;
4df1abf8 244 GError *err = NULL;
2f50c650
AF
245 gchar *s, *span;
246 gint tag = 0, i;
247 struct LatLon coord;
4df1abf8
EB
248
249 if (sd->length == -1) {
250 return;
251 }
252
2f50c650 253 /* - copying from Mozilla seems to give html in UTF-16. */
ddc47a46 254 if (!(s = g_convert ( (gchar *)sd->data, sd->length, "UTF-8", "UTF-16", &r, &w, &err))) {
4df1abf8
EB
255 return;
256 }
2f50c650
AF
257 // g_print("html is %d bytes long: %s\n", sd->length, s);
258
259 /* scrape a coordinate pasted from a geocaching.com page: look for a
260 * telltale tag if possible, and then remove tags
261 */
262 if (!(span = g_strstr_len(s, w, "<span id=\"LatLon\">"))) {
263 span = s;
264 }
265 for (i=0; i<strlen(span); i++) {
266 gchar c = span[i];
267 if (c == '<') {
268 tag++;
269 }
270 if (tag>0) {
271 span[i] = ' ';
272 }
273 if (c == '>') {
274 if (tag>0) tag--;
275 }
276 }
277 if (clip_parse_latlon(span, &coord)) {
278 clip_add_wp(vlp, &coord);
279 }
280
4df1abf8
EB
281 g_free(s);
282}
283
317cbf8a
GB
284/**
285 * clip_receive_targets:
286 *
287 * Deal with various data types a clipboard may hold.
4df1abf8
EB
288 */
289void clip_receive_targets ( GtkClipboard *c, GdkAtom *a, gint n, gpointer p )
290{
291 VikLayersPanel *vlp = p;
292 gint i;
293
2f50c650 294 /* g_print("got targets\n"); */
4df1abf8 295 for (i=0; i<n; i++) {
2f50c650 296 /* g_print(" ""%s""\n", gdk_atom_name(a[i])); */
4df1abf8
EB
297 if (!strcmp(gdk_atom_name(a[i]), "text/html")) {
298 gtk_clipboard_request_contents ( c, gdk_atom_intern("text/html", TRUE), clip_receive_html, vlp );
299 break;
300 }
301 if (a[i] == GDK_TARGET_STRING) {
302 gtk_clipboard_request_text ( c, clip_receive_text, vlp );
303 break;
304 }
305 if (!strcmp(gdk_atom_name(a[i]), "application/viking")) {
306 gtk_clipboard_request_contents ( c, gdk_atom_intern("application/viking", TRUE), clip_receive_viking, vlp );
307 break;
308 }
50a14534 309 }
4df1abf8
EB
310}
311
312/*********************************************************************************
313 ** public functions **
314 *********************************************************************************/
315
317cbf8a
GB
316/**
317 * a_clipboard_copy_selected:
318 *
319 * Make a copy of selected object and associate ourselves with the clipboard.
4df1abf8 320 */
2cebc318 321void a_clipboard_copy_selected ( VikLayersPanel *vlp )
4df1abf8
EB
322{
323 VikLayer *sel = vik_layers_panel_get_selected ( vlp );
324 GtkTreeIter iter;
2cebc318
QT
325 VikClipboardDataType type;
326 guint16 layer_type = 0;
327 gint subtype = 0;
ddc47a46 328 guint8 *data = NULL;
c434d571 329 guint len;
4df1abf8
EB
330
331 if ( ! sel )
332 return;
333
334 vik_treeview_get_selected_iter ( sel->vt, &iter );
f5f53833 335 layer_type = sel->type;
4df1abf8 336
ddc47a46 337 if ( vik_treeview_item_get_type ( sel->vt, &iter ) == VIK_TREEVIEW_TYPE_SUBLAYER ) {
2cebc318 338 type = VIK_CLIPBOARD_DATA_SUBLAYER;
2cebc318
QT
339 if ( vik_layer_get_interface(layer_type)->copy_item) {
340 subtype = vik_treeview_item_get_data(sel->vt, &iter);
341 vik_layer_get_interface(layer_type)->copy_item(sel, subtype, vik_treeview_item_get_pointer(sel->vt, &iter), &data, &len );
c434d571 342 }
4df1abf8
EB
343 }
344 else
345 {
494eb388 346 gint ilen;
2cebc318 347 type = VIK_CLIPBOARD_DATA_LAYER;
494eb388
QT
348 vik_layer_marshall ( sel, &data, &ilen );
349 len = ilen;
4df1abf8 350 }
ddc47a46 351
2cebc318
QT
352 if (data)
353 a_clipboard_copy( type, layer_type, subtype, len, data);
ddc47a46 354
2cebc318 355}
ddc47a46 356
2cebc318
QT
357void a_clipboard_copy( VikClipboardDataType type, guint16 layer_type, gint subtype, guint len, guint8 * data)
358{
359 vik_clipboard_t * vc = g_malloc(sizeof(*vc) + len);
360 GtkClipboard *c = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD );
ddc47a46 361
2cebc318
QT
362 vc->type = type;
363 vc->layer_type = layer_type;
364 vc->subtype = subtype;
365 vc->len = len;
366 memcpy(vc->data, data, len);
367 g_free(data);
368 vc->pid = getpid();
369 gtk_clipboard_set_with_data ( c, target_table, G_N_ELEMENTS(target_table), clip_get, clip_clear, vc );
4df1abf8
EB
370}
371
317cbf8a
GB
372/**
373 * a_clipboard_paste:
374 *
375 * To deal with multiple data types, we first request the type of data on the clipboard,
376 * and handle them in the callback.
4df1abf8
EB
377 */
378gboolean a_clipboard_paste ( VikLayersPanel *vlp )
379{
380 GtkClipboard *c = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD );
381 gtk_clipboard_request_targets ( c, clip_receive_targets, vlp );
382 return TRUE;
50a14534 383}
2f50c650 384