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