aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-table-memory-callbacks.c
blob: a3f919b98139c051932558b4963a340a67c635ad (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Chris Lahey <clahey@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-table-memory-callbacks.h"

G_DEFINE_TYPE (ETableMemoryCallbacks, e_table_memory_callbacks, E_TYPE_TABLE_MEMORY)

static gint
etmc_column_count (ETableModel *etm)
{
    ETableMemoryCallbacks *etmc = E_TABLE_MEMORY_CALLBACKS (etm);

    if (etmc->col_count)
        return etmc->col_count (etm, etmc->data);
    else
        return 0;
}

static gpointer
etmc_value_at (ETableModel *etm,
               gint col,
               gint row)
{
    ETableMemoryCallbacks *etmc = E_TABLE_MEMORY_CALLBACKS (etm);

    if (etmc->value_at)
        return etmc->value_at (etm, col, row, etmc->data);
    else
        return NULL;
}

static void
etmc_set_value_at (ETableModel *etm,
                   gint col,
                   gint row,
                   gconstpointer val)
{
    ETableMemoryCallbacks *etmc = E_TABLE_MEMORY_CALLBACKS (etm);

    if (etmc->set_value_at)
        etmc->set_value_at (etm, col, row, val, etmc->data);
}

static gboolean
etmc_is_cell_editable (ETableModel *etm,
                       gint col,
                       gint row)
{
    ETableMemoryCallbacks *etmc = E_TABLE_MEMORY_CALLBACKS (etm);

    if (etmc->is_cell_editable)
        return etmc->is_cell_editable (etm, col, row, etmc->data);
    else
        return FALSE;
}

/* The default for etmc_duplicate_value is to return the raw value. */
static gpointer
etmc_duplicate_value (ETableModel *etm,
                      gint col,
                      gconstpointer value)
{
    ETableMemoryCallbacks *etmc = E_TABLE_MEMORY_CALLBACKS (etm);

    if (etmc->duplicate_value)
        return etmc->duplicate_value (etm, col, value, etmc->data);
    else
        return (gpointer) value;
}

static void
etmc_free_value (ETableModel *etm,
                 gint col,
                 gpointer value)
{
    ETableMemoryCallbacks *etmc = E_TABLE_MEMORY_CALLBACKS (etm);

    if (etmc->free_value)
        etmc->free_value (etm, col, value, etmc->data);
}

static gpointer
etmc_initialize_value (ETableModel *etm,
                       gint col)
{
    ETableMemoryCallbacks *etmc = E_TABLE_MEMORY_CALLBACKS (etm);

    if (etmc->initialize_value)
        return etmc->initialize_value (etm, col, etmc->data);
    else
        return NULL;
}

static gboolean
etmc_value_is_empty (ETableModel *etm,
                     gint col,
                     gconstpointer value)
{
    ETableMemoryCallbacks *etmc = E_TABLE_MEMORY_CALLBACKS (etm);

    if (etmc->value_is_empty)
        return etmc->value_is_empty (etm, col, value, etmc->data);
    else
        return FALSE;
}

static gchar *
etmc_value_to_string (ETableModel *etm,
                      gint col,
                      gconstpointer value)
{
    ETableMemoryCallbacks *etmc = E_TABLE_MEMORY_CALLBACKS (etm);

    if (etmc->value_to_string)
        return etmc->value_to_string (etm, col, value, etmc->data);
    else
        return g_strdup ("");
}

static void
etmc_append_row (ETableModel *etm,
                 ETableModel *source,
                 gint row)
{
    ETableMemoryCallbacks *etmc = E_TABLE_MEMORY_CALLBACKS (etm);

    if (etmc->append_row)
        etmc->append_row (etm, source, row, etmc->data);
}

static void
e_table_memory_callbacks_class_init (ETableMemoryCallbacksClass *class)
{
    ETableModelClass *model_class = E_TABLE_MODEL_CLASS (class);

    model_class->column_count     = etmc_column_count;
    model_class->value_at         = etmc_value_at;
    model_class->set_value_at     = etmc_set_value_at;
    model_class->is_cell_editable = etmc_is_cell_editable;
    model_class->duplicate_value  = etmc_duplicate_value;
    model_class->free_value       = etmc_free_value;
    model_class->initialize_value = etmc_initialize_value;
    model_class->value_is_empty   = etmc_value_is_empty;
    model_class->value_to_string  = etmc_value_to_string;
    model_class->append_row       = etmc_append_row;

}

static void
e_table_memory_callbacks_init (ETableMemoryCallbacks *etmc)
{
    /* nothing to do */
}

/**
 * e_table_memory_callbacks_new:
 * @col_count:
 * @value_at:
 * @set_value_at:
 * @is_cell_editable:
 * @duplicate_value:
 * @free_value:
 * @initialize_value:
 * @value_is_empty:
 * @value_to_string:
 * @data: closure pointer.
 *
 * This initializes a new ETableMemoryCallbacksModel object.
 * ETableMemoryCallbacksModel is an implementaiton of the abstract class
 * ETableModel.  The ETableMemoryCallbacksModel is designed to allow people
 * to easily create ETableModels without having to create a new GType
 * derived from ETableModel every time they need one.
 *
 * Instead, ETableMemoryCallbacksModel uses a setup based in callback
 * functions, every callback function signature mimics the signature of
 * each ETableModel method and passes the extra @data pointer to each one
 * of the method to provide them with any context they might want to use.
 *
 * Returns: An ETableMemoryCallbacksModel object (which is also an ETableModel
 * object).
 */
ETableModel *
e_table_memory_callbacks_new (ETableMemoryCallbacksColumnCountFn col_count,
                              ETableMemoryCallbacksValueAtFn value_at,
                              ETableMemoryCallbacksSetValueAtFn set_value_at,
                              ETableMemoryCallbacksIsCellEditableFn is_cell_editable,
                              ETableMemoryCallbacksDuplicateValueFn duplicate_value,
                              ETableMemoryCallbacksFreeValueFn free_value,
                              ETableMemoryCallbacksInitializeValueFn initialize_value,
                              ETableMemoryCallbacksValueIsEmptyFn value_is_empty,
                              ETableMemoryCallbacksValueToStringFn value_to_string,
                              gpointer data)
{
    ETableMemoryCallbacks *et;

    et = g_object_new (E_TYPE_TABLE_MEMORY_CALLBACKS, NULL);

    et->col_count        = col_count;
    et->value_at         = value_at;
    et->set_value_at     = set_value_at;
    et->is_cell_editable = is_cell_editable;
    et->duplicate_value  = duplicate_value;
    et->free_value       = free_value;
    et->initialize_value = initialize_value;
    et->value_is_empty   = value_is_empty;
    et->value_to_string  = value_to_string;
    et->data             = data;

    return (ETableModel *) et;
 }