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