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
#include <ctk/ctk.h>

static CtkTargetEntry entries[] = {
  { "CTK_LIST_BOX_ROW", CTK_TARGET_SAME_APP, 0 }
};

static void
drag_begin (CtkWidget      *widget,
            CdkDragContext *context,
            gpointer        data)
{
  CtkWidget *row;
  CtkAllocation alloc;
  cairo_surface_t *surface;
  cairo_t *cr;
  int x, y;
  double sx, sy;

  row = ctk_widget_get_ancestor (widget, CTK_TYPE_LIST_BOX_ROW);
  ctk_widget_get_allocation (row, &alloc);
  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, alloc.width, alloc.height);
  cr = cairo_create (surface);

  ctk_style_context_add_class (ctk_widget_get_style_context (row), "drag-icon");
  ctk_widget_draw (row, cr);
  ctk_style_context_remove_class (ctk_widget_get_style_context (row), "drag-icon");

  ctk_widget_translate_coordinates (widget, row, 0, 0, &x, &y);
  cairo_surface_get_device_scale (surface, &sx, &sy);
  cairo_surface_set_device_offset (surface, -x * sx, -y * sy);
  ctk_drag_set_icon_surface (context, surface);

  cairo_destroy (cr);
  cairo_surface_destroy (surface);
}

void
drag_data_get (CtkWidget        *widget,
               CdkDragContext   *context,
               CtkSelectionData *selection_data,
               guint             info,
               guint             time,
               gpointer          data)
{
  ctk_selection_data_set (selection_data,
                          cdk_atom_intern_static_string ("CTK_LIST_BOX_ROW"),
                          32,
                          (const guchar *)&widget,
                          sizeof (gpointer));
}

static void
drag_data_received (CtkWidget        *widget,
                    CdkDragContext   *context,
                    gint              x,
                    gint              y,
                    CtkSelectionData *selection_data,
                    guint             info,
                    guint32           time,
                    gpointer          data)
{
  CtkWidget *target;
  CtkWidget *row;
  CtkWidget *source;
  int pos;

  target = widget;

  pos = ctk_list_box_row_get_index (CTK_LIST_BOX_ROW (target));
  row = (gpointer)* (gpointer*)ctk_selection_data_get_data (selection_data);
  source = ctk_widget_get_ancestor (row, CTK_TYPE_LIST_BOX_ROW);

  if (source == target)
    return;

  g_object_ref (source);
  ctk_container_remove (CTK_CONTAINER (ctk_widget_get_parent (source)), source);
  ctk_list_box_insert (CTK_LIST_BOX (ctk_widget_get_parent (target)), source, pos);
  g_object_unref (source);
}

static CtkWidget *
create_row (const gchar *text)
{
  CtkWidget *row, *handle, *box, *label, *image;

  row = ctk_list_box_row_new ();

  box = ctk_box_new (CTK_ORIENTATION_HORIZONTAL, 10);
  g_object_set (box, "margin-start", 10, "margin-end", 10, NULL);
  ctk_container_add (CTK_CONTAINER (row), box);

  handle = ctk_event_box_new ();
  image = ctk_image_new_from_icon_name ("open-menu-symbolic", 1);
  ctk_container_add (CTK_CONTAINER (handle), image);
  ctk_container_add (CTK_CONTAINER (box), handle);

  label = ctk_label_new (text);
  ctk_container_add_with_properties (CTK_CONTAINER (box), label, "expand", TRUE, NULL);

  ctk_drag_source_set (handle, CDK_BUTTON1_MASK, entries, 1, CDK_ACTION_MOVE);
  g_signal_connect (handle, "drag-begin", G_CALLBACK (drag_begin), NULL);
  g_signal_connect (handle, "drag-data-get", G_CALLBACK (drag_data_get), NULL);

  ctk_drag_dest_set (row, CTK_DEST_DEFAULT_ALL, entries, 1, CDK_ACTION_MOVE);
  g_signal_connect (row, "drag-data-received", G_CALLBACK (drag_data_received), NULL);

  return row;
}

static const char *css =
  ".drag-icon { "
  "  background: white; "
  "  border: 1px solid black; "
  "}";

int
main (int argc, char *argv[])
{
  CtkWidget *window, *list, *sw;
  gint i;
  CtkCssProvider *provider;

  ctk_init (NULL, NULL);

  provider = ctk_css_provider_new ();
  ctk_css_provider_load_from_data (provider, css, -1, NULL);
  ctk_style_context_add_provider_for_screen (cdk_screen_get_default (),
                                             CTK_STYLE_PROVIDER (provider),
                                             CTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

  window = ctk_window_new (CTK_WINDOW_TOPLEVEL);
  ctk_window_set_default_size (CTK_WINDOW (window), -1, 300);

  sw = ctk_scrolled_window_new (NULL, NULL);
  ctk_widget_set_hexpand (sw, TRUE);
  ctk_scrolled_window_set_policy (CTK_SCROLLED_WINDOW (sw), CTK_POLICY_NEVER, CTK_POLICY_ALWAYS);
  ctk_container_add (CTK_CONTAINER (window), sw);

  list = ctk_list_box_new ();
  ctk_list_box_set_selection_mode (CTK_LIST_BOX (list), CTK_SELECTION_NONE);
  ctk_container_add (CTK_CONTAINER (sw), list);

  for (i = 0; i < 20; i++)
    {
      CtkWidget *row;
      gchar *text;<--- Variable 'text' can be declared as pointer to const

      text = g_strdup_printf ("Row %d", i);
      row = create_row (text);
      ctk_list_box_insert (CTK_LIST_BOX (list), row, -1);
    }

  ctk_widget_show_all (window);

  ctk_main ();

  return 0;
}