Solaris Common Desktop Environment: Programmer's Guide

How Drop Zones Are Used

This section describes how drop zones are used.

Registering a Drop Zone

You generally register drop zones just after the widget that is going to be the drop zone is created. If you want a modal drop zone, you may register the widget as a drop zone when you want users to be able to drop on it and unregister it when you do not want users to drop on it.

Motif text widgets are automatically registered as drop zones for text when they are created. Dual registration is allowed. If you want a text widget to accept drops of other data, such as file names, in addition to text, you may register the text widget as a drop zone for file names as well. The text drop functionality provided by Motif is preserved. The functionality for file-name (or other data-type) drops is layered on top.

Use the function DtDndDropRegister() to register a widget as a drop zone. This function handles dual registration, if necessary, performs desktop-specific setup, and calls XmDropSiteRegister(). The DtDndDropRegister() function synopsis and parameter use are as follows.

void
DtDndDropRegister(
   Widget  dropSite,
 	DtDndProtocol	protocols;
 	unsigned char	operations;
	   XtCallbackList	transferCallback;
 	ArgList	argList;
   Cardinal	argCount)

Widget dropSite

The widget that is being registered as a drop zone.


DtDndProtocol protocols

Specifies the list of data transfer protocols that the drop zone can use. To specify the use of more than one protocol, use OR with the protocol values.


unsigned char operations

The operations supported by the drop zone. The drop zone may support any combination of XmDROP_MOVE, XmDROP_COPY, and XmDROP_LINK by using OR for the desired combination of operations.


XtCallbackList transferCallback

This function accepts the data that is dropped on the drop zone. The transfer callback is explained in greater detail in the next section.


ArgList argList

Specifies an optional argument list.


Cardinal argCount

Specifies the number of arguments in argList.

Using the Transfer Callback

The transfer callback accepts data from the drag source when a drop occurs. The first action in the transfer callback is a verification of the reason field in the callData. If the reason is not DtCR_DND_TRANSFER_DATA, you should return immediately; otherwise, proceed with data transfer based on its type and the operation specified in the reason. For example, if you are handling the copy of a file, retrieve the file name from the data structure, open the file, and copy its contents. If your drop zone supports more than one data type, you need to support the transfer of each data type appropriately.

Here is a simple transfer callback for a drawing area drop zone that supports the copying of text and file-name data types.

void
	TransferCallback(
 	Widget widget,
 	XtPointer clientData,
 	XtPointer callData)
{
   DtDndTransferCallbackStruct *transferInfo =
				(DtDndTransferCallbackStruct*) callData;
   int ii;

   DtDndcontext * dropData = transferInfo->dropData;
 			return;
   switch dropData->protocol {
 	case DtDND_FILENAME_TRANSFER:
 		for (ii=0; ii < dropData->numItems; ii++) {
 			drawTheString(dropData->data, strings[ii]);
 		}
 		break;
 	case DtDND_TEXT_TRANSFER:
 		for (ii=0; ii<dropData->numItems; ii++){
      drawTheFile(dropData->data.files[ii]);
 		}
      break;
 	default:
			transferInfo->status = DtDND_FAILURE;
     }
}

Using Data Typing

In an application that accepts drops of buffers, you may want to handle the dropped data in a different way depending on its type. To accomplish data typing, use the data-typing API. Data-typing function calls of interest are DtDtsBufferToDataType() and DtDtsBufferToAttributeValue(). The former returns the data attribute name for the data, the latter returns the value of a specified attribute of the data. Attributes you may find useful for drag and drop are shown in Table 5-8.

Table 5-8 Data-Typing Attributes

Attributes 

Description 

ICON

Path of icon to use for this data.  

MEDIA

The Message Alliance media name for this data. 

See Chapter 9, Accessing the Data-Typing Database for more information.