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
51 #include "gtktextchild.h"
52 #include "gtktextbtree.h"
53 #include "gtktextlayout.h"
55 #define CHECK_IN_BUFFER(anchor) \
57 if ((anchor)->segment == NULL) \
59 g_warning ("%s: GtkTextChildAnchor hasn't been in a buffer yet", G_GNUC_FUNCTION); \
63 #define CHECK_IN_BUFFER_RETURN(anchor, val) \
65 if ((anchor)->segment == NULL) \
67 g_warning ("%s: GtkTextChildAnchor hasn't been in a buffer yet", G_GNUC_FUNCTION); \
72 static GtkTextLineSegment *
73 pixbuf_segment_cleanup_func (GtkTextLineSegment *seg,
81 pixbuf_segment_delete_func (GtkTextLineSegment *seg,
85 if (seg->body.pixbuf.pixbuf)
86 g_object_unref (seg->body.pixbuf.pixbuf);
94 pixbuf_segment_check_func (GtkTextLineSegment *seg,
97 if (seg->next == NULL)
98 g_error ("pixbuf segment is the last segment in a line");
100 if (seg->byte_count != 3)
101 g_error ("pixbuf segment has byte count of %d", seg->byte_count);
103 if (seg->char_count != 1)
104 g_error ("pixbuf segment has char count of %d", seg->char_count);
108 GtkTextLineSegmentClass gtk_text_pixbuf_type = {
110 FALSE, /* leftGravity */
111 NULL, /* splitFunc */
112 pixbuf_segment_delete_func, /* deleteFunc */
113 pixbuf_segment_cleanup_func, /* cleanupFunc */
114 NULL, /* lineChangeFunc */
115 pixbuf_segment_check_func /* checkFunc */
119 #define PIXBUF_SEG_SIZE ((unsigned) (G_STRUCT_OFFSET (GtkTextLineSegment, body) \
120 + sizeof (GtkTextPixbuf)))
123 _gtk_pixbuf_segment_new (GdkPixbuf *pixbuf)
125 GtkTextLineSegment *seg;
127 seg = g_malloc (PIXBUF_SEG_SIZE);
129 seg->type = >k_text_pixbuf_type;
133 seg->byte_count = 3; /* We convert to the 0xFFFC "unknown character",
134 * a 3-byte sequence in UTF-8
138 seg->body.pixbuf.pixbuf = pixbuf;
140 g_object_ref (pixbuf);
146 static GtkTextLineSegment *
147 child_segment_cleanup_func (GtkTextLineSegment *seg,
150 seg->body.child.line = line;
156 child_segment_delete_func (GtkTextLineSegment *seg,
163 _gtk_text_btree_unregister_child_anchor (seg->body.child.obj);
165 seg->body.child.tree = NULL;
166 seg->body.child.line = NULL;
168 /* avoid removing widgets while walking the list */
169 copy = g_slist_copy (seg->body.child.widgets);
171 while (tmp_list != NULL)
173 GtkWidget *child = tmp_list->data;
175 gtk_widget_destroy (child);
177 tmp_list = g_slist_next (tmp_list);
180 /* On removal from the widget's parents (GtkTextView),
181 * the widget should have been removed from the anchor.
183 g_assert (seg->body.child.widgets == NULL);
187 _gtk_widget_segment_unref (seg);
193 child_segment_check_func (GtkTextLineSegment *seg,
196 if (seg->next == NULL)
197 g_error ("child segment is the last segment in a line");
199 if (seg->byte_count != 3)
200 g_error ("child segment has byte count of %d", seg->byte_count);
202 if (seg->char_count != 1)
203 g_error ("child segment has char count of %d", seg->char_count);
206 GtkTextLineSegmentClass gtk_text_child_type = {
207 "child-widget", /* name */
208 FALSE, /* leftGravity */
209 NULL, /* splitFunc */
210 child_segment_delete_func, /* deleteFunc */
211 child_segment_cleanup_func, /* cleanupFunc */
212 NULL, /* lineChangeFunc */
213 child_segment_check_func /* checkFunc */
216 #define WIDGET_SEG_SIZE ((unsigned) (G_STRUCT_OFFSET (GtkTextLineSegment, body) \
217 + sizeof (GtkTextChildBody)))
220 _gtk_widget_segment_new (GtkTextChildAnchor *anchor)
222 GtkTextLineSegment *seg;
224 seg = g_malloc (WIDGET_SEG_SIZE);
226 seg->type = >k_text_child_type;
230 seg->byte_count = 3; /* We convert to the 0xFFFC "unknown character",
231 * a 3-byte sequence in UTF-8
235 seg->body.child.obj = anchor;
236 seg->body.child.obj->segment = seg;
237 seg->body.child.widgets = NULL;
238 seg->body.child.tree = NULL;
239 seg->body.child.line = NULL;
241 g_object_ref (anchor);
247 _gtk_widget_segment_add (GtkTextLineSegment *widget_segment,
250 g_return_if_fail (widget_segment->type == >k_text_child_type);
251 g_return_if_fail (widget_segment->body.child.tree != NULL);
253 g_object_ref (child);
255 widget_segment->body.child.widgets =
256 g_slist_prepend (widget_segment->body.child.widgets,
261 _gtk_widget_segment_remove (GtkTextLineSegment *widget_segment,
264 g_return_if_fail (widget_segment->type == >k_text_child_type);
266 widget_segment->body.child.widgets =
267 g_slist_remove (widget_segment->body.child.widgets,
270 g_object_unref (child);
274 _gtk_widget_segment_ref (GtkTextLineSegment *widget_segment)
276 g_assert (widget_segment->type == >k_text_child_type);
278 g_object_ref (widget_segment->body.child.obj);
282 _gtk_widget_segment_unref (GtkTextLineSegment *widget_segment)
284 g_assert (widget_segment->type == >k_text_child_type);
286 g_object_unref (widget_segment->body.child.obj);
290 _gtk_anchored_child_get_layout (GtkWidget *child)
292 return g_object_get_data (G_OBJECT (child), "gtk-text-child-anchor-layout");
296 _gtk_anchored_child_set_layout (GtkWidget *child,
297 GtkTextLayout *layout)
299 g_object_set_data (G_OBJECT (child),
300 "gtk-text-child-anchor-layout",
304 static void gtk_text_child_anchor_init (GtkTextChildAnchor *child_anchor);
305 static void gtk_text_child_anchor_class_init (GtkTextChildAnchorClass *klass);
306 static void gtk_text_child_anchor_finalize (GObject *obj);
308 static gpointer parent_class = NULL;
311 gtk_text_child_anchor_get_type (void)
313 static GType object_type = 0;
317 static const GTypeInfo object_info =
319 sizeof (GtkTextChildAnchorClass),
320 (GBaseInitFunc) NULL,
321 (GBaseFinalizeFunc) NULL,
322 (GClassInitFunc) gtk_text_child_anchor_class_init,
323 NULL, /* class_finalize */
324 NULL, /* class_data */
325 sizeof (GtkTextChildAnchor),
327 (GInstanceInitFunc) gtk_text_child_anchor_init,
330 object_type = g_type_register_static (G_TYPE_OBJECT, "GtkTextChildAnchor",
338 gtk_text_child_anchor_init (GtkTextChildAnchor *child_anchor)
340 child_anchor->segment = NULL;
344 gtk_text_child_anchor_class_init (GtkTextChildAnchorClass *klass)
346 GObjectClass *object_class = G_OBJECT_CLASS (klass);
348 parent_class = g_type_class_peek_parent (klass);
350 object_class->finalize = gtk_text_child_anchor_finalize;
354 * gtk_text_child_anchor_new:
356 * Creates a new #GtkTextChildAnchor. Usually you would then insert
357 * it into a #GtkTextBuffer with gtk_text_buffer_insert_child_anchor().
358 * To perform the creation and insertion in one step, use the
359 * convenience function gtk_text_buffer_create_child_anchor().
361 * Return value: a new #GtkTextChildAnchor
364 gtk_text_child_anchor_new (void)
366 return g_object_new (GTK_TYPE_TEXT_CHILD_ANCHOR, NULL);
370 gtk_text_child_anchor_finalize (GObject *obj)
372 GtkTextChildAnchor *anchor;
374 GtkTextLineSegment *seg;
376 anchor = GTK_TEXT_CHILD_ANCHOR (obj);
378 seg = anchor->segment;
380 if (seg->body.child.tree != NULL)
382 g_warning ("Someone removed a reference to a GtkTextChildAnchor "
383 "they didn't own; the anchor is still in the text buffer "
384 "and the refcount is 0.");
388 tmp_list = seg->body.child.widgets;
391 g_object_unref (tmp_list->data);
392 tmp_list = g_slist_next (tmp_list);
395 g_slist_free (seg->body.child.widgets);
399 anchor->segment = NULL;
403 * gtk_text_child_anchor_get_widgets:
404 * @anchor: a #GtkTextChildAnchor
406 * Gets a list of all widgets anchored at this child anchor.
407 * The returned list should be freed with g_list_free().
410 * Return value: list of widgets anchored at @anchor
413 gtk_text_child_anchor_get_widgets (GtkTextChildAnchor *anchor)
415 GtkTextLineSegment *seg = anchor->segment;
419 CHECK_IN_BUFFER_RETURN (anchor, NULL);
421 g_return_val_if_fail (seg->type == >k_text_child_type, NULL);
423 iter = seg->body.child.widgets;
426 list = g_list_prepend (list, iter->data);
428 iter = g_slist_next (iter);
431 /* Order is not relevant, so we don't need to reverse the list
438 * gtk_text_child_anchor_get_deleted:
439 * @anchor: a #GtkTextChildAnchor
441 * Determines whether a child anchor has been deleted from
442 * the buffer. Keep in mind that the child anchor will be
443 * unreferenced when removed from the buffer, so you need to
444 * hold your own reference (with g_object_ref()) if you plan
445 * to use this function — otherwise all deleted child anchors
446 * will also be finalized.
448 * Return value: %TRUE if the child anchor has been deleted from its buffer
451 gtk_text_child_anchor_get_deleted (GtkTextChildAnchor *anchor)
453 GtkTextLineSegment *seg = anchor->segment;
455 CHECK_IN_BUFFER_RETURN (anchor, TRUE);
457 g_return_val_if_fail (seg->type == >k_text_child_type, TRUE);
459 return seg->body.child.tree == NULL;
463 gtk_text_child_anchor_register_child (GtkTextChildAnchor *anchor,
465 GtkTextLayout *layout)
467 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
468 g_return_if_fail (GTK_IS_WIDGET (child));
470 CHECK_IN_BUFFER (anchor);
472 _gtk_anchored_child_set_layout (child, layout);
474 _gtk_widget_segment_add (anchor->segment, child);
476 gtk_text_child_anchor_queue_resize (anchor, layout);
480 gtk_text_child_anchor_unregister_child (GtkTextChildAnchor *anchor,
483 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
484 g_return_if_fail (GTK_IS_WIDGET (child));
486 CHECK_IN_BUFFER (anchor);
488 if (_gtk_anchored_child_get_layout (child))
490 gtk_text_child_anchor_queue_resize (anchor,
491 _gtk_anchored_child_get_layout (child));
494 _gtk_anchored_child_set_layout (child, NULL);
496 _gtk_widget_segment_remove (anchor->segment, child);
500 gtk_text_child_anchor_queue_resize (GtkTextChildAnchor *anchor,
501 GtkTextLayout *layout)
505 GtkTextLineSegment *seg;
507 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
508 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
510 CHECK_IN_BUFFER (anchor);
512 seg = anchor->segment;
514 if (seg->body.child.tree == NULL)
517 gtk_text_buffer_get_iter_at_child_anchor (layout->buffer,
520 gtk_text_iter_forward_char (&end);
522 gtk_text_layout_invalidate (layout, &start, &end);
526 gtk_text_anchored_child_set_layout (GtkWidget *child,
527 GtkTextLayout *layout)
529 g_return_if_fail (GTK_IS_WIDGET (child));
530 g_return_if_fail (layout == NULL || GTK_IS_TEXT_LAYOUT (layout));
532 _gtk_anchored_child_set_layout (child, layout);