2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $TOG: text.c /main/5 1999/07/20 14:50:18 mgreess $ */
24 /*****************************************************************************
25 *****************************************************************************
29 ** Description: Text transfer functions for the CDE Drag & Drop Demo.
31 ** (c) Copyright 1993, 1994 Hewlett-Packard Company
32 ** (c) Copyright 1993, 1994 International Business Machines Corp.
33 ** (c) Copyright 1993, 1994 Sun Microsystems, Inc.
34 ** (c) Copyright 1993, 1994 Unix System Labs, Inc., a subsidiary of
37 ****************************************************************************
38 ************************************<+>*************************************/
40 #include <X11/Intrinsic.h>
45 #include <Xm/RowColumn.h>
54 /*************************************************************************
56 * Data Structures & Private Declarations For Text Transfers
58 **************************************************************************/
61 * Data for text list of fruit
64 char *todaysFruit[] = {
80 /*************************************************************************
84 *************************************************************************/
89 * Sets the text object's text to the text in the fruit list based on where
90 * the pointer was when the drag started.
98 DtDndConvertCallbackStruct *convertInfo =
99 (DtDndConvertCallbackStruct *) callData;
100 Widget fruitList = (Widget) clientData;
105 if (convertInfo == NULL) {
110 * Verify protocol and callback reason
113 if (convertInfo->dragData->protocol != DtDND_TEXT_TRANSFER ||
114 (convertInfo->reason != DtCR_DND_CONVERT_DATA &&
115 convertInfo->reason != DtCR_DND_CONVERT_DELETE) ||
120 switch (convertInfo->reason) {
121 case DtCR_DND_CONVERT_DATA:
124 * Provide the text from the fruit list
127 XtVaGetValues(fruitList,
128 XmNuserData, &selectedPos,
130 XmNitemCount, &itemCount,
133 if (itemCount > 0 && selectedPos < itemCount) {
134 convertInfo->dragData->data.strings[0] =
135 items[selectedPos-1];
137 convertInfo->status = DtDND_FAILURE;
140 DtCR_DND_CONVERT_DELETE:
143 * Delete the text from the fruit list. If the fruit list
144 * were set up to be dynamic, deletion from the list would
148 printf("Delete fruit item #%d\n",
149 convertInfo->dragData->data.strings[0]);
155 * textDragFinishCallback
157 * Normally would free any memory allocated by textConvertCallback
158 * but none was allocated so this is just a placeholder.
161 textDragFinishCallback(
163 XtPointer clientData,
169 * textTransferCallback
171 * Handles transfer of files or text to the text edit. Files are transfered
172 * by placing their name in the field, text by inserting the text into the
176 textTransferCallback(
178 XtPointer clientData,
181 DtDndTransferCallbackStruct *transferInfo =
182 (DtDndTransferCallbackStruct *) callData;
186 * Verify callback reason
189 if (transferInfo == NULL ||
190 transferInfo->reason != DtCR_DND_TRANSFER_DATA) {
194 switch (transferInfo->dropData->protocol) {
196 case DtDND_FILENAME_TRANSFER:
199 * Copy the file name into the text field
202 XtVaSetValues(widget,
203 XmNvalue, transferInfo->dropData->data.files[0],
208 case DtDND_TEXT_TRANSFER:
211 * Copy the fruit name into the text field
214 text = XmStringUnparse(transferInfo->dropData->data.strings[0],
215 NULL, XmCHARSET_TEXT, XmCHARSET_TEXT, NULL, 0, XmOUTPUT_ALL);
217 XtVaSetValues(widget, XmNvalue, text, NULL);
227 * Prepares the fruit list to source drags of text with button 1.
229 void textDragSetup(Widget fruitList)
231 static char translations[] = "\
232 ~c ~s ~m ~a <Btn1Down>:\
233 demoProcessPress(ListBeginSelect,textDragStart)\n\
234 c ~s ~m ~a <Btn1Down>:\
235 demoProcessPress(ListBeginToggle,textDragStart)";
236 static char btn2_translations[] = "\
237 ~c ~s ~m ~a <Btn2Down>:\
238 demoProcessPress(ListBeginSelect,textDragStart)\n\
239 c ~s ~m ~a <Btn2Down>:\
240 demoProcessPress(ListBeginToggle,textDragStart)\n\
241 <Btn2Motion>:ListButtonMotion()\n\
242 ~c ~s ~m ~a <Btn2Up>:ListEndSelect()\n\
243 c ~s ~m ~a <Btn2Up>:ListEndToggle()";
244 static XtActionsRec actionTable[] =
246 {"textDragStart", (XtActionProc) &textDragStart},
247 {"demoProcessPress", (XtActionProc) &demoProcessPress}
250 int btn1_transfer = 0;
251 XtTranslations new_translations;
256 sizeof(actionTable)/sizeof(actionTable[0]));
257 new_translations = XtParseTranslationTable(translations);
258 XtOverrideTranslations(fruitList, new_translations);
261 (Widget) XmGetXmDisplay(XtDisplayOfObject(fruitList)),
262 "enableBtn1Transfer", &btn1_transfer,
265 if (btn1_transfer != True)
267 new_translations = XtParseTranslationTable(btn2_translations);
268 XtOverrideTranslations(fruitList, new_translations);
272 XtAddEventHandler(fruitList, Button1MotionMask, False,
273 (XtEventHandler)demoDragMotionHandler,
274 (XtPointer)DtDND_TEXT_TRANSFER);
281 * Registers text field to accept drops of files.
287 static XtCallbackRec transferCBRec[] = { {textTransferCallback, NULL},
290 DtDndDropRegister(textField, DtDND_FILENAME_TRANSFER,
291 XmDROP_COPY, transferCBRec, NULL, 0);
297 * Initiates a drag of a text item from the fruit list provided the pointer
298 * is over an item in the list.
308 static XtCallbackRec convertCBRec[] = { {textConvertCallback, NULL},
310 static XtCallbackRec dragFinishCBRec[] =
311 { {demoDragFinishCallback, NULL},
312 {textDragFinishCallback, NULL},
316 * Determine which item to drag from the text list
319 XtVaGetValues(widget, XmNitemCount, &itemCount, NULL);
321 selectedPos = XmListYToPos(widget, event->xmotion.y);
323 if (selectedPos == 0 || selectedPos > itemCount) {
327 XtVaSetValues(widget, XmNuserData, selectedPos, NULL);
329 convertCBRec[0].closure = (XtPointer)widget;
335 if (DtDndDragStart(widget, event, DtDND_TEXT_TRANSFER, 1,
336 XmDROP_COPY, convertCBRec, dragFinishCBRec, NULL, 0)
339 printf("DragStart returned NULL.\n");
343 /*************************************************************************
345 * Text Creation & Initialization
347 *************************************************************************/
350 * textCreateDragSource
352 * Creates a scrolling list filled with fruit names.
355 textCreateDragSource(
363 for (ii = 0; todaysFruit[ii] != NULL; ii++)
367 fruits = (XmString *) XtMalloc(sizeof(XmString) * fruitCount);
369 for (ii = 0; ii < fruitCount; ii++) {
370 fruits[ii] = XmStringCreate(todaysFruit[ii],
371 XmFONTLIST_DEFAULT_TAG);
375 XtSetArg(args[ii], XmNitems, fruits); ii++;
376 XtSetArg(args[ii], XmNitemCount, fruitCount); ii++;
378 fruitList = XmCreateScrolledList(parent, "fruitList", args, ii);
379 XtManageChild(fruitList);
381 for (ii = 0; ii < fruitCount; ii++) {
382 XmStringFree(fruits[ii]);
384 XtFree((char *)fruits);
392 * Creates a text field with a label.
398 Widget textRowColumn,
402 textRowColumn = XtVaCreateManagedWidget("textRowColumn",
403 xmRowColumnWidgetClass, parent,
406 textLabel = XtVaCreateManagedWidget("textLabel",
407 xmLabelWidgetClass, textRowColumn,
410 textField = XtVaCreateManagedWidget("textField",
411 xmTextWidgetClass, textRowColumn,