File: binding.c

package info (click to toggle)
libcdk5 5.0.20161210-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,440 kB
  • ctags: 2,833
  • sloc: ansic: 32,375; sh: 4,732; makefile: 1,122; sed: 43; cpp: 41
file content (230 lines) | stat: -rw-r--r-- 5,201 bytes parent folder | download | duplicates (2)
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
#include <cdk_int.h>

/*
 * $Author: tom $
 * $Date: 2011/05/16 22:36:08 $
 * $Revision: 1.56 $
 *
 * Notes:
 *
 * The cdktype parameter passed to bindCDKObject, etc., is redundant since
 * the object parameter also has the same information.  For compatibility
 * just use it for a sanity check.
 */

#ifndef KEY_MAX
#define KEY_MAX 512
#endif

static CDKOBJS *bindableObject (EObjectType * cdktype, void *object)
{
   CDKOBJS *obj = (CDKOBJS *)object;

   if (obj != 0 && *cdktype == ObjTypeOf (obj))
   {
      if (*cdktype == vFSELECT)
      {
	 *cdktype = vENTRY;
	 object = ((CDKFSELECT *)object)->entryField;
      }
      else if (*cdktype == vALPHALIST)
      {
	 *cdktype = vENTRY;
	 object = ((CDKALPHALIST *)object)->entryField;
      }
   }
   else
   {
      object = 0;
   }
   return (CDKOBJS *)object;
}

/*
 * This inserts a binding.
 */
void bindCDKObject (EObjectType cdktype,
		    void *object,
		    chtype key,
		    BINDFN function,
		    void *data)
{
   CDKOBJS *obj = bindableObject (&cdktype, object);

   if ((key < KEY_MAX) && obj != 0)
   {
      if (key != 0 && (unsigned)key >= obj->bindingCount)
      {
	 unsigned next = (unsigned) (key + 1);

	 if (obj->bindingList != 0)
	    obj->bindingList = typeReallocN (CDKBINDING, obj->bindingList, next);
	 else
	    obj->bindingList = typeMallocN (CDKBINDING, next);

	 memset (&(obj->bindingList[obj->bindingCount]), 0,
		 (next - obj->bindingCount) * sizeof (CDKBINDING));
	 obj->bindingCount = next;
      }

      if (obj->bindingList != 0)
      {
	 obj->bindingList[key].bindFunction = function;
	 obj->bindingList[key].bindData = data;
      }
   }
}

/*
 * This removes a binding on an object.
 */
void unbindCDKObject (EObjectType cdktype, void *object, chtype key)
{
   CDKOBJS *obj = bindableObject (&cdktype, object);

   if (obj != 0 && ((unsigned)key < obj->bindingCount))
   {
      obj->bindingList[key].bindFunction = 0;
      obj->bindingList[key].bindData = 0;
   }
}

/*
 * This removes all the bindings for the given objects.
 */
void cleanCDKObjectBindings (EObjectType cdktype, void *object)
{
   CDKOBJS *obj = bindableObject (&cdktype, object);

   if (obj != 0 && obj->bindingList != 0)
   {
      unsigned x;

      for (x = 0; x < obj->bindingCount; x++)
      {
	 (obj)->bindingList[x].bindFunction = 0;
	 (obj)->bindingList[x].bindData = 0;
      }
      freeAndNull ((obj)->bindingList);
   }
}

/*
 * This checks to see if the binding for the key exists:
 * If it does then it runs the command and returns its value, normally TRUE.
 * If it doesn't it returns a FALSE.  This way we can 'overwrite' coded
 * bindings.
 */
int checkCDKObjectBind (EObjectType cdktype, void *object, chtype key)
{
   CDKOBJS *obj = bindableObject (&cdktype, object);

   if (obj != 0 && ((unsigned)key < obj->bindingCount))
   {
      if ((obj)->bindingList[key].bindFunction != 0)
      {
	 BINDFN function = obj->bindingList[key].bindFunction;
	 void *data = obj->bindingList[key].bindData;

	 return function (cdktype, object, data, key);
      }
   }
   return (FALSE);
}

/*
 * This checks to see if the binding for the key exists.
 */
bool isCDKObjectBind (EObjectType cdktype, void *object, chtype key)
{
   bool result = FALSE;
   CDKOBJS *obj = bindableObject (&cdktype, object);

   if (obj != 0 && ((unsigned)key < obj->bindingCount))
   {
      if ((obj)->bindingList[key].bindFunction != 0)
	 result = TRUE;
   }
   return (result);
}

/*
 * This is a dummy function used to ensure that the constant for mapping has
 * a distinct address.
 */
int getcCDKBind (EObjectType cdktype GCC_UNUSED,
		 void *object GCC_UNUSED,
		 void *clientData GCC_UNUSED,
		 chtype input GCC_UNUSED)
{
   return 0;
}

/*
 * Read from the input window, filtering keycodes as needed.
 */
int getcCDKObject (CDKOBJS *obj)
{
   EObjectType cdktype = ObjTypeOf (obj);
   CDKOBJS *test = bindableObject (&cdktype, obj);
   int result = wgetch (InputWindowOf (obj));

   if (result >= 0
       && test != 0
       && (unsigned)result < test->bindingCount
       && test->bindingList[result].bindFunction == getcCDKBind)
   {
      result = (int)(long)test->bindingList[result].bindData;
   }
   else if (test == 0
	    || (unsigned)result >= test->bindingCount
	    || test->bindingList[result].bindFunction == 0)
   {
      switch (result)
      {
      case '\r':
      case '\n':
	 result = KEY_ENTER;
	 break;
      case '\t':
	 result = KEY_TAB;
	 break;
      case DELETE:
	 result = KEY_DC;
	 break;
      case '\b':		/* same as CTRL('H'), for ASCII */
	 result = KEY_BACKSPACE;
	 break;
      case CDK_BEGOFLINE:
	 result = KEY_HOME;
	 break;
      case CDK_ENDOFLINE:
	 result = KEY_END;
	 break;
      case CDK_FORCHAR:
	 result = KEY_RIGHT;
	 break;
      case CDK_BACKCHAR:
	 result = KEY_LEFT;
	 break;
      case CDK_NEXT:
	 result = KEY_TAB;
	 break;
      case CDK_PREV:
	 result = KEY_BTAB;
	 break;
      }
   }
   return result;
}

/*
 * Use this function rather than getcCDKObject(), since we can extend it to
 * handle wide-characters.
 */
int getchCDKObject (CDKOBJS *obj, boolean *functionKey)
{
   int key = getcCDKObject (obj);
   *functionKey = (key >= KEY_MIN && key <= KEY_MAX);
   return key;
}