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