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