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