aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/table/e-table-selection-model.c
blob: a2a0f99aa950b7da06d2a443fdb1333c02ea68a1 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * e-table-selection-model.c: a Table Selection Model
 *
 * Author:
 *   Christopher James Lahey <clahey@ximian.com>
 *
 * (C) 2000, 2001 Ximian, Inc.
 */
#include <config.h>
#include <gtk/gtksignal.h>
#include "e-table-selection-model.h"
#include "gal/util/e-util.h"
#include <gdk/gdkkeysyms.h>

#define ETSM_CLASS(e) ((ETableSelectionModelClass *)((GtkObject *)e)->klass)

#define PARENT_TYPE e_selection_model_get_type ()

static ESelectionModel *parent_class;

static gint etsm_get_row_count (ESelectionModel *esm);

enum {
    ARG_0,
    ARG_MODEL,
};

static void
model_changed(ETableModel *etm, ETableSelectionModel *etsm)
{
    e_selection_model_clear(E_SELECTION_MODEL(etsm));
}

#if 1
static void
model_row_inserted(ETableModel *etm, int row, ETableSelectionModel *etsm)
{
    e_selection_model_insert_row(E_SELECTION_MODEL(etsm), row);
}

static void
model_row_deleted(ETableModel *etm, int row, ETableSelectionModel *etsm)
{
    e_selection_model_delete_row(E_SELECTION_MODEL(etsm), row);
}

#else

static void
model_row_inserted(ETableModel *etm, int row, ETableSelectionModel *etsm)
{
    model_changed(etm, etsm);
}

static void
model_row_deleted(ETableModel *etm, int row, ETableSelectionModel *etsm)
{
    model_changed(etm, etsm);
}
#endif

inline static void
add_model(ETableSelectionModel *etsm, ETableModel *model)
{
    etsm->model = model;
    if (model) {
        gtk_object_ref(GTK_OBJECT(model));
        etsm->model_changed_id = gtk_signal_connect(GTK_OBJECT(model), "model_changed",
                                GTK_SIGNAL_FUNC(model_changed), etsm);
        etsm->model_row_inserted_id = gtk_signal_connect(GTK_OBJECT(model), "model_row_inserted",
                                 GTK_SIGNAL_FUNC(model_row_inserted), etsm);
        etsm->model_row_deleted_id = gtk_signal_connect(GTK_OBJECT(model), "model_row_deleted",
                                GTK_SIGNAL_FUNC(model_row_deleted), etsm);
    }
}

inline static void
drop_model(ETableSelectionModel *etsm)
{
    if (etsm->model) {
        gtk_signal_disconnect(GTK_OBJECT(etsm->model),
                      etsm->model_changed_id);
        gtk_signal_disconnect(GTK_OBJECT(etsm->model),
                      etsm->model_row_inserted_id);
        gtk_signal_disconnect(GTK_OBJECT(etsm->model),
                      etsm->model_row_deleted_id);
        gtk_object_unref(GTK_OBJECT(etsm->model));
    }
    etsm->model = NULL;
}

static void
etsm_destroy (GtkObject *object)
{
    ETableSelectionModel *etsm;

    etsm = E_TABLE_SELECTION_MODEL (object);

    drop_model(etsm);

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

static void
etsm_get_arg (GtkObject *o, GtkArg *arg, guint arg_id)
{
    ETableSelectionModel *etsm = E_TABLE_SELECTION_MODEL (o);

    switch (arg_id){
    case ARG_MODEL:
        GTK_VALUE_OBJECT (*arg) = GTK_OBJECT(etsm->model);
        break;
    }
}

static void
etsm_set_arg (GtkObject *o, GtkArg *arg, guint arg_id)
{
    ETableSelectionModel *etsm = E_TABLE_SELECTION_MODEL (o);
    
    switch (arg_id){
    case ARG_MODEL:
        drop_model(etsm);
        add_model(etsm, GTK_VALUE_OBJECT (*arg) ? E_TABLE_MODEL(GTK_VALUE_OBJECT (*arg)) : NULL);
        break;
    }
}

static void
e_table_selection_model_init (ETableSelectionModel *selection)
{
    selection->model = NULL;
}

static void
e_table_selection_model_class_init (ETableSelectionModelClass *klass)
{
    GtkObjectClass *object_class;
    ESelectionModelClass *esm_class;

    parent_class             = gtk_type_class (PARENT_TYPE);

    object_class             = GTK_OBJECT_CLASS(klass);
    esm_class                = E_SELECTION_MODEL_CLASS(klass);

    object_class->destroy    = etsm_destroy;
    object_class->get_arg    = etsm_get_arg;
    object_class->set_arg    = etsm_set_arg;

    esm_class->get_row_count = etsm_get_row_count;

    gtk_object_add_arg_type ("ETableSelectionModel::model", GTK_TYPE_OBJECT,
                 GTK_ARG_READWRITE, ARG_MODEL);
}

E_MAKE_TYPE(e_table_selection_model, "ETableSelectionModel", ETableSelectionModel,
        e_table_selection_model_class_init, e_table_selection_model_init, PARENT_TYPE);

/** 
 * e_table_selection_model_new
 *
 * This routine creates a new #ETableSelectionModel.
 *
 * Returns: The new #ETableSelectionModel.
 */
ETableSelectionModel *
e_table_selection_model_new (void)
{
    return gtk_type_new (e_table_selection_model_get_type ());
}

static gint
etsm_get_row_count (ESelectionModel *esm)
{
    ETableSelectionModel *etsm = E_TABLE_SELECTION_MODEL(esm);

    return e_table_model_row_count (etsm->model);
}