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