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