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