code cleanup: changing the INT define to an enum conflicts with INT typedef on window...
[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_RCT_SIZE_Y(&rect));
701         }
702         else {
703                 int delta = BLI_RCT_SIZE_X(&rect) - BLI_RCT_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_RCT_SIZE_X(&but->rect), BLI_RCT_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         if (link->ppoin)
812                 sens_from_links = (bController ***)(link->ppoin);
813         else return;
814
815         act_to = (bActuator *)(to->poin);
816
817         /* (1) get the object */
818         CTX_DATA_BEGIN (C, Object *, ob_iter, selected_editable_objects)
819         {
820                 for (sens_iter = ob_iter->sensors.first; sens_iter; sens_iter = sens_iter->next) {
821                         if (&(sens_iter->links) == sens_from_links) {
822                                 ob = ob_iter;
823                                 break;
824                         }
825                 }
826                 if (ob) break;
827         } CTX_DATA_END;
828
829         if (!ob) return;
830
831         /* (2) check if the sensor and the actuator are from the same object */
832         for (act_iter = ob->actuators.first; act_iter; act_iter = (bActuator *)act_iter->next) {
833                 if (act_iter == act_to)
834                         break;
835         }
836
837         /* only works if the sensor and the actuator are from the same object */
838         if (!act_iter) return;
839
840         /* (3) add a new controller */
841         if (WM_operator_name_call(C, "LOGIC_OT_controller_add", WM_OP_EXEC_DEFAULT, NULL) & OPERATOR_FINISHED) {
842                 cont = (bController *)ob->controllers.last;
843                 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... */
844
845                 /* (4) link the sensor->controller->actuator */
846                 tmp_but = MEM_callocN(sizeof(uiBut), "uiBut");
847                 uiSetButLink(tmp_but, (void **)&cont, (void ***)&(cont->links), &(cont->totlinks), from->link->tocode, (int)to->hardmin);
848                 tmp_but->hardmin = from->link->tocode;
849                 tmp_but->poin = (char *)cont;
850
851                 tmp_but->type = INLINK;
852                 ui_add_link(C, from, tmp_but);
853
854                 tmp_but->type = LINK;
855                 ui_add_link(C, tmp_but, to);
856
857                 /* (5) garbage collection */
858                 MEM_freeN(tmp_but->link);
859                 MEM_freeN(tmp_but);
860         }
861 }
862
863 static void ui_add_link(bContext *C, uiBut *from, uiBut *to)
864 {
865         /* in 'from' we have to add a link to 'to' */
866         uiLink *link;
867         uiLinkLine *line;
868         void **oldppoin;
869         int a;
870         
871         if ((line = ui_is_a_link(from, to))) {
872                 line->flag |= UI_SELECT;
873                 ui_delete_active_linkline(from->block);
874                 return;
875         }
876
877         if (from->type == INLINK && to->type == INLINK) {
878                 return;
879         }
880         else if (from->type == LINK && to->type == INLINK) {
881                 if (from->link->tocode != (int)to->hardmin) {
882                         ui_add_smart_controller(C, from, to);
883                         return;
884                 }
885         }
886         else if (from->type == INLINK && to->type == LINK) {
887                 if (to->link->tocode == (int)from->hardmin) {
888                         return;
889                 }
890         }
891         
892         link = from->link;
893         
894         /* are there more pointers allowed? */
895         if (link->ppoin) {
896                 oldppoin = *(link->ppoin);
897                 
898                 (*(link->totlink))++;
899                 *(link->ppoin) = MEM_callocN(*(link->totlink) * sizeof(void *), "new link");
900                 
901                 for (a = 0; a < (*(link->totlink)) - 1; a++) {
902                         (*(link->ppoin))[a] = oldppoin[a];
903                 }
904                 (*(link->ppoin))[a] = to->poin;
905                 
906                 if (oldppoin) MEM_freeN(oldppoin);
907         }
908         else {
909                 *(link->poin) = to->poin;
910         }
911         
912 }
913
914
915 static void ui_apply_but_LINK(bContext *C, uiBut *but, uiHandleButtonData *data)
916 {
917         ARegion *ar = CTX_wm_region(C);
918         uiBut *bt;
919         
920         for (bt = but->block->buttons.first; bt; bt = bt->next) {
921                 if (ui_mouse_inside_button(ar, bt, but->linkto[0] + ar->winrct.xmin, but->linkto[1] + ar->winrct.ymin) )
922                         break;
923         }
924         if (bt && bt != but) {
925                 if (!ELEM(bt->type, LINK, INLINK) || !ELEM(but->type, LINK, INLINK))
926                         return;
927                 
928                 if (but->type == LINK) ui_add_link(C, but, bt);
929                 else ui_add_link(C, bt, but);
930
931                 ui_apply_but_func(C, but);
932                 data->retval = but->retval;
933         }
934         data->applied = TRUE;
935 }
936
937 static void ui_apply_but_IMAGE(bContext *C, uiBut *but, uiHandleButtonData *data)
938 {
939         ui_apply_but_func(C, but);
940         data->retval = but->retval;
941         data->applied = TRUE;
942 }
943
944 static void ui_apply_but_HISTOGRAM(bContext *C, uiBut *but, uiHandleButtonData *data)
945 {
946         ui_apply_but_func(C, but);
947         data->retval = but->retval;
948         data->applied = TRUE;
949 }
950
951 static void ui_apply_but_WAVEFORM(bContext *C, uiBut *but, uiHandleButtonData *data)
952 {
953         ui_apply_but_func(C, but);
954         data->retval = but->retval;
955         data->applied = TRUE;
956 }
957
958 static void ui_apply_but_TRACKPREVIEW(bContext *C, uiBut *but, uiHandleButtonData *data)
959 {
960         ui_apply_but_func(C, but);
961         data->retval = but->retval;
962         data->applied = TRUE;
963 }
964
965
966 static void ui_apply_button(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, int interactive)
967 {
968         char *editstr;
969         double *editval;
970         float *editvec;
971         ColorBand *editcoba;
972         CurveMapping *editcumap;
973
974         data->retval = 0;
975
976         /* if we cancel and have not applied yet, there is nothing to do,
977          * otherwise we have to restore the original value again */
978         if (data->cancel) {
979                 if (!data->applied)
980                         return;
981
982                 if (data->str) MEM_freeN(data->str);
983                 data->str = data->origstr;
984                 data->origstr = NULL;
985                 data->value = data->origvalue;
986                 data->origvalue = 0.0;
987                 copy_v3_v3(data->vec, data->origvec);
988                 data->origvec[0] = data->origvec[1] = data->origvec[2] = 0.0f;
989         }
990         else {
991                 /* we avoid applying interactive edits a second time
992                  * at the end with the appliedinteractive flag */
993                 if (interactive) {
994                         data->appliedinteractive = TRUE;
995                 }
996                 else if (data->appliedinteractive) {
997                         return;
998                 }
999         }
1000
1001         /* ensures we are writing actual values */
1002         editstr = but->editstr;
1003         editval = but->editval;
1004         editvec = but->editvec;
1005         editcoba = but->editcoba;
1006         editcumap = but->editcumap;
1007         but->editstr = NULL;
1008         but->editval = NULL;
1009         but->editvec = NULL;
1010         but->editcoba = NULL;
1011         but->editcumap = NULL;
1012
1013         /* handle different types */
1014         switch (but->type) {
1015                 case BUT:
1016                         ui_apply_but_BUT(C, but, data);
1017                         break;
1018                 case TEX:
1019                 case SEARCH_MENU:
1020                         ui_apply_but_TEX(C, but, data);
1021                         break;
1022                 case TOGBUT: 
1023                 case TOG: 
1024                 case TOGR: 
1025                 case ICONTOG:
1026                 case ICONTOGN:
1027                 case TOGN:
1028                 case BUT_TOGDUAL:
1029                 case OPTION:
1030                 case OPTIONN:
1031                         ui_apply_but_TOG(C, but, data);
1032                         break;
1033                 case ROW:
1034                 case LISTROW:
1035                         ui_apply_but_ROW(C, block, but, data);
1036                         break;
1037                 case SCROLL:
1038                 case NUM:
1039                 case NUMABS:
1040                 case SLI:
1041                 case NUMSLI:
1042                         ui_apply_but_NUM(C, but, data);
1043                         break;
1044                 case HSVSLI:
1045                         break;
1046                 case TOG3:      
1047                         ui_apply_but_TOG3(C, but, data);
1048                         break;
1049                 case MENU:
1050                 case ICONROW:
1051                 case ICONTEXTROW:
1052                 case BLOCK:
1053                 case PULLDOWN:
1054                         ui_apply_but_BLOCK(C, but, data);
1055                         break;
1056                 case COLOR:
1057                         if (data->cancel)
1058                                 ui_apply_but_VEC(C, but, data);
1059                         else
1060                                 ui_apply_but_BLOCK(C, but, data);
1061                         break;
1062                 case BUTM:
1063                         ui_apply_but_BUTM(C, but, data);
1064                         break;
1065                 case BUT_NORMAL:
1066                 case HSVCUBE:
1067                 case HSVCIRCLE:
1068                         ui_apply_but_VEC(C, but, data);
1069                         break;
1070                 case BUT_COLORBAND:
1071                         ui_apply_but_COLORBAND(C, but, data);
1072                         break;
1073                 case BUT_CURVE:
1074                         ui_apply_but_CURVE(C, but, data);
1075                         break;
1076                 case IDPOIN:
1077                         ui_apply_but_IDPOIN(C, but, data);
1078                         break;
1079 #ifdef WITH_INTERNATIONAL
1080                 case CHARTAB:
1081                         ui_apply_but_CHARTAB(C, but, data);
1082                         break;
1083 #endif
1084                 case KEYEVT:
1085                 case HOTKEYEVT:
1086                         ui_apply_but_BUT(C, but, data);
1087                         break;
1088                 case LINK:
1089                 case INLINK:
1090                         ui_apply_but_LINK(C, but, data);
1091                         break;
1092                 case BUT_IMAGE: 
1093                         ui_apply_but_IMAGE(C, but, data);
1094                         break;
1095                 case HISTOGRAM: 
1096                         ui_apply_but_HISTOGRAM(C, but, data);
1097                         break;
1098                 case WAVEFORM:
1099                         ui_apply_but_WAVEFORM(C, but, data);
1100                         break;
1101                 case TRACKPREVIEW:
1102                         ui_apply_but_TRACKPREVIEW(C, but, data);
1103                         break;
1104                 default:
1105                         break;
1106         }
1107
1108         but->editstr = editstr;
1109         but->editval = editval;
1110         but->editvec = editvec;
1111         but->editcoba = editcoba;
1112         but->editcumap = editcumap;
1113 }
1114
1115 /* ******************* drop event ********************  */
1116
1117 /* only call if event type is EVT_DROP */
1118 static void ui_but_drop(bContext *C, wmEvent *event, uiBut *but, uiHandleButtonData *data)
1119 {
1120         wmDrag *wmd;
1121         ListBase *drags = event->customdata; /* drop event type has listbase customdata by default */
1122         
1123         for (wmd = drags->first; wmd; wmd = wmd->next) {
1124                 if (wmd->type == WM_DRAG_ID) {
1125                         /* align these types with UI_but_active_drop_name */
1126                         if (ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1127                                 ID *id = (ID *)wmd->poin;
1128                                 
1129                                 if (but->poin == NULL && but->rnapoin.data == NULL) {}
1130                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1131                                 BLI_strncpy(data->str, id->name + 2, data->maxlen);
1132                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1133                         }
1134                 }
1135         }
1136         
1137 }
1138
1139 /* ******************* copy and paste ********************  */
1140
1141 /* c = copy, v = paste */
1142 static void ui_but_copy_paste(bContext *C, uiBut *but, uiHandleButtonData *data, char mode)
1143 {
1144         static ColorBand but_copypaste_coba = {0};
1145         char buf[UI_MAX_DRAW_STR + 1] = {0};
1146
1147         if (mode == 'v' && but->lock  == TRUE) {
1148                 return;
1149         }
1150
1151         if (mode == 'v') {
1152                 /* extract first line from clipboard in case of multi-line copies */
1153                 char *p, *pbuf = WM_clipboard_text_get(0);
1154                 p = pbuf;
1155                 if (p) {
1156                         int i = 0;
1157                         while (*p && *p != '\r' && *p != '\n' && i < UI_MAX_DRAW_STR) {
1158                                 buf[i++] = *p;
1159                                 p++;
1160                         }
1161                         buf[i] = 0;
1162                         MEM_freeN(pbuf);
1163                 }
1164         }
1165         
1166         /* numeric value */
1167         if (ELEM4(but->type, NUM, NUMABS, NUMSLI, HSVSLI)) {
1168                 
1169                 if (but->poin == NULL && but->rnapoin.data == NULL) ;
1170                 else if (mode == 'c') {
1171                         ui_get_but_string(but, buf, sizeof(buf));
1172                         WM_clipboard_text_set(buf, 0);
1173                 }
1174                 else {
1175                         double val;
1176
1177                         if (ui_set_but_string_eval_num(C, but, buf, &val)) {
1178                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1179                                 data->value = val;
1180                                 ui_set_but_string(C, but, buf);
1181                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1182                         }
1183                 }
1184         }
1185
1186         /* RGB triple */
1187         else if (but->type == COLOR) {
1188                 float rgb[3];
1189                 
1190                 if (but->poin == NULL && but->rnapoin.data == NULL) ;
1191                 else if (mode == 'c') {
1192
1193                         ui_get_but_vectorf(but, rgb);
1194                         /* convert to linear color to do compatible copy between gamma and non-gamma */
1195                         if (but->rnaprop && RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA)
1196                                 srgb_to_linearrgb_v3_v3(rgb, rgb);
1197
1198                         BLI_snprintf(buf, sizeof(buf), "[%f, %f, %f]", rgb[0], rgb[1], rgb[2]);
1199                         WM_clipboard_text_set(buf, 0);
1200                         
1201                 }
1202                 else {
1203                         if (sscanf(buf, "[%f, %f, %f]", &rgb[0], &rgb[1], &rgb[2]) == 3) {
1204                                 /* assume linear colors in buffer */
1205                                 if (but->rnaprop && RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA)
1206                                         linearrgb_to_srgb_v3_v3(rgb, rgb);
1207
1208                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1209                                 ui_set_but_vectorf(but, rgb);
1210                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1211                         }
1212                 }
1213         }
1214
1215         /* text/string and ID data */
1216         else if (ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1217                 uiHandleButtonData *active_data = but->active;
1218
1219                 if (but->poin == NULL && but->rnapoin.data == NULL) ;
1220                 else if (mode == 'c') {
1221                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1222                         BLI_strncpy(buf, active_data->str, UI_MAX_DRAW_STR);
1223                         WM_clipboard_text_set(active_data->str, 0);
1224                         active_data->cancel = TRUE;
1225                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1226                 }
1227                 else {
1228                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1229
1230                         if (ui_is_but_utf8(but)) BLI_strncpy_utf8(active_data->str, buf, active_data->maxlen);
1231                         else BLI_strncpy(active_data->str, buf, active_data->maxlen);
1232
1233                         if (but->type == SEARCH_MENU) {
1234                                 /* else uiSearchboxData.active member is not updated [#26856] */
1235                                 ui_searchbox_update(C, data->searchbox, but, 1);
1236                         }
1237                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1238                 }
1239         }
1240         /* colorband (not supported by system clipboard) */
1241         else if (but->type == BUT_COLORBAND) {
1242                 if (mode == 'c') {
1243                         if (but->poin == NULL)
1244                                 return;
1245
1246                         memcpy(&but_copypaste_coba, but->poin, sizeof(ColorBand));
1247                 }
1248                 else {
1249                         if (but_copypaste_coba.tot == 0)
1250                                 return;
1251
1252                         if (!but->poin)
1253                                 but->poin = MEM_callocN(sizeof(ColorBand), "colorband");
1254
1255                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1256                         memcpy(data->coba, &but_copypaste_coba, sizeof(ColorBand));
1257                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1258                 }
1259         }
1260         /* operator button (any type) */
1261         else if (but->optype) {
1262                 if (mode == 'c') {
1263                         PointerRNA *opptr;
1264                         char *str;
1265                         opptr = uiButGetOperatorPtrRNA(but); /* allocated when needed, the button owns it */
1266
1267                         str = WM_operator_pystring(C, but->optype, opptr, 0);
1268
1269                         WM_clipboard_text_set(str, 0);
1270
1271                         MEM_freeN(str);
1272                 }
1273         }
1274 }
1275
1276 /* ************* in-button text selection/editing ************* */
1277
1278
1279 static int ui_textedit_delete_selection(uiBut *but, uiHandleButtonData *data)
1280 {
1281         char *str = data->str;
1282         int len = strlen(str);
1283         int change = 0;
1284         if (but->selsta != but->selend && len) {
1285                 memmove(str + but->selsta, str + but->selend, (len - but->selend) + 1);
1286                 change = 1;
1287         }
1288         
1289         but->pos = but->selend = but->selsta;
1290         return change;
1291 }
1292
1293 /* note, but->block->aspect is used here, when drawing button style is getting scaled too */
1294 static void ui_textedit_set_cursor_pos(uiBut *but, uiHandleButtonData *data, short x)
1295 {
1296         uiStyle *style = UI_GetStyle();  // XXX pass on as arg
1297         uiFontStyle *fstyle = &style->widget;
1298         int startx = but->rect.xmin;
1299         char *origstr;
1300
1301         uiStyleFontSet(fstyle);
1302
1303         if (fstyle->kerning == 1) /* for BLF_width */
1304                 BLF_enable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
1305         
1306         origstr = MEM_callocN(sizeof(char) * data->maxlen, "ui_textedit origstr");
1307         
1308         BLI_strncpy(origstr, but->drawstr, data->maxlen);
1309         
1310         /* XXX solve generic */
1311         if (but->type == NUM || but->type == NUMSLI)
1312                 startx += (int)(0.5f * (BLI_RCT_SIZE_Y(&but->rect)));
1313         else if (ELEM(but->type, TEX, SEARCH_MENU)) {
1314                 startx += 5;
1315                 if (but->flag & UI_HAS_ICON)
1316                         startx += UI_DPI_ICON_SIZE;
1317         }
1318         
1319         /* mouse dragged outside the widget to the left */
1320         if (x < startx && but->ofs > 0) {       
1321                 int i = but->ofs;
1322
1323                 origstr[but->ofs] = 0;
1324                 
1325                 while (i > 0) {
1326                         if (BLI_str_cursor_step_prev_utf8(origstr, but->ofs, &i)) {
1327                                 /* 0.25 == scale factor for less sensitivity */
1328                                 if (BLF_width(fstyle->uifont_id, origstr + i) > (startx - x) * 0.25f) {
1329                                         break;
1330                                 }
1331                         }
1332                         else {
1333                                 break; /* unlikely but possible */
1334                         }
1335                 }
1336                 but->ofs = i;
1337                 but->pos = but->ofs;
1338         }
1339         /* mouse inside the widget */
1340         else if (x >= startx) {
1341                 int pos_i;
1342
1343                 /* keep track of previous distance from the cursor to the char */
1344                 float cdist, cdist_prev = 0.0f;
1345                 short pos_prev;
1346
1347                 const float aspect_sqrt = sqrtf(but->block->aspect);
1348                 
1349                 but->pos = pos_prev = strlen(origstr) - but->ofs;
1350
1351                 while (TRUE) {
1352                         /* XXX does not take zoom level into account */
1353                         cdist = startx + aspect_sqrt *BLF_width(fstyle->uifont_id, origstr + but->ofs);
1354
1355                         /* check if position is found */
1356                         if (cdist < x) {
1357                                 /* check is previous location was in fact closer */
1358                                 if (((float)x - cdist) > (cdist_prev - (float)x)) {
1359                                         but->pos = pos_prev;
1360                                 }
1361                                 break;
1362                         }
1363                         cdist_prev = cdist;
1364                         pos_prev   = but->pos;
1365                         /* done with tricky distance checks */
1366
1367                         pos_i = but->pos;
1368                         if (but->pos <= 0) break;
1369                         if (BLI_str_cursor_step_prev_utf8(origstr, but->ofs, &pos_i)) {
1370                                 but->pos = pos_i;
1371                                 origstr[but->pos + but->ofs] = 0;
1372                         }
1373                         else {
1374                                 break; /* unlikely but possible */
1375                         }
1376                 }
1377                 but->pos += but->ofs;
1378                 if (but->pos < 0) but->pos = 0;
1379         }
1380         
1381         if (fstyle->kerning == 1)
1382                 BLF_disable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
1383         
1384         MEM_freeN(origstr);
1385 }
1386
1387 static void ui_textedit_set_cursor_select(uiBut *but, uiHandleButtonData *data, short x)
1388 {
1389         if (x > data->selstartx) data->selextend = EXTEND_RIGHT;
1390         else if (x < data->selstartx) data->selextend = EXTEND_LEFT;
1391
1392         ui_textedit_set_cursor_pos(but, data, x);
1393
1394         if (data->selextend == EXTEND_RIGHT) but->selend = but->pos;
1395         if (data->selextend == EXTEND_LEFT) but->selsta = but->pos;
1396
1397         ui_check_but(but);
1398 }
1399
1400 /* this is used for both utf8 and ascii, its meant to be used for single keys,
1401  * notice the buffer is either copied or not, so its not suitable for pasting in
1402  * - campbell */
1403 static int ui_textedit_type_buf(uiBut *but, uiHandleButtonData *data,
1404                                 const char *utf8_buf, int utf8_buf_len)
1405 {
1406         char *str;
1407         int len, changed = 0;
1408
1409         str = data->str;
1410         len = strlen(str);
1411
1412         if (len - (but->selend - but->selsta) + 1 <= data->maxlen) {
1413                 int step = utf8_buf_len;
1414
1415                 /* type over the current selection */
1416                 if ((but->selend - but->selsta) > 0) {
1417                         changed = ui_textedit_delete_selection(but, data);
1418                         len = strlen(str);
1419                 }
1420
1421                 if (len + step < data->maxlen) {
1422                         memmove(&str[but->pos + step], &str[but->pos], (len + 1) - but->pos);
1423                         memcpy(&str[but->pos], utf8_buf, step * sizeof(char));
1424                         but->pos += step;
1425                         changed = 1;
1426                 }
1427         }
1428
1429         return changed;
1430 }
1431
1432 static int ui_textedit_type_ascii(uiBut *but, uiHandleButtonData *data, char ascii)
1433 {
1434         char buf[2] = {ascii, '\0'};
1435
1436         if (ui_is_but_utf8(but) && (BLI_str_utf8_size(buf) == -1)) {
1437                 printf("%s: entering invalid ascii char into an ascii key (%d)\n",
1438                        __func__, (int)(unsigned char)ascii);
1439
1440                 return 0;
1441         }
1442
1443         /* in some cases we want to allow invalid utf8 chars */
1444         return ui_textedit_type_buf(but, data, buf, 1);
1445 }
1446
1447 static void ui_textedit_move(uiBut *but, uiHandleButtonData *data, strCursorJumpDirection direction,
1448                              int select, strCursorJumpType jump)
1449 {
1450         const char *str = data->str;
1451         const int len = strlen(str);
1452         const int pos_prev = but->pos;
1453         const int has_sel = (but->selend - but->selsta) > 0;
1454
1455         ui_check_but(but);
1456
1457         /* special case, quit selection and set cursor */
1458         if (has_sel && !select) {
1459                 if (jump == STRCUR_JUMP_ALL) {
1460                         but->selsta = but->selend = but->pos = direction ? len : 0;
1461                 }
1462                 else {
1463                         if (direction) {
1464                                 but->selsta = but->pos = but->selend;
1465                         }
1466                         else {
1467                                 but->pos = but->selend = but->selsta;
1468                         }
1469                 }
1470                 data->selextend = 0;
1471         }
1472         else {
1473                 int pos_i = but->pos;
1474                 BLI_str_cursor_step_utf8(str, len, &pos_i, direction, jump);
1475                 but->pos = pos_i;
1476
1477                 if (select) {
1478                         /* existing selection */
1479                         if (has_sel) {
1480
1481                                 if (data->selextend == 0) {
1482                                         data->selextend = EXTEND_RIGHT;
1483                                 }
1484
1485                                 if (direction) {
1486                                         if (data->selextend == EXTEND_RIGHT) {
1487                                                 but->selend = but->pos;
1488                                         }
1489                                         else {
1490                                                 but->selsta = but->pos;
1491                                         }
1492                                 }
1493                                 else {
1494                                         if (data->selextend == EXTEND_LEFT) {
1495                                                 but->selsta = but->pos;
1496                                         }
1497                                         else {
1498                                                 but->selend = but->pos;
1499                                         }
1500                                 }
1501
1502                                 if (but->selend < but->selsta) {
1503                                         SWAP(short, but->selsta, but->selend);
1504                                         data->selextend = (data->selextend == EXTEND_RIGHT) ? EXTEND_LEFT : EXTEND_RIGHT;
1505                                 }
1506
1507                         } /* new selection */
1508                         else {
1509                                 if (direction) {
1510                                         data->selextend = EXTEND_RIGHT;
1511                                         but->selend = but->pos;
1512                                         but->selsta = pos_prev;
1513                                 }
1514                                 else {
1515                                         data->selextend = EXTEND_LEFT;
1516                                         but->selend = pos_prev;
1517                                         but->selsta = but->pos;
1518                                 }
1519                         }
1520                 }
1521         }
1522 }
1523
1524 static int ui_textedit_delete(uiBut *but, uiHandleButtonData *data, int direction, strCursorJumpType jump)
1525 {
1526         char *str = data->str;
1527         const int len = strlen(str);
1528
1529         int changed = 0;
1530
1531         if (jump == STRCUR_JUMP_ALL) {
1532                 if (len) changed = 1;
1533                 str[0] = '\0';
1534                 but->pos = 0;
1535         }
1536         else if (direction) { /* delete */
1537                 if ((but->selend - but->selsta) > 0) {
1538                         changed = ui_textedit_delete_selection(but, data);
1539                 }
1540                 else if (but->pos >= 0 && but->pos < len) {
1541                         int pos = but->pos;
1542                         int step;
1543                         BLI_str_cursor_step_utf8(str, len, &pos, direction, jump);
1544                         step = pos - but->pos;
1545                         memmove(&str[but->pos], &str[but->pos + step], (len + 1) - but->pos);
1546                         changed = 1;
1547                 }
1548         }
1549         else { /* backspace */
1550                 if (len != 0) {
1551                         if ((but->selend - but->selsta) > 0) {
1552                                 changed = ui_textedit_delete_selection(but, data);
1553                         }
1554                         else if (but->pos > 0) {
1555                                 int pos = but->pos;
1556                                 int step;
1557
1558                                 BLI_str_cursor_step_utf8(str, len, &pos, direction, jump);
1559                                 step = but->pos - pos;
1560                                 memmove(&str[but->pos - step], &str[but->pos], (len + 1) - but->pos);
1561                                 but->pos -= step;
1562                                 changed = 1;
1563                         }
1564                 } 
1565         }
1566
1567         return changed;
1568 }
1569
1570 static int ui_textedit_autocomplete(bContext *C, uiBut *but, uiHandleButtonData *data)
1571 {
1572         char *str;
1573         int changed = 1;
1574
1575         str = data->str;
1576
1577         if (data->searchbox)
1578                 ui_searchbox_autocomplete(C, data->searchbox, but, data->str);
1579         else
1580                 but->autocomplete_func(C, str, but->autofunc_arg);
1581
1582         but->pos = strlen(str);
1583         but->selsta = but->selend = but->pos;
1584
1585         return changed;
1586 }
1587
1588 static int ui_textedit_copypaste(uiBut *but, uiHandleButtonData *data, int paste, int copy, int cut)
1589 {
1590         char buf[UI_MAX_DRAW_STR] = {0};
1591         char *str, *p, *pbuf;
1592         int len, x, i, changed = 0;
1593
1594         str = data->str;
1595         len = strlen(str);
1596         
1597         /* paste */
1598         if (paste) {
1599                 /* extract the first line from the clipboard */
1600                 p = pbuf = WM_clipboard_text_get(0);
1601
1602                 if (p && p[0]) {
1603                         unsigned int y;
1604                         i = 0;
1605                         while (*p && *p != '\r' && *p != '\n' && i < UI_MAX_DRAW_STR - 1) {
1606                                 buf[i++] = *p;
1607                                 p++;
1608                         }
1609                         buf[i] = 0;
1610
1611                         /* paste over the current selection */
1612                         if ((but->selend - but->selsta) > 0) {
1613                                 ui_textedit_delete_selection(but, data);
1614                                 len = strlen(str);
1615                         }
1616                         
1617                         for (y = 0; y < strlen(buf); y++) {
1618                                 /* add contents of buffer */
1619                                 if (len + 1 < data->maxlen) {
1620                                         for (x = data->maxlen; x > but->pos; x--)
1621                                                 str[x] = str[x - 1];
1622                                         str[but->pos] = buf[y];
1623                                         but->pos++; 
1624                                         len++;
1625                                         str[len] = '\0';
1626                                 }
1627                         }
1628
1629                         changed = 1;
1630                 }
1631
1632                 if (pbuf) {
1633                         MEM_freeN(pbuf);
1634                 }
1635         }
1636         /* cut & copy */
1637         else if (copy || cut) {
1638                 /* copy the contents to the copypaste buffer */
1639                 for (x = but->selsta; x <= but->selend; x++) {
1640                         if (x == but->selend)
1641                                 buf[x] = '\0';
1642                         else
1643                                 buf[(x - but->selsta)] = str[x];
1644                 }
1645
1646                 WM_clipboard_text_set(buf, 0);
1647                 
1648                 /* for cut only, delete the selection afterwards */
1649                 if (cut)
1650                         if ((but->selend - but->selsta) > 0)
1651                                 changed = ui_textedit_delete_selection(but, data);
1652         } 
1653
1654         return changed;
1655 }
1656
1657 static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
1658 {
1659         int len;
1660
1661         if (data->str) {
1662                 MEM_freeN(data->str);
1663                 data->str = NULL;
1664         }
1665
1666         /* retrieve string */
1667         data->maxlen = ui_get_but_string_max_length(but);
1668         data->str = MEM_callocN(sizeof(char) * data->maxlen + 1, "textedit str");
1669         ui_get_but_string(but, data->str, data->maxlen);
1670
1671         if (ELEM3(but->type, NUM, NUMABS, NUMSLI)) {
1672                 ui_convert_to_unit_alt_name(but, data->str, data->maxlen);
1673         }
1674
1675         /* won't change from now on */
1676         len = strlen(data->str);
1677
1678         data->origstr = BLI_strdupn(data->str, len);
1679         data->selextend = 0;
1680         data->selstartx = 0;
1681
1682         /* set cursor pos to the end of the text */
1683         but->editstr = data->str;
1684         but->pos = len;
1685         but->selsta = 0;
1686         but->selend = len;
1687
1688         /* optional searchbox */
1689         if (but->type == SEARCH_MENU) {
1690                 data->searchbox = ui_searchbox_create(C, data->region, but);
1691                 ui_searchbox_update(C, data->searchbox, but, 1); /* 1= reset */
1692         }
1693         
1694         ui_check_but(but);
1695         
1696         WM_cursor_modal(CTX_wm_window(C), BC_TEXTEDITCURSOR);
1697 }
1698
1699 static void ui_textedit_end(bContext *C, uiBut *but, uiHandleButtonData *data)
1700 {
1701         if (but) {
1702                 if (ui_is_but_utf8(but)) {
1703                         int strip = BLI_utf8_invalid_strip(but->editstr, strlen(but->editstr));
1704                         /* not a file?, strip non utf-8 chars */
1705                         if (strip) {
1706                                 /* wont happen often so isn't that annoying to keep it here for a while */
1707                                 printf("%s: invalid utf8 - stripped chars %d\n", __func__, strip);
1708                         }
1709                 }
1710                 
1711                 if (data->searchbox) {
1712                         if (data->cancel == 0)
1713                                 ui_searchbox_apply(but, data->searchbox);
1714
1715                         ui_searchbox_free(C, data->searchbox);
1716                         data->searchbox = NULL;
1717                 }
1718                 
1719                 but->editstr = NULL;
1720                 but->pos = -1;
1721         }
1722         
1723         WM_cursor_restore(CTX_wm_window(C));
1724 }
1725
1726 static void ui_textedit_next_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1727 {
1728         uiBut *but;
1729
1730         /* label and roundbox can overlap real buttons (backdrops...) */
1731         if (ELEM4(actbut->type, LABEL, SEPR, ROUNDBOX, LISTBOX))
1732                 return;
1733
1734         for (but = actbut->next; but; but = but->next) {
1735                 if (ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1736                         if (!(but->flag & UI_BUT_DISABLED)) {
1737                                 data->postbut = but;
1738                                 data->posttype = BUTTON_ACTIVATE_TEXT_EDITING;
1739                                 return;
1740                         }
1741                 }
1742         }
1743         for (but = block->buttons.first; but != actbut; 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 }
1753
1754 static void ui_textedit_prev_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1755 {
1756         uiBut *but;
1757
1758         /* label and roundbox can overlap real buttons (backdrops...) */
1759         if (ELEM4(actbut->type, LABEL, SEPR, ROUNDBOX, LISTBOX))
1760                 return;
1761
1762         for (but = actbut->prev; but; but = but->prev) {
1763                 if (ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1764                         if (!(but->flag & UI_BUT_DISABLED)) {
1765                                 data->postbut = but;
1766                                 data->posttype = BUTTON_ACTIVATE_TEXT_EDITING;
1767                                 return;
1768                         }
1769                 }
1770         }
1771         for (but = block->buttons.last; but != actbut; 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 }
1781
1782
1783 static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1784 {
1785         int mx, my, changed = 0, inbox = 0, update = 0, retval = WM_UI_HANDLER_CONTINUE;
1786
1787         switch (event->type) {
1788                 case WHEELUPMOUSE:
1789                 case WHEELDOWNMOUSE:
1790                 case MOUSEMOVE:
1791                         if (data->searchbox)
1792                                 ui_searchbox_event(C, data->searchbox, but, event);
1793                         
1794                         break;
1795                 case RIGHTMOUSE:
1796                 case ESCKEY:
1797                         data->cancel = TRUE;
1798                         data->escapecancel = TRUE;
1799                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1800                         retval = WM_UI_HANDLER_BREAK;
1801                         break;
1802                 case LEFTMOUSE:
1803                 {
1804                         /* exit on LMB only on RELEASE for searchbox, to mimic other popups, and allow multiple menu levels */
1805                         if (data->searchbox)
1806                                 inbox = ui_searchbox_inside(data->searchbox, event->x, event->y);
1807
1808                         if (event->val == KM_PRESS) {
1809                                 mx = event->x;
1810                                 my = event->y;
1811                                 ui_window_to_block(data->region, block, &mx, &my);
1812
1813                                 if (ui_but_contains_pt(but, mx, my)) {
1814                                         ui_textedit_set_cursor_pos(but, data, mx);
1815                                         but->selsta = but->selend = but->pos;
1816                                         data->selstartx = mx;
1817
1818                                         button_activate_state(C, but, BUTTON_STATE_TEXT_SELECTING);
1819                                         retval = WM_UI_HANDLER_BREAK;
1820                                 }
1821                                 else if (inbox == 0) {
1822                                         /* if searchbox, click outside will cancel */
1823                                         if (data->searchbox)
1824                                                 data->cancel = data->escapecancel = TRUE;
1825                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1826                                         retval = WM_UI_HANDLER_BREAK;
1827                                 }
1828                         }
1829                         else if (inbox) {
1830                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1831                                 retval = WM_UI_HANDLER_BREAK;
1832                         }
1833                         break;
1834                 }
1835         }
1836
1837         if (event->val == KM_PRESS) {
1838                 switch (event->type) {
1839                         case VKEY:
1840                         case XKEY:
1841                         case CKEY:
1842                                 if (event->ctrl || event->oskey) {
1843                                         if (event->type == VKEY)
1844                                                 changed = ui_textedit_copypaste(but, data, 1, 0, 0);
1845                                         else if (event->type == CKEY)
1846                                                 changed = ui_textedit_copypaste(but, data, 0, 1, 0);
1847                                         else if (event->type == XKEY)
1848                                                 changed = ui_textedit_copypaste(but, data, 0, 0, 1);
1849
1850                                         retval = WM_UI_HANDLER_BREAK;
1851                                 }
1852                                 break;
1853                         case RIGHTARROWKEY:
1854                                 ui_textedit_move(but, data, STRCUR_DIR_NEXT,
1855                                                  event->shift, event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE);
1856                                 retval = WM_UI_HANDLER_BREAK;
1857                                 break;
1858                         case LEFTARROWKEY:
1859                                 ui_textedit_move(but, data, STRCUR_DIR_PREV,
1860                                                  event->shift, event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE);
1861                                 retval = WM_UI_HANDLER_BREAK;
1862                                 break;
1863                         case DOWNARROWKEY:
1864                                 if (data->searchbox) {
1865                                         ui_searchbox_event(C, data->searchbox, but, event);
1866                                         break;
1867                                 }
1868                         /* pass on purposedly */
1869                         case ENDKEY:
1870                                 ui_textedit_move(but, data, STRCUR_DIR_NEXT,
1871                                                  event->shift, STRCUR_JUMP_ALL);
1872                                 retval = WM_UI_HANDLER_BREAK;
1873                                 break;
1874                         case UPARROWKEY:
1875                                 if (data->searchbox) {
1876                                         ui_searchbox_event(C, data->searchbox, but, event);
1877                                         break;
1878                                 }
1879                         /* pass on purposedly */
1880                         case HOMEKEY:
1881                                 ui_textedit_move(but, data, STRCUR_DIR_PREV,
1882                                                  event->shift, STRCUR_JUMP_ALL);
1883                                 retval = WM_UI_HANDLER_BREAK;
1884                                 break;
1885                         case PADENTER:
1886                         case RETKEY:
1887                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1888                                 retval = WM_UI_HANDLER_BREAK;
1889                                 break;
1890                         case DELKEY:
1891                                 changed = ui_textedit_delete(but, data, 1,
1892                                                              event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE);
1893                                 retval = WM_UI_HANDLER_BREAK;
1894                                 break;
1895
1896                         case BACKSPACEKEY:
1897                                 changed = ui_textedit_delete(but, data, 0,
1898                                                              event->shift ? STRCUR_JUMP_ALL : (event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE));
1899                                 retval = WM_UI_HANDLER_BREAK;
1900                                 break;
1901                                 
1902                         case TABKEY:
1903                                 /* there is a key conflict here, we can't tab with autocomplete */
1904                                 if (but->autocomplete_func || data->searchbox) {
1905                                         changed = ui_textedit_autocomplete(C, but, data);
1906                                         update = 1; /* do live update for tab key */
1907                                 }
1908                                 /* the hotkey here is not well defined, was G.qual so we check all */
1909                                 else if (event->shift || event->ctrl || event->alt || event->oskey) {
1910                                         ui_textedit_prev_but(block, but, data);
1911                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1912                                 }
1913                                 else {
1914                                         ui_textedit_next_but(block, but, data);
1915                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1916                                 }
1917                                 retval = WM_UI_HANDLER_BREAK;
1918                                 break;
1919                 }
1920
1921                 if ((event->ascii || event->utf8_buf[0]) && (retval == WM_UI_HANDLER_CONTINUE)) {
1922                         char ascii = event->ascii;
1923                         const char *utf8_buf = event->utf8_buf;
1924
1925                         /* exception that's useful for number buttons, some keyboard
1926                          * numpads have a comma instead of a period */
1927                         if (ELEM3(but->type, NUM, NUMABS, NUMSLI)) { /* could use data->min*/
1928                                 if (event->type == PADPERIOD && ascii == ',') {
1929                                         ascii = '.';
1930                                         utf8_buf = NULL; /* force ascii fallback */
1931                                 }
1932                         }
1933
1934                         if (utf8_buf && utf8_buf[0]) {
1935                                 int utf8_buf_len = BLI_str_utf8_size(utf8_buf);
1936                                 /* keep this printf until utf8 is well tested */
1937                                 if (utf8_buf_len != 1) {
1938                                         printf("%s: utf8 char '%.*s'\n", __func__, utf8_buf_len, utf8_buf);
1939                                 }
1940
1941                                 // strcpy(utf8_buf, "12345");
1942                                 changed = ui_textedit_type_buf(but, data, event->utf8_buf, utf8_buf_len);
1943                         }
1944                         else {
1945                                 changed = ui_textedit_type_ascii(but, data, ascii);
1946                         }
1947
1948                         retval = WM_UI_HANDLER_BREAK;
1949                         
1950                 }
1951                 /* textbutton with magnifier icon: do live update for search button */
1952                 if (but->icon == ICON_VIEWZOOM)
1953                         update = 1;
1954         }
1955
1956         if (changed) {
1957                 /* only update when typing for TAB key */
1958                 if (update && data->interactive) ui_apply_button(C, block, but, data, 1);
1959                 else ui_check_but(but);
1960                 but->changed = TRUE;
1961                 
1962                 if (data->searchbox)
1963                         ui_searchbox_update(C, data->searchbox, but, 1);  /* 1 = reset */
1964         }
1965
1966         if (changed || (retval == WM_UI_HANDLER_BREAK))
1967                 ED_region_tag_redraw(data->region);
1968 }
1969
1970 static void ui_do_but_textedit_select(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1971 {
1972         int mx, my, retval = WM_UI_HANDLER_CONTINUE;
1973
1974         switch (event->type) {
1975                 case MOUSEMOVE:
1976                 {
1977                         mx = event->x;
1978                         my = event->y;
1979                         ui_window_to_block(data->region, block, &mx, &my);
1980
1981                         ui_textedit_set_cursor_select(but, data, mx);
1982                         retval = WM_UI_HANDLER_BREAK;
1983                         break;
1984                 }
1985                 case LEFTMOUSE:
1986                         if (event->val == KM_RELEASE)
1987                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1988                         retval = WM_UI_HANDLER_BREAK;
1989                         break;
1990         }
1991
1992         if (retval == WM_UI_HANDLER_BREAK) {
1993                 ui_check_but(but);
1994                 ED_region_tag_redraw(data->region);
1995         }
1996 }
1997
1998 /* ************* number editing for various types ************* */
1999
2000 static void ui_numedit_begin(uiBut *but, uiHandleButtonData *data)
2001 {
2002         if (but->type == BUT_CURVE) {
2003                 but->editcumap = (CurveMapping *)but->poin;
2004         }
2005         else if (but->type == BUT_COLORBAND) {
2006                 data->coba = (ColorBand *)but->poin;
2007                 but->editcoba = data->coba;
2008         }
2009         else if (ELEM3(but->type, BUT_NORMAL, HSVCUBE, HSVCIRCLE)) {
2010                 ui_get_but_vectorf(but, data->origvec);
2011                 copy_v3_v3(data->vec, data->origvec);
2012                 but->editvec = data->vec;
2013         }
2014         else {
2015                 float softrange, softmin, softmax;
2016
2017                 data->startvalue = ui_get_but_val(but);
2018                 data->origvalue = data->startvalue;
2019                 data->value = data->origvalue;
2020                 but->editval = &data->value;
2021
2022                 softmin = but->softmin;
2023                 softmax = but->softmax;
2024                 softrange = softmax - softmin;
2025
2026                 data->dragfstart = (softrange == 0.0f) ? 0.0f : ((float)data->value - softmin) / softrange;
2027                 data->dragf = data->dragfstart;
2028         }
2029
2030         data->dragchange = 0;
2031         data->draglock = 1;
2032 }
2033
2034 static void ui_numedit_end(uiBut *but, uiHandleButtonData *data)
2035 {
2036         but->editval = NULL;
2037         but->editvec = NULL;
2038         but->editcoba = NULL;
2039         but->editcumap = NULL;
2040
2041         data->dragstartx = 0;
2042         data->draglastx = 0;
2043         data->dragchange = 0;
2044         data->dragcbd = NULL;
2045         data->dragsel = 0;
2046 }
2047
2048 static void ui_numedit_apply(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data)
2049 {
2050         if (data->interactive) ui_apply_button(C, block, but, data, 1);
2051         else ui_check_but(but);
2052
2053         ED_region_tag_redraw(data->region);
2054 }
2055
2056 /* ****************** menu opening for various types **************** */
2057
2058 static void ui_blockopen_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
2059 {
2060         uiBlockCreateFunc func = NULL;
2061         uiBlockHandleCreateFunc handlefunc = NULL;
2062         uiMenuCreateFunc menufunc = NULL;
2063         char *menustr = NULL;
2064         void *arg = NULL;
2065
2066         switch (but->type) {
2067                 case BLOCK:
2068                 case PULLDOWN:
2069                         if (but->menu_create_func) {
2070                                 menufunc = but->menu_create_func;
2071                                 arg = but->poin;
2072                         }
2073                         else {
2074                                 func = but->block_create_func;
2075                                 arg = but->poin ? but->poin : but->func_argN;
2076                         }
2077                         break;
2078                 case MENU:
2079                         if (but->menu_create_func) {
2080                                 menufunc = but->menu_create_func;
2081                                 arg = but->poin;
2082                         }
2083                         else {
2084                                 data->origvalue = ui_get_but_val(but);
2085                                 data->value = data->origvalue;
2086                                 but->editval = &data->value;
2087
2088                                 menustr = but->str;
2089                         }
2090                         break;
2091                 case ICONROW:
2092                         menufunc = ui_block_func_ICONROW;
2093                         arg = but;
2094                         break;
2095                 case ICONTEXTROW:
2096                         menufunc = ui_block_func_ICONTEXTROW;
2097                         arg = but;
2098                         break;
2099                 case COLOR:
2100                         ui_get_but_vectorf(but, data->origvec);
2101                         copy_v3_v3(data->vec, data->origvec);
2102                         but->editvec = data->vec;
2103
2104                         handlefunc = ui_block_func_COLOR;
2105                         arg = but;
2106                         break;
2107
2108                         /* quiet warnings for unhandled types */
2109                 default:
2110                         break;
2111         }
2112
2113         if (func || handlefunc) {
2114                 data->menu = ui_popup_block_create(C, data->region, but, func, handlefunc, arg);
2115                 if (but->block->handle)
2116                         data->menu->popup = but->block->handle->popup;
2117         }
2118         else if (menufunc || menustr) {
2119                 data->menu = ui_popup_menu_create(C, data->region, but, menufunc, arg, menustr);
2120                 if (but->block->handle)
2121                         data->menu->popup = but->block->handle->popup;
2122         }
2123
2124         /* this makes adjacent blocks auto open from now on */
2125         //if (but->block->auto_open ==0 ) but->block->auto_open = 1;
2126 }
2127
2128 static void ui_blockopen_end(bContext *C, uiBut *but, uiHandleButtonData *data)
2129 {
2130         if (but) {
2131                 but->editval = NULL;
2132                 but->editvec = NULL;
2133
2134                 but->block->auto_open_last = PIL_check_seconds_timer();
2135         }
2136
2137         if (data->menu) {
2138                 ui_popup_block_free(C, data->menu);
2139                 data->menu = NULL;
2140         }
2141 }
2142
2143 /* ***************** events for different button types *************** */
2144
2145 static int ui_do_but_BUT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2146 {
2147         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2148                 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
2149                         button_activate_state(C, but, BUTTON_STATE_WAIT_RELEASE);
2150                         return WM_UI_HANDLER_BREAK;
2151                 }
2152                 else if (event->type == LEFTMOUSE && but->block->handle) {
2153                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2154                         return WM_UI_HANDLER_BREAK;
2155                 }
2156                 else if (ELEM(event->type, PADENTER, RETKEY) && event->val == KM_PRESS) {
2157                         button_activate_state(C, but, BUTTON_STATE_WAIT_FLASH);
2158                         return WM_UI_HANDLER_BREAK;
2159                 }
2160         }
2161         else if (data->state == BUTTON_STATE_WAIT_RELEASE) {
2162                 if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
2163                         if (!(but->flag & UI_SELECT))
2164                                 data->cancel = TRUE;
2165                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2166                         return WM_UI_HANDLER_BREAK;
2167                 }
2168         }
2169
2170         return WM_UI_HANDLER_CONTINUE;
2171 }
2172
2173 static int ui_do_but_HOTKEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2174 {
2175         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2176                 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2177                         but->drawstr[0] = 0;
2178                         but->modifier_key = 0;
2179                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
2180                         return WM_UI_HANDLER_BREAK;
2181                 }
2182         }
2183         else if (data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2184                 
2185                 if (event->type == MOUSEMOVE)
2186                         return WM_UI_HANDLER_CONTINUE;
2187                 
2188                 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
2189                         /* only cancel if click outside the button */
2190                         if (ui_mouse_inside_button(but->active->region, but, event->x, event->y) == 0) {
2191                                 /* data->cancel doesnt work, this button opens immediate */
2192                                 if (but->flag & UI_BUT_IMMEDIATE)
2193                                         ui_set_but_val(but, 0);
2194                                 else
2195                                         data->cancel = TRUE;
2196                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2197                                 return WM_UI_HANDLER_BREAK;
2198                         }
2199                 }
2200                 
2201                 /* always set */
2202                 but->modifier_key = 0;
2203                 if (event->shift) but->modifier_key |= KM_SHIFT;
2204                 if (event->alt) but->modifier_key |= KM_ALT;
2205                 if (event->ctrl) but->modifier_key |= KM_CTRL;
2206                 if (event->oskey) but->modifier_key |= KM_OSKEY;
2207
2208                 ui_check_but(but);
2209                 ED_region_tag_redraw(data->region);
2210                         
2211                 if (event->val == KM_PRESS) {
2212                         if (ISHOTKEY(event->type)) { 
2213                                 
2214                                 if (WM_key_event_string(event->type)[0])
2215                                         ui_set_but_val(but, event->type);
2216                                 else
2217                                         data->cancel = TRUE;
2218                                 
2219                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2220                                 return WM_UI_HANDLER_BREAK;
2221                         }
2222                         else if (event->type == ESCKEY) {
2223                                 data->cancel = TRUE;
2224                                 data->escapecancel = TRUE;
2225                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2226                         }
2227                         
2228                 }
2229         }
2230         
2231         return WM_UI_HANDLER_CONTINUE;
2232 }
2233
2234 static int ui_do_but_KEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2235 {
2236         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2237                 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2238                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
2239                         return WM_UI_HANDLER_BREAK;
2240                 }
2241         }
2242         else if (data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2243                 if (event->type == MOUSEMOVE)
2244                         return WM_UI_HANDLER_CONTINUE;
2245
2246                 if (event->val == KM_PRESS) {
2247                         if (WM_key_event_string(event->type)[0])
2248                                 ui_set_but_val(but, event->type);
2249                         else
2250                                 data->cancel = TRUE;
2251
2252                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2253                 }
2254         }
2255
2256         return WM_UI_HANDLER_CONTINUE;
2257 }
2258
2259 static int ui_do_but_TEX(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2260 {
2261         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2262                 if (ELEM(event->type, LEFTMOUSE, EVT_BUT_OPEN) && event->val == KM_PRESS) {
2263                         if (but->dt == UI_EMBOSSN && !event->ctrl) ;
2264                         else {
2265                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2266                                 return WM_UI_HANDLER_BREAK;
2267                         }
2268                 }
2269         }
2270         else if (data->state == BUTTON_STATE_TEXT_EDITING) {
2271                 ui_do_but_textedit(C, block, but, data, event);
2272                 return WM_UI_HANDLER_BREAK;
2273         }
2274         else if (data->state == BUTTON_STATE_TEXT_SELECTING) {
2275                 ui_do_but_textedit_select(C, block, but, data, event);
2276                 return WM_UI_HANDLER_BREAK;
2277         }
2278
2279         return WM_UI_HANDLER_CONTINUE;
2280 }
2281
2282 static int ui_do_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2283 {
2284         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2285                 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2286                         data->togdual = event->ctrl;
2287                         data->togonly = !event->shift;
2288                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2289                         return WM_UI_HANDLER_BREAK;
2290                 }
2291         }
2292         return WM_UI_HANDLER_CONTINUE;
2293 }
2294
2295 static int ui_do_but_EXIT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2296 {
2297         
2298         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2299
2300                 /* first handle click on icondrag type button */
2301                 if (event->type == LEFTMOUSE && but->dragpoin) {
2302                         if (ui_but_mouse_inside_icon(but, data->region, event)) {
2303                                 
2304                                 /* tell the button to wait and keep checking further events to
2305                                  * see if it should start dragging */
2306                                 button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
2307                                 data->dragstartx = event->x;
2308                                 data->dragstarty = event->y;
2309                                 return WM_UI_HANDLER_CONTINUE;
2310                         }
2311                 }
2312                 
2313                 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2314                         int ret = WM_UI_HANDLER_BREAK;
2315                         /* XXX (a bit ugly) Special case handling for filebrowser drag button */
2316                         if (but->dragpoin && but->imb && ui_but_mouse_inside_icon(but, data->region, event)) {
2317                                 ret = WM_UI_HANDLER_CONTINUE;
2318                         }
2319                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2320                         return ret;
2321                 }
2322         }
2323         else if (data->state == BUTTON_STATE_WAIT_DRAG) {
2324                 
2325                 /* this function also ends state */
2326                 if (ui_but_start_drag(C, but, data, event)) {
2327                         return WM_UI_HANDLER_BREAK;
2328                 }
2329                 
2330                 /* If the mouse has been pressed and released, getting to 
2331                  * this point without triggering a drag, then clear the 
2332                  * drag state for this button and continue to pass on the event */
2333                 if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
2334                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2335                         return WM_UI_HANDLER_CONTINUE;
2336                 }
2337                 
2338                 /* while waiting for a drag to be triggered, always block 
2339                  * other events from getting handled */
2340                 return WM_UI_HANDLER_BREAK;
2341         }
2342         
2343         return WM_UI_HANDLER_CONTINUE;
2344 }
2345
2346 /* var names match ui_numedit_but_NUM */
2347 static float ui_numedit_apply_snapf(uiBut *but, float tempf, float softmin, float softmax, float softrange, int snap)
2348 {
2349         if (tempf == softmin || tempf == softmax || snap == 0) {
2350                 /* pass */
2351         }
2352         else {
2353                 float fac = 1.0f;
2354                 
2355                 if (ui_is_but_unit(but)) {
2356                         UnitSettings *unit = but->block->unit;
2357                         int unit_type = RNA_SUBTYPE_UNIT_VALUE(uiButGetUnitType(but));
2358
2359                         if (bUnit_IsValid(unit->system, unit_type)) {
2360                                 fac = (float)bUnit_BaseScalar(unit->system, unit_type);
2361                                 if (ELEM3(unit_type, B_UNIT_LENGTH, B_UNIT_AREA, B_UNIT_VOLUME)) {
2362                                         fac /= unit->scale_length;
2363                                 }
2364                         }
2365                 }
2366
2367                 if (fac != 1.0f) {
2368                         /* snap in unit-space */
2369                         tempf /= fac;
2370                         /* softmin /= fac; */ /* UNUSED */
2371                         /* softmax /= fac; */ /* UNUSED */
2372                         softrange /= fac;
2373                 }
2374
2375                 if (snap == 1) {
2376                         if (softrange < 2.10f) tempf = 0.1f * floorf(10.0f * tempf);
2377                         else if (softrange < 21.0f) tempf = floorf(tempf);
2378                         else tempf = 10.0f * floorf(tempf / 10.0f);
2379                 }
2380                 else if (snap == 2) {
2381                         if (softrange < 2.10f) tempf = 0.01f * floorf(100.0f * tempf);
2382                         else if (softrange < 21.0f) tempf = 0.1f * floorf(10.0f * tempf);
2383                         else tempf = floor(tempf);
2384                 }
2385                 
2386                 if (fac != 1.0f)
2387                         tempf *= fac;
2388         }
2389
2390         return tempf;
2391 }
2392
2393 static float ui_numedit_apply_snap(int temp, float softmin, float softmax, int snap)
2394 {
2395         if (temp == softmin || temp == softmax)
2396                 return temp;
2397
2398         switch (snap) {
2399                 case 0:
2400                         break;
2401                 case 1:
2402                         temp = 10 * (temp / 10);
2403                         break;
2404                 case 2:
2405                         temp = 100 * (temp / 100);
2406                         break;
2407         }
2408
2409         return temp;
2410 }
2411
2412 static int ui_numedit_but_NUM(uiBut *but, uiHandleButtonData *data, float fac, int snap, int mx)
2413 {
2414         float deler, tempf, softmin, softmax, softrange;
2415         int lvalue, temp, changed = 0;
2416         
2417         if (mx == data->draglastx)
2418                 return changed;
2419         
2420         /* drag-lock - prevent unwanted scroll adjustments */
2421         /* change value (now 3) to adjust threshold in pixels */
2422         if (data->draglock) {
2423                 if (abs(mx - data->dragstartx) <= 3)
2424                         return changed;
2425
2426                 data->draglock = 0;
2427                 data->dragstartx = mx;  /* ignore mouse movement within drag-lock */
2428         }
2429
2430         softmin = but->softmin;
2431         softmax = but->softmax;
2432         softrange = softmax - softmin;
2433
2434         if (ui_is_a_warp_but(but)) {
2435                 /* Mouse location isn't screen clamped to the screen so use a linear mapping
2436                  * 2px == 1-int, or 1px == 1-ClickStep */
2437                 if (ui_is_but_float(but)) {
2438                         fac *= 0.01f * but->a1;
2439                         tempf = (float)data->startvalue + ((float)(mx - data->dragstartx) * fac);
2440                         tempf = ui_numedit_apply_snapf(but, tempf, softmin, softmax, softrange, snap);
2441
2442 #if 1       /* fake moving the click start, nicer for dragging back after passing the limit */
2443                         if (tempf < softmin) {
2444                                 data->dragstartx -= (softmin - tempf) / fac;
2445                                 tempf = softmin;
2446                         }
2447                         else if (tempf > softmax) {
2448                                 data->dragstartx += (tempf - softmax) / fac;
2449                                 tempf = softmax;
2450                         }
2451 #else
2452                         CLAMP(tempf, softmin, softmax);
2453 #endif
2454
2455                         if (tempf != (float)data->value) {
2456                                 data->dragchange = 1;
2457                                 data->value = tempf;
2458                                 changed = 1;
2459                         }
2460                 }
2461                 else {
2462                         if (softrange > 256) fac = 1.0;             /* 1px == 1 */
2463                         else if (softrange > 32) fac = 1.0 / 2.0;   /* 2px == 1 */
2464                         else fac = 1.0 / 16.0;                  /* 16px == 1? */
2465
2466                         temp = data->startvalue + (((double)mx - data->dragstartx) * (double)fac);
2467                         temp = ui_numedit_apply_snap(temp, softmin, softmax, snap);
2468
2469 #if 1       /* fake moving the click start, nicer for dragging back after passing the limit */
2470                         if (temp < softmin) {
2471                                 data->dragstartx -= (softmin - temp) / fac;
2472                                 temp = softmin;
2473                         }
2474                         else if (temp > softmax) {
2475                                 data->dragstartx += (temp - softmax) / fac;
2476                                 temp = softmax;
2477                         }
2478 #else
2479                         CLAMP(temp, softmin, softmax);
2480 #endif
2481
2482                         if (temp != data->value) {
2483                                 data->dragchange = 1;
2484                                 data->value = temp;
2485                                 changed = 1;
2486                         }
2487                 }
2488
2489                 data->draglastx = mx;
2490         }
2491         else {
2492                 /* Use a non-linear mapping of the mouse drag especially for large floats (normal behavior) */
2493                 deler = 500;
2494                 if (!ui_is_but_float(but)) {
2495                         /* prevent large ranges from getting too out of control */
2496                         if (softrange > 600) deler = powf(softrange, 0.75);
2497                         
2498                         if (softrange < 100) deler = 200.0;
2499                         if (softrange < 25) deler = 50.0;
2500                 }
2501                 deler /= fac;
2502
2503                 if (softrange > 11) {
2504                         /* non linear change in mouse input- good for high precicsion */
2505                         data->dragf += (((float)(mx - data->draglastx)) / deler) * (fabsf(data->dragstartx - mx) * 0.002f);
2506                 }
2507                 else if (softrange > 129) { /* only scale large int buttons */
2508                         /* non linear change in mouse input- good for high precicsionm ints need less fine tuning */
2509                         data->dragf += (((float)(mx - data->draglastx)) / deler) * (fabsf(data->dragstartx - mx) * 0.004f);
2510                 }
2511                 else {
2512                         /*no scaling */
2513                         data->dragf += ((float)(mx - data->draglastx)) / deler;
2514                 }
2515         
2516                 CLAMP(data->dragf, 0.0f, 1.0f);
2517                 data->draglastx = mx;
2518                 tempf = (softmin + data->dragf * softrange);
2519
2520
2521                 if (!ui_is_but_float(but)) {
2522                         temp = floorf(tempf + 0.5f);
2523
2524                         temp = ui_numedit_apply_snap(temp, softmin, softmax, snap);
2525
2526                         CLAMP(temp, softmin, softmax);
2527                         lvalue = (int)data->value;
2528                         
2529                         if (temp != lvalue) {
2530                                 data->dragchange = 1;
2531                                 data->value = (double)temp;
2532                                 changed = 1;
2533                         }
2534                 }
2535                 else {
2536                         temp = 0;
2537                         tempf = ui_numedit_apply_snapf(but, tempf, softmin, softmax, softrange, snap);
2538
2539                         CLAMP(tempf, softmin, softmax);
2540
2541                         if (tempf != (float)data->value) {
2542                                 data->dragchange = 1;
2543                                 data->value = tempf;
2544                                 changed = 1;
2545                         }
2546                 }
2547         }
2548
2549
2550         return changed;
2551 }
2552
2553 static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2554 {
2555         int mx, my; /* mouse location scaled to fit the UI */
2556         int screen_mx, screen_my; /* mouse location kept at screen pixel coords */
2557         int click = 0;
2558         int retval = WM_UI_HANDLER_CONTINUE;
2559         
2560         mx = screen_mx = event->x;
2561         my = screen_my = event->y;
2562
2563         ui_window_to_block(data->region, block, &mx, &my);
2564
2565         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2566                 /* XXX hardcoded keymap check.... */
2567                 if (event->type == WHEELDOWNMOUSE && event->alt) {
2568                         mx = but->rect.xmin;
2569                         click = 1;
2570                 }
2571                 else if (event->type == WHEELUPMOUSE && event->alt) {
2572                         mx = but->rect.xmax;
2573                         click = 1;
2574                 }
2575                 else if (event->val == KM_PRESS) {
2576                         if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2577                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2578                                 retval = WM_UI_HANDLER_BREAK;
2579                         }
2580                         else if (event->type == LEFTMOUSE) {
2581                                 data->dragstartx = data->draglastx = ui_is_a_warp_but(but) ? screen_mx : mx;
2582                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2583                                 retval = WM_UI_HANDLER_BREAK;
2584                         }
2585                         else if (ELEM(event->type, PADENTER, RETKEY) && event->val == KM_PRESS)
2586                                 click = 1;
2587                         else if (event->type == MINUSKEY && event->val == KM_PRESS) {
2588                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2589                                 data->value = -data->value;
2590                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2591                                 retval = WM_UI_HANDLER_BREAK;
2592                         }
2593                 }
2594                 
2595         }
2596         else if (data->state == BUTTON_STATE_NUM_EDITING) {
2597                 if (event->type == ESCKEY) {
2598                         data->cancel = TRUE;
2599                         data->escapecancel = TRUE;
2600                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2601                 }
2602                 else if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
2603                         if (data->dragchange)
2604                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2605                         else
2606                                 click = 1;
2607                 }
2608                 else if (event->type == MOUSEMOVE) {
2609                         float fac;
2610                         int snap;
2611
2612                         fac = 1.0f;
2613                         if (event->shift) fac /= 10.0f;
2614                         if (event->alt) fac /= 20.0f;
2615                         
2616                         snap = (event->ctrl) ? (event->shift) ? 2 : 1 : 0;
2617
2618                         if (ui_numedit_but_NUM(but, data, fac, snap, (ui_is_a_warp_but(but) ? screen_mx : mx)))
2619                                 ui_numedit_apply(C, block, but, data);
2620                 }
2621                 retval = WM_UI_HANDLER_BREAK;
2622         }
2623         else if (data->state == BUTTON_STATE_TEXT_EDITING) {
2624                 ui_do_but_textedit(C, block, but, data, event);
2625                 retval = WM_UI_HANDLER_BREAK;
2626         }
2627         else if (data->state == BUTTON_STATE_TEXT_SELECTING) {
2628                 ui_do_but_textedit_select(C, block, but, data, event);
2629                 retval = WM_UI_HANDLER_BREAK;
2630         }
2631         
2632         if (click) {
2633                 /* we can click on the side arrows to increment/decrement,
2634                  * or click inside to edit the value directly */
2635                 float tempf, softmin, softmax;
2636                 int temp;
2637
2638                 softmin = but->softmin;
2639                 softmax = but->softmax;
2640
2641                 if (!ui_is_but_float(but)) {
2642                         if (mx < (but->rect.xmin + BLI_RCT_SIZE_X(&but->rect) / 3 - 3)) {
2643                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2644
2645                                 temp = (int)data->value - 1;
2646                                 if (temp >= softmin && temp <= softmax)
2647                                         data->value = (double)temp;
2648                                 else
2649                                         data->cancel = TRUE;
2650
2651                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2652                         }
2653                         else if (mx > (but->rect.xmin + (2 * BLI_RCT_SIZE_X(&but->rect) / 3) + 3)) {
2654                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2655
2656                                 temp = (int)data->value + 1;
2657                                 if (temp >= softmin && temp <= softmax)
2658                                         data->value = (double)temp;
2659                                 else
2660                                         data->cancel = TRUE;
2661
2662                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2663                         }
2664                         else
2665                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2666                 }
2667                 else {
2668                         if (mx < (but->rect.xmin + BLI_RCT_SIZE_X(&but->rect) / 3 - 3)) {
2669                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2670
2671                                 tempf = (float)data->value - 0.01f * but->a1;
2672                                 if (tempf < softmin) tempf = softmin;
2673                                 data->value = tempf;
2674
2675                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2676                         }
2677                         else if (mx > but->rect.xmin + (2 * (BLI_RCT_SIZE_X(&but->rect) / 3) + 3)) {
2678                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2679
2680                                 tempf = (float)data->value + 0.01f * but->a1;
2681                                 if (tempf > softmax) tempf = softmax;
2682                                 data->value = tempf;
2683
2684                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2685                         }
2686                         else
2687                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2688                 }
2689
2690                 retval = WM_UI_HANDLER_BREAK;
2691         }
2692         
2693         return retval;
2694 }
2695
2696 static int ui_numedit_but_SLI(uiBut *but, uiHandleButtonData *data, const short shift, const short ctrl, int mx)
2697 {
2698         float deler, f, tempf, softmin, softmax, softrange;
2699         int temp, lvalue, changed = 0;
2700
2701         softmin = but->softmin;
2702         softmax = but->softmax;
2703         softrange = softmax - softmin;
2704
2705         if (but->type == NUMSLI) deler = (BLI_RCT_SIZE_X(&but->rect) - 5.0f * but->aspect);
2706         else if (but->type == HSVSLI) deler = (BLI_RCT_SIZE_X(&but->rect) / 2.0f - 5.0f * but->aspect);
2707         else if (but->type == SCROLL) {
2708                 int horizontal = (BLI_RCT_SIZE_X(&but->rect) > BLI_RCT_SIZE_Y(&but->rect));
2709                 float size = (horizontal) ? BLI_RCT_SIZE_X(&but->rect) : -BLI_RCT_SIZE_Y(&but->rect);
2710                 deler = size * (but->softmax - but->softmin) / (but->softmax - but->softmin + but->a1);
2711         }
2712         else deler = (BLI_RCT_SIZE_X(&but->rect) - 5.0f * but->aspect);
2713
2714         f = (float)(mx - data->dragstartx) / deler + data->dragfstart;
2715         
2716         if (shift)
2717                 f = (f - data->dragfstart) / 10.0f + data->dragfstart;
2718
2719         CLAMP(f, 0.0f, 1.0f);
2720         tempf = softmin + f * softrange;
2721         temp = floorf(tempf + 0.5f);
2722
2723         if (ctrl) {
2724                 if (tempf == softmin || tempf == softmax) ;
2725                 else if (ui_is_but_float(but)) {
2726
2727                         if (shift) {
2728                                 if (tempf == softmin || tempf == softmax) ;
2729                                 else if (softmax - softmin < 2.10f) tempf = 0.01f * floorf(100.0f * tempf);
2730                                 else if (softmax - softmin < 21.0f) tempf = 0.1f * floorf(10.0f * tempf);
2731                                 else tempf = floorf(tempf);
2732                         }
2733                         else {
2734                                 if (softmax - softmin < 2.10f) tempf = 0.1f * floorf(10.0f * tempf);
2735                                 else if (softmax - softmin < 21.0f) tempf = floorf(tempf);
2736                                 else tempf = 10.0f * floorf(tempf / 10.0f);
2737                         }
2738                 }
2739                 else {
2740                         temp = 10 * (temp / 10);
2741                         tempf = temp;
2742                 }
2743         }
2744
2745         if (!ui_is_but_float(but)) {
2746                 lvalue = floor(data->value + 0.5);
2747
2748                 CLAMP(temp, softmin, softmax);
2749
2750                 if (temp != lvalue) {
2751                         data->value = temp;
2752                         data->dragchange = 1;
2753                         changed = 1;
2754                 }
2755         }
2756         else {
2757                 CLAMP(tempf, softmin, softmax);
2758
2759                 if (tempf != (float)data->value) {
2760                         data->value = tempf;
2761                         data->dragchange = 1;
2762                         changed = 1;
2763                 }
2764         }
2765
2766         return changed;
2767 }
2768
2769 static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2770 {
2771         int mx, my, click = 0;
2772         int retval = WM_UI_HANDLER_CONTINUE;
2773
2774         mx = event->x;
2775         my = event->y;
2776         ui_window_to_block(data->region, block, &mx, &my);
2777
2778         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2779                 /* XXX hardcoded keymap check.... */
2780                 if (event->type == WHEELDOWNMOUSE && event->alt) {
2781                         mx = but->rect.xmin;
2782                         click = 2;
2783                 }
2784                 else if (event->type == WHEELUPMOUSE && event->alt) {
2785                         mx = but->rect.xmax;
2786                         click = 2;
2787                 }
2788                 else if (event->val == KM_PRESS) {
2789                         if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2790                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2791                                 retval = WM_UI_HANDLER_BREAK;
2792                         }
2793                         /* alt-click on sides to get "arrows" like in NUM buttons, and match wheel usage above */
2794                         else if (event->type == LEFTMOUSE && event->alt) {
2795                                 int halfpos = BLI_RCT_CENTER_X(&but->rect);
2796                                 click = 2;
2797                                 if (mx < halfpos)
2798                                         mx = but->rect.xmin;
2799                                 else
2800                                         mx = but->rect.xmax;
2801                         }
2802                         else if (event->type == LEFTMOUSE) {
2803                                 data->dragstartx = mx;
2804                                 data->draglastx = mx;
2805                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2806                                 retval = WM_UI_HANDLER_BREAK;
2807                         }
2808                         else if (ELEM(event->type, PADENTER, RETKEY) && event->val == KM_PRESS)
2809                                 click = 1;
2810                         else if (event->type == MINUSKEY && event->val == KM_PRESS) {
2811                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2812                                 data->value = -data->value;
2813                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2814                                 retval = WM_UI_HANDLER_BREAK;
2815                         }
2816                 }
2817         }
2818         else if (data->state == BUTTON_STATE_NUM_EDITING) {
2819                 if (event->type == ESCKEY) {
2820                         data->cancel = TRUE;
2821                         data->escapecancel = TRUE;
2822                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2823                 }
2824                 else if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
2825                         if (data->dragchange)
2826                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2827                         else
2828                                 click = 1;
2829                 }
2830                 else if (event->type == MOUSEMOVE) {
2831                         if (ui_numedit_but_SLI(but, data, event->shift, event->ctrl, mx))
2832                                 ui_numedit_apply(C, block, but, data);
2833                 }
2834                 retval = WM_UI_HANDLER_BREAK;
2835         }
2836         else if (data->state == BUTTON_STATE_TEXT_EDITING) {
2837                 ui_do_but_textedit(C, block, but, data, event);
2838                 retval = WM_UI_HANDLER_BREAK;
2839         }
2840         else if (data->state == BUTTON_STATE_TEXT_SELECTING) {
2841                 ui_do_but_textedit_select(C, block, but, data, event);
2842                 retval = WM_UI_HANDLER_BREAK;
2843         }
2844
2845         if (click) {
2846                 if (click == 2) {
2847                         /* nudge slider to the left or right */
2848                         float f, tempf, softmin, softmax, softrange;
2849                         int temp;
2850                         
2851                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2852                         
2853                         softmin = but->softmin;
2854                         softmax = but->softmax;
2855                         softrange = softmax - softmin;
2856                         
2857                         tempf = data->value;
2858                         temp = (int)data->value;
2859
2860 #if 0
2861                         if (but->type == SLI) {
2862                                 f = (float)(mx - but->rect.xmin) / (BLI_RCT_SIZE_X(&but->rect)); /* same as below */
2863                         }
2864                         else
2865 #endif
2866                         {
2867                                 f = (float)(mx - but->rect.xmin) / (BLI_RCT_SIZE_X(&but->rect));
2868                         }
2869                         
2870                         f = softmin + f * softrange;
2871                         
2872                         if (!ui_is_but_float(but)) {
2873                                 if (f < temp) temp--;
2874                                 else temp++;
2875                                 
2876                                 if (temp >= softmin && temp <= softmax)
2877                                         data->value = temp;
2878                                 else
2879                                         data->cancel = TRUE;
2880                         } 
2881                         else {
2882                                 if (f < tempf) tempf -= 0.01f;
2883                                 else tempf += 0.01f;
2884                                 
2885                                 if (tempf >= softmin && tempf <= softmax)
2886                                         data->value = tempf;
2887                                 else
2888                                         data->cancel = TRUE;
2889                         }
2890                         
2891                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2892                         retval = WM_UI_HANDLER_BREAK;
2893                 }
2894                 else {
2895                         /* edit the value directly */
2896                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2897                         retval = WM_UI_HANDLER_BREAK;
2898                 }
2899         }
2900         
2901         return retval;
2902 }
2903
2904 static int ui_do_but_SCROLL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2905 {
2906         int mx, my /*, click= 0 */;
2907         int retval = WM_UI_HANDLER_CONTINUE;
2908         int horizontal = (BLI_RCT_SIZE_X(&but->rect) > BLI_RCT_SIZE_Y(&but->rect));
2909         
2910         mx = event->x;
2911         my = event->y;
2912         ui_window_to_block(data->region, block, &mx, &my);
2913
2914         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2915                 if (event->val == KM_PRESS) {
2916                         if (event->type == LEFTMOUSE) {
2917                                 if (horizontal) {
2918                                         data->dragstartx = mx;
2919                                         data->draglastx = mx;
2920                                 }
2921                                 else {
2922                                         data->dragstartx = my;
2923                                         data->draglastx = my;
2924                                 }
2925                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2926                                 retval = WM_UI_HANDLER_BREAK;
2927                         }
2928                         /* UNUSED - otherwise code is ok, add back if needed */
2929 #if 0
2930                         else if (ELEM(event->type, PADENTER, RETKEY) && event->val == KM_PRESS)
2931                                 click = 1;
2932 #endif
2933                 }
2934         }
2935         else if (data->state == BUTTON_STATE_NUM_EDITING) {
2936                 if (event->type == ESCKEY) {
2937                         data->cancel = TRUE;
2938                         data->escapecancel = TRUE;
2939                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2940                 }
2941                 else if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
2942                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2943                 }
2944                 else if (event->type == MOUSEMOVE) {
2945                         if (ui_numedit_but_SLI(but, data, 0, 0, (horizontal) ? mx : my))
2946                                 ui_numedit_apply(C, block, but, data);
2947                 }
2948
2949                 retval = WM_UI_HANDLER_BREAK;
2950         }
2951         
2952         return retval;
2953 }
2954
2955
2956 static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2957 {
2958         
2959         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2960                 
2961                 /* first handle click on icondrag type button */