aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/table/e-table-subset-variable.c
blob: 6cc28e540b78b85bf70e5be7632b65de73995cf2 (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * e-table-subset-variable.c
 * Copyright 2000, 2001, Ximian, Inc.
 *
 * Authors:
 *   Chris Lahey <clahey@ximian.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License, version 2, as published by the Free Software Foundation.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include <config.h>

#include <stdlib.h>
#include <string.h>

#include "gal/util/e-util.h"

#include "e-table-subset-variable.h"

#define ETSSV_CLASS(e) (E_TABLE_SUBSET_VARIABLE_GET_CLASS (e))

#define INCREMENT_AMOUNT 10

static void
etssv_add       (ETableSubsetVariable *etssv,
         gint                  row)
{
    ETableModel *etm = E_TABLE_MODEL(etssv);
    ETableSubset *etss = E_TABLE_SUBSET(etssv);
    
    e_table_model_pre_change(etm);

    if (etss->n_map + 1 > etssv->n_vals_allocated){
        etssv->n_vals_allocated += INCREMENT_AMOUNT;
        etss->map_table = g_realloc (etss->map_table, etssv->n_vals_allocated * sizeof(int));
    }

    etss->map_table[etss->n_map++] = row;

    e_table_model_row_inserted (etm, etss->n_map - 1);
}

static void
etssv_add_array (ETableSubsetVariable *etssv,
         const gint           *array,
         gint                  count)
{
    ETableModel *etm = E_TABLE_MODEL(etssv);
    ETableSubset *etss = E_TABLE_SUBSET(etssv);
    int i;

    e_table_model_pre_change(etm);
    
    if (etss->n_map + count > etssv->n_vals_allocated){
        etssv->n_vals_allocated += MAX(INCREMENT_AMOUNT, count);
        etss->map_table = g_realloc (etss->map_table, etssv->n_vals_allocated * sizeof(int));
    }
    for (i = 0; i < count; i++)
        etss->map_table[etss->n_map++] = array[i];

    e_table_model_changed (etm);
}

static void
etssv_add_all   (ETableSubsetVariable *etssv)
{
    ETableModel *etm = E_TABLE_MODEL(etssv);
    ETableSubset *etss = E_TABLE_SUBSET(etssv);
    int rows;
    int i;

    e_table_model_pre_change(etm);
    
    rows = e_table_model_row_count(etss->source);
    if (etss->n_map + rows > etssv->n_vals_allocated){
        etssv->n_vals_allocated += MAX(INCREMENT_AMOUNT, rows);
        etss->map_table = g_realloc (etss->map_table, etssv->n_vals_allocated * sizeof(int));
    }
    for (i = 0; i < rows; i++)
        etss->map_table[etss->n_map++] = i;

    e_table_model_changed (etm);
}

static gboolean
etssv_remove    (ETableSubsetVariable *etssv,
         gint                  row)
{
    ETableModel *etm = E_TABLE_MODEL(etssv);
    ETableSubset *etss = E_TABLE_SUBSET(etssv);
    int i;
    
    for (i = 0; i < etss->n_map; i++){
        if (etss->map_table[i] == row) {
            e_table_model_pre_change (etm);
            memmove (etss->map_table + i, etss->map_table + i + 1, (etss->n_map - i - 1) * sizeof(int));
            etss->n_map --;
            
            e_table_model_row_deleted (etm, i);
            return TRUE;
        }
    }
    return FALSE;
}

static void
etssv_class_init (GObjectClass *object_class)
{
    ETableSubsetVariableClass *klass = E_TABLE_SUBSET_VARIABLE_CLASS(object_class);

    klass->add     = etssv_add;
    klass->add_array = etssv_add_array;
    klass->add_all = etssv_add_all;
    klass->remove  = etssv_remove;
}

E_MAKE_TYPE(e_table_subset_variable, "ETableSubsetVariable", ETableSubsetVariable, etssv_class_init, NULL, E_TABLE_SUBSET_TYPE)

ETableModel *
e_table_subset_variable_construct (ETableSubsetVariable *etssv,
                   ETableModel          *source)
{
    if (e_table_subset_construct (E_TABLE_SUBSET(etssv), source, 1) == NULL)
        return NULL;
    E_TABLE_SUBSET(etssv)->n_map = 0;

    return E_TABLE_MODEL (etssv);
}

ETableModel *
e_table_subset_variable_new (ETableModel *source)
{
    ETableSubsetVariable *etssv = g_object_new (E_TABLE_SUBSET_VARIABLE_TYPE, NULL);

    if (e_table_subset_variable_construct (etssv, source) == NULL){
        g_object_unref (etssv);
        return NULL;
    }

    return (ETableModel *) etssv;
}

void
e_table_subset_variable_add       (ETableSubsetVariable *etssv,
                   gint                  row)
{
    g_return_if_fail (etssv != NULL);
    g_return_if_fail (E_IS_TABLE_SUBSET_VARIABLE(etssv));

    if (ETSSV_CLASS(etssv)->add)
        ETSSV_CLASS (etssv)->add (etssv, row);
}

void
e_table_subset_variable_add_array   (ETableSubsetVariable *etssv,
                     const gint           *array,
                     gint                  count)
{
    g_return_if_fail (etssv != NULL);
    g_return_if_fail (E_IS_TABLE_SUBSET_VARIABLE(etssv));

    if (ETSSV_CLASS(etssv)->add_array)
        ETSSV_CLASS (etssv)->add_array (etssv, array, count);
}

void
e_table_subset_variable_add_all   (ETableSubsetVariable *etssv)
{
    g_return_if_fail (etssv != NULL);
    g_return_if_fail (E_IS_TABLE_SUBSET_VARIABLE(etssv));

    if (ETSSV_CLASS(etssv)->add_all)
        ETSSV_CLASS (etssv)->add_all (etssv);
}

gboolean
e_table_subset_variable_remove    (ETableSubsetVariable *etssv,
                   gint                  row)
{
    g_return_val_if_fail (etssv != NULL, FALSE);
    g_return_val_if_fail (E_IS_TABLE_SUBSET_VARIABLE(etssv), FALSE);

    if (ETSSV_CLASS(etssv)->remove)
        return ETSSV_CLASS (etssv)->remove (etssv, row);
    else
        return FALSE;
}

void
e_table_subset_variable_clear (ETableSubsetVariable *etssv)
{
    ETableModel *etm = E_TABLE_MODEL(etssv);
    ETableSubset *etss = E_TABLE_SUBSET(etssv);

    e_table_model_pre_change (etm);
    etss->n_map = 0;
    g_free (etss->map_table);
    etss->map_table = g_new (unsigned int, 1);
    etssv->n_vals_allocated = 1;
    
    e_table_model_changed (etm);
}

void
e_table_subset_variable_increment (ETableSubsetVariable *etssv,
                   gint                  position,
                   gint                  amount)
{
    int i;
    ETableSubset *etss = E_TABLE_SUBSET(etssv);
    for (i = 0; i < etss->n_map; i++) {
        if (etss->map_table[i] >= position)
            etss->map_table[i] += amount;
    }
}

void
e_table_subset_variable_decrement (ETableSubsetVariable *etssv,
                   gint                  position,
                   gint                  amount)
{
    int i;
    ETableSubset *etss = E_TABLE_SUBSET(etssv);
    for (i = 0; i < etss->n_map; i++) {
        if (etss->map_table[i] >= position)
            etss->map_table[i] -= amount;
    }
}

void
e_table_subset_variable_set_allocation (ETableSubsetVariable *etssv,
                    gint total)
{
    ETableSubset *etss = E_TABLE_SUBSET(etssv);
    if (total <= 0)
        total = 1;
    if (total > etss->n_map){
        etss->map_table = g_realloc (etss->map_table, total * sizeof(int));
    }
}