]> Pileus Git - ~andy/gtk/blob - modules/other/gail/gailcontainercell.c
7fd38bd675bc7bca61fbd3c4d1aafa7e0f4d393a
[~andy/gtk] / modules / other / gail / gailcontainercell.c
1 /* GAIL - The GNOME Accessibility Enabling Library
2  * Copyright 2001 Sun Microsystems Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <gtk/gtk.h>
21 #include "gailcontainercell.h"
22
23 static void       gail_container_cell_class_init          (GailContainerCellClass *klass);
24 static void       gail_container_cell_init                (GailContainerCell   *cell);
25 static void       gail_container_cell_finalize            (GObject             *obj);
26
27
28 static void       _gail_container_cell_recompute_child_indices 
29                                                           (GailContainerCell *container);
30
31 static void       gail_container_cell_refresh_child_index (GailCell *cell);
32
33 static gint       gail_container_cell_get_n_children      (AtkObject *obj);
34
35 static AtkObject* gail_container_cell_ref_child           (AtkObject *obj,
36                                                            gint      child);
37
38 G_DEFINE_TYPE (GailContainerCell, gail_container_cell, GAIL_TYPE_CELL)
39
40 static void 
41 gail_container_cell_class_init (GailContainerCellClass *klass)
42 {
43   AtkObjectClass *class = ATK_OBJECT_CLASS(klass);
44   GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
45
46   g_object_class->finalize = gail_container_cell_finalize;
47
48   class->get_n_children = gail_container_cell_get_n_children;
49   class->ref_child = gail_container_cell_ref_child;
50 }
51
52 static void
53 gail_container_cell_init (GailContainerCell   *cell)
54 {
55 }
56
57 GailContainerCell * 
58 gail_container_cell_new (void)
59 {
60   GObject *object;
61   AtkObject *atk_object;
62   GailContainerCell *container;
63
64   object = g_object_new (GAIL_TYPE_CONTAINER_CELL, NULL);
65
66   g_return_val_if_fail (object != NULL, NULL);
67
68   atk_object = ATK_OBJECT (object);
69   atk_object->role = ATK_ROLE_TABLE_CELL;
70
71   container = GAIL_CONTAINER_CELL(object);
72   container->children = NULL;
73   container->NChildren = 0;
74   return container;
75 }
76
77 static void
78 gail_container_cell_finalize (GObject *obj)
79 {
80   GailContainerCell *container = GAIL_CONTAINER_CELL (obj);
81   GList *list;
82
83   list = container->children;
84   while (list)
85   {
86     g_object_unref (list->data);
87     list = list->next;
88   }
89   g_list_free (container->children);
90   
91   G_OBJECT_CLASS (gail_container_cell_parent_class)->finalize (obj);
92 }
93
94
95 void
96 gail_container_cell_add_child (GailContainerCell *container,
97                                GailCell *child)
98 {
99   gint child_index;
100
101   g_return_if_fail (GAIL_IS_CONTAINER_CELL(container));
102   g_return_if_fail (GAIL_IS_CELL(child));
103
104   child_index = container->NChildren++;
105   container->children = g_list_append (container->children, (gpointer) child);
106   child->index = child_index;
107   atk_object_set_parent (ATK_OBJECT (child), ATK_OBJECT (container));
108   child->refresh_index = gail_container_cell_refresh_child_index;
109 }
110
111
112 void
113 gail_container_cell_remove_child (GailContainerCell *container,
114                                   GailCell *child)
115 {
116   g_return_if_fail (GAIL_IS_CONTAINER_CELL(container));
117   g_return_if_fail (GAIL_IS_CELL(child));
118   g_return_if_fail (container->NChildren > 0);
119
120   g_list_remove (container->children, (gpointer) child);
121   _gail_container_cell_recompute_child_indices (container);
122   container->NChildren--;
123 }
124
125
126 static void
127 _gail_container_cell_recompute_child_indices (GailContainerCell *container)
128 {
129   gint cur_index = 0;
130   GList *temp_list;
131
132   g_return_if_fail (GAIL_IS_CONTAINER_CELL(container));
133
134   for (temp_list = container->children; temp_list; temp_list = temp_list->next)
135     {
136       GAIL_CELL(temp_list->data)->index = cur_index;
137       cur_index++;
138     }
139 }
140
141
142 static void
143 gail_container_cell_refresh_child_index (GailCell *cell)
144 {
145   GailContainerCell *container;
146   g_return_if_fail (GAIL_IS_CELL(cell));
147   container = GAIL_CONTAINER_CELL (atk_object_get_parent (ATK_OBJECT(cell)));
148   g_return_if_fail (GAIL_IS_CONTAINER_CELL (container));
149   _gail_container_cell_recompute_child_indices (container);
150 }
151
152
153
154 static gint
155 gail_container_cell_get_n_children (AtkObject *obj)
156 {
157   GailContainerCell *cell;
158   g_return_val_if_fail (GAIL_IS_CONTAINER_CELL(obj), 0);
159   cell = GAIL_CONTAINER_CELL(obj);
160   return cell->NChildren;
161 }
162
163
164 static AtkObject *
165 gail_container_cell_ref_child (AtkObject *obj,
166                                gint       child)
167 {
168   GailContainerCell *cell;
169   GList *list_node;
170
171   g_return_val_if_fail (GAIL_IS_CONTAINER_CELL(obj), NULL);
172   cell = GAIL_CONTAINER_CELL(obj);
173   
174   list_node = g_list_nth (cell->children, child);
175   if (!list_node)
176     return NULL;
177
178   return g_object_ref (ATK_OBJECT (list_node->data));
179 }