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