1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * anjuta
4  * Copyright (C) James Liggett 2010 <jrliggett@cox.net>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 
20 #include "anjuta-drop-entry.h"
21 
22 /* DnD targets */
23 enum
24 {
25 	DND_TYPE_STRING
26 };
27 
28 static GtkTargetEntry dnd_target_entries[] =
29 {
30 	{
31 		"STRING",
32 		0,
33 		DND_TYPE_STRING
34 	},
35 	{
36 		"text/plain",
37 		0,
38 		DND_TYPE_STRING
39 	}
40 };
41 
42 G_DEFINE_TYPE (AnjutaDropEntry, anjuta_drop_entry, ANJUTA_TYPE_ENTRY);
43 
44 static void
anjuta_drop_entry_init(AnjutaDropEntry * self)45 anjuta_drop_entry_init (AnjutaDropEntry *self)
46 {
47 	gtk_drag_dest_set (GTK_WIDGET (self),
48 	                   GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT,
49 	                   dnd_target_entries,
50 	                   G_N_ELEMENTS (dnd_target_entries), GDK_ACTION_COPY);
51 }
52 
53 static void
anjuta_drop_entry_finalize(GObject * object)54 anjuta_drop_entry_finalize (GObject *object)
55 {
56 	G_OBJECT_CLASS (anjuta_drop_entry_parent_class)->finalize (object);
57 }
58 
59 static void
anjuta_drop_entry_drag_data_received(GtkWidget * widget,GdkDragContext * context,gint x,gint y,GtkSelectionData * data,guint target_type,guint time)60 anjuta_drop_entry_drag_data_received (GtkWidget *widget,
61                                       GdkDragContext *context, gint x, gint y,
62                                       GtkSelectionData *data, guint target_type,
63                                       guint time)
64 {
65 	gboolean success;
66 	gboolean delete;
67 
68 	success = FALSE;
69 	delete = FALSE;
70 
71 	if ((data != NULL) &&
72 	    (gtk_selection_data_get_length (data) >= 0))
73 	{
74 		delete = (gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE);
75 
76 		if (target_type == DND_TYPE_STRING)
77 		{
78 			anjuta_entry_set_text (ANJUTA_ENTRY (widget),
79 			                       (const gchar *) gtk_selection_data_get_data (data));
80 			success = TRUE;
81 		}
82 	}
83 
84 	gtk_drag_finish (context, success, delete, time);
85 }
86 
87 static gboolean
anjuta_drop_entry_drag_drop(GtkWidget * widget,GdkDragContext * context,gint x,gint y,guint time)88 anjuta_drop_entry_drag_drop (GtkWidget *widget, GdkDragContext *context,
89                              gint x, gint y, guint time)
90 {
91 	GdkAtom target_type;
92 
93 	target_type = gtk_drag_dest_find_target (widget, context, NULL);
94 
95 	if (target_type != GDK_NONE)
96 		gtk_drag_get_data (widget, context, target_type, time);
97 	else
98 		gtk_drag_finish (context, FALSE, FALSE, time);
99 
100 	return TRUE;
101 }
102 
103 static void
anjuta_drop_entry_get_preferred_height(GtkWidget * widget,gint * min_height,gint * nat_height)104 anjuta_drop_entry_get_preferred_height (GtkWidget *widget, gint *min_height,
105                                         gint *nat_height)
106 {
107 	GTK_WIDGET_CLASS (anjuta_drop_entry_parent_class)->get_preferred_height (widget,
108 	                                                                         min_height,
109 	                                                                         nat_height);
110 
111 	/* Make the entry a minimum of 40 pixels tall so that it is easier to drag
112 	 * into. */
113 	if (*min_height < 40)
114 		*min_height = 40;
115 
116 	if (*nat_height < 40)
117 		*nat_height = 40;
118 }
119 
120 static void
anjuta_drop_entry_class_init(AnjutaDropEntryClass * klass)121 anjuta_drop_entry_class_init (AnjutaDropEntryClass *klass)
122 {
123 	GObjectClass* object_class = G_OBJECT_CLASS (klass);
124 	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
125 
126 	object_class->finalize = anjuta_drop_entry_finalize;
127 	widget_class->drag_data_received = anjuta_drop_entry_drag_data_received;
128 	widget_class->drag_drop = anjuta_drop_entry_drag_drop;
129 	widget_class->get_preferred_height = anjuta_drop_entry_get_preferred_height;
130 
131 }
132 
133 GtkWidget *
anjuta_drop_entry_new(void)134 anjuta_drop_entry_new (void)
135 {
136 	return g_object_new (ANJUTA_TYPE_DROP_ENTRY, NULL);
137 }
138