home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1999 March / PCWK3A99.iso / Linux / DDD331 / DDD-3_1_.000 / DDD-3_1_ / ddd-3.1.1 / ddd / selection.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-21  |  4.1 KB  |  170 lines

  1. // $Id: selection.C,v 1.1 1998/10/21 16:20:53 zeller Exp $ -*- C++ -*-
  2. // Return current clipboard selection
  3.  
  4. // Copyright (C) 1998 Technische Universitaet Braunschweig, Germany.
  5. // Written by Andreas Zeller <zeller@ips.cs.tu-bs.de>.
  6. // 
  7. // This file is part of DDD.
  8. // 
  9. // DDD is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. // 
  14. // DDD is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. // See the GNU General Public License for more details.
  18. // 
  19. // You should have received a copy of the GNU General Public
  20. // License along with DDD -- see the file COPYING.
  21. // If not, write to the Free Software Foundation, Inc.,
  22. // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. // 
  24. // DDD is the data display debugger.
  25. // For details, see the DDD World-Wide-Web page, 
  26. // `http://www.cs.tu-bs.de/softech/ddd/',
  27. // or send a mail to the DDD developers <ddd@ips.cs.tu-bs.de>.
  28.  
  29. char selection_rcsid[] = 
  30.     "$Id: selection.C,v 1.1 1998/10/21 16:20:53 zeller Exp $";
  31.  
  32. #ifdef __GNUG__
  33. #pragma implementation
  34. #endif
  35.  
  36. #include "selection.h"
  37. #include "bool.h"
  38.  
  39. #include <Xm/CutPaste.h>
  40.  
  41. // Return the current clipboard selection
  42. string current_clipboard(Widget w)
  43. {
  44.     Display *dpy = XtDisplay(w);
  45.     Window win = XtWindow(w);
  46.  
  47.     if (!XtIsRealized(w))
  48.     win = RootWindowOfScreen(XtScreen(w));
  49.  
  50.     int result;
  51.     do {
  52.     Time time = XtLastTimestampProcessed(dpy);
  53.     result = XmClipboardStartRetrieve(dpy, win, time);
  54.     } while (result == XmClipboardLocked);
  55.  
  56.     if (result != XmClipboardSuccess)
  57.     return "";
  58.  
  59.     // Figure out how much we need to paste
  60.     unsigned long len;
  61.     do {
  62.     result = XmClipboardInquireLength(dpy, win, "STRING", &len);
  63.     } while (result == XmClipboardLocked);
  64.  
  65.     if (result != XmClipboardSuccess)
  66.     return "";
  67.  
  68.     char *buf = XtMalloc(len + 1);
  69.     buf[0] = '\0';
  70.  
  71.     unsigned long nbytes;
  72. #if XmVersion >= 1002
  73.     long private_id;
  74. #else
  75.     int private_id;
  76. #endif
  77.     do {
  78.     result = XmClipboardRetrieve(dpy, win, "STRING",
  79.                      buf, len, &nbytes, &private_id);
  80.     } while (result == XmClipboardLocked);
  81.  
  82.     string selection(buf, len);
  83.     XtFree(buf);
  84.  
  85.     if (result != XmClipboardSuccess)
  86.         return "";
  87.  
  88.     do {
  89.     result = XmClipboardEndRetrieve(dpy, win);
  90.     } while (result == XmClipboardLocked);
  91.  
  92.     if (result != XmClipboardSuccess)
  93.     return "";
  94.  
  95.     return selection;
  96. }
  97.  
  98. static void GotSelection(Widget, XtPointer client_data, Atom *,
  99.              Atom *type, XtPointer value, unsigned long *length,
  100.              int *format)
  101. {
  102.     string& selection = *((string *)client_data);
  103.     selection = "";
  104.  
  105.     if (value == 0)
  106.     return;
  107.  
  108.     if (*type != XA_STRING || *format != 8)
  109.     return;
  110.  
  111.     selection = string((char *)value, *length);
  112. }
  113.  
  114. // Return current primary selection.
  115. string current_primary(Widget w)
  116. {
  117.     if (!XtIsRealized(w))
  118.     {
  119.     Arg args[10];
  120.     int arg = 0;
  121.     XtSetArg(args[arg], XmNwidth,  1); arg++;
  122.     XtSetArg(args[arg], XmNheight, 1); arg++;
  123.     Widget shell = XtCreatePopupShell("shell", overrideShellWidgetClass, 
  124.                       w, args, arg);
  125.     XtRealizeWidget(shell);
  126.     XtUnmapWidget(shell);
  127.  
  128.     string ret = current_primary(shell);
  129.  
  130.     XtDestroyWidget(shell);
  131.  
  132.     return ret;
  133.     }
  134.  
  135.     Time time = XtLastTimestampProcessed(XtDisplay(w));
  136.     string selection = char(-1);
  137.     XtGetSelectionValue(w, XA_PRIMARY, XA_STRING, GotSelection, 
  138.             XtPointer(&selection), time);
  139.  
  140.     while (selection == char(-1))
  141.     XtAppProcessEvent(XtWidgetToApplicationContext(w), XtIMAll);
  142.  
  143.     return selection;
  144. }
  145.  
  146. // Return current cut buffer selection.
  147. string current_cut_buffer(Widget w)
  148. {
  149.     int nbytes;
  150.     char *buf = XFetchBytes(XtDisplay(w), &nbytes);
  151.     string selection = buf;
  152.     XFree(buf);
  153.  
  154.     return selection;
  155. }
  156.  
  157. string current_selection(Widget w)
  158. {
  159.     string selection = "";
  160.  
  161.     if (selection == "")
  162.     selection = current_primary(w);
  163.     if (selection == "")
  164.     selection = current_cut_buffer(w);
  165.     if (selection == "")
  166.     selection = current_clipboard(w);
  167.  
  168.     return selection;
  169. }
  170.