Thread: Clipboard

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    198

    Clipboard

    How can I read and write blocks of plain text into the clipboard?

    If there is no easy cross-platform way of doing it, it must be specific to *nix systems.

    I tried searching the Web but all the results are in C++/C# and Windows-specific.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Well, two of the more popular Desktop environments (KDE, and Gnome) for *nix systems, use their own clipboard system, klipper, and Gnome clipboard manager, respectively. Of course, if you are running one of these DE's, then you are also running X, and therefore you also have xclipboard available for use as well. That should get you started anyway.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    I guess that xclipboard would be better because it it would be desktop manager-independent. But would it be able to copy and paste with my GNOME applications?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MTK View Post
    I guess that xclipboard would be better because it it would be desktop manager-independent. But would it be able to copy and paste with my GNOME applications?
    GNOME uses gtk, which is (I think) the stuff abachler refers to is, altho I don't have a DE installed so can't confirm that.
    Code:
    void copytoX (GtkWidget *ignored, int opt) {
            GdkDisplay *display = gdk_display_get_default();
            GtkClipboard *clipboard = gtk_clipboard_get_for_display(display,GDK_SELECTION_CLIPBOARD);
            if (opt) { gtk_text_buffer_get_bounds(Tbuf,&start,&finish);
                    gtk_text_buffer_select_range(Tbuf,(const GtkTextIter*)&start,(const GtkTextIter*)&finish); }
            gtk_text_buffer_copy_clipboard(Tbuf,clipboard); 
    }
    This is the xclipboard; if you use xclipboard you can check the contents, they are the same. There is, in fact, another clipboard which does not rely on the GUI or the use of cut n' paste in a menu. I think many linux users may be unaware of it, but if you simply highlight some text somewhere, leave it highlighted, then go somewhere else and click button 3/both buttons, you will get the contents of the highlighted region. However, that text is not in the xclipboard. I have never investigated how to program it, but I make use of this a lot (the fact that you can have two different clipboard buffers and access them with different methods), but the uncommon one (no GUI interaction using mouse button 3) is not consistent, and that is not what you want. Sorry if that is unnecessarily confusing.

    Anyway, I think the GNOME clipboard viewer is just a "nicer" GUI for xclipboard, it is not really separate, so don't worry -- it is all, in fact, the xclipboard. Which presumably you can access it with Xlib() as well as gtk, and I suppose any other GUI toolkit. But they will all be using the same clipboard, the xclipboard.
    Last edited by MK27; 09-07-2009 at 10:09 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copying to clipboard
    By bballzone in forum Windows Programming
    Replies: 24
    Last Post: 09-30-2004, 03:24 PM
  2. Clipboard and Custom Types
    By McClamm in forum C# Programming
    Replies: 1
    Last Post: 09-16-2004, 04:43 PM
  3. Clipboard Modifier
    By Korhedron in forum Windows Programming
    Replies: 2
    Last Post: 01-03-2004, 02:32 PM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. OLE Clipboard :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 08-11-2002, 05:57 PM