]> git.street.me.uk Git - andy/viking.git/blob - src/clipboard.c
Alex Foobarian's track profile patch
[andy/viking.git] / src / clipboard.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "viking.h"
23
24 /* static for a very good reason -- share between windows */
25
26 #define DATA_NONE 0
27 #define DATA_LAYER 1
28 #define DATA_SUBLAYER 2
29
30 guint8 type = DATA_NONE;
31 gint subtype;
32 guint16 layer_type;
33 gpointer clipboard;
34
35 void a_clipboard_copy ( VikLayersPanel *vlp )
36 {
37   VikLayer *sel = vik_layers_panel_get_selected ( vlp );
38   GtkTreeIter iter;
39   if ( ! sel )
40     return;
41
42   vik_treeview_get_selected_iter ( sel->vt, &iter );
43
44   if ( clipboard )
45     a_clipboard_uninit();
46
47   if ( vik_treeview_item_get_type ( sel->vt, &iter ) == VIK_TREEVIEW_TYPE_SUBLAYER )
48   {
49     layer_type = sel->type;
50     if ( vik_layer_get_interface(layer_type)->copy_item && (clipboard = vik_layer_get_interface(layer_type)->
51         copy_item(sel,subtype=vik_treeview_item_get_data(sel->vt,&iter),vik_treeview_item_get_pointer(sel->vt,&iter)) ))
52       type = DATA_SUBLAYER;
53   }
54   else
55   {
56     clipboard = sel;
57     g_object_ref ( G_OBJECT(sel) );
58     type = DATA_LAYER;
59   }
60 }
61
62 gboolean a_clipboard_paste ( VikLayersPanel *vlp )
63 {
64   if ( clipboard && type == DATA_LAYER )
65   {
66     /* oooh, _private_ ... so sue me, there's no other way to do this. */
67     if ( G_OBJECT(clipboard)->ref_count == 1 )
68     { /* optimization -- if layer has been deleted, don't copy the layer. */
69       g_object_ref ( G_OBJECT(clipboard) );
70       vik_layers_panel_add_layer ( vlp, VIK_LAYER(clipboard) );
71       return TRUE;
72     }
73     else
74     {
75       VikLayer *new_layer = vik_layer_copy ( VIK_LAYER(clipboard), vik_layers_panel_get_viewport(vlp) );
76       if ( new_layer )
77       {
78         vik_layers_panel_add_layer ( vlp, new_layer );
79         return TRUE;
80       }
81     }
82   }
83   else if ( clipboard && type == DATA_SUBLAYER )
84   {
85     VikLayer *sel = vik_layers_panel_get_selected ( vlp );
86     if ( sel && sel->type == layer_type)
87     {
88       if ( vik_layer_get_interface(layer_type)->paste_item )
89         return vik_layer_get_interface(layer_type)->paste_item ( sel, subtype, clipboard );
90     }
91     else
92       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(layer_type)->name );
93     return FALSE;
94   }
95   return FALSE;
96 }
97
98 void a_clipboard_uninit ()
99 {
100   if ( clipboard && type == DATA_LAYER )
101     g_object_unref ( G_OBJECT(clipboard) );
102   else if ( clipboard && type == DATA_SUBLAYER )
103     if ( vik_layer_get_interface(layer_type)->free_copied_item )
104       vik_layer_get_interface(layer_type)->free_copied_item(subtype,clipboard);
105   clipboard = NULL;
106   type = DATA_NONE;
107 }