style cleanup, brackets in else/if, some indentation.
[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  * notice 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         ui_check_but(but);
1531
1532         /* special case, quit selection and set cursor */
1533         if (has_sel && !select) {
1534                 if (jump == BUTTON_EDIT_JUMP_ALL) {
1535                         but->selsta = but->selend= but->pos = direction ? len : 0;
1536                 }
1537                 else {
1538                         if (direction) {
1539                                 but->selsta = but->pos = but->selend;
1540                         }
1541                         else {
1542                                 but->pos = but->selend = but->selsta;
1543                         }
1544                 }
1545                 data->selextend = 0;
1546         }
1547         else {
1548                 ui_textedit_step_utf8(str, len, &but->pos, direction, jump);
1549
1550                 if(select) {
1551                         /* existing selection */
1552                         if (has_sel) {
1553
1554                                 if(data->selextend == 0) {
1555                                         data->selextend= EXTEND_RIGHT;
1556                                 }
1557
1558                                 if (direction) {
1559                                         if (data->selextend == EXTEND_RIGHT) {
1560                                                 but->selend= but->pos;
1561                                         }
1562                                         else {
1563                                                 but->selsta= but->pos;
1564                                         }
1565                                 }
1566                                 else {
1567                                         if (data->selextend == EXTEND_LEFT) {
1568                                                 but->selsta= but->pos;
1569                                         }
1570                                         else {
1571                                                 but->selend= but->pos;
1572                                         }
1573                                 }
1574
1575                                 if (but->selend < but->selsta) {
1576                                         SWAP(short, but->selsta, but->selend);
1577                                         data->selextend= (data->selextend == EXTEND_RIGHT) ? EXTEND_LEFT : EXTEND_RIGHT;
1578                                 }
1579
1580                         } /* new selection */
1581                         else {
1582                                 if (direction) {
1583                                         data->selextend= EXTEND_RIGHT;
1584                                         but->selend= but->pos;
1585                                         but->selsta= pos_prev;
1586                                 }
1587                                 else {
1588                                         data->selextend= EXTEND_LEFT;
1589                                         but->selend= pos_prev;
1590                                         but->selsta= but->pos;
1591                                 }
1592                         }
1593                 }
1594         }
1595 }
1596
1597 static int ui_textedit_delete(uiBut *but, uiHandleButtonData *data, int direction, uiButtonJumpType jump)
1598 {
1599         char *str= data->str;
1600         const int len= strlen(str);
1601
1602         int changed= 0;
1603
1604         if(jump == BUTTON_EDIT_JUMP_ALL) {
1605                 if(len) changed=1;
1606                 str[0]= '\0';
1607                 but->pos= 0;
1608         }
1609         else if(direction) { /* delete */
1610                 if ((but->selend - but->selsta) > 0) {
1611                         changed= ui_textedit_delete_selection(but, data);
1612                 }
1613                 else if (but->pos>=0 && but->pos<len) {
1614                         short pos= but->pos;
1615                         int step;
1616                         ui_textedit_step_utf8(str, len, &pos, direction, jump);
1617                         step= pos - but->pos;
1618                         memmove(&str[but->pos], &str[but->pos + step], (len + 1) - but->pos);
1619                         changed= 1;
1620                 }
1621         }
1622         else { /* backspace */
1623                 if (len != 0) {
1624                         if ((but->selend - but->selsta) > 0) {
1625                                 changed= ui_textedit_delete_selection(but, data);
1626                         }
1627                         else if(but->pos>0) {
1628                                 short pos= but->pos;
1629                                 int step;
1630
1631                                 ui_textedit_step_utf8(str, len, &pos, direction, jump);
1632                                 step= but->pos - pos;
1633                                 memmove(&str[but->pos - step], &str[but->pos], (len + 1) - but->pos);
1634                                 but->pos -= step;
1635                                 changed= 1;
1636                         }
1637                 } 
1638         }
1639
1640         return changed;
1641 }
1642
1643 static int ui_textedit_autocomplete(bContext *C, uiBut *but, uiHandleButtonData *data)
1644 {
1645         char *str;
1646         int changed= 1;
1647
1648         str= data->str;
1649
1650         if(data->searchbox)
1651                 ui_searchbox_autocomplete(C, data->searchbox, but, data->str);
1652         else
1653                 but->autocomplete_func(C, str, but->autofunc_arg);
1654
1655         but->pos= strlen(str);
1656         but->selsta= but->selend= but->pos;
1657
1658         return changed;
1659 }
1660
1661 static int ui_textedit_copypaste(uiBut *but, uiHandleButtonData *data, int paste, int copy, int cut)
1662 {
1663         char buf[UI_MAX_DRAW_STR]={0};
1664         char *str, *p, *pbuf;
1665         int len, x, i, changed= 0;
1666
1667         str= data->str;
1668         len= strlen(str);
1669         
1670         /* paste */
1671         if (paste) {
1672                 /* extract the first line from the clipboard */
1673                 p = pbuf= WM_clipboard_text_get(0);
1674
1675                 if(p && p[0]) {
1676                         unsigned int y;
1677                         i= 0;
1678                         while (*p && *p!='\r' && *p!='\n' && i<UI_MAX_DRAW_STR-1) {
1679                                 buf[i++]=*p;
1680                                 p++;
1681                         }
1682                         buf[i]= 0;
1683
1684                         /* paste over the current selection */
1685                         if ((but->selend - but->selsta) > 0) {
1686                                 ui_textedit_delete_selection(but, data);
1687                                 len= strlen(str);
1688                         }
1689                         
1690                         for (y=0; y<strlen(buf); y++)
1691                         {
1692                                 /* add contents of buffer */
1693                                 if(len+1 < data->maxlen) {
1694                                         for(x= data->maxlen; x>but->pos; x--)
1695                                                 str[x]= str[x-1];
1696                                         str[but->pos]= buf[y];
1697                                         but->pos++; 
1698                                         len++;
1699                                         str[len]= '\0';
1700                                 }
1701                         }
1702
1703                         changed= 1;
1704                 }
1705
1706                 if (pbuf) {
1707                         MEM_freeN(pbuf);
1708                 }
1709         }
1710         /* cut & copy */
1711         else if (copy || cut) {
1712                 /* copy the contents to the copypaste buffer */
1713                 for(x= but->selsta; x <= but->selend; x++) {
1714                         if (x==but->selend)
1715                                 buf[x] = '\0';
1716                         else
1717                                 buf[(x - but->selsta)] = str[x];
1718                 }
1719
1720                 WM_clipboard_text_set(buf, 0);
1721                 
1722                 /* for cut only, delete the selection afterwards */
1723                 if(cut)
1724                         if((but->selend - but->selsta) > 0)
1725                                 changed= ui_textedit_delete_selection(but, data);
1726         } 
1727
1728         return changed;
1729 }
1730
1731 static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
1732 {
1733         if(data->str) {
1734                 MEM_freeN(data->str);
1735                 data->str= NULL;
1736         }
1737
1738         /* retrieve string */
1739         data->maxlen= ui_get_but_string_max_length(but);
1740         data->str= MEM_callocN(sizeof(char)*data->maxlen + 1, "textedit str");
1741         ui_get_but_string(but, data->str, data->maxlen);
1742
1743         if(ELEM3(but->type, NUM, NUMABS, NUMSLI)) {
1744                 ui_convert_to_unit_alt_name(but, data->str, data->maxlen);
1745         }
1746
1747         data->origstr= BLI_strdup(data->str);
1748         data->selextend= 0;
1749         data->selstartx= 0;
1750
1751         /* set cursor pos to the end of the text */
1752         but->editstr= data->str;
1753         but->pos= strlen(data->str);
1754         but->selsta= 0;
1755         but->selend= strlen(data->str);
1756
1757         /* optional searchbox */
1758         if(but->type==SEARCH_MENU) {
1759                 data->searchbox= ui_searchbox_create(C, data->region, but);
1760                 ui_searchbox_update(C, data->searchbox, but, 1); /* 1= reset */
1761         }
1762         
1763         ui_check_but(but);
1764         
1765         WM_cursor_modal(CTX_wm_window(C), BC_TEXTEDITCURSOR);
1766 }
1767
1768 static void ui_textedit_end(bContext *C, uiBut *but, uiHandleButtonData *data)
1769 {
1770         if(but) {
1771                 if(ui_is_but_utf8(but)) {
1772                         int strip= BLI_utf8_invalid_strip(but->editstr, strlen(but->editstr));
1773                         /* not a file?, strip non utf-8 chars */
1774                         if(strip) {
1775                                 /* wont happen often so isnt that annoying to keep it here for a while */
1776                                 printf("%s: invalid utf8 - stripped chars %d\n", __func__, strip);
1777                         }
1778                 }
1779                 
1780                 if(data->searchbox) {
1781                         if(data->cancel==0)
1782                                 ui_searchbox_apply(but, data->searchbox);
1783
1784                         ui_searchbox_free(C, data->searchbox);
1785                         data->searchbox= NULL;
1786                 }
1787                 
1788                 but->editstr= NULL;
1789                 but->pos= -1;
1790         }
1791         
1792         WM_cursor_restore(CTX_wm_window(C));
1793 }
1794
1795 static void ui_textedit_next_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1796 {
1797         uiBut *but;
1798
1799         /* label and roundbox can overlap real buttons (backdrops...) */
1800         if(ELEM4(actbut->type, LABEL, SEPR, ROUNDBOX, LISTBOX))
1801                 return;
1802
1803         for(but= actbut->next; but; but= but->next) {
1804                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1805                         if(!(but->flag & UI_BUT_DISABLED)) {
1806                                 data->postbut= but;
1807                                 data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1808                                 return;
1809                         }
1810                 }
1811         }
1812         for(but= block->buttons.first; but!=actbut; but= but->next) {
1813                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1814                         if(!(but->flag & UI_BUT_DISABLED)) {
1815                                 data->postbut= but;
1816                                 data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1817                                 return;
1818                         }
1819                 }
1820         }
1821 }
1822
1823 static void ui_textedit_prev_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1824 {
1825         uiBut *but;
1826
1827         /* label and roundbox can overlap real buttons (backdrops...) */
1828         if(ELEM4(actbut->type, LABEL, SEPR, ROUNDBOX, LISTBOX))
1829                 return;
1830
1831         for(but= actbut->prev; but; but= but->prev) {
1832                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1833                         if(!(but->flag & UI_BUT_DISABLED)) {
1834                                 data->postbut= but;
1835                                 data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1836                                 return;
1837                         }
1838                 }
1839         }
1840         for(but= block->buttons.last; but!=actbut; but= but->prev) {
1841                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1842                         if(!(but->flag & UI_BUT_DISABLED)) {
1843                                 data->postbut= but;
1844                                 data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1845                                 return;
1846                         }
1847                 }
1848         }
1849 }
1850
1851
1852 static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1853 {
1854         int mx, my, changed= 0, inbox=0, update= 0, retval= WM_UI_HANDLER_CONTINUE;
1855
1856         switch(event->type) {
1857                 case WHEELUPMOUSE:
1858                 case WHEELDOWNMOUSE:
1859                 case MOUSEMOVE:
1860                         if(data->searchbox)
1861                                 ui_searchbox_event(C, data->searchbox, but, event);
1862                         
1863                         break;
1864                 case RIGHTMOUSE:
1865                 case ESCKEY:
1866                         data->cancel= 1;
1867                         data->escapecancel= 1;
1868                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1869                         retval= WM_UI_HANDLER_BREAK;
1870                         break;
1871                 case LEFTMOUSE: {
1872                         
1873                         /* exit on LMB only on RELEASE for searchbox, to mimic other popups, and allow multiple menu levels */
1874                         if(data->searchbox)
1875                                 inbox= ui_searchbox_inside(data->searchbox, event->x, event->y);
1876
1877                         if(event->val==KM_PRESS) {
1878                                 mx= event->x;
1879                                 my= event->y;
1880                                 ui_window_to_block(data->region, block, &mx, &my);
1881
1882                                 if (ui_but_contains_pt(but, mx, my)) {
1883                                         ui_textedit_set_cursor_pos(but, data, mx);
1884                                         but->selsta = but->selend = but->pos;
1885                                         data->selstartx= mx;
1886
1887                                         button_activate_state(C, but, BUTTON_STATE_TEXT_SELECTING);
1888                                         retval= WM_UI_HANDLER_BREAK;
1889                                 }
1890                                 else if(inbox==0) {
1891                                         /* if searchbox, click outside will cancel */
1892                                         if(data->searchbox)
1893                                                 data->cancel= data->escapecancel= 1;
1894                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1895                                         retval= WM_UI_HANDLER_BREAK;
1896                                 }
1897                         }
1898                         else if(inbox) {
1899                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1900                                 retval= WM_UI_HANDLER_BREAK;
1901                         }
1902                         break;
1903                 }
1904         }
1905
1906         if(event->val==KM_PRESS) {
1907                 switch (event->type) {
1908                         case VKEY:
1909                         case XKEY:
1910                         case CKEY:
1911                                 if(event->ctrl || event->oskey) {
1912                                         if(event->type == VKEY)
1913                                                 changed= ui_textedit_copypaste(but, data, 1, 0, 0);
1914                                         else if(event->type == CKEY)
1915                                                 changed= ui_textedit_copypaste(but, data, 0, 1, 0);
1916                                         else if(event->type == XKEY)
1917                                                 changed= ui_textedit_copypaste(but, data, 0, 0, 1);
1918
1919                                         retval= WM_UI_HANDLER_BREAK;
1920                                 }
1921                                 break;
1922                         case RIGHTARROWKEY:
1923                                 ui_textedit_move(but, data, 1, event->shift, event->ctrl ? BUTTON_EDIT_JUMP_DELIM : BUTTON_EDIT_JUMP_NONE);
1924                                 retval= WM_UI_HANDLER_BREAK;
1925                                 break;
1926                         case LEFTARROWKEY:
1927                                 ui_textedit_move(but, data, 0, event->shift, event->ctrl ? BUTTON_EDIT_JUMP_DELIM : BUTTON_EDIT_JUMP_NONE);
1928                                 retval= WM_UI_HANDLER_BREAK;
1929                                 break;
1930                         case DOWNARROWKEY:
1931                                 if(data->searchbox) {
1932                                         ui_searchbox_event(C, data->searchbox, but, event);
1933                                         break;
1934                                 }
1935                                 /* pass on purposedly */
1936                         case ENDKEY:
1937                                 ui_textedit_move(but, data, 1, event->shift, BUTTON_EDIT_JUMP_ALL);
1938                                 retval= WM_UI_HANDLER_BREAK;
1939                                 break;
1940                         case UPARROWKEY:
1941                                 if(data->searchbox) {
1942                                         ui_searchbox_event(C, data->searchbox, but, event);
1943                                         break;
1944                                 }
1945                                 /* pass on purposedly */
1946                         case HOMEKEY:
1947                                 ui_textedit_move(but, data, 0, event->shift, BUTTON_EDIT_JUMP_ALL);
1948                                 retval= WM_UI_HANDLER_BREAK;
1949                                 break;
1950                         case PADENTER:
1951                         case RETKEY:
1952                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1953                                 retval= WM_UI_HANDLER_BREAK;
1954                                 break;
1955                         case DELKEY:
1956                                 changed= ui_textedit_delete(but, data, 1, event->ctrl ? BUTTON_EDIT_JUMP_DELIM : BUTTON_EDIT_JUMP_NONE);
1957                                 retval= WM_UI_HANDLER_BREAK;
1958                                 break;
1959
1960                         case BACKSPACEKEY:
1961                                 changed= ui_textedit_delete(but, data, 0, event->shift ? BUTTON_EDIT_JUMP_ALL :  (event->ctrl ? BUTTON_EDIT_JUMP_DELIM : BUTTON_EDIT_JUMP_NONE));
1962                                 retval= WM_UI_HANDLER_BREAK;
1963                                 break;
1964                                 
1965                         case TABKEY:
1966                                 /* there is a key conflict here, we can't tab with autocomplete */
1967                                 if(but->autocomplete_func || data->searchbox) {
1968                                         changed= ui_textedit_autocomplete(C, but, data);
1969                                         update= 1; /* do live update for tab key */
1970                                 }
1971                                 /* the hotkey here is not well defined, was G.qual so we check all */
1972                                 else if(event->shift || event->ctrl || event->alt || event->oskey) {
1973                                         ui_textedit_prev_but(block, but, data);
1974                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1975                                 }
1976                                 else {
1977                                         ui_textedit_next_but(block, but, data);
1978                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1979                                 }
1980                                 retval= WM_UI_HANDLER_BREAK;
1981                                 break;
1982                 }
1983
1984                 if((event->ascii || event->utf8_buf[0]) && (retval == WM_UI_HANDLER_CONTINUE)) {
1985                         char ascii = event->ascii;
1986                         const char *utf8_buf= event->utf8_buf;
1987
1988                         /* exception that's useful for number buttons, some keyboard
1989                          * numpads have a comma instead of a period */
1990                         if(ELEM3(but->type, NUM, NUMABS, NUMSLI)) { /* could use data->min*/
1991                                 if(event->type == PADPERIOD && ascii == ',') {
1992                                         ascii = '.';
1993                                         utf8_buf= NULL; /* force ascii fallback */
1994                                 }
1995                         }
1996
1997                         if(utf8_buf && utf8_buf[0]) {
1998                                 int utf8_buf_len= BLI_str_utf8_size(utf8_buf);
1999                                 /* keep this printf until utf8 is well tested */
2000                                 if (utf8_buf_len != 1) {
2001                                         printf("%s: utf8 char '%.*s'\n", __func__, utf8_buf_len, utf8_buf);
2002                                 }
2003
2004                                 // strcpy(utf8_buf, "12345");
2005                                 changed= ui_textedit_type_buf(but, data, event->utf8_buf, utf8_buf_len);
2006                         }
2007                         else {
2008                                 changed= ui_textedit_type_ascii(but, data, ascii);
2009                         }
2010
2011                         retval= WM_UI_HANDLER_BREAK;
2012                         
2013                 }
2014                 /* textbutton with magnifier icon: do live update for search button */
2015                 if(but->icon==ICON_VIEWZOOM)
2016                         update= 1;
2017         }
2018
2019         if(changed) {
2020                 /* only update when typing for TAB key */
2021                 if(update && data->interactive) ui_apply_button(C, block, but, data, 1);
2022                 else ui_check_but(but);
2023                 but->changed= TRUE;
2024                 
2025                 if(data->searchbox)
2026                         ui_searchbox_update(C, data->searchbox, but, 1); /* 1 = reset */
2027         }
2028
2029         if(changed || (retval == WM_UI_HANDLER_BREAK))
2030                 ED_region_tag_redraw(data->region);
2031 }
2032
2033 static void ui_do_but_textedit_select(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2034 {
2035         int mx, my, retval= WM_UI_HANDLER_CONTINUE;
2036
2037         switch(event->type) {
2038                 case MOUSEMOVE: {
2039                         mx= event->x;
2040                         my= event->y;
2041                         ui_window_to_block(data->region, block, &mx, &my);
2042
2043                         ui_textedit_set_cursor_select(but, data, mx);
2044                         retval= WM_UI_HANDLER_BREAK;
2045                         break;
2046                 }
2047                 case LEFTMOUSE:
2048                         if(event->val == KM_RELEASE)
2049                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2050                         retval= WM_UI_HANDLER_BREAK;
2051                         break;
2052         }
2053
2054         if(retval == WM_UI_HANDLER_BREAK) {
2055                 ui_check_but(but);
2056                 ED_region_tag_redraw(data->region);
2057         }
2058 }
2059
2060 /* ************* number editing for various types ************* */
2061
2062 static void ui_numedit_begin(uiBut *but, uiHandleButtonData *data)
2063 {
2064         if(but->type == BUT_CURVE) {
2065                 but->editcumap= (CurveMapping*)but->poin;
2066         }
2067         else if(but->type == BUT_COLORBAND) {
2068                 data->coba= (ColorBand*)but->poin;
2069                 but->editcoba= data->coba;
2070         }
2071         else if(ELEM3(but->type, BUT_NORMAL, HSVCUBE, HSVCIRCLE)) {
2072                 ui_get_but_vectorf(but, data->origvec);
2073                 copy_v3_v3(data->vec, data->origvec);
2074                 but->editvec= data->vec;
2075         }
2076         else {
2077                 float softrange, softmin, softmax;
2078
2079                 data->startvalue= ui_get_but_val(but);
2080                 data->origvalue= data->startvalue;
2081                 data->value= data->origvalue;
2082                 but->editval= &data->value;
2083
2084                 softmin= but->softmin;
2085                 softmax= but->softmax;
2086                 softrange= softmax - softmin;
2087
2088                 data->dragfstart= (softrange == 0.0f)? 0.0f: ((float)data->value - softmin)/softrange;
2089                 data->dragf= data->dragfstart;
2090         }
2091
2092         data->dragchange= 0;
2093         data->draglock= 1;
2094 }
2095
2096 static void ui_numedit_end(uiBut *but, uiHandleButtonData *data)
2097 {
2098         but->editval= NULL;
2099         but->editvec= NULL;
2100         but->editcoba= NULL;
2101         but->editcumap= NULL;
2102
2103         data->dragstartx= 0;
2104         data->draglastx= 0;
2105         data->dragchange= 0;
2106         data->dragcbd= NULL;
2107         data->dragsel= 0;
2108 }
2109
2110 static void ui_numedit_apply(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data)
2111 {
2112         if(data->interactive) ui_apply_button(C, block, but, data, 1);
2113         else ui_check_but(but);
2114
2115         ED_region_tag_redraw(data->region);
2116 }
2117
2118 /* ****************** menu opening for various types **************** */
2119
2120 static void ui_blockopen_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
2121 {
2122         uiBlockCreateFunc func= NULL;
2123         uiBlockHandleCreateFunc handlefunc= NULL;
2124         uiMenuCreateFunc menufunc= NULL;
2125         char *menustr= NULL;
2126         void *arg= NULL;
2127
2128         switch(but->type) {
2129                 case BLOCK:
2130                 case PULLDOWN:
2131                         if(but->menu_create_func) {
2132                                 menufunc= but->menu_create_func;
2133                                 arg= but->poin;
2134                         }
2135                         else {
2136                                 func= but->block_create_func;
2137                                 arg= but->poin?but->poin:but->func_argN;
2138                         }
2139                         break;
2140                 case MENU:
2141                         if(but->menu_create_func) {
2142                                 menufunc= but->menu_create_func;
2143                                 arg= but->poin;
2144                         }
2145                         else {
2146                                 data->origvalue= ui_get_but_val(but);
2147                                 data->value= data->origvalue;
2148                                 but->editval= &data->value;
2149
2150                                 menustr= but->str;
2151                         }
2152                         break;
2153                 case ICONROW:
2154                         menufunc= ui_block_func_ICONROW;
2155                         arg= but;
2156                         break;
2157                 case ICONTEXTROW:
2158                         menufunc= ui_block_func_ICONTEXTROW;
2159                         arg= but;
2160                         break;
2161                 case COL:
2162                         ui_get_but_vectorf(but, data->origvec);
2163                         copy_v3_v3(data->vec, data->origvec);
2164                         but->editvec= data->vec;
2165
2166                         handlefunc= ui_block_func_COL;
2167                         arg= but;
2168                         break;
2169         }
2170
2171         if(func || handlefunc) {
2172                 data->menu= ui_popup_block_create(C, data->region, but, func, handlefunc, arg);
2173                 if(but->block->handle)
2174                         data->menu->popup= but->block->handle->popup;
2175         }
2176         else if(menufunc || menustr) {
2177                 data->menu= ui_popup_menu_create(C, data->region, but, menufunc, arg, menustr);
2178                 if(but->block->handle)
2179                         data->menu->popup= but->block->handle->popup;
2180         }
2181
2182         /* this makes adjacent blocks auto open from now on */
2183         //if(but->block->auto_open==0) but->block->auto_open= 1;
2184 }
2185
2186 static void ui_blockopen_end(bContext *C, uiBut *but, uiHandleButtonData *data)
2187 {
2188         if(but) {
2189                 but->editval= NULL;
2190                 but->editvec= NULL;
2191
2192                 but->block->auto_open_last= PIL_check_seconds_timer();
2193         }
2194
2195         if(data->menu) {
2196                 ui_popup_block_free(C, data->menu);
2197                 data->menu= NULL;
2198         }
2199 }
2200
2201 /* ***************** events for different button types *************** */
2202
2203 static int ui_do_but_BUT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2204 {
2205         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2206                 if(event->type == LEFTMOUSE && event->val==KM_PRESS) {
2207                         button_activate_state(C, but, BUTTON_STATE_WAIT_RELEASE);
2208                         return WM_UI_HANDLER_BREAK;
2209                 }
2210                 else if(event->type == LEFTMOUSE && but->block->handle) {
2211                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2212                         return WM_UI_HANDLER_BREAK;
2213                 }
2214                 else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS) {
2215                         button_activate_state(C, but, BUTTON_STATE_WAIT_FLASH);
2216                         return WM_UI_HANDLER_BREAK;
2217                 }
2218         }
2219         else if(data->state == BUTTON_STATE_WAIT_RELEASE) {
2220                 if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2221                         if(!(but->flag & UI_SELECT))
2222                                 data->cancel= 1;
2223                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2224                         return WM_UI_HANDLER_BREAK;
2225                 }
2226         }
2227
2228         return WM_UI_HANDLER_CONTINUE;
2229 }
2230
2231 static int ui_do_but_HOTKEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2232 {
2233         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2234                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2235                         but->drawstr[0]= 0;
2236                         but->modifier_key= 0;
2237                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
2238                         return WM_UI_HANDLER_BREAK;
2239                 }
2240         }
2241         else if(data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2242                 
2243                 if(event->type == MOUSEMOVE)
2244                         return WM_UI_HANDLER_CONTINUE;
2245                 
2246                 if(event->type == LEFTMOUSE && event->val==KM_PRESS) {
2247                         /* only cancel if click outside the button */
2248                         if(ui_mouse_inside_button(but->active->region, but, event->x, event->y) == 0) {
2249                                 /* data->cancel doesnt work, this button opens immediate */
2250                                 if(but->flag & UI_BUT_IMMEDIATE)
2251                                         ui_set_but_val(but, 0);
2252                                 else
2253                                         data->cancel= 1;
2254                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2255                                 return WM_UI_HANDLER_BREAK;
2256                         }
2257                 }
2258                 
2259                 /* always set */
2260                 but->modifier_key = 0;
2261                 if(event->shift) but->modifier_key |= KM_SHIFT;
2262                 if(event->alt)   but->modifier_key |= KM_ALT;
2263                 if(event->ctrl)  but->modifier_key |= KM_CTRL;
2264                 if(event->oskey) but->modifier_key |= KM_OSKEY;
2265
2266                 ui_check_but(but);
2267                 ED_region_tag_redraw(data->region);
2268                         
2269                 if(event->val==KM_PRESS) {
2270                         if(ISHOTKEY(event->type)) { 
2271                                 
2272                                 if(WM_key_event_string(event->type)[0])
2273                                         ui_set_but_val(but, event->type);
2274                                 else
2275                                         data->cancel= 1;
2276                                 
2277                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2278                                 return WM_UI_HANDLER_BREAK;
2279                         }
2280                         else if(event->type == ESCKEY) {
2281                                 data->cancel= 1;
2282                                 data->escapecancel= 1;
2283                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2284                         }
2285                         
2286                 }
2287         }
2288         
2289         return WM_UI_HANDLER_CONTINUE;
2290 }
2291
2292 static int ui_do_but_KEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2293 {
2294         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2295                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2296                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
2297                         return WM_UI_HANDLER_BREAK;
2298                 }
2299         }
2300         else if(data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2301                 if(event->type == MOUSEMOVE)
2302                         return WM_UI_HANDLER_CONTINUE;
2303
2304                 if(event->val==KM_PRESS) {
2305                         if(WM_key_event_string(event->type)[0])
2306                                 ui_set_but_val(but, event->type);
2307                         else
2308                                 data->cancel= 1;
2309
2310                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2311                 }
2312         }
2313
2314         return WM_UI_HANDLER_CONTINUE;
2315 }
2316
2317 static int ui_do_but_TEX(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2318 {
2319         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2320                 if(ELEM(event->type, LEFTMOUSE, EVT_BUT_OPEN) && event->val==KM_PRESS) {
2321                         if(but->dt == UI_EMBOSSN && !event->ctrl);
2322                         else {
2323                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2324                                 return WM_UI_HANDLER_BREAK;
2325                         }
2326                 }
2327         }
2328         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2329                 ui_do_but_textedit(C, block, but, data, event);
2330                 return WM_UI_HANDLER_BREAK;
2331         }
2332         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2333                 ui_do_but_textedit_select(C, block, but, data, event);
2334                 return WM_UI_HANDLER_BREAK;
2335         }
2336
2337         return WM_UI_HANDLER_CONTINUE;
2338 }
2339
2340 static int ui_do_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2341 {
2342         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2343                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2344                         data->togdual= event->ctrl;
2345                         data->togonly= !event->shift;
2346                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2347                         return WM_UI_HANDLER_BREAK;
2348                 }
2349         }
2350         return WM_UI_HANDLER_CONTINUE;
2351 }
2352
2353 static int ui_do_but_EXIT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2354 {
2355         
2356         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2357
2358                 /* first handle click on icondrag type button */
2359                 if(event->type==LEFTMOUSE && but->dragpoin) {
2360                         if(ui_but_mouse_inside_icon(but, data->region, event)) {
2361                                 
2362                                 /* tell the button to wait and keep checking further events to
2363                                  * see if it should start dragging */
2364                                 button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
2365                                 data->dragstartx= event->x;
2366                                 data->dragstarty= event->y;
2367                                 return WM_UI_HANDLER_CONTINUE;
2368                         }
2369                 }
2370                 
2371                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2372                         int ret = WM_UI_HANDLER_BREAK;
2373                         /* XXX (a bit ugly) Special case handling for filebrowser drag button */
2374                         if(but->dragpoin && but->imb && ui_but_mouse_inside_icon(but, data->region, event)) {
2375                                 ret = WM_UI_HANDLER_CONTINUE;
2376                         }
2377                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2378                         return ret;
2379                 }
2380         }
2381         else if(data->state == BUTTON_STATE_WAIT_DRAG) {
2382                 
2383                 /* this function also ends state */
2384                 if(ui_but_start_drag(C, but, data, event)) {
2385                         return WM_UI_HANDLER_BREAK;
2386                 }
2387                 
2388                 /* If the mouse has been pressed and released, getting to 
2389                  * this point without triggering a drag, then clear the 
2390                  * drag state for this button and continue to pass on the event */
2391                 if(event->type==LEFTMOUSE && event->val==KM_RELEASE) {
2392                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2393                         return WM_UI_HANDLER_CONTINUE;
2394                 }
2395                 
2396                 /* while waiting for a drag to be triggered, always block 
2397                  * other events from getting handled */
2398                 return WM_UI_HANDLER_BREAK;
2399         }
2400         
2401         return WM_UI_HANDLER_CONTINUE;
2402 }
2403
2404 /* var names match ui_numedit_but_NUM */
2405 static float ui_numedit_apply_snapf(uiBut *but, float tempf, float softmin, float softmax, float softrange, int snap)
2406 {
2407         if(tempf==softmin || tempf==softmax || snap==0) {
2408                 /* pass */
2409         }
2410         else {
2411                 float fac= 1.0f;
2412                 
2413                 if(ui_is_but_unit(but)) {
2414                         UnitSettings *unit= but->block->unit;
2415                         int unit_type= uiButGetUnitType(but)>>16;
2416
2417                         if(bUnit_IsValid(unit->system, unit_type)) {
2418                                 fac= (float)bUnit_BaseScalar(unit->system, unit_type);
2419                                 if(ELEM3(unit_type, B_UNIT_LENGTH, B_UNIT_AREA, B_UNIT_VOLUME)) {
2420                                         fac /= unit->scale_length;
2421                                 }
2422                         }
2423                 }
2424
2425                 if(fac != 1.0f) {
2426                         /* snap in unit-space */
2427                         tempf /= fac;
2428                         /* softmin /= fac; */ /* UNUSED */
2429                         /* softmax /= fac; */ /* UNUSED */
2430                         softrange /= fac;
2431                 }
2432
2433                 if(snap==1) {
2434                         if(softrange < 2.10f) tempf= 0.1f*floorf(10.0f*tempf);
2435                         else if(softrange < 21.0f) tempf= floorf(tempf);
2436                         else tempf= 10.0f*floorf(tempf/10.0f);
2437                 }
2438                 else if(snap==2) {
2439                         if(softrange < 2.10f) tempf= 0.01f*floorf(100.0f*tempf);
2440                         else if(softrange < 21.0f) tempf= 0.1f*floorf(10.0f*tempf);
2441                         else tempf= floor(tempf);
2442                 }
2443                 
2444                 if(fac != 1.0f)
2445                         tempf *= fac;
2446         }
2447
2448         return tempf;
2449 }
2450
2451 static float ui_numedit_apply_snap(int temp, float softmin, float softmax, int snap)
2452 {
2453         if(temp==softmin || temp==softmax)
2454                 return temp;
2455
2456         switch(snap) {
2457         case 0:
2458                 break;
2459         case 1:
2460                 temp= 10*(temp/10);
2461                 break;
2462         case 2:
2463                 temp= 100*(temp/100);
2464                 break;
2465         }
2466
2467         return temp;
2468 }
2469
2470 static int ui_numedit_but_NUM(uiBut *but, uiHandleButtonData *data, float fac, int snap, int mx)
2471 {
2472         float deler, tempf, softmin, softmax, softrange;
2473         int lvalue, temp, changed= 0;
2474         
2475         if(mx == data->draglastx)
2476                 return changed;
2477         
2478         /* drag-lock - prevent unwanted scroll adjustments */
2479         /* change value (now 3) to adjust threshold in pixels */
2480         if(data->draglock) {
2481                 if(abs(mx-data->dragstartx) <= 3)
2482                         return changed;
2483
2484                 data->draglock= 0;
2485                 data->dragstartx= mx;  /* ignore mouse movement within drag-lock */
2486         }
2487
2488         softmin= but->softmin;
2489         softmax= but->softmax;
2490         softrange= softmax - softmin;
2491
2492         if(ui_is_a_warp_but(but)) {
2493                 /* Mouse location isn't screen clamped to the screen so use a linear mapping
2494                  * 2px == 1-int, or 1px == 1-ClickStep */
2495                 if(ui_is_but_float(but)) {
2496                         fac *= 0.01f*but->a1;
2497                         tempf = (float)data->startvalue + ((float)(mx - data->dragstartx) * fac);
2498                         tempf= ui_numedit_apply_snapf(but, tempf, softmin, softmax, softrange, snap);
2499
2500 #if 1           /* fake moving the click start, nicer for dragging back after passing the limit */
2501                         if(tempf < softmin) {
2502                                 data->dragstartx -= (softmin-tempf) / fac;
2503                                 tempf= softmin;
2504                         } else if (tempf > softmax) {
2505                                 data->dragstartx += (tempf-softmax) / fac;
2506                                 tempf= softmax;
2507                         }
2508 #else
2509                         CLAMP(tempf, softmin, softmax);
2510 #endif
2511
2512                         if(tempf != (float)data->value) {
2513                                 data->dragchange= 1;
2514                                 data->value= tempf;
2515                                 changed= 1;
2516                         }
2517                 }
2518                 else {
2519                         if(softrange > 256)             fac= 1.0;               /* 1px == 1 */
2520                         else if(softrange > 32) fac= 1.0/2.0;   /* 2px == 1 */
2521                         else                                    fac= 1.0/16.0;  /* 16px == 1? */
2522
2523                         temp= data->startvalue + (((double)mx - data->dragstartx) * (double)fac);
2524                         temp= ui_numedit_apply_snap(temp, softmin, softmax, snap);
2525
2526 #if 1           /* fake moving the click start, nicer for dragging back after passing the limit */
2527                         if(temp < softmin) {
2528                                 data->dragstartx -= (softmin-temp) / fac;
2529                                 temp= softmin;
2530                         } else if (temp > softmax) {
2531                                 data->dragstartx += (temp-softmax) / fac;
2532                                 temp= softmax;
2533                         }
2534 #else
2535                         CLAMP(temp, softmin, softmax);
2536 #endif
2537
2538                         if(temp != data->value) {
2539                                 data->dragchange= 1;
2540                                 data->value= temp;
2541                                 changed= 1;
2542                         }
2543                 }
2544
2545                 data->draglastx= mx;
2546         }
2547         else {
2548                 /* Use a non-linear mapping of the mouse drag especially for large floats (normal behavior) */
2549                 deler= 500;
2550                 if(!ui_is_but_float(but)) {
2551                         /* prevent large ranges from getting too out of control */
2552                         if (softrange > 600) deler = powf(softrange, 0.75);
2553                         
2554                         if (softrange < 100) deler= 200.0;
2555                         if (softrange < 25) deler= 50.0;
2556                 }
2557                 deler /= fac;
2558
2559                 if(softrange > 11) {
2560                         /* non linear change in mouse input- good for high precicsion */
2561                         data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabsf(data->dragstartx-mx)*0.002f);
2562                 } else if (softrange > 129) { /* only scale large int buttons */
2563                         /* non linear change in mouse input- good for high precicsionm ints need less fine tuning */
2564                         data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabsf(data->dragstartx-mx)*0.004f);
2565                 } else {
2566                         /*no scaling */
2567                         data->dragf += ((float)(mx - data->draglastx)) / deler;
2568                 }
2569         
2570                 CLAMP(data->dragf, 0.0f, 1.0f);
2571                 data->draglastx= mx;
2572                 tempf= (softmin + data->dragf*softrange);
2573
2574
2575                 if(!ui_is_but_float(but)) {
2576                         temp= floorf(tempf + 0.5f);
2577
2578                         temp= ui_numedit_apply_snap(temp, softmin, softmax, snap);
2579
2580                         CLAMP(temp, softmin, softmax);
2581                         lvalue= (int)data->value;
2582                         
2583                         if(temp != lvalue) {
2584                                 data->dragchange= 1;
2585                                 data->value= (double)temp;
2586                                 changed= 1;
2587                         }
2588                 }
2589                 else {
2590                         temp= 0;
2591                         tempf= ui_numedit_apply_snapf(but, tempf, softmin, softmax, softrange, snap);
2592
2593                         CLAMP(tempf, softmin, softmax);
2594
2595                         if(tempf != (float)data->value) {
2596                                 data->dragchange= 1;
2597                                 data->value= tempf;
2598                                 changed= 1;
2599                         }
2600                 }
2601         }
2602
2603
2604         return changed;
2605 }
2606
2607 static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2608 {
2609         int mx, my;     /* mouse location scaled to fit the UI */
2610         int screen_mx, screen_my; /* mouse location kept at screen pixel coords */
2611         int click= 0;
2612         int retval= WM_UI_HANDLER_CONTINUE;
2613         
2614         mx= screen_mx= event->x;
2615         my= screen_my= event->y;
2616
2617         ui_window_to_block(data->region, block, &mx, &my);
2618
2619         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2620                 /* XXX hardcoded keymap check.... */
2621                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2622                         mx= but->x1;
2623                         click= 1;
2624                 }
2625                 else if(event->type == WHEELUPMOUSE && event->alt) {
2626                         mx= but->x2;
2627                         click= 1;
2628                 }
2629                 else if(event->val==KM_PRESS) {
2630                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2631                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2632                                 retval= WM_UI_HANDLER_BREAK;
2633                         }
2634                         else if(event->type == LEFTMOUSE) {
2635                                 data->dragstartx= data->draglastx= ui_is_a_warp_but(but) ? screen_mx:mx;
2636                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2637                                 retval= WM_UI_HANDLER_BREAK;
2638                         }
2639                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2640                                 click= 1;
2641                         else if (event->type == MINUSKEY && event->val==KM_PRESS) {
2642                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2643                                 data->value = -data->value;
2644                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2645                                 retval= WM_UI_HANDLER_BREAK;
2646                         }
2647                 }
2648                 
2649         }
2650         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2651                 if(event->type == ESCKEY) {
2652                         data->cancel= 1;
2653                         data->escapecancel= 1;
2654                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2655                 }
2656                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2657                         if(data->dragchange)
2658                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2659                         else
2660                                 click= 1;
2661                 }
2662                 else if(event->type == MOUSEMOVE) {
2663                         float fac;
2664                         int snap;
2665
2666                         fac= 1.0f;
2667                         if(event->shift) fac /= 10.0f;
2668                         if(event->alt) fac /= 20.0f;
2669                         
2670                         snap= (event->ctrl)? (event->shift)? 2: 1: 0;
2671
2672                         if(ui_numedit_but_NUM(but, data, fac, snap, (ui_is_a_warp_but(but) ? screen_mx:mx)))
2673                                 ui_numedit_apply(C, block, but, data);
2674                 }
2675                 retval= WM_UI_HANDLER_BREAK;
2676         }
2677         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2678                 ui_do_but_textedit(C, block, but, data, event);
2679                 retval= WM_UI_HANDLER_BREAK;
2680         }
2681         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2682                 ui_do_but_textedit_select(C, block, but, data, event);
2683                 retval= WM_UI_HANDLER_BREAK;
2684         }
2685         
2686         if(click) {
2687                 /* we can click on the side arrows to increment/decrement,
2688                  * or click inside to edit the value directly */
2689                 float tempf, softmin, softmax;
2690                 int temp;
2691
2692                 softmin= but->softmin;
2693                 softmax= but->softmax;
2694
2695                 if(!ui_is_but_float(but)) {
2696                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2697                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2698
2699                                 temp= (int)data->value - 1;
2700                                 if(temp>=softmin && temp<=softmax)
2701                                         data->value= (double)temp;
2702                                 else
2703                                         data->cancel= 1;
2704
2705                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2706                         }
2707                         else if(mx > (but->x1 + (2*(but->x2 - but->x1)/3) + 3)) {
2708                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2709
2710                                 temp= (int)data->value + 1;
2711                                 if(temp>=softmin && temp<=softmax)
2712                                         data->value= (double)temp;
2713                                 else
2714                                         data->cancel= 1;
2715
2716                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2717                         }
2718                         else
2719                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2720                 }
2721                 else {
2722                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2723                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2724
2725                                 tempf= (float)data->value - 0.01f * but->a1;
2726                                 if (tempf < softmin) tempf = softmin;
2727                                 data->value= tempf;
2728
2729                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2730                         }
2731                         else if(mx > but->x1 + (2*((but->x2 - but->x1)/3) + 3)) {
2732                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2733
2734                                 tempf= (float)data->value + 0.01f * but->a1;
2735                                 if (tempf > softmax) tempf = softmax;
2736                                 data->value= tempf;
2737
2738                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2739                         }
2740                         else
2741                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2742                 }
2743
2744                 retval= WM_UI_HANDLER_BREAK;
2745         }
2746         
2747         return retval;
2748 }
2749
2750 static int ui_numedit_but_SLI(uiBut *but, uiHandleButtonData *data, int shift, int ctrl, int mx)
2751 {
2752         float deler, f, tempf, softmin, softmax, softrange;
2753         int temp, lvalue, changed= 0;
2754
2755         softmin= but->softmin;
2756         softmax= but->softmax;
2757         softrange= softmax - softmin;
2758
2759         if(but->type==NUMSLI) deler= ((but->x2-but->x1) - 5.0f*but->aspect);
2760         else if(but->type==HSVSLI) deler= ((but->x2-but->x1)/2.0f - 5.0f*but->aspect);
2761         else if(but->type==SCROLL) {
2762                 int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2763                 float size= (horizontal)? (but->x2-but->x1): -(but->y2-but->y1);
2764                 deler= size*(but->softmax - but->softmin)/(but->softmax - but->softmin + but->a1);
2765         }
2766         else deler= (but->x2-but->x1- 5.0f*but->aspect);
2767
2768         f= (float)(mx-data->dragstartx)/deler + data->dragfstart;
2769         
2770         if(shift)
2771                 f= (f-data->dragfstart)/10.0f + data->dragfstart;
2772
2773         CLAMP(f, 0.0f, 1.0f);
2774         tempf= softmin + f*softrange;
2775         temp= floorf(tempf+0.5f);
2776
2777         if(ctrl) {
2778                 if(tempf==softmin || tempf==softmax);
2779                 else if(ui_is_but_float(but)) {
2780
2781                         if(shift) {
2782                                 if(tempf==softmin || tempf==softmax);
2783                                 else if(softmax-softmin < 2.10f) tempf= 0.01f * floorf(100.0f*tempf);
2784                                 else if(softmax-softmin < 21.0f) tempf= 0.1f * floorf(10.0f*tempf);
2785                                 else tempf= floorf(tempf);
2786                         }
2787                         else {
2788                                 if(softmax-softmin < 2.10f) tempf= 0.1f * floorf(10.0f*tempf);
2789                                 else if(softmax-softmin < 21.0f) tempf= floorf(tempf);
2790                                 else tempf= 10.0f*floorf(tempf/10.0f);
2791                         }
2792                 }
2793                 else {
2794                         temp= 10*(temp/10);
2795                         tempf= temp;
2796                 }
2797         }
2798
2799         if(!ui_is_but_float(but)) {
2800                 lvalue= floor(data->value+0.5);
2801
2802                 CLAMP(temp, softmin, softmax);
2803
2804                 if(temp != lvalue) {
2805                         data->value= temp;
2806                         data->dragchange= 1;
2807                         changed= 1;
2808                 }
2809         }
2810         else {
2811                 CLAMP(tempf, softmin, softmax);
2812
2813                 if(tempf != (float)data->value) {
2814                         data->value= tempf;
2815                         data->dragchange= 1;
2816                         changed= 1;
2817                 }
2818         }
2819
2820         return changed;
2821 }
2822
2823 static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2824 {
2825         int mx, my, click= 0;
2826         int retval= WM_UI_HANDLER_CONTINUE;
2827
2828         mx= event->x;
2829         my= event->y;
2830         ui_window_to_block(data->region, block, &mx, &my);
2831
2832         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2833                 /* XXX hardcoded keymap check.... */
2834                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2835                         mx= but->x1;
2836                         click= 2;
2837                 }
2838                 else if(event->type == WHEELUPMOUSE && event->alt) {
2839                         mx= but->x2;
2840                         click= 2;
2841                 }
2842                 else if(event->val==KM_PRESS) {
2843                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2844                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2845                                 retval= WM_UI_HANDLER_BREAK;
2846                         }
2847                         /* alt-click on sides to get "arrows" like in NUM buttons, and match wheel usage above */
2848                         else if(event->type == LEFTMOUSE && event->alt) {
2849                                 int halfpos = (but->x1 + but->x2) / 2;
2850                                 click = 2;
2851                                 if (mx < halfpos)
2852                                         mx = but->x1;
2853                                 else
2854                                         mx = but->x2;
2855                         }
2856                         else if(event->type == LEFTMOUSE) {
2857                                 data->dragstartx= mx;
2858                                 data->draglastx= mx;
2859                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2860                                 retval= WM_UI_HANDLER_BREAK;
2861                         }
2862                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2863                                 click= 1;
2864                         else if (event->type == MINUSKEY && event->val==KM_PRESS) {
2865                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2866                                 data->value = -data->value;
2867                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2868                                 retval= WM_UI_HANDLER_BREAK;
2869                         }
2870                 }
2871         }
2872         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2873                 if(event->type == ESCKEY) {
2874                         data->cancel= 1;
2875                         data->escapecancel= 1;
2876                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2877                 }
2878                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2879                         if(data->dragchange)
2880                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2881                         else
2882                                 click= 1;
2883                 }
2884                 else if(event->type == MOUSEMOVE) {
2885                         if(ui_numedit_but_SLI(but, data, event->shift, event->ctrl, mx))
2886                                 ui_numedit_apply(C, block, but, data);
2887                 }
2888                 retval= WM_UI_HANDLER_BREAK;
2889         }
2890         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2891                 ui_do_but_textedit(C, block, but, data, event);
2892                 retval= WM_UI_HANDLER_BREAK;
2893         }
2894         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2895                 ui_do_but_textedit_select(C, block, but, data, event);
2896                 retval= WM_UI_HANDLER_BREAK;
2897         }
2898
2899         if(click) {
2900                 if (click==2) {
2901                         /* nudge slider to the left or right */
2902                         float f, tempf, softmin, softmax, softrange;
2903                         int temp;
2904                         
2905                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2906                         
2907                         softmin= but->softmin;
2908                         softmax= but->softmax;
2909                         softrange= softmax - softmin;
2910                         
2911                         tempf= data->value;
2912                         temp= (int)data->value;
2913
2914 #if 0
2915                         if(but->type==SLI) {
2916                                 f= (float)(mx-but->x1)/(but->x2-but->x1); /* same as below */
2917                         }
2918                         else
2919 #endif
2920                         {
2921                                 f= (float)(mx- but->x1)/(but->x2-but->x1);
2922                         }
2923                         
2924                         f= softmin + f*softrange;
2925                         
2926                         if(!ui_is_but_float(but)) {
2927                                 if(f<temp) temp--;
2928                                 else temp++;
2929                                 
2930                                 if(temp>=softmin && temp<=softmax)
2931                                         data->value= temp;
2932                                 else
2933                                         data->cancel= 1;
2934                         } 
2935                         else {
2936                                 if(f<tempf) tempf -= 0.01f;
2937                                 else tempf += 0.01f;
2938                                 
2939                                 if(tempf>=softmin && tempf<=softmax)
2940                                         data->value= tempf;
2941                                 else
2942                                         data->cancel= 1;
2943                         }
2944                         
2945                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2946                         retval= WM_UI_HANDLER_BREAK;
2947                 }
2948                 else {
2949                         /* edit the value directly */
2950                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2951                         retval= WM_UI_HANDLER_BREAK;
2952                 }
2953         }
2954         
2955         return retval;
2956 }
2957
2958 static int ui_do_but_SCROLL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2959 {
2960         int mx, my /*, click= 0 */;
2961         int retval= WM_UI_HANDLER_CONTINUE;
2962         int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2963         
2964         mx= event->x;
2965         my= event->y;
2966         ui_window_to_block(data->region, block, &mx, &my);
2967
2968         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2969                 if(event->val==KM_PRESS) {
2970                         if(event->type == LEFTMOUSE) {
2971                                 if(horizontal) {
2972                                         data->dragstartx= mx;
2973                                         data->draglastx= mx;
2974                                 }
2975                                 else {
2976                                         data->dragstartx= my;
2977                                         data->draglastx= my;
2978                                 }
2979                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2980                                 retval= WM_UI_HANDLER_BREAK;
2981                         }
2982                         /* UNUSED - otherwise code is ok, add back if needed */
2983 #if 0
2984                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2985                                 click= 1;
2986 #endif
2987                 }
2988         }
2989         else if(data->state == BUTTON_STATE_NUM_EDITING) {