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