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