AeKeyFieldUI.c++
4.29 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#include <iostream.h>
#include "AeTypes.h"
#include "AeKeyFieldUI.h"
#include "AeAsset.h"
#include "AeAssetUI.h"
#include "AeBinView.h"
#define kKeyMin 0 // C-1
#define kKeyMax 127 // G9
#define kKeyDef 60 // C4
AeKeyFieldUI::AeKeyFieldUI (const char * name,
Boolean isEditable,
Boolean isSelectable,
Boolean isHierarchical,
TJustify justify,
TIntGetProc getProc,
TIntSetProc setProc)
: AeIntFieldUI (name, isEditable, isSelectable, isHierarchical, justify,
getProc, setProc, kKeyMin, kKeyMax, kKeyDef)
{
}
AeKeyFieldUI::~AeKeyFieldUI (void)
{
}
void
AeKeyFieldUI::GetValueString (AeAsset * asset, String string)
{
int value = fGetProc (asset);
keynum2keystr (value, string);
}
Boolean
AeKeyFieldUI::SetValueString (AeAsset * asset, String string)
{
assert (fSetProc);
int value;
keystr2keynum (string, value);
if (value < fMin)
value = fMin;
else if (value > fMax)
value = fMax;
if (value == fGetProc(asset))
return False;
fSetProc (asset, value);
return True;
}
Boolean
AeKeyFieldUI::VerifyValueString (String string)
{
int value;
// allow all key values and just clip to max and min if out of range
// keystr2keynum (string, value);
// if (value < fMin || value > fMax)
// return False;
return True;
}
Boolean
AeKeyFieldUI::VerifyInput (XmTextVerifyCallbackStruct * verify, const char * prevStr)
{
String string = verify->text->ptr;
if (string)
{
int pos = (int)verify->startPos;
char ch = *string;
if (pos > 4)
return False;
// first char must be A-G
if (pos == 0)
{
if (ch >= 'a' && ch <= 'g')
*string = toupper (ch);
else if (ch <'A' || ch > 'G')
return False;
}
// second char can either be a sharp, flat, minus, or a digit
else if (pos == 1)
{
if (ch != 'b' && ch != '#' && !isdigit(ch) && ch != '-')
return False;
}
// third char must be a minus if second char was sharp or flat,
// or third char must be a digit if second char is a minus
else if (pos == 2)
{
if (ch != '-' && !isdigit(ch))
return False;
if (ch == '-' && prevStr[1] != 'b' && prevStr[1] != '#')
return False;
if (isdigit(ch) && prevStr[1] != '-' && prevStr[1] != 'b' && prevStr[1] != '#')
return False;
}
// fourth char must be a digit only if the third char is a minus sign
else if (pos == 3)
{
if (!isdigit(ch))
return False;
if (prevStr[2] != '-')
return False;
}
}
return True;
}
void
AeKeyFieldUI::keynum2keystr (int num, String str)
{
int keyNorm = num - fMin;
int keyOctave = keyNorm/12;
int keyNote = keyNorm - keyOctave * 12;
keyOctave -= 1; // C4 = 60 = 5*12, so subtract 1 from 5 to get 4
switch (keyNote)
{
case 0:
sprintf (str, "C%d", keyOctave);
break;
case 1:
sprintf (str, "Db%d", keyOctave);
break;
case 2:
sprintf (str, "D%d", keyOctave);
break;
case 3:
sprintf (str, "Eb%d", keyOctave);
break;
case 4:
sprintf (str, "E%d", keyOctave);
break;
case 5:
sprintf (str, "F%d", keyOctave);
break;
case 6:
sprintf (str, "Gb%d", keyOctave);
break;
case 7:
sprintf (str, "G%d", keyOctave);
break;
case 8:
sprintf (str, "Ab%d", keyOctave);
break;
case 9:
sprintf (str, "A%d", keyOctave);
break;
case 10:
sprintf (str, "Bb%d", keyOctave);
break;
case 11:
sprintf (str, "B%d", keyOctave);
break;
}
}
void
AeKeyFieldUI::keystr2keynum (String str, int & num)
{
char ch;
int keyNote;
int keyOctave;
ch = str[0];
switch (ch)
{
case 'c':
case 'C':
keyNote = 0;
break;
case 'd':
case 'D':
keyNote = 2;
break;
case 'e':
case 'E':
keyNote = 4;
break;
case 'f':
case 'F':
keyNote = 5;
break;
case 'g':
case 'G':
keyNote = 7;
break;
case 'a':
case 'A':
keyNote = 9;
break;
case 'b':
case 'B':
keyNote = 11;
break;
}
ch = str[1];
if (ch == 'b')
{
keyNote--;
ch = str[2];
}
else if (ch == '#')
{
keyNote++;
ch = str[2];
}
// hack since atoi isn't working with -1
if (ch == '-')
keyOctave = 0;
else
keyOctave = atoi (&ch) + 1; // add 1 since C4 = 60 = 5*12
num = keyOctave*12 + keyNote;
}