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