replace fixed sizes with sizeof when passing string length since size wasn't always...
[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         double val;
1119         
1120         if(mode=='v' && but->lock)
1121                 return;
1122
1123         if(mode=='v') {
1124                 /* extract first line from clipboard in case of multi-line copies */
1125                 char *p, *pbuf= WM_clipboard_text_get(0);
1126                 p= pbuf;
1127                 if(p) {
1128                         int i = 0;
1129                         while (*p && *p!='\r' && *p!='\n' && i<UI_MAX_DRAW_STR) {
1130                                 buf[i++]=*p;
1131                                 p++;
1132                         }
1133                         buf[i]= 0;
1134                         MEM_freeN(pbuf);
1135                 }
1136         }
1137         
1138         /* numeric value */
1139         if ELEM4(but->type, NUM, NUMABS, NUMSLI, HSVSLI) {
1140                 
1141                 if(but->poin==NULL && but->rnapoin.data==NULL);
1142                 else if(mode=='c') {
1143                         if(ui_is_but_float(but))
1144                                 BLI_snprintf(buf, sizeof(buf), "%f", ui_get_but_val(but));
1145                         else
1146                                 BLI_snprintf(buf, sizeof(buf), "%d", (int)ui_get_but_val(but));
1147
1148                         WM_clipboard_text_set(buf, 0);
1149                 }
1150                 else {
1151                         if (sscanf(buf, " %lf ", &val) == 1) {
1152                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1153                                 data->value= val;
1154                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1155                         }
1156                 }
1157         }
1158
1159         /* RGB triple */
1160         else if(but->type==COL) {
1161                 float rgb[3];
1162                 
1163                 if(but->poin==NULL && but->rnapoin.data==NULL);
1164                 else if(mode=='c') {
1165
1166                         ui_get_but_vectorf(but, rgb);
1167                         BLI_snprintf(buf, sizeof(buf), "[%f, %f, %f]", rgb[0], rgb[1], rgb[2]);
1168                         WM_clipboard_text_set(buf, 0);
1169                         
1170                 }
1171                 else {
1172                         if (sscanf(buf, "[%f, %f, %f]", &rgb[0], &rgb[1], &rgb[2]) == 3) {
1173                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1174                                 ui_set_but_vectorf(but, rgb);
1175                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1176                         }
1177                 }
1178         }
1179
1180         /* text/string and ID data */
1181         else if(ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1182                 uiHandleButtonData *active_data= but->active;
1183
1184                 if(but->poin==NULL && but->rnapoin.data==NULL);
1185                 else if(mode=='c') {
1186                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1187                         BLI_strncpy(buf, active_data->str, UI_MAX_DRAW_STR);
1188                         WM_clipboard_text_set(active_data->str, 0);
1189                         active_data->cancel= 1;
1190                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1191                 }
1192                 else {
1193                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1194
1195                         if(ui_is_but_utf8(but)) BLI_strncpy_utf8(active_data->str, buf, active_data->maxlen);
1196                         else                    BLI_strncpy(active_data->str, buf, active_data->maxlen);
1197
1198                         if(but->type == SEARCH_MENU) {
1199                                 /* else uiSearchboxData.active member is not updated [#26856] */
1200                                 ui_searchbox_update(C, data->searchbox, but, 1);
1201                         }
1202                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1203                 }
1204         }
1205         /* colorband (not supported by system clipboard) */
1206         else if(but->type==BUT_COLORBAND) {
1207                 if(mode=='c') {
1208                         if(but->poin==NULL)
1209                                 return;
1210
1211                         memcpy(&but_copypaste_coba, but->poin, sizeof(ColorBand));
1212                 }
1213                 else {
1214                         if(but_copypaste_coba.tot==0)
1215                                 return;
1216
1217                         if(!but->poin)
1218                                 but->poin= MEM_callocN(sizeof(ColorBand), "colorband");
1219
1220                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1221                         memcpy(data->coba, &but_copypaste_coba, sizeof(ColorBand) );
1222                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1223                 }
1224         }
1225         /* operator button (any type) */
1226         else if (but->optype) {
1227                 if(mode=='c') {
1228                         PointerRNA *opptr;
1229                         char *str;
1230                         opptr= uiButGetOperatorPtrRNA(but); /* allocated when needed, the button owns it */
1231
1232                         str= WM_operator_pystring(C, but->optype, opptr, 0);
1233
1234                         WM_clipboard_text_set(str, 0);
1235
1236                         MEM_freeN(str);
1237                 }
1238         }
1239 }
1240
1241 /* ************* in-button text selection/editing ************* */
1242
1243 /* return 1 if char ch is special character, otherwise return 0 */
1244 static uiButtonDelimType test_special_char(const char ch)
1245 {
1246         if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
1247                 return BUTTON_DELIM_ALPHA;
1248         }
1249
1250         switch(ch) {
1251                 case ',':
1252                 case '.':
1253                         return BUTTON_DELIM_PUNCT;
1254
1255                 case '{':
1256                 case '}':
1257                 case '[':
1258                 case ']':
1259                 case '(':
1260                 case ')':
1261                         return BUTTON_DELIM_BRACE;
1262
1263                 case '+':
1264                 case '-':
1265                 case '=':
1266                 case '~':
1267                 case '%':
1268                 case '/':
1269                 case '<':
1270                 case '>':
1271                 case '^':
1272                 case '*':
1273                 case '&':
1274                         return BUTTON_DELIM_OPERATOR;
1275
1276                 case '\'':
1277                 case '\"': // " - an extra closing one for Aligorith's text editor
1278                         return BUTTON_DELIM_QUOTE;
1279
1280                 case ' ':
1281                         return BUTTON_DELIM_WHITESPACE;
1282
1283                 case '\\':
1284                 case '!':
1285                 case '@':
1286                 case '#':
1287                 case '$':
1288                 case ':':
1289                 case ';':
1290                 case '?':
1291                 case '_':
1292                         return BUTTON_DELIM_OTHER;
1293
1294                 default:
1295                         break;
1296         }
1297         return BUTTON_DELIM_NONE;
1298 }
1299
1300 static int ui_textedit_step_next_utf8(const char *str, size_t maxlen, short *pos)
1301 {
1302         const char *str_end= str + (maxlen + 1);
1303         const char *str_pos= str + (*pos);
1304         const char *str_next= BLI_str_find_next_char_utf8(str_pos, str_end);
1305         if (str_next) {
1306                 (*pos) += (str_next - str_pos);
1307                 if((*pos) > maxlen) (*pos)= maxlen;
1308                 return TRUE;
1309         }
1310
1311         return FALSE;
1312 }
1313
1314 static int ui_textedit_step_prev_utf8(const char *str, size_t UNUSED(maxlen), short *pos)
1315 {
1316         if((*pos) > 0) {
1317                 const char *str_pos= str + (*pos);
1318                 const char *str_prev= BLI_str_find_prev_char_utf8(str, str_pos);
1319                 if (str_prev) {
1320                         (*pos) -= (str_pos - str_prev);
1321                         return TRUE;
1322                 }
1323         }
1324
1325         return FALSE;
1326 }
1327
1328 static void ui_textedit_step_utf8(const char *str, size_t maxlen,
1329                                   short *pos, const char direction,
1330                                   uiButtonJumpType jump)
1331 {
1332         const short pos_prev= *pos;
1333
1334         if(direction) { /* right*/
1335                 if(jump != BUTTON_EDIT_JUMP_NONE) {
1336                         const uiButtonDelimType is_special= (*pos) < maxlen ? test_special_char(str[(*pos)]) : BUTTON_DELIM_NONE;
1337                         /* jump between special characters (/,\,_,-, etc.),
1338                          * look at function test_special_char() for complete
1339                          * list of special character, ctr -> */
1340                         while((*pos) < maxlen) {
1341                                 if (ui_textedit_step_next_utf8(str, maxlen, pos)) {
1342                                         if((jump != BUTTON_EDIT_JUMP_ALL) && (is_special != test_special_char(str[(*pos)]))) break;
1343                                 }
1344                                 else {
1345                                         break; /* unlikely but just incase */
1346                                 }
1347                         }
1348                 }
1349                 else {
1350                         ui_textedit_step_next_utf8(str, maxlen, pos);
1351                 }
1352         }
1353         else { /* left */
1354                 if(jump != BUTTON_EDIT_JUMP_NONE) {
1355                         const uiButtonDelimType is_special= (*pos) > 1 ? test_special_char(str[(*pos) - 1]) : BUTTON_DELIM_NONE;
1356                         /* left only: compensate for index/change in direction */
1357                         ui_textedit_step_prev_utf8(str, maxlen, pos);
1358
1359                         /* jump between special characters (/,\,_,-, etc.),
1360                          * look at function test_special_char() for complete
1361                          * list of special character, ctr -> */
1362                         while ((*pos) > 0) {
1363                                 if (ui_textedit_step_prev_utf8(str, maxlen, pos)) {
1364                                         if((jump != BUTTON_EDIT_JUMP_ALL) && (is_special != test_special_char(str[(*pos)]))) break;
1365                                 }
1366                                 else {
1367                                         break;
1368                                 }
1369                         }
1370
1371                         /* left only: compensate for index/change in direction */
1372                         if(((*pos) != 0) && ABS(pos_prev - (*pos)) > 1) {
1373                                 ui_textedit_step_next_utf8(str, maxlen, pos);
1374                         }
1375                 }
1376                 else {
1377                         ui_textedit_step_prev_utf8(str, maxlen, pos);
1378                 }
1379         }
1380 }
1381
1382 static int ui_textedit_delete_selection(uiBut *but, uiHandleButtonData *data)
1383 {
1384         char *str= data->str;
1385         int len= strlen(str);
1386         int change= 0;
1387         if(but->selsta != but->selend && len) {
1388                 memmove( str+but->selsta, str+but->selend, (len - but->selend) + 1 );
1389                 change= 1;
1390         }
1391         
1392         but->pos = but->selend = but->selsta;
1393         return change;
1394 }
1395
1396 /* note, but->block->aspect is used here, when drawing button style is getting scaled too */
1397 static void ui_textedit_set_cursor_pos(uiBut *but, uiHandleButtonData *data, short x)
1398 {
1399         uiStyle *style= UI_GetStyle();  // XXX pass on as arg
1400         uiFontStyle *fstyle = &style->widget;
1401         int startx= but->x1;
1402         char *origstr;
1403
1404         uiStyleFontSet(fstyle);
1405
1406         if (fstyle->kerning==1) /* for BLF_width */
1407                 BLF_enable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
1408         
1409         origstr= MEM_callocN(sizeof(char)*data->maxlen, "ui_textedit origstr");
1410         
1411         BLI_strncpy(origstr, but->drawstr, data->maxlen);
1412         
1413         /* XXX solve generic */
1414         if(but->type==NUM || but->type==NUMSLI)
1415                 startx += (int)(0.5f*(but->y2 - but->y1));
1416         else if(ELEM(but->type, TEX, SEARCH_MENU)) {
1417                 startx += 5;
1418                 if (but->flag & UI_HAS_ICON)
1419                         startx += UI_DPI_ICON_SIZE;
1420         }
1421         
1422         /* mouse dragged outside the widget to the left */
1423         if (x < startx && but->ofs > 0) {       
1424                 short i= but->ofs;
1425
1426                 origstr[but->ofs] = 0;
1427                 
1428                 while (i > 0) {
1429                         if (ui_textedit_step_prev_utf8(origstr, but->ofs, &i)) {
1430                                 if (BLF_width(fstyle->uifont_id, origstr+i) > (startx - x)*0.25f) break;        // 0.25 == scale factor for less sensitivity
1431                         }
1432                         else {
1433                                 break; /* unlikely but possible */
1434                         }
1435                 }
1436                 but->ofs = i;
1437                 but->pos = but->ofs;
1438         }
1439         /* mouse inside the widget */
1440         else if (x >= startx) {
1441                 const float aspect_sqrt= sqrtf(but->block->aspect);
1442                 
1443                 but->pos= strlen(origstr)-but->ofs;
1444                 
1445                 /* XXX does not take zoom level into account */
1446                 while (startx + aspect_sqrt * BLF_width(fstyle->uifont_id, origstr+but->ofs) > x) {
1447                         if (but->pos <= 0) break;
1448                         if (ui_textedit_step_prev_utf8(origstr, but->ofs, &but->pos)) {
1449                                 origstr[but->pos+but->ofs] = 0;
1450                         }
1451                         else {
1452                                 break; /* unlikely but possible */
1453                         }
1454                 }
1455                 but->pos += but->ofs;
1456                 if(but->pos<0) but->pos= 0;
1457         }
1458         
1459         if (fstyle->kerning == 1)
1460                 BLF_disable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
1461         
1462         MEM_freeN(origstr);
1463 }
1464
1465 static void ui_textedit_set_cursor_select(uiBut *but, uiHandleButtonData *data, short x)
1466 {
1467         if (x > data->selstartx) data->selextend = EXTEND_RIGHT;
1468         else if (x < data->selstartx) data->selextend = EXTEND_LEFT;
1469
1470         ui_textedit_set_cursor_pos(but, data, x);
1471                                                 
1472         if (data->selextend == EXTEND_RIGHT) but->selend = but->pos;
1473         if (data->selextend == EXTEND_LEFT) but->selsta = but->pos;
1474
1475         ui_check_but(but);
1476 }
1477
1478 /* this is used for both utf8 and ascii, its meant to be used for single keys,
1479  * notie the buffer is either copied or not, so its not suitable for pasting in
1480  * - campbell */
1481 static int ui_textedit_type_buf(uiBut *but, uiHandleButtonData *data,
1482                                 const char *utf8_buf, int utf8_buf_len)
1483 {
1484         char *str;
1485         int len, changed= 0;
1486
1487         str= data->str;
1488         len= strlen(str);
1489
1490         if(len-(but->selend - but->selsta)+1 <= data->maxlen) {
1491                 int step= utf8_buf_len;
1492
1493                 /* type over the current selection */
1494                 if ((but->selend - but->selsta) > 0) {
1495                         changed= ui_textedit_delete_selection(but, data);
1496                         len= strlen(str);
1497                 }
1498
1499                 if(len + step < data->maxlen) {
1500                         memmove(&str[but->pos + step], &str[but->pos], (len + 1) - but->pos);
1501                         memcpy(&str[but->pos], utf8_buf, step * sizeof(char));
1502                         but->pos += step;
1503                         changed= 1;
1504                 }
1505         }
1506
1507         return changed;
1508 }
1509
1510 static int ui_textedit_type_ascii(uiBut *but, uiHandleButtonData *data, char ascii)
1511 {
1512         char buf[2]= {ascii, '\0'};
1513
1514         if (ui_is_but_utf8(but) && (BLI_str_utf8_size(buf) == -1)) {
1515                 printf("%s: entering invalid ascii char into an ascii key (%d)\n",
1516                        __func__, (int)(unsigned char)ascii);
1517
1518                 return 0;
1519         }
1520
1521         /* in some cases we want to allow invalid utf8 chars */
1522         return ui_textedit_type_buf(but, data, buf, 1);
1523 }
1524
1525 static void ui_textedit_move(uiBut *but, uiHandleButtonData *data, int direction, int select, uiButtonJumpType jump)
1526 {
1527         const char *str= data->str;
1528         const int len= strlen(str);
1529         const int pos_prev= but->pos;
1530         const int has_sel= (but->selend - but->selsta) > 0;
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         /* cut & copy */
1710         else if (copy || cut) {
1711                 /* copy the contents to the copypaste buffer */
1712                 for(x= but->selsta; x <= but->selend; x++) {
1713                         if (x==but->selend)
1714                                 buf[x] = '\0';
1715                         else
1716                                 buf[(x - but->selsta)] = str[x];
1717                 }
1718
1719                 WM_clipboard_text_set(buf, 0);
1720                 
1721                 /* for cut only, delete the selection afterwards */
1722                 if(cut)
1723                         if((but->selend - but->selsta) > 0)
1724                                 changed= ui_textedit_delete_selection(but, data);
1725         } 
1726
1727         return changed;
1728 }
1729
1730 static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
1731 {
1732         if(data->str) {
1733                 MEM_freeN(data->str);
1734                 data->str= NULL;
1735         }
1736
1737         /* retrieve string */
1738         data->maxlen= ui_get_but_string_max_length(but);
1739         data->str= MEM_callocN(sizeof(char)*data->maxlen + 1, "textedit str");
1740         ui_get_but_string(but, data->str, data->maxlen);
1741
1742         if(ELEM3(but->type, NUM, NUMABS, NUMSLI)) {
1743                 ui_convert_to_unit_alt_name(but, data->str, data->maxlen);
1744         }
1745
1746         data->origstr= BLI_strdup(data->str);
1747         data->selextend= 0;
1748         data->selstartx= 0;
1749
1750         /* set cursor pos to the end of the text */
1751         but->editstr= data->str;
1752         but->pos= strlen(data->str);
1753         but->selsta= 0;
1754         but->selend= strlen(data->str);
1755
1756         /* optional searchbox */
1757         if(but->type==SEARCH_MENU) {
1758                 data->searchbox= ui_searchbox_create(C, data->region, but);
1759                 ui_searchbox_update(C, data->searchbox, but, 1); /* 1= reset */
1760         }
1761         
1762         ui_check_but(but);
1763         
1764         WM_cursor_modal(CTX_wm_window(C), BC_TEXTEDITCURSOR);
1765 }
1766
1767 static void ui_textedit_end(bContext *C, uiBut *but, uiHandleButtonData *data)
1768 {
1769         if(but) {
1770                 if(ui_is_but_utf8(but)) {
1771                         int strip= BLI_utf8_invalid_strip(but->editstr, strlen(but->editstr));
1772                         /* not a file?, strip non utf-8 chars */
1773                         if(strip) {
1774                                 /* wont happen often so isnt that annoying to keep it here for a while */
1775                                 printf("%s: invalid utf8 - stripped chars %d\n", __func__, strip);
1776                         }
1777                 }
1778                 
1779                 if(data->searchbox) {
1780                         if(data->cancel==0)
1781                                 ui_searchbox_apply(but, data->searchbox);
1782
1783                         ui_searchbox_free(C, data->searchbox);
1784                         data->searchbox= NULL;
1785                 }
1786                 
1787                 but->editstr= NULL;
1788                 but->pos= -1;
1789         }
1790         
1791         WM_cursor_restore(CTX_wm_window(C));
1792 }
1793
1794 static void ui_textedit_next_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1795 {
1796         uiBut *but;
1797
1798         /* label and roundbox can overlap real buttons (backdrops...) */
1799         if(ELEM4(actbut->type, LABEL, SEPR, ROUNDBOX, LISTBOX))
1800                 return;
1801
1802         for(but= actbut->next; but; but= but->next) {
1803                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1804                         if(!(but->flag & UI_BUT_DISABLED)) {
1805                                 data->postbut= but;
1806                                 data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1807                                 return;
1808                         }
1809                 }
1810         }
1811         for(but= block->buttons.first; but!=actbut; but= but->next) {
1812                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1813                         if(!(but->flag & UI_BUT_DISABLED)) {
1814                                 data->postbut= but;
1815                                 data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1816                                 return;
1817                         }
1818                 }
1819         }
1820 }
1821
1822 static void ui_textedit_prev_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1823 {
1824         uiBut *but;
1825
1826         /* label and roundbox can overlap real buttons (backdrops...) */
1827         if(ELEM4(actbut->type, LABEL, SEPR, ROUNDBOX, LISTBOX))
1828                 return;
1829
1830         for(but= actbut->prev; but; but= but->prev) {
1831                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1832                         if(!(but->flag & UI_BUT_DISABLED)) {
1833                                 data->postbut= but;
1834                                 data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1835                                 return;
1836                         }
1837                 }
1838         }
1839         for(but= block->buttons.last; but!=actbut; but= but->prev) {
1840                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1841                         if(!(but->flag & UI_BUT_DISABLED)) {
1842                                 data->postbut= but;
1843                                 data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1844                                 return;
1845                         }
1846                 }
1847         }
1848 }
1849
1850
1851 static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1852 {
1853         int mx, my, changed= 0, inbox=0, update= 0, retval= WM_UI_HANDLER_CONTINUE;
1854
1855         switch(event->type) {
1856                 case WHEELUPMOUSE:
1857                 case WHEELDOWNMOUSE:
1858                 case MOUSEMOVE:
1859                         if(data->searchbox)
1860                                 ui_searchbox_event(C, data->searchbox, but, event);
1861                         
1862                         break;
1863                 case RIGHTMOUSE:
1864                 case ESCKEY:
1865                         data->cancel= 1;
1866                         data->escapecancel= 1;
1867                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1868                         retval= WM_UI_HANDLER_BREAK;
1869                         break;
1870                 case LEFTMOUSE: {
1871                         
1872                         /* exit on LMB only on RELEASE for searchbox, to mimic other popups, and allow multiple menu levels */
1873                         if(data->searchbox)
1874                                 inbox= ui_searchbox_inside(data->searchbox, event->x, event->y);
1875
1876                         if(event->val==KM_PRESS) {
1877                                 mx= event->x;
1878                                 my= event->y;
1879                                 ui_window_to_block(data->region, block, &mx, &my);
1880
1881                                 if (ui_but_contains_pt(but, mx, my)) {
1882                                         ui_textedit_set_cursor_pos(but, data, mx);
1883                                         but->selsta = but->selend = but->pos;
1884                                         data->selstartx= mx;
1885
1886                                         button_activate_state(C, but, BUTTON_STATE_TEXT_SELECTING);
1887                                         retval= WM_UI_HANDLER_BREAK;
1888                                 }
1889                                 else if(inbox==0) {
1890                                         /* if searchbox, click outside will cancel */
1891                                         if(data->searchbox)
1892                                                 data->cancel= data->escapecancel= 1;
1893                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1894                                         retval= WM_UI_HANDLER_BREAK;
1895                                 }
1896                         }
1897                         else if(inbox) {
1898                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1899                                 retval= WM_UI_HANDLER_BREAK;
1900                         }
1901                         break;
1902                 }
1903         }
1904
1905         if(event->val==KM_PRESS) {
1906                 switch (event->type) {
1907                         case VKEY:
1908                         case XKEY:
1909                         case CKEY:
1910                                 if(event->ctrl || event->oskey) {
1911                                         if(event->type == VKEY)
1912                                                 changed= ui_textedit_copypaste(but, data, 1, 0, 0);
1913                                         else if(event->type == CKEY)
1914                                                 changed= ui_textedit_copypaste(but, data, 0, 1, 0);
1915                                         else if(event->type == XKEY)
1916                                                 changed= ui_textedit_copypaste(but, data, 0, 0, 1);
1917
1918                                         retval= WM_UI_HANDLER_BREAK;
1919                                 }
1920                                 break;
1921                         case RIGHTARROWKEY:
1922                                 ui_textedit_move(but, data, 1, event->shift, event->ctrl ? BUTTON_EDIT_JUMP_DELIM : BUTTON_EDIT_JUMP_NONE);
1923                                 retval= WM_UI_HANDLER_BREAK;
1924                                 break;
1925                         case LEFTARROWKEY:
1926                                 ui_textedit_move(but, data, 0, event->shift, event->ctrl ? BUTTON_EDIT_JUMP_DELIM : BUTTON_EDIT_JUMP_NONE);
1927                                 retval= WM_UI_HANDLER_BREAK;
1928                                 break;
1929                         case DOWNARROWKEY:
1930                                 if(data->searchbox) {
1931                                         ui_searchbox_event(C, data->searchbox, but, event);
1932                                         break;
1933                                 }
1934                                 /* pass on purposedly */
1935                         case ENDKEY:
1936                                 ui_textedit_move(but, data, 1, event->shift, BUTTON_EDIT_JUMP_ALL);
1937                                 retval= WM_UI_HANDLER_BREAK;
1938                                 break;
1939                         case UPARROWKEY:
1940                                 if(data->searchbox) {
1941                                         ui_searchbox_event(C, data->searchbox, but, event);
1942                                         break;
1943                                 }
1944                                 /* pass on purposedly */
1945                         case HOMEKEY:
1946                                 ui_textedit_move(but, data, 0, event->shift, BUTTON_EDIT_JUMP_ALL);
1947                                 retval= WM_UI_HANDLER_BREAK;
1948                                 break;
1949                         case PADENTER:
1950                         case RETKEY:
1951                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1952                                 retval= WM_UI_HANDLER_BREAK;
1953                                 break;
1954                         case DELKEY:
1955                                 changed= ui_textedit_delete(but, data, 1, event->ctrl ? BUTTON_EDIT_JUMP_DELIM : BUTTON_EDIT_JUMP_NONE);
1956                                 retval= WM_UI_HANDLER_BREAK;
1957                                 break;
1958
1959                         case BACKSPACEKEY:
1960                                 changed= ui_textedit_delete(but, data, 0, event->shift ? BUTTON_EDIT_JUMP_ALL :  (event->ctrl ? BUTTON_EDIT_JUMP_DELIM : BUTTON_EDIT_JUMP_NONE));
1961                                 retval= WM_UI_HANDLER_BREAK;
1962                                 break;
1963                                 
1964                         case TABKEY:
1965                                 /* there is a key conflict here, we can't tab with autocomplete */
1966                                 if(but->autocomplete_func || data->searchbox) {
1967                                         changed= ui_textedit_autocomplete(C, but, data);
1968                                         update= 1; /* do live update for tab key */
1969                                 }
1970                                 /* the hotkey here is not well defined, was G.qual so we check all */
1971                                 else if(event->shift || event->ctrl || event->alt || event->oskey) {
1972                                         ui_textedit_prev_but(block, but, data);
1973                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1974                                 }
1975                                 else {
1976                                         ui_textedit_next_but(block, but, data);
1977                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1978                                 }
1979                                 retval= WM_UI_HANDLER_BREAK;
1980                                 break;
1981                 }
1982
1983                 if((event->ascii || event->utf8_buf[0]) && (retval == WM_UI_HANDLER_CONTINUE)) {
1984                         char ascii = event->ascii;
1985                         const char *utf8_buf= event->utf8_buf;
1986
1987                         /* exception that's useful for number buttons, some keyboard
1988                            numpads have a comma instead of a period */
1989                         if(ELEM3(but->type, NUM, NUMABS, NUMSLI)) { /* could use data->min*/
1990                                 if(event->type == PADPERIOD && ascii == ',') {
1991                                         ascii = '.';
1992                                         utf8_buf= NULL; /* force ascii fallback */
1993                                 }
1994                         }
1995
1996                         if(utf8_buf && utf8_buf[0]) {
1997                                 int utf8_buf_len= BLI_str_utf8_size(utf8_buf);
1998                                 /* keep this printf until utf8 is well tested */
1999                                 if (utf8_buf_len != 1) {
2000                                         printf("%s: utf8 char '%.*s'\n", __func__, utf8_buf_len, utf8_buf);
2001                                 }
2002
2003                                 // strcpy(utf8_buf, "12345");
2004                                 changed= ui_textedit_type_buf(but, data, event->utf8_buf, utf8_buf_len);
2005                         }
2006                         else {
2007                                 changed= ui_textedit_type_ascii(but, data, ascii);
2008                         }
2009
2010                         retval= WM_UI_HANDLER_BREAK;
2011                         
2012                 }
2013                 /* textbutton with magnifier icon: do live update for search button */
2014                 if(but->icon==ICON_VIEWZOOM)
2015                         update= 1;
2016         }
2017
2018         if(changed) {
2019                 /* only update when typing for TAB key */
2020                 if(update && data->interactive) ui_apply_button(C, block, but, data, 1);
2021                 else ui_check_but(but);
2022                 but->changed= TRUE;
2023                 
2024                 if(data->searchbox)
2025                         ui_searchbox_update(C, data->searchbox, but, 1); /* 1 = reset */
2026         }
2027
2028         if(changed || (retval == WM_UI_HANDLER_BREAK))
2029                 ED_region_tag_redraw(data->region);
2030 }
2031
2032 static void ui_do_but_textedit_select(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2033 {
2034         int mx, my, retval= WM_UI_HANDLER_CONTINUE;
2035
2036         switch(event->type) {
2037                 case MOUSEMOVE: {
2038                         mx= event->x;
2039                         my= event->y;
2040                         ui_window_to_block(data->region, block, &mx, &my);
2041
2042                         ui_textedit_set_cursor_select(but, data, mx);
2043                         retval= WM_UI_HANDLER_BREAK;
2044                         break;
2045                 }
2046                 case LEFTMOUSE:
2047                         if(event->val == KM_RELEASE)
2048                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2049                         retval= WM_UI_HANDLER_BREAK;
2050                         break;
2051         }
2052
2053         if(retval == WM_UI_HANDLER_BREAK) {
2054                 ui_check_but(but);
2055                 ED_region_tag_redraw(data->region);
2056         }
2057 }
2058
2059 /* ************* number editing for various types ************* */
2060
2061 static void ui_numedit_begin(uiBut *but, uiHandleButtonData *data)
2062 {
2063         if(but->type == BUT_CURVE) {
2064                 but->editcumap= (CurveMapping*)but->poin;
2065         }
2066         else if(but->type == BUT_COLORBAND) {
2067                 data->coba= (ColorBand*)but->poin;
2068                 but->editcoba= data->coba;
2069         }
2070         else if(ELEM3(but->type, BUT_NORMAL, HSVCUBE, HSVCIRCLE)) {
2071                 ui_get_but_vectorf(but, data->origvec);
2072                 copy_v3_v3(data->vec, data->origvec);
2073                 but->editvec= data->vec;
2074         }
2075         else {
2076                 float softrange, softmin, softmax;
2077
2078                 data->startvalue= ui_get_but_val(but);
2079                 data->origvalue= data->startvalue;
2080                 data->value= data->origvalue;
2081                 but->editval= &data->value;
2082
2083                 softmin= but->softmin;
2084                 softmax= but->softmax;
2085                 softrange= softmax - softmin;
2086
2087                 data->dragfstart= (softrange == 0.0f)? 0.0f: ((float)data->value - softmin)/softrange;
2088                 data->dragf= data->dragfstart;
2089         }
2090
2091         data->dragchange= 0;
2092         data->draglock= 1;
2093 }
2094
2095 static void ui_numedit_end(uiBut *but, uiHandleButtonData *data)
2096 {
2097         but->editval= NULL;
2098         but->editvec= NULL;
2099         but->editcoba= NULL;
2100         but->editcumap= NULL;
2101
2102         data->dragstartx= 0;
2103         data->draglastx= 0;
2104         data->dragchange= 0;
2105         data->dragcbd= NULL;
2106         data->dragsel= 0;
2107 }
2108
2109 static void ui_numedit_apply(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data)
2110 {
2111         if(data->interactive) ui_apply_button(C, block, but, data, 1);
2112         else ui_check_but(but);
2113
2114         ED_region_tag_redraw(data->region);
2115 }
2116
2117 /* ****************** menu opening for various types **************** */
2118
2119 static void ui_blockopen_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
2120 {
2121         uiBlockCreateFunc func= NULL;
2122         uiBlockHandleCreateFunc handlefunc= NULL;
2123         uiMenuCreateFunc menufunc= NULL;
2124         char *menustr= NULL;
2125         void *arg= NULL;
2126
2127         switch(but->type) {
2128                 case BLOCK:
2129                 case PULLDOWN:
2130                         if(but->menu_create_func) {
2131                                 menufunc= but->menu_create_func;
2132                                 arg= but->poin;
2133                         }
2134                         else {
2135                                 func= but->block_create_func;
2136                                 arg= but->poin?but->poin:but->func_argN;
2137                         }
2138                         break;
2139                 case MENU:
2140                         if(but->menu_create_func) {
2141                                 menufunc= but->menu_create_func;
2142                                 arg= but->poin;
2143                         }
2144                         else {
2145                                 data->origvalue= ui_get_but_val(but);
2146                                 data->value= data->origvalue;
2147                                 but->editval= &data->value;
2148
2149                                 menustr= but->str;
2150                         }
2151                         break;
2152                 case ICONROW:
2153                         menufunc= ui_block_func_ICONROW;
2154                         arg= but;
2155                         break;
2156                 case ICONTEXTROW:
2157                         menufunc= ui_block_func_ICONTEXTROW;
2158                         arg= but;
2159                         break;
2160                 case COL:
2161                         ui_get_but_vectorf(but, data->origvec);
2162                         copy_v3_v3(data->vec, data->origvec);
2163                         but->editvec= data->vec;
2164
2165                         handlefunc= ui_block_func_COL;
2166                         arg= but;
2167                         break;
2168         }
2169
2170         if(func || handlefunc) {
2171                 data->menu= ui_popup_block_create(C, data->region, but, func, handlefunc, arg);
2172                 if(but->block->handle)
2173                         data->menu->popup= but->block->handle->popup;
2174         }
2175         else if(menufunc || menustr) {
2176                 data->menu= ui_popup_menu_create(C, data->region, but, menufunc, arg, menustr);
2177                 if(but->block->handle)
2178                         data->menu->popup= but->block->handle->popup;
2179         }
2180
2181         /* this makes adjacent blocks auto open from now on */
2182         //if(but->block->auto_open==0) but->block->auto_open= 1;
2183 }
2184
2185 static void ui_blockopen_end(bContext *C, uiBut *but, uiHandleButtonData *data)
2186 {
2187         if(but) {
2188                 but->editval= NULL;
2189                 but->editvec= NULL;
2190
2191                 but->block->auto_open_last= PIL_check_seconds_timer();
2192         }
2193
2194         if(data->menu) {
2195                 ui_popup_block_free(C, data->menu);
2196                 data->menu= NULL;
2197         }
2198 }
2199
2200 /* ***************** events for different button types *************** */
2201
2202 static int ui_do_but_BUT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2203 {
2204         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2205                 if(event->type == LEFTMOUSE && event->val==KM_PRESS) {
2206                         button_activate_state(C, but, BUTTON_STATE_WAIT_RELEASE);
2207                         return WM_UI_HANDLER_BREAK;
2208                 }
2209                 else if(event->type == LEFTMOUSE && but->block->handle) {
2210                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2211                         return WM_UI_HANDLER_BREAK;
2212                 }
2213                 else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS) {
2214                         button_activate_state(C, but, BUTTON_STATE_WAIT_FLASH);
2215                         return WM_UI_HANDLER_BREAK;
2216                 }
2217         }
2218         else if(data->state == BUTTON_STATE_WAIT_RELEASE) {
2219                 if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2220                         if(!(but->flag & UI_SELECT))
2221                                 data->cancel= 1;
2222                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2223                         return WM_UI_HANDLER_BREAK;
2224                 }
2225         }
2226
2227         return WM_UI_HANDLER_CONTINUE;
2228 }
2229
2230 static int ui_do_but_HOTKEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2231 {
2232         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2233                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2234                         but->drawstr[0]= 0;
2235                         but->modifier_key= 0;
2236                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
2237                         return WM_UI_HANDLER_BREAK;
2238                 }
2239         }
2240         else if(data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2241                 
2242                 if(event->type == MOUSEMOVE)
2243                         return WM_UI_HANDLER_CONTINUE;
2244                 
2245                 if(event->type == LEFTMOUSE && event->val==KM_PRESS) {
2246                         /* only cancel if click outside the button */
2247                         if(ui_mouse_inside_button(but->active->region, but, event->x, event->y) == 0) {
2248                                 /* data->cancel doesnt work, this button opens immediate */
2249                                 if(but->flag & UI_BUT_IMMEDIATE)
2250                                         ui_set_but_val(but, 0);
2251                                 else
2252                                         data->cancel= 1;
2253                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2254                                 return WM_UI_HANDLER_BREAK;
2255                         }
2256                 }
2257                 
2258                 /* always set */
2259                 but->modifier_key = 0;
2260                 if(event->shift) but->modifier_key |= KM_SHIFT;
2261                 if(event->alt)   but->modifier_key |= KM_ALT;
2262                 if(event->ctrl)  but->modifier_key |= KM_CTRL;
2263                 if(event->oskey) but->modifier_key |= KM_OSKEY;
2264
2265                 ui_check_but(but);
2266                 ED_region_tag_redraw(data->region);
2267                         
2268                 if(event->val==KM_PRESS) {
2269                         if(ISHOTKEY(event->type)) { 
2270                                 
2271                                 if(WM_key_event_string(event->type)[0])
2272                                         ui_set_but_val(but, event->type);
2273                                 else
2274                                         data->cancel= 1;
2275                                 
2276                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2277                                 return WM_UI_HANDLER_BREAK;
2278                         }
2279                         else if(event->type == ESCKEY) {
2280                                 data->cancel= 1;
2281                                 data->escapecancel= 1;
2282                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2283                         }
2284                         
2285                 }
2286         }
2287         
2288         return WM_UI_HANDLER_CONTINUE;
2289 }
2290
2291 static int ui_do_but_KEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2292 {
2293         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2294                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2295                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
2296                         return WM_UI_HANDLER_BREAK;
2297                 }
2298         }
2299         else if(data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2300                 if(event->type == MOUSEMOVE)
2301                         return WM_UI_HANDLER_CONTINUE;
2302
2303                 if(event->val==KM_PRESS) {
2304                         if(WM_key_event_string(event->type)[0])
2305                                 ui_set_but_val(but, event->type);
2306                         else
2307                                 data->cancel= 1;
2308
2309                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2310                 }
2311         }
2312
2313         return WM_UI_HANDLER_CONTINUE;
2314 }
2315
2316 static int ui_do_but_TEX(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2317 {
2318         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2319                 if(ELEM(event->type, LEFTMOUSE, EVT_BUT_OPEN) && event->val==KM_PRESS) {
2320                         if(but->dt == UI_EMBOSSN && !event->ctrl);
2321                         else {
2322                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2323                                 return WM_UI_HANDLER_BREAK;
2324                         }
2325                 }
2326         }
2327         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2328                 ui_do_but_textedit(C, block, but, data, event);
2329                 return WM_UI_HANDLER_BREAK;
2330         }
2331         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2332                 ui_do_but_textedit_select(C, block, but, data, event);
2333                 return WM_UI_HANDLER_BREAK;
2334         }
2335
2336         return WM_UI_HANDLER_CONTINUE;
2337 }
2338
2339 static int ui_do_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2340 {
2341         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2342                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2343                         data->togdual= event->ctrl;
2344                         data->togonly= !event->shift;
2345                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2346                         return WM_UI_HANDLER_BREAK;
2347                 }
2348         }
2349         return WM_UI_HANDLER_CONTINUE;
2350 }
2351
2352 static int ui_do_but_EXIT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2353 {
2354         
2355         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2356
2357                 /* first handle click on icondrag type button */
2358                 if(event->type==LEFTMOUSE && but->dragpoin) {
2359                         if(ui_but_mouse_inside_icon(but, data->region, event)) {
2360                                 
2361                                 /* tell the button to wait and keep checking further events to
2362                                  * see if it should start dragging */
2363                                 button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
2364                                 data->dragstartx= event->x;
2365                                 data->dragstarty= event->y;
2366                                 return WM_UI_HANDLER_CONTINUE;
2367                         }
2368                 }
2369                 
2370                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2371                         int ret = WM_UI_HANDLER_BREAK;
2372                         /* XXX (a bit ugly) Special case handling for filebrowser drag button */
2373                         if(but->dragpoin && but->imb && ui_but_mouse_inside_icon(but, data->region, event)) {
2374                                 ret = WM_UI_HANDLER_CONTINUE;
2375                         }
2376                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2377                         return ret;
2378                 }
2379         }
2380         else if(data->state == BUTTON_STATE_WAIT_DRAG) {
2381                 
2382                 /* this function also ends state */
2383                 if(ui_but_start_drag(C, but, data, event)) {
2384                         return WM_UI_HANDLER_BREAK;
2385                 }
2386                 
2387                 /* If the mouse has been pressed and released, getting to 
2388                  * this point without triggering a drag, then clear the 
2389                  * drag state for this button and continue to pass on the event */
2390                 if(event->type==LEFTMOUSE && event->val==KM_RELEASE) {
2391                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2392                         return WM_UI_HANDLER_CONTINUE;
2393                 }
2394                 
2395                 /* while waiting for a drag to be triggered, always block 
2396                  * other events from getting handled */
2397                 return WM_UI_HANDLER_BREAK;
2398         }
2399         
2400         return WM_UI_HANDLER_CONTINUE;
2401 }
2402
2403 /* var names match ui_numedit_but_NUM */
2404 static float ui_numedit_apply_snapf(uiBut *but, float tempf, float softmin, float softmax, float softrange, int snap)
2405 {
2406         if(tempf==softmin || tempf==softmax || snap==0) {
2407                 /* pass */
2408         }
2409         else {
2410                 float fac= 1.0f;
2411                 
2412                 if(ui_is_but_unit(but)) {
2413                         UnitSettings *unit= but->block->unit;
2414                         int unit_type= uiButGetUnitType(but)>>16;
2415
2416                         if(bUnit_IsValid(unit->system, unit_type)) {
2417                                 fac= (float)bUnit_BaseScalar(unit->system, unit_type);
2418                                 if(ELEM3(unit_type, B_UNIT_LENGTH, B_UNIT_AREA, B_UNIT_VOLUME)) {
2419                                         fac /= unit->scale_length;
2420                                 }
2421                         }
2422                 }
2423
2424                 if(fac != 1.0f) {
2425                         /* snap in unit-space */
2426                         tempf /= fac;
2427                         /* softmin /= fac; */ /* UNUSED */
2428                         /* softmax /= fac; */ /* UNUSED */
2429                         softrange /= fac;
2430                 }
2431
2432                 if(snap==1) {
2433                         if(softrange < 2.10f) tempf= 0.1f*floorf(10.0f*tempf);
2434                         else if(softrange < 21.0f) tempf= floorf(tempf);
2435                         else tempf= 10.0f*floorf(tempf/10.0f);
2436                 }
2437                 else if(snap==2) {
2438                         if(softrange < 2.10f) tempf= 0.01f*floorf(100.0f*tempf);
2439                         else if(softrange < 21.0f) tempf= 0.1f*floorf(10.0f*tempf);
2440                         else tempf= floor(tempf);
2441                 }
2442                 
2443                 if(fac != 1.0f)
2444                         tempf *= fac;
2445         }
2446
2447         return tempf;
2448 }
2449
2450 static float ui_numedit_apply_snap(int temp, float softmin, float softmax, int snap)
2451 {
2452         if(temp==softmin || temp==softmax)
2453                 return temp;
2454
2455         switch(snap) {
2456         case 0:
2457                 break;
2458         case 1:
2459                 temp= 10*(temp/10);
2460                 break;
2461         case 2:
2462                 temp= 100*(temp/100);
2463                 break;
2464         }
2465
2466         return temp;
2467 }
2468
2469 static int ui_numedit_but_NUM(uiBut *but, uiHandleButtonData *data, float fac, int snap, int mx)
2470 {
2471         float deler, tempf, softmin, softmax, softrange;
2472         int lvalue, temp, changed= 0;
2473         
2474         if(mx == data->draglastx)
2475                 return changed;
2476         
2477         /* drag-lock - prevent unwanted scroll adjustments */
2478         /* change value (now 3) to adjust threshold in pixels */
2479         if(data->draglock) {
2480                 if(abs(mx-data->dragstartx) <= 3)
2481                         return changed;
2482
2483                 data->draglock= 0;
2484                 data->dragstartx= mx;  /* ignore mouse movement within drag-lock */
2485         }
2486
2487         softmin= but->softmin;
2488         softmax= but->softmax;
2489         softrange= softmax - softmin;
2490
2491         if(ui_is_a_warp_but(but)) {
2492                 /* Mouse location isn't screen clamped to the screen so use a linear mapping
2493                  * 2px == 1-int, or 1px == 1-ClickStep */
2494                 if(ui_is_but_float(but)) {
2495                         fac *= 0.01f*but->a1;
2496                         tempf = (float)data->startvalue + ((float)(mx - data->dragstartx) * fac);
2497                         tempf= ui_numedit_apply_snapf(but, tempf, softmin, softmax, softrange, snap);
2498
2499 #if 1           /* fake moving the click start, nicer for dragging back after passing the limit */
2500                         if(tempf < softmin) {
2501                                 data->dragstartx -= (softmin-tempf) / fac;
2502                                 tempf= softmin;
2503                         } else if (tempf > softmax) {
2504                                 data->dragstartx += (tempf-softmax) / fac;
2505                                 tempf= softmax;
2506                         }
2507 #else
2508                         CLAMP(tempf, softmin, softmax);
2509 #endif
2510
2511                         if(tempf != (float)data->value) {
2512                                 data->dragchange= 1;
2513                                 data->value= tempf;
2514                                 changed= 1;
2515                         }
2516                 }
2517                 else {
2518                         if(softrange > 256)             fac= 1.0;               /* 1px == 1 */
2519                         else if(softrange > 32) fac= 1.0/2.0;   /* 2px == 1 */
2520                         else                                    fac= 1.0/16.0;  /* 16px == 1? */
2521
2522                         temp= data->startvalue + (((double)mx - data->dragstartx) * (double)fac);
2523                         temp= ui_numedit_apply_snap(temp, softmin, softmax, snap);
2524
2525 #if 1           /* fake moving the click start, nicer for dragging back after passing the limit */
2526                         if(temp < softmin) {
2527                                 data->dragstartx -= (softmin-temp) / fac;
2528                                 temp= softmin;
2529                         } else if (temp > softmax) {
2530                                 data->dragstartx += (temp-softmax) / fac;
2531                                 temp= softmax;
2532                         }
2533 #else
2534                         CLAMP(temp, softmin, softmax);
2535 #endif
2536
2537                         if(temp != data->value) {
2538                                 data->dragchange= 1;
2539                                 data->value= temp;
2540                                 changed= 1;
2541                         }
2542                 }
2543
2544                 data->draglastx= mx;
2545         }
2546         else {
2547                 /* Use a non-linear mapping of the mouse drag especially for large floats (normal behavior) */
2548                 deler= 500;
2549                 if(!ui_is_but_float(but)) {
2550                         /* prevent large ranges from getting too out of control */
2551                         if (softrange > 600) deler = powf(softrange, 0.75);
2552                         
2553                         if (softrange < 100) deler= 200.0;
2554                         if (softrange < 25) deler= 50.0;
2555                 }
2556                 deler /= fac;
2557
2558                 if(softrange > 11) {
2559                         /* non linear change in mouse input- good for high precicsion */
2560                         data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabsf(data->dragstartx-mx)*0.002f);
2561                 } else if (softrange > 129) { /* only scale large int buttons */
2562                         /* non linear change in mouse input- good for high precicsionm ints need less fine tuning */
2563                         data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabsf(data->dragstartx-mx)*0.004f);
2564                 } else {
2565                         /*no scaling */
2566                         data->dragf+= ((float)(mx-data->draglastx))/deler ;
2567                 }
2568         
2569                 CLAMP(data->dragf, 0.0f, 1.0f);
2570                 data->draglastx= mx;
2571                 tempf= (softmin + data->dragf*softrange);
2572
2573
2574                 if(!ui_is_but_float(but)) {
2575                         temp= floorf(tempf + 0.5f);
2576
2577                         temp= ui_numedit_apply_snap(temp, softmin, softmax, snap);
2578
2579                         CLAMP(temp, softmin, softmax);
2580                         lvalue= (int)data->value;
2581                         
2582                         if(temp != lvalue) {
2583                                 data->dragchange= 1;
2584                                 data->value= (double)temp;
2585                                 changed= 1;
2586                         }
2587                 }
2588                 else {
2589                         temp= 0;
2590                         tempf= ui_numedit_apply_snapf(but, tempf, softmin, softmax, softrange, snap);
2591
2592                         CLAMP(tempf, softmin, softmax);
2593
2594                         if(tempf != (float)data->value) {
2595                                 data->dragchange= 1;
2596                                 data->value= tempf;
2597                                 changed= 1;
2598                         }
2599                 }
2600         }
2601
2602
2603         return changed;
2604 }
2605
2606 static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2607 {
2608         int mx, my;     /* mouse location scaled to fit the UI */
2609         int screen_mx, screen_my; /* mouse location kept at screen pixel coords */
2610         int click= 0;
2611         int retval= WM_UI_HANDLER_CONTINUE;
2612         
2613         mx= screen_mx= event->x;
2614         my= screen_my= event->y;
2615
2616         ui_window_to_block(data->region, block, &mx, &my);
2617
2618         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2619                 /* XXX hardcoded keymap check.... */
2620                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2621                         mx= but->x1;
2622                         click= 1;
2623                 }
2624                 else if(event->type == WHEELUPMOUSE && event->alt) {
2625                         mx= but->x2;
2626                         click= 1;
2627                 }
2628                 else if(event->val==KM_PRESS) {
2629                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2630                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2631                                 retval= WM_UI_HANDLER_BREAK;
2632                         }
2633                         else if(event->type == LEFTMOUSE) {
2634                                 data->dragstartx= data->draglastx= ui_is_a_warp_but(but) ? screen_mx:mx;
2635                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2636                                 retval= WM_UI_HANDLER_BREAK;
2637                         }
2638                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2639                                 click= 1;
2640                         else if (event->type == MINUSKEY && event->val==KM_PRESS) {
2641                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2642                                 data->value = -data->value;
2643                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2644                                 retval= WM_UI_HANDLER_BREAK;
2645                         }
2646                 }
2647                 
2648         }
2649         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2650                 if(event->type == ESCKEY) {
2651                         data->cancel= 1;
2652                         data->escapecancel= 1;
2653                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2654                 }
2655                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2656                         if(data->dragchange)
2657                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2658                         else
2659                                 click= 1;
2660                 }
2661                 else if(event->type == MOUSEMOVE) {
2662                         float fac;
2663                         int snap;
2664
2665                         fac= 1.0f;
2666                         if(event->shift) fac /= 10.0f;
2667                         if(event->alt) fac /= 20.0f;
2668                         
2669                         snap= (event->ctrl)? (event->shift)? 2: 1: 0;
2670
2671                         if(ui_numedit_but_NUM(but, data, fac, snap, (ui_is_a_warp_but(but) ? screen_mx:mx)))
2672                                 ui_numedit_apply(C, block, but, data);
2673                 }
2674                 retval= WM_UI_HANDLER_BREAK;
2675         }
2676         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2677                 ui_do_but_textedit(C, block, but, data, event);
2678                 retval= WM_UI_HANDLER_BREAK;
2679         }
2680         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2681                 ui_do_but_textedit_select(C, block, but, data, event);
2682                 retval= WM_UI_HANDLER_BREAK;
2683         }
2684         
2685         if(click) {
2686                 /* we can click on the side arrows to increment/decrement,
2687                  * or click inside to edit the value directly */
2688                 float tempf, softmin, softmax;
2689                 int temp;
2690
2691                 softmin= but->softmin;
2692                 softmax= but->softmax;
2693
2694                 if(!ui_is_but_float(but)) {
2695                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2696                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2697
2698                                 temp= (int)data->value - 1;
2699                                 if(temp>=softmin && temp<=softmax)
2700                                         data->value= (double)temp;
2701                                 else
2702                                         data->cancel= 1;
2703
2704                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2705                         }
2706                         else if(mx > (but->x1 + (2*(but->x2 - but->x1)/3) + 3)) {
2707                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2708
2709                                 temp= (int)data->value + 1;
2710                                 if(temp>=softmin && temp<=softmax)
2711                                         data->value= (double)temp;
2712                                 else
2713                                         data->cancel= 1;
2714
2715                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2716                         }
2717                         else
2718                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2719                 }
2720                 else {
2721                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2722                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2723
2724                                 tempf= (float)data->value - 0.01f * but->a1;
2725                                 if (tempf < softmin) tempf = softmin;
2726                                 data->value= tempf;
2727
2728                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2729                         }
2730                         else if(mx > but->x1 + (2*((but->x2 - but->x1)/3) + 3)) {
2731                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2732
2733                                 tempf= (float)data->value + 0.01f * but->a1;
2734                                 if (tempf > softmax) tempf = softmax;
2735                                 data->value= tempf;
2736
2737                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2738                         }
2739                         else
2740                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2741                 }
2742
2743                 retval= WM_UI_HANDLER_BREAK;
2744         }
2745         
2746         return retval;
2747 }
2748
2749 static int ui_numedit_but_SLI(uiBut *but, uiHandleButtonData *data, int shift, int ctrl, int mx)
2750 {
2751         float deler, f, tempf, softmin, softmax, softrange;
2752         int temp, lvalue, changed= 0;
2753
2754         softmin= but->softmin;
2755         softmax= but->softmax;
2756         softrange= softmax - softmin;
2757
2758         if(but->type==NUMSLI) deler= ((but->x2-but->x1) - 5.0f*but->aspect);
2759         else if(but->type==HSVSLI) deler= ((but->x2-but->x1)/2.0f - 5.0f*but->aspect);
2760         else if(but->type==SCROLL) {
2761                 int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2762                 float size= (horizontal)? (but->x2-but->x1): -(but->y2-but->y1);
2763                 deler= size*(but->softmax - but->softmin)/(but->softmax - but->softmin + but->a1);
2764         }
2765         else deler= (but->x2-but->x1- 5.0f*but->aspect);
2766
2767         f= (float)(mx-data->dragstartx)/deler + data->dragfstart;
2768         
2769         if(shift)
2770                 f= (f-data->dragfstart)/10.0f + data->dragfstart;
2771
2772         CLAMP(f, 0.0f, 1.0f);
2773         tempf= softmin + f*softrange;
2774         temp= floorf(tempf+0.5f);
2775
2776         if(ctrl) {
2777                 if(tempf==softmin || tempf==softmax);
2778                 else if(ui_is_but_float(but)) {
2779
2780                         if(shift) {
2781                                 if(tempf==softmin || tempf==softmax);
2782                                 else if(softmax-softmin < 2.10f) tempf= 0.01f * floorf(100.0f*tempf);
2783                                 else if(softmax-softmin < 21.0f) tempf= 0.1f * floorf(10.0f*tempf);
2784                                 else tempf= floorf(tempf);
2785                         }
2786                         else {
2787                                 if(softmax-softmin < 2.10f) tempf= 0.1f * floorf(10.0f*tempf);
2788                                 else if(softmax-softmin < 21.0f) tempf= floorf(tempf);
2789                                 else tempf= 10.0f*floorf(tempf/10.0f);
2790                         }
2791                 }
2792                 else {
2793                         temp= 10*(temp/10);
2794                         tempf= temp;
2795                 }
2796         }
2797
2798         if(!ui_is_but_float(but)) {
2799                 lvalue= floor(data->value+0.5);
2800
2801                 CLAMP(temp, softmin, softmax);
2802
2803                 if(temp != lvalue) {
2804                         data->value= temp;
2805                         data->dragchange= 1;
2806                         changed= 1;
2807                 }
2808         }
2809         else {
2810                 CLAMP(tempf, softmin, softmax);
2811
2812                 if(tempf != (float)data->value) {
2813                         data->value= tempf;
2814                         data->dragchange= 1;
2815                         changed= 1;
2816                 }
2817         }
2818
2819         return changed;
2820 }
2821
2822 static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2823 {
2824         int mx, my, click= 0;
2825         int retval= WM_UI_HANDLER_CONTINUE;
2826
2827         mx= event->x;
2828         my= event->y;
2829         ui_window_to_block(data->region, block, &mx, &my);
2830
2831         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2832                 /* XXX hardcoded keymap check.... */
2833                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2834                         mx= but->x1;
2835                         click= 2;
2836                 }
2837                 else if(event->type == WHEELUPMOUSE && event->alt) {
2838                         mx= but->x2;
2839                         click= 2;
2840                 }
2841                 else if(event->val==KM_PRESS) {
2842                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2843                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2844                                 retval= WM_UI_HANDLER_BREAK;
2845                         }
2846                         /* alt-click on sides to get "arrows" like in NUM buttons, and match wheel usage above */
2847                         else if(event->type == LEFTMOUSE && event->alt) {
2848                                 int halfpos = (but->x1 + but->x2) / 2;
2849                                 click = 2;
2850                                 if (mx < halfpos)
2851                                         mx = but->x1;
2852                                 else
2853                                         mx = but->x2;
2854                         }
2855                         else if(event->type == LEFTMOUSE) {
2856                                 data->dragstartx= mx;
2857                                 data->draglastx= mx;
2858                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2859                                 retval= WM_UI_HANDLER_BREAK;
2860                         }
2861                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2862                                 click= 1;
2863                         else if (event->type == MINUSKEY && event->val==KM_PRESS) {
2864                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2865                                 data->value = -data->value;
2866                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2867                                 retval= WM_UI_HANDLER_BREAK;
2868                         }
2869                 }
2870         }
2871         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2872                 if(event->type == ESCKEY) {
2873                         data->cancel= 1;
2874                         data->escapecancel= 1;
2875                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2876                 }
2877                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2878                         if(data->dragchange)
2879                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2880                         else
2881                                 click= 1;
2882                 }
2883                 else if(event->type == MOUSEMOVE) {
2884                         if(ui_numedit_but_SLI(but, data, event->shift, event->ctrl, mx))
2885                                 ui_numedit_apply(C, block, but, data);
2886                 }
2887                 retval= WM_UI_HANDLER_BREAK;
2888         }
2889         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2890                 ui_do_but_textedit(C, block, but, data, event);
2891                 retval= WM_UI_HANDLER_BREAK;
2892         }
2893         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2894                 ui_do_but_textedit_select(C, block, but, data, event);
2895                 retval= WM_UI_HANDLER_BREAK;
2896         }
2897
2898         if(click) {
2899                 if (click==2) {
2900                         /* nudge slider to the left or right */
2901                         float f, tempf, softmin, softmax, softrange;
2902                         int temp;
2903                         
2904                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2905                         
2906                         softmin= but->softmin;
2907                         softmax= but->softmax;
2908                         softrange= softmax - softmin;
2909                         
2910                         tempf= data->value;
2911                         temp= (int)data->value;
2912
2913 #if 0
2914                         if(but->type==SLI) {
2915                                 f= (float)(mx-but->x1)/(but->x2-but->x1); /* same as below */
2916                         }
2917                         else
2918 #endif
2919                         {
2920                                 f= (float)(mx- but->x1)/(but->x2-but->x1);
2921                         }
2922                         
2923                         f= softmin + f*softrange;
2924                         
2925                         if(!ui_is_but_float(but)) {
2926                                 if(f<temp) temp--;
2927                                 else temp++;
2928                                 
2929                                 if(temp>=softmin && temp<=softmax)
2930                                         data->value= temp;
2931                                 else
2932                                         data->cancel= 1;
2933                         } 
2934                         else {
2935                                 if(f<tempf) tempf -= 0.01f;
2936                                 else tempf += 0.01f;
2937                                 
2938                                 if(tempf>=softmin && tempf<=softmax)
2939                                         data->value= tempf;
2940                                 else
2941                                         data->cancel= 1;
2942                         }
2943                         
2944                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2945                         retval= WM_UI_HANDLER_BREAK;
2946                 }
2947                 else {
2948                         /* edit the value directly */
2949                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2950                         retval= WM_UI_HANDLER_BREAK;
2951                 }
2952         }
2953         
2954         return retval;
2955 }
2956
2957 static int ui_do_but_SCROLL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2958 {
2959         int mx, my /*, click= 0 */;
2960         int retval= WM_UI_HANDLER_CONTINUE;
2961         int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2962         
2963         mx= event->x;
2964         my= event->y;
2965         ui_window_to_block(data->region, block, &mx, &my);
2966
2967         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2968                 if(event->val==KM_PRESS) {
2969                         if(event->type == LEFTMOUSE) {
2970                                 if(horizontal) {
2971                                         data->dragstartx= mx;
2972                                         data->draglastx= mx;
2973                                 }
2974                                 else {
2975                                         data->dragstartx= my;
2976                                         data->draglastx= my;
2977                                 }
2978                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2979                                 retval= WM_UI_HANDLER_BREAK;
2980                         }
2981                         /* UNUSED - otherwise code is ok, add back if needed */
2982                         /* else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2983                                 click= 1;
2984                         */
2985                 }
2986         }
2987         else if(data->state == BUTTON_STATE_NUM_EDITING) {