]> Pileus Git - ~andy/gtk/blob - gdk/linux-fb/gdkproperty-fb.c
Don't mangle sequences of consecutive \n or \r.
[~andy/gtk] / gdk / linux-fb / gdkproperty-fb.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.  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 <string.h>
28 #include <time.h>
29
30 #include "gdkfb.h"
31 #include "gdkproperty.h"
32 #include "gdkprivate.h"
33 #include "gdkprivate-fb.h"
34
35 GdkAtom
36 gdk_atom_intern (const gchar *atom_name,
37                  gboolean     only_if_exists)
38 {
39   g_return_val_if_fail (atom_name != NULL, GDK_NONE);
40
41   if (strcmp (atom_name, "PRIMARY") == 0)
42     return GDK_SELECTION_PRIMARY;
43   else if (strcmp (atom_name, "SECONDARY") == 0)
44     return GDK_SELECTION_SECONDARY;
45   else if (strcmp (atom_name, "CLIPBOARD") == 0)
46     return GDK_SELECTION_CLIPBOARD;
47   else if (strcmp (atom_name, "ATOM") == 0)
48     return GDK_SELECTION_TYPE_ATOM;
49   else if (strcmp (atom_name, "BITMAP") == 0)
50     return GDK_SELECTION_TYPE_BITMAP;
51   else if (strcmp (atom_name, "COLORMAP") == 0)
52     return GDK_SELECTION_TYPE_COLORMAP;
53   else if (strcmp (atom_name, "DRAWABLE") == 0)
54     return GDK_SELECTION_TYPE_DRAWABLE;
55   else if (strcmp (atom_name, "INTEGER") == 0)
56     return GDK_SELECTION_TYPE_INTEGER;
57   else if (strcmp (atom_name, "PIXMAP") == 0)
58     return GDK_SELECTION_TYPE_PIXMAP;
59   else if (strcmp (atom_name, "WINDOW") == 0)
60     return GDK_SELECTION_TYPE_WINDOW;
61   else if (strcmp (atom_name, "STRING") == 0)
62     return GDK_SELECTION_TYPE_STRING;
63   else
64     return GUINT_TO_POINTER (256 + g_quark_from_string (atom_name));
65 }
66
67 gchar*
68 gdk_atom_name (GdkAtom atom)
69 {
70   if (GPOINTER_TO_UINT (atom) < 256)
71     {
72       
73       switch (GPOINTER_TO_UINT (atom))
74         {
75         case GDK_SELECTION_PRIMARY: return g_strdup ("PRIMARY");
76         case GDK_SELECTION_SECONDARY: return g_strdup ("SECONDARY");
77         case GDK_SELECTION_CLIPBOARD: return g_strdup ("CLIPBOARD");
78         case GDK_SELECTION_TYPE_ATOM: return g_strdup ("ATOM");
79         case GDK_SELECTION_TYPE_BITMAP: return g_strdup ("BITMAP");
80         case GDK_SELECTION_TYPE_COLORMAP: return g_strdup ("COLORMAP");
81         case GDK_SELECTION_TYPE_DRAWABLE: return g_strdup ("DRAWABLE");
82         case GDK_SELECTION_TYPE_INTEGER: return g_strdup ("INTEGER");
83         case GDK_SELECTION_TYPE_PIXMAP: return g_strdup ("PIXMAP");
84         case GDK_SELECTION_TYPE_WINDOW: return g_strdup ("WINDOW");
85         case GDK_SELECTION_TYPE_STRING: return g_strdup ("STRING");
86         default:
87           g_warning (G_STRLOC "Invalid atom");
88           return g_strdup ("<invalid>");
89         }
90     }
91   else
92     return g_strdup (g_quark_to_string (GPOINTER_TO_UINT (atom) - 256));
93 }
94
95 static void
96 gdk_property_delete_2 (GdkWindow *window,
97                        GdkAtom property,
98                        GdkWindowProperty *prop)
99 {
100   GdkWindowFBData *fbd = GDK_WINDOW_IMPL_FBDATA(window);
101   GdkEvent *event;
102   GdkWindow *event_window;
103   
104   g_hash_table_remove (fbd->properties, GUINT_TO_POINTER (property));
105   g_free (prop);
106
107   event_window = gdk_fb_other_event_window (window, GDK_PROPERTY_NOTIFY);
108   if (event_window)
109     {
110       event = gdk_event_make (event_window, GDK_PROPERTY_NOTIFY, TRUE);
111       event->property.atom = property;
112       event->property.state = GDK_PROPERTY_DELETE;
113     }
114 }
115
116 void
117 gdk_property_delete (GdkWindow *window,
118                      GdkAtom    property)
119 {
120   GdkWindowFBData *fbd = GDK_WINDOW_FBDATA (window);
121   GdkWindowProperty *prop;
122
123   g_return_if_fail (window != NULL);
124   g_return_if_fail (GDK_IS_WINDOW (window));
125
126   if (!fbd->properties)
127     return;
128
129   prop = g_hash_table_lookup (fbd->properties, GUINT_TO_POINTER(property));
130   if (!prop)
131     return;
132
133   gdk_property_delete_2 (window, property, prop);
134 }
135
136 gint
137 gdk_property_get (GdkWindow   *window,
138                   GdkAtom      property,
139                   GdkAtom      type,
140                   gulong       offset,
141                   gulong       length,
142                   gint         pdelete,
143                   GdkAtom     *actual_property_type,
144                   gint        *actual_format_type,
145                   gint        *actual_length,
146                   guchar     **data)
147 {
148   GdkWindowFBData *fbd = GDK_WINDOW_FBDATA (window);
149   GdkWindowProperty *prop;
150   int nbytes;
151
152   g_return_val_if_fail (window != NULL, FALSE);
153   g_return_val_if_fail (data != NULL, FALSE);
154   g_return_val_if_fail (actual_length != NULL, FALSE);
155   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
156
157   if (!fbd->properties)
158     return FALSE;
159
160   prop = g_hash_table_lookup (fbd->properties, GUINT_TO_POINTER (property));
161   if (!prop)
162     return FALSE;
163
164   nbytes = (offset + length * (prop->format >> 3)) - prop->length;
165   nbytes = MAX (nbytes, 0);
166   if (nbytes > 0)
167     {
168       *data = g_malloc (nbytes+1);
169       memcpy (data, prop->data + offset, nbytes);
170       (*data)[nbytes] = 0;
171     }
172   else
173     *data = NULL;
174   *actual_length = nbytes / (prop->format >> 3);
175   *actual_property_type = prop->type;
176   *actual_format_type = prop->format;
177
178   if (pdelete)
179     gdk_property_delete_2 (window, property, prop);
180
181   return TRUE;
182 }
183
184 void
185 gdk_property_change (GdkWindow   *window,
186                      GdkAtom      property,
187                      GdkAtom      type,
188                      gint         format,
189                      GdkPropMode  mode,
190                      const guchar *data,
191                      gint         nelements)
192 {
193   GdkWindowFBData *fbd = GDK_WINDOW_FBDATA (window);
194   GdkWindowProperty *prop, *new_prop;
195   int new_size = 0;
196   GdkEvent *event;
197   GdkWindow *event_window;
198
199   g_return_if_fail (window != NULL);
200   g_return_if_fail (GDK_IS_WINDOW (window));
201
202   if (!fbd->properties)
203     fbd->properties = g_hash_table_new (NULL, NULL);
204
205   prop = g_hash_table_lookup (fbd->properties, GUINT_TO_POINTER (property));
206
207   switch(mode)
208     {
209     case GDK_PROP_MODE_REPLACE:
210       new_size = nelements * (format >> 3);
211       break;
212     case GDK_PROP_MODE_PREPEND:
213     case GDK_PROP_MODE_APPEND:
214       new_size = nelements * (format >> 3);
215       if (prop)
216         new_size += prop->length;
217     default:
218       break;
219     }
220
221   new_prop = g_malloc (G_STRUCT_OFFSET (GdkWindowProperty, data) + new_size);
222   new_prop->length = new_size;
223   new_prop->type = type;
224   new_prop->format = format;
225
226   switch (mode)
227     {
228     case GDK_PROP_MODE_REPLACE:
229       memcpy (new_prop->data, data, new_size);
230       break;
231     case GDK_PROP_MODE_APPEND:
232       if (prop)
233         memcpy (new_prop->data, prop->data, prop->length);
234       memcpy (new_prop->data + prop->length, data, (nelements * (format >> 3)));
235       break;
236     case GDK_PROP_MODE_PREPEND:
237       memcpy (new_prop->data, data, (nelements * (format >> 3)));
238       if (prop)
239         memcpy (new_prop->data + (nelements * (format >> 3)), prop->data, prop->length);
240       break;
241     }
242
243   g_hash_table_insert (fbd->properties, GUINT_TO_POINTER (property), new_prop);
244   g_free (prop);
245
246   event_window = gdk_fb_other_event_window (window, GDK_PROPERTY_NOTIFY);
247   if (event_window)
248     {
249       event = gdk_event_make (event_window, GDK_PROPERTY_NOTIFY, TRUE);
250       event->property.atom = property;
251       event->property.state = GDK_PROPERTY_NEW_VALUE;
252     }
253 }