1 /* gtktextchild.c - child pixmaps and widgets
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>
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.
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.
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.
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.
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.
50 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
53 #include "gtktextchild.h"
54 #include "gtktextbtree.h"
55 #include "gtktextlayout.h"
57 #define CHECK_IN_BUFFER(anchor) \
59 if ((anchor)->segment == NULL) \
61 g_warning ("%s: GtkTextChildAnchor hasn't been in a buffer yet", G_GNUC_FUNCTION); \
65 #define CHECK_IN_BUFFER_RETURN(anchor, val) \
67 if ((anchor)->segment == NULL) \
69 g_warning ("%s: GtkTextChildAnchor hasn't been in a buffer yet", G_GNUC_FUNCTION); \
74 static GtkTextLineSegment *
75 pixbuf_segment_cleanup_func (GtkTextLineSegment *seg,
83 pixbuf_segment_delete_func (GtkTextLineSegment *seg,
87 if (seg->body.pixbuf.pixbuf)
88 g_object_unref (seg->body.pixbuf.pixbuf);
96 pixbuf_segment_check_func (GtkTextLineSegment *seg,
99 if (seg->next == NULL)
100 g_error ("pixbuf segment is the last segment in a line");
102 if (seg->byte_count != 3)
103 g_error ("pixbuf segment has byte count of %d", seg->byte_count);
105 if (seg->char_count != 1)
106 g_error ("pixbuf segment has char count of %d", seg->char_count);
110 GtkTextLineSegmentClass gtk_text_pixbuf_type = {
112 FALSE, /* leftGravity */
113 NULL, /* splitFunc */
114 pixbuf_segment_delete_func, /* deleteFunc */
115 pixbuf_segment_cleanup_func, /* cleanupFunc */
116 NULL, /* lineChangeFunc */
117 pixbuf_segment_check_func /* checkFunc */
121 #define PIXBUF_SEG_SIZE ((unsigned) (G_STRUCT_OFFSET (GtkTextLineSegment, body) \
122 + sizeof (GtkTextPixbuf)))
125 _gtk_pixbuf_segment_new (GdkPixbuf *pixbuf)
127 GtkTextLineSegment *seg;
129 seg = g_malloc (PIXBUF_SEG_SIZE);
131 seg->type = >k_text_pixbuf_type;
135 seg->byte_count = 3; /* We convert to the 0xFFFC "unknown character",
136 * a 3-byte sequence in UTF-8
140 seg->body.pixbuf.pixbuf = pixbuf;
142 g_object_ref (pixbuf);
148 static GtkTextLineSegment *
149 child_segment_cleanup_func (GtkTextLineSegment *seg,
152 seg->body.child.line = line;
158 child_segment_delete_func (GtkTextLineSegment *seg,
165 _gtk_text_btree_unregister_child_anchor (seg->body.child.obj);
167 seg->body.child.tree = NULL;
168 seg->body.child.line = NULL;
170 /* avoid removing widgets while walking the list */
171 copy = g_slist_copy (seg->body.child.widgets);
173 while (tmp_list != NULL)
175 GtkWidget *child = tmp_list->data;
177 gtk_widget_destroy (child);
179 tmp_list = g_slist_next (tmp_list);
182 /* On removal from the widget's parents (GtkTextView),
183 * the widget should have been removed from the anchor.
185 g_assert (seg->body.child.widgets == NULL);
189 _gtk_widget_segment_unref (seg);
195 child_segment_check_func (GtkTextLineSegment *seg,
198 if (seg->next == NULL)
199 g_error ("child segment is the last segment in a line");
201 if (seg->byte_count != 3)
202 g_error ("child segment has byte count of %d", seg->byte_count);
204 if (seg->char_count != 1)
205 g_error ("child segment has char count of %d", seg->char_count);
208 GtkTextLineSegmentClass gtk_text_child_type = {
209 "child-widget", /* name */
210 FALSE, /* leftGravity */
211 NULL, /* splitFunc */
212 child_segment_delete_func, /* deleteFunc */
213 child_segment_cleanup_func, /* cleanupFunc */
214 NULL, /* lineChangeFunc */
215 child_segment_check_func /* checkFunc */
218 #define WIDGET_SEG_SIZE ((unsigned) (G_STRUCT_OFFSET (GtkTextLineSegment, body) \
219 + sizeof (GtkTextChildBody)))
222 _gtk_widget_segment_new (GtkTextChildAnchor *anchor)
224 GtkTextLineSegment *seg;
226 seg = g_malloc (WIDGET_SEG_SIZE);
228 seg->type = >k_text_child_type;
232 seg->byte_count = 3; /* We convert to the 0xFFFC "unknown character",
233 * a 3-byte sequence in UTF-8
237 seg->body.child.obj = anchor;
238 seg->body.child.obj->segment = seg;
239 seg->body.child.widgets = NULL;
240 seg->body.child.tree = NULL;
241 seg->body.child.line = NULL;
243 g_object_ref (anchor);
249 _gtk_widget_segment_add (GtkTextLineSegment *widget_segment,
252 g_return_if_fail (widget_segment->type == >k_text_child_type);
253 g_return_if_fail (widget_segment->body.child.tree != NULL);
255 g_object_ref (child);
257 widget_segment->body.child.widgets =
258 g_slist_prepend (widget_segment->body.child.widgets,
263 _gtk_widget_segment_remove (GtkTextLineSegment *widget_segment,
266 g_return_if_fail (widget_segment->type == >k_text_child_type);
268 widget_segment->body.child.widgets =
269 g_slist_remove (widget_segment->body.child.widgets,
272 g_object_unref (child);
276 _gtk_widget_segment_ref (GtkTextLineSegment *widget_segment)
278 g_assert (widget_segment->type == >k_text_child_type);
280 g_object_ref (widget_segment->body.child.obj);
284 _gtk_widget_segment_unref (GtkTextLineSegment *widget_segment)
286 g_assert (widget_segment->type == >k_text_child_type);
288 g_object_unref (widget_segment->body.child.obj);
292 _gtk_anchored_child_get_layout (GtkWidget *child)
294 return g_object_get_data (G_OBJECT (child), "gtk-text-child-anchor-layout");
298 _gtk_anchored_child_set_layout (GtkWidget *child,
299 GtkTextLayout *layout)
301 g_object_set_data (G_OBJECT (child),
302 "gtk-text-child-anchor-layout",
306 static void gtk_text_child_anchor_init (GtkTextChildAnchor *child_anchor);
307 static void gtk_text_child_anchor_class_init (GtkTextChildAnchorClass *klass);
308 static void gtk_text_child_anchor_finalize (GObject *obj);
310 static gpointer parent_class = NULL;
313 gtk_text_child_anchor_get_type (void)
315 static GType object_type = 0;
319 static const GTypeInfo object_info =
321 sizeof (GtkTextChildAnchorClass),
322 (GBaseInitFunc) NULL,
323 (GBaseFinalizeFunc) NULL,
324 (GClassInitFunc) gtk_text_child_anchor_class_init,
325 NULL, /* class_finalize */
326 NULL, /* class_data */
327 sizeof (GtkTextChildAnchor),
329 (GInstanceInitFunc) gtk_text_child_anchor_init,
332 object_type = g_type_register_static (G_TYPE_OBJECT, "GtkTextChildAnchor",
340 gtk_text_child_anchor_init (GtkTextChildAnchor *child_anchor)
342 child_anchor->segment = NULL;
346 gtk_text_child_anchor_class_init (GtkTextChildAnchorClass *klass)
348 GObjectClass *object_class = G_OBJECT_CLASS (klass);
350 parent_class = g_type_class_peek_parent (klass);
352 object_class->finalize = gtk_text_child_anchor_finalize;
356 * gtk_text_child_anchor_new:
358 * Creates a new #GtkTextChildAnchor. Usually you would then insert
359 * it into a #GtkTextBuffer with gtk_text_buffer_insert_child_anchor().
360 * To perform the creation and insertion in one step, use the
361 * convenience function gtk_text_buffer_create_child_anchor().
363 * Return value: a new #GtkTextChildAnchor
366 gtk_text_child_anchor_new (void)
368 return g_object_new (GTK_TYPE_TEXT_CHILD_ANCHOR, NULL);
372 gtk_text_child_anchor_finalize (GObject *obj)
374 GtkTextChildAnchor *anchor;
376 GtkTextLineSegment *seg;
378 anchor = GTK_TEXT_CHILD_ANCHOR (obj);
380 seg = anchor->segment;
384 if (seg->body.child.tree != NULL)
386 g_warning ("Someone removed a reference to a GtkTextChildAnchor "
387 "they didn't own; the anchor is still in the text buffer "
388 "and the refcount is 0.");
392 tmp_list = seg->body.child.widgets;
395 g_object_unref (tmp_list->data);
396 tmp_list = g_slist_next (tmp_list);
399 g_slist_free (seg->body.child.widgets);
404 anchor->segment = NULL;
406 G_OBJECT_CLASS (parent_class)->finalize (obj);
410 * gtk_text_child_anchor_get_widgets:
411 * @anchor: a #GtkTextChildAnchor
413 * Gets a list of all widgets anchored at this child anchor.
414 * The returned list should be freed with g_list_free().
417 * Return value: list of widgets anchored at @anchor
420 gtk_text_child_anchor_get_widgets (GtkTextChildAnchor *anchor)
422 GtkTextLineSegment *seg = anchor->segment;
426 CHECK_IN_BUFFER_RETURN (anchor, NULL);
428 g_return_val_if_fail (seg->type == >k_text_child_type, NULL);
430 iter = seg->body.child.widgets;
433 list = g_list_prepend (list, iter->data);
435 iter = g_slist_next (iter);
438 /* Order is not relevant, so we don't need to reverse the list
445 * gtk_text_child_anchor_get_deleted:
446 * @anchor: a #GtkTextChildAnchor
448 * Determines whether a child anchor has been deleted from
449 * the buffer. Keep in mind that the child anchor will be
450 * unreferenced when removed from the buffer, so you need to
451 * hold your own reference (with g_object_ref()) if you plan
452 * to use this function — otherwise all deleted child anchors
453 * will also be finalized.
455 * Return value: %TRUE if the child anchor has been deleted from its buffer
458 gtk_text_child_anchor_get_deleted (GtkTextChildAnchor *anchor)
460 GtkTextLineSegment *seg = anchor->segment;
462 CHECK_IN_BUFFER_RETURN (anchor, TRUE);
464 g_return_val_if_fail (seg->type == >k_text_child_type, TRUE);
466 return seg->body.child.tree == NULL;
470 gtk_text_child_anchor_register_child (GtkTextChildAnchor *anchor,
472 GtkTextLayout *layout)
474 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
475 g_return_if_fail (GTK_IS_WIDGET (child));
477 CHECK_IN_BUFFER (anchor);
479 _gtk_anchored_child_set_layout (child, layout);
481 _gtk_widget_segment_add (anchor->segment, child);
483 gtk_text_child_anchor_queue_resize (anchor, layout);
487 gtk_text_child_anchor_unregister_child (GtkTextChildAnchor *anchor,
490 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
491 g_return_if_fail (GTK_IS_WIDGET (child));
493 CHECK_IN_BUFFER (anchor);
495 if (_gtk_anchored_child_get_layout (child))
497 gtk_text_child_anchor_queue_resize (anchor,
498 _gtk_anchored_child_get_layout (child));
501 _gtk_anchored_child_set_layout (child, NULL);
503 _gtk_widget_segment_remove (anchor->segment, child);
507 gtk_text_child_anchor_queue_resize (GtkTextChildAnchor *anchor,
508 GtkTextLayout *layout)
512 GtkTextLineSegment *seg;
514 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
515 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
517 CHECK_IN_BUFFER (anchor);
519 seg = anchor->segment;
521 if (seg->body.child.tree == NULL)
524 gtk_text_buffer_get_iter_at_child_anchor (layout->buffer,
527 gtk_text_iter_forward_char (&end);
529 gtk_text_layout_invalidate (layout, &start, &end);
533 gtk_text_anchored_child_set_layout (GtkWidget *child,
534 GtkTextLayout *layout)
536 g_return_if_fail (GTK_IS_WIDGET (child));
537 g_return_if_fail (layout == NULL || GTK_IS_TEXT_LAYOUT (layout));
539 _gtk_anchored_child_set_layout (child, layout);