aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/e-table/e-tree-gnode.c
blob: ce53751b56bfdf1569ab8f881cda217b89bfa04b (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * e-tree-gnode.c: a Tree Model that reflects a GNode structure visually.
 *
 * Author:
 *   Chris Toshok (toshok@helixcode.com)
 *
 * (C) 2000 Helix Code, Inc.
 */
#include <config.h>
#include <gtk/gtksignal.h>
#include "e-util/e-util.h"
#include "e-tree-gnode.h"

#define PARENT_TYPE E_TREE_MODEL_TYPE

static ETreePath *
gnode_get_root (ETreeModel *etm)
{
  ETreeGNode *etg = E_TREE_GNODE (etm);
  ETreePath *path = NULL;

  path = g_list_append(path, etg->root);

  return path;
}

static ETreePath *
gnode_get_prev (ETreeModel *etm, ETreePath *node)
{
  ETreePath *prev_path;

  GNode *gnode;
  GNode *prev_sibling;

  g_return_val_if_fail (node && node->data, NULL);

  gnode = (GNode*)node->data;
  prev_sibling = g_node_prev_sibling(gnode);

  if (!prev_sibling)
      return NULL;

  prev_path = g_list_copy (node->next);
  prev_path = g_list_prepend (prev_path, prev_sibling);
  return prev_path;
}

static ETreePath *
gnode_get_next (ETreeModel *etm, ETreePath *node)
{
  ETreePath *next_path;
  GNode *gnode;
  GNode *next_sibling;

  g_return_val_if_fail (node && node->data, NULL);

  gnode = (GNode*)node->data;
  next_sibling = g_node_next_sibling(gnode);

  if (!next_sibling)
      return NULL;

  next_path = g_list_copy (node->next);
  next_path = g_list_prepend (next_path, next_sibling);
  return next_path;
}

static void *
gnode_value_at (ETreeModel *etm, ETreePath *node, int col)
{
  ETreeGNode *etg = E_TREE_GNODE (etm);
  GNode *gnode;

  g_return_val_if_fail (node && node->data, NULL);

  gnode = (GNode*)node->data;

  return etg->value_at (etm, gnode, col, etg->data);
}

static void
gnode_set_value_at (ETreeModel *etm, ETreePath *node, int col, const void *val)
{
  ETreeGNode *etg = E_TREE_GNODE (etm);
  GNode *gnode;

  g_return_if_fail (node && node->data);

  gnode = (GNode*)node->data;

  /* XXX */
}

static gboolean
gnode_is_editable (ETreeModel *etm, ETreePath *node, int col)
{
  ETreeGNode *etg = E_TREE_GNODE (etm);
  GNode *gnode;

  g_return_val_if_fail (node && node->data, FALSE);

  gnode = (GNode*)node->data;

  /* XXX */
  return FALSE;
}

static guint
gnode_get_children (ETreeModel *etm, ETreePath *node, ETreePath ***paths)
{
  ETreeGNode *etg = E_TREE_GNODE (etm);
  GNode *gnode;
  guint n_children;

  g_return_val_if_fail (node && node->data, 0);

  gnode = (GNode*)node->data;

  n_children = g_node_n_children (gnode);

  if (paths)
    {
      int i;
      (*paths) = g_malloc (sizeof (ETreePath*) * n_children);
      for (i = 0; i < n_children; i ++) {
    (*paths)[i] = g_list_copy (node);
    (*paths)[i] = g_list_prepend ((*paths)[i], g_node_nth_child (gnode, i));
      }
    }

  return n_children;
}

static void
gnode_release_paths (ETreeModel *etm, ETreePath **paths, guint num_paths)
{
    guint i;
    g_return_if_fail (paths);

    for (i = 0; i < num_paths; i ++)
        g_list_free (paths[i]);
    g_free (paths);
}

static gboolean
gnode_is_expanded (ETreeModel *etm, ETreePath *node)
{
  ETreeGNode *etg = E_TREE_GNODE (etm);
  GNode *gnode;

  g_return_val_if_fail (node && node->data, FALSE);

  gnode = (GNode*)node->data;

  return (gboolean)gnode->data;
}

static void
gnode_set_expanded (ETreeModel *etm, ETreePath *node, gboolean expanded)
{
  ETreeGNode *etg = E_TREE_GNODE (etm);
  GNode *gnode;
  int num_descendents;

  g_return_if_fail (node && node->data);

  gnode = (GNode*)node->data;

  /* XXX */
  gnode->data = (gpointer)expanded;

  e_table_model_changed (E_TABLE_MODEL(etm));
}

static void
e_tree_gnode_class_init (GtkObjectClass *object_class)
{
    ETreeModelClass *model_class = (ETreeModelClass *) object_class;

    model_class->get_root = gnode_get_root;
    model_class->get_next = gnode_get_next;
    model_class->get_prev = gnode_get_prev;
    model_class->value_at = gnode_value_at;
    model_class->set_value_at = gnode_set_value_at;
    model_class->is_editable = gnode_is_editable;
    model_class->get_children = gnode_get_children;
    model_class->release_paths = gnode_release_paths;
    model_class->is_expanded = gnode_is_expanded;
    model_class->set_expanded = gnode_set_expanded;
}

E_MAKE_TYPE(e_tree_gnode, "ETreeGNode", ETreeGNode, e_tree_gnode_class_init, NULL, PARENT_TYPE)

ETreeModel *
e_tree_gnode_new (GNode *root_node,
          ETreeGNodeValueAtFn value_at,
          void *data)
{
    ETreeGNode *etg;

    etg = gtk_type_new (e_tree_gnode_get_type ());

    etg->root = root_node;

    etg->value_at = value_at;
    etg->data = data;

    return (ETreeModel*)etg;
}