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