-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdnd.c
140 lines (122 loc) · 4.64 KB
/
dnd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* This file is part of TBO, a gnome comic editor
* Copyright (C) 2010 Daniel Garcia Moreno <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <gtk/gtk.h>
#include "dnd.h"
#include "tbo-drawing.h"
#include "frame.h"
#include "tbo-object-svg.h"
#include "tbo-object-pixmap.h"
#include "tbo-window.h"
static GtkWidget *DND_IMAGE = NULL;
void
drag_data_received_handl (GtkWidget *widget,
GdkDragContext *context,
gint x, gint y,
GtkSelectionData *selection_data,
guint target_type,
guint time,
TboWindow *tbo)
{
GtkAdjustment *adj;
float zoom = tbo_drawing_get_zoom (TBO_DRAWING (tbo->drawing));
const gchar *_sdata;
gboolean dnd_success = FALSE;
gboolean delete_selection_data = FALSE;
/* Deal with what we are given from source */
if ((selection_data != NULL) && (gtk_selection_data_get_length (selection_data) >= 0))
{
if (gdk_drag_context_get_selected_action (context) == GDK_ACTION_ASK)
{
/* Ask the user to move or copy, then set the context action. */
}
if (gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE)
delete_selection_data = TRUE;
/* Check that we got the format we can use */
switch (target_type)
{
case TARGET_STRING:
_sdata = gtk_selection_data_get_data (selection_data);
TboObjectBase *image;
Frame *frame = tbo_drawing_get_current_frame (TBO_DRAWING (tbo->drawing));
adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (tbo->dw_scroll));
int rx = tbo_frame_get_base_x ((x + gtk_adjustment_get_value(adj)) / zoom);
adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (tbo->dw_scroll));
int ry = tbo_frame_get_base_y ((y + gtk_adjustment_get_value(adj)) / zoom);
if (tbo_files_is_svg_file ((gchar *)_sdata)) {
image = TBO_OBJECT_BASE (tbo_object_svg_new_with_params (rx, ry, 0, 0, (gchar*)_sdata));
} else {
image = TBO_OBJECT_BASE (tbo_object_pixmap_new_with_params (rx, ry, 0, 0, (gchar*)_sdata));
}
tbo_drawing_update (TBO_DRAWING (tbo->drawing));
tbo_frame_add_obj (frame, TBO_OBJECT_BASE (image));
dnd_success = TRUE;
break;
default:
g_print ("nothing good");
}
}
if (dnd_success == FALSE)
{
g_print ("DnD data transfer failed!\n");
}
gtk_drag_finish (context, dnd_success, delete_selection_data, time);
}
void
drag_data_get_handl (GtkWidget *widget,
GdkDragContext *context,
GtkSelectionData *selection_data,
guint target_type,
guint time,
char *svg)
{
g_assert (selection_data != NULL);
switch (target_type)
{
case TARGET_STRING:
gtk_selection_data_set (selection_data,
gtk_selection_data_get_target (selection_data),
8*sizeof(char),
(guchar*) svg,
strlen (svg));
break;
default:
/* Default to some a safe target instead of fail. */
g_assert_not_reached ();
}
}
void
drag_begin_handl (GtkWidget *widget,
GdkDragContext *context,
char *svg)
{
DND_IMAGE = gtk_image_new_from_file (svg);
GdkPixbuf *pix = gtk_image_get_pixbuf (GTK_IMAGE (DND_IMAGE));
gtk_drag_set_icon_pixbuf (context, pix, 0, 0);
}
void
drag_end_handl (GtkWidget *widget,
GdkDragContext *context,
gpointer user_data)
{
if (DND_IMAGE)
{
gtk_widget_destroy (GTK_WIDGET (DND_IMAGE));
DND_IMAGE = NULL;
}
}