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