66ea2ebc265b42c2564e0b22bc25dee12fb5e551
[blender-staging.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                 }
2085         }
2086         
2087         return WM_UI_HANDLER_CONTINUE;
2088 }
2089
2090 static int ui_do_but_KEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2091 {
2092         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2093                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2094                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
2095                         return WM_UI_HANDLER_BREAK;
2096                 }
2097         }
2098         else if(data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2099                 if(event->type == MOUSEMOVE)
2100                         return WM_UI_HANDLER_CONTINUE;
2101
2102                 if(event->val==KM_PRESS) {
2103                         if(WM_key_event_string(event->type)[0])
2104                                 ui_set_but_val(but, event->type);
2105                         else
2106                                 data->cancel= 1;
2107
2108                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2109                 }
2110         }
2111
2112         return WM_UI_HANDLER_CONTINUE;
2113 }
2114
2115 static int ui_do_but_TEX(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2116 {
2117         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2118                 if(ELEM(event->type, LEFTMOUSE, EVT_BUT_OPEN) && event->val==KM_PRESS) {
2119                         if(but->dt == UI_EMBOSSN && !event->ctrl);
2120                         else {
2121                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2122                                 return WM_UI_HANDLER_BREAK;
2123                         }
2124                 }
2125         }
2126         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2127                 ui_do_but_textedit(C, block, but, data, event);
2128                 return WM_UI_HANDLER_BREAK;
2129         }
2130         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2131                 ui_do_but_textedit_select(C, block, but, data, event);
2132                 return WM_UI_HANDLER_BREAK;
2133         }
2134
2135         return WM_UI_HANDLER_CONTINUE;
2136 }
2137
2138 static int ui_do_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2139 {
2140         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2141                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2142                         data->togdual= event->ctrl;
2143                         data->togonly= !event->shift;
2144                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2145                         return WM_UI_HANDLER_BREAK;
2146                 }
2147         }
2148         return WM_UI_HANDLER_CONTINUE;
2149 }
2150
2151 static int ui_do_but_EXIT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2152 {
2153         
2154         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2155
2156                 /* first handle click on icondrag type button */
2157                 if(event->type==LEFTMOUSE && but->dragpoin) {
2158                         if(ui_but_mouse_inside_icon(but, data->region, event)) {
2159                                 
2160                                 /* tell the button to wait and keep checking further events to
2161                                  * see if it should start dragging */
2162                                 button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
2163                                 data->dragstartx= event->x;
2164                                 data->dragstarty= event->y;
2165                                 return WM_UI_HANDLER_CONTINUE;
2166                         }
2167                 }
2168                 
2169                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2170                         int ret = WM_UI_HANDLER_BREAK;
2171                         /* XXX (a bit ugly) Special case handling for filebrowser drag button */
2172                         if(but->dragpoin && but->imb && ui_but_mouse_inside_icon(but, data->region, event)) {
2173                                 ret = WM_UI_HANDLER_CONTINUE;
2174                         }
2175                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2176                         return ret;
2177                 }
2178         }
2179         else if(data->state == BUTTON_STATE_WAIT_DRAG) {
2180                 
2181                 /* this function also ends state */
2182                 if(ui_but_start_drag(C, but, data, event)) {
2183                         return WM_UI_HANDLER_BREAK;
2184                 }
2185                 
2186                 /* If the mouse has been pressed and released, getting to 
2187                  * this point without triggering a drag, then clear the 
2188                  * drag state for this button and continue to pass on the event */
2189                 if(event->type==LEFTMOUSE && event->val==KM_RELEASE) {
2190                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2191                         return WM_UI_HANDLER_CONTINUE;
2192                 }
2193                 
2194                 /* while waiting for a drag to be triggered, always block 
2195                  * other events from getting handled */
2196                 return WM_UI_HANDLER_BREAK;
2197         }
2198         
2199         return WM_UI_HANDLER_CONTINUE;
2200 }
2201
2202 /* var names match ui_numedit_but_NUM */
2203 static float ui_numedit_apply_snapf(uiBut *but, float tempf, float softmin, float softmax, float softrange, int snap)
2204 {
2205         if(tempf==softmin || tempf==softmax || snap==0) {
2206                 /* pass */
2207         }
2208         else {
2209                 float fac= 1.0f;
2210                 
2211                 if(ui_is_but_unit(but)) {
2212                         Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
2213                         int unit_type = RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
2214
2215                         if(bUnit_IsValid(scene->unit.system, unit_type)) {
2216                                 fac= (float)bUnit_BaseScalar(scene->unit.system, unit_type);
2217                                 if(ELEM3(unit_type, B_UNIT_LENGTH, B_UNIT_AREA, B_UNIT_VOLUME)) {
2218                                         fac /= scene->unit.scale_length;
2219                                 }
2220                         }
2221                 }
2222
2223                 if(fac != 1.0f) {
2224                         /* snap in unit-space */
2225                         tempf /= fac;
2226                         softmin /= fac;
2227                         softmax /= fac;
2228                         softrange /= fac;
2229                 }
2230
2231                 if(snap==1) {
2232                         if(softrange < 2.10) tempf= 0.1*floor(10*tempf);
2233                         else if(softrange < 21.0) tempf= floor(tempf);
2234                         else tempf= 10.0*floor(tempf/10.0);
2235                 }
2236                 else if(snap==2) {
2237                         if(softrange < 2.10) tempf= 0.01*floor(100.0*tempf);
2238                         else if(softrange < 21.0) tempf= 0.1*floor(10.0*tempf);
2239                         else tempf= floor(tempf);
2240                 }
2241                 
2242                 if(fac != 1.0f)
2243                         tempf *= fac;
2244         }
2245
2246         return tempf;
2247 }
2248
2249 static float ui_numedit_apply_snap(int temp, float softmin, float softmax, int snap)
2250 {
2251         if(temp==softmin || temp==softmax)
2252                 return temp;
2253
2254         switch(snap) {
2255         case 0:
2256                 break;
2257         case 1:
2258                 temp= 10*(temp/10);
2259                 break;
2260         case 2:
2261                 temp= 100*(temp/100);
2262                 break;
2263         }
2264
2265         return temp;
2266 }
2267
2268 static int ui_numedit_but_NUM(uiBut *but, uiHandleButtonData *data, float fac, int snap, int mx)
2269 {
2270         float deler, tempf, softmin, softmax, softrange;
2271         int lvalue, temp, changed= 0;
2272         
2273         if(mx == data->draglastx)
2274                 return changed;
2275         
2276         /* drag-lock - prevent unwanted scroll adjustments */
2277         /* change value (now 3) to adjust threshold in pixels */
2278         if(data->draglock) {
2279                 if(abs(mx-data->dragstartx) <= 3)
2280                         return changed;
2281
2282                 data->draglock= 0;
2283                 data->dragstartx= mx;  /* ignore mouse movement within drag-lock */
2284         }
2285
2286         softmin= but->softmin;
2287         softmax= but->softmax;
2288         softrange= softmax - softmin;
2289
2290         if(ui_is_a_warp_but(but)) {
2291                 /* Mouse location isn't screen clamped to the screen so use a linear mapping
2292                  * 2px == 1-int, or 1px == 1-ClickStep */
2293                 if(ui_is_but_float(but)) {
2294                         fac *= 0.01*but->a1;
2295                         tempf = data->startvalue + ((mx - data->dragstartx) * fac);
2296                         tempf= ui_numedit_apply_snapf(but, tempf, softmin, softmax, softrange, snap);
2297
2298 #if 1           /* fake moving the click start, nicer for dragging back after passing the limit */
2299                         if(tempf < softmin) {
2300                                 data->dragstartx -= (softmin-tempf) / fac;
2301                                 tempf= softmin;
2302                         } else if (tempf > softmax) {
2303                                 data->dragstartx += (tempf-softmax) / fac;
2304                                 tempf= softmax;
2305                         }
2306 #else
2307                         CLAMP(tempf, softmin, softmax);
2308 #endif
2309
2310                         if(tempf != data->value) {
2311                                 data->dragchange= 1;
2312                                 data->value= tempf;
2313                                 changed= 1;
2314                         }
2315                 }
2316                 else {
2317                         if(softrange > 256)             fac= 1.0;               /* 1px == 1 */
2318                         else if(softrange > 32) fac= 1.0/2.0;   /* 2px == 1 */
2319                         else                                    fac= 1.0/16.0;  /* 16px == 1? */
2320
2321                         temp= data->startvalue + ((mx - data->dragstartx) * fac);
2322                         temp= ui_numedit_apply_snap(temp, softmin, softmax, snap);
2323
2324 #if 1           /* fake moving the click start, nicer for dragging back after passing the limit */
2325                         if(temp < softmin) {
2326                                 data->dragstartx -= (softmin-temp) / fac;
2327                                 temp= softmin;
2328                         } else if (temp > softmax) {
2329                                 data->dragstartx += (temp-softmax) / fac;
2330                                 temp= softmax;
2331                         }
2332 #else
2333                         CLAMP(temp, softmin, softmax);
2334 #endif
2335
2336                         if(temp != data->value) {
2337                                 data->dragchange= 1;
2338                                 data->value= temp;
2339                                 changed= 1;
2340                         }
2341                 }
2342
2343                 data->draglastx= mx;
2344         }
2345         else {
2346                 /* Use a non-linear mapping of the mouse drag especially for large floats (normal behavior) */
2347                 deler= 500;
2348                 if(!ui_is_but_float(but)) {
2349                         /* prevent large ranges from getting too out of control */
2350                         if (softrange > 600) deler = powf(softrange, 0.75);
2351                         
2352                         if (softrange < 100) deler= 200.0;
2353                         if (softrange < 25) deler= 50.0;
2354                 }
2355                 deler /= fac;
2356
2357                 if(softrange > 11) {
2358                         /* non linear change in mouse input- good for high precicsion */
2359                         data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.002);
2360                 } else if (softrange > 129) { /* only scale large int buttons */
2361                         /* non linear change in mouse input- good for high precicsionm ints need less fine tuning */
2362                         data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.004);
2363                 } else {
2364                         /*no scaling */
2365                         data->dragf+= ((float)(mx-data->draglastx))/deler ;
2366                 }
2367         
2368                 CLAMP(data->dragf, 0.0, 1.0);
2369                 data->draglastx= mx;
2370                 tempf= (softmin + data->dragf*softrange);
2371
2372
2373                 if(!ui_is_but_float(but)) {
2374                         temp= floor(tempf+.5);
2375
2376                         temp= ui_numedit_apply_snap(temp, softmin, softmax, snap);
2377
2378                         CLAMP(temp, softmin, softmax);
2379                         lvalue= (int)data->value;
2380                         
2381                         if(temp != lvalue) {
2382                                 data->dragchange= 1;
2383                                 data->value= (double)temp;
2384                                 changed= 1;
2385                         }
2386                 }
2387                 else {
2388                         temp= 0;
2389                         tempf= ui_numedit_apply_snapf(but, tempf, softmin, softmax, softrange, snap);
2390
2391                         CLAMP(tempf, softmin, softmax);
2392
2393                         if(tempf != data->value) {
2394                                 data->dragchange= 1;
2395                                 data->value= tempf;
2396                                 changed= 1;
2397                         }
2398                 }
2399         }
2400
2401
2402         return changed;
2403 }
2404
2405 static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2406 {
2407         int mx, my;     /* mouse location scaled to fit the UI */
2408         int screen_mx, screen_my; /* mouse location kept at screen pixel coords */
2409         int click= 0;
2410         int retval= WM_UI_HANDLER_CONTINUE;
2411         
2412         mx= screen_mx= event->x;
2413         my= screen_my= event->y;
2414
2415         ui_window_to_block(data->region, block, &mx, &my);
2416
2417         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2418                 /* XXX hardcoded keymap check.... */
2419                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2420                         mx= but->x1;
2421                         click= 1;
2422                 }
2423                 else if(event->type == WHEELUPMOUSE && event->alt) {
2424                         mx= but->x2;
2425                         click= 1;
2426                 }
2427                 else if(event->val==KM_PRESS) {
2428                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2429                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2430                                 retval= WM_UI_HANDLER_BREAK;
2431                         }
2432                         else if(event->type == LEFTMOUSE) {
2433                                 data->dragstartx= data->draglastx= ui_is_a_warp_but(but) ? screen_mx:mx;
2434                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2435                                 retval= WM_UI_HANDLER_BREAK;
2436                         }
2437                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2438                                 click= 1;
2439                         else if (event->type == MINUSKEY && event->val==KM_PRESS) {
2440                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2441                                 data->value = -data->value;
2442                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2443                                 retval= WM_UI_HANDLER_BREAK;
2444                         }
2445                 }
2446                 
2447         }
2448         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2449                 if(event->type == ESCKEY) {
2450                         data->cancel= 1;
2451                         data->escapecancel= 1;
2452                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2453                 }
2454                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2455                         if(data->dragchange)
2456                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2457                         else
2458                                 click= 1;
2459                 }
2460                 else if(event->type == MOUSEMOVE) {
2461                         float fac;
2462                         int snap;
2463
2464                         fac= 1.0f;
2465                         if(event->shift) fac /= 10.0f;
2466                         if(event->alt) fac /= 20.0f;
2467                         
2468                         snap= (event->ctrl)? (event->shift)? 2: 1: 0;
2469
2470                         if(ui_numedit_but_NUM(but, data, fac, snap, (ui_is_a_warp_but(but) ? screen_mx:mx)))
2471                                 ui_numedit_apply(C, block, but, data);
2472                 }
2473                 retval= WM_UI_HANDLER_BREAK;
2474         }
2475         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2476                 ui_do_but_textedit(C, block, but, data, event);
2477                 retval= WM_UI_HANDLER_BREAK;
2478         }
2479         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2480                 ui_do_but_textedit_select(C, block, but, data, event);
2481                 retval= WM_UI_HANDLER_BREAK;
2482         }
2483         
2484         if(click) {
2485                 /* we can click on the side arrows to increment/decrement,
2486                  * or click inside to edit the value directly */
2487                 float tempf, softmin, softmax;
2488                 int temp;
2489
2490                 softmin= but->softmin;
2491                 softmax= but->softmax;
2492
2493                 if(!ui_is_but_float(but)) {
2494                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2495                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2496
2497                                 temp= (int)data->value - 1;
2498                                 if(temp>=softmin && temp<=softmax)
2499                                         data->value= (double)temp;
2500                                 else
2501                                         data->cancel= 1;
2502
2503                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2504                         }
2505                         else if(mx > (but->x1 + (2*(but->x2 - but->x1)/3) + 3)) {
2506                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2507
2508                                 temp= (int)data->value + 1;
2509                                 if(temp>=softmin && temp<=softmax)
2510                                         data->value= (double)temp;
2511                                 else
2512                                         data->cancel= 1;
2513
2514                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2515                         }
2516                         else
2517                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2518                 }
2519                 else {
2520                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2521                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2522
2523                                 tempf= data->value - 0.01*but->a1;
2524                                 if (tempf < softmin) tempf = softmin;
2525                                 data->value= tempf;
2526
2527                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2528                         }
2529                         else if(mx > but->x1 + (2*((but->x2 - but->x1)/3) + 3)) {
2530                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2531
2532                                 tempf= data->value + 0.01*but->a1;
2533                                 if (tempf > softmax) tempf = softmax;
2534                                 data->value= tempf;
2535
2536                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2537                         }
2538                         else
2539                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2540                 }
2541
2542                 retval= WM_UI_HANDLER_BREAK;
2543         }
2544         
2545         return retval;
2546 }
2547
2548 static int ui_numedit_but_SLI(uiBut *but, uiHandleButtonData *data, int shift, int ctrl, int mx)
2549 {
2550         float deler, f, tempf, softmin, softmax, softrange;
2551         int temp, lvalue, changed= 0;
2552
2553         softmin= but->softmin;
2554         softmax= but->softmax;
2555         softrange= softmax - softmin;
2556
2557         if(but->type==NUMSLI) deler= ((but->x2-but->x1) - 5.0*but->aspect);
2558         else if(but->type==HSVSLI) deler= ((but->x2-but->x1)/2 - 5.0*but->aspect);
2559         else if(but->type==SCROLL) {
2560                 int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2561                 float size= (horizontal)? (but->x2-but->x1): -(but->y2-but->y1);
2562                 deler= size*(but->softmax - but->softmin)/(but->softmax - but->softmin + but->a1);
2563         }
2564         else deler= (but->x2-but->x1- 5.0*but->aspect);
2565
2566         f= (float)(mx-data->dragstartx)/deler + data->dragfstart;
2567         
2568         if(shift)
2569                 f= (f-data->dragfstart)/10.0 + data->dragfstart;
2570
2571         CLAMP(f, 0.0, 1.0);
2572         tempf= softmin + f*softrange;
2573         temp= floor(tempf+.5);
2574
2575         if(ctrl) {
2576                 if(tempf==softmin || tempf==softmax);
2577                 else if(ui_is_but_float(but)) {
2578
2579                         if(shift) {
2580                                 if(tempf==softmin || tempf==softmax);
2581                                 else if(softmax-softmin < 2.10) tempf= 0.01*floor(100.0*tempf);
2582                                 else if(softmax-softmin < 21.0) tempf= 0.1*floor(10.0*tempf);
2583                                 else tempf= floor(tempf);
2584                         }
2585                         else {
2586                                 if(softmax-softmin < 2.10) tempf= 0.1*floor(10*tempf);
2587                                 else if(softmax-softmin < 21.0) tempf= floor(tempf);
2588                                 else tempf= 10.0*floor(tempf/10.0);
2589                         }
2590                 }
2591                 else {
2592                         temp= 10*(temp/10);
2593                         tempf= temp;
2594                 }
2595         }
2596
2597         if(!ui_is_but_float(but)) {
2598                 lvalue= floor(data->value+0.5);
2599
2600                 CLAMP(temp, softmin, softmax);
2601
2602                 if(temp != lvalue) {
2603                         data->value= temp;
2604                         data->dragchange= 1;
2605                         changed= 1;
2606                 }
2607         }
2608         else {
2609                 CLAMP(tempf, softmin, softmax);
2610
2611                 if(tempf != data->value) {
2612                         data->value= tempf;
2613                         data->dragchange= 1;
2614                         changed= 1;
2615                 }
2616         }
2617
2618         return changed;
2619 }
2620
2621 static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2622 {
2623         int mx, my, click= 0;
2624         int retval= WM_UI_HANDLER_CONTINUE;
2625
2626         mx= event->x;
2627         my= event->y;
2628         ui_window_to_block(data->region, block, &mx, &my);
2629
2630         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2631                 /* XXX hardcoded keymap check.... */
2632                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2633                         mx= but->x1;
2634                         click= 2;
2635                 }
2636                 else if(event->type == WHEELUPMOUSE && event->alt) {
2637                         mx= but->x2;
2638                         click= 2;
2639                 }
2640                 else if(event->val==KM_PRESS) {
2641                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2642                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2643                                 retval= WM_UI_HANDLER_BREAK;
2644                         }
2645                         /* alt-click on sides to get "arrows" like in NUM buttons, and match wheel usage above */
2646                         else if(event->type == LEFTMOUSE && event->alt) {
2647                                 int halfpos = (but->x1 + but->x2) / 2;
2648                                 click = 2;
2649                                 if (mx < halfpos)
2650                                         mx = but->x1;
2651                                 else
2652                                         mx = but->x2;
2653                         }
2654                         else if(event->type == LEFTMOUSE) {
2655                                 data->dragstartx= mx;
2656                                 data->draglastx= mx;
2657                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2658                                 retval= WM_UI_HANDLER_BREAK;
2659                         }
2660                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2661                                 click= 1;
2662                         else if (event->type == MINUSKEY && event->val==KM_PRESS) {
2663                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2664                                 data->value = -data->value;
2665                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2666                                 retval= WM_UI_HANDLER_BREAK;
2667                         }
2668                 }
2669         }
2670         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2671                 if(event->type == ESCKEY) {
2672                         data->cancel= 1;
2673                         data->escapecancel= 1;
2674                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2675                 }
2676                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2677                         if(data->dragchange)
2678                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2679                         else
2680                                 click= 1;
2681                 }
2682                 else if(event->type == MOUSEMOVE) {
2683                         if(ui_numedit_but_SLI(but, data, event->shift, event->ctrl, mx))
2684                                 ui_numedit_apply(C, block, but, data);
2685                 }
2686                 retval= WM_UI_HANDLER_BREAK;
2687         }
2688         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2689                 ui_do_but_textedit(C, block, but, data, event);
2690                 retval= WM_UI_HANDLER_BREAK;
2691         }
2692         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2693                 ui_do_but_textedit_select(C, block, but, data, event);
2694                 retval= WM_UI_HANDLER_BREAK;
2695         }
2696
2697         if(click) {
2698                 if (click==2) {
2699                         /* nudge slider to the left or right */
2700                         float f, tempf, softmin, softmax, softrange;
2701                         int temp;
2702                         
2703                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2704                         
2705                         softmin= but->softmin;
2706                         softmax= but->softmax;
2707                         softrange= softmax - softmin;
2708                         
2709                         tempf= data->value;
2710                         temp= (int)data->value;
2711                         
2712                         /* XXX useles "if", same result for f, uh??? */
2713                         if(but->type==SLI) f= (float)(mx-but->x1)/(but->x2-but->x1);
2714                         else f= (float)(mx- but->x1)/(but->x2-but->x1);
2715                         
2716                         f= softmin + f*softrange;
2717                         
2718                         if(!ui_is_but_float(but)) {
2719                                 if(f<temp) temp--;
2720                                 else temp++;
2721                                 
2722                                 if(temp>=softmin && temp<=softmax)
2723                                         data->value= temp;
2724                                 else
2725                                         data->cancel= 1;
2726                         } 
2727                         else {
2728                                 if(f<tempf) tempf-=.01;
2729                                 else tempf+=.01;
2730                                 
2731                                 if(tempf>=softmin && tempf<=softmax)
2732                                         data->value= tempf;
2733                                 else
2734                                         data->cancel= 1;
2735                         }
2736                         
2737                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2738                         retval= WM_UI_HANDLER_BREAK;
2739                 }
2740                 else {
2741                         /* edit the value directly */
2742                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2743                         retval= WM_UI_HANDLER_BREAK;
2744                 }
2745         }
2746         
2747         return retval;
2748 }
2749
2750 static int ui_do_but_SCROLL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2751 {
2752         int mx, my, click= 0;
2753         int retval= WM_UI_HANDLER_CONTINUE;
2754         int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2755         
2756         mx= event->x;
2757         my= event->y;
2758         ui_window_to_block(data->region, block, &mx, &my);
2759
2760         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2761                 if(event->val==KM_PRESS) {
2762                         if(event->type == LEFTMOUSE) {
2763                                 if(horizontal) {
2764                                         data->dragstartx= mx;
2765                                         data->draglastx= mx;
2766                                 }
2767                                 else {
2768                                         data->dragstartx= my;
2769                                         data->draglastx= my;
2770                                 }
2771                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2772                                 retval= WM_UI_HANDLER_BREAK;
2773                         }
2774                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2775                                 click= 1;
2776                 }
2777         }
2778         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2779                 if(event->type == ESCKEY) {
2780                         data->cancel= 1;
2781                         data->escapecancel= 1;
2782                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2783                 }
2784                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2785                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2786                 }
2787                 else if(event->type == MOUSEMOVE) {
2788                         if(ui_numedit_but_SLI(but, data, 0, 0, (horizontal)? mx: my))
2789                                 ui_numedit_apply(C, block, but, data);
2790                 }
2791
2792                 retval= WM_UI_HANDLER_BREAK;
2793         }
2794         
2795         return retval;
2796 }
2797
2798
2799 static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2800 {
2801         
2802         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2803                 
2804                 /* first handle click on icondrag type button */
2805                 if(event->type==LEFTMOUSE && but->dragpoin && event->val==KM_PRESS) {
2806                         if(ui_but_mouse_inside_icon(but, data->region, event)) {
2807                                 button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
2808                                 data->dragstartx= event->x;
2809                                 data->dragstarty= event->y;
2810                                 return WM_UI_HANDLER_BREAK;
2811                         }
2812                 }
2813                 
2814                 /* regular open menu */
2815                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2816                         button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
2817                         return WM_UI_HANDLER_BREAK;
2818                 }
2819                 else if(ELEM3(but->type, MENU, ICONROW, ICONTEXTROW)) {
2820                         
2821                         if(event->type == WHEELDOWNMOUSE && event->alt) {
2822                                 data->value= ui_step_name_menu(but, -1);
2823                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2824                                 ui_apply_button(C, but->block, but, data, 1);
2825                                 return WM_UI_HANDLER_BREAK;
2826                         }
2827                         else if(event->type == WHEELUPMOUSE && 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                 }
2834                 else if(but->type==COL) {
2835                         if( ELEM(event->type, WHEELDOWNMOUSE, WHEELUPMOUSE) && event->alt) {
2836                                 float *hsv= ui_block_hsv_get(but->block);
2837                                 float col[3];
2838                                 
2839                                 ui_get_but_vectorf(but, col);
2840                                 rgb_to_hsv_compat(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
2841
2842                                 if(event->type==WHEELDOWNMOUSE)
2843                                         hsv[2]= CLAMPIS(hsv[2]-0.05f, 0.0f, 1.0f);
2844                                 else
2845                                         hsv[2]= CLAMPIS(hsv[2]+0.05f, 0.0f, 1.0f);
2846                                 
2847                                 hsv_to_rgb(hsv[0], hsv[1], hsv[2], data->vec, data->vec+1, data->vec+2);
2848                                 ui_set_but_vectorf(but, data->vec);
2849                                 
2850                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2851                                 ui_apply_button(C, but->block, but, data, 1);
2852                                 return WM_UI_HANDLER_BREAK;
2853                         }
2854                 }
2855         }
2856         else if(data->state == BUTTON_STATE_WAIT_DRAG) {
2857                 
2858                 /* this function also ends state */
2859                 if(ui_but_start_drag(C, but, data, event)) {
2860                         return WM_UI_HANDLER_BREAK;
2861                 }
2862                 
2863                 /* outside icon quit, not needed if drag activated */
2864                 if(0==ui_but_mouse_inside_icon(but, data->region, event)) {
2865                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2866                         data->cancel= 1;
2867                         return WM_UI_HANDLER_BREAK;
2868                 }
2869                 
2870                 if(event->type==LEFTMOUSE && event->val==KM_RELEASE) {
2871                         button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
2872                         return WM_UI_HANDLER_BREAK;
2873                 }
2874
2875         }
2876
2877         return WM_UI_HANDLER_CONTINUE;
2878 }
2879
2880 static int ui_numedit_but_NORMAL(uiBut *but, uiHandleButtonData *data, int mx, int my)
2881 {
2882         float dx, dy, rad, radsq, mrad, *fp;
2883         int mdx, mdy, changed= 1;
2884         
2885         /* button is presumed square */
2886         /* if mouse moves outside of sphere, it does negative normal */
2887
2888         fp= data->origvec;
2889         rad= (but->x2 - but->x1);
2890         radsq= rad*rad;
2891         
2892         if(fp[2]>0.0f) {
2893                 mdx= (rad*fp[0]);
2894                 mdy= (rad*fp[1]);
2895         }
2896         else if(fp[2]> -1.0f) {
2897                 mrad= rad/sqrt(fp[0]*fp[0] + fp[1]*fp[1]);
2898