]> Pileus Git - ~andy/gtk/blob - gtk/gtktextchild.c
Add warnings if you try to use a child anchor that isn't inside a buffer.
[~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 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
51 #include "gtktextchild.h"
52 #include "gtktextbtree.h"
53 #include "gtktextlayout.h"
54
55 #define CHECK_IN_BUFFER(anchor) do { if ((anchor)->segment == NULL) g_warning ("%s: GtkTextChildAnchor hasn't been in a buffer yet", __FUNCTION__); } while (0)
56 #define CHECK_IN_BUFFER_RETURN(anchor, val) do { if ((anchor)->segment == NULL) g_warning ("%s: GtkTextChildAnchor hasn't been in a buffer yet", __FUNCTION__); return (val); } while (0)
57
58 static GtkTextLineSegment *
59 pixbuf_segment_cleanup_func (GtkTextLineSegment *seg,
60                              GtkTextLine        *line)
61 {
62   /* nothing */
63   return seg;
64 }
65
66 static int
67 pixbuf_segment_delete_func (GtkTextLineSegment *seg,
68                             GtkTextLine        *line,
69                             gboolean            tree_gone)
70 {
71   if (seg->body.pixbuf.pixbuf)
72     g_object_unref (G_OBJECT (seg->body.pixbuf.pixbuf));
73
74   g_free (seg);
75
76   return 0;
77 }
78
79 static void
80 pixbuf_segment_check_func (GtkTextLineSegment *seg,
81                            GtkTextLine        *line)
82 {
83   if (seg->next == NULL)
84     g_error ("pixbuf segment is the last segment in a line");
85
86   if (seg->byte_count != 3)
87     g_error ("pixbuf segment has byte count of %d", seg->byte_count);
88
89   if (seg->char_count != 1)
90     g_error ("pixbuf segment has char count of %d", seg->char_count);
91 }
92
93
94 GtkTextLineSegmentClass gtk_text_pixbuf_type = {
95   "pixbuf",                          /* name */
96   FALSE,                                            /* leftGravity */
97   NULL,                                          /* splitFunc */
98   pixbuf_segment_delete_func,                             /* deleteFunc */
99   pixbuf_segment_cleanup_func,                            /* cleanupFunc */
100   NULL,                                                    /* lineChangeFunc */
101   pixbuf_segment_check_func                               /* checkFunc */
102
103 };
104
105 #define PIXBUF_SEG_SIZE ((unsigned) (G_STRUCT_OFFSET (GtkTextLineSegment, body) \
106         + sizeof (GtkTextPixbuf)))
107
108 GtkTextLineSegment *
109 _gtk_pixbuf_segment_new (GdkPixbuf *pixbuf)
110 {
111   GtkTextLineSegment *seg;
112
113   seg = g_malloc (PIXBUF_SEG_SIZE);
114
115   seg->type = &gtk_text_pixbuf_type;
116
117   seg->next = NULL;
118
119   seg->byte_count = 3; /* We convert to the 0xFFFC "unknown character",
120                         * a 3-byte sequence in UTF-8
121                         */
122   seg->char_count = 1;
123
124   seg->body.pixbuf.pixbuf = pixbuf;
125
126   g_object_ref (G_OBJECT (pixbuf));
127
128   return seg;
129 }
130
131
132 static GtkTextLineSegment *
133 child_segment_cleanup_func (GtkTextLineSegment *seg,
134                             GtkTextLine        *line)
135 {
136   seg->body.child.line = line;
137
138   return seg;
139 }
140
141 static int
142 child_segment_delete_func (GtkTextLineSegment *seg,
143                            GtkTextLine       *line,
144                            gboolean           tree_gone)
145 {
146   GSList *tmp_list;
147   GSList *copy;
148
149   _gtk_text_btree_unregister_child_anchor (seg->body.child.obj);
150   
151   seg->body.child.tree = NULL;
152   seg->body.child.line = NULL;
153
154   /* avoid removing widgets while walking the list */
155   copy = g_slist_copy (seg->body.child.widgets);
156   tmp_list = copy;
157   while (tmp_list != NULL)
158     {
159       GtkWidget *child = tmp_list->data;
160
161       gtk_widget_destroy (child);
162       
163       tmp_list = g_slist_next (tmp_list);
164     }
165
166   /* On removal from the widget's parents (GtkTextView),
167    * the widget should have been removed from the anchor.
168    */
169   g_assert (seg->body.child.widgets == NULL);
170
171   g_slist_free (copy);
172   
173   _gtk_widget_segment_unref (seg);  
174   
175   return 0;
176 }
177
178 static void
179 child_segment_check_func (GtkTextLineSegment *seg,
180                           GtkTextLine        *line)
181 {
182   if (seg->next == NULL)
183     g_error ("child segment is the last segment in a line");
184
185   if (seg->byte_count != 3)
186     g_error ("child segment has byte count of %d", seg->byte_count);
187
188   if (seg->char_count != 1)
189     g_error ("child segment has char count of %d", seg->char_count);
190 }
191
192 GtkTextLineSegmentClass gtk_text_child_type = {
193   "child-widget",                                        /* name */
194   FALSE,                                                 /* leftGravity */
195   NULL,                                                  /* splitFunc */
196   child_segment_delete_func,                             /* deleteFunc */
197   child_segment_cleanup_func,                            /* cleanupFunc */
198   NULL,                                                  /* lineChangeFunc */
199   child_segment_check_func                               /* checkFunc */
200 };
201
202 #define WIDGET_SEG_SIZE ((unsigned) (G_STRUCT_OFFSET (GtkTextLineSegment, body) \
203         + sizeof (GtkTextChildBody)))
204
205 GtkTextLineSegment *
206 _gtk_widget_segment_new (GtkTextChildAnchor *anchor)
207 {
208   GtkTextLineSegment *seg;
209
210   seg = g_malloc (WIDGET_SEG_SIZE);
211
212   seg->type = &gtk_text_child_type;
213
214   seg->next = NULL;
215
216   seg->byte_count = 3; /* We convert to the 0xFFFC "unknown character",
217                         * a 3-byte sequence in UTF-8
218                         */
219   seg->char_count = 1;
220
221   seg->body.child.obj = anchor;
222   seg->body.child.obj->segment = seg;
223   seg->body.child.widgets = NULL;
224   seg->body.child.tree = NULL;
225   seg->body.child.line = NULL;
226
227   g_object_ref (G_OBJECT (anchor));
228   
229   return seg;
230 }
231
232 void
233 _gtk_widget_segment_add    (GtkTextLineSegment *widget_segment,
234                             GtkWidget          *child)
235 {
236   g_return_if_fail (widget_segment->type = &gtk_text_child_type);
237   g_return_if_fail (widget_segment->body.child.tree != NULL);
238
239   g_object_ref (G_OBJECT (child));
240   
241   widget_segment->body.child.widgets =
242     g_slist_prepend (widget_segment->body.child.widgets,
243                      child);
244 }
245
246 void
247 _gtk_widget_segment_remove (GtkTextLineSegment *widget_segment,
248                             GtkWidget          *child)
249 {
250   g_return_if_fail (widget_segment->type = &gtk_text_child_type);
251   
252   widget_segment->body.child.widgets =
253     g_slist_remove (widget_segment->body.child.widgets,
254                     child);
255
256   g_object_unref (G_OBJECT (child));
257 }
258
259 void
260 _gtk_widget_segment_ref (GtkTextLineSegment *widget_segment)
261 {
262   g_assert (widget_segment->type = &gtk_text_child_type);
263
264   g_object_ref (G_OBJECT (widget_segment->body.child.obj));
265 }
266
267 void
268 _gtk_widget_segment_unref (GtkTextLineSegment *widget_segment)
269 {
270   g_assert (widget_segment->type = &gtk_text_child_type);
271
272   g_object_unref (G_OBJECT (widget_segment->body.child.obj));
273 }
274
275 GtkTextLayout*
276 _gtk_anchored_child_get_layout (GtkWidget *child)
277 {
278   return g_object_get_data (G_OBJECT (child), "gtk-text-child-anchor-layout");  
279 }
280
281 static void
282 _gtk_anchored_child_set_layout (GtkWidget     *child,
283                                 GtkTextLayout *layout)
284 {
285   g_object_set_data (G_OBJECT (child),
286                      "gtk-text-child-anchor-layout",
287                      layout);  
288 }
289      
290 static void gtk_text_child_anchor_init       (GtkTextChildAnchor      *child_anchor);
291 static void gtk_text_child_anchor_class_init (GtkTextChildAnchorClass *klass);
292 static void gtk_text_child_anchor_finalize   (GObject                 *obj);
293
294 static gpointer parent_class = NULL;
295
296 GType
297 gtk_text_child_anchor_get_type (void)
298 {
299   static GType object_type = 0;
300
301   if (!object_type)
302     {
303       static const GTypeInfo object_info =
304       {
305         sizeof (GtkTextChildAnchorClass),
306         (GBaseInitFunc) NULL,
307         (GBaseFinalizeFunc) NULL,
308         (GClassInitFunc) gtk_text_child_anchor_class_init,
309         NULL,           /* class_finalize */
310         NULL,           /* class_data */
311         sizeof (GtkTextChildAnchor),
312         0,              /* n_preallocs */
313         (GInstanceInitFunc) gtk_text_child_anchor_init,
314       };
315
316       object_type = g_type_register_static (G_TYPE_OBJECT,
317                                             "GtkTextChildAnchor",
318                                             &object_info, 0);
319     }
320
321   return object_type;
322 }
323
324 static void
325 gtk_text_child_anchor_init (GtkTextChildAnchor *child_anchor)
326 {
327   child_anchor->segment = NULL;
328 }
329
330 static void
331 gtk_text_child_anchor_class_init (GtkTextChildAnchorClass *klass)
332 {
333   GObjectClass *object_class = G_OBJECT_CLASS (klass);
334
335   parent_class = g_type_class_peek_parent (klass);
336
337   object_class->finalize = gtk_text_child_anchor_finalize;
338 }
339
340 /**
341  * gtk_text_child_anchor_new:
342  * 
343  * Creates a new #GtkTextChildAnchor. Usually you would then insert
344  * it into a #GtkTextBuffer with gtk_text_buffer_insert_child_anchor().
345  * To perform the creation and insertion in one step, use the
346  * convenience function gtk_text_buffer_create_child_anchor().
347  * 
348  * Return value: a new #GtkTextChildAnchor
349  **/
350 GtkTextChildAnchor*
351 gtk_text_child_anchor_new (void)
352 {
353   return GTK_TEXT_CHILD_ANCHOR (g_object_new (GTK_TYPE_TEXT_CHILD_ANCHOR,
354                                               NULL));
355 }
356
357 static void
358 gtk_text_child_anchor_finalize (GObject *obj)
359 {
360   GtkTextChildAnchor *anchor;
361   GSList *tmp_list;
362   GtkTextLineSegment *seg;
363   
364   anchor = GTK_TEXT_CHILD_ANCHOR (obj);
365
366   seg = anchor->segment;
367   
368   if (seg->body.child.tree != NULL)
369     {
370       g_warning ("Someone removed a reference to a GtkTextChildAnchor "
371                  "they didn't own; the anchor is still in the text buffer "
372                  "and the refcount is 0.");
373       return;
374     }
375       
376   tmp_list = seg->body.child.widgets;
377   while (tmp_list)
378     {
379       g_object_unref (G_OBJECT (tmp_list->data));
380       
381       tmp_list = g_slist_next (tmp_list);
382     }
383   
384   g_slist_free (seg->body.child.widgets);
385   
386   g_free (seg);
387
388   anchor->segment = NULL;
389 }
390
391 /**
392  * gtk_text_child_anchor_get_widgets:
393  * @anchor: a #GtkTextChildAnchor
394  * 
395  * Gets a list of all widgets anchored at this child anchor.
396  * The returned list should be freed with g_list_free().
397  * 
398  * 
399  * Return value: list of widgets anchored at @anchor
400  **/
401 GList*
402 gtk_text_child_anchor_get_widgets (GtkTextChildAnchor *anchor)
403 {
404   GtkTextLineSegment *seg = anchor->segment;
405   GList *list = NULL;
406   GSList *iter;
407
408   CHECK_IN_BUFFER_RETURN (anchor, NULL);
409   
410   g_return_val_if_fail (seg->type = &gtk_text_child_type, NULL);
411
412   iter = seg->body.child.widgets;
413   while (iter != NULL)
414     {
415       list = g_list_prepend (list, iter->data);
416
417       iter = g_slist_next (iter);
418     }
419
420   /* Order is not relevant, so we don't need to reverse the list
421    * again.
422    */
423   return list;
424 }
425
426 /**
427  * gtk_text_child_anchor_get_deleted:
428  * @anchor: a #GtkTextChildAnchor
429  * 
430  * Determines whether a child anchor has been deleted from
431  * the buffer. Keep in mind that the child anchor will be
432  * unreferenced when removed from the buffer, so you need to
433  * hold your own reference (with g_object_ref()) if you plan
434  * to use this function &mdash; otherwise all deleted child anchors
435  * will also be finalized.
436  * 
437  * Return value: %TRUE if the child anchor has been deleted from its buffer
438  **/
439 gboolean
440 gtk_text_child_anchor_get_deleted (GtkTextChildAnchor *anchor)
441 {
442   GtkTextLineSegment *seg = anchor->segment;
443
444   CHECK_IN_BUFFER_RETURN (anchor, TRUE);
445   
446   g_return_val_if_fail (seg->type = &gtk_text_child_type, TRUE);
447
448   return seg->body.child.tree == NULL;
449 }
450
451 void
452 gtk_text_child_anchor_register_child (GtkTextChildAnchor *anchor,
453                                       GtkWidget          *child,
454                                       GtkTextLayout      *layout)
455 {
456   g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
457   g_return_if_fail (GTK_IS_WIDGET (child));
458
459   CHECK_IN_BUFFER (anchor);
460   
461   _gtk_anchored_child_set_layout (child, layout);
462   
463   _gtk_widget_segment_add (anchor->segment, child);
464
465   gtk_text_child_anchor_queue_resize (anchor, layout);
466 }
467
468 void
469 gtk_text_child_anchor_unregister_child (GtkTextChildAnchor *anchor,
470                                         GtkWidget          *child)
471 {
472   g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
473   g_return_if_fail (GTK_IS_WIDGET (child));
474
475   CHECK_IN_BUFFER (anchor);
476   
477   if (_gtk_anchored_child_get_layout (child))
478     {
479       gtk_text_child_anchor_queue_resize (anchor,
480                                           _gtk_anchored_child_get_layout (child));
481     }
482   
483   _gtk_anchored_child_set_layout (child, NULL);
484   
485   _gtk_widget_segment_remove (anchor->segment, child);
486 }
487
488 void
489 gtk_text_child_anchor_queue_resize (GtkTextChildAnchor *anchor,
490                                     GtkTextLayout      *layout)
491 {
492   GtkTextIter start;
493   GtkTextIter end;
494   GtkTextLineSegment *seg;
495   
496   g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
497   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
498
499   CHECK_IN_BUFFER (anchor);
500   
501   seg = anchor->segment;
502
503   if (seg->body.child.tree == NULL)
504     return;
505   
506   gtk_text_buffer_get_iter_at_child_anchor (layout->buffer,
507                                             &start, anchor);
508   end = start;
509   gtk_text_iter_forward_char (&end);
510   
511   gtk_text_layout_invalidate (layout, &start, &end);
512 }
513
514 void
515 gtk_text_anchored_child_set_layout (GtkWidget     *child,
516                                     GtkTextLayout *layout)
517 {
518   g_return_if_fail (GTK_IS_WIDGET (child));
519   g_return_if_fail (layout == NULL || GTK_IS_TEXT_LAYOUT (layout));
520   
521   _gtk_anchored_child_set_layout (child, layout);
522 }
523
524