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