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