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