summaryrefslogtreecommitdiff
path: root/iup/srclua5/il_tree_aux.c
blob: 439d8974cf51b837a0bc6012736eced99c231c01 (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
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
/** \file
 * \brief iuptree binding for Lua 5.
 *
 * See Copyright Notice in "iup.h"
 */

#include <lua.h>
#include <lauxlib.h>

#include "iup.h"
#include "iupcontrols.h"

#include "iuplua.h"
#include "il.h"
#include "il_controls.h"

/* 
   The REGISTRY is used to store references to the associated Lua objects.
   Given an ID, to retreive a Lua object is quite simple.

   But given a user_id to obtain the ID is more complicated.
   The iup.TREEREFTABLE is used to do this mapping.
   We use the object as the index to this table.
*/

/* iup.TREEREFTABLE[object at pos] = ref */
static void tree_settableref(lua_State *L, int pos, int ref)
{
  lua_getglobal(L, "iup");
  lua_pushstring(L, "TREEREFTABLE");
  lua_gettable(L, -2);
  lua_remove(L, -2); /* remove "iup" from stack */
  lua_pushvalue(L, pos);
  if(ref == LUA_NOREF)
    lua_pushnil(L);
  else
    lua_pushinteger(L, ref);
  lua_settable(L, -3);
  lua_pop(L, 1);
}

/* ref = iup.TREEREFTABLE[object at pos] */
static int tree_gettableref(lua_State *L, int pos)
{
  lua_getglobal(L, "iup");
  lua_pushstring(L, "TREEREFTABLE");
  lua_gettable(L, -2);
  lua_remove(L, -2); /* remove "iup" from stack */
  lua_pushvalue(L, pos);
  lua_gettable(L, -2);
  if (lua_isnil(L, -1))
  {
    lua_pop(L, 1);
    return LUA_NOREF;
  }
  else
  {
    int ref = lua_tointeger(L, -1);
    lua_pop(L, 1);
    return ref;
  }
}

static void tree_push_userid(lua_State *L, void* userid)
{
  int ref = (int)userid;
  if (ref == 0) /* userid is actually NULL */
    lua_pushnil(L);
  else 
  {
    if (ref > 0) ref--; /* only positive references are shifted */
    lua_getref(L, ref);
  }
}

/*****************************************************************************
 * Userdata/Table <-> id functions 
 ****************************************************************************/

static int TreeGetId(lua_State *L)
{
  Ihandle *ih = iuplua_checkihandle(L,1);
  int ref = tree_gettableref(L, 2);
  if (ref == LUA_NOREF)
    lua_pushnil(L);
  else
  {
    int id;
    if (ref >= 0) ref++;  /* only positive references are shifted */
    id = IupTreeGetId(ih, (void*)ref);
    if (id == -1)
      lua_pushnil(L);
    else
      lua_pushinteger(L, id);
  }
  return 1;        
}

static int TreeGetUserId(lua_State *L)
{  
  Ihandle *ih = iuplua_checkihandle(L,1);
  int id = luaL_checkinteger(L,2);
  tree_push_userid(L, IupTreeGetUserId(ih, id));
  return 1;
}

static int TreeSetUserId(lua_State *L)
{  
  Ihandle *ih = iuplua_checkihandle(L,1);
  int id = luaL_checkinteger(L,2);
  int ref = (int)IupTreeGetUserId(ih, id);
  if (ref != 0) /* userid is not NULL */
  {
    if (ref > 0) ref--; /* only positive references are shifted */

    /* release the previous object referenced there */
    lua_getref(L, ref);
    tree_settableref(L, 4, LUA_NOREF);
    lua_unref(L, ref);
    lua_pop(L, 1);
  }

  if (lua_isnil(L, 3))
    IupTreeSetUserId(ih, id, NULL);
  else
  {
    /* add a new reference */
    lua_pushvalue(L, 3);
    ref = lua_ref(L, 1);
    tree_settableref(L, 3, ref);

    if (ref >= 0) ref++;  /* only positive references are shifted */
    IupTreeSetUserId(ih, id, (void*)ref);
  }

  return 0;
}

static int tree_multiselection_cb(Ihandle *ih, int* ids, int p1)
{
  int i;
  lua_State *L = iuplua_call_start(ih, "multiselection_cb");
  lua_newtable(L);
  for (i = 0; i < p1; i++)
  {
    lua_pushinteger(L,i+1);
    lua_pushinteger(L,ids[i]);
    lua_settable(L,-3);
  }
  lua_pushinteger(L, p1);
  return iuplua_call(L, 2);
}

static int tree_multiunselection_cb(Ihandle *ih, int* ids, int p1)
{
  int i;
  lua_State *L = iuplua_call_start(ih, "multiunselection_cb");
  lua_newtable(L);
  for (i = 0; i < p1; i++)
  {
    lua_pushinteger(L,i+1);
    lua_pushinteger(L,ids[i]);
    lua_settable(L,-3);
  }
  lua_pushinteger(L, p1);
  return iuplua_call(L, 2);
}

static int tree_noderemoved_cb(Ihandle *ih, void* p1)
{
  lua_State *L = iuplua_call_start(ih, "noderemoved_cb");
  tree_push_userid(L, p1);
  return iuplua_call(L, 1);
}

void iuplua_treefuncs_open (lua_State *L)
{
  iuplua_register_cb(L, "MULTISELECTION_CB", (lua_CFunction)tree_multiselection_cb, NULL);
  iuplua_register_cb(L, "MULTIUNSELECTION_CB", (lua_CFunction)tree_multiunselection_cb, NULL);
  iuplua_register_cb(L, "NODEREMOVED_CB", (lua_CFunction)tree_noderemoved_cb, NULL);

/* In Lua 5:
  TreeSetTableId = TreeSetUserId
  TreeGetTable   = TreeGetUserId
  TreeGetTableId = TreeGetId
*/

  /* Userdata <-> id */
  iuplua_register(L, TreeGetId, "TreeGetId");
  iuplua_register(L, TreeGetUserId, "TreeGetUserId");
  iuplua_register(L, TreeSetUserId, "TreeSetUserId");

  /* Table <-> id */
  iuplua_register(L, TreeGetId, "TreeGetTableId");
  iuplua_register(L, TreeGetUserId, "TreeGetTable");
  iuplua_register(L, TreeSetUserId, "TreeSetTableId");
}