use BLI_strncpy and BLI_snprintf when the size of the string is known.
[blender-staging.git] / source / blender / editors / util / numinput.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Jonathan Smith
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/util/numinput.c
29  *  \ingroup edutil
30  */
31
32
33 #include <math.h>                       /* fabs */
34 #include <stdio.h>                      /* for size_t */
35
36 #include "BLI_utildefines.h"
37 #include "BLI_string.h"
38
39 #include "WM_types.h"
40
41 #include "ED_numinput.h"
42
43 /* ************************** Functions *************************** */
44
45 /* ************************** NUMINPUT **************************** */
46
47 void initNumInput(NumInput *n)
48 {
49         n->flag         =
50         n->idx          =
51         n->idx_max      =
52         n->inv[0]   =
53         n->inv[1]   =
54         n->inv[2]   =
55         n->ctrl[0]      = 
56         n->ctrl[1]      = 
57         n->ctrl[2]      = 0;
58
59         n->val[0]               = 
60         n->val[1]       = 
61         n->val[2]       = 0.0f;
62 }
63
64 void outputNumInput(NumInput *n, char *str)
65 {
66         char cur;
67         char inv[] = "1/";
68         short i, j;
69
70         for (j=0; j<=n->idx_max; j++) {
71                 /* if AFFECTALL and no number typed and cursor not on number, use first number */
72                 if (n->flag & NUM_AFFECT_ALL && n->idx != j && n->ctrl[j] == 0)
73                         i = 0;
74                 else
75                         i = j;
76
77                 if (n->idx != i)
78                         cur = ' ';
79                 else
80                         cur = '|';
81
82                 if (n->inv[i])
83                         inv[0] = '1';
84                 else
85                         inv[0] = 0;
86
87                 if( n->val[i] > 1e10f || n->val[i] < -1e10f )
88                         BLI_snprintf(&str[j*20], 20, "%s%.4e%c", inv, n->val[i], cur);
89                 else
90                         switch (n->ctrl[i]) {
91                         case 0:
92                                 BLI_snprintf(&str[j*20], 20, "%sNONE%c", inv, cur);
93                                 break;
94                         case 1:
95                         case -1:
96                                 BLI_snprintf(&str[j*20], 20, "%s%.0f%c", inv, n->val[i], cur);
97                                 break;
98                         case 10:
99                         case -10:
100                                 BLI_snprintf(&str[j*20], 20, "%s%.f.%c", inv, n->val[i], cur);
101                                 break;
102                         case 100:
103                         case -100:
104                                 BLI_snprintf(&str[j*20], 20, "%s%.1f%c", inv, n->val[i], cur);
105                                 break;
106                         case 1000:
107                         case -1000:
108                                 BLI_snprintf(&str[j*20], 20, "%s%.2f%c", inv, n->val[i], cur);
109                                 break;
110                         case 10000:
111                         case -10000:
112                                 BLI_snprintf(&str[j*20], 20, "%s%.3f%c", inv, n->val[i], cur);
113                                 break;
114                         default:
115                                 BLI_snprintf(&str[j*20], 20, "%s%.4e%c", inv, n->val[i], cur);
116                         }
117         }
118 }
119
120 short hasNumInput(NumInput *n)
121 {
122         short i;
123
124         for (i=0; i<=n->idx_max; i++) {
125                 if (n->ctrl[i])
126                         return 1;
127         }
128
129         return 0;
130 }
131
132 void applyNumInput(NumInput *n, float *vec)
133 {
134         short i, j;
135
136         if (hasNumInput(n)) {
137                 for (j=0; j<=n->idx_max; j++) {
138                         /* if AFFECTALL and no number typed and cursor not on number, use first number */
139                         if (n->flag & NUM_AFFECT_ALL && n->idx != j && n->ctrl[j] == 0)
140                                 i = 0;
141                         else
142                                 i = j;
143
144                         if (n->ctrl[i] == 0 && n->flag & NUM_NULL_ONE) {
145                                 vec[j] = 1.0f;
146                         }
147                         else if (n->val[i] == 0.0f && n->flag & NUM_NO_ZERO) {
148                                 vec[j] = 0.0001f;
149                         }
150                         else {
151                                 if (n->inv[i])
152                                 {
153                                         vec[j] = 1.0f / n->val[i];
154                                 }
155                                 else
156                                 {
157                                         vec[j] = n->val[i];
158                                 }
159                         }
160                 }
161         }
162 }
163
164 char handleNumInput(NumInput *n, wmEvent *event)
165 {
166         float Val = 0;
167         short idx = n->idx, idx_max = n->idx_max;
168
169         if (event->type == EVT_MODAL_MAP) {
170                 switch (event->val) {
171                 case NUM_MODAL_INCREMENT_UP:
172                         if (!n->ctrl[idx])
173                                 n->ctrl[idx] = 1;
174
175                         n->val[idx] += n->increment;
176                         break;
177                 case NUM_MODAL_INCREMENT_DOWN:
178                         if (!n->ctrl[idx])
179                                 n->ctrl[idx] = 1;
180
181                         n->val[idx] -= n->increment;
182                         break;
183                 default:
184                         return 0;
185                 }
186         } else {
187                 switch (event->type) {
188                 case BACKSPACEKEY:
189                         if (n->ctrl[idx] == 0) {
190                                 n->val[0]               =
191                                         n->val[1]       =
192                                         n->val[2]       = 0.0f;
193                                 n->ctrl[0]              =
194                                         n->ctrl[1]      =
195                                         n->ctrl[2]      = 0;
196                                 n->inv[0]               =
197                                         n->inv[1]       =
198                                         n->inv[2]       = 0;
199                         }
200                         else {
201                                 n->val[idx] = 0.0f;
202                                 n->ctrl[idx] = 0;
203                                 n->inv[idx] = 0;
204                         }
205                         break;
206                 case PERIODKEY:
207                 case PADPERIOD:
208                         if (n->flag & NUM_NO_FRACTION)
209                                 return 0;
210
211                         switch (n->ctrl[idx])
212                         {
213                         case 0:
214                         case 1:
215                                 n->ctrl[idx] = 10;
216                                 break;
217                         case -1:
218                                 n->ctrl[idx] = -10;
219                         }
220                         break;
221                 case PADMINUS:
222                         if(event->alt)
223                                 break;
224                 case MINUSKEY:
225                         if (n->flag & NUM_NO_NEGATIVE)
226                                 break;
227
228                         if (n->ctrl[idx]) {
229                                 n->ctrl[idx] *= -1;
230                                 n->val[idx] *= -1;
231                         }
232                         else
233                                 n->ctrl[idx] = -1;
234                         break;
235                 case PADSLASHKEY:
236                 case SLASHKEY:
237                         if (n->flag & NUM_NO_FRACTION)
238                                 return 0;
239
240                         n->inv[idx] = !n->inv[idx];
241                         break;
242                 case TABKEY:
243                         if (idx_max == 0)
244                                 return 0;
245
246                         idx++;
247                         if (idx > idx_max)
248                                 idx = 0;
249                         n->idx = idx;
250                         break;
251                 case PAD9:
252                 case NINEKEY:
253                         Val += 1.0f;
254                 case PAD8:
255                 case EIGHTKEY:
256                         Val += 1.0f;
257                 case PAD7:
258                 case SEVENKEY:
259                         Val += 1.0f;
260                 case PAD6:
261                 case SIXKEY:
262                         Val += 1.0f;
263                 case PAD5:
264                 case FIVEKEY:
265                         Val += 1.0f;
266                 case PAD4:
267                 case FOURKEY:
268                         Val += 1.0f;
269                 case PAD3:
270                 case THREEKEY:
271                         Val += 1.0f;
272                 case PAD2:
273                 case TWOKEY:
274                         Val += 1.0f;
275                 case PAD1:
276                 case ONEKEY:
277                         Val += 1.0f;
278                 case PAD0:
279                 case ZEROKEY:
280                         if (!n->ctrl[idx])
281                                 n->ctrl[idx] = 1;
282
283                         if (fabsf(n->val[idx]) > 9999999.0f);
284                         else if (n->ctrl[idx] == 1) {
285                                 n->val[idx] *= 10;
286                                 n->val[idx] += Val;
287                         }
288                         else if (n->ctrl[idx] == -1) {
289                                 n->val[idx] *= 10;
290                                 n->val[idx] -= Val;
291                         }
292                         else {
293                                 /* float resolution breaks when over six digits after comma */
294                                 if( ABS(n->ctrl[idx]) < 10000000) {
295                                         n->val[idx] += Val / (float)n->ctrl[idx];
296                                         n->ctrl[idx] *= 10;
297                                 }
298                         }
299                         break;
300                 default:
301                         return 0;
302                 }
303         }
304         
305         // printf("%f\n", n->val[idx]);
306
307         /* REDRAW SINCE NUMBERS HAVE CHANGED */
308         return 1;
309 }