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