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