text.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  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)
  10. * any later version.
  11. *
  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
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $TOG: text.c /main/5 1999/07/20 14:50:18 mgreess $ */
  24. /*****************************************************************************
  25. *****************************************************************************
  26. **
  27. ** File: text.c
  28. **
  29. ** Description: Text transfer functions for the CDE Drag & Drop Demo.
  30. **
  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
  35. ** Novell, Inc.
  36. **
  37. ****************************************************************************
  38. ************************************<+>*************************************/
  39. #include <X11/Intrinsic.h>
  40. #include <Xm/Xm.h>
  41. #include <Xm/Label.h>
  42. #include <Xm/List.h>
  43. #include <Xm/RowColumn.h>
  44. #include <Xm/Text.h>
  45. #include <Dt/Dt.h>
  46. #include <Dt/Dnd.h>
  47. #include "demo.h"
  48. #include "text.h"
  49. /*************************************************************************
  50. *
  51. * Data Structures & Private Declarations For Text Transfers
  52. *
  53. **************************************************************************/
  54. /*
  55. * Data for text list of fruit
  56. */
  57. char *todaysFruit[] = {
  58. "Oranges",
  59. "Peaches",
  60. "Lemons",
  61. "Watermelons",
  62. "Apples",
  63. "Bananas",
  64. "Plums",
  65. "Limes",
  66. "Cantaloupes",
  67. "Nectarines",
  68. "Papayas",
  69. "Mangos",
  70. NULL
  71. };
  72. /*************************************************************************
  73. *
  74. * Text Drag & Drop
  75. *
  76. *************************************************************************/
  77. /*
  78. * textConvertCallback
  79. *
  80. * Sets the text object's text to the text in the fruit list based on where
  81. * the pointer was when the drag started.
  82. */
  83. void
  84. textConvertCallback(
  85. Widget dragContext,
  86. XtPointer clientData,
  87. XtPointer callData)
  88. {
  89. DtDndConvertCallbackStruct *convertInfo =
  90. (DtDndConvertCallbackStruct *) callData;
  91. Widget fruitList = (Widget) clientData;
  92. int selectedPos;
  93. XmString *items;
  94. Cardinal itemCount;
  95. if (convertInfo == NULL) {
  96. return;
  97. }
  98. /*
  99. * Verify protocol and callback reason
  100. */
  101. if (convertInfo->dragData->protocol != DtDND_TEXT_TRANSFER ||
  102. (convertInfo->reason != DtCR_DND_CONVERT_DATA &&
  103. convertInfo->reason != DtCR_DND_CONVERT_DELETE) ||
  104. fruitList == NULL) {
  105. return;
  106. }
  107. switch (convertInfo->reason) {
  108. case DtCR_DND_CONVERT_DATA:
  109. /*
  110. * Provide the text from the fruit list
  111. */
  112. XtVaGetValues(fruitList,
  113. XmNuserData, &selectedPos,
  114. XmNitems, &items,
  115. XmNitemCount, &itemCount,
  116. NULL);
  117. if (itemCount > 0 && selectedPos < itemCount) {
  118. convertInfo->dragData->data.strings[0] =
  119. items[selectedPos-1];
  120. } else {
  121. convertInfo->status = DtDND_FAILURE;
  122. }
  123. break;
  124. DtCR_DND_CONVERT_DELETE:
  125. /*
  126. * Delete the text from the fruit list. If the fruit list
  127. * were set up to be dynamic, deletion from the list would
  128. * occur here.
  129. */
  130. printf("Delete fruit item #%d\n",
  131. convertInfo->dragData->data.strings[0]);
  132. break;
  133. }
  134. }
  135. /*
  136. * textDragFinishCallback
  137. *
  138. * Normally would free any memory allocated by textConvertCallback
  139. * but none was allocated so this is just a placeholder.
  140. */
  141. void
  142. textDragFinishCallback(
  143. Widget widget,
  144. XtPointer clientData,
  145. XtPointer callData)
  146. {
  147. }
  148. /*
  149. * textTransferCallback
  150. *
  151. * Handles transfer of files or text to the text edit. Files are transfered
  152. * by placing their name in the field, text by inserting the text into the
  153. * field.
  154. */
  155. void
  156. textTransferCallback(
  157. Widget widget,
  158. XtPointer clientData,
  159. XtPointer callData)
  160. {
  161. DtDndTransferCallbackStruct *transferInfo =
  162. (DtDndTransferCallbackStruct *) callData;
  163. String text;
  164. /*
  165. * Verify callback reason
  166. */
  167. if (transferInfo == NULL ||
  168. transferInfo->reason != DtCR_DND_TRANSFER_DATA) {
  169. return;
  170. }
  171. switch (transferInfo->dropData->protocol) {
  172. case DtDND_FILENAME_TRANSFER:
  173. /*
  174. * Copy the file name into the text field
  175. */
  176. XtVaSetValues(widget,
  177. XmNvalue, transferInfo->dropData->data.files[0],
  178. NULL);
  179. break;
  180. case DtDND_TEXT_TRANSFER:
  181. /*
  182. * Copy the fruit name into the text field
  183. */
  184. text = XmStringUnparse(transferInfo->dropData->data.strings[0],
  185. NULL, XmCHARSET_TEXT, XmCHARSET_TEXT, NULL, 0, XmOUTPUT_ALL);
  186. XtVaSetValues(widget, XmNvalue, text, NULL);
  187. XtFree (text);
  188. break;
  189. }
  190. }
  191. /*
  192. * textDragSetup
  193. *
  194. * Prepares the fruit list to source drags of text with button 1.
  195. */
  196. void textDragSetup(Widget fruitList)
  197. {
  198. static char translations[] = "\
  199. ~c ~s ~m ~a <Btn1Down>:\
  200. demoProcessPress(ListBeginSelect,textDragStart)\n\
  201. c ~s ~m ~a <Btn1Down>:\
  202. demoProcessPress(ListBeginToggle,textDragStart)";
  203. static char btn2_translations[] = "\
  204. ~c ~s ~m ~a <Btn2Down>:\
  205. demoProcessPress(ListBeginSelect,textDragStart)\n\
  206. c ~s ~m ~a <Btn2Down>:\
  207. demoProcessPress(ListBeginToggle,textDragStart)\n\
  208. <Btn2Motion>:ListButtonMotion()\n\
  209. ~c ~s ~m ~a <Btn2Up>:ListEndSelect()\n\
  210. c ~s ~m ~a <Btn2Up>:ListEndToggle()";
  211. static XtActionsRec actionTable[] =
  212. {
  213. {"textDragStart", (XtActionProc) &textDragStart},
  214. {"demoProcessPress", (XtActionProc) &demoProcessPress}
  215. };
  216. int btn1_transfer = 0;
  217. XtTranslations new_translations;
  218. XtAppAddActions(
  219. demoAppContext,
  220. actionTable,
  221. sizeof(actionTable)/sizeof(actionTable[0]));
  222. new_translations = XtParseTranslationTable(translations);
  223. XtOverrideTranslations(fruitList, new_translations);
  224. XtVaGetValues(
  225. (Widget) XmGetXmDisplay(XtDisplayOfObject(fruitList)),
  226. "enableBtn1Transfer", &btn1_transfer,
  227. NULL);
  228. if (btn1_transfer != True)
  229. {
  230. new_translations = XtParseTranslationTable(btn2_translations);
  231. XtOverrideTranslations(fruitList, new_translations);
  232. }
  233. #if 0
  234. XtAddEventHandler(fruitList, Button1MotionMask, False,
  235. (XtEventHandler)demoDragMotionHandler,
  236. (XtPointer)DtDND_TEXT_TRANSFER);
  237. #endif
  238. }
  239. /*
  240. * textDropSetup
  241. *
  242. * Registers text field to accept drops of files.
  243. */
  244. void
  245. textDropSetup(
  246. Widget textField)
  247. {
  248. static XtCallbackRec transferCBRec[] = { {textTransferCallback, NULL},
  249. {NULL, NULL} };
  250. DtDndDropRegister(textField, DtDND_FILENAME_TRANSFER,
  251. XmDROP_COPY, transferCBRec, NULL, 0);
  252. }
  253. /*
  254. * textDragStart
  255. *
  256. * Initiates a drag of a text item from the fruit list provided the pointer
  257. * is over an item in the list.
  258. */
  259. void
  260. textDragStart(
  261. Widget widget,
  262. XEvent *event)
  263. {
  264. int itemCount,
  265. selectedPos;
  266. static XtCallbackRec convertCBRec[] = { {textConvertCallback, NULL},
  267. {NULL, NULL} };
  268. static XtCallbackRec dragFinishCBRec[] =
  269. { {demoDragFinishCallback, NULL},
  270. {textDragFinishCallback, NULL},
  271. {NULL, NULL} };
  272. /*
  273. * Determine which item to drag from the text list
  274. */
  275. XtVaGetValues(widget, XmNitemCount, &itemCount, NULL);
  276. selectedPos = XmListYToPos(widget, event->xmotion.y);
  277. if (selectedPos == 0 || selectedPos > itemCount) {
  278. return;
  279. }
  280. XtVaSetValues(widget, XmNuserData, selectedPos, NULL);
  281. convertCBRec[0].closure = (XtPointer)widget;
  282. /*
  283. * Start the drag
  284. */
  285. if (DtDndDragStart(widget, event, DtDND_TEXT_TRANSFER, 1,
  286. XmDROP_COPY, convertCBRec, dragFinishCBRec, NULL, 0)
  287. == NULL) {
  288. printf("DragStart returned NULL.\n");
  289. }
  290. }
  291. /*************************************************************************
  292. *
  293. * Text Creation & Initialization
  294. *
  295. *************************************************************************/
  296. /*
  297. * textCreateDragSource
  298. *
  299. * Creates a scrolling list filled with fruit names.
  300. */
  301. Widget
  302. textCreateDragSource(
  303. Widget parent)
  304. {
  305. Widget fruitList;
  306. XmString *fruits;
  307. Arg args[2];
  308. int ii, fruitCount;
  309. for (ii = 0; todaysFruit[ii] != NULL; ii++)
  310. ;
  311. fruitCount = ii;
  312. fruits = (XmString *) XtMalloc(sizeof(XmString) * fruitCount);
  313. for (ii = 0; ii < fruitCount; ii++) {
  314. fruits[ii] = XmStringCreate(todaysFruit[ii],
  315. XmFONTLIST_DEFAULT_TAG);
  316. }
  317. ii = 0;
  318. XtSetArg(args[ii], XmNitems, fruits); ii++;
  319. XtSetArg(args[ii], XmNitemCount, fruitCount); ii++;
  320. fruitList = XmCreateScrolledList(parent, "fruitList", args, ii);
  321. XtManageChild(fruitList);
  322. for (ii = 0; ii < fruitCount; ii++) {
  323. XmStringFree(fruits[ii]);
  324. }
  325. XtFree((char *)fruits);
  326. return fruitList;
  327. }
  328. /*
  329. * textCreateDropSite
  330. *
  331. * Creates a text field with a label.
  332. */
  333. Widget
  334. textCreateDropSite(
  335. Widget parent)
  336. {
  337. Widget textRowColumn,
  338. textLabel,
  339. textField;
  340. textRowColumn = XtVaCreateManagedWidget("textRowColumn",
  341. xmRowColumnWidgetClass, parent,
  342. NULL);
  343. textLabel = XtVaCreateManagedWidget("textLabel",
  344. xmLabelWidgetClass, textRowColumn,
  345. NULL);
  346. textField = XtVaCreateManagedWidget("textField",
  347. xmTextWidgetClass, textRowColumn,
  348. NULL);
  349. return textField;
  350. }