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