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