aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/text/e-text-model.c
blob: bbd442b46ca23392924ebe5d077bbfe48a59df60 (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* ETextModel - Text item model for evolution.
 * Copyright (C) 2000 Helix Code, Inc.
 *
 * Author: Chris Lahey <clahey@umich.edu>
 *
 * A majority of code taken from:
 *
 * Text item type for GnomeCanvas widget
 *
 * GnomeCanvas is basically a port of the Tk toolkit's most excellent
 * canvas widget.  Tk is copyrighted by the Regents of the University
 * of California, Sun Microsystems, and other parties.
 *
 * Copyright (C) 1998 The Free Software Foundation
 *
 * Author: Federico Mena <federico@nuclecu.unam.mx> */

#include <config.h>
#include <ctype.h>
#include "e-text-model.h"

enum {
    E_TEXT_MODEL_CHANGED,
    E_TEXT_MODEL_POSITION,
    E_TEXT_MODEL_LAST_SIGNAL
};

static guint e_text_model_signals[E_TEXT_MODEL_LAST_SIGNAL] = { 0 };

static void e_text_model_class_init (ETextModelClass *class);
static void e_text_model_init (ETextModel *model);
static void e_text_model_destroy (GtkObject *object);

static gchar *e_text_model_real_get_text(ETextModel *model);
static void e_text_model_real_set_text(ETextModel *model, gchar *text);
static void e_text_model_real_insert(ETextModel *model, gint position, gchar *text);
static void e_text_model_real_insert_length(ETextModel *model, gint position, gchar *text, gint length);
static void e_text_model_real_delete(ETextModel *model, gint position, gint length);

static gint e_text_model_real_object_count(ETextModel *model);
static const gchar *e_text_model_real_get_nth_object(ETextModel *model, gint n);
static void e_text_model_real_activate_nth_object(ETextModel *mode, gint n);


static GtkObject *parent_class;



/**
 * e_text_model_get_type:
 * @void: 
 * 
 * Registers the &ETextModel class if necessary, and returns the type ID
 * associated to it.
 * 
 * Return value: The type ID of the &ETextModel class.
 **/
GtkType
e_text_model_get_type (void)
{
    static GtkType model_type = 0;

    if (!model_type) {
        GtkTypeInfo model_info = {
            "ETextModel",
            sizeof (ETextModel),
            sizeof (ETextModelClass),
            (GtkClassInitFunc) e_text_model_class_init,
            (GtkObjectInitFunc) e_text_model_init,
            NULL, /* reserved_1 */
            NULL, /* reserved_2 */
            (GtkClassInitFunc) NULL
        };

        model_type = gtk_type_unique (gtk_object_get_type (), &model_info);
    }

    return model_type;
}

/* Class initialization function for the text item */
static void
e_text_model_class_init (ETextModelClass *klass)
{
    GtkObjectClass *object_class;

    object_class = (GtkObjectClass *) klass;

    parent_class = gtk_type_class (gtk_object_get_type ());

    e_text_model_signals[E_TEXT_MODEL_CHANGED] =
        gtk_signal_new ("changed",
                GTK_RUN_LAST,
                object_class->type,
                GTK_SIGNAL_OFFSET (ETextModelClass, changed),
                gtk_marshal_NONE__NONE,
                GTK_TYPE_NONE, 0);

    e_text_model_signals[E_TEXT_MODEL_POSITION] =
        gtk_signal_new ("position",
                GTK_RUN_LAST,
                object_class->type,
                GTK_SIGNAL_OFFSET (ETextModelClass, position),
                gtk_marshal_NONE__INT,
                GTK_TYPE_NONE, 1, GTK_TYPE_INT);
    
    gtk_object_class_add_signals (object_class, e_text_model_signals, E_TEXT_MODEL_LAST_SIGNAL);
    
    klass->changed = NULL;
    klass->get_text = e_text_model_real_get_text;
    klass->set_text = e_text_model_real_set_text;
    klass->insert = e_text_model_real_insert;
    klass->insert_length = e_text_model_real_insert_length;
    klass->delete = e_text_model_real_delete;
    klass->obj_count = e_text_model_real_object_count;
    klass->get_nth_obj = e_text_model_real_get_nth_object;
    klass->activate_nth_obj = e_text_model_real_activate_nth_object;
    
    object_class->destroy = e_text_model_destroy;
}

/* Object initialization function for the text item */
static void
e_text_model_init (ETextModel *model)
{
    model->text = NULL;
}

/* Destroy handler for the text item */
static void
e_text_model_destroy (GtkObject *object)
{
    ETextModel *model;

    g_return_if_fail (object != NULL);
    g_return_if_fail (E_IS_TEXT_MODEL (object));

    model = E_TEXT_MODEL (object);

    if (model->text)
        g_free (model->text);

    if (GTK_OBJECT_CLASS (parent_class)->destroy)
        (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}

static gchar *
e_text_model_real_get_text(ETextModel *model)
{
    if (model->text)
        return model->text;
    else
        return "";
}

static void
e_text_model_real_set_text(ETextModel *model, gchar *text)
{
    if (model->text)
        g_free(model->text);
    model->text = g_strdup(text);
    e_text_model_changed(model);
}

static void
e_text_model_real_insert(ETextModel *model, gint position, gchar *text)
{
    gchar *temp;

    g_return_if_fail (0<= position && position <= strlen (model->text));

    temp = g_strdup_printf("%.*s%s%s", position, model->text, text, model->text + position);

    if (model->text)
        g_free(model->text);
    model->text = temp;
    e_text_model_changed(model);

    e_text_model_suggest_position (model, position + strlen(text));
}

static void
e_text_model_real_insert_length(ETextModel *model, gint position, gchar *text, gint length)
{
    gchar *temp;

    g_return_if_fail (0 <= position && position <= strlen (model->text));

    temp = g_strdup_printf("%.*s%.*s%s", position, model->text, length, text, model->text + position);

    if (model->text)
        g_free(model->text);
    model->text = temp;
    e_text_model_changed(model);

    e_text_model_suggest_position (model, position + length);
}

static void
e_text_model_real_delete(ETextModel *model, gint position, gint length)
{
    g_return_if_fail (0 <= position && position <= strlen (model->text));
    
    memmove(model->text + position, model->text + position + length, strlen(model->text + position + length) + 1);
    e_text_model_changed(model);
}

static gint
e_text_model_real_object_count(ETextModel *model)
{
    gint count = 0;
    gchar *c = model->text;

    if (c) {
        while (*c) {
            if (*c == '\1')
                ++count;
            ++c;
        }
    }
    return count;
}

static const gchar *
e_text_model_real_get_nth_object(ETextModel *model, gint n)
{
    return "";
}

static void
e_text_model_real_activate_nth_object(ETextModel *model, gint n)
{
    /* By default, do nothing */
}

void
e_text_model_changed(ETextModel *model)
{
    g_return_if_fail (model != NULL);
    g_return_if_fail (E_IS_TEXT_MODEL (model));

    gtk_signal_emit (GTK_OBJECT (model),
             e_text_model_signals [E_TEXT_MODEL_CHANGED]);
}

gchar *
e_text_model_get_text(ETextModel *model)
{
    g_return_val_if_fail (model != NULL, NULL);
    g_return_val_if_fail (E_IS_TEXT_MODEL (model), NULL);

    if ( E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->get_text )
        return E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->get_text(model);
    else
        return "";
}

void
e_text_model_set_text(ETextModel *model, gchar *text)
{
    g_return_if_fail (model != NULL);
    g_return_if_fail (E_IS_TEXT_MODEL (model));

    if ( E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->set_text )
        E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->set_text(model, text);
}

void
e_text_model_insert(ETextModel *model, gint position, gchar *text)
{
    g_return_if_fail (model != NULL);
    g_return_if_fail (E_IS_TEXT_MODEL (model));

    if ( E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->insert )
        E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->insert(model, position, text);
}

void
e_text_model_insert_length(ETextModel *model, gint position, gchar *text, gint length)
{
    g_return_if_fail (model != NULL);
    g_return_if_fail (E_IS_TEXT_MODEL (model));

    if ( E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->insert_length )
        E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->insert_length(model, position, text, length);
}

void
e_text_model_delete(ETextModel *model, gint position, gint length)
{
    g_return_if_fail (model != NULL);
    g_return_if_fail (E_IS_TEXT_MODEL (model));

    if ( E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->delete )
        E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->delete(model, position, length);
}

void
e_text_model_suggest_position(ETextModel *model, gint position)
{
    g_return_if_fail (model != NULL);
    g_return_if_fail (E_IS_TEXT_MODEL (model));
    g_return_if_fail (0 <= position);
    g_return_if_fail (position <= strlen (model->text));

    gtk_signal_emit (GTK_OBJECT (model), e_text_model_signals[E_TEXT_MODEL_POSITION], position);
}

gint
e_text_model_object_count(ETextModel *model)
{
    g_return_val_if_fail (model != NULL, 0);
    g_return_val_if_fail (E_IS_TEXT_MODEL (model), 0);

    if ( E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->obj_count)
        return E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->obj_count(model);
    else
        return 0;
}

const gchar *
e_text_model_get_nth_object(ETextModel *model, gint n)
{
    g_return_val_if_fail (model != NULL, NULL);
    g_return_val_if_fail (E_IS_TEXT_MODEL (model), NULL);
    g_return_val_if_fail (n >= 0, NULL);

    if ( E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->get_nth_obj )
        return E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->get_nth_obj(model, n);
    else
        return "";
}

void
e_text_model_activate_nth_object(ETextModel *model, gint n)
{
    g_return_if_fail (model != NULL);
    g_return_if_fail (E_IS_TEXT_MODEL (model));
    g_return_if_fail (n >= 0);

    if ( E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->activate_nth_obj )
        E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->activate_nth_obj(model, n);
}

gchar *
e_text_model_strdup_expanded_text(ETextModel *model)
{
    gint len = 0, i, N;
    gchar *expanded, *dest;
    const gchar *src;

    g_return_val_if_fail (model != NULL, NULL);
    g_return_val_if_fail (E_IS_TEXT_MODEL (model), NULL);

    if (model->text == NULL)
        return NULL;

    N = e_text_model_object_count (model);
    if (N == 0)
        return g_strdup (model->text);

    /* First, compute the length of the expanded string. */

    len = strlen (model->text);
    len -= N; /* Subtract out the \1s that signify the objects. */

    for (i=0; i<N; ++i)
        len += strlen (e_text_model_get_nth_object (model, i));

    /* Next, allocate and build the expanded string. */
    expanded = g_new0 (gchar, len+2);

    src = model->text;
    dest = expanded;
    i = 0;
    while (*src) {
        if (*src == '\1') {
            const gchar *src_obj;
            
            g_assert (i < N);
            src_obj = e_text_model_get_nth_object (model, i);

            if (src_obj) {
                while (*src_obj) {
                    *dest = *src_obj;
                    ++src_obj;
                    ++dest;
                }
            }
            
            ++src;
            ++i;

        } else {

            *dest = *src;
            ++src;
            ++dest;

        }
    }

    return expanded;
}

ETextModel *
e_text_model_new(void)
{
    ETextModel *model = gtk_type_new (e_text_model_get_type ());
    model->text = g_strdup("");
    return model;
}