home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / clients / xclipboard / xcutsel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-17  |  7.8 KB  |  306 lines

  1. /*
  2.  * $XConsortium: xcutsel.c,v 1.15 91/02/17 12:05:27 dave Exp $
  3.  *
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for any purpose and without fee is hereby granted, provided
  8.  * that the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising
  11.  * or publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Ralph Swick, DEC/Project Athena
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <X11/Intrinsic.h>
  28. #include <X11/StringDefs.h>
  29. #include <X11/Xatom.h>
  30.  
  31. #include <X11/Xmu/Atoms.h>
  32. #include <X11/Xmu/StdSel.h>
  33.  
  34. #include <X11/Xaw/Command.h>
  35. #include <X11/Xaw/Box.h>
  36. #include <X11/Xaw/Cardinals.h>
  37. #include <X11/Xfuncs.h>
  38.  
  39. static XrmOptionDescRec optionDesc[] = {
  40.     {"-selection", "selection",    XrmoptionSepArg, NULL},
  41.     {"-select",    "selection",    XrmoptionSepArg, NULL},
  42.     {"-sel",       "selection",    XrmoptionSepArg, NULL},
  43.     {"-s",       "selection",    XrmoptionSepArg, NULL},
  44.     {"-cutbuffer", "cutBuffer",    XrmoptionSepArg, NULL},
  45. };
  46.  
  47. typedef struct {
  48.     String  selection_name;
  49.     int        buffer;
  50.     Atom    selection;
  51.     char*   value;
  52.     int     length;
  53. } OptionsRec;
  54.  
  55. OptionsRec options;
  56.  
  57. #define Offset(field) XtOffsetOf(OptionsRec, field)
  58.  
  59. static XtResource resources[] = {
  60.     {"selection", "Selection", XtRString, sizeof(String),
  61.        Offset(selection_name), XtRString, "PRIMARY"},
  62.     {"cutBuffer", "CutBuffer", XtRInt, sizeof(int),
  63.        Offset(buffer), XtRImmediate, (caddr_t)0},
  64. };
  65.  
  66. #undef Offset
  67.  
  68. typedef struct {
  69.     Widget button;
  70.     Boolean is_on;
  71. } ButtonState;
  72.  
  73. static ButtonState state;
  74.  
  75. Syntax(call)
  76.     char *call;
  77. {
  78.     fprintf (stderr, "usage:  %s [-selection name] [-cutbuffer number]\n", 
  79.          call);
  80.     exit (1);
  81. }
  82.  
  83.  
  84. static void StoreBuffer(w, client_data, selection, type, value, length, format)
  85.     Widget w;
  86.     XtPointer client_data;
  87.     Atom *selection, *type;
  88.     XtPointer value;
  89.     unsigned long *length;
  90.     int *format;
  91. {
  92.  
  93.     if (*type == 0 || *type == XT_CONVERT_FAIL || *length == 0) {
  94.     XBell( XtDisplay(w), 0 );
  95.     return;
  96.     }
  97.  
  98.     XStoreBuffer( XtDisplay(w), (char*)value, (int)(*length),
  99.           options.buffer );
  100.    
  101.     XtFree(value);
  102. }
  103.  
  104.  
  105. static Boolean ConvertSelection(w, selection, target,
  106.                 type, value, length, format)
  107.     Widget w;
  108.     Atom *selection, *target, *type;
  109.     XtPointer *value;
  110.     unsigned long *length;
  111.     int *format;
  112. {
  113.     Display* d = XtDisplay(w);
  114.     XSelectionRequestEvent* req =
  115.     XtGetSelectionRequest(w, *selection, (XtRequestId)NULL);
  116.     
  117.     if (*target == XA_TARGETS(d)) {
  118.     Atom* targetP;
  119.     Atom* std_targets;
  120.     unsigned long std_length;
  121.     XmuConvertStandardSelection(w, req->time, selection, target, type,
  122.                    (caddr_t*)&std_targets, &std_length, format);
  123.     *value = XtMalloc(sizeof(Atom)*(std_length + 4));
  124.     targetP = *(Atom**)value;
  125.     *length = std_length + 4;
  126.     *targetP++ = XA_STRING;
  127.     *targetP++ = XA_TEXT(d);
  128.     *targetP++ = XA_LENGTH(d);
  129.     *targetP++ = XA_LIST_LENGTH(d);
  130. /*
  131.     *targetP++ = XA_CHARACTER_POSITION(d);
  132. */
  133.     bcopy((char*)std_targets, (char*)targetP, sizeof(Atom)*std_length);
  134.     XtFree((char*)std_targets);
  135.     *type = XA_ATOM;
  136.     *format = 32;
  137.     return True;
  138.     }
  139.     if (*target == XA_STRING || *target == XA_TEXT(d)) {
  140.     *type = XA_STRING;
  141.     *value = options.value;
  142.     *length = options.length;
  143.     *format = 8;
  144.     return True;
  145.     }
  146.     if (*target == XA_LIST_LENGTH(d)) {
  147.     long *temp = (long *) XtMalloc (sizeof(long));
  148.     *temp = 1L;
  149.     *value = (caddr_t) temp;
  150.     *type = XA_INTEGER;
  151.     *length = 1;
  152.     *format = 32;
  153.     return True;
  154.     }
  155.     if (*target == XA_LENGTH(d)) {
  156.     long *temp = (long *) XtMalloc (sizeof(long));
  157.     *temp = options.length;
  158.     *value = (caddr_t) temp;
  159.     *type = XA_INTEGER;
  160.     *length = 1;
  161.     *format = 32;
  162.     return True;
  163.     }
  164. #ifdef notdef
  165.     if (*target == XA_CHARACTER_POSITION(d)) {
  166.     long *temp = (long *) XtMalloc (2 * sizeof(long));
  167.     temp[0] = ctx->text.s.left + 1;
  168.     temp[1] = ctx->text.s.right;
  169.     *value = (caddr_t) temp;
  170.     *type = XA_SPAN(d);
  171.     *length = 2;
  172.     *format = 32;
  173.     return True;
  174.     }
  175. #endif /* notdef */
  176.     if (XmuConvertStandardSelection(w, req->time, selection, target, type,
  177.                     (caddr_t *)value, length, format))
  178.     return True;
  179.  
  180.     /* else */
  181.     return False;
  182. }
  183.  
  184.  
  185. static void SetButton(state, on)
  186.     ButtonState *state;
  187.     Boolean on;
  188. {
  189.     if (state->is_on != on) {
  190.     Arg args[2];
  191.     Pixel fg, bg;
  192.     XtSetArg( args[0], XtNforeground, &fg );
  193.     XtSetArg( args[1], XtNbackground, &bg );
  194.     XtGetValues( state->button, args, TWO );
  195.     args[0].value = (XtArgVal)bg;
  196.     args[1].value = (XtArgVal)fg;
  197.     XtSetValues( state->button, args, TWO );
  198.     state->is_on = on;
  199.     }
  200. }
  201.  
  202.  
  203. static void LoseSelection(w, selection)
  204.     Widget w;
  205.     Atom *selection;
  206. {
  207.     XtFree( options.value );
  208.     options.value = NULL;
  209.     SetButton(&state, False);
  210. }
  211.  
  212.  
  213. /* ARGSUSED */
  214. static void Quit(w, closure, callData)
  215.     Widget w;
  216.     XtPointer closure;        /* unused */
  217.     XtPointer callData;        /* unused */
  218. {
  219.     XtCloseDisplay( XtDisplay(w) );
  220.     exit(0);
  221. }
  222.  
  223.  
  224. /* ARGSUSED */
  225. static void GetSelection(w, closure, callData)
  226.     Widget w;
  227.     XtPointer closure;        /* unused */
  228.     XtPointer callData;        /* unused */
  229. {
  230.     XtGetSelectionValue(w, options.selection, XA_STRING,
  231.             StoreBuffer, NULL,
  232.             XtLastTimestampProcessed(XtDisplay(w)));
  233. }
  234.  
  235.  
  236. /* ARGSUSED */
  237. static void GetBuffer(w, closure, callData)
  238.     Widget w;
  239.     XtPointer closure;
  240.     XtPointer callData;        /* unused */
  241. {
  242.     XtFree( options.value );
  243.     options.value =
  244.     XFetchBuffer(XtDisplay(w), &options.length, options.buffer);
  245.     if (options.value != NULL) {
  246.     if (XtOwnSelection(w, options.selection,
  247.                XtLastTimestampProcessed(XtDisplay(w)),
  248.                ConvertSelection, LoseSelection, NULL))
  249.         SetButton((ButtonState*)closure, True);
  250.     }
  251. }
  252.  
  253.  
  254. void main(argc, argv)
  255.     int argc;
  256.     char *argv[];
  257. {
  258.     char label[100];
  259.     Widget box, button;
  260.     XtAppContext appcon;
  261.     Widget shell =
  262.     XtAppInitialize( &appcon, "XCutsel", optionDesc, XtNumber(optionDesc),
  263.              &argc, argv, NULL, NULL, 0 );
  264.     XrmDatabase rdb = XtDatabase(XtDisplay(shell));
  265.  
  266.     if (argc != 1) Syntax(argv[0]);
  267.  
  268.     XtGetApplicationResources( shell, (caddr_t)&options,
  269.                    resources, XtNumber(resources),
  270.                    NULL, ZERO );
  271.  
  272.     options.value = NULL;
  273.     XmuInternStrings( XtDisplay(shell), &options.selection_name, ONE,
  274.               &options.selection );
  275.  
  276.     box = XtCreateManagedWidget("box", boxWidgetClass, shell, NULL, ZERO);
  277.  
  278.     button =
  279.     XtCreateManagedWidget("quit", commandWidgetClass, box, NULL, ZERO);
  280.     XtAddCallback( button, XtNcallback, Quit, NULL );
  281.  
  282.     /* %%% hack alert... */
  283.     sprintf(label, "*label:copy %s to %d",
  284.         options.selection_name,
  285.         options.buffer);
  286.     XrmPutLineResource( &rdb, label );
  287.  
  288.     button =
  289.     XtCreateManagedWidget("sel-cut", commandWidgetClass, box, NULL, ZERO);
  290.     XtAddCallback( button, XtNcallback, GetSelection, NULL );
  291.  
  292.     sprintf(label, "*label:copy %d to %s",
  293.         options.buffer,
  294.         options.selection_name);
  295.     XrmPutLineResource( &rdb, label );
  296.  
  297.     button =
  298.     XtCreateManagedWidget("cut-sel", commandWidgetClass, box, NULL, ZERO);
  299.     XtAddCallback( button, XtNcallback, GetBuffer, (XtPointer)&state );
  300.      state.button = button;
  301.     state.is_on = False;
  302.    
  303.     XtRealizeWidget(shell);
  304.     XtAppMainLoop(appcon);
  305. }
  306.