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