summaryrefslogtreecommitdiff
path: root/dist/cdk/binding.c
blob: a1ac200229f58d2e02c9523680268ee0caa03bed (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
#include <cdk.h>

/*
 * $Author: garbled $
 * $Date: 2001/01/04 19:58:29 $
 * $Revision: 1.1.1.1 $
 */

/*
 * Declare file local prototypes.
 */
static int mapChtype (chtype key);

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

  /*
   * When an alarm is set and this function is entered, a very wild
   * value for the key is provided, and the index gets messed up big time.
   * So we will make sure that index is a valid value before using it.
   */
   if ((Index >= 0) && (Index < MAX_BINDINGS))
   {
      if (cdktype == vFSELECT)
      {
	 bindCDKObject (vENTRY, ((CDKFSELECT *)object)->entryField, key, function, data);
      }
      else if (cdktype == vALPHALIST)
      {
	 bindCDKObject (vENTRY, ((CDKALPHALIST *)object)->entryField, key, function, data);
      }
      else
      {
	 if (Index >= obj->bindingCount)
	 {
	    unsigned next = (Index + 1);
	    unsigned need = next * sizeof(CDKBINDING);

	    if (obj->bindingList != 0)
	       obj->bindingList = (CDKBINDING *)realloc(obj->bindingList, need);
	    else
	       obj->bindingList = (CDKBINDING *)malloc(need);

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

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

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

  /*
   * When an alarm is set and this function is entered, a very wild
   * value for the key is provided, and the index gets messed up big time.
   * So we will make sure that index is a valid value before using it.
   */
   if (cdktype == vFSELECT)
   {
      unbindCDKObject (vENTRY, ((CDKFSELECT *)object)->entryField, key);
   }
   else if (cdktype == vALPHALIST)
   {
      unbindCDKObject (vENTRY, ((CDKALPHALIST *)object)->entryField, key);
   }
   else if (Index >= 0 && Index < obj->bindingCount)
   {
      obj->bindingList[Index].bindFunction	= 0;
      obj->bindingList[Index].bindData		= 0;
   }
}

/*
 * This sets all the bindings for the given objects.
 */
void cleanCDKObjectBindings (EObjectType cdktype, void *object)
{
  /*
   * Since dereferencing a void pointer is a no-no, we have to cast
   * our pointer correctly.
   */
   if (cdktype == vFSELECT)
   {
      cleanCDKObjectBindings (vENTRY, ((CDKFSELECT *)object)->entryField);
      cleanCDKObjectBindings (vSCROLL, ((CDKFSELECT *)object)->scrollField);
   }
   else if (cdktype == vALPHALIST)
   {
      cleanCDKObjectBindings (vENTRY, ((CDKALPHALIST *)object)->entryField);
      cleanCDKObjectBindings (vSCROLL, ((CDKALPHALIST *)object)->scrollField);
   }
   else
   {
      int x;
      CDKOBJS *obj = (CDKOBJS *)object;
      for (x=0; x < obj->bindingCount; x++)
      {
	 (obj)->bindingList[x].bindFunction	= 0;
	 (obj)->bindingList[x].bindData		= 0;
      }
   }
}

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

  /*
   * When an alarm is set and this function is entered, a very wild
   * value for the key is provided, and the index gets messed up big time.
   * So we will make sure that index is a valid value before using it.
   */
   if ((Index >= 0) && (Index < obj->bindingCount))
   {
      if ( (obj)->bindingList[Index].bindFunction != 0 )
      {
	 BINDFN function	= obj->bindingList[Index].bindFunction;
	 void * data		= obj->bindingList[Index].bindData;
	 function (cdktype, object, data, key);
	 return (TRUE);
      }
   }
   return (FALSE);
}

/*
 * This translates non ascii characters like KEY_UP to an 'equivalent'
 * ascii value.
 */
static int mapChtype (chtype key)
{
   static const struct {
      int key_out;
      chtype key_in;
   } table[] = {
      { 257, KEY_UP },
      { 258, KEY_DOWN },
      { 259, KEY_LEFT },
      { 260, KEY_RIGHT },
      { 261, KEY_NPAGE },
      { 262, KEY_PPAGE },
      { 263, KEY_HOME },
      { 264, KEY_END },
      { 265, KEY_F0 },
      { 266, KEY_F1 },
      { 267, KEY_F2 },
      { 268, KEY_F3 },
      { 269, KEY_F4 },
      { 270, KEY_F5 },
      { 271, KEY_F6 },
      { 272, KEY_F7 },
      { 273, KEY_A1 },
      { 274, KEY_A3 },
      { 275, KEY_B2 },
      { 276, KEY_C1 },
      { 277, KEY_C3 },
      { 278, KEY_ESC },
   };
   unsigned n;

   for (n = 0; n < sizeof(table)/sizeof(table[0]); n++)
   {
      if (table[n].key_in == key)
      {
	 key = table[n].key_out;
	 break;
      }
   }
   return (key);
}