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