1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) James Liggett 2010 <jrliggett@cox.net>
6 * anjuta is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * anjuta is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "anjuta-file-drop-entry.h"
30 static GtkTargetEntry dnd_target_entries
[] =
39 struct _AnjutaFileDropEntryPriv
44 G_DEFINE_TYPE (AnjutaFileDropEntry
, anjuta_file_drop_entry
, ANJUTA_TYPE_DROP_ENTRY
);
47 anjuta_file_drop_entry_init (AnjutaFileDropEntry
*self
)
49 self
->priv
= g_new0 (AnjutaFileDropEntryPriv
, 1);
51 gtk_drag_dest_set (GTK_WIDGET (self
),
52 GTK_DEST_DEFAULT_MOTION
| GTK_DEST_DEFAULT_HIGHLIGHT
,
54 G_N_ELEMENTS (dnd_target_entries
),
55 GDK_ACTION_COPY
| GDK_ACTION_MOVE
);
59 anjuta_file_drop_entry_finalize (GObject
*object
)
61 AnjutaFileDropEntry
*self
;
63 self
= ANJUTA_FILE_DROP_ENTRY (object
);
65 g_free (self
->priv
->relative_path
);
68 G_OBJECT_CLASS (anjuta_file_drop_entry_parent_class
)->finalize (object
);
72 anjuta_file_drop_entry_set_property (GObject
*object
, guint prop_id
,
73 const GValue
*value
, GParamSpec
*pspec
)
75 AnjutaFileDropEntry
*self
;
77 g_return_if_fail (ANJUTA_IS_FILE_DROP_ENTRY (object
));
79 self
= ANJUTA_FILE_DROP_ENTRY (object
);
83 case PROP_RELATIVE_PATH
:
84 g_free (self
->priv
->relative_path
);
86 self
->priv
->relative_path
= g_value_dup_string (value
);
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
95 anjuta_file_drop_entry_get_property (GObject
*object
, guint prop_id
,
96 GValue
*value
, GParamSpec
*pspec
)
98 AnjutaFileDropEntry
*self
;
100 g_return_if_fail (ANJUTA_IS_FILE_DROP_ENTRY (object
));
102 self
= ANJUTA_FILE_DROP_ENTRY (object
);
106 case PROP_RELATIVE_PATH
:
107 g_value_set_string (value
, self
->priv
->relative_path
);
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
116 anjuta_file_drop_entry_drag_data_received (GtkWidget
*widget
,
117 GdkDragContext
*context
, gint x
, gint y
,
118 GtkSelectionData
*data
, guint target_type
,
121 AnjutaFileDropEntry
*self
;
128 self
= ANJUTA_FILE_DROP_ENTRY (widget
);
131 if ((data
!= NULL
) &&
132 (gtk_selection_data_get_length (data
) >= 0))
134 if (target_type
== 0)
136 uri_list
= gtk_selection_data_get_uris (data
);
139 if (self
->priv
->relative_path
)
140 parent_file
= g_file_new_for_path (self
->priv
->relative_path
);
142 /* Take only the first file */
143 file
= g_file_new_for_uri (uri_list
[0]);
147 path
= g_file_get_relative_path (parent_file
, file
);
149 g_object_unref (parent_file
);
152 path
= g_file_get_path (file
);
156 anjuta_entry_set_text (ANJUTA_ENTRY (self
), path
);
163 g_object_unref (file
);
164 g_strfreev (uri_list
);
168 /* Do not delete source data */
169 gtk_drag_finish (context
, success
, FALSE
, time
);
173 anjuta_file_drop_entry_drag_drop (GtkWidget
*widget
, GdkDragContext
*context
,
174 gint x
, gint y
, guint time
)
178 target_type
= gtk_drag_dest_find_target (widget
, context
, NULL
);
180 g_print ("Drag drop target type: %p\n", GDK_ATOM_TO_POINTER (target_type
));
182 if (target_type
!= GDK_NONE
)
183 gtk_drag_get_data (widget
, context
, target_type
, time
);
185 gtk_drag_finish (context
, FALSE
, FALSE
, time
);
191 anjuta_file_drop_entry_class_init (AnjutaFileDropEntryClass
*klass
)
193 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
194 GtkWidgetClass
*widget_class
= GTK_WIDGET_CLASS (klass
);
196 object_class
->finalize
= anjuta_file_drop_entry_finalize
;
197 object_class
->set_property
= anjuta_file_drop_entry_set_property
;
198 object_class
->get_property
= anjuta_file_drop_entry_get_property
;
199 widget_class
->drag_data_received
= anjuta_file_drop_entry_drag_data_received
;
200 widget_class
->drag_drop
= anjuta_file_drop_entry_drag_drop
;
202 g_object_class_install_property (object_class
,
204 g_param_spec_string ("relative-path",
206 _("Path that dropped files should be relative to"),
212 anjuta_file_drop_entry_new (void)
214 return g_object_new (ANJUTA_TYPE_FILE_DROP_ENTRY
, NULL
);
218 anjuta_file_drop_entry_set_relative_path (AnjutaFileDropEntry
*self
,
221 g_object_set (G_OBJECT (self
), "relative-path", path
, NULL
);