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