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