]> Pileus Git - ~andy/gtk/blob - gdk/directfb/gdkselection-directfb.c
gdk/: fully remove gdkalias hacks
[~andy/gtk] / gdk / directfb / gdkselection-directfb.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 Lesser 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser 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-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.
23  */
24
25 /*
26  * GTK+ DirectFB backend
27  * Copyright (C) 2001-2002  convergence integrated media GmbH
28  * Copyright (C) 2002-2004  convergence GmbH
29  * Written by Denis Oliver Kropp <dok@convergence.de> and
30  *            Sven Neumann <sven@convergence.de>
31  */
32
33 #include "config.h"
34
35 #include <string.h>
36
37 #include "gdkdirectfb.h"
38 #include "gdkprivate-directfb.h"
39
40 #include "gdkproperty.h"
41 #include "gdkselection.h"
42 #include "gdkprivate.h"
43
44
45 typedef struct _OwnerInfo OwnerInfo;
46
47 struct _OwnerInfo
48 {
49   GdkAtom    selection;
50   GdkWindow *owner;
51 };
52
53 GSList *owner_list = NULL;
54
55 /* When a window is destroyed we check if it is the owner
56  * of any selections. This is somewhat inefficient, but
57  * owner_list is typically short, and it is a low memory,
58  * low code solution
59  */
60 void
61 _gdk_selection_window_destroyed (GdkWindow *window)
62 {
63   GSList *tmp_list = owner_list;
64
65   while (tmp_list)
66     {
67       OwnerInfo *info = tmp_list->data;
68
69       tmp_list = tmp_list->next;
70
71       if (info->owner == window)
72         {
73           owner_list = g_slist_remove (owner_list, info);
74           g_free (info);
75         }
76     }
77 }
78
79 gint
80 gdk_selection_owner_set_for_display (GdkDisplay *display,
81                                      GdkWindow  *owner,
82                                      GdkAtom     selection,
83                                      guint32     time,
84                                      gint        send_event)
85 {
86   GSList    *tmp_list;
87   OwnerInfo *info;
88
89   tmp_list = owner_list;
90   while (tmp_list)
91     {
92       info = tmp_list->data;
93       if (info->selection == selection)
94         {
95           owner_list = g_slist_remove (owner_list, info);
96           g_free (info);
97           break;
98         }
99       tmp_list = tmp_list->next;
100     }
101
102   if (owner)
103     {
104       info = g_new (OwnerInfo, 1);
105       info->owner = owner;
106       info->selection = selection;
107
108       owner_list = g_slist_prepend (owner_list, info);
109     }
110
111   return TRUE;
112 }
113
114 GdkWindow *
115 gdk_selection_owner_get_for_display (GdkDisplay *display,
116                                      GdkAtom     selection)
117 {
118   OwnerInfo *info;
119   GSList    *tmp_list;
120
121   tmp_list = owner_list;
122   while (tmp_list)
123     {
124       info = tmp_list->data;
125       if (info->selection == selection)
126         {
127           return info->owner;
128         }
129       tmp_list = tmp_list->next;
130     }
131   return NULL;
132 }
133
134 void
135 gdk_selection_convert (GdkWindow *requestor,
136                        GdkAtom    selection,
137                        GdkAtom    target,
138                        guint32    time)
139 {
140   GdkEvent  *event;
141   GdkWindow *owner;
142   GdkWindow *event_window;
143
144   owner = gdk_selection_owner_get (selection);
145
146   if (owner)
147     {
148       event_window = gdk_directfb_other_event_window (owner,
149                                                       GDK_SELECTION_REQUEST);
150       if (event_window)
151         {
152           event = gdk_directfb_event_make (event_window,
153                                            GDK_SELECTION_REQUEST);
154           event->selection.requestor = GDK_WINDOW_DFB_ID (requestor);
155           event->selection.selection = selection;
156           event->selection.target    = target;
157           event->selection.property  = _gdk_selection_property;
158         }
159     }
160   else
161     {
162       /* If no owner for the specified selection exists, the X server
163        * generates a SelectionNotify event to the requestor with property None.
164        */
165       gdk_selection_send_notify (GDK_WINDOW_DFB_ID (requestor),
166                                  selection,
167                                  target,
168                                  GDK_NONE,
169                                  0);
170     }
171 }
172
173 gint
174 gdk_selection_property_get (GdkWindow  *requestor,
175                             guchar    **data,
176                             GdkAtom    *ret_type,
177                             gint       *ret_format)
178 {
179   guchar *t = NULL;
180   GdkAtom prop_type;
181   gint prop_format;
182   gint prop_len;
183
184   g_return_val_if_fail (requestor != NULL, 0);
185   g_return_val_if_fail (GDK_IS_WINDOW (requestor), 0);
186
187   if (!gdk_property_get (requestor,
188                          _gdk_selection_property,
189                          0/*AnyPropertyType?*/,
190                          0, 0,
191                          FALSE,
192                          &prop_type, &prop_format, &prop_len,
193                          &t))
194     {
195       *data = NULL;
196       return 0;
197     }
198
199   if (ret_type)
200     *ret_type = prop_type;
201   if (ret_format)
202     *ret_format = prop_format;
203
204   if (!gdk_property_get (requestor,
205                          _gdk_selection_property,
206                          0/*AnyPropertyType?*/,
207                          0, prop_len + 1,
208                          FALSE,
209                          &prop_type, &prop_format, &prop_len,
210                          &t))
211     {
212       *data = NULL;
213       return 0;
214     }
215
216   *data = t;
217
218   return prop_len;
219 }
220
221
222 void
223 gdk_selection_send_notify_for_display (GdkDisplay *display,
224                                        guint32     requestor,
225                                        GdkAtom     selection,
226                                        GdkAtom     target,
227                                        GdkAtom     property,
228                                        guint32     time)
229 {
230   GdkEvent  *event;
231   GdkWindow *event_window;
232
233   event_window = gdk_window_lookup ((GdkNativeWindow) requestor);
234
235   if (!event_window)
236     return;
237
238   event_window = gdk_directfb_other_event_window (event_window,
239                                                   GDK_SELECTION_NOTIFY);
240
241   if (event_window)
242     {
243       event = gdk_directfb_event_make (event_window, GDK_SELECTION_NOTIFY);
244       event->selection.selection = selection;
245       event->selection.target = target;
246       event->selection.property = property;
247       event->selection.requestor = (GdkNativeWindow) requestor;
248     }
249 }
250
251 gint
252 gdk_text_property_to_text_list_for_display (GdkDisplay      *display,
253                                             GdkAtom          encoding,
254                                             gint             format,
255                                             const guchar    *text,
256                                             gint             length,
257                                             gchar         ***list)
258 {
259   g_warning ("gdk_text_property_to_text_list() not implemented\n");
260   return 0;
261 }
262
263 void
264 gdk_free_text_list (gchar **list)
265 {
266   g_return_if_fail (list != NULL);
267   g_warning ("gdk_free_text_list() not implemented\n");
268 }
269
270 gint
271 gdk_string_to_compound_text_for_display (GdkDisplay   *display,
272                                          const gchar  *str,
273                                          GdkAtom      *encoding,
274                                          gint         *format,
275                                          guchar      **ctext,
276                                          gint         *length)
277 {
278   g_warning ("gdk_string_to_compound_text() not implemented\n");
279   return 0;
280 }
281
282 void
283 gdk_free_compound_text (guchar *ctext)
284 {
285   g_warning ("gdk_free_compound_text() not implemented\n");
286 }
287
288 /**
289  * gdk_utf8_to_string_target:
290  * @str: a UTF-8 string
291  *
292  * Convert an UTF-8 string into the best possible representation
293  * as a STRING. The representation of characters not in STRING
294  * is not specified; it may be as pseudo-escape sequences
295  * \x{ABCD}, or it may be in some other form of approximation.
296  *
297  * Return value: the newly allocated string, or %NULL if the
298  *               conversion failed. (It should not fail for
299  *               any properly formed UTF-8 string.)
300  **/
301 gchar *
302 gdk_utf8_to_string_target (const gchar *str)
303 {
304   g_warning ("gdk_utf8_to_string_target() not implemented\n");
305   return 0;
306 }
307
308 /**
309  * gdk_utf8_to_compound_text:
310  * @str:      a UTF-8 string
311  * @encoding: location to store resulting encoding
312  * @format:   location to store format of the result
313  * @ctext:    location to store the data of the result
314  * @length:   location to store the length of the data
315  *            stored in @ctext
316  *
317  * Convert from UTF-8 to compound text.
318  *
319  * Return value: %TRUE if the conversion succeeded, otherwise
320  *               false.
321  **/
322 gboolean
323 gdk_utf8_to_compound_text_for_display (GdkDisplay   *display,
324                                        const gchar  *str,
325                                        GdkAtom      *encoding,
326                                        gint         *format,
327                                        guchar      **ctext,
328                                        gint         *length)
329 {
330   g_warning ("gdk_utf8_to_compound_text() not implemented\n");
331   return 0;
332 }
333
334 static gint
335 make_list (const gchar  *text,
336            gint          length,
337            gboolean      latin1,
338            gchar      ***list)
339 {
340   GSList *strings = NULL;
341   gint n_strings = 0;
342   gint i;
343   const gchar *p = text;
344   const gchar *q;
345   GSList *tmp_list;
346   GError *error = NULL;
347
348   while (p < text + length)
349     {
350       gchar *str;
351
352       q = p;
353       while (*q && q < text + length)
354         q++;
355
356       if (latin1)
357         {
358           str = g_convert (p, q - p,
359                            "UTF-8", "ISO-8859-1",
360                            NULL, NULL, &error);
361
362           if (!str)
363             {
364               g_warning ("Error converting selection from STRING: %s",
365                          error->message);
366               g_error_free (error);
367             }
368         }
369       else
370         str = g_strndup (p, q - p);
371
372       if (str)
373         {
374           strings = g_slist_prepend (strings, str);
375           n_strings++;
376         }
377
378       p = q + 1;
379     }
380
381   if (list)
382     *list = g_new (gchar *, n_strings + 1);
383
384   (*list)[n_strings] = NULL;
385
386   i = n_strings;
387   tmp_list = strings;
388   while (tmp_list)
389     {
390       if (list)
391         (*list)[--i] = tmp_list->data;
392       else
393         g_free (tmp_list->data);
394
395       tmp_list = tmp_list->next;
396     }
397
398   g_slist_free (strings);
399
400   return n_strings;
401 }
402
403
404 /**
405  * gdk_text_property_to_utf8_list:
406  * @encoding: an atom representing the encoding of the text
407  * @format:   the format of the property
408  * @text:     the text to convert
409  * @length:   the length of @text, in bytes
410  * @list:     location to store the list of strings or %NULL. The
411  *            list should be freed with g_strfreev().
412  *
413  * Convert a text property in the giving encoding to
414  * a list of UTF-8 strings.
415  *
416  * Return value: the number of strings in the resulting
417  *               list.
418  **/
419 gint
420 gdk_text_property_to_utf8_list_for_display (GdkDisplay     *display,
421                                             GdkAtom         encoding,
422                                             gint            format,
423                                             const guchar   *text,
424                                             gint            length,
425                                             gchar        ***list)
426 {
427   g_return_val_if_fail (text != NULL, 0);
428   g_return_val_if_fail (length >= 0, 0);
429
430   if (encoding == GDK_TARGET_STRING)
431     {
432       return make_list ((gchar *)text, length, TRUE, list);
433     }
434   else if (encoding == gdk_atom_intern ("UTF8_STRING", FALSE))
435     {
436       return make_list ((gchar *)text, length, FALSE, list);
437     }
438   else
439     {
440       gchar **local_list;
441       gint local_count;
442       gint i;
443       const gchar *charset = NULL;
444       gboolean need_conversion = !g_get_charset (&charset);
445       gint count = 0;
446       GError *error = NULL;
447
448       /* Probably COMPOUND text, we fall back to Xlib routines
449        */
450       local_count = gdk_text_property_to_text_list (encoding,
451                                                     format,
452                                                     text,
453                                                     length,
454                                                     &local_list);
455       if (list)
456         *list = g_new (gchar *, local_count + 1);
457
458       for (i = 0; i < local_count; i++)
459         {
460           /* list contains stuff in our default encoding
461            */
462           if (need_conversion)
463             {
464               gchar *utf = g_convert (local_list[i], -1,
465                                       "UTF-8", charset,
466                                       NULL, NULL, &error);
467               if (utf)
468                 {
469                   if (list)
470                     (*list)[count++] = utf;
471                   else
472                     g_free (utf);
473                 }
474               else
475                 {
476                   g_warning ("Error converting to UTF-8 from '%s': %s",
477                              charset, error->message);
478                   g_error_free (error);
479                   error = NULL;
480                 }
481             }
482           else
483             {
484               if (list)
485                 (*list)[count++] = g_strdup (local_list[i]);
486             }
487         }
488
489       gdk_free_text_list (local_list);
490       (*list)[count] = NULL;
491
492       return count;
493     }
494 }