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