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