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