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