home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / m / master12.zip / mastering / buttondnd.c < prev    next >
C/C++ Source or Header  |  1992-08-18  |  5KB  |  177 lines

  1. /*  include files  */
  2.  
  3. #include <stdio.h>
  4. #include <Xm/PushB.h>
  5. #include <Xm/BulletinB.h>
  6. #include <Xm/Form.h>
  7. #include <Xm/DragDrop.h>
  8.  
  9. /*  Functions defined in this program  */
  10.  
  11. void main();
  12. void activateCB();    /* Callback for the pushbutton */
  13. void TransferData();  /* Transfers information from initiator */ 
  14. void ProcessDrop();    /* Handles the drop */
  15.  
  16. /*  Global variables  */
  17.  
  18. XmString       btn_text;  /* button label pointer for compound string */
  19. Atom           COMPOUND_TEXT;
  20.  
  21. /*-------------------------------------------------------------
  22. **  TransferData
  23. *
  24. * This routine transfers information from the initiator */
  25.  
  26. static void TransferData(w, client_data, selection, type, value, length,
  27.                                  format) 
  28. Widget           w;
  29. XtPointer        client_data;
  30. Atom            *selection;
  31. Atom            *type;
  32. XtPointer        value;
  33. unsigned long   *length;
  34. int             *format;
  35.  
  36. {
  37. int         n;
  38. Arg         args[10];
  39.  
  40. /* Information from the drag initiator is passed in compound
  41.  * text format.  Convert it to compound string and replace the button
  42.  * label.   */
  43.  
  44.   if (*type == COMPOUND_TEXT) {
  45.      n = 0;
  46.      XtSetArg(args[n], XmNlabelString, XmCvtCTToXmString(value)); n++;
  47.      XtSetValues(client_data, args, n);
  48.    } 
  49. }
  50.  
  51. /*-------------------------------------------------------------
  52. **  ProcessDrop
  53. *
  54. * This routine is performed when a drop is made.  It decides what 
  55. information it wants and uses TransferData to transfer the data
  56. from the initiator */
  57.  
  58. static void ProcessDrop(w, client_data, call_data)
  59. Widget          w;
  60. XtPointer       client_data, call_data;
  61. {
  62.  
  63. XmDropProcCallback      DropData;
  64. XmDropTransferEntryRec  transferEntries[1];
  65. XmDropTransferEntry     transferList;
  66. Arg                     args[10];
  67. int                     n;
  68.  
  69.  DropData = (XmDropProcCallback)call_data; 
  70.  
  71. /* set the transfer resources */
  72.  n = 0; 
  73.  
  74. /* if the action is not Drop or the operation is not Copy,
  75.  * cancel the drop */
  76.  
  77.  if ((DropData->dropAction != XmDROP) ||
  78.      (DropData->operation !=  XmDROP_COPY)) {
  79.  
  80.      XtSetArg(args[n], XmNtransferStatus, XmTRANSFER_FAILURE); n++;
  81.  }
  82.  else {
  83.      transferEntries[0].target = COMPOUND_TEXT;
  84.      transferEntries[0].client_data = (XtPointer)w;
  85.      transferList = transferEntries;
  86.      XtSetArg(args[n], XmNdropTransfers, transferList); n++;
  87.      XtSetArg(args[n], XmNnumDropTransfers, 1); n++;
  88.      XtSetArg(args[n], XmNtransferProc, TransferData); n++;
  89.  }
  90.  
  91. /* start the transfer or cancel */ 
  92.  XmDropTransferStart(DropData->dragContext, args, n);
  93. }
  94.  
  95. /*-------------------------------------------------------------
  96. **  activateCB - callback for button
  97. */
  98. void activateCB (w, client_data, call_data) 
  99.   Widget   w;            /*  widget id              */
  100.   XtPointer   client_data;  /*  data from application  */
  101.   XtPointer   call_data;    /*  data from widget class */
  102. {
  103. /*  Print message and terminate program  */
  104.   printf ("Pushbutton Activated.\n");
  105.   exit (0);
  106. }
  107.  
  108. /*-------------------------------------------------------------
  109. **  main - Main logic for xmbutton
  110. */
  111. void main (argc,argv)
  112. unsigned int argc;
  113. char **argv;
  114. {
  115.   Widget       toplevel;       /*  Shell widget          */
  116.   Widget       bboard;         /*  Bulletin board widget */
  117.   Widget       form1;          /*  Form widget           */
  118.   Widget       button;         /*  Pushbutton widget     */
  119.   Arg          args[10];       /*  arg list              */
  120.   register     int n;          /*  arg count             */
  121.   XtAppContext app_context;    /*  application context   */
  122.   Atom         import_list[1]; /*  compound text         */
  123.  
  124. /*  Initialize the toolkit, create application context, open display, 
  125.     and create a toplevel shell */
  126.   toplevel = XtAppInitialize (&app_context, "Buttondnd", NULL, 0, &argc,
  127.                               argv, NULL, args, 0);
  128.  
  129. /*  Create a bulletin board widget in which to place the pushbutton 
  130.   n = 0;
  131.   bboard = XmCreateBulletinBoard (toplevel, "bboard", args, n); */
  132.  
  133.   n = 0;
  134.   form1 = XmCreateForm (toplevel, "form1", args, n); 
  135.  
  136. /*  Manage the bulletin board widget */
  137.   XtManageChild (form1);
  138.  
  139. /*  Create a compound string for the button label  */
  140.   btn_text = XmStringCreateLtoR("Push Here", XmSTRING_DEFAULT_CHARSET);
  141.  
  142. /*  Set up an arglist for the pushbutton */
  143.   n = 0;
  144.   XtSetArg(args[n], XmNlabelString, btn_text); n++;
  145.  
  146. /*  Create the push button */
  147.   button = XtCreateManagedWidget ("button", xmPushButtonWidgetClass, form1,
  148.   args, n); 
  149.  
  150. /*  Create compound text for the button label  */
  151.   COMPOUND_TEXT = XmInternAtom(XtDisplay(toplevel), "COMPOUND_TEXT", False);
  152.   
  153. /* register the button as a drop site */
  154.   import_list[0] = COMPOUND_TEXT;
  155.  
  156.   n = 0;
  157.   XtSetArg(args[n], XmNimportTargets, import_list); n++;
  158.   XtSetArg(args[n], XmNnumImportTargets, 1); n++;
  159.   XtSetArg(args[n], XmNdropSiteOperations, XmDROP_COPY); n++;
  160.   XtSetArg(args[n], XmNdropProc, ProcessDrop); n++;
  161.   XmDropSiteRegister(button, args, n);
  162.  
  163. /*  Free the compound string memory */
  164.   XmStringFree (btn_text);
  165.  
  166. /*  Add a callback  */
  167.   XtAddCallback (button, XmNactivateCallback, activateCB, NULL);
  168.  
  169. /*  Realize widgets  */
  170.   XtRealizeWidget (toplevel);
  171.  
  172. /*  Process events  */
  173.   XtAppMainLoop (app_context);
  174. }
  175.  
  176.  
  177.