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