changes to color wheel commit.
[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
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_object_types.h"
35 #include "DNA_scene_types.h"
36
37 #include "BLI_math.h"
38 #include "BLI_blenlib.h"
39 #include "PIL_time.h"
40
41 #include "BKE_colortools.h"
42 #include "BKE_context.h"
43 #include "BKE_idprop.h"
44 #include "BKE_report.h"
45 #include "BKE_texture.h"
46 #include "BKE_utildefines.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+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         data->origstr= BLI_strdup(data->str);
1539         data->selextend= 0;
1540         data->selstartx= 0;
1541
1542         /* set cursor pos to the end of the text */
1543         but->editstr= data->str;
1544         but->pos= strlen(data->str);
1545         but->selsta= 0;
1546         but->selend= strlen(data->str);
1547
1548         /* optional searchbox */
1549         if(but->type==SEARCH_MENU) {
1550                 data->searchbox= ui_searchbox_create(C, data->region, but);
1551                 ui_searchbox_update(C, data->searchbox, but, 1); /* 1= reset */
1552         }
1553         
1554         ui_check_but(but);
1555         
1556         WM_cursor_modal(CTX_wm_window(C), BC_TEXTEDITCURSOR);
1557 }
1558
1559 static void ui_textedit_end(bContext *C, uiBut *but, uiHandleButtonData *data)
1560 {
1561         if(but) {
1562                 if(data->searchbox) {
1563                         if(data->cancel==0)
1564                                 ui_searchbox_apply(but, data->searchbox);
1565
1566                         ui_searchbox_free(C, data->searchbox);
1567                         data->searchbox= NULL;
1568                 }
1569                 
1570                 but->editstr= NULL;
1571                 but->pos= -1;
1572         }
1573         
1574         WM_cursor_restore(CTX_wm_window(C));
1575 }
1576
1577 static void ui_textedit_next_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1578 {
1579         uiBut *but;
1580
1581         /* label and roundbox can overlap real buttons (backdrops...) */
1582         if(ELEM4(actbut->type, LABEL, SEPR, ROUNDBOX, LISTBOX))
1583                 return;
1584
1585         for(but= actbut->next; but; but= but->next) {
1586                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1587                         if(!(but->flag & UI_BUT_DISABLED)) {
1588                                 data->postbut= but;
1589                                 data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1590                                 return;
1591                         }
1592                 }
1593         }
1594         for(but= block->buttons.first; but!=actbut; but= but->next) {
1595                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1596                         if(!(but->flag & UI_BUT_DISABLED)) {
1597                                 data->postbut= but;
1598                                 data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1599                                 return;
1600                         }
1601                 }
1602         }
1603 }
1604
1605 static void ui_textedit_prev_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1606 {
1607         uiBut *but;
1608
1609         /* label and roundbox can overlap real buttons (backdrops...) */
1610         if(ELEM4(actbut->type, LABEL, SEPR, ROUNDBOX, LISTBOX))
1611                 return;
1612
1613         for(but= actbut->prev; but; but= but->prev) {
1614                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1615                         if(!(but->flag & UI_BUT_DISABLED)) {
1616                                 data->postbut= but;
1617                                 data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1618                                 return;
1619                         }
1620                 }
1621         }
1622         for(but= block->buttons.last; but!=actbut; but= but->prev) {
1623                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1624                         if(!(but->flag & UI_BUT_DISABLED)) {
1625                                 data->postbut= but;
1626                                 data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1627                                 return;
1628                         }
1629                 }
1630         }
1631 }
1632
1633
1634 static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1635 {
1636         int mx, my, changed= 0, inbox=0, update= 0, retval= WM_UI_HANDLER_CONTINUE;
1637
1638         switch(event->type) {
1639                 case WHEELUPMOUSE:
1640                 case WHEELDOWNMOUSE:
1641                 case MOUSEMOVE:
1642                         if(data->searchbox)
1643                                 ui_searchbox_event(C, data->searchbox, but, event);
1644                         
1645                         break;
1646                 case RIGHTMOUSE:
1647                 case ESCKEY:
1648                         data->cancel= 1;
1649                         data->escapecancel= 1;
1650                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1651                         retval= WM_UI_HANDLER_BREAK;
1652                         break;
1653                 case LEFTMOUSE: {
1654                         
1655                         /* exit on LMB only on RELEASE for searchbox, to mimic other popups, and allow multiple menu levels */
1656                         if(data->searchbox)
1657                                 inbox= ui_searchbox_inside(data->searchbox, event->x, event->y);
1658
1659                         if(event->val==KM_PRESS) {
1660                                 mx= event->x;
1661                                 my= event->y;
1662                                 ui_window_to_block(data->region, block, &mx, &my);
1663
1664                                 if (ui_but_contains_pt(but, mx, my)) {
1665                                         ui_textedit_set_cursor_pos(but, data, mx);
1666                                         but->selsta = but->selend = but->pos;
1667                                         data->selstartx= mx;
1668
1669                                         button_activate_state(C, but, BUTTON_STATE_TEXT_SELECTING);
1670                                         retval= WM_UI_HANDLER_BREAK;
1671                                 }
1672                                 else if(inbox==0) {
1673                                         /* if searchbox, click outside will cancel */
1674                                         if(data->searchbox)
1675                                                 data->cancel= data->escapecancel= 1;
1676                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1677                                         retval= WM_UI_HANDLER_BREAK;
1678                                 }
1679                         }
1680                         else if(inbox) {
1681                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1682                                 retval= WM_UI_HANDLER_BREAK;
1683                         }
1684                         break;
1685                 }
1686         }
1687
1688         if(event->val==KM_PRESS) {
1689                 switch (event->type) {
1690                         case VKEY:
1691                         case XKEY:
1692                         case CKEY:
1693                                 if(event->ctrl || event->oskey) {
1694                                         if(event->type == VKEY)
1695                                                 changed= ui_textedit_copypaste(but, data, 1, 0, 0);
1696                                         else if(event->type == CKEY)
1697                                                 changed= ui_textedit_copypaste(but, data, 0, 1, 0);
1698                                         else if(event->type == XKEY)
1699                                                 changed= ui_textedit_copypaste(but, data, 0, 0, 1);
1700
1701                                         retval= WM_UI_HANDLER_BREAK;
1702                                 }
1703                                 break;
1704                         case RIGHTARROWKEY:
1705                                 ui_textedit_move(but, data, 1, event->shift, event->ctrl);
1706                                 retval= WM_UI_HANDLER_BREAK;
1707                                 break;
1708                         case LEFTARROWKEY:
1709                                 ui_textedit_move(but, data, 0, event->shift, event->ctrl);
1710                                 retval= WM_UI_HANDLER_BREAK;
1711                                 break;
1712                         case DOWNARROWKEY:
1713                                 if(data->searchbox) {
1714                                         ui_searchbox_event(C, data->searchbox, but, event);
1715                                         break;
1716                                 }
1717                                 /* pass on purposedly */
1718                         case ENDKEY:
1719                                 ui_textedit_move_end(but, data, 1, event->shift);
1720                                 retval= WM_UI_HANDLER_BREAK;
1721                                 break;
1722                         case UPARROWKEY:
1723                                 if(data->searchbox) {
1724                                         ui_searchbox_event(C, data->searchbox, but, event);
1725                                         break;
1726                                 }
1727                                 /* pass on purposedly */
1728                         case HOMEKEY:
1729                                 ui_textedit_move_end(but, data, 0, event->shift);
1730                                 retval= WM_UI_HANDLER_BREAK;
1731                                 break;
1732                         case PADENTER:
1733                         case RETKEY:
1734                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1735                                 retval= WM_UI_HANDLER_BREAK;
1736                                 break;
1737                         case DELKEY:
1738                                 changed= ui_textedit_delete(but, data, 1, 0);
1739                                 retval= WM_UI_HANDLER_BREAK;
1740                                 break;
1741
1742                         case BACKSPACEKEY:
1743                                 changed= ui_textedit_delete(but, data, 0, event->shift);
1744                                 retval= WM_UI_HANDLER_BREAK;
1745                                 break;
1746                                 
1747                         case TABKEY:
1748                                 /* there is a key conflict here, we can't tab with autocomplete */
1749                                 if(but->autocomplete_func || data->searchbox) {
1750                                         changed= ui_textedit_autocomplete(C, but, data);
1751                                         update= 1; /* do live update for tab key */
1752                                         retval= WM_UI_HANDLER_BREAK;
1753                                 }
1754                                 /* the hotkey here is not well defined, was G.qual so we check all */
1755                                 else if(event->shift || event->ctrl || event->alt || event->oskey) {
1756                                         ui_textedit_prev_but(block, but, data);
1757                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1758                                 }
1759                                 else {
1760                                         ui_textedit_next_but(block, but, data);
1761                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1762                                 }
1763                                 retval= WM_UI_HANDLER_BREAK;
1764                                 break;
1765                 }
1766
1767                 if(event->ascii && (retval == WM_UI_HANDLER_CONTINUE)) {
1768                         changed= ui_textedit_type_ascii(but, data, event->ascii);
1769                         retval= WM_UI_HANDLER_BREAK;
1770                         
1771                 }
1772                 /* textbutton with magnifier icon: do live update for search button */
1773                 if(but->icon==ICON_VIEWZOOM)
1774                         update= 1;
1775         }
1776
1777         if(changed) {
1778                 /* only update when typing for TAB key */
1779                 if(update && data->interactive) ui_apply_button(C, block, but, data, 1);
1780                 else ui_check_but(but);
1781                 
1782                 if(data->searchbox)
1783                         ui_searchbox_update(C, data->searchbox, but, 1); /* 1 = reset */
1784         }
1785
1786         if(changed || (retval == WM_UI_HANDLER_BREAK))
1787                 ED_region_tag_redraw(data->region);
1788 }
1789
1790 static void ui_do_but_textedit_select(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1791 {
1792         int mx, my, retval= WM_UI_HANDLER_CONTINUE;
1793
1794         switch(event->type) {
1795                 case MOUSEMOVE: {
1796                         mx= event->x;
1797                         my= event->y;
1798                         ui_window_to_block(data->region, block, &mx, &my);
1799
1800                         ui_textedit_set_cursor_select(but, data, mx);
1801                         retval= WM_UI_HANDLER_BREAK;
1802                         break;
1803                 }
1804                 case LEFTMOUSE:
1805                         if(event->val == KM_RELEASE)
1806                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1807                         retval= WM_UI_HANDLER_BREAK;
1808                         break;
1809         }
1810
1811         if(retval == WM_UI_HANDLER_BREAK) {
1812                 ui_check_but(but);
1813                 ED_region_tag_redraw(data->region);
1814         }
1815 }
1816
1817 /* ************* number editing for various types ************* */
1818
1819 static void ui_numedit_begin(uiBut *but, uiHandleButtonData *data)
1820 {
1821         float softrange, softmin, softmax;
1822
1823         if(but->type == BUT_CURVE) {
1824                 data->cumap= (CurveMapping*)but->poin;
1825                 but->editcumap= data->coba;
1826         }
1827         else if(but->type == BUT_COLORBAND) {
1828                 data->coba= (ColorBand*)but->poin;
1829                 but->editcoba= data->coba;
1830         }
1831         else if(ELEM3(but->type, BUT_NORMAL, HSVCUBE, HSVCIRCLE)) {
1832                 ui_get_but_vectorf(but, data->origvec);
1833                 VECCOPY(data->vec, data->origvec);
1834                 but->editvec= data->vec;
1835         }
1836         else {
1837                 data->startvalue= ui_get_but_val(but);
1838                 data->origvalue= data->startvalue;
1839                 data->value= data->origvalue;
1840                 but->editval= &data->value;
1841
1842                 softmin= but->softmin;
1843                 softmax= but->softmax;
1844                 softrange= softmax - softmin;
1845
1846                 data->dragfstart= (softrange == 0.0)? 0.0: (data->value - softmin)/softrange;
1847                 data->dragf= data->dragfstart;
1848         }
1849
1850         data->dragchange= 0;
1851         data->draglock= 1;
1852 }
1853
1854 static void ui_numedit_end(uiBut *but, uiHandleButtonData *data)
1855 {
1856         but->editval= NULL;
1857         but->editvec= NULL;
1858         but->editcoba= NULL;
1859         but->editcumap= NULL;
1860
1861         data->dragstartx= 0;
1862         data->draglastx= 0;
1863         data->dragchange= 0;
1864         data->dragcbd= NULL;
1865         data->dragsel= 0;
1866 }
1867
1868 static void ui_numedit_apply(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data)
1869 {
1870         if(data->interactive) ui_apply_button(C, block, but, data, 1);
1871         else ui_check_but(but);
1872
1873         ED_region_tag_redraw(data->region);
1874 }
1875
1876 /* ****************** menu opening for various types **************** */
1877
1878 static void ui_blockopen_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
1879 {
1880         uiBlockCreateFunc func= NULL;
1881         uiBlockHandleCreateFunc handlefunc= NULL;
1882         uiMenuCreateFunc menufunc= NULL;
1883         char *menustr= NULL;
1884         void *arg= NULL;
1885
1886         switch(but->type) {
1887                 case BLOCK:
1888                 case PULLDOWN:
1889                         if(but->menu_create_func) {
1890                                 menufunc= but->menu_create_func;
1891                                 arg= but->poin;
1892                         }
1893                         else {
1894                                 func= but->block_create_func;
1895                                 arg= but->poin?but->poin:but->func_argN;
1896                         }
1897                         break;
1898                 case MENU:
1899                         if(but->menu_create_func) {
1900                                 menufunc= but->menu_create_func;
1901                                 arg= but->poin;
1902                         }
1903                         else {
1904                                 data->origvalue= ui_get_but_val(but);
1905                                 data->value= data->origvalue;
1906                                 but->editval= &data->value;
1907
1908                                 menustr= but->str;
1909                         }
1910                         break;
1911                 case ICONROW:
1912                         menufunc= ui_block_func_ICONROW;
1913                         arg= but;
1914                         break;
1915                 case ICONTEXTROW:
1916                         menufunc= ui_block_func_ICONTEXTROW;
1917                         arg= but;
1918                         break;
1919                 case COL:
1920                         ui_get_but_vectorf(but, data->origvec);
1921                         VECCOPY(data->vec, data->origvec);
1922                         but->editvec= data->vec;
1923
1924                         handlefunc= ui_block_func_COL;
1925                         arg= but;
1926                         break;
1927         }
1928
1929         if(func || handlefunc) {
1930                 data->menu= ui_popup_block_create(C, data->region, but, func, handlefunc, arg);
1931                 if(but->block->handle)
1932                         data->menu->popup= but->block->handle->popup;
1933         }
1934         else if(menufunc || menustr) {
1935                 data->menu= ui_popup_menu_create(C, data->region, but, menufunc, arg, menustr);
1936                 if(but->block->handle)
1937                         data->menu->popup= but->block->handle->popup;
1938         }
1939
1940         /* this makes adjacent blocks auto open from now on */
1941         //if(but->block->auto_open==0) but->block->auto_open= 1;
1942 }
1943
1944 static void ui_blockopen_end(bContext *C, uiBut *but, uiHandleButtonData *data)
1945 {
1946         if(but) {
1947                 but->editval= NULL;
1948                 but->editvec= NULL;
1949
1950                 but->block->auto_open_last= PIL_check_seconds_timer();
1951         }
1952
1953         if(data->menu) {
1954                 ui_popup_block_free(C, data->menu);
1955                 data->menu= NULL;
1956         }
1957 }
1958
1959 /* ***************** events for different button types *************** */
1960
1961 static int ui_do_but_BUT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1962 {
1963         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1964                 if(event->type == LEFTMOUSE && event->val==KM_PRESS) {
1965                         button_activate_state(C, but, BUTTON_STATE_WAIT_RELEASE);
1966                         return WM_UI_HANDLER_BREAK;
1967                 }
1968                 else if(event->type == LEFTMOUSE && but->block->handle) {
1969                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1970                         return WM_UI_HANDLER_BREAK;
1971                 }
1972                 else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS) {
1973                         button_activate_state(C, but, BUTTON_STATE_WAIT_FLASH);
1974                         return WM_UI_HANDLER_BREAK;
1975                 }
1976         }
1977         else if(data->state == BUTTON_STATE_WAIT_RELEASE) {
1978                 if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
1979                         if(!(but->flag & UI_SELECT))
1980                                 data->cancel= 1;
1981                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1982                         return WM_UI_HANDLER_BREAK;
1983                 }
1984         }
1985
1986         return WM_UI_HANDLER_CONTINUE;
1987 }
1988
1989 static int ui_do_but_HOTKEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1990 {
1991         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1992                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
1993                         but->drawstr[0]= 0;
1994                         but->modifier_key= 0;
1995                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
1996                         return WM_UI_HANDLER_BREAK;
1997                 }
1998         }
1999         else if(data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2000                 
2001                 if(event->type == MOUSEMOVE)
2002                         return WM_UI_HANDLER_CONTINUE;
2003                 
2004                 if(event->type == LEFTMOUSE && event->val==KM_PRESS) {
2005                         /* only cancel if click outside the button */
2006                         if(ui_mouse_inside_button(but->active->region, but, event->x, event->y) == 0) {
2007                                 /* data->cancel doesnt work, this button opens immediate */
2008                                 if(but->flag & UI_BUT_IMMEDIATE)
2009                                         ui_set_but_val(but, 0);
2010                                 else
2011                                         data->cancel= 1;
2012                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2013                                 return WM_UI_HANDLER_BREAK;
2014                         }
2015                 }
2016                 
2017                 /* always set */
2018                 but->modifier_key = 0;
2019                 if(event->shift)
2020                         but->modifier_key |= KM_SHIFT;
2021                 if(event->alt)
2022                         but->modifier_key |= KM_ALT;
2023                 if(event->ctrl)
2024                         but->modifier_key |= KM_CTRL;
2025                 if(event->oskey)
2026                         but->modifier_key |= KM_OSKEY;
2027                 
2028                 ui_check_but(but);
2029                 ED_region_tag_redraw(data->region);
2030                         
2031                 if(event->val==KM_PRESS) {
2032                         if(ISHOTKEY(event->type)) { 
2033                                 
2034                                 if(WM_key_event_string(event->type)[0])
2035                                         ui_set_but_val(but, event->type);
2036                                 else
2037                                         data->cancel= 1;
2038                                 
2039                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2040                                 return WM_UI_HANDLER_BREAK;
2041                         }
2042                 }
2043         }
2044         
2045         return WM_UI_HANDLER_CONTINUE;
2046 }
2047
2048 static int ui_do_but_KEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2049 {
2050         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2051                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2052                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
2053                         return WM_UI_HANDLER_BREAK;
2054                 }
2055         }
2056         else if(data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2057                 if(event->type == MOUSEMOVE)
2058                         return WM_UI_HANDLER_CONTINUE;
2059
2060                 if(event->val==KM_PRESS) {
2061                         if(WM_key_event_string(event->type)[0])
2062                                 ui_set_but_val(but, event->type);
2063                         else
2064                                 data->cancel= 1;
2065
2066                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2067                 }
2068         }
2069
2070         return WM_UI_HANDLER_CONTINUE;
2071 }
2072
2073 static int ui_do_but_TEX(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2074 {
2075         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2076                 if(ELEM(event->type, LEFTMOUSE, EVT_BUT_OPEN) && event->val==KM_PRESS) {
2077                         if(but->dt == UI_EMBOSSN && !event->ctrl);
2078                         else {
2079                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2080                                 return WM_UI_HANDLER_BREAK;
2081                         }
2082                 }
2083         }
2084         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2085                 ui_do_but_textedit(C, block, but, data, event);
2086                 return WM_UI_HANDLER_BREAK;
2087         }
2088         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2089                 ui_do_but_textedit_select(C, block, but, data, event);
2090                 return WM_UI_HANDLER_BREAK;
2091         }
2092
2093         return WM_UI_HANDLER_CONTINUE;
2094 }
2095
2096 static int ui_do_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2097 {
2098         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2099                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2100                         data->togdual= event->ctrl;
2101                         data->togonly= !event->shift;
2102                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2103                         return WM_UI_HANDLER_BREAK;
2104                 }
2105         }
2106         return WM_UI_HANDLER_CONTINUE;
2107 }
2108
2109 static int ui_do_but_EXIT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2110 {
2111         
2112         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2113
2114                 /* first handle click on icondrag type button */
2115                 if(event->type==LEFTMOUSE && but->dragpoin) {
2116                         if(ui_but_mouse_inside_icon(but, data->region, event)) {
2117                                 
2118                                 /* tell the button to wait and keep checking further events to
2119                                  * see if it should start dragging */
2120                                 button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
2121                                 data->dragstartx= event->x;
2122                                 data->dragstarty= event->y;
2123                                 return WM_UI_HANDLER_CONTINUE;
2124                         }
2125                 }
2126                 
2127                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2128                         int ret = WM_UI_HANDLER_BREAK;
2129                         /* XXX (a bit ugly) Special case handling for filebrowser drag button */
2130                         if(but->dragpoin && but->imb && ui_but_mouse_inside_icon(but, data->region, event)) {
2131                                 ret = WM_UI_HANDLER_CONTINUE;
2132                         }
2133                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2134                         return ret;
2135                 }
2136         }
2137         else if(data->state == BUTTON_STATE_WAIT_DRAG) {
2138                 
2139                 /* this function also ends state */
2140                 if(ui_but_start_drag(C, but, data, event)) {
2141                         return WM_UI_HANDLER_BREAK;
2142                 }
2143                 
2144                 /* If the mouse has been pressed and released, getting to 
2145                  * this point without triggering a drag, then clear the 
2146                  * drag state for this button and continue to pass on the event */
2147                 if(event->type==LEFTMOUSE && event->val==KM_RELEASE) {
2148                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2149                         return WM_UI_HANDLER_CONTINUE;
2150                 }
2151                 
2152                 /* while waiting for a drag to be triggered, always block 
2153                  * other events from getting handled */
2154                 return WM_UI_HANDLER_BREAK;
2155         }
2156         
2157         return WM_UI_HANDLER_CONTINUE;
2158 }
2159
2160 /* var names match ui_numedit_but_NUM */
2161 static float ui_numedit_apply_snapf(float tempf, float softmin, float softmax, float softrange, int snap)
2162 {
2163         if(tempf==softmin || tempf==softmax)
2164                 return tempf;
2165
2166         switch(snap) {
2167         case 0:
2168                 break;
2169         case 1:
2170                 if(tempf==softmin || tempf==softmax) { }
2171                 else if(softrange < 2.10) tempf= 0.1*floor(10*tempf);
2172                 else if(softrange < 21.0) tempf= floor(tempf);
2173                 else tempf= 10.0*floor(tempf/10.0);
2174                 break;
2175         case 2:
2176                 if(tempf==softmin || tempf==softmax) { }
2177                 else if(softrange < 2.10) tempf= 0.01*floor(100.0*tempf);
2178                 else if(softrange < 21.0) tempf= 0.1*floor(10.0*tempf);
2179                 else tempf= floor(tempf);
2180                 break;
2181         }
2182
2183         return tempf;
2184 }
2185
2186 static float ui_numedit_apply_snap(int temp, float softmin, float softmax, int snap)
2187 {
2188         if(temp==softmin || temp==softmax)
2189                 return temp;
2190
2191         switch(snap) {
2192         case 0:
2193                 break;
2194         case 1:
2195                 temp= 10*(temp/10);
2196                 break;
2197         case 2:
2198                 temp= 100*(temp/100);
2199                 break;
2200         }
2201
2202         return temp;
2203 }
2204
2205 static int ui_numedit_but_NUM(uiBut *but, uiHandleButtonData *data, float fac, int snap, int mx)
2206 {
2207         float deler, tempf, softmin, softmax, softrange;
2208         int lvalue, temp, changed= 0;
2209         
2210         if(mx == data->draglastx)
2211                 return changed;
2212         
2213         /* drag-lock - prevent unwanted scroll adjustments */
2214         /* change value (now 3) to adjust threshold in pixels */
2215         if(data->draglock) {
2216                 if(abs(mx-data->dragstartx) <= 3)
2217                         return changed;
2218
2219                 data->draglock= 0;
2220                 data->dragstartx= mx;  /* ignore mouse movement within drag-lock */
2221         }
2222
2223         softmin= but->softmin;
2224         softmax= but->softmax;
2225         softrange= softmax - softmin;
2226
2227         if(ui_is_a_warp_but(but)) {
2228                 /* Mouse location isn't screen clamped to the screen so use a linear mapping
2229                  * 2px == 1-int, or 1px == 1-ClickStep */
2230                 if(ui_is_but_float(but)) {
2231                         fac *= 0.01*but->a1;
2232                         tempf = data->startvalue + ((mx - data->dragstartx) * fac);
2233                         tempf= ui_numedit_apply_snapf(tempf, softmin, softmax, softrange, snap);
2234
2235 #if 1           /* fake moving the click start, nicer for dragging back after passing the limit */
2236                         if(tempf < softmin) {
2237                                 data->dragstartx -= (softmin-tempf) / fac;
2238                                 tempf= softmin;
2239                         } else if (tempf > softmax) {
2240                                 data->dragstartx += (tempf-softmax) / fac;
2241                                 tempf= softmax;
2242                         }
2243 #else
2244                         CLAMP(tempf, softmin, softmax);
2245 #endif
2246
2247                         if(tempf != data->value) {
2248                                 data->dragchange= 1;
2249                                 data->value= tempf;
2250                                 changed= 1;
2251                         }
2252                 }
2253                 else {
2254                         if(softrange > 256)             fac= 1.0;               /* 1px == 1 */
2255                         else if(softrange > 32) fac= 1.0/2.0;   /* 2px == 1 */
2256                         else                                    fac= 1.0/16.0;  /* 16px == 1? */
2257
2258                         temp= data->startvalue + ((mx - data->dragstartx) * fac);
2259                         temp= ui_numedit_apply_snap(temp, softmin, softmax, snap);
2260
2261 #if 1           /* fake moving the click start, nicer for dragging back after passing the limit */
2262                         if(temp < softmin) {
2263                                 data->dragstartx -= (softmin-temp) / fac;
2264                                 temp= softmin;
2265                         } else if (temp > softmax) {
2266                                 data->dragstartx += (temp-softmax) / fac;
2267                                 temp= softmax;
2268                         }
2269 #else
2270                         CLAMP(temp, softmin, softmax);
2271 #endif
2272
2273                         if(temp != data->value) {
2274                                 data->dragchange= 1;
2275                                 data->value= temp;
2276                                 changed= 1;
2277                         }
2278                 }
2279
2280                 data->draglastx= mx;
2281         }
2282         else {
2283                 /* Use a non-linear mapping of the mouse drag especially for large floats (normal behavior) */
2284                 deler= 500;
2285                 if(!ui_is_but_float(but)) {
2286                         /* prevent large ranges from getting too out of control */
2287                         if (softrange > 600) deler = powf(softrange, 0.75);
2288                         
2289                         if (softrange < 100) deler= 200.0;
2290                         if (softrange < 25) deler= 50.0;
2291                 }
2292                 deler /= fac;
2293
2294                 if(softrange > 11) {
2295                         /* non linear change in mouse input- good for high precicsion */
2296                         data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.002);
2297                 } else if (softrange > 129) { /* only scale large int buttons */
2298                         /* non linear change in mouse input- good for high precicsionm ints need less fine tuning */
2299                         data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.004);
2300                 } else {
2301                         /*no scaling */
2302                         data->dragf+= ((float)(mx-data->draglastx))/deler ;
2303                 }
2304         
2305                 CLAMP(data->dragf, 0.0, 1.0);
2306                 data->draglastx= mx;
2307                 tempf= (softmin + data->dragf*softrange);
2308
2309
2310                 if(!ui_is_but_float(but)) {
2311                         temp= floor(tempf+.5);
2312
2313                         temp= ui_numedit_apply_snap(temp, softmin, softmax, snap);
2314
2315                         CLAMP(temp, softmin, softmax);
2316                         lvalue= (int)data->value;
2317                         
2318                         if(temp != lvalue) {
2319                                 data->dragchange= 1;
2320                                 data->value= (double)temp;
2321                                 changed= 1;
2322                         }
2323                 }
2324                 else {
2325                         temp= 0;
2326                         tempf= ui_numedit_apply_snapf(tempf, softmin, softmax, softrange, snap);
2327
2328                         CLAMP(tempf, softmin, softmax);
2329
2330                         if(tempf != data->value) {
2331                                 data->dragchange= 1;
2332                                 data->value= tempf;
2333                                 changed= 1;
2334                         }
2335                 }
2336         }
2337
2338
2339         return changed;
2340 }
2341
2342 static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2343 {
2344         int mx, my;     /* mouse location scaled to fit the UI */
2345         int screen_mx, screen_my; /* mouse location kept at screen pixel coords */
2346         int click= 0;
2347         int retval= WM_UI_HANDLER_CONTINUE;
2348         
2349         mx= screen_mx= event->x;
2350         my= screen_my= event->y;
2351
2352         ui_window_to_block(data->region, block, &mx, &my);
2353
2354         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2355                 /* XXX hardcoded keymap check.... */
2356                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2357                         mx= but->x1;
2358                         click= 1;
2359                 }
2360                 else if(event->type == WHEELUPMOUSE && event->alt) {
2361                         mx= but->x2;
2362                         click= 1;
2363                 }
2364                 else if(event->val==KM_PRESS) {
2365                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2366                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2367                                 retval= WM_UI_HANDLER_BREAK;
2368                         }
2369                         else if(event->type == LEFTMOUSE) {
2370                                 data->dragstartx= data->draglastx= ui_is_a_warp_but(but) ? screen_mx:mx;
2371                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2372                                 retval= WM_UI_HANDLER_BREAK;
2373                         }
2374                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2375                                 click= 1;
2376                         else if (event->type == MINUSKEY && event->val==KM_PRESS) {
2377                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2378                                 data->value = -data->value;
2379                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2380                                 retval= WM_UI_HANDLER_BREAK;
2381                         }
2382                 }
2383                 
2384         }
2385         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2386                 if(event->type == ESCKEY) {
2387                         data->cancel= 1;
2388                         data->escapecancel= 1;
2389                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2390                 }
2391                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2392                         if(data->dragchange)
2393                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2394                         else
2395                                 click= 1;
2396                 }
2397                 else if(event->type == MOUSEMOVE) {
2398                         float fac;
2399                         int snap;
2400
2401                         fac= 1.0f;
2402                         if(event->shift) fac /= 10.0f;
2403                         if(event->alt) fac /= 20.0f;
2404                         
2405                         snap= (event->ctrl)? (event->shift)? 2: 1: 0;
2406
2407                         if(ui_numedit_but_NUM(but, data, fac, snap, (ui_is_a_warp_but(but) ? screen_mx:mx)))
2408                                 ui_numedit_apply(C, block, but, data);
2409                 }
2410                 retval= WM_UI_HANDLER_BREAK;
2411         }
2412         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2413                 ui_do_but_textedit(C, block, but, data, event);
2414                 retval= WM_UI_HANDLER_BREAK;
2415         }
2416         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2417                 ui_do_but_textedit_select(C, block, but, data, event);
2418                 retval= WM_UI_HANDLER_BREAK;
2419         }
2420         
2421         if(click) {
2422                 /* we can click on the side arrows to increment/decrement,
2423                  * or click inside to edit the value directly */
2424                 float tempf, softmin, softmax;
2425                 int temp;
2426
2427                 softmin= but->softmin;
2428                 softmax= but->softmax;
2429
2430                 if(!ui_is_but_float(but)) {
2431                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2432                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2433
2434                                 temp= (int)data->value - 1;
2435                                 if(temp>=softmin && temp<=softmax)
2436                                         data->value= (double)temp;
2437                                 else
2438                                         data->cancel= 1;
2439
2440                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2441                         }
2442                         else if(mx > (but->x1 + (2*(but->x2 - but->x1)/3) + 3)) {
2443                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2444
2445                                 temp= (int)data->value + 1;
2446                                 if(temp>=softmin && temp<=softmax)
2447                                         data->value= (double)temp;
2448                                 else
2449                                         data->cancel= 1;
2450
2451                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2452                         }
2453                         else
2454                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2455                 }
2456                 else {
2457                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2458                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2459
2460                                 tempf= data->value - 0.01*but->a1;
2461                                 if (tempf < softmin) tempf = softmin;
2462                                 data->value= tempf;
2463
2464                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2465                         }
2466                         else if(mx > but->x1 + (2*((but->x2 - but->x1)/3) + 3)) {
2467                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2468
2469                                 tempf= data->value + 0.01*but->a1;
2470                                 if (tempf > softmax) tempf = softmax;
2471                                 data->value= tempf;
2472
2473                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2474                         }
2475                         else
2476                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2477                 }
2478
2479                 retval= WM_UI_HANDLER_BREAK;
2480         }
2481         
2482         return retval;
2483 }
2484
2485 static int ui_numedit_but_SLI(uiBut *but, uiHandleButtonData *data, int shift, int ctrl, int mx)
2486 {
2487         float deler, f, tempf, softmin, softmax, softrange;
2488         int temp, lvalue, changed= 0;
2489
2490         softmin= but->softmin;
2491         softmax= but->softmax;
2492         softrange= softmax - softmin;
2493
2494         if(but->type==NUMSLI) deler= ((but->x2-but->x1) - 5.0*but->aspect);
2495         else if(but->type==HSVSLI) deler= ((but->x2-but->x1)/2 - 5.0*but->aspect);
2496         else if(but->type==SCROLL) {
2497                 int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2498                 float size= (horizontal)? (but->x2-but->x1): -(but->y2-but->y1);
2499                 deler= size*(but->softmax - but->softmin)/(but->softmax - but->softmin + but->a1);
2500         }
2501         else deler= (but->x2-but->x1- 5.0*but->aspect);
2502
2503         f= (float)(mx-data->dragstartx)/deler + data->dragfstart;
2504         
2505         if(shift)
2506                 f= (f-data->dragfstart)/10.0 + data->dragfstart;
2507
2508         CLAMP(f, 0.0, 1.0);
2509         tempf= softmin + f*softrange;
2510         temp= floor(tempf+.5);
2511
2512         if(ctrl) {
2513                 if(tempf==softmin || tempf==softmax);
2514                 else if(ui_is_but_float(but)) {
2515
2516                         if(shift) {
2517                                 if(tempf==softmin || tempf==softmax);
2518                                 else if(softmax-softmin < 2.10) tempf= 0.01*floor(100.0*tempf);
2519                                 else if(softmax-softmin < 21.0) tempf= 0.1*floor(10.0*tempf);
2520                                 else tempf= floor(tempf);
2521                         }
2522                         else {
2523                                 if(softmax-softmin < 2.10) tempf= 0.1*floor(10*tempf);
2524                                 else if(softmax-softmin < 21.0) tempf= floor(tempf);
2525                                 else tempf= 10.0*floor(tempf/10.0);
2526                         }
2527                 }
2528                 else {
2529                         temp= 10*(temp/10);
2530                         tempf= temp;
2531                 }
2532         }
2533
2534         if(!ui_is_but_float(but)) {
2535                 lvalue= floor(data->value+0.5);
2536
2537                 CLAMP(temp, softmin, softmax);
2538
2539                 if(temp != lvalue) {
2540                         data->value= temp;
2541                         data->dragchange= 1;
2542                         changed= 1;
2543                 }
2544         }
2545         else {
2546                 CLAMP(tempf, softmin, softmax);
2547
2548                 if(tempf != data->value) {
2549                         data->value= tempf;
2550                         data->dragchange= 1;
2551                         changed= 1;
2552                 }
2553         }
2554
2555         return changed;
2556 }
2557
2558 static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2559 {
2560         int mx, my, click= 0;
2561         int retval= WM_UI_HANDLER_CONTINUE;
2562
2563         mx= event->x;
2564         my= event->y;
2565         ui_window_to_block(data->region, block, &mx, &my);
2566
2567         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2568                 /* XXX hardcoded keymap check.... */
2569                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2570                         mx= but->x1;
2571                         click= 2;
2572                 }
2573                 else if(event->type == WHEELUPMOUSE && event->alt) {
2574                         mx= but->x2;
2575                         click= 2;
2576                 }
2577                 else if(event->val==KM_PRESS) {
2578                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2579                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2580                                 retval= WM_UI_HANDLER_BREAK;
2581                         }
2582                         /* alt-click on sides to get "arrows" like in NUM buttons, and match wheel usage above */
2583                         else if(event->type == LEFTMOUSE && event->alt) {
2584                                 int halfpos = (but->x1 + but->x2) / 2;
2585                                 click = 2;
2586                                 if (mx < halfpos)
2587                                         mx = but->x1;
2588                                 else
2589                                         mx = but->x2;
2590                         }
2591                         else if(event->type == LEFTMOUSE) {
2592                                 data->dragstartx= mx;
2593                                 data->draglastx= mx;
2594                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2595                                 retval= WM_UI_HANDLER_BREAK;
2596                         }
2597                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2598                                 click= 1;
2599                         else if (event->type == MINUSKEY && event->val==KM_PRESS) {
2600                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2601                                 data->value = -data->value;
2602                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2603                                 retval= WM_UI_HANDLER_BREAK;
2604                         }
2605                 }
2606         }
2607         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2608                 if(event->type == ESCKEY) {
2609                         data->cancel= 1;
2610                         data->escapecancel= 1;
2611                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2612                 }
2613                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2614                         if(data->dragchange)
2615                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2616                         else
2617                                 click= 1;
2618                 }
2619                 else if(event->type == MOUSEMOVE) {
2620                         if(ui_numedit_but_SLI(but, data, event->shift, event->ctrl, mx))
2621                                 ui_numedit_apply(C, block, but, data);
2622                 }
2623                 retval= WM_UI_HANDLER_BREAK;
2624         }
2625         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2626                 ui_do_but_textedit(C, block, but, data, event);
2627                 retval= WM_UI_HANDLER_BREAK;
2628         }
2629         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2630                 ui_do_but_textedit_select(C, block, but, data, event);
2631                 retval= WM_UI_HANDLER_BREAK;
2632         }
2633
2634         if(click) {
2635                 if (click==2) {
2636                         /* nudge slider to the left or right */
2637                         float f, tempf, softmin, softmax, softrange;
2638                         int temp;
2639                         
2640                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2641                         
2642                         softmin= but->softmin;
2643                         softmax= but->softmax;
2644                         softrange= softmax - softmin;
2645                         
2646                         tempf= data->value;
2647                         temp= (int)data->value;
2648                         
2649                         /* XXX useles "if", same result for f, uh??? */
2650                         if(but->type==SLI) f= (float)(mx-but->x1)/(but->x2-but->x1);
2651                         else f= (float)(mx- but->x1)/(but->x2-but->x1);
2652                         
2653                         f= softmin + f*softrange;
2654                         
2655                         if(!ui_is_but_float(but)) {
2656                                 if(f<temp) temp--;
2657                                 else temp++;
2658                                 
2659                                 if(temp>=softmin && temp<=softmax)
2660                                         data->value= temp;
2661                                 else
2662                                         data->cancel= 1;
2663                         } 
2664                         else {
2665                                 if(f<tempf) tempf-=.01;
2666                                 else tempf+=.01;
2667                                 
2668                                 if(tempf>=softmin && tempf<=softmax)
2669                                         data->value= tempf;
2670                                 else
2671                                         data->cancel= 1;
2672                         }
2673                         
2674                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2675                         retval= WM_UI_HANDLER_BREAK;
2676                 }
2677                 else {
2678                         /* edit the value directly */
2679                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2680                         retval= WM_UI_HANDLER_BREAK;
2681                 }
2682         }
2683         
2684         return retval;
2685 }
2686
2687 static int ui_do_but_SCROLL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2688 {
2689         int mx, my, click= 0;
2690         int retval= WM_UI_HANDLER_CONTINUE;
2691         int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2692         
2693         mx= event->x;
2694         my= event->y;
2695         ui_window_to_block(data->region, block, &mx, &my);
2696
2697         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2698                 if(event->val==KM_PRESS) {
2699                         if(event->type == LEFTMOUSE) {
2700                                 if(horizontal) {
2701                                         data->dragstartx= mx;
2702                                         data->draglastx= mx;
2703                                 }
2704                                 else {
2705                                         data->dragstartx= my;
2706                                         data->draglastx= my;
2707                                 }
2708                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2709                                 retval= WM_UI_HANDLER_BREAK;
2710                         }
2711                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2712                                 click= 1;
2713                 }
2714         }
2715         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2716                 if(event->type == ESCKEY) {
2717                         data->cancel= 1;
2718                         data->escapecancel= 1;
2719                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2720                 }
2721                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2722                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2723                 }
2724                 else if(event->type == MOUSEMOVE) {
2725                         if(ui_numedit_but_SLI(but, data, 0, 0, (horizontal)? mx: my))
2726                                 ui_numedit_apply(C, block, but, data);
2727                 }
2728
2729                 retval= WM_UI_HANDLER_BREAK;
2730         }
2731         
2732         return retval;
2733 }
2734
2735
2736 static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2737 {
2738         
2739         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2740                 
2741                 /* first handle click on icondrag type button */
2742                 if(event->type==LEFTMOUSE && but->dragpoin && event->val==KM_PRESS) {
2743                         if(ui_but_mouse_inside_icon(but, data->region, event)) {
2744                                 button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
2745                                 data->dragstartx= event->x;
2746                                 data->dragstarty= event->y;
2747                                 return WM_UI_HANDLER_BREAK;
2748                         }
2749                 }
2750                 
2751                 /* regular open menu */
2752                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2753                         button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
2754                         return WM_UI_HANDLER_BREAK;
2755                 }
2756                 else if(ELEM3(but->type, MENU, ICONROW, ICONTEXTROW)) {
2757                         
2758                         if(event->type == WHEELDOWNMOUSE && event->alt) {
2759                                 data->value= ui_step_name_menu(but, -1);
2760                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2761                                 ui_apply_button(C, but->block, but, data, 1);
2762                                 return WM_UI_HANDLER_BREAK;
2763                         }
2764                         else if(event->type == WHEELUPMOUSE && event->alt) {
2765                                 data->value= ui_step_name_menu(but, 1);
2766                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2767                                 ui_apply_button(C, but->block, but, data, 1);
2768                                 return WM_UI_HANDLER_BREAK;
2769                         }
2770                 }
2771                 else if(but->type==COL) {
2772                         if( ELEM(event->type, WHEELDOWNMOUSE, WHEELUPMOUSE) && event->alt) {
2773                                 float col[3];
2774                                 
2775                                 ui_get_but_vectorf(but, col);
2776                                 rgb_to_hsv(col[0], col[1], col[2], but->hsv, but->hsv+1, but->hsv+2);
2777
2778                                 if(event->type==WHEELDOWNMOUSE)
2779                                         but->hsv[2]= CLAMPIS(but->hsv[2]-0.05f, 0.0f, 1.0f);
2780                                 else
2781                                         but->hsv[2]= CLAMPIS(but->hsv[2]+0.05f, 0.0f, 1.0f);
2782                                 
2783                                 hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], data->vec, data->vec+1, data->vec+2);
2784                                 ui_set_but_vectorf(but, data->vec);
2785                                 
2786                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2787                                 ui_apply_button(C, but->block, but, data, 1);
2788                                 return WM_UI_HANDLER_BREAK;
2789                         }
2790                 }
2791         }
2792         else if(data->state == BUTTON_STATE_WAIT_DRAG) {
2793                 
2794                 /* this function also ends state */
2795                 if(ui_but_start_drag(C, but, data, event)) {
2796                         return WM_UI_HANDLER_BREAK;
2797                 }
2798                 
2799                 /* outside icon quit, not needed if drag activated */
2800                 if(0==ui_but_mouse_inside_icon(but, data->region, event)) {
2801                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2802                         data->cancel= 1;
2803                         return WM_UI_HANDLER_BREAK;
2804                 }
2805                 
2806                 if(event->type==LEFTMOUSE && event->val==KM_RELEASE) {
2807                         button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
2808                         return WM_UI_HANDLER_BREAK;
2809                 }
2810
2811         }
2812
2813         return WM_UI_HANDLER_CONTINUE;
2814 }
2815
2816 static int ui_numedit_but_NORMAL(uiBut *but, uiHandleButtonData *data, int mx, int my)
2817 {
2818         float dx, dy, rad, radsq, mrad, *fp;
2819         int mdx, mdy, changed= 1;
2820         
2821         /* button is presumed square */
2822         /* if mouse moves outside of sphere, it does negative normal */
2823
2824         fp= data->origvec;
2825         rad= (but->x2 - but->x1);
2826         radsq= rad*rad;
2827         
2828         if(fp[2]>0.0f) {
2829                 mdx= (rad*fp[0]);
2830                 mdy= (rad*fp[1]);
2831         }
2832         else if(fp[2]> -1.0f) {
2833                 mrad= rad/sqrt(fp[0]*fp[0] + fp[1]*fp[1]);
2834                 
2835                 mdx= 2.0f*mrad*fp[0] - (rad*fp[0]);
2836                 mdy= 2.0f*mrad*fp[1] - (rad*fp[1]);
2837         }
2838         else mdx= mdy= 0;
2839         
2840         dx= (float)(mx+mdx-data->dragstartx);
2841         dy= (float)(my+mdy-data->dragstarty);
2842
2843         fp= data->vec;
2844         mrad= dx*dx+dy*dy;
2845         if(mrad < radsq) {      /* inner circle */
2846                 fp[0]= dx;
2847                 fp[1]= dy;
2848                 fp[2]= sqrt( radsq-dx*dx-dy*dy );
2849         }
2850         else {  /* outer circle */
2851                 
2852                 mrad= rad/sqrt(mrad);   // veclen
2853                 
2854                 dx*= (2.0f*mrad - 1.0f);
2855                 dy*= (2.0f*mrad - 1.0f);
2856                 
2857                 mrad= dx*dx+dy*dy;
2858                 if(mrad < radsq) {
2859                         fp[0]= dx;
2860                         fp[1]= dy;
2861                         fp[2]= -sqrt( radsq-dx*dx-dy*dy );
2862                 }
2863         }
2864         normalize_v3(fp);
2865
2866         data->draglastx= mx;
2867         data->draglasty= my;
2868
2869         return changed;
2870 }
2871
2872 static int ui_do_but_NORMAL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2873 {
2874         int mx, my;
2875
2876         mx= event->x;
2877         my= event->y;
2878         ui_window_to_block(data->region, block, &mx, &my);
2879
2880         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2881                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2882                         data->dragstartx= mx;
2883                         data->dragstarty= my;
2884                         data->draglastx= mx;
2885                         data->draglasty= my;
2886                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2887
2888                         /* also do drag the first time */
2889                         if(ui_numedit_but_NORMAL(but, data, mx, my))
2890                                 ui_numedit_apply(C, block, but, data);
2891                         
2892                         return WM_UI_HANDLER_BREAK;
2893                 }
2894         }
2895         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2896                 if(event->type == MOUSEMOVE) {
2897                         if(mx!=data->draglastx || my!=data->draglasty) {
2898                                 if(ui_numedit_but_NORMAL(but, data, mx, my))
2899                                         ui_numedit_apply(C, block, but, data);
2900                         }
2901                 }
2902                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2903                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2904
2905                 return WM_UI_HANDLER_BREAK;
2906         }
2907         
2908         return WM_UI_HANDLER_CONTINUE;
2909 }
2910
2911 static int ui_numedit_but_HSVCUBE(uiBut *but, uiHandleButtonData *data, int mx, int my)
2912 {
2913         float rgb[3], hsv[3];
2914         float x, y;
2915         int changed= 1;
2916         int color_profile = but->block->color_profile;
2917         
2918         if (but->rnaprop) {
2919                 if (RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA)
2920                         color_profile = BLI_PR_NONE;
2921         }
2922         
2923         ui_get_but_vectorf(but, rgb);
2924         rgb_to_hsv(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
2925                 
2926         /* relative position within box */
2927         x= ((float)mx-but->x1)/(but->x2-but->x1);
2928         y= ((float)my-but->y1)/(but->y2-but->y1);
2929         CLAMP(x, 0.0, 1.0);
2930         CLAMP(y, 0.0, 1.0);
2931         
2932         if(but->a1==0) {
2933                 hsv[2]= x; 
2934                 hsv[1]= y; 
2935         }
2936         else if(but->a1==1) {
2937                 hsv[0]= x;                              
2938                 hsv[2]= y;                              
2939         }
2940         else if(but->a1==2) {
2941                 hsv[0]= x; 
2942                 hsv[1]= y; 
2943         }
2944         else if(but->a1==3) {
2945                 hsv[0]= x; 
2946         }
2947         else if(but->a1==4) {
2948                 hsv[1]= x; 
2949         }
2950         else if(but->a1==5) {
2951                 hsv[2]= x; 
2952         }
2953         else if (but->a1==9){
2954                 float range;
2955                 
2956                 /* vertical 'value' strip */
2957
2958                 /* exception only for value strip - use the range set in but->min/max */
2959                 range = but->softmax - but->softmin;
2960                 hsv[2] = y*range + but->softmin;
2961                 
2962                 if (color_profile)
2963                         hsv[2] = srgb_to_linearrgb(hsv[2]);
2964                 
2965                 if (hsv[2] > but->softmax)
2966                         hsv[2] = but->softmax;
2967         }
2968
2969         hsv_to_rgb(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2);
2970         copy_v3_v3(data->vec, rgb);
2971
2972         data->draglastx= mx;
2973         data->draglasty= my;
2974
2975         return changed;
2976 }
2977
2978 static int ui_do_but_HSVCUBE(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2979 {
2980         int mx, my;
2981
2982         mx= event->x;