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