summaryrefslogtreecommitdiff
path: root/gladeui/glade-dnd.c
blob: 4a94fc227eae3316ba2c88f8810d49e1882e03d4 (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * glade-dnd.c
 *
 * Copyright (C) 2013  Juan Pablo Ugarte
 *
 * Authors:
 *   Juan Pablo Ugarte <juanpablougarte@gmail.com>
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "glade.h"
#include "glade-dnd.h"

GtkTargetEntry *
_glade_dnd_get_target (void)
{
  static GtkTargetEntry target = {GLADE_DND_TARGET_DATA, GTK_TARGET_SAME_APP, GLADE_DND_INFO_DATA};
  return &target;
}

void
_glade_dnd_dest_set (GtkWidget *target)
{
  gtk_drag_dest_set (target, 0, _glade_dnd_get_target (), 1, GDK_ACTION_MOVE | GDK_ACTION_COPY);
}

GObject *
_glade_dnd_get_data (GdkDragContext   *context,
                     GtkSelectionData *selection,
                     guint             info)
{
  GdkAtom target = gtk_selection_data_get_target (selection);
  gchar *target_name = gdk_atom_name (target);
  gboolean is_target_data = (g_strcmp0 (target_name, GLADE_DND_TARGET_DATA) == 0);

  g_free (target_name);

  if (info == GLADE_DND_INFO_DATA && is_target_data)
    {
      const guchar *data = gtk_selection_data_get_data (selection);
      if (data)
        return *((GObject **)data);
    }
  return NULL;
}


void
_glade_dnd_set_data (GtkSelectionData *selection, GObject *data)
{
  static GdkAtom type = 0;

  if (!type)
    type = gdk_atom_intern_static_string (GLADE_DND_TARGET_DATA);

  gtk_selection_data_set (selection, type, sizeof (gpointer),
                          (const guchar *)&data,
                          sizeof (gpointer));
}

static gboolean
on_drag_icon_draw (GtkWidget *widget, cairo_t *cr)
{
  GtkStyleContext *context = gtk_widget_get_style_context (widget);
  cairo_pattern_t *gradient;
  GtkAllocation alloc;
  gint x, y, w, h;
  gdouble h2;
  GdkRGBA bg;

  /* Not needed acording to GtkWidget:draw documentation
   * But seems like there is a bug when used as a drag_icon that makes the
   * cairo translation used here persist when drawing children.
   */
  cairo_save (cr);

  /* Clear BG */
  cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
  cairo_paint (cr);
  cairo_set_operator (cr, CAIRO_OPERATOR_OVER);

  gtk_widget_get_allocation (widget, &alloc);
  x = alloc.x;
  y = alloc.y;
  w = alloc.width;
  h = alloc.height;
  h2 = h/2.0;

  gtk_style_context_get_background_color (context, gtk_style_context_get_state (context), &bg);

  gradient = cairo_pattern_create_linear (x, y, x, y+h);
  cairo_pattern_add_color_stop_rgba (gradient, 0, bg.red, bg.green, bg.blue, 0);
  cairo_pattern_add_color_stop_rgba (gradient, .5, bg.red, bg.green, bg.blue, .8);
  cairo_pattern_add_color_stop_rgba (gradient, 1, bg.red, bg.green, bg.blue, 0);

  cairo_set_source (cr, gradient);
  cairo_rectangle (cr, x+h2, y, w-h, h);
  cairo_fill (cr);
  cairo_pattern_destroy (gradient);

  gradient = cairo_pattern_create_radial (x+h2, y+h2, 0, x+h2, y+h2, h2);
  cairo_pattern_add_color_stop_rgba (gradient, 0, bg.red, bg.green, bg.blue, .8);
  cairo_pattern_add_color_stop_rgba (gradient, 1, bg.red, bg.green, bg.blue, 0);

  cairo_set_source (cr, gradient);
  cairo_rectangle (cr, x, y, h2, h);
  cairo_fill (cr);

  cairo_translate (cr, w-h, 0);
  cairo_set_source (cr, gradient);
  cairo_rectangle (cr, x+h2, y, h2, h);
  cairo_fill (cr);

  cairo_pattern_destroy (gradient);
  cairo_restore (cr);

  return FALSE;
}

void
_glade_dnd_set_icon_widget (GdkDragContext *context,
                            const gchar *icon_name,
                            const gchar *description)
{
  GtkWidget *window, *box, *label, *icon;
  GdkScreen *screen;
  GdkVisual *visual;

  screen = gdk_window_get_screen (gdk_drag_context_get_source_window (context));
  window = gtk_window_new (GTK_WINDOW_POPUP);

  gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DND);
  gtk_window_set_screen (GTK_WINDOW (window), screen);

  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
  gtk_container_set_border_width (GTK_CONTAINER (box), 12);

  icon = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
  gtk_widget_set_opacity (icon, .8);

  label = gtk_label_new (description);

  gtk_box_pack_start (GTK_BOX (box), icon, FALSE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (box), label, FALSE, TRUE, 0);

  gtk_widget_show_all (box);
  gtk_container_add (GTK_CONTAINER (window), box);

  if ((visual = gdk_screen_get_rgba_visual (screen)))
    {
      gtk_widget_set_visual (window, visual);
      gtk_widget_set_app_paintable (window, TRUE);
      g_signal_connect (window, "draw", G_CALLBACK (on_drag_icon_draw), NULL);
    }

  g_object_ref_sink (window);
  gtk_drag_set_icon_widget (context, window, 0, 0);
  g_object_unref (window);
}