code cleanup: WM naming conventions
[blender.git] / source / blender / editors / interface / interface_handlers.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) 2008 Blender Foundation.
19  * All rights reserved.
20  * 
21  * Contributor(s): Blender Foundation
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/interface/interface_handlers.c
27  *  \ingroup edinterface
28  */
29
30
31 #include <float.h>
32 #include <limits.h>
33 #include <math.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <assert.h>
38
39 #include "MEM_guardedalloc.h"
40
41 #include "DNA_sensor_types.h"
42 #include "DNA_controller_types.h"
43 #include "DNA_actuator_types.h"
44
45 #include "DNA_object_types.h"
46 #include "DNA_scene_types.h"
47
48 #include "BLI_math.h"
49 #include "BLI_blenlib.h"
50 #include "BLI_utildefines.h"
51 #include "BLI_string_cursor_utf8.h"
52
53 #include "BLF_translation.h"
54
55 #include "PIL_time.h"
56
57 #include "BKE_colortools.h"
58 #include "BKE_context.h"
59 #include "BKE_idprop.h"
60 #include "BKE_report.h"
61 #include "BKE_texture.h"
62 #include "BKE_tracking.h"
63 #include "BKE_unit.h"
64
65 #include "ED_screen.h"
66 #include "ED_util.h"
67 #include "ED_keyframing.h"
68
69 #include "UI_interface.h"
70
71 #include "BLF_api.h"
72
73 #include "interface_intern.h"
74
75 #include "RNA_access.h"
76
77 #include "WM_api.h"
78 #include "WM_types.h"
79
80 /* proto */
81 static void ui_add_smart_controller(bContext *C, uiBut *from, uiBut *to);
82 static void ui_add_link(bContext *C, uiBut *from, uiBut *to);
83
84 /***************** structs and defines ****************/
85
86 #define BUTTON_TOOLTIP_DELAY        0.500
87 #define BUTTON_FLASH_DELAY          0.020
88 #define MENU_SCROLL_INTERVAL        0.1
89 #define BUTTON_AUTO_OPEN_THRESH     0.3
90 #define BUTTON_MOUSE_TOWARDS_THRESH 1.0
91
92 typedef enum uiButtonActivateType {
93         BUTTON_ACTIVATE_OVER,
94         BUTTON_ACTIVATE,
95         BUTTON_ACTIVATE_APPLY,
96         BUTTON_ACTIVATE_TEXT_EDITING,
97         BUTTON_ACTIVATE_OPEN
98 } uiButtonActivateType;
99
100 typedef enum uiHandleButtonState {
101         BUTTON_STATE_INIT,
102         BUTTON_STATE_HIGHLIGHT,
103         BUTTON_STATE_WAIT_FLASH,
104         BUTTON_STATE_WAIT_RELEASE,
105         BUTTON_STATE_WAIT_KEY_EVENT,
106         BUTTON_STATE_NUM_EDITING,
107         BUTTON_STATE_TEXT_EDITING,
108         BUTTON_STATE_TEXT_SELECTING,
109         BUTTON_STATE_MENU_OPEN,
110         BUTTON_STATE_WAIT_DRAG,
111         BUTTON_STATE_EXIT
112 } uiHandleButtonState;
113
114 typedef struct uiHandleButtonData {
115         wmWindowManager *wm;
116         wmWindow *window;
117         ARegion *region;
118
119         int interactive;
120
121         /* overall state */
122         uiHandleButtonState state;
123         int cancel, escapecancel, retval;
124         int applied, appliedinteractive;
125         wmTimer *flashtimer;
126
127         /* edited value */
128         char *str, *origstr;
129         double value, origvalue, startvalue;
130         float vec[3], origvec[3];
131         int togdual, togonly;
132         ColorBand *coba;
133
134         /* tooltip */
135         ARegion *tooltip;
136         wmTimer *tooltiptimer;
137         
138         /* auto open */
139         int used_mouse;
140         wmTimer *autoopentimer;
141
142         /* text selection/editing */
143         int maxlen, selextend, selstartx;
144
145         /* number editing / dragging */
146         int draglastx, draglasty;
147         int dragstartx, dragstarty;
148         int dragchange, draglock, dragsel;
149         float dragf, dragfstart;
150         CBData *dragcbd;
151
152         /* menu open (watch uiFreeActiveButtons) */
153         uiPopupBlockHandle *menu;
154         int menuretval;
155         
156         /* search box (watch uiFreeActiveButtons) */
157         ARegion *searchbox;
158
159         /* post activate */
160         uiButtonActivateType posttype;
161         uiBut *postbut;
162 } uiHandleButtonData;
163
164 typedef struct uiAfterFunc {
165         struct uiAfterFunc *next, *prev;
166
167         uiButHandleFunc func;
168         void *func_arg1;
169         void *func_arg2;
170         void *func_arg3;
171         
172         uiButHandleNFunc funcN;
173         void *func_argN;
174
175         uiButHandleRenameFunc rename_func;
176         void *rename_arg1;
177         void *rename_orig;
178         
179         uiBlockHandleFunc handle_func;
180         void *handle_func_arg;
181         int retval;
182
183         uiMenuHandleFunc butm_func;
184         void *butm_func_arg;
185         int a2;
186
187         wmOperatorType *optype;
188         int opcontext;
189         PointerRNA *opptr;
190
191         PointerRNA rnapoin;
192         PropertyRNA *rnaprop;
193
194         bContextStore *context;
195
196         char undostr[512];
197
198         int autokey;
199 } uiAfterFunc;
200
201 static int ui_but_contains_pt(uiBut *but, int mx, int my);
202 static int ui_mouse_inside_button(ARegion *ar, uiBut *but, int x, int y);
203 static void button_activate_state(bContext *C, uiBut *but, uiHandleButtonState state);
204 static int ui_handler_region_menu(bContext *C, wmEvent *event, void *userdata);
205 static void ui_handle_button_activate(bContext *C, ARegion *ar, uiBut *but, uiButtonActivateType type);
206 static void button_timers_tooltip_remove(bContext *C, uiBut *but);
207
208 /* ******************** menu navigation helpers ************** */
209
210 static int ui_but_editable(uiBut *but)
211 {
212         return ELEM5(but->type, LABEL, SEPR, ROUNDBOX, LISTBOX, PROGRESSBAR);
213 }
214
215 static uiBut *ui_but_prev(uiBut *but)
216 {
217         while (but->prev) {
218                 but = but->prev;
219                 if (!ui_but_editable(but)) return but;
220         }
221         return NULL;
222 }
223
224 static uiBut *ui_but_next(uiBut *but)
225 {
226         while (but->next) {
227                 but = but->next;
228                 if (!ui_but_editable(but)) return but;
229         }
230         return NULL;
231 }
232
233 static uiBut *ui_but_first(uiBlock *block)
234 {
235         uiBut *but;
236         
237         but = block->buttons.first;
238         while (but) {
239                 if (!ui_but_editable(but)) return but;
240                 but = but->next;
241         }
242         return NULL;
243 }
244
245 static uiBut *ui_but_last(uiBlock *block)
246 {
247         uiBut *but;
248         
249         but = block->buttons.last;
250         while (but) {
251                 if (!ui_but_editable(but)) return but;
252                 but = but->prev;
253         }
254         return NULL;
255 }
256
257 static int ui_is_a_warp_but(uiBut *but)
258 {
259         if (U.uiflag & USER_CONTINUOUS_MOUSE) {
260                 if (ELEM6(but->type, NUM, NUMABS, HSVCIRCLE, TRACKPREVIEW, HSVCUBE, BUT_CURVE)) {
261                         return TRUE;
262                 }
263         }
264
265         return FALSE;
266 }
267
268 static float ui_mouse_scale_warp_factor(const short shift)
269 {
270         if (U.uiflag & USER_CONTINUOUS_MOUSE) {
271                 return shift ? 0.05f : 1.0f;
272         }
273         else {
274                 return 1.0f;
275         }
276 }
277
278 static void ui_mouse_scale_warp(uiHandleButtonData *data,
279                                 const float mx, const float my,
280                                 float *r_mx, float *r_my,
281                                 const short shift)
282 {
283         if (U.uiflag & USER_CONTINUOUS_MOUSE) {
284                 const float fac = ui_mouse_scale_warp_factor(shift);
285                 /* slow down the mouse, this is fairly picky */
286                 *r_mx = (data->dragstartx * (1.0f - fac) + mx * fac);
287                 *r_my = (data->dragstarty * (1.0f - fac) + my * fac);
288         }
289         else {
290                 *r_mx = mx;
291                 *r_my = my;
292         }
293 }
294
295 /* file selectors are exempt from utf-8 checks */
296 int ui_is_but_utf8(uiBut *but)
297 {
298         if (but->rnaprop) {
299                 const int subtype = RNA_property_subtype(but->rnaprop);
300                 return !(ELEM4(subtype, PROP_FILEPATH, PROP_DIRPATH, PROP_FILENAME, PROP_BYTESTRING));
301         }
302         else {
303                 return !(but->flag & UI_BUT_NO_UTF8);
304         }
305 }
306
307 /* ********************** button apply/revert ************************/
308
309 static ListBase UIAfterFuncs = {NULL, NULL};
310
311 static void ui_apply_but_func(bContext *C, uiBut *but)
312 {
313         uiAfterFunc *after;
314         uiBlock *block = but->block;
315
316         /* these functions are postponed and only executed after all other
317          * handling is done, i.e. menus are closed, in order to avoid conflicts
318          * with these functions removing the buttons we are working with */
319
320         if (but->func || but->funcN || block->handle_func || but->rename_func ||
321             (but->type == BUTM && block->butm_func) || but->optype || but->rnaprop)
322         {
323                 after = MEM_callocN(sizeof(uiAfterFunc), "uiAfterFunc");
324
325                 if (but->func && ELEM(but, but->func_arg1, but->func_arg2)) {
326                         /* exception, this will crash due to removed button otherwise */
327                         but->func(C, but->func_arg1, but->func_arg2);
328                 }
329                 else
330                         after->func = but->func;
331
332                 after->func_arg1 = but->func_arg1;
333                 after->func_arg2 = but->func_arg2;
334                 after->func_arg3 = but->func_arg3;
335
336                 after->funcN = but->funcN;
337                 after->func_argN = MEM_dupallocN(but->func_argN);
338
339                 after->rename_func = but->rename_func;
340                 after->rename_arg1 = but->rename_arg1;
341                 after->rename_orig = but->rename_orig; /* needs free! */
342
343                 after->handle_func = block->handle_func;
344                 after->handle_func_arg = block->handle_func_arg;
345                 after->retval = but->retval;
346
347                 if (but->type == BUTM) {
348                         after->butm_func = block->butm_func;
349                         after->butm_func_arg = block->butm_func_arg;
350                         after->a2 = but->a2;
351                 }
352
353                 after->optype = but->optype;
354                 after->opcontext = but->opcontext;
355                 after->opptr = but->opptr;
356
357                 after->rnapoin = but->rnapoin;
358                 after->rnaprop = but->rnaprop;
359
360                 if (but->context)
361                         after->context = CTX_store_copy(but->context);
362
363                 but->optype = NULL;
364                 but->opcontext = 0;
365                 but->opptr = NULL;
366
367                 BLI_addtail(&UIAfterFuncs, after);
368         }
369 }
370
371 static void ui_apply_autokey_undo(bContext *C, uiBut *but)
372 {
373         Scene *scene = CTX_data_scene(C);
374         uiAfterFunc *after;
375
376         if (but->flag & UI_BUT_UNDO) {
377                 const char *str = NULL;
378
379                 /* define which string to use for undo */
380                 if (ELEM(but->type, LINK, INLINK)) str = "Add button link";
381                 else if (ELEM(but->type, MENU, ICONTEXTROW)) str = but->drawstr;
382                 else if (but->drawstr[0]) str = but->drawstr;
383                 else str = but->tip;
384
385                 /* fallback, else we don't get an undo! */
386                 if (str == NULL || str[0] == '\0') {
387                         str = "Unknown Action";
388                 }
389
390                 /* delayed, after all other funcs run, popups are closed, etc */
391                 after = MEM_callocN(sizeof(uiAfterFunc), "uiAfterFunc");
392                 BLI_strncpy(after->undostr, str, sizeof(after->undostr));
393                 BLI_addtail(&UIAfterFuncs, after);
394         }
395
396         /* try autokey */
397         ui_but_anim_autokey(C, but, scene, scene->r.cfra);
398 }
399
400 static void ui_apply_but_funcs_after(bContext *C)
401 {
402         uiAfterFunc *afterf, after;
403         PointerRNA opptr;
404         ListBase funcs;
405
406         /* copy to avoid recursive calls */
407         funcs = UIAfterFuncs;
408         UIAfterFuncs.first = UIAfterFuncs.last = NULL;
409
410         for (afterf = funcs.first; afterf; afterf = after.next) {
411                 after = *afterf; /* copy to avoid memleak on exit() */
412                 BLI_freelinkN(&funcs, afterf);
413
414                 if (after.context)
415                         CTX_store_set(C, after.context);
416
417                 if (after.opptr) {
418                         /* free in advance to avoid leak on exit */
419                         opptr = *after.opptr,
420                         MEM_freeN(after.opptr);
421                 }
422
423                 if (after.optype)
424                         WM_operator_name_call(C, after.optype->idname, after.opcontext, (after.opptr) ? &opptr : NULL);
425
426                 if (after.opptr)
427                         WM_operator_properties_free(&opptr);
428
429                 if (after.rnapoin.data)
430                         RNA_property_update(C, &after.rnapoin, after.rnaprop);
431
432                 if (after.context) {
433                         CTX_store_set(C, NULL);
434                         CTX_store_free(after.context);
435                 }
436
437                 if (after.func)
438                         after.func(C, after.func_arg1, after.func_arg2);
439                 if (after.funcN)
440                         after.funcN(C, after.func_argN, after.func_arg2);
441                 if (after.func_argN)
442                         MEM_freeN(after.func_argN);
443                 
444                 if (after.handle_func)
445                         after.handle_func(C, after.handle_func_arg, after.retval);
446                 if (after.butm_func)
447                         after.butm_func(C, after.butm_func_arg, after.a2);
448                 
449                 if (after.rename_func)
450                         after.rename_func(C, after.rename_arg1, after.rename_orig);
451                 if (after.rename_orig)
452                         MEM_freeN(after.rename_orig);
453                 
454                 if (after.undostr[0])
455                         ED_undo_push(C, after.undostr);
456         }
457 }
458
459 static void ui_apply_but_BUT(bContext *C, uiBut *but, uiHandleButtonData *data)
460 {
461         ui_apply_but_func(C, but);
462
463         data->retval = but->retval;
464         data->applied = 1;
465 }
466
467 static void ui_apply_but_BUTM(bContext *C, uiBut *but, uiHandleButtonData *data)
468 {
469         ui_set_but_val(but, but->hardmin);
470         ui_apply_but_func(C, but);
471
472         data->retval = but->retval;
473         data->applied = 1;
474 }
475
476 static void ui_apply_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data)
477 {
478         if (ELEM3(but->type, MENU, ICONROW, ICONTEXTROW))
479                 ui_set_but_val(but, data->value);
480
481         ui_check_but(but);
482         ui_apply_but_func(C, but);
483         data->retval = but->retval;
484         data->applied = 1;
485 }
486
487 static void ui_apply_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data)
488 {
489         double value;
490         int w, lvalue, push;
491         
492         /* local hack... */
493         if (but->type == BUT_TOGDUAL && data->togdual) {
494                 if (but->pointype == SHO)
495                         but->poin += 2;
496                 else if (but->pointype == INT)
497                         but->poin += 4;
498         }
499         
500         value = ui_get_but_val(but);
501         lvalue = (int)value;
502         
503         if (but->bit) {
504                 w = BTST(lvalue, but->bitnr);
505                 if (w) lvalue = BCLR(lvalue, but->bitnr);
506                 else lvalue = BSET(lvalue, but->bitnr);
507                 
508                 if (but->type == TOGR) {
509                         if (!data->togonly) {
510                                 lvalue = 1 << (but->bitnr);
511         
512                                 ui_set_but_val(but, (double)lvalue);
513                         }
514                         else {
515                                 if (lvalue == 0) lvalue = 1 << (but->bitnr);
516                         }
517                 }
518                 
519                 ui_set_but_val(but, (double)lvalue);
520                 if (but->type == ICONTOG || but->type == ICONTOGN) ui_check_but(but);
521         }
522         else {
523                 
524                 if (value == 0.0) push = 1;
525                 else push = 0;
526                 
527                 if (ELEM3(but->type, TOGN, ICONTOGN, OPTIONN)) push = !push;
528                 ui_set_but_val(but, (double)push);
529                 if (but->type == ICONTOG || but->type == ICONTOGN) ui_check_but(but);
530         }
531         
532         /* end local hack... */
533         if (but->type == BUT_TOGDUAL && data->togdual) {
534                 if (but->pointype == SHO)
535                         but->poin -= 2;
536                 else if (but->pointype == INT)
537                         but->poin -= 4;
538         }
539         
540         ui_apply_but_func(C, but);
541
542         data->retval = but->retval;
543         data->applied = 1;
544 }
545
546 static void ui_apply_but_ROW(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data)
547 {
548         uiBut *bt;
549         
550         ui_set_but_val(but, but->hardmax);
551         
552         /* states of other row buttons */
553         for (bt = block->buttons.first; bt; bt = bt->next)
554                 if (bt != but && bt->poin == but->poin && ELEM(bt->type, ROW, LISTROW))
555                         ui_check_but(bt);
556         
557         ui_apply_but_func(C, but);
558
559         data->retval = but->retval;
560         data->applied = 1;
561 }
562
563 static void ui_apply_but_TEX(bContext *C, uiBut *but, uiHandleButtonData *data)
564 {
565         if (!data->str)
566                 return;
567
568         ui_set_but_string(C, but, data->str);
569         ui_check_but(but);
570
571         /* give butfunc the original text too */
572         /* feature used for bone renaming, channels, etc */
573         /* afterfunc frees origstr */
574         but->rename_orig = data->origstr;
575         data->origstr = NULL;
576         ui_apply_but_func(C, but);
577
578         data->retval = but->retval;
579         data->applied = 1;
580 }
581
582 static void ui_apply_but_NUM(bContext *C, uiBut *but, uiHandleButtonData *data)
583 {
584         if (data->str) {
585                 if (ui_set_but_string(C, but, data->str)) {
586                         data->value = ui_get_but_val(but);
587                 }
588                 else {
589                         data->cancel = 1;
590                         return;
591                 }
592         }
593         else
594                 ui_set_but_val(but, data->value);
595
596         ui_check_but(but);
597         ui_apply_but_func(C, but);
598
599         data->retval = but->retval;
600         data->applied = 1;
601 }
602
603 static void ui_apply_but_TOG3(bContext *C, uiBut *but, uiHandleButtonData *data)
604
605         if (but->pointype == SHO) {
606                 short *sp = (short *)but->poin;
607                 
608                 if (BTST(sp[1], but->bitnr)) {
609                         sp[1] = BCLR(sp[1], but->bitnr);
610                         sp[0] = BCLR(sp[0], but->bitnr);
611                 }
612                 else if (BTST(sp[0], but->bitnr)) {
613                         sp[1] = BSET(sp[1], but->bitnr);
614                 }
615                 else {
616                         sp[0] = BSET(sp[0], but->bitnr);
617                 }
618         }
619         else {
620                 if (BTST(*(but->poin + 2), but->bitnr)) {
621                         *(but->poin + 2) = BCLR(*(but->poin + 2), but->bitnr);
622                         *(but->poin) = BCLR(*(but->poin), but->bitnr);
623                 }
624                 else if (BTST(*(but->poin), but->bitnr)) {
625                         *(but->poin + 2) = BSET(*(but->poin + 2), but->bitnr);
626                 }
627                 else {
628                         *(but->poin) = BSET(*(but->poin), but->bitnr);
629                 }
630         }
631         
632         ui_check_but(but);
633         ui_apply_but_func(C, but);
634         data->retval = but->retval;
635         data->applied = 1;
636 }
637
638 static void ui_apply_but_VEC(bContext *C, uiBut *but, uiHandleButtonData *data)
639 {
640         ui_set_but_vectorf(but, data->vec);
641         ui_check_but(but);
642         ui_apply_but_func(C, but);
643
644         data->retval = but->retval;
645         data->applied = 1;
646 }
647
648 static void ui_apply_but_COLORBAND(bContext *C, uiBut *but, uiHandleButtonData *data)
649 {
650         ui_apply_but_func(C, but);
651         data->retval = but->retval;
652         data->applied = 1;
653 }
654
655 static void ui_apply_but_CURVE(bContext *C, uiBut *but, uiHandleButtonData *data)
656 {
657         ui_apply_but_func(C, but);
658         data->retval = but->retval;
659         data->applied = 1;
660 }
661
662 static void ui_apply_but_IDPOIN(bContext *C, uiBut *but, uiHandleButtonData *data)
663 {
664         ui_set_but_string(C, but, data->str);
665         ui_check_but(but);
666         ui_apply_but_func(C, but);
667         data->retval = but->retval;
668         data->applied = 1;
669 }
670
671 #ifdef WITH_INTERNATIONAL
672 static void ui_apply_but_CHARTAB(bContext *C, uiBut *but, uiHandleButtonData *data)
673 {
674         ui_apply_but_func(C, but);
675         data->retval = but->retval;
676         data->applied = 1;
677 }
678 #endif
679
680 /* ****************** drag drop code *********************** */
681
682 static int ui_but_mouse_inside_icon(uiBut *but, ARegion *ar, wmEvent *event)
683 {
684         rcti rect;
685         int x = event->x, y = event->y;
686         
687         ui_window_to_block(ar, but->block, &x, &y);
688         
689         rect.xmin = but->x1; rect.xmax = but->x2;
690         rect.ymin = but->y1; rect.ymax = but->y2;
691         
692         if (but->imb) ;  /* use button size itself */
693         else if (but->flag & UI_ICON_LEFT) {
694                 rect.xmax = rect.xmin + (rect.ymax - rect.ymin);
695         }
696         else {
697                 int delta = (rect.xmax - rect.xmin) - (rect.ymax - rect.ymin);
698                 rect.xmin += delta / 2;
699                 rect.xmax -= delta / 2;
700         }
701         
702         return BLI_in_rcti(&rect, x, y);
703 }
704
705 static int ui_but_start_drag(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
706 {
707         /* prevent other WM gestures to start while we try to drag */
708         WM_gestures_remove(C);
709
710         if (ABS(data->dragstartx - event->x) + ABS(data->dragstarty - event->y) > U.dragthreshold) {
711                 wmDrag *drag;
712                 
713                 button_activate_state(C, but, BUTTON_STATE_EXIT);
714                 data->cancel = 1;
715                 
716                 drag = WM_event_start_drag(C, but->icon, but->dragtype, but->dragpoin, ui_get_but_val(but));
717                 if (but->imb)
718                         WM_event_drag_image(drag, but->imb, but->imb_scale, but->x2 - but->x1, but->y2 - but->y1);
719                 return 1;
720         }
721         
722         return 0;
723 }
724
725 /* ********************** linklines *********************** */
726
727 static void ui_delete_active_linkline(uiBlock *block)
728 {
729         uiBut *but;
730         uiLink *link;
731         uiLinkLine *line, *nline;
732         int a, b;
733         
734         but = block->buttons.first;
735         while (but) {
736                 if (but->type == LINK && but->link) {
737                         line = but->link->lines.first;
738                         while (line) {
739                                 
740                                 nline = line->next;
741                                 
742                                 if (line->flag & UI_SELECT) {
743                                         BLI_remlink(&but->link->lines, line);
744                                         
745                                         link = line->from->link;
746                                         
747                                         /* are there more pointers allowed? */
748                                         if (link->ppoin) {
749                                                 
750                                                 if (*(link->totlink) == 1) {
751                                                         *(link->totlink) = 0;
752                                                         MEM_freeN(*(link->ppoin));
753                                                         *(link->ppoin) = NULL;
754                                                 }
755                                                 else {
756                                                         b = 0;
757                                                         for (a = 0; a < (*(link->totlink)); a++) {
758                                                                 
759                                                                 if ( (*(link->ppoin))[a] != line->to->poin) {
760                                                                         (*(link->ppoin))[b] = (*(link->ppoin))[a];
761                                                                         b++;
762                                                                 }
763                                                         }       
764                                                         (*(link->totlink))--;
765                                                 }
766                                         }
767                                         else {
768                                                 *(link->poin) = NULL;
769                                         }
770                                         
771                                         MEM_freeN(line);
772                                 }
773                                 line = nline;
774                         }
775                 }
776                 but = but->next;
777         }
778 }
779
780
781 static uiLinkLine *ui_is_a_link(uiBut *from, uiBut *to)
782 {
783         uiLinkLine *line;
784         uiLink *link;
785         
786         link = from->link;
787         if (link) {
788                 line = link->lines.first;
789                 while (line) {
790                         if (line->from == from && line->to == to) return line;
791                         line = line->next;
792                 }
793         }
794         return NULL;
795 }
796
797 /* XXX BAD BAD HACK, fixme later **************** */
798 /* Try to add an AND Controller between the sensor and the actuator logic bricks and to connect them all */
799 static void ui_add_smart_controller(bContext *C, uiBut *from, uiBut *to)
800 {
801         Object *ob = NULL;
802         bSensor *sens_iter;
803         bActuator *act_to, *act_iter;
804         bController *cont;
805         bController ***sens_from_links;
806         uiBut *tmp_but;
807
808         uiLink *link = from->link;
809
810         if (link->ppoin)
811                 sens_from_links = (bController ***)(link->ppoin);
812         else return;
813
814         act_to = (bActuator *)(to->poin);
815
816         /* (1) get the object */
817         CTX_DATA_BEGIN (C, Object *, ob_iter, selected_editable_objects)
818         {
819                 for (sens_iter = ob_iter->sensors.first; sens_iter; sens_iter = sens_iter->next) {
820                         if (&(sens_iter->links) == sens_from_links) {
821                                 ob = ob_iter;
822                                 break;
823                         }
824                 }
825                 if (ob) break;
826         } CTX_DATA_END;
827
828         if (!ob) return;
829
830         /* (2) check if the sensor and the actuator are from the same object */
831         for (act_iter = ob->actuators.first; act_iter; act_iter = (bActuator *)act_iter->next) {
832                 if (act_iter == act_to)
833                         break;
834         }
835
836         /* only works if the sensor and the actuator are from the same object */
837         if (!act_iter) return;
838
839         /* (3) add a new controller */
840         if (WM_operator_name_call(C, "LOGIC_OT_controller_add", WM_OP_EXEC_DEFAULT, NULL) & OPERATOR_FINISHED) {
841                 cont = (bController *)ob->controllers.last;
842
843                 /* (4) link the sensor->controller->actuator */
844                 tmp_but = MEM_callocN(sizeof(uiBut), "uiBut");
845                 uiSetButLink(tmp_but, (void **)&cont, (void ***)&(cont->links), &(cont->totlinks), from->link->tocode, (int)to->hardmin);
846                 tmp_but->hardmin = from->link->tocode;
847                 tmp_but->poin = (char *)cont;
848
849                 tmp_but->type = INLINK;
850                 ui_add_link(C, from, tmp_but);
851
852                 tmp_but->type = LINK;
853                 ui_add_link(C, tmp_but, to);
854
855                 /* (5) garbage collection */
856                 MEM_freeN(tmp_but->link);
857                 MEM_freeN(tmp_but);
858         }
859 }
860
861 static void ui_add_link(bContext *C, uiBut *from, uiBut *to)
862 {
863         /* in 'from' we have to add a link to 'to' */
864         uiLink *link;
865         uiLinkLine *line;
866         void **oldppoin;
867         int a;
868         
869         if ( (line = ui_is_a_link(from, to)) ) {
870                 line->flag |= UI_SELECT;
871                 ui_delete_active_linkline(from->block);
872                 return;
873         }
874
875         if (from->type == INLINK && to->type == INLINK) {
876                 return;
877         }
878         else if (from->type == LINK && to->type == INLINK) {
879                 if (from->link->tocode != (int)to->hardmin) {
880                         ui_add_smart_controller(C, from, to);
881                         return;
882                 }
883         }
884         else if (from->type == INLINK && to->type == LINK) {
885                 if (to->link->tocode == (int)from->hardmin) {
886                         return;
887                 }
888         }
889         
890         link = from->link;
891         
892         /* are there more pointers allowed? */
893         if (link->ppoin) {
894                 oldppoin = *(link->ppoin);
895                 
896                 (*(link->totlink))++;
897                 *(link->ppoin) = MEM_callocN(*(link->totlink) * sizeof(void *), "new link");
898                 
899                 for (a = 0; a < (*(link->totlink)) - 1; a++) {
900                         (*(link->ppoin))[a] = oldppoin[a];
901                 }
902                 (*(link->ppoin))[a] = to->poin;
903                 
904                 if (oldppoin) MEM_freeN(oldppoin);
905         }
906         else {
907                 *(link->poin) = to->poin;
908         }
909         
910 }
911
912
913 static void ui_apply_but_LINK(bContext *C, uiBut *but, uiHandleButtonData *data)
914 {
915         ARegion *ar = CTX_wm_region(C);
916         uiBut *bt;
917         
918         for (bt = but->block->buttons.first; bt; bt = bt->next) {
919                 if (ui_mouse_inside_button(ar, bt, but->linkto[0] + ar->winrct.xmin, but->linkto[1] + ar->winrct.ymin) )
920                         break;
921         }
922         if (bt && bt != but) {
923                 if (!ELEM(bt->type, LINK, INLINK) || !ELEM(but->type, LINK, INLINK))
924                         return;
925                 
926                 if (but->type == LINK) ui_add_link(C, but, bt);
927                 else ui_add_link(C, bt, but);
928
929                 ui_apply_but_func(C, but);
930                 data->retval = but->retval;
931         }
932         data->applied = 1;
933 }
934
935 static void ui_apply_but_IMAGE(bContext *C, uiBut *but, uiHandleButtonData *data)
936 {
937         ui_apply_but_func(C, but);
938         data->retval = but->retval;
939         data->applied = 1;
940 }
941
942 static void ui_apply_but_HISTOGRAM(bContext *C, uiBut *but, uiHandleButtonData *data)
943 {
944         ui_apply_but_func(C, but);
945         data->retval = but->retval;
946         data->applied = 1;
947 }
948
949 static void ui_apply_but_WAVEFORM(bContext *C, uiBut *but, uiHandleButtonData *data)
950 {
951         ui_apply_but_func(C, but);
952         data->retval = but->retval;
953         data->applied = 1;
954 }
955
956 static void ui_apply_but_TRACKPREVIEW(bContext *C, uiBut *but, uiHandleButtonData *data)
957 {
958         ui_apply_but_func(C, but);
959         data->retval = but->retval;
960         data->applied = 1;
961 }
962
963
964 static void ui_apply_button(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, int interactive)
965 {
966         char *editstr;
967         double *editval;
968         float *editvec;
969         ColorBand *editcoba;
970         CurveMapping *editcumap;
971
972         data->retval = 0;
973
974         /* if we cancel and have not applied yet, there is nothing to do,
975          * otherwise we have to restore the original value again */
976         if (data->cancel) {
977                 if (!data->applied)
978                         return;
979
980                 if (data->str) MEM_freeN(data->str);
981                 data->str = data->origstr;
982                 data->origstr = NULL;
983                 data->value = data->origvalue;
984                 data->origvalue = 0.0;
985                 copy_v3_v3(data->vec, data->origvec);
986                 data->origvec[0] = data->origvec[1] = data->origvec[2] = 0.0f;
987         }
988         else {
989                 /* we avoid applying interactive edits a second time
990                  * at the end with the appliedinteractive flag */
991                 if (interactive)
992                         data->appliedinteractive = 1;
993                 else if (data->appliedinteractive)
994                         return;
995         }
996
997         /* ensures we are writing actual values */
998         editstr = but->editstr;
999         editval = but->editval;
1000         editvec = but->editvec;
1001         editcoba = but->editcoba;
1002         editcumap = but->editcumap;
1003         but->editstr = NULL;
1004         but->editval = NULL;
1005         but->editvec = NULL;
1006         but->editcoba = NULL;
1007         but->editcumap = NULL;
1008
1009         /* handle different types */
1010         switch (but->type) {
1011                 case BUT:
1012                         ui_apply_but_BUT(C, but, data);
1013                         break;
1014                 case TEX:
1015                 case SEARCH_MENU:
1016                         ui_apply_but_TEX(C, but, data);
1017                         break;
1018                 case TOGBUT: 
1019                 case TOG: 
1020                 case TOGR: 
1021                 case ICONTOG:
1022                 case ICONTOGN:
1023                 case TOGN:
1024                 case BUT_TOGDUAL:
1025                 case OPTION:
1026                 case OPTIONN:
1027                         ui_apply_but_TOG(C, but, data);
1028                         break;
1029                 case ROW:
1030                 case LISTROW:
1031                         ui_apply_but_ROW(C, block, but, data);
1032                         break;
1033                 case SCROLL:
1034                 case NUM:
1035                 case NUMABS:
1036                 case SLI:
1037                 case NUMSLI:
1038                         ui_apply_but_NUM(C, but, data);
1039                         break;
1040                 case HSVSLI:
1041                         break;
1042                 case TOG3:      
1043                         ui_apply_but_TOG3(C, but, data);
1044                         break;
1045                 case MENU:
1046                 case ICONROW:
1047                 case ICONTEXTROW:
1048                 case BLOCK:
1049                 case PULLDOWN:
1050                         ui_apply_but_BLOCK(C, but, data);
1051                         break;
1052                 case COL:
1053                         if (data->cancel)
1054                                 ui_apply_but_VEC(C, but, data);
1055                         else
1056                                 ui_apply_but_BLOCK(C, but, data);
1057                         break;
1058                 case BUTM:
1059                         ui_apply_but_BUTM(C, but, data);
1060                         break;
1061                 case BUT_NORMAL:
1062                 case HSVCUBE:
1063                 case HSVCIRCLE:
1064                         ui_apply_but_VEC(C, but, data);
1065                         break;
1066                 case BUT_COLORBAND:
1067                         ui_apply_but_COLORBAND(C, but, data);
1068                         break;
1069                 case BUT_CURVE:
1070                         ui_apply_but_CURVE(C, but, data);
1071                         break;
1072                 case IDPOIN:
1073                         ui_apply_but_IDPOIN(C, but, data);
1074                         break;
1075 #ifdef WITH_INTERNATIONAL
1076                 case CHARTAB:
1077                         ui_apply_but_CHARTAB(C, but, data);
1078                         break;
1079 #endif
1080                 case KEYEVT:
1081                 case HOTKEYEVT:
1082                         ui_apply_but_BUT(C, but, data);
1083                         break;
1084                 case LINK:
1085                 case INLINK:
1086                         ui_apply_but_LINK(C, but, data);
1087                         break;
1088                 case BUT_IMAGE: 
1089                         ui_apply_but_IMAGE(C, but, data);
1090                         break;
1091                 case HISTOGRAM: 
1092                         ui_apply_but_HISTOGRAM(C, but, data);
1093                         break;
1094                 case WAVEFORM:
1095                         ui_apply_but_WAVEFORM(C, but, data);
1096                         break;
1097                 case TRACKPREVIEW:
1098                         ui_apply_but_TRACKPREVIEW(C, but, data);
1099                         break;
1100                 default:
1101                         break;
1102         }
1103
1104         but->editstr = editstr;
1105         but->editval = editval;
1106         but->editvec = editvec;
1107         but->editcoba = editcoba;
1108         but->editcumap = editcumap;
1109 }
1110
1111 /* ******************* drop event ********************  */
1112
1113 /* only call if event type is EVT_DROP */
1114 static void ui_but_drop(bContext *C, wmEvent *event, uiBut *but, uiHandleButtonData *data)
1115 {
1116         wmDrag *wmd;
1117         ListBase *drags = event->customdata; /* drop event type has listbase customdata by default */
1118         
1119         for (wmd = drags->first; wmd; wmd = wmd->next) {
1120                 if (wmd->type == WM_DRAG_ID) {
1121                         /* align these types with UI_but_active_drop_name */
1122                         if (ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1123                                 ID *id = (ID *)wmd->poin;
1124                                 
1125                                 if (but->poin == NULL && but->rnapoin.data == NULL) {}
1126                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1127                                 BLI_strncpy(data->str, id->name + 2, data->maxlen);
1128                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1129                         }
1130                 }
1131         }
1132         
1133 }
1134
1135 /* ******************* copy and paste ********************  */
1136
1137 /* c = copy, v = paste */
1138 static void ui_but_copy_paste(bContext *C, uiBut *but, uiHandleButtonData *data, char mode)
1139 {
1140         static ColorBand but_copypaste_coba = {0};
1141         char buf[UI_MAX_DRAW_STR + 1] = {0};
1142
1143         if (mode == 'v' && but->lock)
1144                 return;
1145
1146         if (mode == 'v') {
1147                 /* extract first line from clipboard in case of multi-line copies */
1148                 char *p, *pbuf = WM_clipboard_text_get(0);
1149                 p = pbuf;
1150                 if (p) {
1151                         int i = 0;
1152                         while (*p && *p != '\r' && *p != '\n' && i < UI_MAX_DRAW_STR) {
1153                                 buf[i++] = *p;
1154                                 p++;
1155                         }
1156                         buf[i] = 0;
1157                         MEM_freeN(pbuf);
1158                 }
1159         }
1160         
1161         /* numeric value */
1162         if (ELEM4(but->type, NUM, NUMABS, NUMSLI, HSVSLI)) {
1163                 
1164                 if (but->poin == NULL && but->rnapoin.data == NULL) ;
1165                 else if (mode == 'c') {
1166                         ui_get_but_string(but, buf, sizeof(buf));
1167                         WM_clipboard_text_set(buf, 0);
1168                 }
1169                 else {
1170                         double val;
1171
1172                         if (ui_set_but_string_eval_num(C, but, buf, &val)) {
1173                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1174                                 data->value = val;
1175                                 ui_set_but_string(C, but, buf);
1176                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1177                         }
1178                 }
1179         }
1180
1181         /* RGB triple */
1182         else if (but->type == COL) {
1183                 float rgb[3];
1184                 
1185                 if (but->poin == NULL && but->rnapoin.data == NULL) ;
1186                 else if (mode == 'c') {
1187
1188                         ui_get_but_vectorf(but, rgb);
1189                         BLI_snprintf(buf, sizeof(buf), "[%f, %f, %f]", rgb[0], rgb[1], rgb[2]);
1190                         WM_clipboard_text_set(buf, 0);
1191                         
1192                 }
1193                 else {
1194                         if (sscanf(buf, "[%f, %f, %f]", &rgb[0], &rgb[1], &rgb[2]) == 3) {
1195                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1196                                 ui_set_but_vectorf(but, rgb);
1197                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1198                         }
1199                 }
1200         }
1201
1202         /* text/string and ID data */
1203         else if (ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1204                 uiHandleButtonData *active_data = but->active;
1205
1206                 if (but->poin == NULL && but->rnapoin.data == NULL) ;
1207                 else if (mode == 'c') {
1208                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1209                         BLI_strncpy(buf, active_data->str, UI_MAX_DRAW_STR);
1210                         WM_clipboard_text_set(active_data->str, 0);
1211                         active_data->cancel = 1;
1212                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1213                 }
1214                 else {
1215                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1216
1217                         if (ui_is_but_utf8(but)) BLI_strncpy_utf8(active_data->str, buf, active_data->maxlen);
1218                         else BLI_strncpy(active_data->str, buf, active_data->maxlen);
1219
1220                         if (but->type == SEARCH_MENU) {
1221                                 /* else uiSearchboxData.active member is not updated [#26856] */
1222                                 ui_searchbox_update(C, data->searchbox, but, 1);
1223                         }
1224                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1225                 }
1226         }
1227         /* colorband (not supported by system clipboard) */
1228         else if (but->type == BUT_COLORBAND) {
1229                 if (mode == 'c') {
1230                         if (but->poin == NULL)
1231                                 return;
1232
1233                         memcpy(&but_copypaste_coba, but->poin, sizeof(ColorBand));
1234                 }
1235                 else {
1236                         if (but_copypaste_coba.tot == 0)
1237                                 return;
1238
1239                         if (!but->poin)
1240                                 but->poin = MEM_callocN(sizeof(ColorBand), "colorband");
1241
1242                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1243                         memcpy(data->coba, &but_copypaste_coba, sizeof(ColorBand));
1244                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1245                 }
1246         }
1247         /* operator button (any type) */
1248         else if (but->optype) {
1249                 if (mode == 'c') {
1250                         PointerRNA *opptr;
1251                         char *str;
1252                         opptr = uiButGetOperatorPtrRNA(but); /* allocated when needed, the button owns it */
1253
1254                         str = WM_operator_pystring(C, but->optype, opptr, 0);
1255
1256                         WM_clipboard_text_set(str, 0);
1257
1258                         MEM_freeN(str);
1259                 }
1260         }
1261 }
1262
1263 /* ************* in-button text selection/editing ************* */
1264
1265
1266 static int ui_textedit_delete_selection(uiBut *but, uiHandleButtonData *data)
1267 {
1268         char *str = data->str;
1269         int len = strlen(str);
1270         int change = 0;
1271         if (but->selsta != but->selend && len) {
1272                 memmove(str + but->selsta, str + but->selend, (len - but->selend) + 1);
1273                 change = 1;
1274         }
1275         
1276         but->pos = but->selend = but->selsta;
1277         return change;
1278 }
1279
1280 /* note, but->block->aspect is used here, when drawing button style is getting scaled too */
1281 static void ui_textedit_set_cursor_pos(uiBut *but, uiHandleButtonData *data, short x)
1282 {
1283         uiStyle *style = UI_GetStyle();  // XXX pass on as arg
1284         uiFontStyle *fstyle = &style->widget;
1285         int startx = but->x1;
1286         char *origstr;
1287
1288         uiStyleFontSet(fstyle);
1289
1290         if (fstyle->kerning == 1) /* for BLF_width */
1291                 BLF_enable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
1292         
1293         origstr = MEM_callocN(sizeof(char) * data->maxlen, "ui_textedit origstr");
1294         
1295         BLI_strncpy(origstr, but->drawstr, data->maxlen);
1296         
1297         /* XXX solve generic */
1298         if (but->type == NUM || but->type == NUMSLI)
1299                 startx += (int)(0.5f * (but->y2 - but->y1));
1300         else if (ELEM(but->type, TEX, SEARCH_MENU)) {
1301                 startx += 5;
1302                 if (but->flag & UI_HAS_ICON)
1303                         startx += UI_DPI_ICON_SIZE;
1304         }
1305         
1306         /* mouse dragged outside the widget to the left */
1307         if (x < startx && but->ofs > 0) {       
1308                 int i = but->ofs;
1309
1310                 origstr[but->ofs] = 0;
1311                 
1312                 while (i > 0) {
1313                         if (BLI_str_cursor_step_prev_utf8(origstr, but->ofs, &i)) {
1314                                 if (BLF_width(fstyle->uifont_id, origstr + i) > (startx - x) * 0.25f) break;  // 0.25 == scale factor for less sensitivity
1315                         }
1316                         else {
1317                                 break; /* unlikely but possible */
1318                         }
1319                 }
1320                 but->ofs = i;
1321                 but->pos = but->ofs;
1322         }
1323         /* mouse inside the widget */
1324         else if (x >= startx) {
1325                 int pos_i;
1326
1327                 /* keep track of previous distance from the cursor to the char */
1328                 float cdist, cdist_prev = 0.0f;
1329                 short pos_prev;
1330
1331                 const float aspect_sqrt = sqrtf(but->block->aspect);
1332                 
1333                 but->pos = pos_prev = strlen(origstr) - but->ofs;
1334
1335                 while (TRUE) {
1336                         /* XXX does not take zoom level into account */
1337                         cdist = startx + aspect_sqrt *BLF_width(fstyle->uifont_id, origstr + but->ofs);
1338
1339                         /* check if position is found */
1340                         if (cdist < x) {
1341                                 /* check is previous location was in fact closer */
1342                                 if (((float)x - cdist) > (cdist_prev - (float)x)) {
1343                                         but->pos = pos_prev;
1344                                 }
1345                                 break;
1346                         }
1347                         cdist_prev = cdist;
1348                         pos_prev   = but->pos;
1349                         /* done with tricky distance checks */
1350
1351                         pos_i = but->pos;
1352                         if (but->pos <= 0) break;
1353                         if (BLI_str_cursor_step_prev_utf8(origstr, but->ofs, &pos_i)) {
1354                                 but->pos = pos_i;
1355                                 origstr[but->pos + but->ofs] = 0;
1356                         }
1357                         else {
1358                                 break; /* unlikely but possible */
1359                         }
1360                 }
1361                 but->pos += but->ofs;
1362                 if (but->pos < 0) but->pos = 0;
1363         }
1364         
1365         if (fstyle->kerning == 1)
1366                 BLF_disable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
1367         
1368         MEM_freeN(origstr);
1369 }
1370
1371 static void ui_textedit_set_cursor_select(uiBut *but, uiHandleButtonData *data, short x)
1372 {
1373         if (x > data->selstartx) data->selextend = EXTEND_RIGHT;
1374         else if (x < data->selstartx) data->selextend = EXTEND_LEFT;
1375
1376         ui_textedit_set_cursor_pos(but, data, x);
1377                                                 
1378         if (data->selextend == EXTEND_RIGHT) but->selend = but->pos;
1379         if (data->selextend == EXTEND_LEFT) but->selsta = but->pos;
1380
1381         ui_check_but(but);
1382 }
1383
1384 /* this is used for both utf8 and ascii, its meant to be used for single keys,
1385  * notice the buffer is either copied or not, so its not suitable for pasting in
1386  * - campbell */
1387 static int ui_textedit_type_buf(uiBut *but, uiHandleButtonData *data,
1388                                 const char *utf8_buf, int utf8_buf_len)
1389 {
1390         char *str;
1391         int len, changed = 0;
1392
1393         str = data->str;
1394         len = strlen(str);
1395
1396         if (len - (but->selend - but->selsta) + 1 <= data->maxlen) {
1397                 int step = utf8_buf_len;
1398
1399                 /* type over the current selection */
1400                 if ((but->selend - but->selsta) > 0) {
1401                         changed = ui_textedit_delete_selection(but, data);
1402                         len = strlen(str);
1403                 }
1404
1405                 if (len + step < data->maxlen) {
1406                         memmove(&str[but->pos + step], &str[but->pos], (len + 1) - but->pos);
1407                         memcpy(&str[but->pos], utf8_buf, step * sizeof(char));
1408                         but->pos += step;
1409                         changed = 1;
1410                 }
1411         }
1412
1413         return changed;
1414 }
1415
1416 static int ui_textedit_type_ascii(uiBut *but, uiHandleButtonData *data, char ascii)
1417 {
1418         char buf[2] = {ascii, '\0'};
1419
1420         if (ui_is_but_utf8(but) && (BLI_str_utf8_size(buf) == -1)) {
1421                 printf("%s: entering invalid ascii char into an ascii key (%d)\n",
1422                        __func__, (int)(unsigned char)ascii);
1423
1424                 return 0;
1425         }
1426
1427         /* in some cases we want to allow invalid utf8 chars */
1428         return ui_textedit_type_buf(but, data, buf, 1);
1429 }
1430
1431 static void ui_textedit_move(uiBut *but, uiHandleButtonData *data, strCursorJumpDirection direction, int select, strCursorJumpType jump)
1432 {
1433         const char *str = data->str;
1434         const int len = strlen(str);
1435         const int pos_prev = but->pos;
1436         const int has_sel = (but->selend - but->selsta) > 0;
1437
1438         ui_check_but(but);
1439
1440         /* special case, quit selection and set cursor */
1441         if (has_sel && !select) {
1442                 if (jump == STRCUR_JUMP_ALL) {
1443                         but->selsta = but->selend = but->pos = direction ? len : 0;
1444                 }
1445                 else {
1446                         if (direction) {
1447                                 but->selsta = but->pos = but->selend;
1448                         }
1449                         else {
1450                                 but->pos = but->selend = but->selsta;
1451                         }
1452                 }
1453                 data->selextend = 0;
1454         }
1455         else {
1456                 int pos_i = but->pos;
1457                 BLI_str_cursor_step_utf8(str, len, &pos_i, direction, jump);
1458                 but->pos = pos_i;
1459
1460                 if (select) {
1461                         /* existing selection */
1462                         if (has_sel) {
1463
1464                                 if (data->selextend == 0) {
1465                                         data->selextend = EXTEND_RIGHT;
1466                                 }
1467
1468                                 if (direction) {
1469                                         if (data->selextend == EXTEND_RIGHT) {
1470                                                 but->selend = but->pos;
1471                                         }
1472                                         else {
1473                                                 but->selsta = but->pos;
1474                                         }
1475                                 }
1476                                 else {
1477                                         if (data->selextend == EXTEND_LEFT) {
1478                                                 but->selsta = but->pos;
1479                                         }
1480                                         else {
1481                                                 but->selend = but->pos;
1482                                         }
1483                                 }
1484
1485                                 if (but->selend < but->selsta) {
1486                                         SWAP(short, but->selsta, but->selend);
1487                                         data->selextend = (data->selextend == EXTEND_RIGHT) ? EXTEND_LEFT : EXTEND_RIGHT;
1488                                 }
1489
1490                         } /* new selection */
1491                         else {
1492                                 if (direction) {
1493                                         data->selextend = EXTEND_RIGHT;
1494                                         but->selend = but->pos;
1495                                         but->selsta = pos_prev;
1496                                 }
1497                                 else {
1498                                         data->selextend = EXTEND_LEFT;
1499                                         but->selend = pos_prev;
1500                                         but->selsta = but->pos;
1501                                 }
1502                         }
1503                 }
1504         }
1505 }
1506
1507 static int ui_textedit_delete(uiBut *but, uiHandleButtonData *data, int direction, strCursorJumpType jump)
1508 {
1509         char *str = data->str;
1510         const int len = strlen(str);
1511
1512         int changed = 0;
1513
1514         if (jump == STRCUR_JUMP_ALL) {
1515                 if (len) changed = 1;
1516                 str[0] = '\0';
1517                 but->pos = 0;
1518         }
1519         else if (direction) { /* delete */
1520                 if ((but->selend - but->selsta) > 0) {
1521                         changed = ui_textedit_delete_selection(but, data);
1522                 }
1523                 else if (but->pos >= 0 && but->pos < len) {
1524                         int pos = but->pos;
1525                         int step;
1526                         BLI_str_cursor_step_utf8(str, len, &pos, direction, jump);
1527                         step = pos - but->pos;
1528                         memmove(&str[but->pos], &str[but->pos + step], (len + 1) - but->pos);
1529                         changed = 1;
1530                 }
1531         }
1532         else { /* backspace */
1533                 if (len != 0) {
1534                         if ((but->selend - but->selsta) > 0) {
1535                                 changed = ui_textedit_delete_selection(but, data);
1536                         }
1537                         else if (but->pos > 0) {
1538                                 int pos = but->pos;
1539                                 int step;
1540
1541                                 BLI_str_cursor_step_utf8(str, len, &pos, direction, jump);
1542                                 step = but->pos - pos;
1543                                 memmove(&str[but->pos - step], &str[but->pos], (len + 1) - but->pos);
1544                                 but->pos -= step;
1545                                 changed = 1;
1546                         }
1547                 } 
1548         }
1549
1550         return changed;
1551 }
1552
1553 static int ui_textedit_autocomplete(bContext *C, uiBut *but, uiHandleButtonData *data)
1554 {
1555         char *str;
1556         int changed = 1;
1557
1558         str = data->str;
1559
1560         if (data->searchbox)
1561                 ui_searchbox_autocomplete(C, data->searchbox, but, data->str);
1562         else
1563                 but->autocomplete_func(C, str, but->autofunc_arg);
1564
1565         but->pos = strlen(str);
1566         but->selsta = but->selend = but->pos;
1567
1568         return changed;
1569 }
1570
1571 static int ui_textedit_copypaste(uiBut *but, uiHandleButtonData *data, int paste, int copy, int cut)
1572 {
1573         char buf[UI_MAX_DRAW_STR] = {0};
1574         char *str, *p, *pbuf;
1575         int len, x, i, changed = 0;
1576
1577         str = data->str;
1578         len = strlen(str);
1579         
1580         /* paste */
1581         if (paste) {
1582                 /* extract the first line from the clipboard */
1583                 p = pbuf = WM_clipboard_text_get(0);
1584
1585                 if (p && p[0]) {
1586                         unsigned int y;
1587                         i = 0;
1588                         while (*p && *p != '\r' && *p != '\n' && i < UI_MAX_DRAW_STR - 1) {
1589                                 buf[i++] = *p;
1590                                 p++;
1591                         }
1592                         buf[i] = 0;
1593
1594                         /* paste over the current selection */
1595                         if ((but->selend - but->selsta) > 0) {
1596                                 ui_textedit_delete_selection(but, data);
1597                                 len = strlen(str);
1598                         }
1599                         
1600                         for (y = 0; y < strlen(buf); y++) {
1601                                 /* add contents of buffer */
1602                                 if (len + 1 < data->maxlen) {
1603                                         for (x = data->maxlen; x > but->pos; x--)
1604                                                 str[x] = str[x - 1];
1605                                         str[but->pos] = buf[y];
1606                                         but->pos++; 
1607                                         len++;
1608                                         str[len] = '\0';
1609                                 }
1610                         }
1611
1612                         changed = 1;
1613                 }
1614
1615                 if (pbuf) {
1616                         MEM_freeN(pbuf);
1617                 }
1618         }
1619         /* cut & copy */
1620         else if (copy || cut) {
1621                 /* copy the contents to the copypaste buffer */
1622                 for (x = but->selsta; x <= but->selend; x++) {
1623                         if (x == but->selend)
1624                                 buf[x] = '\0';
1625                         else
1626                                 buf[(x - but->selsta)] = str[x];
1627                 }
1628
1629                 WM_clipboard_text_set(buf, 0);
1630                 
1631                 /* for cut only, delete the selection afterwards */
1632                 if (cut)
1633                         if ((but->selend - but->selsta) > 0)
1634                                 changed = ui_textedit_delete_selection(but, data);
1635         } 
1636
1637         return changed;
1638 }
1639
1640 static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
1641 {
1642         int len;
1643
1644         if (data->str) {
1645                 MEM_freeN(data->str);
1646                 data->str = NULL;
1647         }
1648
1649         /* retrieve string */
1650         data->maxlen = ui_get_but_string_max_length(but);
1651         data->str = MEM_callocN(sizeof(char) * data->maxlen + 1, "textedit str");
1652         ui_get_but_string(but, data->str, data->maxlen);
1653
1654         if (ELEM3(but->type, NUM, NUMABS, NUMSLI)) {
1655                 ui_convert_to_unit_alt_name(but, data->str, data->maxlen);
1656         }
1657
1658         /* won't change from now on */
1659         len = strlen(data->str);
1660
1661         data->origstr = BLI_strdupn(data->str, len);
1662         data->selextend = 0;
1663         data->selstartx = 0;
1664
1665         /* set cursor pos to the end of the text */
1666         but->editstr = data->str;
1667         but->pos = len;
1668         but->selsta = 0;
1669         but->selend = len;
1670
1671         /* optional searchbox */
1672         if (but->type == SEARCH_MENU) {
1673                 data->searchbox = ui_searchbox_create(C, data->region, but);
1674                 ui_searchbox_update(C, data->searchbox, but, 1); /* 1= reset */
1675         }
1676         
1677         ui_check_but(but);
1678         
1679         WM_cursor_modal(CTX_wm_window(C), BC_TEXTEDITCURSOR);
1680 }
1681
1682 static void ui_textedit_end(bContext *C, uiBut *but, uiHandleButtonData *data)
1683 {
1684         if (but) {
1685                 if (ui_is_but_utf8(but)) {
1686                         int strip = BLI_utf8_invalid_strip(but->editstr, strlen(but->editstr));
1687                         /* not a file?, strip non utf-8 chars */
1688                         if (strip) {
1689                                 /* wont happen often so isn't that annoying to keep it here for a while */
1690                                 printf("%s: invalid utf8 - stripped chars %d\n", __func__, strip);
1691                         }
1692                 }
1693                 
1694                 if (data->searchbox) {
1695                         if (data->cancel == 0)
1696                                 ui_searchbox_apply(but, data->searchbox);
1697
1698                         ui_searchbox_free(C, data->searchbox);
1699                         data->searchbox = NULL;
1700                 }
1701                 
1702                 but->editstr = NULL;
1703                 but->pos = -1;
1704         }
1705         
1706         WM_cursor_restore(CTX_wm_window(C));
1707 }
1708
1709 static void ui_textedit_next_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1710 {
1711         uiBut *but;
1712
1713         /* label and roundbox can overlap real buttons (backdrops...) */
1714         if (ELEM4(actbut->type, LABEL, SEPR, ROUNDBOX, LISTBOX))
1715                 return;
1716
1717         for (but = actbut->next; but; but = but->next) {
1718                 if (ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1719                         if (!(but->flag & UI_BUT_DISABLED)) {
1720                                 data->postbut = but;
1721                                 data->posttype = BUTTON_ACTIVATE_TEXT_EDITING;
1722                                 return;
1723                         }
1724                 }
1725         }
1726         for (but = block->buttons.first; but != actbut; but = but->next) {
1727                 if (ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1728                         if (!(but->flag & UI_BUT_DISABLED)) {
1729                                 data->postbut = but;
1730                                 data->posttype = BUTTON_ACTIVATE_TEXT_EDITING;
1731                                 return;
1732                         }
1733                 }
1734         }
1735 }
1736
1737 static void ui_textedit_prev_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1738 {
1739         uiBut *but;
1740
1741         /* label and roundbox can overlap real buttons (backdrops...) */
1742         if (ELEM4(actbut->type, LABEL, SEPR, ROUNDBOX, LISTBOX))
1743                 return;
1744
1745         for (but = actbut->prev; but; but = but->prev) {
1746                 if (ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1747                         if (!(but->flag & UI_BUT_DISABLED)) {
1748                                 data->postbut = but;
1749                                 data->posttype = BUTTON_ACTIVATE_TEXT_EDITING;
1750                                 return;
1751                         }
1752                 }
1753         }
1754         for (but = block->buttons.last; but != actbut; but = but->prev) {
1755                 if (ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1756                         if (!(but->flag & UI_BUT_DISABLED)) {
1757                                 data->postbut = but;
1758                                 data->posttype = BUTTON_ACTIVATE_TEXT_EDITING;
1759                                 return;
1760                         }
1761                 }
1762         }
1763 }
1764
1765
1766 static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1767 {
1768         int mx, my, changed = 0, inbox = 0, update = 0, retval = WM_UI_HANDLER_CONTINUE;
1769
1770         switch (event->type) {
1771                 case WHEELUPMOUSE:
1772                 case WHEELDOWNMOUSE:
1773                 case MOUSEMOVE:
1774                         if (data->searchbox)
1775                                 ui_searchbox_event(C, data->searchbox, but, event);
1776                         
1777                         break;
1778                 case RIGHTMOUSE:
1779                 case ESCKEY:
1780                         data->cancel = 1;
1781                         data->escapecancel = 1;
1782                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1783                         retval = WM_UI_HANDLER_BREAK;
1784                         break;
1785                 case LEFTMOUSE: {
1786                         
1787                         /* exit on LMB only on RELEASE for searchbox, to mimic other popups, and allow multiple menu levels */
1788                         if (data->searchbox)
1789                                 inbox = ui_searchbox_inside(data->searchbox, event->x, event->y);
1790
1791                         if (event->val == KM_PRESS) {
1792                                 mx = event->x;
1793                                 my = event->y;
1794                                 ui_window_to_block(data->region, block, &mx, &my);
1795
1796                                 if (ui_but_contains_pt(but, mx, my)) {
1797                                         ui_textedit_set_cursor_pos(but, data, mx);
1798                                         but->selsta = but->selend = but->pos;
1799                                         data->selstartx = mx;
1800
1801                                         button_activate_state(C, but, BUTTON_STATE_TEXT_SELECTING);
1802                                         retval = WM_UI_HANDLER_BREAK;
1803                                 }
1804                                 else if (inbox == 0) {
1805                                         /* if searchbox, click outside will cancel */
1806                                         if (data->searchbox)
1807                                                 data->cancel = data->escapecancel = 1;
1808                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1809                                         retval = WM_UI_HANDLER_BREAK;
1810                                 }
1811                         }
1812                         else if (inbox) {
1813                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1814                                 retval = WM_UI_HANDLER_BREAK;
1815                         }
1816                         break;
1817                 }
1818         }
1819
1820         if (event->val == KM_PRESS) {
1821                 switch (event->type) {
1822                         case VKEY:
1823                         case XKEY:
1824                         case CKEY:
1825                                 if (event->ctrl || event->oskey) {
1826                                         if (event->type == VKEY)
1827                                                 changed = ui_textedit_copypaste(but, data, 1, 0, 0);
1828                                         else if (event->type == CKEY)
1829                                                 changed = ui_textedit_copypaste(but, data, 0, 1, 0);
1830                                         else if (event->type == XKEY)
1831                                                 changed = ui_textedit_copypaste(but, data, 0, 0, 1);
1832
1833                                         retval = WM_UI_HANDLER_BREAK;
1834                                 }
1835                                 break;
1836                         case RIGHTARROWKEY:
1837                                 ui_textedit_move(but, data, STRCUR_DIR_NEXT, event->shift, event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE);
1838                                 retval = WM_UI_HANDLER_BREAK;
1839                                 break;
1840                         case LEFTARROWKEY:
1841                                 ui_textedit_move(but, data, STRCUR_DIR_PREV, event->shift, event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE);
1842                                 retval = WM_UI_HANDLER_BREAK;
1843                                 break;
1844                         case DOWNARROWKEY:
1845                                 if (data->searchbox) {
1846                                         ui_searchbox_event(C, data->searchbox, but, event);
1847                                         break;
1848                                 }
1849                         /* pass on purposedly */
1850                         case ENDKEY:
1851                                 ui_textedit_move(but, data, STRCUR_DIR_NEXT, event->shift, STRCUR_JUMP_ALL);
1852                                 retval = WM_UI_HANDLER_BREAK;
1853                                 break;
1854                         case UPARROWKEY:
1855                                 if (data->searchbox) {
1856                                         ui_searchbox_event(C, data->searchbox, but, event);
1857                                         break;
1858                                 }
1859                         /* pass on purposedly */
1860                         case HOMEKEY:
1861                                 ui_textedit_move(but, data, STRCUR_DIR_PREV, event->shift, STRCUR_JUMP_ALL);
1862                                 retval = WM_UI_HANDLER_BREAK;
1863                                 break;
1864                         case PADENTER:
1865                         case RETKEY:
1866                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1867                                 retval = WM_UI_HANDLER_BREAK;
1868                                 break;
1869                         case DELKEY:
1870                                 changed = ui_textedit_delete(but, data, 1, event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE);
1871                                 retval = WM_UI_HANDLER_BREAK;
1872                                 break;
1873
1874                         case BACKSPACEKEY:
1875                                 changed = ui_textedit_delete(but, data, 0, event->shift ? STRCUR_JUMP_ALL :  (event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE));
1876                                 retval = WM_UI_HANDLER_BREAK;
1877                                 break;
1878                                 
1879                         case TABKEY:
1880                                 /* there is a key conflict here, we can't tab with autocomplete */
1881                                 if (but->autocomplete_func || data->searchbox) {
1882                                         changed = ui_textedit_autocomplete(C, but, data);
1883                                         update = 1; /* do live update for tab key */
1884                                 }
1885                                 /* the hotkey here is not well defined, was G.qual so we check all */
1886                                 else if (event->shift || event->ctrl || event->alt || event->oskey) {
1887                                         ui_textedit_prev_but(block, but, data);
1888                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1889                                 }
1890                                 else {
1891                                         ui_textedit_next_but(block, but, data);
1892                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1893                                 }
1894                                 retval = WM_UI_HANDLER_BREAK;
1895                                 break;
1896                 }
1897
1898                 if ((event->ascii || event->utf8_buf[0]) && (retval == WM_UI_HANDLER_CONTINUE)) {
1899                         char ascii = event->ascii;
1900                         const char *utf8_buf = event->utf8_buf;
1901
1902                         /* exception that's useful for number buttons, some keyboard
1903                          * numpads have a comma instead of a period */
1904                         if (ELEM3(but->type, NUM, NUMABS, NUMSLI)) { /* could use data->min*/
1905                                 if (event->type == PADPERIOD && ascii == ',') {
1906                                         ascii = '.';
1907                                         utf8_buf = NULL; /* force ascii fallback */
1908                                 }
1909                         }
1910
1911                         if (utf8_buf && utf8_buf[0]) {
1912                                 int utf8_buf_len = BLI_str_utf8_size(utf8_buf);
1913                                 /* keep this printf until utf8 is well tested */
1914                                 if (utf8_buf_len != 1) {
1915                                         printf("%s: utf8 char '%.*s'\n", __func__, utf8_buf_len, utf8_buf);
1916                                 }
1917
1918                                 // strcpy(utf8_buf, "12345");
1919                                 changed = ui_textedit_type_buf(but, data, event->utf8_buf, utf8_buf_len);
1920                         }
1921                         else {
1922                                 changed = ui_textedit_type_ascii(but, data, ascii);
1923                         }
1924
1925                         retval = WM_UI_HANDLER_BREAK;
1926                         
1927                 }
1928                 /* textbutton with magnifier icon: do live update for search button */
1929                 if (but->icon == ICON_VIEWZOOM)
1930                         update = 1;
1931         }
1932
1933         if (changed) {
1934                 /* only update when typing for TAB key */
1935                 if (update && data->interactive) ui_apply_button(C, block, but, data, 1);
1936                 else ui_check_but(but);
1937                 but->changed = TRUE;
1938                 
1939                 if (data->searchbox)
1940                         ui_searchbox_update(C, data->searchbox, but, 1);  /* 1 = reset */
1941         }
1942
1943         if (changed || (retval == WM_UI_HANDLER_BREAK))
1944                 ED_region_tag_redraw(data->region);
1945 }
1946
1947 static void ui_do_but_textedit_select(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1948 {
1949         int mx, my, retval = WM_UI_HANDLER_CONTINUE;
1950
1951         switch (event->type) {
1952                 case MOUSEMOVE: {
1953                         mx = event->x;
1954                         my = event->y;
1955                         ui_window_to_block(data->region, block, &mx, &my);
1956
1957                         ui_textedit_set_cursor_select(but, data, mx);
1958                         retval = WM_UI_HANDLER_BREAK;
1959                         break;
1960                 }
1961                 case LEFTMOUSE:
1962                         if (event->val == KM_RELEASE)
1963                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1964                         retval = WM_UI_HANDLER_BREAK;
1965                         break;
1966         }
1967
1968         if (retval == WM_UI_HANDLER_BREAK) {
1969                 ui_check_but(but);
1970                 ED_region_tag_redraw(data->region);
1971         }
1972 }
1973
1974 /* ************* number editing for various types ************* */
1975
1976 static void ui_numedit_begin(uiBut *but, uiHandleButtonData *data)
1977 {
1978         if (but->type == BUT_CURVE) {
1979                 but->editcumap = (CurveMapping *)but->poin;
1980         }
1981         else if (but->type == BUT_COLORBAND) {
1982                 data->coba = (ColorBand *)but->poin;
1983                 but->editcoba = data->coba;
1984         }
1985         else if (ELEM3(but->type, BUT_NORMAL, HSVCUBE, HSVCIRCLE)) {
1986                 ui_get_but_vectorf(but, data->origvec);
1987                 copy_v3_v3(data->vec, data->origvec);
1988                 but->editvec = data->vec;
1989         }
1990         else {
1991                 float softrange, softmin, softmax;
1992
1993                 data->startvalue = ui_get_but_val(but);
1994                 data->origvalue = data->startvalue;
1995                 data->value = data->origvalue;
1996                 but->editval = &data->value;
1997
1998                 softmin = but->softmin;
1999                 softmax = but->softmax;
2000                 softrange = softmax - softmin;
2001
2002                 data->dragfstart = (softrange == 0.0f) ? 0.0f : ((float)data->value - softmin) / softrange;
2003                 data->dragf = data->dragfstart;
2004         }
2005
2006         data->dragchange = 0;
2007         data->draglock = 1;
2008 }
2009
2010 static void ui_numedit_end(uiBut *but, uiHandleButtonData *data)
2011 {
2012         but->editval = NULL;
2013         but->editvec = NULL;
2014         but->editcoba = NULL;
2015         but->editcumap = NULL;
2016
2017         data->dragstartx = 0;
2018         data->draglastx = 0;
2019         data->dragchange = 0;
2020         data->dragcbd = NULL;
2021         data->dragsel = 0;
2022 }
2023
2024 static void ui_numedit_apply(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data)
2025 {
2026         if (data->interactive) ui_apply_button(C, block, but, data, 1);
2027         else ui_check_but(but);
2028
2029         ED_region_tag_redraw(data->region);
2030 }
2031
2032 /* ****************** menu opening for various types **************** */
2033
2034 static void ui_blockopen_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
2035 {
2036         uiBlockCreateFunc func = NULL;
2037         uiBlockHandleCreateFunc handlefunc = NULL;
2038         uiMenuCreateFunc menufunc = NULL;
2039         char *menustr = NULL;
2040         void *arg = NULL;
2041
2042         switch (but->type) {
2043                 case BLOCK:
2044                 case PULLDOWN:
2045                         if (but->menu_create_func) {
2046                                 menufunc = but->menu_create_func;
2047                                 arg = but->poin;
2048                         }
2049                         else {
2050                                 func = but->block_create_func;
2051                                 arg = but->poin ? but->poin : but->func_argN;
2052                         }
2053                         break;
2054                 case MENU:
2055                         if (but->menu_create_func) {
2056                                 menufunc = but->menu_create_func;
2057                                 arg = but->poin;
2058                         }
2059                         else {
2060                                 data->origvalue = ui_get_but_val(but);
2061                                 data->value = data->origvalue;
2062                                 but->editval = &data->value;
2063
2064                                 menustr = but->str;
2065                         }
2066                         break;
2067                 case ICONROW:
2068                         menufunc = ui_block_func_ICONROW;
2069                         arg = but;
2070                         break;
2071                 case ICONTEXTROW:
2072                         menufunc = ui_block_func_ICONTEXTROW;
2073                         arg = but;
2074                         break;
2075                 case COL:
2076                         ui_get_but_vectorf(but, data->origvec);
2077                         copy_v3_v3(data->vec, data->origvec);
2078                         but->editvec = data->vec;
2079
2080                         handlefunc = ui_block_func_COL;
2081                         arg = but;
2082                         break;
2083         }
2084
2085         if (func || handlefunc) {
2086                 data->menu = ui_popup_block_create(C, data->region, but, func, handlefunc, arg);
2087                 if (but->block->handle)
2088                         data->menu->popup = but->block->handle->popup;
2089         }
2090         else if (menufunc || menustr) {
2091                 data->menu = ui_popup_menu_create(C, data->region, but, menufunc, arg, menustr);
2092                 if (but->block->handle)
2093                         data->menu->popup = but->block->handle->popup;
2094         }
2095
2096         /* this makes adjacent blocks auto open from now on */
2097         //if (but->block->auto_open==0) but->block->auto_open= 1;
2098 }
2099
2100 static void ui_blockopen_end(bContext *C, uiBut *but, uiHandleButtonData *data)
2101 {
2102         if (but) {
2103                 but->editval = NULL;
2104                 but->editvec = NULL;
2105
2106                 but->block->auto_open_last = PIL_check_seconds_timer();
2107         }
2108
2109         if (data->menu) {
2110                 ui_popup_block_free(C, data->menu);
2111                 data->menu = NULL;
2112         }
2113 }
2114
2115 /* ***************** events for different button types *************** */
2116
2117 static int ui_do_but_BUT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2118 {
2119         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2120                 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
2121                         button_activate_state(C, but, BUTTON_STATE_WAIT_RELEASE);
2122                         return WM_UI_HANDLER_BREAK;
2123                 }
2124                 else if (event->type == LEFTMOUSE && but->block->handle) {
2125                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2126                         return WM_UI_HANDLER_BREAK;
2127                 }
2128                 else if (ELEM(event->type, PADENTER, RETKEY) && event->val == KM_PRESS) {
2129                         button_activate_state(C, but, BUTTON_STATE_WAIT_FLASH);
2130                         return WM_UI_HANDLER_BREAK;
2131                 }
2132         }
2133         else if (data->state == BUTTON_STATE_WAIT_RELEASE) {
2134                 if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
2135                         if (!(but->flag & UI_SELECT))
2136                                 data->cancel = 1;
2137                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2138                         return WM_UI_HANDLER_BREAK;
2139                 }
2140         }
2141
2142         return WM_UI_HANDLER_CONTINUE;
2143 }
2144
2145 static int ui_do_but_HOTKEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2146 {
2147         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2148                 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2149                         but->drawstr[0] = 0;
2150                         but->modifier_key = 0;
2151                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
2152                         return WM_UI_HANDLER_BREAK;
2153                 }
2154         }
2155         else if (data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2156                 
2157                 if (event->type == MOUSEMOVE)
2158                         return WM_UI_HANDLER_CONTINUE;
2159                 
2160                 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
2161                         /* only cancel if click outside the button */
2162                         if (ui_mouse_inside_button(but->active->region, but, event->x, event->y) == 0) {
2163                                 /* data->cancel doesnt work, this button opens immediate */
2164                                 if (but->flag & UI_BUT_IMMEDIATE)
2165                                         ui_set_but_val(but, 0);
2166                                 else
2167                                         data->cancel = 1;
2168                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2169                                 return WM_UI_HANDLER_BREAK;
2170                         }
2171                 }
2172                 
2173                 /* always set */
2174                 but->modifier_key = 0;
2175                 if (event->shift) but->modifier_key |= KM_SHIFT;
2176                 if (event->alt) but->modifier_key |= KM_ALT;
2177                 if (event->ctrl) but->modifier_key |= KM_CTRL;
2178                 if (event->oskey) but->modifier_key |= KM_OSKEY;
2179
2180                 ui_check_but(but);
2181                 ED_region_tag_redraw(data->region);
2182                         
2183                 if (event->val == KM_PRESS) {
2184                         if (ISHOTKEY(event->type)) { 
2185                                 
2186                                 if (WM_key_event_string(event->type)[0])
2187                                         ui_set_but_val(but, event->type);
2188                                 else
2189                                         data->cancel = 1;
2190                                 
2191                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2192                                 return WM_UI_HANDLER_BREAK;
2193                         }
2194                         else if (event->type == ESCKEY) {
2195                                 data->cancel = 1;
2196                                 data->escapecancel = 1;
2197                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2198                         }
2199                         
2200                 }
2201         }
2202         
2203         return WM_UI_HANDLER_CONTINUE;
2204 }
2205
2206 static int ui_do_but_KEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2207 {
2208         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2209                 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2210                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
2211                         return WM_UI_HANDLER_BREAK;
2212                 }
2213         }
2214         else if (data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2215                 if (event->type == MOUSEMOVE)
2216                         return WM_UI_HANDLER_CONTINUE;
2217
2218                 if (event->val == KM_PRESS) {
2219                         if (WM_key_event_string(event->type)[0])
2220                                 ui_set_but_val(but, event->type);
2221                         else
2222                                 data->cancel = 1;
2223
2224                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2225                 }
2226         }
2227
2228         return WM_UI_HANDLER_CONTINUE;
2229 }
2230
2231 static int ui_do_but_TEX(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2232 {
2233         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2234                 if (ELEM(event->type, LEFTMOUSE, EVT_BUT_OPEN) && event->val == KM_PRESS) {
2235                         if (but->dt == UI_EMBOSSN && !event->ctrl) ;
2236                         else {
2237                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2238                                 return WM_UI_HANDLER_BREAK;
2239                         }
2240                 }
2241         }
2242         else if (data->state == BUTTON_STATE_TEXT_EDITING) {
2243                 ui_do_but_textedit(C, block, but, data, event);
2244                 return WM_UI_HANDLER_BREAK;
2245         }
2246         else if (data->state == BUTTON_STATE_TEXT_SELECTING) {
2247                 ui_do_but_textedit_select(C, block, but, data, event);
2248                 return WM_UI_HANDLER_BREAK;
2249         }
2250
2251         return WM_UI_HANDLER_CONTINUE;
2252 }
2253
2254 static int ui_do_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2255 {
2256         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2257                 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2258                         data->togdual = event->ctrl;
2259                         data->togonly = !event->shift;
2260                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2261                         return WM_UI_HANDLER_BREAK;
2262                 }
2263         }
2264         return WM_UI_HANDLER_CONTINUE;
2265 }
2266
2267 static int ui_do_but_EXIT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2268 {
2269         
2270         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2271
2272                 /* first handle click on icondrag type button */
2273                 if (event->type == LEFTMOUSE && but->dragpoin) {
2274                         if (ui_but_mouse_inside_icon(but, data->region, event)) {
2275                                 
2276                                 /* tell the button to wait and keep checking further events to
2277                                  * see if it should start dragging */
2278                                 button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
2279                                 data->dragstartx = event->x;
2280                                 data->dragstarty = event->y;
2281                                 return WM_UI_HANDLER_CONTINUE;
2282                         }
2283                 }
2284                 
2285                 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2286                         int ret = WM_UI_HANDLER_BREAK;
2287                         /* XXX (a bit ugly) Special case handling for filebrowser drag button */
2288                         if (but->dragpoin && but->imb && ui_but_mouse_inside_icon(but, data->region, event)) {
2289                                 ret = WM_UI_HANDLER_CONTINUE;
2290                         }
2291                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2292                         return ret;
2293                 }
2294         }
2295         else if (data->state == BUTTON_STATE_WAIT_DRAG) {
2296                 
2297                 /* this function also ends state */
2298                 if (ui_but_start_drag(C, but, data, event)) {
2299                         return WM_UI_HANDLER_BREAK;
2300                 }
2301                 
2302                 /* If the mouse has been pressed and released, getting to 
2303                  * this point without triggering a drag, then clear the 
2304                  * drag state for this button and continue to pass on the event */
2305                 if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
2306                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2307                         return WM_UI_HANDLER_CONTINUE;
2308                 }
2309                 
2310                 /* while waiting for a drag to be triggered, always block 
2311                  * other events from getting handled */
2312                 return WM_UI_HANDLER_BREAK;
2313         }
2314         
2315         return WM_UI_HANDLER_CONTINUE;
2316 }
2317
2318 /* var names match ui_numedit_but_NUM */
2319 static float ui_numedit_apply_snapf(uiBut *but, float tempf, float softmin, float softmax, float softrange, int snap)
2320 {
2321         if (tempf == softmin || tempf == softmax || snap == 0) {
2322                 /* pass */
2323         }
2324         else {
2325                 float fac = 1.0f;
2326                 
2327                 if (ui_is_but_unit(but)) {
2328                         UnitSettings *unit = but->block->unit;
2329                         int unit_type = RNA_SUBTYPE_UNIT_VALUE(uiButGetUnitType(but));
2330
2331                         if (bUnit_IsValid(unit->system, unit_type)) {
2332                                 fac = (float)bUnit_BaseScalar(unit->system, unit_type);
2333                                 if (ELEM3(unit_type, B_UNIT_LENGTH, B_UNIT_AREA, B_UNIT_VOLUME)) {
2334                                         fac /= unit->scale_length;
2335                                 }
2336                         }
2337                 }
2338
2339                 if (fac != 1.0f) {
2340                         /* snap in unit-space */
2341                         tempf /= fac;
2342                         /* softmin /= fac; */ /* UNUSED */
2343                         /* softmax /= fac; */ /* UNUSED */
2344                         softrange /= fac;
2345                 }
2346
2347                 if (snap == 1) {
2348                         if (softrange < 2.10f) tempf = 0.1f * floorf(10.0f * tempf);
2349                         else if (softrange < 21.0f) tempf = floorf(tempf);
2350                         else tempf = 10.0f * floorf(tempf / 10.0f);
2351                 }
2352                 else if (snap == 2) {
2353                         if (softrange < 2.10f) tempf = 0.01f * floorf(100.0f * tempf);
2354                         else if (softrange < 21.0f) tempf = 0.1f * floorf(10.0f * tempf);
2355                         else tempf = floor(tempf);
2356                 }
2357                 
2358                 if (fac != 1.0f)
2359                         tempf *= fac;
2360         }
2361
2362         return tempf;
2363 }
2364
2365 static float ui_numedit_apply_snap(int temp, float softmin, float softmax, int snap)
2366 {
2367         if (temp == softmin || temp == softmax)
2368                 return temp;
2369
2370         switch (snap) {
2371                 case 0:
2372                         break;
2373                 case 1:
2374                         temp = 10 * (temp / 10);
2375                         break;
2376                 case 2:
2377                         temp = 100 * (temp / 100);
2378                         break;
2379         }
2380
2381         return temp;
2382 }
2383
2384 static int ui_numedit_but_NUM(uiBut *but, uiHandleButtonData *data, float fac, int snap, int mx)
2385 {
2386         float deler, tempf, softmin, softmax, softrange;
2387         int lvalue, temp, changed = 0;
2388         
2389         if (mx == data->draglastx)
2390                 return changed;
2391         
2392         /* drag-lock - prevent unwanted scroll adjustments */
2393         /* change value (now 3) to adjust threshold in pixels */
2394         if (data->draglock) {
2395                 if (abs(mx - data->dragstartx) <= 3)
2396                         return changed;
2397
2398                 data->draglock = 0;
2399                 data->dragstartx = mx;  /* ignore mouse movement within drag-lock */
2400         }
2401
2402         softmin = but->softmin;
2403         softmax = but->softmax;
2404         softrange = softmax - softmin;
2405
2406         if (ui_is_a_warp_but(but)) {
2407                 /* Mouse location isn't screen clamped to the screen so use a linear mapping
2408                  * 2px == 1-int, or 1px == 1-ClickStep */
2409                 if (ui_is_but_float(but)) {
2410                         fac *= 0.01f * but->a1;
2411                         tempf = (float)data->startvalue + ((float)(mx - data->dragstartx) * fac);
2412                         tempf = ui_numedit_apply_snapf(but, tempf, softmin, softmax, softrange, snap);
2413
2414 #if 1       /* fake moving the click start, nicer for dragging back after passing the limit */
2415                         if (tempf < softmin) {
2416                                 data->dragstartx -= (softmin - tempf) / fac;
2417                                 tempf = softmin;
2418                         }
2419                         else if (tempf > softmax) {
2420                                 data->dragstartx += (tempf - softmax) / fac;
2421                                 tempf = softmax;
2422                         }
2423 #else
2424                         CLAMP(tempf, softmin, softmax);
2425 #endif
2426
2427                         if (tempf != (float)data->value) {
2428                                 data->dragchange = 1;
2429                                 data->value = tempf;
2430                                 changed = 1;
2431                         }
2432                 }
2433                 else {
2434                         if (softrange > 256) fac = 1.0;             /* 1px == 1 */
2435                         else if (softrange > 32) fac = 1.0 / 2.0;   /* 2px == 1 */
2436                         else fac = 1.0 / 16.0;                  /* 16px == 1? */
2437
2438                         temp = data->startvalue + (((double)mx - data->dragstartx) * (double)fac);
2439                         temp = ui_numedit_apply_snap(temp, softmin, softmax, snap);
2440
2441 #if 1       /* fake moving the click start, nicer for dragging back after passing the limit */
2442                         if (temp < softmin) {
2443                                 data->dragstartx -= (softmin - temp) / fac;
2444                                 temp = softmin;
2445                         }
2446                         else if (temp > softmax) {
2447                                 data->dragstartx += (temp - softmax) / fac;
2448                                 temp = softmax;
2449                         }
2450 #else
2451                         CLAMP(temp, softmin, softmax);
2452 #endif
2453
2454                         if (temp != data->value) {
2455                                 data->dragchange = 1;
2456                                 data->value = temp;
2457                                 changed = 1;
2458                         }
2459                 }
2460
2461                 data->draglastx = mx;
2462         }
2463         else {
2464                 /* Use a non-linear mapping of the mouse drag especially for large floats (normal behavior) */
2465                 deler = 500;
2466                 if (!ui_is_but_float(but)) {
2467                         /* prevent large ranges from getting too out of control */
2468                         if (softrange > 600) deler = powf(softrange, 0.75);
2469                         
2470                         if (softrange < 100) deler = 200.0;
2471                         if (softrange < 25) deler = 50.0;
2472                 }
2473                 deler /= fac;
2474
2475                 if (softrange > 11) {
2476                         /* non linear change in mouse input- good for high precicsion */
2477                         data->dragf += (((float)(mx - data->draglastx)) / deler) * (fabsf(data->dragstartx - mx) * 0.002f);
2478                 }
2479                 else if (softrange > 129) { /* only scale large int buttons */
2480                         /* non linear change in mouse input- good for high precicsionm ints need less fine tuning */
2481                         data->dragf += (((float)(mx - data->draglastx)) / deler) * (fabsf(data->dragstartx - mx) * 0.004f);
2482                 }
2483                 else {
2484                         /*no scaling */
2485                         data->dragf += ((float)(mx - data->draglastx)) / deler;
2486                 }
2487         
2488                 CLAMP(data->dragf, 0.0f, 1.0f);
2489                 data->draglastx = mx;
2490                 tempf = (softmin + data->dragf * softrange);
2491
2492
2493                 if (!ui_is_but_float(but)) {
2494                         temp = floorf(tempf + 0.5f);
2495
2496                         temp = ui_numedit_apply_snap(temp, softmin, softmax, snap);
2497
2498                         CLAMP(temp, softmin, softmax);
2499                         lvalue = (int)data->value;
2500                         
2501                         if (temp != lvalue) {
2502                                 data->dragchange = 1;
2503                                 data->value = (double)temp;
2504                                 changed = 1;
2505                         }
2506                 }
2507                 else {
2508                         temp = 0;
2509                         tempf = ui_numedit_apply_snapf(but, tempf, softmin, softmax, softrange, snap);
2510
2511                         CLAMP(tempf, softmin, softmax);
2512
2513                         if (tempf != (float)data->value) {
2514                                 data->dragchange = 1;
2515                                 data->value = tempf;
2516                                 changed = 1;
2517                         }
2518                 }
2519         }
2520
2521
2522         return changed;
2523 }
2524
2525 static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2526 {
2527         int mx, my; /* mouse location scaled to fit the UI */
2528         int screen_mx, screen_my; /* mouse location kept at screen pixel coords */
2529         int click = 0;
2530         int retval = WM_UI_HANDLER_CONTINUE;
2531         
2532         mx = screen_mx = event->x;
2533         my = screen_my = event->y;
2534
2535         ui_window_to_block(data->region, block, &mx, &my);
2536
2537         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2538                 /* XXX hardcoded keymap check.... */
2539                 if (event->type == WHEELDOWNMOUSE && event->alt) {
2540                         mx = but->x1;
2541                         click = 1;
2542                 }
2543                 else if (event->type == WHEELUPMOUSE && event->alt) {
2544                         mx = but->x2;
2545                         click = 1;
2546                 }
2547                 else if (event->val == KM_PRESS) {
2548                         if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2549                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2550                                 retval = WM_UI_HANDLER_BREAK;
2551                         }
2552                         else if (event->type == LEFTMOUSE) {
2553                                 data->dragstartx = data->draglastx = ui_is_a_warp_but(but) ? screen_mx : mx;
2554                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2555                                 retval = WM_UI_HANDLER_BREAK;
2556                         }
2557                         else if (ELEM(event->type, PADENTER, RETKEY) && event->val == KM_PRESS)
2558                                 click = 1;
2559                         else if (event->type == MINUSKEY && event->val == KM_PRESS) {
2560                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2561                                 data->value = -data->value;
2562                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2563                                 retval = WM_UI_HANDLER_BREAK;
2564                         }
2565                 }
2566                 
2567         }
2568         else if (data->state == BUTTON_STATE_NUM_EDITING) {
2569                 if (event->type == ESCKEY) {
2570                         data->cancel = 1;
2571                         data->escapecancel = 1;
2572                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2573                 }
2574                 else if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
2575                         if (data->dragchange)
2576                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2577                         else
2578                                 click = 1;
2579                 }
2580                 else if (event->type == MOUSEMOVE) {
2581                         float fac;
2582                         int snap;
2583
2584                         fac = 1.0f;
2585                         if (event->shift) fac /= 10.0f;
2586                         if (event->alt) fac /= 20.0f;
2587                         
2588                         snap = (event->ctrl) ? (event->shift) ? 2 : 1 : 0;
2589
2590                         if (ui_numedit_but_NUM(but, data, fac, snap, (ui_is_a_warp_but(but) ? screen_mx : mx)))
2591                                 ui_numedit_apply(C, block, but, data);
2592                 }
2593                 retval = WM_UI_HANDLER_BREAK;
2594         }
2595         else if (data->state == BUTTON_STATE_TEXT_EDITING) {
2596                 ui_do_but_textedit(C, block, but, data, event);
2597                 retval = WM_UI_HANDLER_BREAK;
2598         }
2599         else if (data->state == BUTTON_STATE_TEXT_SELECTING) {
2600                 ui_do_but_textedit_select(C, block, but, data, event);
2601                 retval = WM_UI_HANDLER_BREAK;
2602         }
2603         
2604         if (click) {
2605                 /* we can click on the side arrows to increment/decrement,
2606                  * or click inside to edit the value directly */
2607                 float tempf, softmin, softmax;
2608                 int temp;
2609
2610                 softmin = but->softmin;
2611                 softmax = but->softmax;
2612
2613                 if (!ui_is_but_float(but)) {
2614                         if (mx < (but->x1 + (but->x2 - but->x1) / 3 - 3)) {
2615                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2616
2617                                 temp = (int)data->value - 1;
2618                                 if (temp >= softmin && temp <= softmax)
2619                                         data->value = (double)temp;
2620                                 else
2621                                         data->cancel = 1;
2622
2623                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2624                         }
2625                         else if (mx > (but->x1 + (2 * (but->x2 - but->x1) / 3) + 3)) {
2626                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2627
2628                                 temp = (int)data->value + 1;
2629                                 if (temp >= softmin && temp <= softmax)
2630                                         data->value = (double)temp;
2631                                 else
2632                                         data->cancel = 1;
2633
2634                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2635                         }
2636                         else
2637                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2638                 }
2639                 else {
2640                         if (mx < (but->x1 + (but->x2 - but->x1) / 3 - 3)) {
2641                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2642
2643                                 tempf = (float)data->value - 0.01f * but->a1;
2644                                 if (tempf < softmin) tempf = softmin;
2645                                 data->value = tempf;
2646
2647                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2648                         }
2649                         else if (mx > but->x1 + (2 * ((but->x2 - but->x1) / 3) + 3)) {
2650                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2651
2652                                 tempf = (float)data->value + 0.01f * but->a1;
2653                                 if (tempf > softmax) tempf = softmax;
2654                                 data->value = tempf;
2655
2656                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2657                         }
2658                         else
2659                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2660                 }
2661
2662                 retval = WM_UI_HANDLER_BREAK;
2663         }
2664         
2665         return retval;
2666 }
2667
2668 static int ui_numedit_but_SLI(uiBut *but, uiHandleButtonData *data, const short shift, const short ctrl, int mx)
2669 {
2670         float deler, f, tempf, softmin, softmax, softrange;
2671         int temp, lvalue, changed = 0;
2672
2673         softmin = but->softmin;
2674         softmax = but->softmax;
2675         softrange = softmax - softmin;
2676
2677         if (but->type == NUMSLI) deler = ((but->x2 - but->x1) - 5.0f * but->aspect);
2678         else if (but->type == HSVSLI) deler = ((but->x2 - but->x1) / 2.0f - 5.0f * but->aspect);
2679         else if (but->type == SCROLL) {
2680                 int horizontal = (but->x2 - but->x1 > but->y2 - but->y1);
2681                 float size = (horizontal) ? (but->x2 - but->x1) : -(but->y2 - but->y1);
2682                 deler = size * (but->softmax - but->softmin) / (but->softmax - but->softmin + but->a1);
2683         }
2684         else deler = (but->x2 - but->x1 - 5.0f * but->aspect);
2685
2686         f = (float)(mx - data->dragstartx) / deler + data->dragfstart;
2687         
2688         if (shift)
2689                 f = (f - data->dragfstart) / 10.0f + data->dragfstart;
2690
2691         CLAMP(f, 0.0f, 1.0f);
2692         tempf = softmin + f * softrange;
2693         temp = floorf(tempf + 0.5f);
2694
2695         if (ctrl) {
2696                 if (tempf == softmin || tempf == softmax) ;
2697                 else if (ui_is_but_float(but)) {
2698
2699                         if (shift) {
2700                                 if (tempf == softmin || tempf == softmax) ;
2701                                 else if (softmax - softmin < 2.10f) tempf = 0.01f * floorf(100.0f * tempf);
2702                                 else if (softmax - softmin < 21.0f) tempf = 0.1f * floorf(10.0f * tempf);
2703                                 else tempf = floorf(tempf);
2704                         }
2705                         else {
2706                                 if (softmax - softmin < 2.10f) tempf = 0.1f * floorf(10.0f * tempf);
2707                                 else if (softmax - softmin < 21.0f) tempf = floorf(tempf);
2708                                 else tempf = 10.0f * floorf(tempf / 10.0f);
2709                         }
2710                 }
2711                 else {
2712                         temp = 10 * (temp / 10);
2713                         tempf = temp;
2714                 }
2715         }
2716
2717         if (!ui_is_but_float(but)) {
2718                 lvalue = floor(data->value + 0.5);
2719
2720                 CLAMP(temp, softmin, softmax);
2721
2722                 if (temp != lvalue) {
2723                         data->value = temp;
2724                         data->dragchange = 1;
2725                         changed = 1;
2726                 }
2727         }
2728         else {
2729                 CLAMP(tempf, softmin, softmax);
2730
2731                 if (tempf != (float)data->value) {
2732                         data->value = tempf;
2733                         data->dragchange = 1;
2734                         changed = 1;
2735                 }
2736         }
2737
2738         return changed;
2739 }
2740
2741 static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2742 {
2743         int mx, my, click = 0;
2744         int retval = WM_UI_HANDLER_CONTINUE;
2745
2746         mx = event->x;
2747         my = event->y;
2748         ui_window_to_block(data->region, block, &mx, &my);
2749
2750         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2751                 /* XXX hardcoded keymap check.... */
2752                 if (event->type == WHEELDOWNMOUSE && event->alt) {
2753                         mx = but->x1;
2754                         click = 2;
2755                 }
2756                 else if (event->type == WHEELUPMOUSE && event->alt) {
2757                         mx = but->x2;
2758                         click = 2;
2759                 }
2760                 else if (event->val == KM_PRESS) {
2761                         if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2762                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2763                                 retval = WM_UI_HANDLER_BREAK;
2764                         }
2765                         /* alt-click on sides to get "arrows" like in NUM buttons, and match wheel usage above */
2766                         else if (event->type == LEFTMOUSE && event->alt) {
2767                                 int halfpos = (but->x1 + but->x2) / 2;
2768                                 click = 2;
2769                                 if (mx < halfpos)
2770                                         mx = but->x1;
2771                                 else
2772                                         mx = but->x2;
2773                         }
2774                         else if (event->type == LEFTMOUSE) {
2775                                 data->dragstartx = mx;
2776                                 data->draglastx = mx;
2777                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2778                                 retval = WM_UI_HANDLER_BREAK;
2779                         }
2780                         else if (ELEM(event->type, PADENTER, RETKEY) && event->val == KM_PRESS)
2781                                 click = 1;
2782                         else if (event->type == MINUSKEY && event->val == KM_PRESS) {
2783                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2784                                 data->value = -data->value;
2785                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2786                                 retval = WM_UI_HANDLER_BREAK;
2787                         }
2788                 }
2789         }
2790         else if (data->state == BUTTON_STATE_NUM_EDITING) {
2791                 if (event->type == ESCKEY) {
2792                         data->cancel = 1;
2793                         data->escapecancel = 1;
2794                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2795                 }
2796                 else if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
2797                         if (data->dragchange)
2798                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2799                         else
2800                                 click = 1;
2801                 }
2802                 else if (event->type == MOUSEMOVE) {
2803                         if (ui_numedit_but_SLI(but, data, event->shift, event->ctrl, mx))
2804                                 ui_numedit_apply(C, block, but, data);
2805                 }
2806                 retval = WM_UI_HANDLER_BREAK;
2807         }
2808         else if (data->state == BUTTON_STATE_TEXT_EDITING) {
2809                 ui_do_but_textedit(C, block, but, data, event);
2810                 retval = WM_UI_HANDLER_BREAK;
2811         }
2812         else if (data->state == BUTTON_STATE_TEXT_SELECTING) {
2813                 ui_do_but_textedit_select(C, block, but, data, event);
2814                 retval = WM_UI_HANDLER_BREAK;
2815         }
2816
2817         if (click) {
2818                 if (click == 2) {
2819                         /* nudge slider to the left or right */
2820                         float f, tempf, softmin, softmax, softrange;
2821                         int temp;
2822                         
2823                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2824                         
2825                         softmin = but->softmin;
2826                         softmax = but->softmax;
2827                         softrange = softmax - softmin;
2828                         
2829                         tempf = data->value;
2830                         temp = (int)data->value;
2831
2832 #if 0
2833                         if (but->type == SLI) {
2834                                 f = (float)(mx - but->x1) / (but->x2 - but->x1); /* same as below */
2835                         }
2836                         else
2837 #endif
2838                         {
2839                                 f = (float)(mx - but->x1) / (but->x2 - but->x1);
2840                         }
2841                         
2842                         f = softmin + f * softrange;
2843                         
2844                         if (!ui_is_but_float(but)) {
2845                                 if (f < temp) temp--;
2846                                 else temp++;
2847                                 
2848                                 if (temp >= softmin && temp <= softmax)
2849                                         data->value = temp;
2850                                 else
2851                                         data->cancel = 1;
2852                         } 
2853                         else {
2854                                 if (f < tempf) tempf -= 0.01f;
2855                                 else tempf += 0.01f;
2856                                 
2857                                 if (tempf >= softmin && tempf <= softmax)
2858                                         data->value = tempf;
2859                                 else
2860                                         data->cancel = 1;
2861                         }
2862                         
2863                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2864                         retval = WM_UI_HANDLER_BREAK;
2865                 }
2866                 else {
2867                         /* edit the value directly */
2868                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2869                         retval = WM_UI_HANDLER_BREAK;
2870                 }
2871         }
2872         
2873         return retval;
2874 }
2875
2876 static int ui_do_but_SCROLL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2877 {
2878         int mx, my /*, click= 0 */;
2879         int retval = WM_UI_HANDLER_CONTINUE;
2880         int horizontal = (but->x2 - but->x1 > but->y2 - but->y1);
2881         
2882         mx = event->x;
2883         my = event->y;
2884         ui_window_to_block(data->region, block, &mx, &my);
2885
2886         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2887                 if (event->val == KM_PRESS) {
2888                         if (event->type == LEFTMOUSE) {
2889                                 if (horizontal) {
2890                                         data->dragstartx = mx;
2891                                         data->draglastx = mx;
2892                                 }
2893                                 else {
2894                                         data->dragstartx = my;
2895                                         data->draglastx = my;
2896                                 }
2897                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2898                                 retval = WM_UI_HANDLER_BREAK;
2899                         }
2900                         /* UNUSED - otherwise code is ok, add back if needed */
2901 #if 0
2902                         else if (ELEM(event->type, PADENTER, RETKEY) && event->val == KM_PRESS)
2903                                 click = 1;
2904 #endif
2905                 }
2906         }
2907         else if (data->state == BUTTON_STATE_NUM_EDITING) {
2908                 if (event->type == ESCKEY) {
2909                         data->cancel = 1;
2910                         data->escapecancel = 1;
2911                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2912                 }
2913                 else if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
2914                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2915                 }
2916                 else if (event->type == MOUSEMOVE) {
2917                         if (ui_numedit_but_SLI(but, data, 0, 0, (horizontal) ? mx : my))
2918                                 ui_numedit_apply(C, block, but, data);
2919                 }
2920
2921                 retval = WM_UI_HANDLER_BREAK;
2922         }
2923         
2924         return retval;
2925 }
2926
2927
2928 static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2929 {
2930         
2931         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2932                 
2933                 /* first handle click on icondrag type button */
2934                 if (event->type == LEFTMOUSE && but->dragpoin && event->val == KM_PRESS) {
2935                         if (ui_but_mouse_inside_icon(but, data->region, event)) {
2936                                 button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
2937                                 data->dragstartx = event->x;
2938                                 data->dragstarty = event->y;
2939                                 return WM_UI_HANDLER_BREAK;
2940                         }
2941                 }
2942                 
2943                 /* regular open menu */
2944                 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2945                         button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
2946                         return WM_UI_HANDLER_BREAK;
2947                 }
2948                 else if (ELEM3(but->type, MENU, ICONROW, ICONTEXTROW)) {
2949                         
2950                         if (event->type == WHEELDOWNMOUSE && event->alt) {
2951                                 data->value = ui_step_name_menu(but, -1);
2952                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2953                                 ui_apply_button(C, but->block, but, data, 1);
2954
2955                                 /* button's state need to be changed to EXIT so moving mouse away from this mouse wouldn't lead
2956                                  * to cancel changes made to this button, but changing state to EXIT also makes no button active for
2957                                  * a while which leads to triggering operator when doing fast scrolling mouse wheel.
2958                                  * using post activate stuff from button allows to make button be active again after checking for all
2959                                  * all that mouse leave and cancel stuff, so quick scroll wouldn't be an issue anymore.
2960             &