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