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