]> Pileus Git - ~andy/gtk/blob - gtk/gtktextchild.c
Removed warning
[~andy/gtk] / gtk / gtktextchild.c
1 /* gtktextchild.c - child pixmaps and widgets
2  *
3  * Copyright (c) 1994 The Regents of the University of California.
4  * Copyright (c) 1994-1997 Sun Microsystems, Inc.
5  * Copyright (c) 2000      Red Hat, Inc.
6  * Tk -> Gtk port by Havoc Pennington <hp@redhat.com>
7  *
8  * This software is copyrighted by the Regents of the University of
9  * California, Sun Microsystems, Inc., and other parties.  The
10  * following terms apply to all files associated with the software
11  * unless explicitly disclaimed in individual files.
12  *
13  * The authors hereby grant permission to use, copy, modify,
14  * distribute, and license this software and its documentation for any
15  * purpose, provided that existing copyright notices are retained in
16  * all copies and that this notice is included verbatim in any
17  * distributions. No written agreement, license, or royalty fee is
18  * required for any of the authorized uses.  Modifications to this
19  * software may be copyrighted by their authors and need not follow
20  * the licensing terms described here, provided that the new terms are
21  * clearly indicated on the first page of each file where they apply.
22  *
23  * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY
24  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
25  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION,
26  * OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED
27  * OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
30  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
32  * NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
33  * AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
34  * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
35  *
36  * GOVERNMENT USE: If you are acquiring this software on behalf of the
37  * U.S. government, the Government shall have only "Restricted Rights"
38  * in the software and related documentation as defined in the Federal
39  * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2).  If you
40  * are acquiring the software on behalf of the Department of Defense,
41  * the software shall be classified as "Commercial Computer Software"
42  * and the Government shall have only "Restricted Rights" as defined
43  * in Clause 252.227-7013 (c) (1) of DFARs.  Notwithstanding the
44  * foregoing, the authors grant the U.S. Government and others acting
45  * in its behalf permission to use and distribute the software in
46  * accordance with the terms specified in this license.
47  *
48  */
49
50 #include "gtktextchild.h"
51 #include "gtktextbtree.h"
52 #include "gtktextlayout.h"
53
54 static GtkTextLineSegment *
55 pixbuf_segment_cleanup_func (GtkTextLineSegment *seg,
56                              GtkTextLine        *line)
57 {
58   /* nothing */
59   return seg;
60 }
61
62 static int
63 pixbuf_segment_delete_func (GtkTextLineSegment *seg,
64                             GtkTextLine        *line,
65                             gboolean            tree_gone)
66 {
67   if (seg->body.pixbuf.pixbuf)
68     g_object_unref (G_OBJECT (seg->body.pixbuf.pixbuf));
69
70   g_free (seg);
71
72   return 0;
73 }
74
75 static void
76 pixbuf_segment_check_func (GtkTextLineSegment *seg,
77                            GtkTextLine        *line)
78 {
79   if (seg->next == NULL)
80     g_error ("pixbuf segment is the last segment in a line");
81
82   if (seg->byte_count != 3)
83     g_error ("pixbuf segment has byte count of %d", seg->byte_count);
84
85   if (seg->char_count != 1)
86     g_error ("pixbuf segment has char count of %d", seg->char_count);
87 }
88
89
90 GtkTextLineSegmentClass gtk_text_pixbuf_type = {
91   "pixbuf",                          /* name */
92   FALSE,                                            /* leftGravity */
93   NULL,                                          /* splitFunc */
94   pixbuf_segment_delete_func,                             /* deleteFunc */
95   pixbuf_segment_cleanup_func,                            /* cleanupFunc */
96   NULL,                                                    /* lineChangeFunc */
97   pixbuf_segment_check_func                               /* checkFunc */
98
99 };
100
101 #define PIXBUF_SEG_SIZE ((unsigned) (G_STRUCT_OFFSET (GtkTextLineSegment, body) \
102         + sizeof (GtkTextPixbuf)))
103
104 GtkTextLineSegment *
105 _gtk_pixbuf_segment_new (GdkPixbuf *pixbuf)
106 {
107   GtkTextLineSegment *seg;
108
109   seg = g_malloc (PIXBUF_SEG_SIZE);
110
111   seg->type = &gtk_text_pixbuf_type;
112
113   seg->next = NULL;
114
115   seg->byte_count = 3; /* We convert to the 0xFFFC "unknown character",
116                         * a 3-byte sequence in UTF-8
117                         */
118   seg->char_count = 1;
119
120   seg->body.pixbuf.pixbuf = pixbuf;
121
122   g_object_ref (G_OBJECT (pixbuf));
123
124   return seg;
125 }
126
127
128 static GtkTextLineSegment *
129 child_segment_cleanup_func (GtkTextLineSegment *seg,
130                             GtkTextLine        *line)
131 {
132   seg->body.child.line = line;
133
134   return seg;
135 }
136
137 static int
138 child_segment_delete_func (GtkTextLineSegment *seg,
139                            GtkTextLine       *line,
140                            gboolean           tree_gone)
141 {
142   GSList *tmp_list;
143   GSList *copy;
144
145   _gtk_text_btree_unregister_child_anchor (seg->body.child.obj);
146   
147   seg->body.child.tree = NULL;
148   seg->body.child.line = NULL;
149
150   /* avoid removing widgets while walking the list */
151   copy = g_slist_copy (seg->body.child.widgets);
152   tmp_list = copy;
153   while (tmp_list != NULL)
154     {
155       GtkWidget *child = tmp_list->data;
156
157       gtk_widget_destroy (child);
158       
159       tmp_list = g_slist_next (tmp_list);
160     }
161
162   /* On removal from the widget's parents (GtkTextView),
163    * the widget should have been removed from the anchor.
164    */
165   g_assert (seg->body.child.widgets == NULL);
166
167   g_slist_free (copy);
168   
169   _gtk_widget_segment_unref (seg);  
170   
171   return 0;
172 }
173
174 static void
175 child_segment_check_func (GtkTextLineSegment *seg,
176                           GtkTextLine        *line)
177 {
178   if (seg->next == NULL)
179     g_error ("child segment is the last segment in a line");
180
181   if (seg->byte_count != 3)
182     g_error ("child segment has byte count of %d", seg->byte_count);
183
184   if (seg->char_count != 1)
185     g_error ("child segment has char count of %d", seg->char_count);
186 }
187
188 GtkTextLineSegmentClass gtk_text_child_type = {
189   "child-widget",                                        /* name */
190   FALSE,                                                 /* leftGravity */
191   NULL,                                                  /* splitFunc */
192   child_segment_delete_func,                             /* deleteFunc */
193   child_segment_cleanup_func,                            /* cleanupFunc */
194   NULL,                                                  /* lineChangeFunc */
195   child_segment_check_func                               /* checkFunc */
196 };
197
198 #define WIDGET_SEG_SIZE ((unsigned) (G_STRUCT_OFFSET (GtkTextLineSegment, body) \
199         + sizeof (GtkTextChildBody)))
200
201 GtkTextLineSegment *
202 _gtk_widget_segment_new (GtkTextChildAnchor *anchor)
203 {
204   GtkTextLineSegment *seg;
205
206   seg = g_malloc (WIDGET_SEG_SIZE);
207
208   seg->type = &gtk_text_child_type;
209
210   seg->next = NULL;
211
212   seg->byte_count = 3; /* We convert to the 0xFFFC "unknown character",
213                         * a 3-byte sequence in UTF-8
214                         */
215   seg->char_count = 1;
216
217   seg->body.child.obj = anchor;
218   seg->body.child.obj->segment = seg;
219   seg->body.child.widgets = NULL;
220   seg->body.child.tree = NULL;
221   seg->body.child.line = NULL;
222
223   g_object_ref (G_OBJECT (anchor));
224   
225   return seg;
226 }
227
228 void
229 _gtk_widget_segment_add    (GtkTextLineSegment *widget_segment,
230                             GtkWidget          *child)
231 {
232   g_return_if_fail (widget_segment->type = &gtk_text_child_type);
233   g_return_if_fail (widget_segment->body.child.tree != NULL);
234
235   g_object_ref (G_OBJECT (child));
236   
237   widget_segment->body.child.widgets =
238     g_slist_prepend (widget_segment->body.child.widgets,
239                      child);
240 }
241
242 void
243 _gtk_widget_segment_remove (GtkTextLineSegment *widget_segment,
244                             GtkWidget          *child)
245 {
246   g_return_if_fail (widget_segment->type = &gtk_text_child_type);
247   
248   widget_segment->body.child.widgets =
249     g_slist_remove (widget_segment->body.child.widgets,
250                     child);
251
252   g_object_unref (G_OBJECT (child));
253 }
254
255 void
256 _gtk_widget_segment_ref (GtkTextLineSegment *widget_segment)
257 {
258   g_assert (widget_segment->type = &gtk_text_child_type);
259
260   g_object_ref (G_OBJECT (widget_segment->body.child.obj));
261 }
262
263 void
264 _gtk_widget_segment_unref (GtkTextLineSegment *widget_segment)
265 {
266   g_assert (widget_segment->type = &gtk_text_child_type);
267
268   g_object_unref (G_OBJECT (widget_segment->body.child.obj));
269 }
270
271 GtkTextLayout*
272 _gtk_anchored_child_get_layout (GtkWidget *child)
273 {
274   return g_object_get_data (G_OBJECT (child), "gtk-text-child-anchor-layout");  
275 }
276
277 static void
278 _gtk_anchored_child_set_layout (GtkWidget     *child,
279                                 GtkTextLayout *layout)
280 {
281   g_object_set_data (G_OBJECT (child),
282                      "gtk-text-child-anchor-layout",
283                      layout);  
284 }
285      
286 static void gtk_text_child_anchor_init       (GtkTextChildAnchor      *child_anchor);
287 static void gtk_text_child_anchor_class_init (GtkTextChildAnchorClass *klass);
288 static void gtk_text_child_anchor_finalize   (GObject                 *obj);
289
290 static gpointer parent_class = NULL;
291
292 GType
293 gtk_text_child_anchor_get_type (void)
294 {
295   static GType object_type = 0;
296
297   if (!object_type)
298     {
299       static const GTypeInfo object_info =
300       {
301         sizeof (GtkTextChildAnchorClass),
302         (GBaseInitFunc) NULL,
303         (GBaseFinalizeFunc) NULL,
304         (GClassInitFunc) gtk_text_child_anchor_class_init,
305         NULL,           /* class_finalize */
306         NULL,           /* class_data */
307         sizeof (GtkTextChildAnchor),
308         0,              /* n_preallocs */
309         (GInstanceInitFunc) gtk_text_child_anchor_init,
310       };
311
312       object_type = g_type_register_static (G_TYPE_OBJECT,
313                                             "GtkTextChildAnchor",
314                                             &object_info, 0);
315     }
316
317   return object_type;
318 }
319
320 static void
321 gtk_text_child_anchor_init (GtkTextChildAnchor *child_anchor)
322 {
323   child_anchor->segment = NULL;
324 }
325
326 static void
327 gtk_text_child_anchor_class_init (GtkTextChildAnchorClass *klass)
328 {
329   GObjectClass *object_class = G_OBJECT_CLASS (klass);
330
331   parent_class = g_type_class_peek_parent (klass);
332
333   object_class->finalize = gtk_text_child_anchor_finalize;
334 }
335
336 GtkTextChildAnchor*
337 gtk_text_child_anchor_new (void)
338 {
339   return GTK_TEXT_CHILD_ANCHOR (g_object_new (GTK_TYPE_TEXT_CHILD_ANCHOR,
340                                               NULL));
341 }
342
343 static void
344 gtk_text_child_anchor_finalize (GObject *obj)
345 {
346   GtkTextChildAnchor *anchor;
347   GSList *tmp_list;
348   GtkTextLineSegment *seg;
349   
350   anchor = GTK_TEXT_CHILD_ANCHOR (obj);
351
352   seg = anchor->segment;
353   
354   if (seg->body.child.tree != NULL)
355     {
356       g_warning ("Someone removed a reference to a GtkTextChildAnchor "
357                  "they didn't own; the anchor is still in the text buffer "
358                  "and the refcount is 0.");
359       return;
360     }
361       
362   tmp_list = seg->body.child.widgets;
363   while (tmp_list)
364     {
365       g_object_unref (G_OBJECT (tmp_list->data));
366       
367       tmp_list = g_slist_next (tmp_list);
368     }
369   
370   g_slist_free (seg->body.child.widgets);
371   
372   g_free (seg);
373
374   anchor->segment = NULL;
375 }
376
377 GList*
378 gtk_text_child_anchor_get_widgets (GtkTextChildAnchor *anchor)
379 {
380   GtkTextLineSegment *seg = anchor->segment;
381   GList *list = NULL;
382   GSList *iter;
383
384   g_return_val_if_fail (seg->type = &gtk_text_child_type, NULL);
385
386   iter = seg->body.child.widgets;
387   while (iter != NULL)
388     {
389       list = g_list_prepend (list, iter->data);
390
391       iter = g_slist_next (iter);
392     }
393
394   /* Order is not relevant, so we don't need to reverse the list
395    * again.
396    */
397   return list;
398 }
399
400 gboolean
401 gtk_text_child_anchor_get_deleted (GtkTextChildAnchor *anchor)
402 {
403   GtkTextLineSegment *seg = anchor->segment;
404
405   g_return_val_if_fail (seg->type = &gtk_text_child_type, TRUE);
406
407   return seg->body.child.tree == NULL;
408 }
409
410 void
411 gtk_text_child_anchor_register_child (GtkTextChildAnchor *anchor,
412                                       GtkWidget          *child,
413                                       GtkTextLayout      *layout)
414 {
415   g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
416   g_return_if_fail (GTK_IS_WIDGET (child));
417
418   _gtk_anchored_child_set_layout (child, layout);
419   
420   _gtk_widget_segment_add (anchor->segment, child);
421
422   gtk_text_child_anchor_queue_resize (anchor, layout);
423 }
424
425 void
426 gtk_text_child_anchor_unregister_child (GtkTextChildAnchor *anchor,
427                                         GtkWidget          *child)
428 {
429   g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
430   g_return_if_fail (GTK_IS_WIDGET (child));
431
432   if (_gtk_anchored_child_get_layout (child))
433     {
434       gtk_text_child_anchor_queue_resize (anchor,
435                                           _gtk_anchored_child_get_layout (child));
436     }
437   
438   _gtk_anchored_child_set_layout (child, NULL);
439   
440   _gtk_widget_segment_remove (anchor->segment, child);
441 }
442
443 void
444 gtk_text_child_anchor_queue_resize (GtkTextChildAnchor *anchor,
445                                     GtkTextLayout      *layout)
446 {
447   GtkTextIter start;
448   GtkTextIter end;
449   GtkTextLineSegment *seg;
450   
451   g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
452   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
453   
454   seg = anchor->segment;
455
456   if (seg->body.child.tree == NULL)
457     return;
458   
459   gtk_text_buffer_get_iter_at_child_anchor (layout->buffer,
460                                             &start, anchor);
461   end = start;
462   gtk_text_iter_forward_char (&end);
463   
464   gtk_text_layout_invalidate (layout, &start, &end);
465 }
466
467 void
468 gtk_text_anchored_child_set_layout (GtkWidget     *child,
469                                     GtkTextLayout *layout)
470 {
471   g_return_if_fail (GTK_IS_WIDGET (child));
472   g_return_if_fail (layout == NULL || GTK_IS_TEXT_LAYOUT (layout));
473   
474   _gtk_anchored_child_set_layout (child, layout);
475 }
476
477