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