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