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 #include "gtktextchild.h"
51 #include "gtktextbtree.h"
52 #include "gtktextlayout.h"
54 static GtkTextLineSegment *
55 pixbuf_segment_cleanup_func (GtkTextLineSegment *seg,
63 pixbuf_segment_delete_func (GtkTextLineSegment *seg,
67 if (seg->body.pixbuf.pixbuf)
68 g_object_unref (G_OBJECT (seg->body.pixbuf.pixbuf));
76 pixbuf_segment_check_func (GtkTextLineSegment *seg,
79 if (seg->next == NULL)
80 g_error ("pixbuf segment is the last segment in a line");
82 if (seg->byte_count != 3)
83 g_error ("pixbuf segment has byte count of %d", seg->byte_count);
85 if (seg->char_count != 1)
86 g_error ("pixbuf segment has char count of %d", seg->char_count);
90 GtkTextLineSegmentClass gtk_text_pixbuf_type = {
92 FALSE, /* leftGravity */
94 pixbuf_segment_delete_func, /* deleteFunc */
95 pixbuf_segment_cleanup_func, /* cleanupFunc */
96 NULL, /* lineChangeFunc */
97 pixbuf_segment_check_func /* checkFunc */
101 #define PIXBUF_SEG_SIZE ((unsigned) (G_STRUCT_OFFSET (GtkTextLineSegment, body) \
102 + sizeof (GtkTextPixbuf)))
105 _gtk_pixbuf_segment_new (GdkPixbuf *pixbuf)
107 GtkTextLineSegment *seg;
109 seg = g_malloc (PIXBUF_SEG_SIZE);
111 seg->type = >k_text_pixbuf_type;
115 seg->byte_count = 3; /* We convert to the 0xFFFC "unknown character",
116 * a 3-byte sequence in UTF-8
120 seg->body.pixbuf.pixbuf = pixbuf;
122 g_object_ref (G_OBJECT (pixbuf));
128 static GtkTextLineSegment *
129 child_segment_cleanup_func (GtkTextLineSegment *seg,
132 seg->body.child.line = line;
138 child_segment_delete_func (GtkTextLineSegment *seg,
145 _gtk_text_btree_unregister_child_anchor (seg->body.child.obj);
147 seg->body.child.tree = NULL;
148 seg->body.child.line = NULL;
150 /* avoid removing widgets while walking the list */
151 copy = g_slist_copy (seg->body.child.widgets);
153 while (tmp_list != NULL)
155 GtkWidget *child = tmp_list->data;
157 gtk_widget_destroy (child);
159 tmp_list = g_slist_next (tmp_list);
162 /* On removal from the widget's parents (GtkTextView),
163 * the widget should have been removed from the anchor.
165 g_assert (seg->body.child.widgets == NULL);
169 _gtk_widget_segment_unref (seg);
175 child_segment_check_func (GtkTextLineSegment *seg,
178 if (seg->next == NULL)
179 g_error ("child segment is the last segment in a line");
181 if (seg->byte_count != 3)
182 g_error ("child segment has byte count of %d", seg->byte_count);
184 if (seg->char_count != 1)
185 g_error ("child segment has char count of %d", seg->char_count);
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 */
198 #define WIDGET_SEG_SIZE ((unsigned) (G_STRUCT_OFFSET (GtkTextLineSegment, body) \
199 + sizeof (GtkTextChildBody)))
202 _gtk_widget_segment_new (void)
204 GtkTextLineSegment *seg;
206 seg = g_malloc (WIDGET_SEG_SIZE);
208 seg->type = >k_text_child_type;
212 seg->byte_count = 3; /* We convert to the 0xFFFC "unknown character",
213 * a 3-byte sequence in UTF-8
217 seg->body.child.obj = g_object_new (GTK_TYPE_TEXT_CHILD_ANCHOR, NULL);
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;
227 _gtk_widget_segment_add (GtkTextLineSegment *widget_segment,
230 g_return_if_fail (widget_segment->type = >k_text_child_type);
231 g_return_if_fail (widget_segment->body.child.tree != NULL);
233 g_object_ref (G_OBJECT (child));
235 widget_segment->body.child.widgets =
236 g_slist_prepend (widget_segment->body.child.widgets,
241 _gtk_widget_segment_remove (GtkTextLineSegment *widget_segment,
244 g_return_if_fail (widget_segment->type = >k_text_child_type);
246 widget_segment->body.child.widgets =
247 g_slist_remove (widget_segment->body.child.widgets,
250 g_object_unref (G_OBJECT (child));
254 _gtk_widget_segment_ref (GtkTextLineSegment *widget_segment)
256 g_assert (widget_segment->type = >k_text_child_type);
258 g_object_ref (G_OBJECT (widget_segment->body.child.obj));
262 _gtk_widget_segment_unref (GtkTextLineSegment *widget_segment)
264 g_assert (widget_segment->type = >k_text_child_type);
266 g_object_unref (G_OBJECT (widget_segment->body.child.obj));
270 _gtk_anchored_child_get_layout (GtkWidget *child)
272 return gtk_object_get_data (GTK_OBJECT (child), "gtk-text-child-anchor-layout");
276 _gtk_anchored_child_set_layout (GtkWidget *child,
277 GtkTextLayout *layout)
279 gtk_object_set_data (GTK_OBJECT (child),
280 "gtk-text-child-anchor-layout",
284 static void gtk_text_child_anchor_init (GtkTextChildAnchor *child_anchor);
285 static void gtk_text_child_anchor_class_init (GtkTextChildAnchorClass *klass);
286 static void gtk_text_child_anchor_finalize (GObject *obj);
288 static gpointer parent_class = NULL;
291 gtk_text_child_anchor_get_type (void)
293 static GType object_type = 0;
297 static const GTypeInfo object_info =
299 sizeof (GtkTextChildAnchorClass),
300 (GBaseInitFunc) NULL,
301 (GBaseFinalizeFunc) NULL,
302 (GClassInitFunc) gtk_text_child_anchor_class_init,
303 NULL, /* class_finalize */
304 NULL, /* class_data */
305 sizeof (GtkTextChildAnchor),
307 (GInstanceInitFunc) gtk_text_child_anchor_init,
310 object_type = g_type_register_static (G_TYPE_OBJECT,
311 "GtkTextChildAnchor",
319 gtk_text_child_anchor_init (GtkTextChildAnchor *child_anchor)
321 child_anchor->segment = NULL;
325 gtk_text_child_anchor_class_init (GtkTextChildAnchorClass *klass)
327 GObjectClass *object_class = G_OBJECT_CLASS (klass);
329 parent_class = g_type_class_peek_parent (klass);
331 object_class->finalize = gtk_text_child_anchor_finalize;
335 gtk_text_child_anchor_finalize (GObject *obj)
337 GtkTextChildAnchor *anchor;
339 GtkTextLineSegment *seg;
341 anchor = GTK_TEXT_CHILD_ANCHOR (obj);
343 seg = anchor->segment;
345 if (seg->body.child.tree != NULL)
347 g_warning ("Someone removed a reference to a GtkTextChildAnchor "
348 "they didn't own; the anchor is still in the text buffer "
349 "and the refcount is 0.");
353 tmp_list = seg->body.child.widgets;
356 g_object_unref (G_OBJECT (tmp_list->data));
358 tmp_list = g_slist_next (tmp_list);
361 g_slist_free (seg->body.child.widgets);
365 anchor->segment = NULL;
369 gtk_text_child_anchor_get_widgets (GtkTextChildAnchor *anchor)
371 GtkTextLineSegment *seg = anchor->segment;
375 g_return_val_if_fail (seg->type = >k_text_child_type, NULL);
377 iter = seg->body.child.widgets;
380 list = g_list_prepend (list, iter->data);
382 iter = g_slist_next (iter);
385 /* Order is not relevant, so we don't need to reverse the list
392 gtk_text_child_anchor_get_deleted (GtkTextChildAnchor *anchor)
394 GtkTextLineSegment *seg = anchor->segment;
396 g_return_val_if_fail (seg->type = >k_text_child_type, TRUE);
398 return seg->body.child.tree == NULL;
402 gtk_text_child_anchor_register_child (GtkTextChildAnchor *anchor,
404 GtkTextLayout *layout)
406 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
407 g_return_if_fail (GTK_IS_WIDGET (child));
409 _gtk_anchored_child_set_layout (child, layout);
411 _gtk_widget_segment_add (anchor->segment, child);
413 gtk_text_child_anchor_queue_resize (anchor, layout);
417 gtk_text_child_anchor_unregister_child (GtkTextChildAnchor *anchor,
420 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
421 g_return_if_fail (GTK_IS_WIDGET (child));
423 if (_gtk_anchored_child_get_layout (child))
425 gtk_text_child_anchor_queue_resize (anchor,
426 _gtk_anchored_child_get_layout (child));
429 _gtk_anchored_child_set_layout (child, NULL);
431 _gtk_widget_segment_remove (anchor->segment, child);
435 gtk_text_child_anchor_queue_resize (GtkTextChildAnchor *anchor,
436 GtkTextLayout *layout)
440 GtkTextLineSegment *seg;
442 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
443 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
445 seg = anchor->segment;
447 if (seg->body.child.tree == NULL)
450 gtk_text_buffer_get_iter_at_child_anchor (layout->buffer,
453 gtk_text_iter_forward_char (&end);
455 gtk_text_layout_invalidate (layout, &start, &end);
459 gtk_text_anchored_child_set_layout (GtkWidget *child,
460 GtkTextLayout *layout)
462 g_return_if_fail (GTK_IS_WIDGET (child));
463 g_return_if_fail (layout == NULL || GTK_IS_TEXT_LAYOUT (layout));
465 _gtk_anchored_child_set_layout (child, layout);