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