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