aboutsummaryrefslogtreecommitdiffstats
path: root/a11y/ea-cell-table.c
blob: 1f79d3b525296c6e5de032c7f74a5f3da5d8f368 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* vim:expandtab:shiftwidth=8:tabstop=8:
 */
/* Evolution Accessibility: ea-cell-table.c
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Author: Bolian Yin <bolian.yin@sun.com> Sun Microsystem Inc., 2003
 *
 */

#include "ea-cell-table.h"

EaCellTable *
ea_cell_table_create (gint rows, gint columns, gboolean column_first)
{
    EaCellTable * cell_data;
    gint index;

    g_return_val_if_fail (((columns > 0) && (rows > 0)), NULL);

    cell_data = g_new0 (EaCellTable, 1);

    cell_data->column_first = column_first;
    cell_data->columns = columns;
    cell_data->rows = rows;

    cell_data->column_labels = g_new0 (gchar*, columns);
    for (index = columns -1; index >= 0; --index)
        cell_data->column_labels [index] = NULL;

    cell_data->row_labels = g_new0 (gchar*, rows);
    for (index = rows -1; index >= 0; --index)
        cell_data->row_labels [index] = NULL;

    cell_data->cells = g_new0(gpointer, (columns * rows));
    for (index = (columns * rows) -1; index >= 0; --index)
        cell_data->cells [index] = NULL;
    return cell_data;
}

void
ea_cell_table_destroy (EaCellTable * cell_data)
{
    gint index;
    g_return_if_fail (cell_data);

    for (index = 0; index < cell_data->columns; ++index)
        if (cell_data->column_labels [index])
            g_free (cell_data->column_labels [index]);
    g_free (cell_data->column_labels);

    for (index = 0; index < cell_data->rows; ++index)
        if (cell_data->row_labels [index])
            g_free (cell_data->row_labels [index]);
    g_free (cell_data->row_labels);

    for (index = (cell_data->columns * cell_data->rows) -1;
         index >= 0; --index)
        if (cell_data->cells[index] &&
            G_IS_OBJECT (cell_data->cells[index]))
            g_object_unref (cell_data->cells[index]);

    g_free (cell_data->cells);
}

gpointer
ea_cell_table_get_cell (EaCellTable * cell_data,
            gint row, gint column)
{
    gint index;

    g_return_val_if_fail (cell_data, NULL);

    index = ea_cell_table_get_index (cell_data, column, row);
    if (index == -1)
        return NULL;

    return cell_data->cells[index];
}

gboolean
ea_cell_table_set_cell (EaCellTable * cell_data,
            gint row, gint column, gpointer cell)
{
    gint index;

    g_return_val_if_fail (cell_data, FALSE);

    index = ea_cell_table_get_index (cell_data, column, row);
    if (index == -1)
        return FALSE;

    if (cell && G_IS_OBJECT(cell))
        g_object_ref (cell);
    if (cell_data->cells[index] &&
        G_IS_OBJECT (cell_data->cells[index]))
        g_object_unref (cell_data->cells[index]);
    cell_data->cells[index] = cell;

    return TRUE;
}

gpointer
ea_cell_table_get_cell_at_index (EaCellTable * cell_data,
                 gint index)
{
    g_return_val_if_fail (cell_data, NULL);

    if (index >=0 && index < (cell_data->columns * cell_data->rows))
        return cell_data->cells [index];
    return NULL;
}

gboolean
ea_cell_table_set_cell_at_index (EaCellTable * cell_data,
                 gint index, gpointer cell)
{
    g_return_val_if_fail (cell_data, FALSE);

    if (index < 0 || index >=cell_data->columns * cell_data->rows)
        return FALSE;

    if (cell && G_IS_OBJECT(cell))
        g_object_ref (cell);
    if (cell_data->cells[index] &&
        G_IS_OBJECT (cell_data->cells[index]))
        g_object_unref (cell_data->cells[index]);
    cell_data->cells[index] = cell;

    return TRUE;
}

G_CONST_RETURN gchar*
ea_cell_table_get_column_label (EaCellTable * cell_data,
                gint column)
{
    g_return_val_if_fail (cell_data, NULL);
    g_return_val_if_fail ((column >= 0 && column < cell_data->columns), NULL);

    return cell_data->column_labels[column];
}

void
ea_cell_table_set_column_label (EaCellTable * cell_data,
                gint column, const gchar *label)
{
    g_return_if_fail (cell_data);
    g_return_if_fail ((column >= 0 && column < cell_data->columns));

    if (cell_data->column_labels[column])
        g_free (cell_data->column_labels[column]);
    cell_data->column_labels[column] = g_strdup(label);
}

G_CONST_RETURN gchar*
ea_cell_table_get_row_label (EaCellTable * cell_data,
                 gint row)
{
    g_return_val_if_fail (cell_data, NULL);
    g_return_val_if_fail ((row >= 0 && row < cell_data->rows), NULL);

    return cell_data->row_labels[row];
}

void
ea_cell_table_set_row_label (EaCellTable * cell_data,
                 gint row, const gchar *label)
{
    g_return_if_fail (cell_data);
    g_return_if_fail ((row >= 0 && row < cell_data->rows));

    if (cell_data->row_labels[row])
        g_free (cell_data->row_labels[row]);
    cell_data->row_labels[row] = g_strdup(label);
}

gint
ea_cell_table_get_index (EaCellTable *cell_data,
             gint row, gint column)
{
    g_return_val_if_fail (cell_data, -1);
    if (row < 0 || row >= cell_data->rows ||
        column < 0 || column >= cell_data->columns)
        return -1;

    if (cell_data->column_first)
        return column * cell_data->rows + row;
    else
        return row * cell_data->columns + column;
}