aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/misc/e-reflow-model.c
blob: 74e7b1726cffd945a05c21398f0015c082306c63 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * e-reflow-model.c: a Reflow Model
 *
 * Authors:
 *   Chris Lahey <clahey@ximian.com>
 *
 * (C) 2001 Ximian, Inc.
 */
#include <config.h>
#include "e-reflow-model.h"
#include <gtk/gtksignal.h>

#define ERM_CLASS(e) ((EReflowModelClass *)((GtkObject *)e)->klass)

#define PARENT_TYPE gtk_object_get_type ()

#define d(x)

d(static gint depth = 0);


static GtkObjectClass *e_reflow_model_parent_class;

enum {
    MODEL_CHANGED,
    MODEL_ITEMS_INSERTED,
    MODEL_ITEM_CHANGED,
    LAST_SIGNAL
};

static guint e_reflow_model_signals [LAST_SIGNAL] = { 0, };

/**
 * e_reflow_model_set_width:
 * @e_reflow_model: The e-reflow-model to operate on
 * @width: The new value for the width of each item.
 */
void
e_reflow_model_set_width (EReflowModel *e_reflow_model, int width)
{
    g_return_if_fail (e_reflow_model != NULL);
    g_return_if_fail (E_IS_REFLOW_MODEL (e_reflow_model));

    ERM_CLASS (e_reflow_model)->set_width (e_reflow_model, width);
}

/**
 * e_reflow_model_count:
 * @e_reflow_model: The e-reflow-model to operate on
 *
 * Returns: the number of items in the reflow model.
 */
int
e_reflow_model_count (EReflowModel *e_reflow_model)
{
    g_return_val_if_fail (e_reflow_model != NULL, 0);
    g_return_val_if_fail (E_IS_REFLOW_MODEL (e_reflow_model), 0);

    return ERM_CLASS (e_reflow_model)->count (e_reflow_model);
}

/**
 * e_reflow_model_height:
 * @e_reflow_model: The e-reflow-model to operate on
 * @n: The item number to get the height of.
 * @parent: The parent GnomeCanvasItem.
 *
 * Returns: the height of the nth item.
 */
int
e_reflow_model_height (EReflowModel *e_reflow_model, int n, GnomeCanvasGroup *parent)
{
    g_return_val_if_fail (e_reflow_model != NULL, 0);
    g_return_val_if_fail (E_IS_REFLOW_MODEL (e_reflow_model), 0);

    return ERM_CLASS (e_reflow_model)->height (e_reflow_model, n, parent);
}

/**
 * e_reflow_model_incarnate:
 * @e_reflow_model: The e-reflow-model to operate on
 * @n: The item to create.
 * @parent: The parent GnomeCanvasItem to create a child of.
 *
 * Create a GnomeCanvasItem to represent the nth piece of data.
 *
 * Returns: the new GnomeCanvasItem.
 */
GnomeCanvasItem *
e_reflow_model_incarnate (EReflowModel *e_reflow_model, int n, GnomeCanvasGroup *parent)
{
    g_return_val_if_fail (e_reflow_model != NULL, NULL);
    g_return_val_if_fail (E_IS_REFLOW_MODEL (e_reflow_model), NULL);

    return ERM_CLASS (e_reflow_model)->incarnate (e_reflow_model, n, parent);
}

/**
 * e_reflow_model_compare:
 * @e_reflow_model: The e-reflow-model to operate on
 * @n1: The first item to compare
 * @n2: The second item to compare
 *
 * Compares item n1 and item n2 to see which should come first.
 *
 * Returns: strcmp like semantics for the comparison value.
 */
int
e_reflow_model_compare (EReflowModel *e_reflow_model, int n1, int n2)
{
#if 0
    g_return_val_if_fail (e_reflow_model != NULL, 0);
    g_return_val_if_fail (E_IS_REFLOW_MODEL (e_reflow_model), 0);
#endif

    return ERM_CLASS (e_reflow_model)->compare (e_reflow_model, n1, n2);
}

/**
 * e_reflow_model_reincarnate:
 * @e_reflow_model: The e-reflow-model to operate on
 * @n: The item to create.
 * @item: The item to reuse.
 *
 * Update item to represent the nth piece of data.
 */
void
e_reflow_model_reincarnate (EReflowModel *e_reflow_model, int n, GnomeCanvasItem *item)
{
    g_return_if_fail (e_reflow_model != NULL);
    g_return_if_fail (E_IS_REFLOW_MODEL (e_reflow_model));

    ERM_CLASS (e_reflow_model)->reincarnate (e_reflow_model, n, item);
}

static void
e_reflow_model_class_init (GtkObjectClass *object_class)
{
    EReflowModelClass *klass = E_REFLOW_MODEL_CLASS(object_class);
    e_reflow_model_parent_class = gtk_type_class (PARENT_TYPE);

    e_reflow_model_signals [MODEL_CHANGED] =
        gtk_signal_new ("model_changed",
                GTK_RUN_LAST,
                object_class->type,
                GTK_SIGNAL_OFFSET (EReflowModelClass, model_changed),
                gtk_marshal_NONE__NONE,
                GTK_TYPE_NONE, 0);

    e_reflow_model_signals [MODEL_ITEMS_INSERTED] =
        gtk_signal_new ("model_items_inserted",
                GTK_RUN_LAST,
                object_class->type,
                GTK_SIGNAL_OFFSET (EReflowModelClass, model_items_inserted),
                gtk_marshal_NONE__INT_INT,
                GTK_TYPE_NONE, 2, GTK_TYPE_INT, GTK_TYPE_INT);

    e_reflow_model_signals [MODEL_ITEM_CHANGED] =
        gtk_signal_new ("model_item_changed",
                GTK_RUN_LAST,
                object_class->type,
                GTK_SIGNAL_OFFSET (EReflowModelClass, model_item_changed),
                gtk_marshal_NONE__INT,
                GTK_TYPE_NONE, 1, GTK_TYPE_INT);

    gtk_object_class_add_signals (object_class, e_reflow_model_signals, LAST_SIGNAL);

    klass->set_width            = NULL;
    klass->count                = NULL;
    klass->height               = NULL;
    klass->incarnate            = NULL;
    klass->reincarnate          = NULL;

    klass->model_changed        = NULL;
    klass->model_items_inserted = NULL;
    klass->model_item_changed   = NULL;
}


guint
e_reflow_model_get_type (void)
{
    static guint type = 0;
    
    if (!type)
    {
        GtkTypeInfo info =
        {
            "EReflowModel",
            sizeof (EReflowModel),
            sizeof (EReflowModelClass),
            (GtkClassInitFunc) e_reflow_model_class_init,
            NULL,
            /* reserved_1 */ NULL,
            /* reserved_2 */ NULL,
            (GtkClassInitFunc) NULL,
        };
        
        type = gtk_type_unique (PARENT_TYPE, &info);
    }

  return type;
}

#if d(!)0
static void
print_tabs (void)
{
    int i;
    for (i = 0; i < depth; i++)
        g_print("\t");
}
#endif

/**
 * e_reflow_model_changed:
 * @e_reflow_model: the reflow model to notify of the change
 *
 * Use this function to notify any views of this reflow model that
 * the contents of the reflow model have changed.  This will emit
 * the signal "model_changed" on the @e_reflow_model object.
 *
 * It is preferable to use the e_reflow_model_item_changed() signal to
 * notify of smaller changes than to invalidate the entire model, as
 * the views might have ways of caching the information they render
 * from the model.
 */
void
e_reflow_model_changed (EReflowModel *e_reflow_model)
{
    g_return_if_fail (e_reflow_model != NULL);
    g_return_if_fail (E_IS_REFLOW_MODEL (e_reflow_model));
    
    d(print_tabs());
    d(g_print("Emitting model_changed on model 0x%p.\n", e_reflow_model));
    d(depth++);
    gtk_signal_emit (GTK_OBJECT (e_reflow_model),
             e_reflow_model_signals [MODEL_CHANGED]);
    d(depth--);
}

/**
 * e_reflow_model_items_inserted:
 * @e_reflow_model: The model changed.
 * @position: The position the items were insert in.
 * @count: The number of items inserted.
 * 
 * Use this function to notify any views of the reflow model that a number of items have been inserted.
 **/
void
e_reflow_model_items_inserted (EReflowModel *e_reflow_model, int position, int count)
{
    g_return_if_fail (e_reflow_model != NULL);
    g_return_if_fail (E_IS_REFLOW_MODEL (e_reflow_model));

    d(print_tabs());
    d(g_print("Emitting items_inserted on model 0x%p, position=%d, count=%d.\n", e_reflow_model, position, count));
    d(depth++);
    gtk_signal_emit (GTK_OBJECT (e_reflow_model),
             e_reflow_model_signals [MODEL_ITEMS_INSERTED], position, count);
    d(depth--);
}

/**
 * e_reflow_model_item_changed:
 * @e_reflow_model: the reflow model to notify of the change
 * @item: the item that was changed in the model.
 *
 * Use this function to notify any views of the reflow model that the
 * contents of item @item have changed in model such that the height
 * has changed or the item needs to be reincarnated.  This function
 * will emit the "model_item_changed" signal on the @e_reflow_model
 * object
 */
void
e_reflow_model_item_changed (EReflowModel *e_reflow_model, int n)
{
    g_return_if_fail (e_reflow_model != NULL);
    g_return_if_fail (E_IS_REFLOW_MODEL (e_reflow_model));

    d(print_tabs());
    d(g_print("Emitting item_changed on model 0x%p, n=%d.\n", e_reflow_model, n));
    d(depth++);
    gtk_signal_emit (GTK_OBJECT (e_reflow_model),
             e_reflow_model_signals [MODEL_ITEM_CHANGED], n);
    d(depth--);
}