aboutsummaryrefslogtreecommitdiff
path: root/src/iemguts-objlist.h
blob: fedfdf103356c09ae75447b8441afa525a9fe810 (plain)
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
/******************************************************
 *
 * iemguts - implementation file
 *
 * copyleft (c) IOhannes m zmölnig
 *
 *   2008:forum::für::umläute:2008
 *
 *   institute of electronic music and acoustics (iem)
 *
 ******************************************************
 *
 * license: GNU General Public License v.2
 *
 ******************************************************/

/* this file implements some helperr functions for dealing with lists of
 * objects (containing other objects)
 *
 * used for callbacks to enumerated objects without $0 tricks
 */

/* 
 * TODO: documentation
 */

#include "m_pd.h"


/* ------------------------- helper methods for callbacks ---------------------------- */

typedef struct _iemguts_objlist {
  const t_pd*obj;
  struct _iemguts_objlist*next;
} t_iemguts_objlist;

typedef struct _iemguts_canvaslist {
  const t_pd*parent;
  t_iemguts_objlist*obj;

  struct _iemguts_canvaslist*next;
} t_iemguts_canvaslist;

static t_iemguts_canvaslist*s_canvaslist=0;


static t_iemguts_canvaslist*findCanvas(const t_pd*parent) {
  t_iemguts_canvaslist*list=s_canvaslist;
  if(0==parent  || 0==list)
    return 0;

  for(list=s_canvaslist; list; list=list->next) {
    if(parent == list->parent) {
      return list;
    }
  }
  return 0; 
}

static t_iemguts_canvaslist*addCanvas(const t_pd*parent)
{
  t_iemguts_canvaslist*list=findCanvas(parent);
  if(!list) {
    list=(t_iemguts_canvaslist*)getbytes(sizeof(t_iemguts_canvaslist));
    list->parent=parent;
    list->obj=0;
    list->next=0;

    if(0==s_canvaslist) {
      /* new list */
      s_canvaslist=list;
    } else {
      /* add to the end of existing list */
      t_iemguts_canvaslist*dummy=s_canvaslist;
      while(dummy->next)
        dummy=dummy->next;
      dummy->next = list;
    }
  }
  return list;
}

static t_iemguts_objlist*objectsInCanvas(const t_pd*parent) {
  t_iemguts_canvaslist*list=findCanvas(parent);
  if(list)
    return list->obj;

  return 0;
}

static void addObjectToCanvas(const t_pd*parent, const t_pd*obj) {
  t_iemguts_canvaslist*p=addCanvas(parent);
  t_iemguts_objlist*list=0;
  t_iemguts_objlist*entry=0;
  if(!p || !obj)
    return;
  list=p->obj;

  if(list&&obj==list->obj)
    return;

  while(list && list->next) {
    if(obj==list->obj) /* obj already in list */
      return;
    list=list->next;
  }

  /* we are at the end of the list that does not contain obj yet, so add it */
  entry=(t_iemguts_objlist*)getbytes(sizeof(t_iemguts_objlist));
  entry->obj=obj;
  entry->next=0;
  if(list) {
    list->next=entry;
  } else {
    p->obj=entry;
  }
}

static void removeObjectFromCanvas(const t_pd*parent, const t_pd*obj) {
  t_iemguts_canvaslist*p=findCanvas(parent);
  t_iemguts_objlist*list=0, *last=0, *next=0;
  if(!p || !obj)return;
  list=p->obj;
  if(!list)
    return;

  while(list && obj!=list->obj) {
    last=list;
    list=list->next;
  }

  if(!list) /* couldn't find this object */
    return;

  next=list->next;

  if(last)
    last->next=next;
  else
    p->obj=next;

  freebytes((void*)list, sizeof(t_iemguts_objlist));
  list=0;
}

static void removeObjectFromCanvases(const t_pd*obj) {
   t_iemguts_canvaslist*parents=s_canvaslist;

  while(parents) {
    removeObjectFromCanvas(parents->parent, obj);
    parents=parents->next;
  }
}