macros for rectangle center and size
[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         BLI_rcti_rctf_copy(&rect, &but->rect);
690         
691         if (but->imb) ;  /* use button size itself */
692         else if (but->flag & UI_ICON_LEFT) {
693                 rect.xmax = rect.xmin + (rect.ymax - rect.ymin);
694         }
695         else {
696                 int delta = BLI_RCT_SIZE_X(&rect) - BLI_RCT_SIZE_Y(&rect);
697                 rect.xmin += delta / 2;
698                 rect.xmax -= delta / 2;
699         }
700         
701         return BLI_in_rcti(&rect, x, y);
702 }
703
704 static int ui_but_start_drag(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
705 {
706         /* prevent other WM gestures to start while we try to drag */
707         WM_gestures_remove(C);
708
709         if (ABS(data->dragstartx - event->x) + ABS(data->dragstarty - event->y) > U.dragthreshold) {
710                 wmDrag *drag;
711                 
712                 button_activate_state(C, but, BUTTON_STATE_EXIT);
713                 data->cancel = 1;
714                 
715                 drag = WM_event_start_drag(C, but->icon, but->dragtype, but->dragpoin, ui_get_but_val(but));
716                 if (but->imb)
717                         WM_event_drag_image(drag, but->imb, but->imb_scale, but->rect.xmax - but->rect.xmin, but->rect.ymax - but->rect.ymin);
718                 return 1;
719         }
720         
721         return 0;
722 }
723
724 /* ********************** linklines *********************** */
725
726 static void ui_delete_active_linkline(uiBlock *block)
727 {
728         uiBut *but;
729         uiLink *link;
730         uiLinkLine *line, *nline;
731         int a, b;
732         
733         but = block->buttons.first;
734         while (but) {
735                 if (but->type == LINK && but->link) {
736                         line = but->link->lines.first;
737                         while (line) {
738                                 
739                                 nline = line->next;
740                                 
741                                 if (line->flag & UI_SELECT) {
742                                         BLI_remlink(&but->link->lines, line);
743                                         
744                                         link = line->from->link;
745                                         
746                                         /* are there more pointers allowed? */
747                                         if (link->ppoin) {
748                                                 
749                                                 if (*(link->totlink) == 1) {
750                                                         *(link->totlink) = 0;
751                                                         MEM_freeN(*(link->ppoin));
752                                                         *(link->ppoin) = NULL;
753                                                 }
754                                                 else {
755                                                         b = 0;
756                                                         for (a = 0; a < (*(link->totlink)); a++) {
757                                                                 
758                                                                 if ( (*(link->ppoin))[a] != line->to->poin) {
759                                                                         (*(link->ppoin))[b] = (*(link->ppoin))[a];
760                                                                         b++;
761                                                                 }
762                                                         }       
763                                                         (*(link->totlink))--;
764                                                 }
765                                         }
766                                         else {
767                                                 *(link->poin) = NULL;
768                                         }
769                                         
770                                         MEM_freeN(line);
771                                 }
772                                 line = nline;
773                         }
774                 }
775                 but = but->next;
776         }
777 }
778
779
780 static uiLinkLine *ui_is_a_link(uiBut *from, uiBut *to)
781 {
782         uiLinkLine *line;
783         uiLink *link;
784         
785         link = from->link;
786         if (link) {
787                 line = link->lines.first;
788                 while (line) {
789                         if (line->from == from && line->to == to) return line;
790                         line = line->next;
791                 }
792         }
793         return NULL;
794 }
795
796 /* XXX BAD BAD HACK, fixme later **************** */
797 /* Try to add an AND Controller between the sensor and the actuator logic bricks and to connect them all */
798 static void ui_add_smart_controller(bContext *C, uiBut *from, uiBut *to)
799 {
800         Object *ob = NULL;
801         bSensor *sens_iter;
802         bActuator *act_to, *act_iter;
803         bController *cont;
804         bController ***sens_from_links;
805         uiBut *tmp_but;
806
807         uiLink *link = from->link;
808
809         if (link->ppoin)
810                 sens_from_links = (bController ***)(link->ppoin);
811         else return;
812
813         act_to = (bActuator *)(to->poin);
814
815         /* (1) get the object */
816         CTX_DATA_BEGIN (C, Object *, ob_iter, selected_editable_objects)
817         {
818                 for (sens_iter = ob_iter->sensors.first; sens_iter; sens_iter = sens_iter->next) {
819                         if (&(sens_iter->links) == sens_from_links) {
820                                 ob = ob_iter;
821                                 break;
822                         }
823                 }
824                 if (ob) break;
825         } CTX_DATA_END;
826
827         if (!ob) return;
828
829         /* (2) check if the sensor and the actuator are from the same object */
830         for (act_iter = ob->actuators.first; act_iter; act_iter = (bActuator *)act_iter->next) {
831                 if (act_iter == act_to)
832                         break;
833         }
834
835         /* only works if the sensor and the actuator are from the same object */
836         if (!act_iter) return;
837
838         /* (3) add a new controller */
839         if (WM_operator_name_call(C, "LOGIC_OT_controller_add", WM_OP_EXEC_DEFAULT, NULL) & OPERATOR_FINISHED) {
840                 cont = (bController *)ob->controllers.last;
841
842                 /* (4) link the sensor->controller->actuator */
843                 tmp_but = MEM_callocN(sizeof(uiBut), "uiBut");
844                 uiSetButLink(tmp_but, (void **)&cont, (void ***)&(cont->links), &(cont->totlinks), from->link->tocode, (int)to->hardmin);
845                 tmp_but->hardmin = from->link->tocode;
846                 tmp_but->poin = (char *)cont;
847
848                 tmp_but->type = INLINK;
849                 ui_add_link(C, from, tmp_but);
850
851                 tmp_but->type = LINK;
852                 ui_add_link(C, tmp_but, to);
853
854                 /* (5) garbage collection */
855                 MEM_freeN(tmp_but->link);
856                 MEM_freeN(tmp_but);
857         }
858 }
859
860 static void ui_add_link(bContext *C, uiBut *from, uiBut *to)
861 {
862         /* in 'from' we have to add a link to 'to' */
863         uiLink *link;
864         uiLinkLine *line;
865         void **oldppoin;
866         int a;
867         
868         if ( (line = ui_is_a_link(from, to)) ) {
869                 line->flag |= UI_SELECT;
870                 ui_delete_active_linkline(from->block);
871                 return;
872         }
873
874         if (from->type == INLINK && to->type == INLINK) {
875                 return;
876         }
877         else if (from->type == LINK && to->type == INLINK) {
878                 if (from->link->tocode != (int)to->hardmin) {
879                         ui_add_smart_controller(C, from, to);
880                         return;
881                 }
882         }
883         else if (from->type == INLINK && to->type == LINK) {
884                 if (to->link->tocode == (int)from->hardmin) {
885                         return;
886                 }
887         }
888         
889         link = from->link;
890         
891         /* are there more pointers allowed? */
892         if (link->ppoin) {
893                 oldppoin = *(link->ppoin);
894                 
895                 (*(link->totlink))++;
896                 *(link->ppoin) = MEM_callocN(*(link->totlink) * sizeof(void *), "new link");
897                 
898                 for (a = 0; a < (*(link->totlink)) - 1; a++) {
899                         (*(link->ppoin))[a] = oldppoin[a];
900                 }
901                 (*(link->ppoin))[a] = to->poin;
902                 
903                 if (oldppoin) MEM_freeN(oldppoin);
904         }
905         else {
906                 *(link->poin) = to->poin;
907         }
908         
909 }
910
911
912 static void ui_apply_but_LINK(bContext *C, uiBut *but, uiHandleButtonData *data)
913 {
914         ARegion *ar = CTX_wm_region(C);
915         uiBut *bt;
916         
917         for (bt = but->block->buttons.first; bt; bt = bt->next) {
918                 if (ui_mouse_inside_button(ar, bt, but->linkto[0] + ar->winrct.xmin, but->linkto[1] + ar->winrct.ymin) )
919                         break;
920         }
921         if (bt && bt != but) {
922                 if (!ELEM(bt->type, LINK, INLINK) || !ELEM(but->type, LINK, INLINK))
923                         return;
924                 
925                 if (but->type == LINK) ui_add_link(C, but, bt);
926                 else ui_add_link(C, bt, but);
927
928                 ui_apply_but_func(C, but);
929                 data->retval = but->retval;
930         }
931         data->applied = 1;
932 }
933
934 static void ui_apply_but_IMAGE(bContext *C, uiBut *but, uiHandleButtonData *data)
935 {
936         ui_apply_but_func(C, but);
937         data->retval = but->retval;
938         data->applied = 1;
939 }
940
941 static void ui_apply_but_HISTOGRAM(bContext *C, uiBut *but, uiHandleButtonData *data)
942 {
943         ui_apply_but_func(C, but);
944         data->retval = but->retval;
945         data->applied = 1;
946 }
947
948 static void ui_apply_but_WAVEFORM(bContext *C, uiBut *but, uiHandleButtonData *data)
949 {
950         ui_apply_but_func(C, but);
951         data->retval = but->retval;
952         data->applied = 1;
953 }
954
955 static void ui_apply_but_TRACKPREVIEW(bContext *C, uiBut *but, uiHandleButtonData *data)
956 {
957         ui_apply_but_func(C, but);
958         data->retval = but->retval;
959         data->applied = 1;
960 }
961
962
963 static void ui_apply_button(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, int interactive)
964 {
965         char *editstr;
966         double *editval;
967         float *editvec;
968         ColorBand *editcoba;
969         CurveMapping *editcumap;
970
971         data->retval = 0;
972
973         /* if we cancel and have not applied yet, there is nothing to do,
974          * otherwise we have to restore the original value again */
975         if (data->cancel) {
976                 if (!data->applied)
977                         return;
978
979                 if (data->str) MEM_freeN(data->str);
980                 data->str = data->origstr;
981                 data->origstr = NULL;
982                 data->value = data->origvalue;
983                 data->origvalue = 0.0;
984                 copy_v3_v3(data->vec, data->origvec);
985                 data->origvec[0] = data->origvec[1] = data->origvec[2] = 0.0f;
986         }
987         else {
988                 /* we avoid applying interactive edits a second time
989                  * at the end with the appliedinteractive flag */
990                 if (interactive)
991                         data->appliedinteractive = 1;
992                 else if (data->appliedinteractive)
993                         return;
994         }
995
996         /* ensures we are writing actual values */
997         editstr = but->editstr;
998         editval = but->editval;
999         editvec = but->editvec;
1000         editcoba = but->editcoba;
1001         editcumap = but->editcumap;
1002         but->editstr = NULL;
1003         but->editval = NULL;
1004         but->editvec = NULL;
1005         but->editcoba = NULL;
1006         but->editcumap = NULL;
1007
1008         /* handle different types */
1009         switch (but->type) {
1010                 case BUT:
1011                         ui_apply_but_BUT(C, but, data);
1012                         break;
1013                 case TEX:
1014                 case SEARCH_MENU:
1015                         ui_apply_but_TEX(C, but, data);
1016                         break;
1017                 case TOGBUT: 
1018                 case TOG: 
1019                 case TOGR: 
1020                 case ICONTOG:
1021                 case ICONTOGN:
1022                 case TOGN:
1023                 case BUT_TOGDUAL:
1024                 case OPTION:
1025                 case OPTIONN:
1026                         ui_apply_but_TOG(C, but, data);
1027                         break;
1028                 case ROW:
1029                 case LISTROW:
1030                         ui_apply_but_ROW(C, block, but, data);
1031                         break;
1032                 case SCROLL:
1033                 case NUM:
1034                 case NUMABS:
1035                 case SLI:
1036                 case NUMSLI:
1037                         ui_apply_but_NUM(C, but, data);
1038                         break;
1039                 case HSVSLI:
1040                         break;
1041                 case TOG3:      
1042                         ui_apply_but_TOG3(C, but, data);
1043                         break;
1044                 case MENU:
1045                 case ICONROW:
1046                 case ICONTEXTROW:
1047                 case BLOCK:
1048                 case PULLDOWN:
1049                         ui_apply_but_BLOCK(C, but, data);
1050                         break;
1051                 case COL:
1052                         if (data->cancel)
1053                                 ui_apply_but_VEC(C, but, data);
1054                         else
1055                                 ui_apply_but_BLOCK(C, but, data);
1056                         break;
1057                 case BUTM:
1058                         ui_apply_but_BUTM(C, but, data);
1059                         break;
1060                 case BUT_NORMAL:
1061                 case HSVCUBE:
1062                 case HSVCIRCLE:
1063                         ui_apply_but_VEC(C, but, data);
1064                         break;
1065                 case BUT_COLORBAND:
1066                         ui_apply_but_COLORBAND(C, but, data);
1067                         break;
1068                 case BUT_CURVE:
1069                         ui_apply_but_CURVE(C, but, data);
1070                         break;
1071                 case IDPOIN:
1072                         ui_apply_but_IDPOIN(C, but, data);
1073                         break;
1074 #ifdef WITH_INTERNATIONAL
1075                 case CHARTAB:
1076                         ui_apply_but_CHARTAB(C, but, data);
1077                         break;
1078 #endif
1079                 case KEYEVT:
1080                 case HOTKEYEVT:
1081                         ui_apply_but_BUT(C, but, data);
1082                         break;
1083                 case LINK:
1084                 case INLINK:
1085                         ui_apply_but_LINK(C, but, data);
1086                         break;
1087                 case BUT_IMAGE: 
1088                         ui_apply_but_IMAGE(C, but, data);
1089                         break;
1090                 case HISTOGRAM: 
1091                         ui_apply_but_HISTOGRAM(C, but, data);
1092                         break;
1093                 case WAVEFORM:
1094                         ui_apply_but_WAVEFORM(C, but, data);
1095                         break;
1096                 case TRACKPREVIEW:
1097                         ui_apply_but_TRACKPREVIEW(C, but, data);
1098                         break;
1099                 default:
1100                         break;
1101         }
1102
1103         but->editstr = editstr;
1104         but->editval = editval;
1105         but->editvec = editvec;
1106         but->editcoba = editcoba;
1107         but->editcumap = editcumap;
1108 }
1109
1110 /* ******************* drop event ********************  */
1111
1112 /* only call if event type is EVT_DROP */
1113 static void ui_but_drop(bContext *C, wmEvent *event, uiBut *but, uiHandleButtonData *data)
1114 {
1115         wmDrag *wmd;
1116         ListBase *drags = event->customdata; /* drop event type has listbase customdata by default */
1117         
1118         for (wmd = drags->first; wmd; wmd = wmd->next) {
1119                 if (wmd->type == WM_DRAG_ID) {
1120                         /* align these types with UI_but_active_drop_name */
1121                         if (ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1122                                 ID *id = (ID *)wmd->poin;
1123                                 
1124                                 if (but->poin == NULL && but->rnapoin.data == NULL) {}
1125                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1126                                 BLI_strncpy(data->str, id->name + 2, data->maxlen);
1127                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1128                         }
1129                 }
1130         }
1131         
1132 }
1133
1134 /* ******************* copy and paste ********************  */
1135
1136 /* c = copy, v = paste */
1137 static void ui_but_copy_paste(bContext *C, uiBut *but, uiHandleButtonData *data, char mode)
1138 {
1139         static ColorBand but_copypaste_coba = {0};
1140         char buf[UI_MAX_DRAW_STR + 1] = {0};
1141
1142         if (mode == 'v' && but->lock)
1143                 return;
1144
1145         if (mode == 'v') {
1146                 /* extract first line from clipboard in case of multi-line copies */
1147                 char *p, *pbuf = WM_clipboard_text_get(0);
1148                 p = pbuf;
1149                 if (p) {
1150                         int i = 0;
1151                         while (*p && *p != '\r' && *p != '\n' && i < UI_MAX_DRAW_STR) {
1152                                 buf[i++] = *p;
1153                                 p++;
1154                         }
1155                         buf[i] = 0;
1156                         MEM_freeN(pbuf);
1157                 }
1158         }
1159         
1160         /* numeric value */
1161         if (ELEM4(but->type, NUM, NUMABS, NUMSLI, HSVSLI)) {
1162                 
1163                 if (but->poin == NULL && but->rnapoin.data == NULL) ;
1164                 else if (mode == 'c') {
1165                         ui_get_but_string(but, buf, sizeof(buf));
1166                         WM_clipboard_text_set(buf, 0);
1167                 }
1168                 else {
1169                         double val;
1170
1171                         if (ui_set_but_string_eval_num(C, but, buf, &val)) {
1172                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1173                                 data->value = val;
1174                                 ui_set_but_string(C, but, buf);
1175                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1176                         }
1177                 }
1178         }
1179
1180         /* RGB triple */
1181         else if (but->type == COL) {
1182                 float rgb[3];
1183                 
1184                 if (but->poin == NULL && but->rnapoin.data == NULL) ;
1185                 else if (mode == 'c') {
1186
1187                         ui_get_but_vectorf(but, rgb);
1188                         BLI_snprintf(buf, sizeof(buf), "[%f, %f, %f]", rgb[0], rgb[1], rgb[2]);
1189                         WM_clipboard_text_set(buf, 0);
1190                         
1191                 }
1192                 else {
1193                         if (sscanf(buf, "[%f, %f, %f]", &rgb[0], &rgb[1], &rgb[2]) == 3) {
1194                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1195                                 ui_set_but_vectorf(but, rgb);
1196                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1197                         }
1198                 }
1199         }
1200
1201         /* text/string and ID data */
1202         else if (ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1203                 uiHandleButtonData *active_data = but->active;
1204
1205                 if (but->poin == NULL && but->rnapoin.data == NULL) ;
1206                 else if (mode == 'c') {
1207                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1208                         BLI_strncpy(buf, active_data->str, UI_MAX_DRAW_STR);
1209                         WM_clipboard_text_set(active_data->str, 0);
1210                         active_data->cancel = 1;
1211                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1212                 }
1213                 else {
1214                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1215
1216                         if (ui_is_but_utf8(but)) BLI_strncpy_utf8(active_data->str, buf, active_data->maxlen);
1217                         else BLI_strncpy(active_data->str, buf, active_data->maxlen);
1218
1219                         if (but->type == SEARCH_MENU) {
1220                                 /* else uiSearchboxData.active member is not updated [#26856] */
1221                                 ui_searchbox_update(C, data->searchbox, but, 1);
1222                         }
1223                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1224                 }
1225         }
1226         /* colorband (not supported by system clipboard) */
1227         else if (but->type == BUT_COLORBAND) {
1228                 if (mode == 'c') {
1229                         if (but->poin == NULL)
1230                                 return;
1231
1232                         memcpy(&but_copypaste_coba, but->poin, sizeof(ColorBand));
1233                 }
1234                 else {
1235                         if (but_copypaste_coba.tot == 0)
1236                                 return;
1237
1238                         if (!but->poin)
1239                                 but->poin = MEM_callocN(sizeof(ColorBand), "colorband");
1240
1241                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1242                         memcpy(data->coba, &but_copypaste_coba, sizeof(ColorBand));
1243                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1244                 }
1245         }
1246         /* operator button (any type) */
1247         else if (but->optype) {
1248                 if (mode == 'c') {
1249                         PointerRNA *opptr;
1250                         char *str;
1251                         opptr = uiButGetOperatorPtrRNA(but); /* allocated when needed, the button owns it */
1252
1253                         str = WM_operator_pystring(C, but->optype, opptr, 0);
1254
1255                         WM_clipboard_text_set(str, 0);
1256
1257                         MEM_freeN(str);
1258                 }
1259         }
1260 }
1261
1262 /* ************* in-button text selection/editing ************* */
1263
1264
1265 static int ui_textedit_delete_selection(uiBut *but, uiHandleButtonData *data)
1266 {
1267         char *str = data->str;
1268         int len = strlen(str);
1269         int change = 0;
1270         if (but->selsta != but->selend && len) {
1271                 memmove(str + but->selsta, str + but->selend, (len - but->selend) + 1);
1272                 change = 1;
1273         }
1274         
1275         but->pos = but->selend = but->selsta;
1276         return change;
1277 }
1278
1279 /* note, but->block->aspect is used here, when drawing button style is getting scaled too */
1280 static void ui_textedit_set_cursor_pos(uiBut *but, uiHandleButtonData *data, short x)
1281 {
1282         uiStyle *style = UI_GetStyle();  // XXX pass on as arg
1283         uiFontStyle *fstyle = &style->widget;
1284         int startx = but->rect.xmin;
1285         char *origstr;
1286
1287         uiStyleFontSet(fstyle);
1288
1289         if (fstyle->kerning == 1) /* for BLF_width */
1290                 BLF_enable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
1291         
1292         origstr = MEM_callocN(sizeof(char) * data->maxlen, "ui_textedit origstr");
1293         
1294         BLI_strncpy(origstr, but->drawstr, data->maxlen);
1295         
1296         /* XXX solve generic */
1297         if (but->type == NUM || but->type == NUMSLI)
1298                 startx += (int)(0.5f * (but->rect.ymax - but->rect.ymin));
1299         else if (ELEM(but->type, TEX, SEARCH_MENU)) {
1300                 startx += 5;
1301                 if (but->flag & UI_HAS_ICON)
1302                         startx += UI_DPI_ICON_SIZE;
1303         }
1304         
1305         /* mouse dragged outside the widget to the left */
1306         if (x < startx && but->ofs > 0) {       
1307                 int i = but->ofs;
1308
1309                 origstr[but->ofs] = 0;
1310                 
1311                 while (i > 0) {
1312                         if (BLI_str_cursor_step_prev_utf8(origstr, but->ofs, &i)) {
1313                                 if (BLF_width(fstyle->uifont_id, origstr + i) > (startx - x) * 0.25f) break;  // 0.25 == scale factor for less sensitivity
1314                         }
1315                         else {
1316                                 break; /* unlikely but possible */
1317                         }
1318                 }
1319                 but->ofs = i;
1320                 but->pos = but->ofs;
1321         }
1322         /* mouse inside the widget */
1323         else if (x >= startx) {
1324                 int pos_i;
1325
1326                 /* keep track of previous distance from the cursor to the char */
1327                 float cdist, cdist_prev = 0.0f;
1328                 short pos_prev;
1329
1330                 const float aspect_sqrt = sqrtf(but->block->aspect);
1331                 
1332                 but->pos = pos_prev = strlen(origstr) - but->ofs;
1333
1334                 while (TRUE) {
1335                         /* XXX does not take zoom level into account */
1336                         cdist = startx + aspect_sqrt *BLF_width(fstyle->uifont_id, origstr + but->ofs);
1337
1338                         /* check if position is found */
1339                         if (cdist < x) {
1340                                 /* check is previous location was in fact closer */
1341                                 if (((float)x - cdist) > (cdist_prev - (float)x)) {
1342                                         but->pos = pos_prev;
1343                                 }
1344                                 break;
1345                         }
1346                         cdist_prev = cdist;
1347                         pos_prev   = but->pos;
1348                         /* done with tricky distance checks */
1349
1350                         pos_i = but->pos;
1351                         if (but->pos <= 0) break;
1352                         if (BLI_str_cursor_step_prev_utf8(origstr, but->ofs, &pos_i)) {
1353                                 but->pos = pos_i;
1354                                 origstr[but->pos + but->ofs] = 0;
1355                         }
1356                         else {
1357                                 break; /* unlikely but possible */
1358                         }
1359                 }
1360                 but->pos += but->ofs;
1361                 if (but->pos < 0) but->pos = 0;
1362         }
1363         
1364         if (fstyle->kerning == 1)
1365                 BLF_disable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
1366         
1367         MEM_freeN(origstr);
1368 }
1369
1370 static void ui_textedit_set_cursor_select(uiBut *but, uiHandleButtonData *data, short x)
1371 {
1372         if (x > data->selstartx) data->selextend = EXTEND_RIGHT;
1373         else if (x < data->selstartx) data->selextend = EXTEND_LEFT;
1374
1375         ui_textedit_set_cursor_pos(but, data, x);
1376                                                 
1377         if (data->selextend == EXTEND_RIGHT) but->selend = but->pos;
1378         if (data->selextend == EXTEND_LEFT) but->selsta = but->pos;
1379
1380         ui_check_but(but);
1381 }
1382
1383 /* this is used for both utf8 and ascii, its meant to be used for single keys,
1384  * notice the buffer is either copied or not, so its not suitable for pasting in
1385  * - campbell */
1386 static int ui_textedit_type_buf(uiBut *but, uiHandleButtonData *data,
1387                                 const char *utf8_buf, int utf8_buf_len)
1388 {
1389         char *str;
1390         int len, changed = 0;
1391
1392         str = data->str;
1393         len = strlen(str);
1394
1395         if (len - (but->selend - but->selsta) + 1 <= data->maxlen) {
1396                 int step = utf8_buf_len;
1397
1398                 /* type over the current selection */
1399                 if ((but->selend - but->selsta) > 0) {
1400                         changed = ui_textedit_delete_selection(but, data);
1401                         len = strlen(str);
1402                 }
1403
1404                 if (len + step < data->maxlen) {
1405                         memmove(&str[but->pos + step], &str[but->pos], (len + 1) - but->pos);
1406                         memcpy(&str[but->pos], utf8_buf, step * sizeof(char));
1407                         but->pos += step;
1408                         changed = 1;
1409                 }
1410         }
1411
1412         return changed;
1413 }
1414
1415 static int ui_textedit_type_ascii(uiBut *but, uiHandleButtonData *data, char ascii)
1416 {
1417         char buf[2] = {ascii, '\0'};
1418
1419         if (ui_is_but_utf8(but) && (BLI_str_utf8_size(buf) == -1)) {
1420                 printf("%s: entering invalid ascii char into an ascii key (%d)\n",
1421                        __func__, (int)(unsigned char)ascii);
1422
1423                 return 0;
1424         }
1425
1426         /* in some cases we want to allow invalid utf8 chars */
1427         return ui_textedit_type_buf(but, data, buf, 1);
1428 }
1429
1430 static void ui_textedit_move(uiBut *but, uiHandleButtonData *data, strCursorJumpDirection direction, int select, strCursorJumpType jump)
1431 {
1432         const char *str = data->str;
1433         const int len = strlen(str);
1434         const int pos_prev = but->pos;
1435         const int has_sel = (but->selend - but->selsta) > 0;
1436
1437         ui_check_but(but);
1438
1439         /* special case, quit selection and set cursor */
1440         if (has_sel && !select) {
1441                 if (jump == STRCUR_JUMP_ALL) {
1442                         but->selsta = but->selend = but->pos = direction ? len : 0;
1443                 }
1444                 else {
1445                         if (direction) {
1446                                 but->selsta = but->pos = but->selend;
1447                         }
1448                         else {
1449                                 but->pos = but->selend = but->selsta;
1450                         }
1451                 }
1452                 data->selextend = 0;
1453         }
1454         else {
1455                 int pos_i = but->pos;
1456                 BLI_str_cursor_step_utf8(str, len, &pos_i, direction, jump);
1457                 but->pos = pos_i;
1458
1459                 if (select) {
1460                         /* existing selection */
1461                         if (has_sel) {
1462
1463                                 if (data->selextend == 0) {
1464                                         data->selextend = EXTEND_RIGHT;
1465                                 }
1466
1467                                 if (direction) {
1468                                         if (data->selextend == EXTEND_RIGHT) {
1469                                                 but->selend = but->pos;
1470                                         }
1471                                         else {
1472                                                 but->selsta = but->pos;
1473                                         }
1474                                 }
1475                                 else {
1476                                         if (data->selextend == EXTEND_LEFT) {
1477                                                 but->selsta = but->pos;
1478                                         }
1479                                         else {
1480                                                 but->selend = but->pos;
1481                                         }
1482                                 }
1483
1484                                 if (but->selend < but->selsta) {
1485                                         SWAP(short, but->selsta, but->selend);
1486                                         data->selextend = (data->selextend == EXTEND_RIGHT) ? EXTEND_LEFT : EXTEND_RIGHT;
1487                                 }
1488
1489                         } /* new selection */
1490                         else {
1491                                 if (direction) {
1492                                         data->selextend = EXTEND_RIGHT;
1493                                         but->selend = but->pos;
1494                                         but->selsta = pos_prev;
1495                                 }
1496                                 else {
1497                                         data->selextend = EXTEND_LEFT;
1498                                         but->selend = pos_prev;
1499                                         but->selsta = but->pos;
1500                                 }
1501                         }
1502                 }
1503         }
1504 }
1505
1506 static int ui_textedit_delete(uiBut *but, uiHandleButtonData *data, int direction, strCursorJumpType jump)
1507 {
1508         char *str = data->str;
1509         const int len = strlen(str);
1510
1511         int changed = 0;
1512
1513         if (jump == STRCUR_JUMP_ALL) {
1514                 if (len) changed = 1;
1515                 str[0] = '\0';
1516                 but->pos = 0;
1517         }
1518         else if (direction) { /* delete */
1519                 if ((but->selend - but->selsta) > 0) {
1520                         changed = ui_textedit_delete_selection(but, data);
1521                 }
1522                 else if (but->pos >= 0 && but->pos < len) {
1523                         int pos = but->pos;
1524                         int step;
1525                         BLI_str_cursor_step_utf8(str, len, &pos, direction, jump);
1526                         step = pos - but->pos;
1527                         memmove(&str[but->pos], &str[but->pos + step], (len + 1) - but->pos);
1528                         changed = 1;
1529                 }
1530         }
1531         else { /* backspace */
1532                 if (len != 0) {
1533                         if ((but->selend - but->selsta) > 0) {
1534                                 changed = ui_textedit_delete_selection(but, data);
1535                         }
1536                         else if (but->pos > 0) {
1537                                 int pos = but->pos;
1538                                 int step;
1539
1540                                 BLI_str_cursor_step_utf8(str, len, &pos, direction, jump);
1541                                 step = but->pos - pos;
1542                                 memmove(&str[but->pos - step], &str[but->pos], (len + 1) - but->pos);
1543                                 but->pos -= step;
1544                                 changed = 1;
1545                         }
1546                 } 
1547         }
1548
1549         return changed;
1550 }
1551
1552 static int ui_textedit_autocomplete(bContext *C, uiBut *but, uiHandleButtonData *data)
1553 {
1554         char *str;
1555         int changed = 1;
1556
1557         str = data->str;
1558
1559         if (data->searchbox)
1560                 ui_searchbox_autocomplete(C, data->searchbox, but, data->str);
1561         else
1562                 but->autocomplete_func(C, str, but->autofunc_arg);
1563
1564         but->pos = strlen(str);
1565         but->selsta = but->selend = but->pos;
1566
1567         return changed;
1568 }
1569
1570 static int ui_textedit_copypaste(uiBut *but, uiHandleButtonData *data, int paste, int copy, int cut)
1571 {
1572         char buf[UI_MAX_DRAW_STR] = {0};
1573         char *str, *p, *pbuf;
1574         int len, x, i, changed = 0;
1575
1576         str = data->str;
1577         len = strlen(str);
1578         
1579         /* paste */
1580         if (paste) {
1581                 /* extract the first line from the clipboard */
1582                 p = pbuf = WM_clipboard_text_get(0);
1583
1584                 if (p && p[0]) {
1585                         unsigned int y;
1586                         i = 0;
1587                         while (*p && *p != '\r' && *p != '\n' && i < UI_MAX_DRAW_STR - 1) {
1588                                 buf[i++] = *p;
1589                                 p++;
1590                         }
1591                         buf[i] = 0;
1592
1593                         /* paste over the current selection */
1594                         if ((but->selend - but->selsta) > 0) {
1595                                 ui_textedit_delete_selection(but, data);
1596                                 len = strlen(str);
1597                         }
1598                         
1599                         for (y = 0; y < strlen(buf); y++) {
1600                                 /* add contents of buffer */
1601                                 if (len + 1 < data->maxlen) {
1602                                         for (x = data->maxlen; x > but->pos; x--)
1603                                                 str[x] = str[x - 1];
1604                                         str[but->pos] = buf[y];
1605                                         but->pos++; 
1606                                         len++;
1607                                         str[len] = '\0';
1608                                 }
1609                         }
1610
1611                         changed = 1;
1612                 }
1613
1614                 if (pbuf) {
1615                         MEM_freeN(pbuf);
1616                 }
1617         }
1618         /* cut & copy */
1619         else if (copy || cut) {
1620                 /* copy the contents to the copypaste buffer */
1621                 for (x = but->selsta; x <= but->selend; x++) {
1622                         if (x == but->selend)
1623                                 buf[x] = '\0';
1624                         else
1625                                 buf[(x - but->selsta)] = str[x];
1626                 }
1627
1628                 WM_clipboard_text_set(buf, 0);
1629                 
1630                 /* for cut only, delete the selection afterwards */
1631                 if (cut)
1632                         if ((but->selend - but->selsta) > 0)
1633                                 changed = ui_textedit_delete_selection(but, data);
1634         } 
1635
1636         return changed;
1637 }
1638
1639 static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
1640 {
1641         int len;
1642
1643         if (data->str) {
1644                 MEM_freeN(data->str);
1645                 data->str = NULL;
1646         }
1647
1648         /* retrieve string */
1649         data->maxlen = ui_get_but_string_max_length(but);
1650         data->str = MEM_callocN(sizeof(char) * data->maxlen + 1, "textedit str");
1651         ui_get_but_string(but, data->str, data->maxlen);
1652
1653         if (ELEM3(but->type, NUM, NUMABS, NUMSLI)) {
1654                 ui_convert_to_unit_alt_name(but, data->str, data->maxlen);
1655         }
1656
1657         /* won't change from now on */
1658         len = strlen(data->str);
1659
1660         data->origstr = BLI_strdupn(data->str, len);
1661         data->selextend = 0;
1662         data->selstartx = 0;
1663
1664         /* set cursor pos to the end of the text */
1665         but->editstr = data->str;
1666         but->pos = len;
1667         but->selsta = 0;
1668         but->selend = len;
1669
1670         /* optional searchbox */
1671         if (but->type == SEARCH_MENU) {
1672                 data->searchbox = ui_searchbox_create(C, data->region, but);
1673                 ui_searchbox_update(C, data->searchbox, but, 1); /* 1= reset */
1674         }
1675         
1676         ui_check_but(but);
1677         
1678         WM_cursor_modal(CTX_wm_window(C), BC_TEXTEDITCURSOR);
1679 }
1680
1681 static void ui_textedit_end(bContext *C, uiBut *but, uiHandleButtonData *data)
1682 {
1683         if (but) {
1684                 if (ui_is_but_utf8(but)) {
1685                         int strip = BLI_utf8_invalid_strip(but->editstr, strlen(but->editstr));
1686                         /* not a file?, strip non utf-8 chars */
1687                         if (strip) {
1688                                 /* wont happen often so isn't that annoying to keep it here for a while */
1689                                 printf("%s: invalid utf8 - stripped chars %d\n", __func__, strip);
1690                         }
1691                 }
1692                 
1693                 if (data->searchbox) {
1694                         if (data->cancel == 0)
1695                                 ui_searchbox_apply(but, data->searchbox);
1696
1697                         ui_searchbox_free(C, data->searchbox);
1698                         data->searchbox = NULL;
1699                 }
1700                 
1701                 but->editstr = NULL;
1702                 but->pos = -1;
1703         }
1704         
1705         WM_cursor_restore(CTX_wm_window(C));
1706 }
1707
1708 static void ui_textedit_next_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1709 {
1710         uiBut *but;
1711
1712         /* label and roundbox can overlap real buttons (backdrops...) */
1713         if (ELEM4(actbut->type, LABEL, SEPR, ROUNDBOX, LISTBOX))
1714                 return;
1715
1716         for (but = actbut->next; but; but = but->next) {
1717                 if (ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1718                         if (!(but->flag & UI_BUT_DISABLED)) {
1719                                 data->postbut = but;
1720                                 data->posttype = BUTTON_ACTIVATE_TEXT_EDITING;
1721                                 return;
1722                         }
1723                 }
1724         }
1725         for (but = block->buttons.first; but != actbut; but = but->next) {
1726                 if (ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1727                         if (!(but->flag & UI_BUT_DISABLED)) {
1728                                 data->postbut = but;
1729                                 data->posttype = BUTTON_ACTIVATE_TEXT_EDITING;
1730                                 return;
1731                         }
1732                 }
1733         }
1734 }
1735
1736 static void ui_textedit_prev_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1737 {
1738         uiBut *but;
1739
1740         /* label and roundbox can overlap real buttons (backdrops...) */
1741         if (ELEM4(actbut->type, LABEL, SEPR, ROUNDBOX, LISTBOX))
1742                 return;
1743
1744         for (but = actbut->prev; but; but = but->prev) {
1745                 if (ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1746                         if (!(but->flag & UI_BUT_DISABLED)) {
1747                                 data->postbut = but;
1748                                 data->posttype = BUTTON_ACTIVATE_TEXT_EDITING;
1749                                 return;
1750                         }
1751                 }
1752         }
1753         for (but = block->buttons.last; but != actbut; but = but->prev) {
1754                 if (ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1755                         if (!(but->flag & UI_BUT_DISABLED)) {
1756                                 data->postbut = but;
1757                                 data->posttype = BUTTON_ACTIVATE_TEXT_EDITING;
1758                                 return;
1759                         }
1760                 }
1761         }
1762 }
1763
1764
1765 static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1766 {
1767         int mx, my, changed = 0, inbox = 0, update = 0, retval = WM_UI_HANDLER_CONTINUE;
1768
1769         switch (event->type) {
1770                 case WHEELUPMOUSE:
1771                 case WHEELDOWNMOUSE:
1772                 case MOUSEMOVE:
1773                         if (data->searchbox)
1774                                 ui_searchbox_event(C, data->searchbox, but, event);
1775                         
1776                         break;
1777                 case RIGHTMOUSE:
1778                 case ESCKEY:
1779                         data->cancel = 1;
1780                         data->escapecancel = 1;
1781                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1782                         retval = WM_UI_HANDLER_BREAK;
1783                         break;
1784                 case LEFTMOUSE: {
1785                         
1786                         /* exit on LMB only on RELEASE for searchbox, to mimic other popups, and allow multiple menu levels */
1787                         if (data->searchbox)
1788                                 inbox = ui_searchbox_inside(data->searchbox, event->x, event->y);
1789
1790                         if (event->val == KM_PRESS) {
1791                                 mx = event->x;
1792                                 my = event->y;
1793                                 ui_window_to_block(data->region, block, &mx, &my);
1794
1795                                 if (ui_but_contains_pt(but, mx, my)) {
1796                                         ui_textedit_set_cursor_pos(but, data, mx);
1797                                         but->selsta = but->selend = but->pos;
1798                                         data->selstartx = mx;
1799
1800                                         button_activate_state(C, but, BUTTON_STATE_TEXT_SELECTING);
1801                                         retval = WM_UI_HANDLER_BREAK;
1802                                 }
1803                                 else if (inbox == 0) {
1804                                         /* if searchbox, click outside will cancel */
1805                                         if (data->searchbox)
1806                                                 data->cancel = data->escapecancel = 1;
1807                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1808                                         retval = WM_UI_HANDLER_BREAK;
1809                                 }
1810                         }
1811                         else if (inbox) {
1812                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1813                                 retval = WM_UI_HANDLER_BREAK;
1814                         }
1815                         break;
1816                 }
1817         }
1818
1819         if (event->val == KM_PRESS) {
1820                 switch (event->type) {
1821                         case VKEY:
1822                         case XKEY:
1823                         case CKEY:
1824                                 if (event->ctrl || event->oskey) {
1825                                         if (event->type == VKEY)
1826                                                 changed = ui_textedit_copypaste(but, data, 1, 0, 0);
1827                                         else if (event->type == CKEY)
1828                                                 changed = ui_textedit_copypaste(but, data, 0, 1, 0);
1829                                         else if (event->type == XKEY)
1830                                                 changed = ui_textedit_copypaste(but, data, 0, 0, 1);
1831
1832                                         retval = WM_UI_HANDLER_BREAK;
1833                                 }
1834                                 break;
1835                         case RIGHTARROWKEY:
1836                                 ui_textedit_move(but, data, STRCUR_DIR_NEXT, event->shift, event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE);
1837                                 retval = WM_UI_HANDLER_BREAK;
1838                                 break;
1839                         case LEFTARROWKEY:
1840                                 ui_textedit_move(but, data, STRCUR_DIR_PREV, event->shift, event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE);
1841                                 retval = WM_UI_HANDLER_BREAK;
1842                                 break;
1843                         case DOWNARROWKEY:
1844                                 if (data->searchbox) {
1845                                         ui_searchbox_event(C, data->searchbox, but, event);
1846                                         break;
1847                                 }
1848                         /* pass on purposedly */
1849                         case ENDKEY:
1850                                 ui_textedit_move(but, data, STRCUR_DIR_NEXT, event->shift, STRCUR_JUMP_ALL);
1851                                 retval = WM_UI_HANDLER_BREAK;
1852                                 break;
1853                         case UPARROWKEY:
1854                                 if (data->searchbox) {
1855                                         ui_searchbox_event(C, data->searchbox, but, event);
1856                                         break;
1857                                 }
1858                         /* pass on purposedly */
1859                         case HOMEKEY:
1860                                 ui_textedit_move(but, data, STRCUR_DIR_PREV, event->shift, STRCUR_JUMP_ALL);
1861                                 retval = WM_UI_HANDLER_BREAK;
1862                                 break;
1863                         case PADENTER:
1864                         case RETKEY:
1865                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1866                                 retval = WM_UI_HANDLER_BREAK;
1867                                 break;
1868                         case DELKEY:
1869                                 changed = ui_textedit_delete(but, data, 1, event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE);
1870                                 retval = WM_UI_HANDLER_BREAK;
1871                                 break;
1872
1873                         case BACKSPACEKEY:
1874                                 changed = ui_textedit_delete(but, data, 0, event->shift ? STRCUR_JUMP_ALL :  (event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE));
1875                                 retval = WM_UI_HANDLER_BREAK;
1876                                 break;
1877                                 
1878                         case TABKEY:
1879                                 /* there is a key conflict here, we can't tab with autocomplete */
1880                                 if (but->autocomplete_func || data->searchbox) {
1881                                         changed = ui_textedit_autocomplete(C, but, data);
1882                                         update = 1; /* do live update for tab key */
1883                                 }
1884                                 /* the hotkey here is not well defined, was G.qual so we check all */
1885                                 else if (event->shift || event->ctrl || event->alt || event->oskey) {
1886                                         ui_textedit_prev_but(block, but, data);
1887                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1888                                 }
1889                                 else {
1890                                         ui_textedit_next_but(block, but, data);
1891                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1892                                 }
1893                                 retval = WM_UI_HANDLER_BREAK;
1894                                 break;
1895                 }
1896
1897                 if ((event->ascii || event->utf8_buf[0]) && (retval == WM_UI_HANDLER_CONTINUE)) {
1898                         char ascii = event->ascii;
1899                         const char *utf8_buf = event->utf8_buf;
1900
1901                         /* exception that's useful for number buttons, some keyboard
1902                          * numpads have a comma instead of a period */
1903                         if (ELEM3(but->type, NUM, NUMABS, NUMSLI)) { /* could use data->min*/
1904                                 if (event->type == PADPERIOD && ascii == ',') {
1905                                         ascii = '.';
1906                                         utf8_buf = NULL; /* force ascii fallback */
1907                                 }
1908                         }
1909
1910                         if (utf8_buf && utf8_buf[0]) {
1911                                 int utf8_buf_len = BLI_str_utf8_size(utf8_buf);
1912                                 /* keep this printf until utf8 is well tested */
1913                                 if (utf8_buf_len != 1) {
1914                                         printf("%s: utf8 char '%.*s'\n", __func__, utf8_buf_len, utf8_buf);
1915                                 }
1916
1917                                 // strcpy(utf8_buf, "12345");
1918                                 changed = ui_textedit_type_buf(but, data, event->utf8_buf, utf8_buf_len);
1919                         }
1920                         else {
1921                                 changed = ui_textedit_type_ascii(but, data, ascii);
1922                         }
1923
1924                         retval = WM_UI_HANDLER_BREAK;
1925                         
1926                 }
1927                 /* textbutton with magnifier icon: do live update for search button */
1928                 if (but->icon == ICON_VIEWZOOM)
1929                         update = 1;
1930         }
1931
1932         if (changed) {
1933                 /* only update when typing for TAB key */
1934                 if (update && data->interactive) ui_apply_button(C, block, but, data, 1);
1935                 else ui_check_but(but);
1936                 but->changed = TRUE;
1937                 
1938                 if (data->searchbox)
1939                         ui_searchbox_update(C, data->searchbox, but, 1);  /* 1 = reset */
1940         }
1941
1942         if (changed || (retval == WM_UI_HANDLER_BREAK))
1943                 ED_region_tag_redraw(data->region);
1944 }
1945
1946 static void ui_do_but_textedit_select(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1947 {
1948         int mx, my, retval = WM_UI_HANDLER_CONTINUE;
1949
1950         switch (event->type) {
1951                 case MOUSEMOVE: {
1952                         mx = event->x;
1953                         my = event->y;
1954                         ui_window_to_block(data->region, block, &mx, &my);
1955
1956                         ui_textedit_set_cursor_select(but, data, mx);
1957                         retval = WM_UI_HANDLER_BREAK;
1958                         break;
1959                 }
1960                 case LEFTMOUSE:
1961                         if (event->val == KM_RELEASE)
1962                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1963                         retval = WM_UI_HANDLER_BREAK;
1964                         break;
1965         }
1966
1967         if (retval == WM_UI_HANDLER_BREAK) {
1968                 ui_check_but(but);
1969                 ED_region_tag_redraw(data->region);
1970         }
1971 }
1972
1973 /* ************* number editing for various types ************* */
1974
1975 static void ui_numedit_begin(uiBut *but, uiHandleButtonData *data)
1976 {
1977         if (but->type == BUT_CURVE) {
1978                 but->editcumap = (CurveMapping *)but->poin;
1979         }
1980         else if (but->type == BUT_COLORBAND) {
1981                 data->coba = (ColorBand *)but->poin;
1982                 but->editcoba = data->coba;
1983         }
1984         else if (ELEM3(but->type, BUT_NORMAL, HSVCUBE, HSVCIRCLE)) {
1985                 ui_get_but_vectorf(but, data->origvec);
1986                 copy_v3_v3(data->vec, data->origvec);
1987                 but->editvec = data->vec;
1988         }
1989         else {
1990                 float softrange, softmin, softmax;
1991
1992                 data->startvalue = ui_get_but_val(but);
1993                 data->origvalue = data->startvalue;
1994                 data->value = data->origvalue;
1995                 but->editval = &data->value;
1996
1997                 softmin = but->softmin;
1998                 softmax = but->softmax;
1999                 softrange = softmax - softmin;
2000
2001                 data->dragfstart = (softrange == 0.0f) ? 0.0f : ((float)data->value - softmin) / softrange;
2002                 data->dragf = data->dragfstart;
2003         }
2004
2005         data->dragchange = 0;
2006         data->draglock = 1;
2007 }
2008
2009 static void ui_numedit_end(uiBut *but, uiHandleButtonData *data)
2010 {
2011         but->editval = NULL;
2012         but->editvec = NULL;
2013         but->editcoba = NULL;
2014         but->editcumap = NULL;
2015
2016         data->dragstartx = 0;
2017         data->draglastx = 0;
2018         data->dragchange = 0;
2019         data->dragcbd = NULL;
2020         data->dragsel = 0;
2021 }
2022
2023 static void ui_numedit_apply(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data)
2024 {
2025         if (data->interactive) ui_apply_button(C, block, but, data, 1);
2026         else ui_check_but(but);
2027
2028         ED_region_tag_redraw(data->region);
2029 }
2030
2031 /* ****************** menu opening for various types **************** */
2032
2033 static void ui_blockopen_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
2034 {
2035         uiBlockCreateFunc func = NULL;
2036         uiBlockHandleCreateFunc handlefunc = NULL;
2037         uiMenuCreateFunc menufunc = NULL;
2038         char *menustr = NULL;
2039         void *arg = NULL;
2040
2041         switch (but->type) {
2042                 case BLOCK:
2043                 case PULLDOWN:
2044                         if (but->menu_create_func) {
2045                                 menufunc = but->menu_create_func;
2046                                 arg = but->poin;
2047                         }
2048                         else {
2049                                 func = but->block_create_func;
2050                                 arg = but->poin ? but->poin : but->func_argN;
2051                         }
2052                         break;
2053                 case MENU:
2054                         if (but->menu_create_func) {
2055                                 menufunc = but->menu_create_func;
2056                                 arg = but->poin;
2057                         }
2058                         else {
2059                                 data->origvalue = ui_get_but_val(but);
2060                                 data->value = data->origvalue;
2061                                 but->editval = &data->value;
2062
2063                                 menustr = but->str;
2064                         }
2065                         break;
2066                 case ICONROW:
2067                         menufunc = ui_block_func_ICONROW;
2068                         arg = but;
2069                         break;
2070                 case ICONTEXTROW:
2071                         menufunc = ui_block_func_ICONTEXTROW;
2072                         arg = but;
2073                         break;
2074                 case COL:
2075                         ui_get_but_vectorf(but, data->origvec);
2076                         copy_v3_v3(data->vec, data->origvec);
2077                         but->editvec = data->vec;
2078
2079                         handlefunc = ui_block_func_COL;
2080                         arg = but;
2081                         break;
2082         }
2083
2084         if (func || handlefunc) {
2085                 data->menu = ui_popup_block_create(C, data->region, but, func, handlefunc, arg);
2086                 if (but->block->handle)
2087                         data->menu->popup = but->block->handle->popup;
2088         }
2089         else if (menufunc || menustr) {
2090                 data->menu = ui_popup_menu_create(C, data->region, but, menufunc, arg, menustr);
2091                 if (but->block->handle)
2092                         data->menu->popup = but->block->handle->popup;
2093         }
2094
2095         /* this makes adjacent blocks auto open from now on */
2096         //if (but->block->auto_open ==0 ) but->block->auto_open = 1;
2097 }
2098
2099 static void ui_blockopen_end(bContext *C, uiBut *but, uiHandleButtonData *data)
2100 {
2101         if (but) {
2102                 but->editval = NULL;
2103                 but->editvec = NULL;
2104
2105                 but->block->auto_open_last = PIL_check_seconds_timer();
2106         }
2107
2108         if (data->menu) {
2109                 ui_popup_block_free(C, data->menu);
2110                 data->menu = NULL;
2111         }
2112 }
2113
2114 /* ***************** events for different button types *************** */
2115
2116 static int ui_do_but_BUT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2117 {
2118         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2119                 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
2120                         button_activate_state(C, but, BUTTON_STATE_WAIT_RELEASE);
2121                         return WM_UI_HANDLER_BREAK;
2122                 }
2123                 else if (event->type == LEFTMOUSE && but->block->handle) {
2124                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2125                         return WM_UI_HANDLER_BREAK;
2126                 }
2127                 else if (ELEM(event->type, PADENTER, RETKEY) && event->val == KM_PRESS) {
2128                         button_activate_state(C, but, BUTTON_STATE_WAIT_FLASH);
2129                         return WM_UI_HANDLER_BREAK;
2130                 }
2131         }
2132         else if (data->state == BUTTON_STATE_WAIT_RELEASE) {
2133                 if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
2134                         if (!(but->flag & UI_SELECT))
2135                                 data->cancel = 1;
2136                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2137                         return WM_UI_HANDLER_BREAK;
2138                 }
2139         }
2140
2141         return WM_UI_HANDLER_CONTINUE;
2142 }
2143
2144 static int ui_do_but_HOTKEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2145 {
2146         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2147                 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2148                         but->drawstr[0] = 0;
2149                         but->modifier_key = 0;
2150                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
2151                         return WM_UI_HANDLER_BREAK;
2152                 }
2153         }
2154         else if (data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2155                 
2156                 if (event->type == MOUSEMOVE)
2157                         return WM_UI_HANDLER_CONTINUE;
2158                 
2159                 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
2160                         /* only cancel if click outside the button */
2161                         if (ui_mouse_inside_button(but->active->region, but, event->x, event->y) == 0) {
2162                                 /* data->cancel doesnt work, this button opens immediate */
2163                                 if (but->flag & UI_BUT_IMMEDIATE)
2164                                         ui_set_but_val(but, 0);
2165                                 else
2166                                         data->cancel = 1;
2167                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2168                                 return WM_UI_HANDLER_BREAK;
2169                         }
2170                 }
2171                 
2172                 /* always set */
2173                 but->modifier_key = 0;
2174                 if (event->shift) but->modifier_key |= KM_SHIFT;
2175                 if (event->alt) but->modifier_key |= KM_ALT;
2176                 if (event->ctrl) but->modifier_key |= KM_CTRL;
2177                 if (event->oskey) but->modifier_key |= KM_OSKEY;
2178
2179                 ui_check_but(but);
2180                 ED_region_tag_redraw(data->region);
2181                         
2182                 if (event->val == KM_PRESS) {
2183                         if (ISHOTKEY(event->type)) { 
2184                                 
2185                                 if (WM_key_event_string(event->type)[0])
2186                                         ui_set_but_val(but, event->type);
2187                                 else
2188                                         data->cancel = 1;
2189                                 
2190                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2191                                 return WM_UI_HANDLER_BREAK;
2192                         }
2193                         else if (event->type == ESCKEY) {
2194                                 data->cancel = 1;
2195                                 data->escapecancel = 1;
2196                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2197                         }
2198                         
2199                 }
2200         }
2201         
2202         return WM_UI_HANDLER_CONTINUE;
2203 }
2204
2205 static int ui_do_but_KEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2206 {
2207         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2208                 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2209                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
2210                         return WM_UI_HANDLER_BREAK;
2211                 }
2212         }
2213         else if (data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2214                 if (event->type == MOUSEMOVE)
2215                         return WM_UI_HANDLER_CONTINUE;
2216
2217                 if (event->val == KM_PRESS) {
2218                         if (WM_key_event_string(event->type)[0])
2219                                 ui_set_but_val(but, event->type);
2220                         else
2221                                 data->cancel = 1;
2222
2223                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2224                 }
2225         }
2226
2227         return WM_UI_HANDLER_CONTINUE;
2228 }
2229
2230 static int ui_do_but_TEX(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2231 {
2232         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2233                 if (ELEM(event->type, LEFTMOUSE, EVT_BUT_OPEN) && event->val == KM_PRESS) {
2234                         if (but->dt == UI_EMBOSSN && !event->ctrl) ;
2235                         else {
2236                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2237                                 return WM_UI_HANDLER_BREAK;
2238                         }
2239                 }
2240         }
2241         else if (data->state == BUTTON_STATE_TEXT_EDITING) {
2242                 ui_do_but_textedit(C, block, but, data, event);
2243                 return WM_UI_HANDLER_BREAK;
2244         }
2245         else if (data->state == BUTTON_STATE_TEXT_SELECTING) {
2246                 ui_do_but_textedit_select(C, block, but, data, event);
2247                 return WM_UI_HANDLER_BREAK;
2248         }
2249
2250         return WM_UI_HANDLER_CONTINUE;
2251 }
2252
2253 static int ui_do_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2254 {
2255         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2256                 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2257                         data->togdual = event->ctrl;
2258                         data->togonly = !event->shift;
2259                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2260                         return WM_UI_HANDLER_BREAK;
2261                 }
2262         }
2263         return WM_UI_HANDLER_CONTINUE;
2264 }
2265
2266 static int ui_do_but_EXIT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2267 {
2268         
2269         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2270
2271                 /* first handle click on icondrag type button */
2272                 if (event->type == LEFTMOUSE && but->dragpoin) {
2273                         if (ui_but_mouse_inside_icon(but, data->region, event)) {
2274                                 
2275                                 /* tell the button to wait and keep checking further events to
2276                                  * see if it should start dragging */
2277                                 button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
2278                                 data->dragstartx = event->x;
2279                                 data->dragstarty = event->y;
2280                                 return WM_UI_HANDLER_CONTINUE;
2281                         }
2282                 }
2283                 
2284                 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2285                         int ret = WM_UI_HANDLER_BREAK;
2286                         /* XXX (a bit ugly) Special case handling for filebrowser drag button */
2287                         if (but->dragpoin && but->imb && ui_but_mouse_inside_icon(but, data->region, event)) {
2288                                 ret = WM_UI_HANDLER_CONTINUE;
2289                         }
2290                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2291                         return ret;
2292                 }
2293         }
2294         else if (data->state == BUTTON_STATE_WAIT_DRAG) {
2295                 
2296                 /* this function also ends state */
2297                 if (ui_but_start_drag(C, but, data, event)) {
2298                         return WM_UI_HANDLER_BREAK;
2299                 }
2300                 
2301                 /* If the mouse has been pressed and released, getting to 
2302                  * this point without triggering a drag, then clear the 
2303                  * drag state for this button and continue to pass on the event */
2304                 if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
2305                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2306                         return WM_UI_HANDLER_CONTINUE;
2307                 }
2308                 
2309                 /* while waiting for a drag to be triggered, always block 
2310                  * other events from getting handled */
2311                 return WM_UI_HANDLER_BREAK;
2312         }
2313         
2314         return WM_UI_HANDLER_CONTINUE;
2315 }
2316
2317 /* var names match ui_numedit_but_NUM */
2318 static float ui_numedit_apply_snapf(uiBut *but, float tempf, float softmin, float softmax, float softrange, int snap)
2319 {
2320         if (tempf == softmin || tempf == softmax || snap == 0) {
2321                 /* pass */
2322         }
2323         else {
2324                 float fac = 1.0f;
2325                 
2326                 if (ui_is_but_unit(but)) {
2327                         UnitSettings *unit = but->block->unit;
2328                         int unit_type = RNA_SUBTYPE_UNIT_VALUE(uiButGetUnitType(but));
2329
2330                         if (bUnit_IsValid(unit->system, unit_type)) {
2331                                 fac = (float)bUnit_BaseScalar(unit->system, unit_type);
2332                                 if (ELEM3(unit_type, B_UNIT_LENGTH, B_UNIT_AREA, B_UNIT_VOLUME)) {
2333                                         fac /= unit->scale_length;
2334                                 }
2335                         }
2336                 }
2337
2338                 if (fac != 1.0f) {
2339                         /* snap in unit-space */
2340                         tempf /= fac;
2341                         /* softmin /= fac; */ /* UNUSED */
2342                         /* softmax /= fac; */ /* UNUSED */
2343                         softrange /= fac;
2344                 }
2345
2346                 if (snap == 1) {
2347                         if (softrange < 2.10f) tempf = 0.1f * floorf(10.0f * tempf);
2348                         else if (softrange < 21.0f) tempf = floorf(tempf);
2349                         else tempf = 10.0f * floorf(tempf / 10.0f);
2350                 }
2351                 else if (snap == 2) {
2352                         if (softrange < 2.10f) tempf = 0.01f * floorf(100.0f * tempf);
2353                         else if (softrange < 21.0f) tempf = 0.1f * floorf(10.0f * tempf);
2354                         else tempf = floor(tempf);
2355                 }
2356                 
2357                 if (fac != 1.0f)
2358                         tempf *= fac;
2359         }
2360
2361         return tempf;
2362 }
2363
2364 static float ui_numedit_apply_snap(int temp, float softmin, float softmax, int snap)
2365 {
2366         if (temp == softmin || temp == softmax)
2367                 return temp;
2368
2369         switch (snap) {
2370                 case 0:
2371                         break;
2372                 case 1:
2373                         temp = 10 * (temp / 10);
2374                         break;
2375                 case 2:
2376                         temp = 100 * (temp / 100);
2377                         break;
2378         }
2379
2380         return temp;
2381 }
2382
2383 static int ui_numedit_but_NUM(uiBut *but, uiHandleButtonData *data, float fac, int snap, int mx)
2384 {
2385         float deler, tempf, softmin, softmax, softrange;
2386         int lvalue, temp, changed = 0;
2387         
2388         if (mx == data->draglastx)
2389                 return changed;
2390         
2391         /* drag-lock - prevent unwanted scroll adjustments */
2392         /* change value (now 3) to adjust threshold in pixels */
2393         if (data->draglock) {
2394                 if (abs(mx - data->dragstartx) <= 3)
2395                         return changed;
2396
2397                 data->draglock = 0;
2398                 data->dragstartx = mx;  /* ignore mouse movement within drag-lock */
2399         }
2400
2401         softmin = but->softmin;
2402         softmax = but->softmax;
2403         softrange = softmax - softmin;
2404
2405         if (ui_is_a_warp_but(but)) {
2406                 /* Mouse location isn't screen clamped to the screen so use a linear mapping
2407                  * 2px == 1-int, or 1px == 1-ClickStep */
2408                 if (ui_is_but_float(but)) {
2409                         fac *= 0.01f * but->a1;
2410                         tempf = (float)data->startvalue + ((float)(mx - data->dragstartx) * fac);
2411                         tempf = ui_numedit_apply_snapf(but, tempf, softmin, softmax, softrange, snap);
2412
2413 #if 1       /* fake moving the click start, nicer for dragging back after passing the limit */
2414                         if (tempf < softmin) {
2415                                 data->dragstartx -= (softmin - tempf) / fac;
2416                                 tempf = softmin;
2417                         }
2418                         else if (tempf > softmax) {
2419                                 data->dragstartx += (tempf - softmax) / fac;
2420                                 tempf = softmax;
2421                         }
2422 #else
2423                         CLAMP(tempf, softmin, softmax);
2424 #endif
2425
2426                         if (tempf != (float)data->value) {
2427                                 data->dragchange = 1;
2428                                 data->value = tempf;
2429                                 changed = 1;
2430                         }
2431                 }
2432                 else {
2433                         if (softrange > 256) fac = 1.0;             /* 1px == 1 */
2434                         else if (softrange > 32) fac = 1.0 / 2.0;   /* 2px == 1 */
2435                         else fac = 1.0 / 16.0;                  /* 16px == 1? */
2436
2437                         temp = data->startvalue + (((double)mx - data->dragstartx) * (double)fac);
2438                         temp = ui_numedit_apply_snap(temp, softmin, softmax, snap);
2439
2440 #if 1       /* fake moving the click start, nicer for dragging back after passing the limit */
2441                         if (temp < softmin) {
2442                                 data->dragstartx -= (softmin - temp) / fac;
2443                                 temp = softmin;
2444                         }
2445                         else if (temp > softmax) {
2446                                 data->dragstartx += (temp - softmax) / fac;
2447                                 temp = softmax;
2448                         }
2449 #else
2450                         CLAMP(temp, softmin, softmax);
2451 #endif
2452
2453                         if (temp != data->value) {
2454                                 data->dragchange = 1;
2455                                 data->value = temp;
2456                                 changed = 1;
2457                         }
2458                 }
2459
2460                 data->draglastx = mx;
2461         }
2462         else {
2463                 /* Use a non-linear mapping of the mouse drag especially for large floats (normal behavior) */
2464                 deler = 500;
2465                 if (!ui_is_but_float(but)) {
2466                         /* prevent large ranges from getting too out of control */
2467                         if (softrange > 600) deler = powf(softrange, 0.75);
2468                         
2469                         if (softrange < 100) deler = 200.0;
2470                         if (softrange < 25) deler = 50.0;
2471                 }
2472                 deler /= fac;
2473
2474                 if (softrange > 11) {
2475                         /* non linear change in mouse input- good for high precicsion */
2476                         data->dragf += (((float)(mx - data->draglastx)) / deler) * (fabsf(data->dragstartx - mx) * 0.002f);
2477                 }
2478                 else if (softrange > 129) { /* only scale large int buttons */
2479                         /* non linear change in mouse input- good for high precicsionm ints need less fine tuning */
2480                         data->dragf += (((float)(mx - data->draglastx)) / deler) * (fabsf(data->dragstartx - mx) * 0.004f);
2481                 }
2482                 else {
2483                         /*no scaling */
2484                         data->dragf += ((float)(mx - data->draglastx)) / deler;
2485                 }
2486         
2487                 CLAMP(data->dragf, 0.0f, 1.0f);
2488                 data->draglastx = mx;
2489                 tempf = (softmin + data->dragf * softrange);
2490
2491
2492                 if (!ui_is_but_float(but)) {
2493                         temp = floorf(tempf + 0.5f);
2494
2495                         temp = ui_numedit_apply_snap(temp, softmin, softmax, snap);
2496
2497                         CLAMP(temp, softmin, softmax);
2498                         lvalue = (int)data->value;
2499                         
2500                         if (temp != lvalue) {
2501                                 data->dragchange = 1;
2502                                 data->value = (double)temp;
2503                                 changed = 1;
2504                         }
2505                 }
2506                 else {
2507                         temp = 0;
2508                         tempf = ui_numedit_apply_snapf(but, tempf, softmin, softmax, softrange, snap);
2509
2510                         CLAMP(tempf, softmin, softmax);
2511
2512                         if (tempf != (float)data->value) {
2513                                 data->dragchange = 1;
2514                                 data->value = tempf;
2515                                 changed = 1;
2516                         }
2517                 }
2518         }
2519
2520
2521         return changed;
2522 }
2523
2524 static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2525 {
2526         int mx, my; /* mouse location scaled to fit the UI */
2527         int screen_mx, screen_my; /* mouse location kept at screen pixel coords */
2528         int click = 0;
2529         int retval = WM_UI_HANDLER_CONTINUE;
2530         
2531         mx = screen_mx = event->x;
2532         my = screen_my = event->y;
2533
2534         ui_window_to_block(data->region, block, &mx, &my);
2535
2536         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2537                 /* XXX hardcoded keymap check.... */
2538                 if (event->type == WHEELDOWNMOUSE && event->alt) {
2539                         mx = but->rect.xmin;
2540                         click = 1;
2541                 }
2542                 else if (event->type == WHEELUPMOUSE && event->alt) {
2543                         mx = but->rect.xmax;
2544                         click = 1;
2545                 }
2546                 else if (event->val == KM_PRESS) {
2547                         if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2548                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2549                                 retval = WM_UI_HANDLER_BREAK;
2550                         }
2551                         else if (event->type == LEFTMOUSE) {
2552                                 data->dragstartx = data->draglastx = ui_is_a_warp_but(but) ? screen_mx : mx;
2553                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2554                                 retval = WM_UI_HANDLER_BREAK;
2555                         }
2556                         else if (ELEM(event->type, PADENTER, RETKEY) && event->val == KM_PRESS)
2557                                 click = 1;
2558                         else if (event->type == MINUSKEY && event->val == KM_PRESS) {
2559                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2560                                 data->value = -data->value;
2561                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2562                                 retval = WM_UI_HANDLER_BREAK;
2563                         }
2564                 }
2565                 
2566         }
2567         else if (data->state == BUTTON_STATE_NUM_EDITING) {
2568                 if (event->type == ESCKEY) {
2569                         data->cancel = 1;
2570                         data->escapecancel = 1;
2571                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2572                 }
2573                 else if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
2574                         if (data->dragchange)
2575                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2576                         else
2577                                 click = 1;
2578                 }
2579                 else if (event->type == MOUSEMOVE) {
2580                         float fac;
2581                         int snap;
2582
2583                         fac = 1.0f;
2584                         if (event->shift) fac /= 10.0f;
2585                         if (event->alt) fac /= 20.0f;
2586                         
2587                         snap = (event->ctrl) ? (event->shift) ? 2 : 1 : 0;
2588
2589                         if (ui_numedit_but_NUM(but, data, fac, snap, (ui_is_a_warp_but(but) ? screen_mx : mx)))
2590                                 ui_numedit_apply(C, block, but, data);
2591                 }
2592                 retval = WM_UI_HANDLER_BREAK;
2593         }
2594         else if (data->state == BUTTON_STATE_TEXT_EDITING) {
2595                 ui_do_but_textedit(C, block, but, data, event);
2596                 retval = WM_UI_HANDLER_BREAK;
2597         }
2598         else if (data->state == BUTTON_STATE_TEXT_SELECTING) {
2599                 ui_do_but_textedit_select(C, block, but, data, event);
2600                 retval = WM_UI_HANDLER_BREAK;
2601         }
2602         
2603         if (click) {
2604                 /* we can click on the side arrows to increment/decrement,
2605                  * or click inside to edit the value directly */
2606                 float tempf, softmin, softmax;
2607                 int temp;
2608
2609                 softmin = but->softmin;
2610                 softmax = but->softmax;
2611
2612                 if (!ui_is_but_float(but)) {
2613                         if (mx < (but->rect.xmin + (but->rect.xmax - but->rect.xmin) / 3 - 3)) {
2614                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2615
2616                                 temp = (int)data->value - 1;
2617                                 if (temp >= softmin && temp <= softmax)
2618                                         data->value = (double)temp;
2619                                 else
2620                                         data->cancel = 1;
2621
2622                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2623                         }
2624                         else if (mx > (but->rect.xmin + (2 * (but->rect.xmax - but->rect.xmin) / 3) + 3)) {
2625                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2626
2627                                 temp = (int)data->value + 1;
2628                                 if (temp >= softmin && temp <= softmax)
2629                                         data->value = (double)temp;
2630                                 else
2631                                         data->cancel = 1;
2632
2633                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2634                         }
2635                         else
2636                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2637                 }
2638                 else {
2639                         if (mx < (but->rect.xmin + (but->rect.xmax - but->rect.xmin) / 3 - 3)) {
2640                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2641
2642                                 tempf = (float)data->value - 0.01f * but->a1;
2643                                 if (tempf < softmin) tempf = softmin;
2644                                 data->value = tempf;
2645
2646                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2647                         }
2648                         else if (mx > but->rect.xmin + (2 * ((but->rect.xmax - but->rect.xmin) / 3) + 3)) {
2649                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2650
2651                                 tempf = (float)data->value + 0.01f * but->a1;
2652                                 if (tempf > softmax) tempf = softmax;
2653                                 data->value = tempf;
2654
2655                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2656                         }
2657                         else
2658                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2659                 }
2660
2661                 retval = WM_UI_HANDLER_BREAK;
2662         }
2663         
2664         return retval;
2665 }
2666
2667 static int ui_numedit_but_SLI(uiBut *but, uiHandleButtonData *data, const short shift, const short ctrl, int mx)
2668 {
2669         float deler, f, tempf, softmin, softmax, softrange;
2670         int temp, lvalue, changed = 0;
2671
2672         softmin = but->softmin;
2673         softmax = but->softmax;
2674         softrange = softmax - softmin;
2675
2676         if (but->type == NUMSLI) deler = ((but->rect.xmax - but->rect.xmin) - 5.0f * but->aspect);
2677         else if (but->type == HSVSLI) deler = ((but->rect.xmax - but->rect.xmin) / 2.0f - 5.0f * but->aspect);
2678         else if (but->type == SCROLL) {
2679                 int horizontal = (but->rect.xmax - but->rect.xmin > but->rect.ymax - but->rect.ymin);
2680                 float size = (horizontal) ? (but->rect.xmax - but->rect.xmin) : -(but->rect.ymax - but->rect.ymin);
2681                 deler = size * (but->softmax - but->softmin) / (but->softmax - but->softmin + but->a1);
2682         }
2683         else deler = (but->rect.xmax - but->rect.xmin - 5.0f * but->aspect);
2684
2685         f = (float)(mx - data->dragstartx) / deler + data->dragfstart;
2686         
2687         if (shift)
2688                 f = (f - data->dragfstart) / 10.0f + data->dragfstart;
2689
2690         CLAMP(f, 0.0f, 1.0f);
2691         tempf = softmin + f * softrange;
2692         temp = floorf(tempf + 0.5f);
2693
2694         if (ctrl) {
2695                 if (tempf == softmin || tempf == softmax) ;
2696                 else if (ui_is_but_float(but)) {
2697
2698                         if (shift) {
2699                                 if (tempf == softmin || tempf == softmax) ;
2700                                 else if (softmax - softmin < 2.10f) tempf = 0.01f * floorf(100.0f * tempf);
2701                                 else if (softmax - softmin < 21.0f) tempf = 0.1f * floorf(10.0f * tempf);
2702                                 else tempf = floorf(tempf);
2703                         }
2704                         else {
2705                                 if (softmax - softmin < 2.10f) tempf = 0.1f * floorf(10.0f * tempf);
2706                                 else if (softmax - softmin < 21.0f) tempf = floorf(tempf);
2707                                 else tempf = 10.0f * floorf(tempf / 10.0f);
2708                         }
2709                 }
2710                 else {
2711                         temp = 10 * (temp / 10);
2712                         tempf = temp;
2713                 }
2714         }
2715
2716         if (!ui_is_but_float(but)) {
2717                 lvalue = floor(data->value + 0.5);
2718
2719                 CLAMP(temp, softmin, softmax);
2720
2721                 if (temp != lvalue) {
2722                         data->value = temp;
2723                         data->dragchange = 1;
2724                         changed = 1;
2725                 }
2726         }
2727         else {
2728                 CLAMP(tempf, softmin, softmax);
2729
2730                 if (tempf != (float)data->value) {
2731                         data->value = tempf;
2732                         data->dragchange = 1;
2733                         changed = 1;
2734                 }
2735         }
2736
2737         return changed;
2738 }
2739
2740 static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2741 {
2742         int mx, my, click = 0;
2743         int retval = WM_UI_HANDLER_CONTINUE;
2744
2745         mx = event->x;
2746         my = event->y;
2747         ui_window_to_block(data->region, block, &mx, &my);
2748
2749         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2750                 /* XXX hardcoded keymap check.... */
2751                 if (event->type == WHEELDOWNMOUSE && event->alt) {
2752                         mx = but->rect.xmin;
2753                         click = 2;
2754                 }
2755                 else if (event->type == WHEELUPMOUSE && event->alt) {
2756                         mx = but->rect.xmax;
2757                         click = 2;
2758                 }
2759                 else if (event->val == KM_PRESS) {
2760                         if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2761                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2762                                 retval = WM_UI_HANDLER_BREAK;
2763                         }
2764                         /* alt-click on sides to get "arrows" like in NUM buttons, and match wheel usage above */
2765                         else if (event->type == LEFTMOUSE && event->alt) {
2766                                 int halfpos = BLI_RCT_CENTER_X(&but->rect);
2767                                 click = 2;
2768                                 if (mx < halfpos)
2769                                         mx = but->rect.xmin;
2770                                 else
2771                                         mx = but->rect.xmax;
2772                         }
2773                         else if (event->type == LEFTMOUSE) {
2774                                 data->dragstartx = mx;
2775                                 data->draglastx = mx;
2776                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2777                                 retval = WM_UI_HANDLER_BREAK;
2778                         }
2779                         else if (ELEM(event->type, PADENTER, RETKEY) && event->val == KM_PRESS)
2780                                 click = 1;
2781                         else if (event->type == MINUSKEY && event->val == KM_PRESS) {
2782                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2783                                 data->value = -data->value;
2784                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2785                                 retval = WM_UI_HANDLER_BREAK;
2786                         }
2787                 }
2788         }
2789         else if (data->state == BUTTON_STATE_NUM_EDITING) {
2790                 if (event->type == ESCKEY) {
2791                         data->cancel = 1;
2792                         data->escapecancel = 1;
2793                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2794                 }
2795                 else if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
2796                         if (data->dragchange)
2797                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2798                         else
2799                                 click = 1;
2800                 }
2801                 else if (event->type == MOUSEMOVE) {
2802                         if (ui_numedit_but_SLI(but, data, event->shift, event->ctrl, mx))
2803                                 ui_numedit_apply(C, block, but, data);
2804                 }
2805                 retval = WM_UI_HANDLER_BREAK;
2806         }
2807         else if (data->state == BUTTON_STATE_TEXT_EDITING) {
2808                 ui_do_but_textedit(C, block, but, data, event);
2809                 retval = WM_UI_HANDLER_BREAK;
2810         }
2811         else if (data->state == BUTTON_STATE_TEXT_SELECTING) {
2812                 ui_do_but_textedit_select(C, block, but, data, event);
2813                 retval = WM_UI_HANDLER_BREAK;
2814         }
2815
2816         if (click) {
2817                 if (click == 2) {
2818                         /* nudge slider to the left or right */
2819                         float f, tempf, softmin, softmax, softrange;
2820                         int temp;
2821                         
2822                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2823                         
2824                         softmin = but->softmin;
2825                         softmax = but->softmax;
2826                         softrange = softmax - softmin;
2827                         
2828                         tempf = data->value;
2829                         temp = (int)data->value;
2830
2831 #if 0
2832                         if (but->type == SLI) {
2833                                 f = (float)(mx - but->rect.xmin) / (but->rect.xmax - but->rect.xmin); /* same as below */
2834                         }
2835                         else
2836 #endif
2837                         {
2838                                 f = (float)(mx - but->rect.xmin) / (but->rect.xmax - but->rect.xmin);
2839                         }
2840                         
2841                         f = softmin + f * softrange;
2842                         
2843                         if (!ui_is_but_float(but)) {
2844                                 if (f < temp) temp--;
2845                                 else temp++;
2846                                 
2847                                 if (temp >= softmin && temp <= softmax)
2848                                         data->value = temp;
2849                                 else
2850                                         data->cancel = 1;
2851                         } 
2852                         else {
2853                                 if (f < tempf) tempf -= 0.01f;
2854                                 else tempf += 0.01f;
2855                                 
2856                                 if (tempf >= softmin && tempf <= softmax)
2857                                         data->value = tempf;
2858                                 else
2859                                         data->cancel = 1;
2860                         }
2861                         
2862                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2863                         retval = WM_UI_HANDLER_BREAK;
2864                 }
2865                 else {
2866                         /* edit the value directly */
2867                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2868                         retval = WM_UI_HANDLER_BREAK;
2869                 }
2870         }
2871         
2872         return retval;
2873 }
2874
2875 static int ui_do_but_SCROLL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2876 {
2877         int mx, my /*, click= 0 */;
2878         int retval = WM_UI_HANDLER_CONTINUE;
2879         int horizontal = (but->rect.xmax - but->rect.xmin > but->rect.ymax - but->rect.ymin);
2880         
2881         mx = event->x;
2882         my = event->y;
2883         ui_window_to_block(data->region, block, &mx, &my);
2884
2885         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2886                 if (event->val == KM_PRESS) {
2887                         if (event->type == LEFTMOUSE) {
2888                                 if (horizontal) {
2889                                         data->dragstartx = mx;
2890                                         data->draglastx = mx;
2891                                 }
2892                                 else {
2893                                         data->dragstartx = my;
2894                                         data->draglastx = my;
2895                                 }
2896                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2897                                 retval = WM_UI_HANDLER_BREAK;
2898                         }
2899                         /* UNUSED - otherwise code is ok, add back if needed */
2900 #if 0
2901                         else if (ELEM(event->type, PADENTER, RETKEY) && event->val == KM_PRESS)
2902                                 click = 1;
2903 #endif
2904                 }
2905         }
2906         else if (data->state == BUTTON_STATE_NUM_EDITING) {
2907                 if (event->type == ESCKEY) {
2908                         data->cancel = 1;
2909                         data->escapecancel = 1;
2910                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2911                 }
2912                 else if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
2913                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2914                 }
2915                 else if (event->type == MOUSEMOVE) {
2916                         if (ui_numedit_but_SLI(but, data, 0, 0, (horizontal) ? mx : my))
2917                                 ui_numedit_apply(C, block, but, data);
2918                 }
2919
2920                 retval = WM_UI_HANDLER_BREAK;
2921         }
2922         
2923         return retval;
2924 }
2925
2926
2927 static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2928 {
2929         
2930         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2931                 
2932                 /* first handle click on icondrag type button */
2933                 if (event->type == LEFTMOUSE && but->dragpoin && event->val == KM_PRESS) {
2934                         if (ui_but_mouse_inside_icon(but, data->region, event)) {
2935                                 button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
2936                                 data->dragstartx = event->x;
2937                                 data->dragstarty = event->y;
2938                                 return WM_UI_HANDLER_BREAK;
2939                         }
2940                 }
2941                 
2942                 /* regular open menu */
2943                 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2944                         button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
2945                         return WM_UI_HANDLER_BREAK;
2946                 }
2947                 else if (ELEM3(but->type, MENU, ICONROW, ICONTEXTROW)) {
2948                         
2949                         if (event->type == WHEELDOWNMOUSE && event->alt) {
2950                                 data->value = ui_step_name_menu(but, -1);
2951                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2952                                 ui_apply_button(C, but->block, but, data, 1);
2953
2954                                 /* button's state need to be changed to EXIT so moving mouse away from this mouse wouldn't lead
2955                                  * to cancel changes made to this button, but changing state to EXIT also makes no button active for
2956                                  * a while which leads to triggering operator when doing fast scrolling mouse wheel.
2957                                  * using post activate stuff from button allows to make button be active again after checking for all
2958                                  * all that mouse leave and cancel stuff, so quick scroll wouldn't be an issue anymore.
2959                   &