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