PyGTK - Drag and Drop


Advertisements


Widgets having associated X Window are capable of drag and drop. In the program, a widget as a source and/or destination for drag-and-drop must first be designated. The widget defined as source can send out the dragged data. The destination widget accepts it when dragged data is dropped on it.

The following steps are involved in setting up a drag-and-drop enabled application −

Step 1 − Setting up a source widget.

Step 2 − The drag_source_set() method specifies the target types for a drag operation −

widget.drag_source_set(start_button_mask, targets, info)

Step 3 − The start_button_mask argument specifies a bitmask of buttons that starts the drag operation.

Step 4 − The target argument is a list of tuples of this structure −

(target, flags, info)

The target argument is a string representing drag type, for example, text/plain or image/x-xpixmap.

Step 6 − The following flags are predefined −

  • gtk.TARGET_SAME_APP
  • gtk.TARGET_SAME_WIDGET

Step 7 − There will be no limitation as the flag is set to 0.

If the widget is not required to act as source, it can be unset −

widget.drag_source_unset()

The source signal emits signals. The following table lists the signals and their callbacks.

drag_begin def drag_begin_cb(widget, drag_context, data):
drag_data_get def drag_data_get_cb(widget, drag_context, selection_data, info, time, data):
drag_data_delete def drag_data_delete_cb(widget, drag_context, data):
drag_end def drag_end_cb(widget, drag_context, data):

Setting up a Destination Widget

The drag_dest_set() method specifies which widget can receive dragged data.

widget.drag_dest_set(flags, targets, action)

The flags parameter can take one of the following constants −

gtk.DEST_DEFAULT_MOTION This checks if the drag matches this widget's list of possible targets and actions, then calls the drag_status() as appropriate.
gtk.DEST_DEFAULT_HIGHLIGHT This draws a highlight on this widget as long as a drag is over this widget
gtk.DEST_DEFAULT_DROP When a drop occurs, if the drag matches this widget's list of possible targets and actions call drag_get_data() on behalf of the widget. Whether or not the drop is successful, call drag_finish(). If the action was a move and the drag was successful, then TRUE will be passed for the delete parameter to drag_finish().
gtk.DEST_DEFAULT_ALL If set, specifies that all default actions should be taken.

The target is a list of tuples containing target information. The actions argument is a bitmask of or a combination of one or more of the following values −

  • gtk.gdk.ACTION_DEFAULT
  • gtk.gdk.ACTION_COPY
  • gtk.gdk.ACTION_MOVE
  • gtk.gdk.ACTION_LINK
  • gtk.gdk.ACTION_PRIVATE
  • gtk.gdk.ACTION_ASK

The "drag-motion" handler must determine if the drag data is appropriate by matching the destination targets with the gtk.gdk.DragContext targets and optionally by examining the drag data by calling the drag_get_data() method. The gtk.gdk.DragContext. drag_status() method must be called to update the drag_context status.

The "drag-drop" handler must determine the matching target using the drag_dest_find_target() method and then ask for the drag data using the drag_get_data() method. The data will be available in the "drag-data-received" handler.



Advertisements