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