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