2
0

buff.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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: buff.c /main/5 1999/07/20 14:48:23 mgreess $ */
  24. /*****************************************************************************
  25. *****************************************************************************
  26. **
  27. ** File: buff.c
  28. **
  29. ** Description: Buffer 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 <time.h>
  40. #include <X11/Intrinsic.h>
  41. #include <Xm/Xm.h>
  42. #include <Xm/List.h>
  43. #include <Dt/Dt.h>
  44. #include <Dt/Dnd.h>
  45. #include "icon.h"
  46. #include "file.h"
  47. #include "demo.h"
  48. #include "buff.h"
  49. extern Widget topLevel;
  50. /*************************************************************************
  51. *
  52. * Data Structures & Private Declarations For Appointment Buffers
  53. *
  54. **************************************************************************/
  55. /*
  56. * Appointment structure used to store appointments internally.
  57. */
  58. typedef struct _Appointment {
  59. char *date;
  60. char *start;
  61. char *end;
  62. char *what;
  63. } Appointment;
  64. /*
  65. * List of appointments which always appear to be for the current date.
  66. */
  67. char today[9]; /* initialized in apptCreateList() */
  68. Appointment todaysApptList[] = {
  69. #ifdef TEST_APPT_NAMES
  70. { today, "", "", "Staff Meeting" }, /* no name */
  71. { today, " 9:30am", "10:00am", "Will's Party1" }, /* duplicate names */
  72. { today, " 9:30am", "10:00am", "Will's Party2" },
  73. #else
  74. { today, " 9:00am", " 9:30am", "Staff Meeting" },
  75. { today, " 9:30am", "10:00am", "Will's Party" },
  76. #endif
  77. { today, "10:00am", "10:30am", "Conference Call" },
  78. { today, "10:30am", "11:30am", "Work on Mail" },
  79. { today, "11:00am", "11:30pm", "B'fast w/ Robert" },
  80. { today, " 1:30pm", " 2:30pm", "Design Meeting" },
  81. { today, " 3:00pm", " 4:00pm", "Communications" },
  82. { today, " 4:00pm", " 4:30pm", "Pick up Dogs" },
  83. { today, " 5:00pm", " 6:30pm", "Beer Bust" },
  84. { today, " 7:00pm", " 9:00pm", "Dinner - Stuart" },
  85. { NULL, NULL, NULL, NULL }
  86. };
  87. /*
  88. * CDE appointment format used to transfer the appointments via drag & drop.
  89. */
  90. char *apptFormat =
  91. " ** Calendar Appointment **\n\
  92. \n\
  93. Date: %s\n\
  94. Start: %s\n\
  95. End: %s\n\
  96. What: %s\n";
  97. /*
  98. * Private Appointment Buffer Function Declarations
  99. */
  100. static void apptConvertCallback(Widget, XtPointer, XtPointer);
  101. static int apptCreateList(XmString**);
  102. static void apptDestroyList(XmString*,int);
  103. static Appointment* apptFromListEntry(XmString);
  104. static char* apptGetLabel(char*);
  105. /*************************************************************************
  106. *
  107. * Appointment Drag & Drop
  108. *
  109. **************************************************************************/
  110. /*
  111. * apptConvertCallback
  112. *
  113. * Fills in buffer structure with calendar appointment string based on which
  114. * appointments are selected in the scrolled list when the drag is started.
  115. * ihen no appointments are selected, the appointment under the pointer is
  116. * used. Supply a label for the calendar appointment based on the contents
  117. * of the appointment.
  118. */
  119. static void
  120. apptConvertCallback(
  121. Widget dragContext,
  122. XtPointer clientData,
  123. XtPointer callData)
  124. {
  125. DtDndConvertCallbackStruct *convertInfo =
  126. (DtDndConvertCallbackStruct *) callData;
  127. DtDndBuffer *buffers = convertInfo->dragData->data.buffers;
  128. Widget apptList = (Widget)clientData;
  129. int selectedPos, ii,
  130. selectedItemCount;
  131. XmStringTable selectedItems;
  132. char apptString[1024];
  133. String labelString;
  134. Appointment *appt;
  135. if (convertInfo == NULL) {
  136. return;
  137. }
  138. /*
  139. * Verify the validity of the callback reason
  140. */
  141. if (convertInfo->dragData->protocol != DtDND_BUFFER_TRANSFER ||
  142. (convertInfo->reason != DtCR_DND_CONVERT_DATA &&
  143. convertInfo->reason != DtCR_DND_CONVERT_DELETE)) {
  144. return;
  145. }
  146. /*
  147. * Get selected items from the list
  148. */
  149. XtVaGetValues(apptList,
  150. XmNuserData, &selectedPos,
  151. XmNselectedItemCount, &selectedItemCount,
  152. XmNselectedItems, &selectedItems,
  153. NULL);
  154. for (ii = 0; ii < convertInfo->dragData->numItems; ii++) {
  155. /*
  156. * Get the actual appointment(s)
  157. */
  158. if (selectedItemCount == 0) {
  159. appt = &todaysApptList[selectedPos-1+ii];
  160. } else {
  161. appt = apptFromListEntry(selectedItems[ii]);
  162. }
  163. sprintf(apptString, apptFormat,
  164. appt->date, appt->start, appt->end, appt->what);
  165. /*
  166. * Supply the appointment(s) for transfer
  167. */
  168. if (convertInfo->reason == DtCR_DND_CONVERT_DATA) {
  169. /* Copy the appointment into the buffer for transfer */
  170. buffers[ii].bp = XtNewString(apptString);
  171. buffers[ii].size = strlen(buffers[ii].bp);
  172. /* Supply the name for the appointment */
  173. labelString = apptGetLabel(apptString);
  174. if (labelString == NULL) {
  175. buffers[ii].name = (char *)NULL;
  176. } else {
  177. buffers[ii].name = XtNewString(labelString);
  178. XtFree(labelString);
  179. }
  180. /*
  181. * Delete the moved appointment(s)
  182. */
  183. } else if (convertInfo->reason == DtCR_DND_CONVERT_DELETE) {
  184. printf("Delete appointment for %s\n", appt->what);
  185. }
  186. }
  187. }
  188. /*
  189. * apptDragFinishCallback
  190. *
  191. * Free buffer data/names allocated in apptConvertCallback()
  192. */
  193. void
  194. apptDragFinishCallback(
  195. Widget widget,
  196. XtPointer clientData,
  197. XtPointer callData)
  198. {
  199. DtDndDragFinishCallbackStruct *dropFinishInfo =
  200. (DtDndDragFinishCallbackStruct *)callData;
  201. DtDndContext *dragData = dropFinishInfo->dragData;
  202. int ii;
  203. for (ii = 0; ii < dragData->numItems; ii++) {
  204. XtFree(dragData->data.buffers[ii].bp);
  205. if (dragData->data.buffers[ii].name != NULL)
  206. XtFree(dragData->data.buffers[ii].name);
  207. }
  208. }
  209. /*
  210. * apptTransferCallback
  211. *
  212. * Handles the transfer of an appointment to the draw area drop site.
  213. * Adds the appropriate icon to the list of icons on the draw area.
  214. */
  215. void
  216. apptTransferCallback(
  217. Widget widget,
  218. XtPointer clientData,
  219. XtPointer callData)
  220. {
  221. DtDndTransferCallbackStruct *transferInfo =
  222. (DtDndTransferCallbackStruct*) callData;
  223. DtDndBuffer *buffers;
  224. IconInfo *iconList, *iconPtr;
  225. char *name;
  226. char *filename;
  227. int ii;
  228. if (transferInfo == NULL) {
  229. return;
  230. }
  231. /*
  232. * Verify the validity of the callback reason.
  233. */
  234. if (transferInfo->dropData->protocol != DtDND_BUFFER_TRANSFER ||
  235. transferInfo->reason != DtCR_DND_TRANSFER_DATA) {
  236. return;
  237. }
  238. /*
  239. * Use abbreviated method of reffering to the data buffers.
  240. */
  241. if (transferInfo != NULL && transferInfo->dropData != NULL) {
  242. buffers = transferInfo->dropData->data.buffers;
  243. } else {
  244. return;
  245. }
  246. /*
  247. * Process each item being transfered.
  248. */
  249. XtVaGetValues(widget, XmNuserData, &iconList, NULL);
  250. for (ii = 0; ii < transferInfo->dropData->numItems; ii++) {
  251. /*
  252. * Check format of buffer
  253. */
  254. /*
  255. * Transfer the buffer data. Here there is no actual transfer
  256. * taking place. Only the icons representing the appointments
  257. * are created to indicate the transfer.
  258. */
  259. name = buffers[ii].name;
  260. if (name == NULL)
  261. name = "unnamed";
  262. /* Create file from buffer */
  263. filename = fileStoreBuffer(buffers[ii].name,
  264. buffers[ii].bp, buffers[ii].size);
  265. printf("Stored buffer into '%s'\n", filename);
  266. XtFree(filename);
  267. /* Create icon */
  268. iconPtr = IconNew();
  269. IconInitialize(widget, iconPtr,
  270. transferInfo->x + ii * 10,
  271. transferInfo->y + ii * 10,
  272. buffers[ii].bp, buffers[ii].size, name, IconByData);
  273. /* Add to icon list on drop site */
  274. iconPtr->next = iconList;
  275. if (iconList != NULL) {
  276. iconList->prev = iconPtr;
  277. }
  278. iconList = iconPtr;
  279. XtVaSetValues(widget, XmNuserData, iconList, NULL);
  280. }
  281. }
  282. /*
  283. * apptDragSetup
  284. *
  285. * Prepares the appointment list to source drags of appointments with button 1.
  286. */
  287. void apptDragSetup(Widget apptDragSource)
  288. {
  289. static char translations[] = "\
  290. ~c ~s ~m ~a <Btn1Down>:\
  291. demoProcessPress(ListBeginSelect,apptDragStart)\n\
  292. c ~s ~m ~a <Btn1Down>:\
  293. demoProcessPress(ListBeginToggle,apptDragStart)";
  294. static char btn2_translations[] = "\
  295. ~c ~s ~m ~a <Btn2Down>:\
  296. demoProcessPress(ListBeginSelect,apptDragStart)\n\
  297. c ~s ~m ~a <Btn2Down>:\
  298. demoProcessPress(ListBeginToggle,apptDragStart)\n\
  299. <Btn2Motion>:ListButtonMotion()\n\
  300. ~c ~s ~m ~a <Btn2Up>:ListEndSelect()\n\
  301. c ~s ~m ~a <Btn2Up>:ListEndToggle()";
  302. static XtActionsRec actionTable[] =
  303. {
  304. {"apptDragStart", (XtActionProc) &apptDragStart},
  305. {"demoProcessPress", (XtActionProc) &demoProcessPress}
  306. };
  307. int btn1_transfer = 0;
  308. XtTranslations new_translations;
  309. XtAppAddActions(
  310. demoAppContext,
  311. actionTable,
  312. sizeof(actionTable)/sizeof(actionTable[0]));
  313. new_translations = XtParseTranslationTable(translations);
  314. XtOverrideTranslations(apptDragSource, new_translations);
  315. XtVaGetValues(
  316. (Widget) XmGetXmDisplay(XtDisplayOfObject(apptDragSource)),
  317. "enableBtn1Transfer", &btn1_transfer,
  318. NULL);
  319. if (btn1_transfer != True)
  320. {
  321. new_translations = XtParseTranslationTable(btn2_translations);
  322. XtOverrideTranslations(apptDragSource, new_translations);
  323. }
  324. #if 0
  325. XtAddEventHandler(apptDragSource, Button1MotionMask, False,
  326. (XtEventHandler)demoDragMotionHandler,
  327. (XtPointer)DtDND_BUFFER_TRANSFER);
  328. #endif
  329. }
  330. /*
  331. * apptDropSetup
  332. *
  333. * Such a function is not needed since the demoDropSetup in demo.c registers
  334. * the draw area as a drop site for drops of buffers such as appointments.
  335. */
  336. /*
  337. * apptDragStart
  338. *
  339. * Initiates a drag of an appointment from the appointment list provided
  340. * the pointer is over an appointment in the list.
  341. */
  342. void
  343. apptDragStart(
  344. Widget widget,
  345. XEvent *event)
  346. {
  347. static XtCallbackRec convertCBRec[] = { {apptConvertCallback, NULL},
  348. {NULL, NULL} };
  349. static XtCallbackRec dragFinishCBRec[] =
  350. { {demoDragFinishCallback, NULL},
  351. {apptDragFinishCallback, NULL},
  352. {NULL, NULL} };
  353. static IconInfo *iconPtr = NULL;
  354. Widget dragIcon;
  355. Display *display = XtDisplay(widget);
  356. int itemCount,
  357. selectedPos,
  358. selectedItemCount;
  359. char apptString[1024];
  360. convertCBRec[0].closure = (XtPointer)widget;
  361. /*
  362. * Get list of selected items from the scrolled list of appointments
  363. */
  364. XtVaGetValues(widget,
  365. XmNitemCount, &itemCount,
  366. XmNselectedItemCount, &selectedItemCount,
  367. NULL);
  368. /*
  369. * Find out which item the pointer was over when the drag began
  370. */
  371. selectedPos = XmListYToPos(widget, event->xmotion.y);
  372. if (selectedPos == 0 || selectedPos > itemCount) {
  373. return;
  374. }
  375. XtVaSetValues(widget, XmNuserData, selectedPos, NULL);
  376. /*
  377. * Copy the appointment information into an appointment string
  378. */
  379. sprintf(apptString, apptFormat,
  380. todaysApptList[1].date, todaysApptList[1].start,
  381. todaysApptList[1].end, todaysApptList[1].what);
  382. /*
  383. * Create drag icon for appointment buffer drag
  384. */
  385. if (iconPtr == NULL) {
  386. iconPtr = IconNew();
  387. IconInitialize(widget, iconPtr, 0, 0,
  388. apptString, strlen(apptString), NULL, IconByData);
  389. }
  390. if (iconPtr->dragIcon == NULL) {
  391. iconPtr->dragIcon = DtDndCreateSourceIcon(widget,
  392. iconPtr->bitmap, iconPtr->mask);
  393. }
  394. if (selectedItemCount > 1) {
  395. dragIcon = NULL;
  396. itemCount = selectedItemCount;
  397. } else {
  398. dragIcon = iconPtr->dragIcon;
  399. itemCount = 1;
  400. }
  401. /*
  402. * Start the drag
  403. */
  404. if (DtDndVaDragStart(widget, event, DtDND_BUFFER_TRANSFER, itemCount,
  405. XmDROP_COPY,
  406. convertCBRec, dragFinishCBRec,
  407. DtNsourceIcon, dragIcon,
  408. DtNbufferIsText, True,
  409. NULL)
  410. == NULL) {
  411. printf("DragStart returned NULL.\n");
  412. }
  413. }
  414. /*************************************************************************
  415. *
  416. * Appointment Creation, Initialization & Destruction
  417. *
  418. *************************************************************************/
  419. /*
  420. * apptCreateDragSource
  421. *
  422. * Create a scrolling list filled with appointments.
  423. */
  424. Widget
  425. apptCreateDragSource(
  426. Widget parent)
  427. {
  428. Widget apptList;
  429. XmString *apptListItems;
  430. int apptCount;
  431. apptCount = apptCreateList(&apptListItems);
  432. apptList = XtVaCreateManagedWidget("apptList",
  433. xmListWidgetClass, parent,
  434. /*
  435. * Uncomment the line specifying the selection policy to
  436. * enable multiple buffer transfers from the scrolled list
  437. * of appointments. WARNING: There is a bug in the interaction
  438. * of the scrolled list and Drag and Drop which causes items
  439. * in the scrolled list which are selected to appear unselected
  440. * and vice versa.
  441. */
  442. /* XmNselectionPolicy, XmMULTIPLE_SELECT, */
  443. XmNitems, apptListItems,
  444. XmNitemCount, apptCount,
  445. NULL);
  446. apptDestroyList(apptListItems, apptCount);
  447. return apptList;
  448. }
  449. /*
  450. * apptCreateDropSite
  451. *
  452. * Such a function is not needed since the drop site is the draw area which
  453. * is created in demoCreateDropSite() in demo.c
  454. */
  455. /*
  456. * apptCreateList
  457. *
  458. * Creates a list of XmStrings with appointment data in them.
  459. */
  460. static int
  461. apptCreateList(
  462. XmString **appts)
  463. {
  464. int ii, apptCount;
  465. char tmpStr[256];
  466. time_t now;
  467. struct tm *tm;
  468. now = time(&now);
  469. tm = localtime(&now);
  470. sprintf(today, "%2d/%2d/%2d", tm->tm_mon+1, tm->tm_mday, tm->tm_year);
  471. for (ii = 0; todaysApptList[ii].date; ii++);
  472. apptCount = ii;
  473. *appts = (XmString *) XtMalloc(sizeof(XmString) * apptCount);
  474. for (ii = 0; todaysApptList[ii].date; ii++) {
  475. sprintf(tmpStr, "%s %s", todaysApptList[ii].start,
  476. todaysApptList[ii].what);
  477. (*appts)[ii] = XmStringCreate(tmpStr, XmFONTLIST_DEFAULT_TAG);
  478. }
  479. return apptCount;
  480. }
  481. /*
  482. * apptDestroyList
  483. *
  484. * Destroys a list of XmStrings with appointment data in them.
  485. */
  486. static void
  487. apptDestroyList(
  488. XmString *appts,
  489. int apptCount)
  490. {
  491. int ii;
  492. for (ii = 0; ii < apptCount; ii++) {
  493. XmStringFree(appts[ii]);
  494. }
  495. XtFree((char *)appts);
  496. }
  497. /*************************************************************************
  498. *
  499. * Appointment Utility Functions
  500. *
  501. *************************************************************************/
  502. /*
  503. * apptFromListEntry
  504. *
  505. * Returns the full appointment based on the text of the appointment as
  506. * given in the text entry from the scrolled list of appointments.
  507. */
  508. static Appointment*
  509. apptFromListEntry(
  510. XmString listEntry)
  511. {
  512. int ii;
  513. char *entryText,
  514. *string;
  515. /*
  516. * Get text string from XmString for use in comparisons
  517. */
  518. entryText = XmStringUnparse(listEntry, NULL, XmCHARSET_TEXT,
  519. XmCHARSET_TEXT, NULL, 0, XmOUTPUT_ALL);
  520. for (ii = 0; todaysApptList[ii].what != NULL; ii++) {
  521. string = strstr(entryText, todaysApptList[ii].what);
  522. if (string != NULL) {
  523. XtFree(entryText);
  524. return(&todaysApptList[ii]);
  525. }
  526. }
  527. XtFree(entryText);
  528. return NULL;
  529. }
  530. /*
  531. * apptGetLabel
  532. *
  533. * Creates a label for an appointment icon given an appointment.
  534. */
  535. static char*
  536. apptGetLabel(
  537. char *appt)
  538. {
  539. char start[128];
  540. int count;
  541. if (appt == NULL) {
  542. return NULL;
  543. }
  544. appt = strstr(appt, "Start:");
  545. count = sscanf(appt, "Start:%*[ \t]%[^\n]", start);
  546. if (count != 1) {
  547. return NULL;
  548. }
  549. return XtNewString(start);
  550. }