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