CutPaste.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright 1989, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included
  9. in all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  11. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  13. IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
  14. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  15. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  16. OTHER DEALINGS IN THE SOFTWARE.
  17. Except as contained in this notice, the name of The Open Group shall
  18. not be used in advertising or otherwise to promote the sale, use or
  19. other dealings in this Software without prior written authorization
  20. from The Open Group.
  21. */
  22. /*
  23. * Author: Davor Matic, MIT X Consortium
  24. */
  25. #include <X11/IntrinsicP.h>
  26. #include <X11/Xmu/StdSel.h>
  27. #include <X11/Xmu/Atoms.h>
  28. #include <X11/Xatom.h>
  29. #include "ScaleP.h" /* This file should be part of the Scale widget */
  30. #include "Scale.h"
  31. #include "CutPaste.h"
  32. #include <stdio.h>
  33. /*ARGSUSED*/
  34. static Boolean
  35. ConvertSelection(Widget w, Atom *selection, Atom *target, Atom *type,
  36. XtPointer *value, unsigned long *length, int *format)
  37. {
  38. Boolean success;
  39. if (*target == XA_PIXMAP || *target == XA_BITMAP) {
  40. ScaleWidget sw = (ScaleWidget) w;
  41. Pixmap *pixmap = (Pixmap *) XtMalloc(sizeof(Pixmap));
  42. *pixmap = XCreatePixmap(XtDisplay(w), XtWindow(w),
  43. sw->scale.image->width,
  44. sw->scale.image->height,
  45. sw->scale.image->depth);
  46. XPutImage(XtDisplay(w), *pixmap, sw->scale.gc, sw->scale.image,
  47. 0, 0, 0, 0, sw->scale.image->width, sw->scale.image->height);
  48. *type = XA_PIXMAP;
  49. *value = (XtPointer) pixmap;
  50. *length = 1;
  51. *format = 32;
  52. success = True;
  53. } else {
  54. /* Xt will always respond to selection requests for the TIMESTAMP
  55. target, so we can pass a bogus time to XmuConvertStandardSelection.
  56. In addition to the targets provided by XmuConvertStandardSelection,
  57. Xt converts MULTIPLE, and we convert PIXMAP and BITMAP.
  58. */
  59. success = XmuConvertStandardSelection(w, (Time)0, selection, target,
  60. type, (XPointer *)value, length,
  61. format);
  62. if (success && *target == XA_TARGETS(XtDisplay(w))) {
  63. Atom* tmp;
  64. tmp = (Atom *) XtRealloc(*value, (*length + 3) * sizeof(Atom));
  65. tmp[(*length)++] = XInternAtom(XtDisplay(w), "MULTIPLE", False);
  66. tmp[(*length)++] = XA_PIXMAP;
  67. tmp[(*length)++] = XA_BITMAP;
  68. *value = (XtPointer) tmp;
  69. }
  70. }
  71. return success;
  72. }
  73. void
  74. SWGrabSelection(Widget w, Time time)
  75. {
  76. (void) XtOwnSelection(w, XA_PRIMARY, time, ConvertSelection, NULL, NULL);
  77. }
  78. /*ARGSUSED*/
  79. static void
  80. SelectionCallback(Widget w, XtPointer client_data, Atom *selection,
  81. Atom *type, XtPointer value, unsigned long *length,
  82. int *format)
  83. {
  84. if (*type == XA_PIXMAP) {
  85. Pixmap *pixmap;
  86. XImage *image;
  87. Window root;
  88. int x, y;
  89. unsigned int width, height, border_width, depth;
  90. pixmap = (Pixmap *) value;
  91. XGetGeometry(XtDisplay(w), *pixmap, &root, &x, &y,
  92. &width, &height, &border_width, &depth);
  93. image = XGetImage(XtDisplay(w), *pixmap, 0, 0, width, height,
  94. AllPlanes, ZPixmap);
  95. SWAutoscale(w, NULL, NULL, NULL);
  96. SWSetImage(w, image);
  97. XtFree(value);
  98. XDestroyImage(image);
  99. }
  100. }
  101. void
  102. SWRequestSelection(Widget w, Time time)
  103. {
  104. XtGetSelectionValue(w, XA_PRIMARY, XA_PIXMAP, SelectionCallback, NULL,
  105. time);
  106. }