UI: List Template tweaks to get it a bit more usable
[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                         if(but->dt == UI_EMBOSSN && !event->ctrl);
1948                         else {
1949                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1950                                 return WM_UI_HANDLER_BREAK;
1951                         }
1952                 }
1953         }
1954         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
1955                 ui_do_but_textedit(C, block, but, data, event);
1956                 return WM_UI_HANDLER_BREAK;
1957         }
1958         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
1959                 ui_do_but_textedit_select(C, block, but, data, event);
1960                 return WM_UI_HANDLER_BREAK;
1961         }
1962
1963         return WM_UI_HANDLER_CONTINUE;
1964 }
1965
1966 static int ui_do_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1967 {
1968         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1969                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
1970                         data->togdual= event->ctrl;
1971                         data->togonly= !event->shift;
1972                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1973                         return WM_UI_HANDLER_BREAK;
1974                 }
1975         }
1976         return WM_UI_HANDLER_CONTINUE;
1977 }
1978
1979 static int ui_do_but_EXIT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1980 {
1981         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1982                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
1983                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1984                         return WM_UI_HANDLER_BREAK;
1985                 }
1986         }
1987
1988         return WM_UI_HANDLER_CONTINUE;
1989 }
1990
1991 /* var names match ui_numedit_but_NUM */
1992 static float ui_numedit_apply_snapf(float tempf, float softmin, float softmax, float softrange, int snap)
1993 {
1994         if(tempf==softmin || tempf==softmax)
1995                 return tempf;
1996
1997         switch(snap) {
1998         case 0:
1999                 break;
2000         case 1:
2001                 if(tempf==softmin || tempf==softmax) { }
2002                 else if(softrange < 2.10) tempf= 0.1*floor(10*tempf);
2003                 else if(softrange < 21.0) tempf= floor(tempf);
2004                 else tempf= 10.0*floor(tempf/10.0);
2005                 break;
2006         case 2:
2007                 if(tempf==softmin || tempf==softmax) { }
2008                 else if(softrange < 2.10) tempf= 0.01*floor(100.0*tempf);
2009                 else if(softrange < 21.0) tempf= 0.1*floor(10.0*tempf);
2010                 else tempf= floor(tempf);
2011                 break;
2012         }
2013
2014         return tempf;
2015 }
2016
2017 static float ui_numedit_apply_snap(int temp, float softmin, float softmax, int snap)
2018 {
2019         if(temp==softmin || temp==softmax)
2020                 return temp;
2021
2022         switch(snap) {
2023         case 0:
2024                 break;
2025         case 1:
2026                 temp= 10*(temp/10);
2027                 break;
2028         case 2:
2029                 temp= 100*(temp/100);
2030                 break;
2031         }
2032
2033         return temp;
2034 }
2035
2036 static int ui_numedit_but_NUM(uiBut *but, uiHandleButtonData *data, float fac, int snap, int mx)
2037 {
2038         float deler, tempf, softmin, softmax, softrange;
2039         int lvalue, temp, changed= 0;
2040         
2041         if(mx == data->draglastx)
2042                 return changed;
2043         
2044         /* drag-lock - prevent unwanted scroll adjustments */
2045         /* change value (now 3) to adjust threshold in pixels */
2046         if(data->draglock) {
2047                 if(abs(mx-data->dragstartx) <= 3)
2048                         return changed;
2049
2050                 data->draglock= 0;
2051                 data->dragstartx= mx;  /* ignore mouse movement within drag-lock */
2052         }
2053
2054         softmin= but->softmin;
2055         softmax= but->softmax;
2056         softrange= softmax - softmin;
2057
2058
2059         if(ui_is_a_warp_but(but)) {
2060                 /* Mouse location isn't screen clamped to the screen so use a linear mapping
2061                  * 2px == 1-int, or 1px == 1-ClickStep */
2062                 if(ui_is_but_float(but)) {
2063                         fac *= 0.01*but->a1;
2064                         tempf = data->startvalue + ((mx - data->dragstartx) * fac);
2065                         tempf= ui_numedit_apply_snapf(tempf, softmin, softmax, softrange, snap);
2066
2067 #if 1           /* fake moving the click start, nicer for dragging back after passing the limit */
2068                         if(tempf < softmin) {
2069                                 data->dragstartx -= (softmin-tempf) / fac;
2070                                 tempf= softmin;
2071                         } else if (tempf > softmax) {
2072                                 data->dragstartx += (tempf-softmax) / fac;
2073                                 tempf= softmax;
2074                         }
2075 #else
2076                         CLAMP(tempf, softmin, softmax);
2077 #endif
2078
2079                         if(tempf != data->value) {
2080                                 data->dragchange= 1;
2081                                 data->value= tempf;
2082                                 changed= 1;
2083                         }
2084                 }
2085                 else {
2086                         fac = 0.5; /* simple 2px == 1 */
2087
2088                         temp= data->startvalue + ((mx - data->dragstartx) * fac);
2089                         temp= ui_numedit_apply_snap(temp, softmin, softmax, snap);
2090
2091 #if 1           /* fake moving the click start, nicer for dragging back after passing the limit */
2092                         if(temp < softmin) {
2093                                 data->dragstartx -= (softmin-temp) / fac;
2094                                 temp= softmin;
2095                         } else if (temp > softmax) {
2096                                 data->dragstartx += (temp-softmax) / fac;
2097                                 temp= softmax;
2098                         }
2099 #else
2100                         CLAMP(temp, softmin, softmax);
2101 #endif
2102
2103                         if(temp != data->value) {
2104                                 data->dragchange= 1;
2105                                 data->value= temp;
2106                                 changed= 1;
2107                         }
2108                 }
2109
2110                 data->draglastx= mx;
2111         }
2112         else {
2113                 /* Use a non-linear mapping of the mouse drag especially for large floats (normal behavior) */
2114                 deler= 500;
2115                 if(!ui_is_but_float(but)) {
2116                         if((softrange)<100) deler= 200.0;
2117                         if((softrange)<25) deler= 50.0;
2118                 }
2119                 deler /= fac;
2120
2121                 if(ui_is_but_float(but) && softrange > 11) {
2122                         /* non linear change in mouse input- good for high precicsion */
2123                         data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.002);
2124                 } else if (!ui_is_but_float(but) && softrange > 129) { /* only scale large int buttons */
2125                         /* non linear change in mouse input- good for high precicsionm ints need less fine tuning */
2126                         data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.004);
2127                 } else {
2128                         /*no scaling */
2129                         data->dragf+= ((float)(mx-data->draglastx))/deler ;
2130                 }
2131         
2132                 if(data->dragf>1.0) data->dragf= 1.0;
2133                 if(data->dragf<0.0) data->dragf= 0.0;
2134                 data->draglastx= mx;
2135                 tempf= (softmin + data->dragf*softrange);
2136
2137
2138                 if(!ui_is_but_float(but)) {
2139                         temp= floor(tempf+.5);
2140
2141                         temp= ui_numedit_apply_snap(temp, softmin, softmax, snap);
2142
2143                         CLAMP(temp, softmin, softmax);
2144                         lvalue= (int)data->value;
2145
2146                         if(temp != lvalue) {
2147                                 data->dragchange= 1;
2148                                 data->value= (double)temp;
2149                                 changed= 1;
2150                         }
2151                 }
2152                 else {
2153                         temp= 0;
2154                         tempf= ui_numedit_apply_snapf(tempf, softmin, softmax, softrange, snap);
2155
2156                         CLAMP(tempf, softmin, softmax);
2157
2158                         if(tempf != data->value) {
2159                                 data->dragchange= 1;
2160                                 data->value= tempf;
2161                                 changed= 1;
2162                         }
2163                 }
2164         }
2165
2166
2167         return changed;
2168 }
2169
2170 static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2171 {
2172         int mx, my;     /* mouse location scaled to fit the UI */
2173         int screen_mx, screen_my; /* mouse location kept at screen pixel coords */
2174         int click= 0;
2175         int retval= WM_UI_HANDLER_CONTINUE;
2176         
2177         mx= screen_mx= event->x;
2178         my= screen_my= event->y;
2179
2180         ui_window_to_block(data->region, block, &mx, &my);
2181
2182         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2183                 /* XXX hardcoded keymap check.... */
2184                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2185                         mx= but->x1;
2186                         click= 1;
2187                 }
2188                 else if(event->type == WHEELUPMOUSE && event->alt) {
2189                         mx= but->x2;
2190                         click= 1;
2191                 }
2192                 else if(event->val==KM_PRESS) {
2193                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2194                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2195                                 retval= WM_UI_HANDLER_BREAK;
2196                         }
2197                         else if(event->type == LEFTMOUSE) {
2198                                 data->dragstartx= data->draglastx= ui_is_a_warp_but(but) ? screen_mx:mx;
2199                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2200                                 retval= WM_UI_HANDLER_BREAK;
2201                         }
2202                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2203                                 click= 1;
2204                 }
2205                 
2206         }
2207         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2208                 if(event->type == ESCKEY) {
2209                         data->cancel= 1;
2210                         data->escapecancel= 1;
2211                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2212                 }
2213                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2214                         if(data->dragchange)
2215                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2216                         else
2217                                 click= 1;
2218                 }
2219                 else if(event->type == MOUSEMOVE) {
2220                         float fac;
2221                         int snap;
2222
2223                         fac= 1.0f;
2224                         if(event->shift) fac /= 10.0f;
2225                         if(event->alt) fac /= 20.0f;
2226
2227                         if(event->custom == EVT_DATA_TABLET) {
2228                                 wmTabletData *wmtab= event->customdata;
2229
2230                                 /* de-sensitise based on tablet pressure */
2231                                 if (wmtab->Active != EVT_TABLET_NONE)
2232                                         fac *= wmtab->Pressure;
2233                         }
2234                         
2235                         snap= (event->ctrl)? (event->shift)? 2: 1: 0;
2236
2237                         if(ui_numedit_but_NUM(but, data, fac, snap, (ui_is_a_warp_but(but) ? screen_mx:mx)))
2238                                 ui_numedit_apply(C, block, but, data);
2239                 }
2240                 retval= WM_UI_HANDLER_BREAK;
2241         }
2242         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2243                 ui_do_but_textedit(C, block, but, data, event);
2244                 retval= WM_UI_HANDLER_BREAK;
2245         }
2246         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2247                 ui_do_but_textedit_select(C, block, but, data, event);
2248                 retval= WM_UI_HANDLER_BREAK;
2249         }
2250         
2251         if(click) {
2252                 /* we can click on the side arrows to increment/decrement,
2253                  * or click inside to edit the value directly */
2254                 float tempf, softmin, softmax;
2255                 int temp;
2256
2257                 softmin= but->softmin;
2258                 softmax= but->softmax;
2259
2260                 if(!ui_is_but_float(but)) {
2261                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2262                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2263
2264                                 temp= (int)data->value - 1;
2265                                 if(temp>=softmin && temp<=softmax)
2266                                         data->value= (double)temp;
2267                                 else
2268                                         data->cancel= 1;
2269
2270                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2271                         }
2272                         else if(mx > (but->x1 + (2*(but->x2 - but->x1)/3) + 3)) {
2273                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2274
2275                                 temp= (int)data->value + 1;
2276                                 if(temp>=softmin && temp<=softmax)
2277                                         data->value= (double)temp;
2278                                 else
2279                                         data->cancel= 1;
2280
2281                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2282                         }
2283                         else
2284                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2285                 }
2286                 else {
2287                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2288                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2289
2290                                 tempf= data->value - 0.01*but->a1;
2291                                 if (tempf < softmin) tempf = softmin;
2292                                 data->value= tempf;
2293
2294                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2295                         }
2296                         else if(mx > but->x1 + (2*((but->x2 - but->x1)/3) + 3)) {
2297                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2298
2299                                 tempf= data->value + 0.01*but->a1;
2300                                 if (tempf > softmax) tempf = softmax;
2301                                 data->value= tempf;
2302
2303                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2304                         }
2305                         else
2306                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2307                 }
2308
2309                 retval= WM_UI_HANDLER_BREAK;
2310         }
2311         
2312         return retval;
2313 }
2314
2315 static int ui_numedit_but_SLI(uiBut *but, uiHandleButtonData *data, int shift, int ctrl, int mx)
2316 {
2317         float deler, f, tempf, softmin, softmax, softrange;
2318         int temp, lvalue, changed= 0;
2319
2320         softmin= but->softmin;
2321         softmax= but->softmax;
2322         softrange= softmax - softmin;
2323
2324         if(but->type==NUMSLI) deler= ((but->x2-but->x1) - 5.0*but->aspect);
2325         else if(but->type==HSVSLI) deler= ((but->x2-but->x1)/2 - 5.0*but->aspect);
2326         else if(but->type==SCROLL) {
2327                 int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2328                 float size= (horizontal)? (but->x2-but->x1): -(but->y2-but->y1);
2329                 deler= size*(but->softmax - but->softmin)/(but->softmax - but->softmin + but->a1);
2330         }
2331         else deler= (but->x2-but->x1- 5.0*but->aspect);
2332
2333         f= (float)(mx-data->dragstartx)/deler + data->dragfstart;
2334         
2335         if(shift)
2336                 f= (f-data->dragfstart)/10.0 + data->dragfstart;
2337
2338         CLAMP(f, 0.0, 1.0);
2339         tempf= softmin + f*softrange;
2340         temp= floor(tempf+.5);
2341
2342         if(ctrl) {
2343                 if(tempf==softmin || tempf==softmax);
2344                 else if(ui_is_but_float(but)) {
2345
2346                         if(shift) {
2347                                 if(tempf==softmin || tempf==softmax);
2348                                 else if(softmax-softmin < 2.10) tempf= 0.01*floor(100.0*tempf);
2349                                 else if(softmax-softmin < 21.0) tempf= 0.1*floor(10.0*tempf);
2350                                 else tempf= floor(tempf);
2351                         }
2352                         else {
2353                                 if(softmax-softmin < 2.10) tempf= 0.1*floor(10*tempf);
2354                                 else if(softmax-softmin < 21.0) tempf= floor(tempf);
2355                                 else tempf= 10.0*floor(tempf/10.0);
2356                         }
2357                 }
2358                 else {
2359                         temp= 10*(temp/10);
2360                         tempf= temp;
2361                 }
2362         }
2363
2364         if(!ui_is_but_float(but)) {
2365                 lvalue= floor(data->value+0.5);
2366
2367                 CLAMP(temp, softmin, softmax);
2368
2369                 if(temp != lvalue) {
2370                         data->value= temp;
2371                         data->dragchange= 1;
2372                         changed= 1;
2373                 }
2374         }
2375         else {
2376                 CLAMP(tempf, softmin, softmax);
2377
2378                 if(tempf != data->value) {
2379                         data->value= tempf;
2380                         data->dragchange= 1;
2381                         changed= 1;
2382                 }
2383         }
2384
2385         return changed;
2386 }
2387
2388 static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2389 {
2390         int mx, my, click= 0;
2391         int retval= WM_UI_HANDLER_CONTINUE;
2392
2393         mx= event->x;
2394         my= event->y;
2395         ui_window_to_block(data->region, block, &mx, &my);
2396
2397         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2398                 /* XXX hardcoded keymap check.... */
2399                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2400                         mx= but->x1;
2401                         click= 2;
2402                 }
2403                 else if(event->type == WHEELUPMOUSE && event->alt) {
2404                         mx= but->x2;
2405                         click= 2;
2406                 }
2407                 else if(event->val==KM_PRESS) {
2408                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2409                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2410                                 retval= WM_UI_HANDLER_BREAK;
2411                         }
2412                         /* alt-click on sides to get "arrows" like in NUM buttons, and match wheel usage above */
2413                         else if(event->type == LEFTMOUSE && event->alt) {
2414                                 int halfpos = (but->x1 + but->x2) / 2;
2415                                 click = 2;
2416                                 if (mx < halfpos)
2417                                         mx = but->x1;
2418                                 else
2419                                         mx = but->x2;
2420                         }
2421                         else if(event->type == LEFTMOUSE) {
2422                                 data->dragstartx= mx;
2423                                 data->draglastx= mx;
2424                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2425                                 retval= WM_UI_HANDLER_BREAK;
2426                         }
2427                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2428                                 click= 1;
2429                 }
2430         }
2431         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2432                 if(event->type == ESCKEY) {
2433                         data->cancel= 1;
2434                         data->escapecancel= 1;
2435                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2436                 }
2437                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2438                         if(data->dragchange)
2439                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2440                         else
2441                                 click= 1;
2442                 }
2443                 else if(event->type == MOUSEMOVE) {
2444                         if(ui_numedit_but_SLI(but, data, event->shift, event->ctrl, mx))
2445                                 ui_numedit_apply(C, block, but, data);
2446                 }
2447                 retval= WM_UI_HANDLER_BREAK;
2448         }
2449         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2450                 ui_do_but_textedit(C, block, but, data, event);
2451                 retval= WM_UI_HANDLER_BREAK;
2452         }
2453         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2454                 ui_do_but_textedit_select(C, block, but, data, event);
2455                 retval= WM_UI_HANDLER_BREAK;
2456         }
2457
2458         if(click) {
2459                 if (click==2) {
2460                         /* nudge slider to the left or right */
2461                         float f, tempf, softmin, softmax, softrange;
2462                         int temp;
2463                         
2464                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2465                         
2466                         softmin= but->softmin;
2467                         softmax= but->softmax;
2468                         softrange= softmax - softmin;
2469                         
2470                         tempf= data->value;
2471                         temp= (int)data->value;
2472                         
2473                         /* XXX useles "if", same result for f, uh??? */
2474                         if(but->type==SLI) f= (float)(mx-but->x1)/(but->x2-but->x1);
2475                         else f= (float)(mx- but->x1)/(but->x2-but->x1);
2476                         
2477                         f= softmin + f*softrange;
2478                         
2479                         if(!ui_is_but_float(but)) {
2480                                 if(f<temp) temp--;
2481                                 else temp++;
2482                                 
2483                                 if(temp>=softmin && temp<=softmax)
2484                                         data->value= temp;
2485                                 else
2486                                         data->cancel= 1;
2487                         } 
2488                         else {
2489                                 if(f<tempf) tempf-=.01;
2490                                 else tempf+=.01;
2491                                 
2492                                 if(tempf>=softmin && tempf<=softmax)
2493                                         data->value= tempf;
2494                                 else
2495                                         data->cancel= 1;
2496                         }
2497                         
2498                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2499                         retval= WM_UI_HANDLER_BREAK;
2500                 }
2501                 else {
2502                         /* edit the value directly */
2503                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2504                         retval= WM_UI_HANDLER_BREAK;
2505                 }
2506         }
2507         
2508         return retval;
2509 }
2510
2511 static int ui_do_but_SCROLL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2512 {
2513         int mx, my, click= 0;
2514         int retval= WM_UI_HANDLER_CONTINUE;
2515         int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2516         
2517         mx= event->x;
2518         my= event->y;
2519         ui_window_to_block(data->region, block, &mx, &my);
2520
2521         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2522                 if(event->val==KM_PRESS) {
2523                         if(event->type == LEFTMOUSE) {
2524                                 if(horizontal) {
2525                                         data->dragstartx= mx;
2526                                         data->draglastx= mx;
2527                                 }
2528                                 else {
2529                                         data->dragstartx= my;
2530                                         data->draglastx= my;
2531                                 }
2532                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2533                                 retval= WM_UI_HANDLER_BREAK;
2534                         }
2535                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2536                                 click= 1;
2537                 }
2538         }
2539         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2540                 if(event->type == ESCKEY) {
2541                         data->cancel= 1;
2542                         data->escapecancel= 1;
2543                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2544                 }
2545                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2546                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2547                 }
2548                 else if(event->type == MOUSEMOVE) {
2549                         if(ui_numedit_but_SLI(but, data, 0, 0, (horizontal)? mx: my))
2550                                 ui_numedit_apply(C, block, but, data);
2551                 }
2552
2553                 retval= WM_UI_HANDLER_BREAK;
2554         }
2555         
2556         return retval;
2557 }
2558
2559 static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2560 {
2561         
2562         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2563                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2564                         button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
2565                         return WM_UI_HANDLER_BREAK;
2566                 }
2567                 else if(ELEM3(but->type, MENU, ICONROW, ICONTEXTROW)) {
2568                         
2569                         if(event->type == WHEELDOWNMOUSE && event->alt) {
2570                                 data->value= ui_step_name_menu(but, -1);
2571                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2572                                 ui_apply_button(C, but->block, but, data, 1);
2573                                 return WM_UI_HANDLER_BREAK;
2574                         }
2575                         else if(event->type == WHEELUPMOUSE && event->alt) {
2576                                 data->value= ui_step_name_menu(but, 1);
2577                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2578                                 ui_apply_button(C, but->block, but, data, 1);
2579                                 return WM_UI_HANDLER_BREAK;
2580                         }
2581                 }
2582                 else if(but->type==COL) {
2583                         if( ELEM(event->type, WHEELDOWNMOUSE, WHEELUPMOUSE) && event->alt) {
2584                                 float col[3];
2585                                 
2586                                 ui_get_but_vectorf(but, col);
2587                                 rgb_to_hsv(col[0], col[1], col[2], but->hsv, but->hsv+1, but->hsv+2);
2588
2589                                 if(event->type==WHEELDOWNMOUSE)
2590                                         but->hsv[2]= CLAMPIS(but->hsv[2]-0.05f, 0.0f, 1.0f);
2591                                 else
2592                                         but->hsv[2]= CLAMPIS(but->hsv[2]+0.05f, 0.0f, 1.0f);
2593                                 
2594                                 hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], data->vec, data->vec+1, data->vec+2);
2595                                 
2596                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2597                                 ui_apply_button(C, but->block, but, data, 1);
2598                                 return WM_UI_HANDLER_BREAK;
2599                         }
2600                 }
2601         }
2602
2603         return WM_UI_HANDLER_CONTINUE;
2604 }
2605
2606 static int ui_numedit_but_NORMAL(uiBut *but, uiHandleButtonData *data, int mx, int my)
2607 {
2608         float dx, dy, rad, radsq, mrad, *fp;
2609         int mdx, mdy, changed= 1;
2610         
2611         /* button is presumed square */
2612         /* if mouse moves outside of sphere, it does negative normal */
2613
2614         fp= data->origvec;
2615         rad= (but->x2 - but->x1);
2616         radsq= rad*rad;
2617         
2618         if(fp[2]>0.0f) {
2619                 mdx= (rad*fp[0]);
2620                 mdy= (rad*fp[1]);
2621         }
2622         else if(fp[2]> -1.0f) {
2623                 mrad= rad/sqrt(fp[0]*fp[0] + fp[1]*fp[1]);
2624                 
2625                 mdx= 2.0f*mrad*fp[0] - (rad*fp[0]);
2626                 mdy= 2.0f*mrad*fp[1] - (rad*fp[1]);
2627         }
2628         else mdx= mdy= 0;
2629         
2630         dx= (float)(mx+mdx-data->dragstartx);
2631         dy= (float)(my+mdy-data->dragstarty);
2632
2633         fp= data->vec;
2634         mrad= dx*dx+dy*dy;
2635         if(mrad < radsq) {      /* inner circle */
2636                 fp[0]= dx;
2637                 fp[1]= dy;
2638                 fp[2]= sqrt( radsq-dx*dx-dy*dy );
2639         }
2640         else {  /* outer circle */
2641                 
2642                 mrad= rad/sqrt(mrad);   // veclen
2643                 
2644                 dx*= (2.0f*mrad - 1.0f);
2645                 dy*= (2.0f*mrad - 1.0f);
2646                 
2647                 mrad= dx*dx+dy*dy;
2648                 if(mrad < radsq) {
2649                         fp[0]= dx;
2650                         fp[1]= dy;
2651                         fp[2]= -sqrt( radsq-dx*dx-dy*dy );
2652                 }
2653         }
2654         Normalize(fp);
2655
2656         data->draglastx= mx;
2657         data->draglasty= my;
2658
2659         return changed;
2660 }
2661
2662 static int ui_do_but_NORMAL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2663 {
2664         int mx, my;
2665
2666         mx= event->x;
2667         my= event->y;
2668         ui_window_to_block(data->region, block, &mx, &my);
2669
2670         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2671                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2672                         data->dragstartx= mx;
2673                         data->dragstarty= my;
2674                         data->draglastx= mx;
2675                         data->draglasty= my;
2676                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2677
2678                         /* also do drag the first time */
2679                         if(ui_numedit_but_NORMAL(but, data, mx, my))
2680                                 ui_numedit_apply(C, block, but, data);
2681                         
2682                         return WM_UI_HANDLER_BREAK;
2683                 }
2684         }
2685         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2686                 if(event->type == MOUSEMOVE) {
2687                         if(mx!=data->draglastx || my!=data->draglasty) {
2688                                 if(ui_numedit_but_NORMAL(but, data, mx, my))
2689                                         ui_numedit_apply(C, block, but, data);
2690                         }
2691                 }
2692                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2693                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2694
2695                 return WM_UI_HANDLER_BREAK;
2696         }
2697         
2698         return WM_UI_HANDLER_CONTINUE;
2699 }
2700
2701 static int ui_numedit_but_HSVCUBE(uiBut *but, uiHandleButtonData *data, int mx, int my)
2702 {
2703         float x, y;
2704         int changed= 1;
2705
2706         /* relative position within box */
2707         x= ((float)mx-but->x1)/(but->x2-but->x1);
2708         y= ((float)my-but->y1)/(but->y2-but->y1);
2709         CLAMP(x, 0.0, 1.0);
2710         CLAMP(y, 0.0, 1.0);
2711         
2712         if(but->a1==0) {
2713                 but->hsv[0]= x; 
2714                 but->hsv[2]= y; 
2715         }
2716         else if(but->a1==1) {
2717                 but->hsv[0]= x;                                 
2718                 but->hsv[1]= y;                                 
2719         }
2720         else if(but->a1==2) {
2721                 but->hsv[2]= x; 
2722                 but->hsv[1]= y; 
2723         }
2724         else if(but->a1==3) {
2725                 but->hsv[0]= x; 
2726         }
2727         else
2728                 but->hsv[2]= y; 
2729
2730         ui_set_but_hsv(but);    // converts to rgb
2731         
2732         // update button values and strings
2733         ui_update_block_buts_hsv(but->block, but->hsv);
2734
2735         data->draglastx= mx;
2736         data->draglasty= my;
2737
2738         return changed;
2739 }
2740
2741 static int ui_do_but_HSVCUBE(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2742 {
2743         int mx, my;
2744
2745         mx= event->x;
2746         my= event->y;
2747         ui_window_to_block(data->region, block, &mx, &my);
2748
2749         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2750                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2751                         data->dragstartx= mx;
2752                         data->dragstarty= my;
2753                         data->draglastx= mx;
2754                         data->draglasty= my;
2755                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2756
2757                         /* also do drag the first time */
2758                         if(ui_numedit_but_HSVCUBE(but, data, mx, my))
2759                                 ui_numedit_apply(C, block, but, data);
2760                         
2761                         return WM_UI_HANDLER_BREAK;
2762                 }
2763         }
2764         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2765                 if(event->type == MOUSEMOVE) {
2766                         if(mx!=data->draglastx || my!=data->draglasty) {
2767                                 if(ui_numedit_but_HSVCUBE(but, data, mx, my))
2768                                         ui_numedit_apply(C, block, but, data);
2769                         }
2770                 }
2771                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2772                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2773                 
2774                 return WM_UI_HANDLER_BREAK;
2775         }
2776
2777         return WM_UI_HANDLER_CONTINUE;
2778 }
2779
2780 static int ui_numedit_but_HSVCIRCLE(uiBut *but, uiHandleButtonData *data, int mx, int my)
2781 {
2782         rcti rect;
2783         int changed= 1;
2784
2785         rect.xmin= but->x1; rect.xmax= but->x2;
2786         rect.ymin= but->y1; rect.ymax= but->y2;
2787         
2788         ui_hsvcircle_vals_from_pos(but->hsv, but->hsv+1, &rect, (float)mx, (float)my);
2789         
2790         ui_set_but_hsv(but);    // converts to rgb
2791         
2792         // update button values and strings
2793         // XXX ui_update_block_buts_hsv(but->block, but->hsv);
2794         
2795         data->draglastx= mx;
2796         data->draglasty= my;
2797         
2798         return changed;
2799 }
2800
2801
2802 static int ui_do_but_HSVCIRCLE(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2803 {
2804         int mx, my;
2805         
2806         mx= event->x;
2807         my= event->y;
2808         ui_window_to_block(data->region, block, &mx, &my);
2809         
2810         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2811                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2812                         data->dragstartx= mx;
2813                         data->dragstarty= my;
2814                         data->draglastx= mx;
2815                         data->draglasty= my;
2816                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2817                         
2818                         /* also do drag the first time */
2819                         if(ui_numedit_but_HSVCIRCLE(but, data, mx, my))
2820                                 ui_numedit_apply(C, block, but, data);
2821                         
2822                         return WM_UI_HANDLER_BREAK;
2823                 }
2824         }
2825         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2826                 /* XXX hardcoded keymap check.... */
2827                 if(event->type == WHEELDOWNMOUSE) {
2828                         but->hsv[2]= CLAMPIS(but->hsv[2]-0.05f, 0.0f, 1.0f);
2829                         ui_set_but_hsv(but);    // converts to rgb
2830                         ui_numedit_apply(C, block, but, data);
2831                 }
2832                 else if(event->type == WHEELUPMOUSE) {
2833                         but->hsv[2]= CLAMPIS(but->hsv[2]+0.05f, 0.0f, 1.0f);
2834                         ui_set_but_hsv(but);    // converts to rgb
2835                         ui_numedit_apply(C, block, but, data);
2836                 }
2837                 else if(event->type == MOUSEMOVE) {
2838                         if(mx!=data->draglastx || my!=data->draglasty) {
2839                                 if(ui_numedit_but_HSVCIRCLE(but, data, mx, my))
2840                                         ui_numedit_apply(C, block, but, data);
2841                         }
2842                 }
2843                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2844                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2845                 
2846                 return WM_UI_HANDLER_BREAK;
2847         }
2848         
2849         return WM_UI_HANDLER_CONTINUE;
2850 }
2851
2852
2853 static int verg_colorband(const void *a1, const void *a2)
2854 {
2855         const CBData *x1=a1, *x2=a2;
2856         
2857         if( x1->pos > x2->pos ) return 1;
2858         else if( x1->pos < x2->pos) return -1;
2859         return WM_UI_HANDLER_CONTINUE;
2860 }
2861
2862 static void ui_colorband_update(ColorBand *coba)
2863 {
2864         int a;
2865         
2866         if(coba->tot<2) return;
2867         
2868         for(a=0; a<coba->tot; a++) coba->data[a].cur= a;
2869                 qsort(coba->data, coba->tot, sizeof(CBData), verg_colorband);
2870         for(a=0; a<coba->tot; a++) {
2871                 if(coba->data[a].cur==coba->cur) {
2872                         coba->cur= a;
2873                         break;
2874                 }
2875         }
2876 }
2877
2878 static int ui_numedit_but_COLORBAND(uiBut *but, uiHandleButtonData *data, int mx)
2879 {
2880         float dx;
2881         int changed= 0;
2882
2883         if(data->draglastx == mx)
2884                 return changed;
2885
2886         dx= ((float)(mx - data->draglastx))/(but->x2-but->x1);
2887         data->dragcbd->pos += dx;
2888         CLAMP(data->dragcbd->pos, 0.0, 1.0);
2889         
2890         ui_colorband_update(data->coba);
2891         data->dragcbd= data->coba->data + data->coba->cur;      /* because qsort */
2892         
2893         data->draglastx= mx;
2894         changed= 1;
2895
2896         return changed;
2897 }
2898
2899 static int ui_do_but_COLORBAND(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2900 {
2901         ColorBand *coba;
2902         CBData *cbd;
2903         int mx, my, a, xco, mindist= 12;
2904
2905         mx= event->x;
2906         my= event->y;
2907         ui_window_to_block(data->region, block, &mx, &my);
2908
2909         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2910                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2911                         coba= (ColorBand*)but->poin;
2912
2913                         if(event->ctrl) {
2914                                 /* insert new key on mouse location */
2915                                 if(coba->tot < MAXCOLORBAND-1) {
2916                                         float pos= ((float)(mx - but->x1))/(but->x2-but->x1);
2917                                         float col[4];
2918                                         
2919                                         do_colorband(coba, pos, col);   /* executes it */
2920                                         
2921                                         coba->tot++;
2922                                         coba->cur= coba->tot-1;
2923                                         
2924                                         coba->data[coba->cur].r= col[0];
2925                                         coba->data[coba->cur].g= col[1];
2926                                         coba->data[coba-&g