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