Gtk_drag_set_icon_surface (context, surface) Surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
![gtk tutorials gtk tutorials](https://www.monodevelop.com/images/330-Stetic_Tutorial_08.jpg)
…and do some extra work to create a nice ‘drag icon’: row = gtk_widget_get_ancestor (widget, GTK_TYPE_LIST_BOX_ROW) To make that happen, we connect to the drag-begin signal on the drag source: g_signal_connect (handle, "drag-begin", The expected behavior is to drag a visual representation of the row. So far, during the drag, you just see just the cursor, which is not very helpful and not very pretty. Yay!Īs a final step, lets make it look good. The only trick here is that we need to take a reference on the widget before removing it from its parent container, to prevent it from getting finalized.Īnd with this, we have reorderable rows. Gtk_list_box_insert (GTK_LIST_BOX (target_list), source, position) Gtk_container_remove (GTK_CONTAINER (source_list), source) Position = gtk_list_box_row_get_index (GTK_LIST_BOX_ROW (target)) Target_list = gtk_widget_get_parent (target)
![gtk tutorials gtk tutorials](https://stefanoprenna.com/wp-content/uploads/2016/02/broadway-2.0-Chromium_002.png)
Source_list = gtk_widget_get_parent (source) Source = gtk_widget_get_ancestor (handle, GTK_TYPE_LIST_BOX_ROW) handle = *(gpointer*)gtk_selection_data_get_data (selection_data) In our case, we will pull the pointer out of the selection data, and reorder the row. On the target side, drag-data-received is emitted on the drop target when GTK+ passes the data it received on to the application. Gdk_atom_intern_static_string ("GTK_LIST_BOX_ROW"), In our case, the function will just put a pointer to the source widget in the selection data: gtk_selection_data_set (selection_data, On the source side, the drag-data-get signal is emitted when GTK+ needs the data to send it to the drop target. G_signal_connect (row, "drag-data-received", To send and receive data, we need to connect to signals on both the source and the target side: g_signal_connect (handle, "drag-data-get", GTK+ uses a data holder object called GtkSelectionData for this. Transferring the dataĭrag-and-drop is often used to transfer data between applications. We need to do a little bit of extra work to make the reordering happen. Now we can start a drag on the handle, and we can drop it on some other row. GTK_DEST_DEFAULT_ALL tells GTK+ to handle all aspects of the DND operation for us, so we can keep this example simple. The entries are the same that we discussed above. GTK_DEST_DEFAULT_ALL, entries, 1, GDK_ACTION_MOVE) a place that will potentially accept drops). The easiest way to do that is to just make each row a drop target (i.e. In contrast to drags, where we created a visible drag handle to give users a hint that drag-and-drop is supported, we want to just accept drops anywhere in the list.
GTK TUTORIALS CODE
With this code in place, you can already drag your rows, but so far, there’s nowhere to drop them. GTK4 will offer a different API to create drag sources that avoids the need for a window. A GtkButton or a GtkEventBox (as in this example) will work. In our case, we will not offer a standard mime type like text/plain, but instead make up our own, private type, and also hint GTK+ that we don’t want to support drags to other applications: static GtkTargetEntry entries = Ī little gotcha here is that the widget you set up as drag source must have a GdkWindow.
![gtk tutorials gtk tutorials](https://img.youtube.com/vi/o-iK7xStuZE/0.jpg)
The entries tell GTK+ what data we want to offer via drags from this source. Note that I choose to create a visible drag handle here instead of allowing drags to start anywhere on the row. GDK_BUTTON1_MASK, entries, 1, GDK_ACTION_MOVE)
![gtk tutorials gtk tutorials](http://www.monobrasil.com.br/archived/images/5/57/IFolder_Linux.png)
Gtk_image_new_from_icon_name ("open-menu-symbolic", 1)) Gtk_container_add (GTK_CONTAINER (handle), But we go for the simpler approach here: we just declare statically that our list rows should be drag sources, and let GTK+ handle all the details: handle = gtk_event_box_new () You can decide dynamically to initiate a drag by calling gtk_drag_begin(). a place where clicking and dragging will initiate a DND operation). There are two ways to make a GTK+ widget a drag source (i.e. Since I haven’t seen drag-and-drop used much with list boxes, here is a quick summary of what is needed to get the basics working. I’ve recently had an occasion to implement reordering of a GtkListBox via drag-and-drop (DND).