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