Copy-paste operations in X

Copy-paste operations in X

Post by Philip Van Hoo » Thu, 12 Apr 2001 05:06:53



Hi there,

I am trying to make an application that needs the
information that is in the copypaste buffer of X
(the selection). Using the examples of this little
document I can already do all this but only using
pure X apps. So not when the app uses gtk or KDE
libraries.. Also browsers like netscape and mozilla
dont work with these examples..
--> http://www.enfin.com/getweb/X-copy+paste.txt
So, These examples will printf() the selected text..
but only when it's a pure X application like xterm.

My question..
Is there a general way.. a solution that can works
with all Windowmanagers.. Or what are the solutions
for Gtk clients? What for KDE ? This is all so
confusing.. why isn't there a standard defined for
this? If this is where Linux/unix/posix development
is heading to.. then we are all doomed since not ONE
single desktop programmer wants to learn
specifications for ALL managers and librabries..

However.. I belive that there is a solution for this
since all gtk and kde applications/objects can get the
selection (copypaste buffer) from other applications.
Only..it seems to be that the code to do this is
hidden somewhere in the gtk and qt libs .. and noway
that I am planning to dig out 100 000 rules of code
in gtk+ just to know how to do this manually :-(

My primary target is to 'get' the selected text.
Once that works I also want to 'set' the buffer..
I think this can't be hard because you can 'select'
text in objects like the gtk_entry and gtk_text...
Well I think you can.. again "hidden" and hard to
find documentation about this... :(

-
--
Philip van Hoof aka freax (http://www.freax.eu.org)

 
 
 

Copy-paste operations in X

Post by Philip Van Hoo » Thu, 12 Apr 2001 08:52:25


This is some information that I've found myself
so far I havn't got it working..

http://developer.gnome.org/doc/API/gtk/gtk-selections.html
http://developer.gnome.org/doc/API/2.0/gtk/gtk-clipboards.html

Using this

....
   GtkClipboard *clipb;

   clipb = gtk_clipboard_get(GDK_NONE); /* GDK_SELECTION_PRIMARY */
   g_warning("text %s", gtk_clipboard_wait_for_text(clipb));
....

I get


gcc -DDEBUG -DHAVE_CONFIG_H -I. -I. -I.. -I../intl      -I/usr/lib/glib/include -I/usr/X11R6/include    -g -O2 -Wall -c callbacks.c
In file included from callbacks.c:11:
netcode.c: In function `get_string':
netcode.c:29: warning: function returns address of local variable
callbacks.c: In function `on_button_set_clicked':
callbacks.c:21: `GtkClipboard' undeclared (first use in this function)
callbacks.c:21: (Each undeclared identifier is reported only once
callbacks.c:21: for each function it appears in.)
callbacks.c:21: `clip' undeclared (first use in this function)
callbacks.c:21: warning: statement with no effect
callbacks.c:42: `clipb' undeclared (first use in this function)
callbacks.c:42: warning: implicit declaration of function `gtk_clipboard_get'
callbacks.c:43: warning: implicit declaration of function `gtk_clipboard_wait_for_text'
callbacks.c:43: warning: format argument is not a pointer (arg 4)
callbacks.c:20: warning: unused variable `selection'
make: *** [callbacks.o] Error 1

btw .. I am indeed using glade.. and I am calling this code
in a file which contains

#include <gtk/gtk.h>

in a function called

void
on_button_set_clicked                  (GtkButton       *button,
                                        gpointer         user_data)
{

...

Quote:}

The application compiled (without this code of course) and it's
a working gtk application. So yes :) I did not forget includes
and etc etc..

--
Philip van Hoof aka freax (http://www.freax.eu.org)


 
 
 

Copy-paste operations in X

Post by Philip Van Hoo » Sun, 15 Apr 2001 01:44:20


Alright.. it's a bit more compilated then just getting it from a buffer.
You must tell an object to convert a specific atom to another one, and
then an event listner called selection recived will recive the data in a
GtkSelectionData widget where GtkSelectionData->data is the raw data.

In this example the name of the button that gets the selection is
GetClip and the name of the GtkText is text_in

Pushing text into the buffer is boing done by telling the button that
he is the owner of the selection.. a very good example can be found in
chapter 20 of the gtk tutorial on http://www.gtk.org .. the example about
getting the information of the selection will only display the available
atom names of the selection .. changing the target into 'TEXT" and
g_print()'ing data->data is all you have to do to get the raw data..

 GtkWidget *button_GetClip;

/* the Callbacks of our button */

void on_button_GetClip_clicked (
   GtkButton       *button,
   gpointer         user_data) {    

  static GdkAtom targets_atom = GDK_NONE;

  if (targets_atom == GDK_NONE)
/* Convert it to a TEXT atom since we want our
   data as TEXT */
  targets_atom = gdk_atom_intern ("TEXT", FALSE);
  gtk_selection_convert (button, GDK_SELECTION_PRIMARY,
                         targets_atom,
                         GDK_CURRENT_TIME);

Quote:}

void on_button_GetClip_selection_received (
    GtkWidget       *widget,  
    GtkSelectionData *data,
    guint            time,
    gpointer         user_data) {                                      

  if (data->length > 0) {
        g_print("%s", data->data);
  }                        

Quote:}

/* Creating a button */

void create_a_button...() {

 button_GetClip = gtk_button_new_with_label (_("Get the real Clipboard"));
 gtk_widget_ref (button_GetClip);
 gtk_object_set_data_full (GTK_OBJECT (main_win), "button_GetClip", button_GetClip,
                            (GtkDestroyNotify) gtk_widget_unref);
 gtk_widget_show (button_GetClip);

 gtk_signal_connect (GTK_OBJECT (button_GetClip), "clicked",
                      GTK_SIGNAL_FUNC (on_button_GetClip_clicked),
                      NULL);
  gtk_signal_connect (GTK_OBJECT (button_GetClip), "selection_received",
                      GTK_SIGNAL_FUNC (on_button_GetClip_selection_received),
                      NULL);
  gtk_signal_connect (GTK_OBJECT (button_GetClip), "selection_get",
                      GTK_SIGNAL_FUNC (on_button_GetClip_selection_get),
                      NULL);

/* Adding some targets */

  gtk_selection_add_target (button_GetClip,
                            GDK_SELECTION_PRIMARY,
                            GDK_SELECTION_TYPE_STRING,
                            1);

Quote:}

--
Philip van Hoof aka freax (http://www.freax.eu.org)

 
 
 

Copy-paste operations in X

Post by Teèé á?e?êè » Sun, 15 Apr 2001 03:25:27




Quote:> Hi there,

> I am trying to make an application that needs the
> information that is in the copypaste buffer of X
> (the selection). Using the examples of this little
> document I can already do all this but only using
> pure X apps. So not when the app uses gtk or KDE
> libraries.. Also browsers like netscape and mozilla
> dont work with these examples..

Netscape is one of Motif based applications and therefore may use
motif style processing of selection, different in text format.

To exchange data using cut/paste you should know: 1) what buffer
holds data (PRIMARY/SECONDARY/CLIPBOARD/ANY ANOTHER) and 2) in
what formats it is available. Many applications can send you (and receive
from you) data
in several formats (XA_STRING,XA_TEXT...), while others may handle
only one.  Besides, current data may be basically non-convertable to
readable
text (image,audio,man page:).

You may query current owner of clipboard for available formats
for current data and request the one you want - if it's on the list :-)

Hope this helps
        Yuri Borsky

 
 
 

1. Copy-paste operations in X

his is some information that I've found myself
so far I havn't got it working..

http://developer.gnome.org/doc/API/gtk/gtk-selections.html
http://developer.gnome.org/doc/API/2.0/gtk/gtk-clipboards.html

Using this

....
   GtkClipboard *clipb;

   clipb = gtk_clipboard_get(GDK_NONE); /* GDK_SELECTION_PRIMARY */
   g_warning("text %s", gtk_clipboard_wait_for_text(clipb));
....

I get


gcc -DDEBUG -DHAVE_CONFIG_H -I. -I. -I.. -I../intl      -I/usr/lib/glib/include -I/usr/X11R6/include    -g -O2 -Wall -c callbacks.c
In file included from callbacks.c:11:
netcode.c: In function `get_string':
netcode.c:29: warning: function returns address of local variable
callbacks.c: In function `on_button_set_clicked':
callbacks.c:21: `GtkClipboard' undeclared (first use in this function)
callbacks.c:21: (Each undeclared identifier is reported only once
callbacks.c:21: for each function it appears in.)
callbacks.c:21: `clip' undeclared (first use in this function)
callbacks.c:21: warning: statement with no effect
callbacks.c:42: `clipb' undeclared (first use in this function)
callbacks.c:42: warning: implicit declaration of function `gtk_clipboard_get'
callbacks.c:43: warning: implicit declaration of function `gtk_clipboard_wait_for_text'
callbacks.c:43: warning: format argument is not a pointer (arg 4)
callbacks.c:20: warning: unused variable `selection'
make: *** [callbacks.o] Error 1

btw .. I am indeed using glade.. and I am calling this code
in a file which contains

#include <gtk/gtk.h>

in a function called

void
on_button_set_clicked                  (GtkButton       *button,
                                        gpointer         user_data)
{

...

The application compiled (without this code of course) and it's
a working gtk application. So yes :) I did not forget includes
and etc etc..

--
Philip van Hoof aka freax (http://www.freax.eu.org)

2. Routing X video back to my Linux Machine

3. Sun-compatible HW closeouts

4. Help required to fix copy-paste problem

5. Linux and Colorado Trakker Tape?

6. Copy-Paste between KWrite and Netscape

7. Ovislink ether PCMCIA card driver HELP

8. Using 'Copy-Paste' in GUI help

9. GPL and copy-paste

10. copy-paste on PowerBook

11. copy-paste

12. COPY-PASTE == `head -c 1k oldfile > newfile` !?