]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkselection-x11.c
31a6eb0b912f484702707a3e58163efb7c64f66d
[~andy/gtk] / gdk / x11 / gdkselection-x11.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include <X11/Xlib.h>
28 #include <X11/Xatom.h>
29 #include <string.h>
30
31 #include "gdkproperty.h"
32 #include "gdkselection.h"
33 #include "gdkprivate.h"
34 #include "gdkprivate-x11.h"
35
36
37 gboolean
38 gdk_selection_owner_set (GdkWindow *owner,
39                          GdkAtom    selection,
40                          guint32    time,
41                          gboolean   send_event)
42 {
43   Display *xdisplay;
44   Window xwindow;
45
46   if (owner)
47     {
48       if (GDK_DRAWABLE_DESTROYED (owner))
49         return FALSE;
50
51       xdisplay = GDK_DRAWABLE_XDISPLAY (owner);
52       xwindow = GDK_DRAWABLE_XID (owner);
53     }
54   else
55     {
56       xdisplay = gdk_display;
57       xwindow = None;
58     }
59
60   XSetSelectionOwner (xdisplay, selection, xwindow, time);
61
62   return (XGetSelectionOwner (xdisplay, selection) == xwindow);
63 }
64
65 GdkWindow*
66 gdk_selection_owner_get (GdkAtom selection)
67 {
68   Window xwindow;
69
70   xwindow = XGetSelectionOwner (gdk_display, selection);
71   if (xwindow == None)
72     return NULL;
73
74   return gdk_window_lookup (xwindow);
75 }
76
77 void
78 gdk_selection_convert (GdkWindow *requestor,
79                        GdkAtom    selection,
80                        GdkAtom    target,
81                        guint32    time)
82 {
83   if (GDK_DRAWABLE_DESTROYED (requestor))
84     return;
85
86   XConvertSelection (GDK_DRAWABLE_XDISPLAY (requestor), selection, target,
87                      gdk_selection_property, GDK_DRAWABLE_XID (requestor), time);
88 }
89
90 gint
91 gdk_selection_property_get (GdkWindow  *requestor,
92                             guchar    **data,
93                             GdkAtom    *ret_type,
94                             gint       *ret_format)
95 {
96   gulong nitems;
97   gulong nbytes;
98   gulong length;
99   GdkAtom prop_type;
100   gint prop_format;
101   guchar *t = NULL;
102
103   g_return_val_if_fail (requestor != NULL, 0);
104   g_return_val_if_fail (GDK_IS_WINDOW (requestor), 0);
105
106   /* If retrieved chunks are typically small, (and the ICCCM says the
107      should be) it would be a win to try first with a buffer of
108      moderate length, to avoid two round trips to the server */
109
110   if (GDK_DRAWABLE_DESTROYED (requestor))
111     return 0;
112
113   t = NULL;
114   XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (requestor),
115                       GDK_DRAWABLE_XID (requestor),
116                       gdk_selection_property, 0, 0, False,
117                       AnyPropertyType, &prop_type, &prop_format,
118                       &nitems, &nbytes, &t);
119
120   if (ret_type)
121     *ret_type = prop_type;
122   if (ret_format)
123     *ret_format = prop_format;
124
125   if (prop_type == None)
126     {
127       *data = NULL;
128       return 0;
129     }
130   
131   if (t)
132     {
133       XFree (t);
134       t = NULL;
135     }
136
137   /* Add on an extra byte to handle null termination.  X guarantees
138      that t will be 1 longer than nbytes and null terminated */
139   length = nbytes + 1;
140
141   /* We can't delete the selection here, because it might be the INCR
142      protocol, in which case the client has to make sure they'll be
143      notified of PropertyChange events _before_ the property is deleted.
144      Otherwise there's no guarantee we'll win the race ... */
145   XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (requestor),
146                       GDK_DRAWABLE_XID (requestor),
147                       gdk_selection_property, 0, (nbytes + 3) / 4, False,
148                       AnyPropertyType, &prop_type, &prop_format,
149                       &nitems, &nbytes, &t);
150
151   if (prop_type != None)
152     {
153       *data = g_new (guchar, length);
154       memcpy (*data, t, length);
155       if (t)
156         XFree (t);
157       return length-1;
158     }
159   else
160     {
161       *data = NULL;
162       return 0;
163     }
164 }
165
166
167 void
168 gdk_selection_send_notify (guint32  requestor,
169                            GdkAtom  selection,
170                            GdkAtom  target,
171                            GdkAtom  property,
172                            guint32  time)
173 {
174   XSelectionEvent xevent;
175
176   xevent.type = SelectionNotify;
177   xevent.serial = 0;
178   xevent.send_event = True;
179   xevent.display = gdk_display;
180   xevent.requestor = requestor;
181   xevent.selection = selection;
182   xevent.target = target;
183   xevent.property = property;
184   xevent.time = time;
185
186   gdk_send_xevent (requestor, False, NoEventMask, (XEvent*) &xevent);
187 }
188
189 gint
190 gdk_text_property_to_text_list (GdkAtom       encoding,
191                                 gint          format, 
192                                 const guchar *text,
193                                 gint          length,
194                                 gchar      ***list)
195 {
196   XTextProperty property;
197   gint count = 0;
198   gint res;
199
200   if (!list) 
201     return 0;
202
203   property.value = (guchar *)text;
204   property.encoding = encoding;
205   property.format = format;
206   property.nitems = length;
207   res = XmbTextPropertyToTextList (GDK_DISPLAY(), &property, list, &count);
208
209   if (res == XNoMemory || res == XLocaleNotSupported || 
210       res == XConverterNotFound)
211     return 0;
212   else
213     return count;
214 }
215
216 void
217 gdk_free_text_list (gchar **list)
218 {
219   g_return_if_fail (list != NULL);
220
221   XFreeStringList (list);
222 }
223
224 gint
225 gdk_string_to_compound_text (const gchar *str,
226                              GdkAtom     *encoding,
227                              gint        *format,
228                              guchar     **ctext,
229                              gint        *length)
230 {
231   gint res;
232   XTextProperty property;
233
234   res = XmbTextListToTextProperty (GDK_DISPLAY(), 
235                                    (char **)&str, 1, XCompoundTextStyle,
236                                    &property);
237   if (res != Success)
238     {
239       property.encoding = None;
240       property.format = None;
241       property.value = NULL;
242       property.nitems = 0;
243     }
244
245   if (encoding)
246     *encoding = property.encoding;
247   if (format)
248     *format = property.format;
249   if (ctext)
250     *ctext = property.value;
251   if (length)
252     *length = property.nitems;
253
254   return res;
255 }
256
257 void gdk_free_compound_text (guchar *ctext)
258 {
259   if (ctext)
260     XFree (ctext);
261 }