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