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