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