]> Pileus Git - ~andy/gtk/blob - gdk/gdkselection.c
Fix a reference to window_private->destroyed.
[~andy/gtk] / gdk / gdkselection.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 "gdkx.h"
35
36
37 gint
38 gdk_selection_owner_set (GdkWindow *owner,
39                          GdkAtom    selection,
40                          guint32    time,
41                          gint       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, gint format, 
191                              guchar *text, gint length,
192                              gchar ***list)
193 {
194   XTextProperty property;
195   gint count = 0;
196   gint res;
197
198   if (!list) 
199     return 0;
200
201   property.value = text;
202   property.encoding = encoding;
203   property.format = format;
204   property.nitems = length;
205   res = XmbTextPropertyToTextList (GDK_DISPLAY(), &property, list, &count);
206
207   if (res == XNoMemory || res == XLocaleNotSupported || 
208       res == XConverterNotFound)
209     return 0;
210   else
211     return count;
212 }
213
214 void
215 gdk_free_text_list (gchar **list)
216 {
217   g_return_if_fail (list != NULL);
218
219   XFreeStringList (list);
220 }
221
222 gint
223 gdk_string_to_compound_text (gchar *str,
224                              GdkAtom *encoding, gint *format,
225                              guchar **ctext, gint *length)
226 {
227   gint res;
228   XTextProperty property;
229
230   res = XmbTextListToTextProperty (GDK_DISPLAY(), 
231                                    &str, 1, XCompoundTextStyle,
232                                    &property);
233   if (res != Success)
234     {
235       property.encoding = None;
236       property.format = None;
237       property.value = NULL;
238       property.nitems = 0;
239     }
240
241   if (encoding)
242     *encoding = property.encoding;
243   if (format)
244     *format = property.format;
245   if (ctext)
246     *ctext = property.value;
247   if (length)
248     *length = property.nitems;
249
250   return res;
251 }
252
253 void gdk_free_compound_text (guchar *ctext)
254 {
255   if (ctext)
256     XFree (ctext);
257 }