No issues found
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2 of the License, or (at your option) version 3.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with the program; if not, see <http://www.gnu.org/licenses/>
14 *
15 *
16 * Authors:
17 * Bolian Yin <bolian.yin@sun.com>
18 *
19 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
20 *
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "ea-cell-table.h"
28
29 EaCellTable *
30 ea_cell_table_create (gint rows,
31 gint columns,
32 gboolean column_first)
33 {
34 EaCellTable *cell_data;
35 gint index;
36
37 g_return_val_if_fail (((columns > 0) && (rows > 0)), NULL);
38
39 cell_data = g_new0 (EaCellTable, 1);
40
41 cell_data->column_first = column_first;
42 cell_data->columns = columns;
43 cell_data->rows = rows;
44
45 cell_data->column_labels = g_new0 (gchar *, columns);
46 for (index = columns -1; index >= 0; --index)
47 cell_data->column_labels[index] = NULL;
48
49 cell_data->row_labels = g_new0 (gchar *, rows);
50 for (index = rows -1; index >= 0; --index)
51 cell_data->row_labels[index] = NULL;
52
53 cell_data->cells = g_new0 (gpointer, (columns * rows));
54 for (index = (columns * rows) -1; index >= 0; --index)
55 cell_data->cells[index] = NULL;
56 return cell_data;
57 }
58
59 void
60 ea_cell_table_destroy (EaCellTable *cell_data)
61 {
62 gint index;
63 g_return_if_fail (cell_data);
64
65 for (index = 0; index < cell_data->columns; ++index)
66 if (cell_data->column_labels[index])
67 g_free (cell_data->column_labels[index]);
68 g_free (cell_data->column_labels);
69
70 for (index = 0; index < cell_data->rows; ++index)
71 if (cell_data->row_labels[index])
72 g_free (cell_data->row_labels[index]);
73 g_free (cell_data->row_labels);
74
75 for (index = (cell_data->columns * cell_data->rows) -1;
76 index >= 0; --index)
77 if (cell_data->cells[index] &&
78 G_IS_OBJECT (cell_data->cells[index]))
79 g_object_unref (cell_data->cells[index]);
80
81 g_free (cell_data->cells);
82 }
83
84 gpointer
85 ea_cell_table_get_cell (EaCellTable *cell_data,
86 gint row,
87 gint column)
88 {
89 gint index;
90
91 g_return_val_if_fail (cell_data, NULL);
92
93 index = ea_cell_table_get_index (cell_data, column, row);
94 if (index == -1)
95 return NULL;
96
97 return cell_data->cells[index];
98 }
99
100 gboolean
101 ea_cell_table_set_cell (EaCellTable *cell_data,
102 gint row,
103 gint column,
104 gpointer cell)
105 {
106 gint index;
107
108 g_return_val_if_fail (cell_data, FALSE);
109
110 index = ea_cell_table_get_index (cell_data, column, row);
111 if (index == -1)
112 return FALSE;
113
114 if (cell && G_IS_OBJECT (cell))
115 g_object_ref (cell);
116 if (cell_data->cells[index] &&
117 G_IS_OBJECT (cell_data->cells[index]))
118 g_object_unref (cell_data->cells[index]);
119 cell_data->cells[index] = cell;
120
121 return TRUE;
122 }
123
124 gpointer
125 ea_cell_table_get_cell_at_index (EaCellTable *cell_data,
126 gint index)
127 {
128 g_return_val_if_fail (cell_data, NULL);
129
130 if (index >=0 && index < (cell_data->columns * cell_data->rows))
131 return cell_data->cells[index];
132 return NULL;
133 }
134
135 gboolean
136 ea_cell_table_set_cell_at_index (EaCellTable *cell_data,
137 gint index,
138 gpointer cell)
139 {
140 g_return_val_if_fail (cell_data, FALSE);
141
142 if (index < 0 || index >=cell_data->columns * cell_data->rows)
143 return FALSE;
144
145 if (cell && G_IS_OBJECT (cell))
146 g_object_ref (cell);
147 if (cell_data->cells[index] &&
148 G_IS_OBJECT (cell_data->cells[index]))
149 g_object_unref (cell_data->cells[index]);
150 cell_data->cells[index] = cell;
151
152 return TRUE;
153 }
154
155 const gchar *
156 ea_cell_table_get_column_label (EaCellTable *cell_data,
157 gint column)
158 {
159 g_return_val_if_fail (cell_data, NULL);
160 g_return_val_if_fail ((column >= 0 && column < cell_data->columns), NULL);
161
162 return cell_data->column_labels[column];
163 }
164
165 void
166 ea_cell_table_set_column_label (EaCellTable *cell_data,
167 gint column,
168 const gchar *label)
169 {
170 g_return_if_fail (cell_data);
171 g_return_if_fail ((column >= 0 && column < cell_data->columns));
172
173 if (cell_data->column_labels[column])
174 g_free (cell_data->column_labels[column]);
175 cell_data->column_labels[column] = g_strdup (label);
176 }
177
178 const gchar *
179 ea_cell_table_get_row_label (EaCellTable *cell_data,
180 gint row)
181 {
182 g_return_val_if_fail (cell_data, NULL);
183 g_return_val_if_fail ((row >= 0 && row < cell_data->rows), NULL);
184
185 return cell_data->row_labels[row];
186 }
187
188 void
189 ea_cell_table_set_row_label (EaCellTable *cell_data,
190 gint row,
191 const gchar *label)
192 {
193 g_return_if_fail (cell_data);
194 g_return_if_fail ((row >= 0 && row < cell_data->rows));
195
196 if (cell_data->row_labels[row])
197 g_free (cell_data->row_labels[row]);
198 cell_data->row_labels[row] = g_strdup (label);
199 }
200
201 gint
202 ea_cell_table_get_index (EaCellTable *cell_data,
203 gint row,
204 gint column)
205 {
206 g_return_val_if_fail (cell_data, -1);
207 if (row < 0 || row >= cell_data->rows ||
208 column < 0 || column >= cell_data->columns)
209 return -1;
210
211 if (cell_data->column_first)
212 return column * cell_data->rows + row;
213 else
214 return row * cell_data->columns + column;
215 }