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