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