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