Merging r40848 through r40876 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 WITH_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                 cont = (bController *)ob->controllers.last;
806
807                 /* (4) link the sensor->controller->actuator */
808                 tmp_but = MEM_callocN(sizeof(uiBut), "uiBut");
809                 uiSetButLink(tmp_but, (void **)&cont, (void ***)&(cont->links), &(cont->totlinks), from->link->tocode, (int)to->hardmin);
810                 tmp_but->hardmin= from->link->tocode;
811                 tmp_but->poin= (char *)cont;
812
813                 tmp_but->type= INLINK;
814                 ui_add_link(C, from, tmp_but);
815
816                 tmp_but->type= LINK;
817                 ui_add_link(C, tmp_but, to);
818
819                 /* (5) garbage collection */
820                 MEM_freeN(tmp_but->link);
821                 MEM_freeN(tmp_but);
822         }
823 }
824
825 static void ui_add_link(bContext *C, uiBut *from, uiBut *to)
826 {
827         /* in 'from' we have to add a link to 'to' */
828         uiLink *link;
829         uiLinkLine *line;
830         void **oldppoin;
831         int a;
832         
833         if( (line= ui_is_a_link(from, to)) ) {
834                 line->flag |= UI_SELECT;
835                 ui_delete_active_linkline(from->block);
836                 return;
837         }
838
839         if (from->type==INLINK && to->type==INLINK) {
840                 return;
841         }
842         else if (from->type==LINK && to->type==INLINK) {
843                 if( from->link->tocode != (int)to->hardmin ) {
844                         ui_add_smart_controller(C, from, to);
845                         return;
846                 }
847         }
848         else if(from->type==INLINK && to->type==LINK) {
849                 if( to->link->tocode == (int)from->hardmin ) {
850                         return;
851                 }
852         }
853         
854         link= from->link;
855         
856         /* are there more pointers allowed? */
857         if(link->ppoin) {
858                 oldppoin= *(link->ppoin);
859                 
860                 (*(link->totlink))++;
861                 *(link->ppoin)= MEM_callocN( *(link->totlink)*sizeof(void *), "new link");
862                 
863                 for(a=0; a< (*(link->totlink))-1; a++) {
864                         (*(link->ppoin))[a]= oldppoin[a];
865                 }
866                 (*(link->ppoin))[a]= to->poin;
867                 
868                 if(oldppoin) MEM_freeN(oldppoin);
869         }
870         else {
871                 *(link->poin)= to->poin;
872         }
873         
874 }
875
876
877 static void ui_apply_but_LINK(bContext *C, uiBut *but, uiHandleButtonData *data)
878 {
879         ARegion *ar= CTX_wm_region(C);
880         uiBut *bt;
881         
882         for(bt= but->block->buttons.first; bt; bt= bt->next) {
883                 if( ui_mouse_inside_button(ar, bt, but->linkto[0]+ar->winrct.xmin, but->linkto[1]+ar->winrct.ymin) )
884                         break;
885         }
886         if(bt && bt!=but) {
887                 if (!ELEM(bt->type, LINK, INLINK) || !ELEM(but->type, LINK, INLINK))
888                         return;
889                 
890                 if(but->type==LINK) ui_add_link(C, but, bt);
891                 else ui_add_link(C, bt, but);
892
893                 ui_apply_but_func(C, but);
894                 data->retval= but->retval;
895         }
896         data->applied= 1;
897 }
898
899 static void ui_apply_but_IMAGE(bContext *C, uiBut *but, uiHandleButtonData *data)
900 {
901         ui_apply_but_func(C, but);
902         data->retval= but->retval;
903         data->applied= 1;
904 }
905
906 static void ui_apply_but_HISTOGRAM(bContext *C, uiBut *but, uiHandleButtonData *data)
907 {
908         ui_apply_but_func(C, but);
909         data->retval= but->retval;
910         data->applied= 1;
911 }
912
913 static void ui_apply_but_WAVEFORM(bContext *C, uiBut *but, uiHandleButtonData *data)
914 {
915         ui_apply_but_func(C, but);
916         data->retval= but->retval;
917         data->applied= 1;
918 }
919
920 static void ui_apply_but_TRACKPREVIEW(bContext *C, uiBut *but, uiHandleButtonData *data)
921 {
922         ui_apply_but_func(C, but);
923         data->retval= but->retval;
924         data->applied= 1;
925 }
926
927
928 static void ui_apply_button(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, int interactive)
929 {
930         char *editstr;
931         double *editval;
932         float *editvec;
933         ColorBand *editcoba;
934         CurveMapping *editcumap;
935
936         data->retval= 0;
937
938         /* if we cancel and have not applied yet, there is nothing to do,
939          * otherwise we have to restore the original value again */
940         if(data->cancel) {
941                 if(!data->applied)
942                         return;
943
944                 if(data->str) MEM_freeN(data->str);
945                 data->str= data->origstr;
946                 data->origstr= NULL;
947                 data->value= data->origvalue;
948                 data->origvalue= 0.0;
949                 VECCOPY(data->vec, data->origvec);
950                 data->origvec[0]= data->origvec[1]= data->origvec[2]= 0.0f;
951         }
952         else {
953                 /* we avoid applying interactive edits a second time
954                  * at the end with the appliedinteractive flag */
955                 if(interactive)
956                         data->appliedinteractive= 1;
957                 else if(data->appliedinteractive)
958                         return;
959         }
960
961         /* ensures we are writing actual values */
962         editstr= but->editstr;
963         editval= but->editval;
964         editvec= but->editvec;
965         editcoba= but->editcoba;
966         editcumap= but->editcumap;
967         but->editstr= NULL;
968         but->editval= NULL;
969         but->editvec= NULL;
970         but->editcoba= NULL;
971         but->editcumap= NULL;
972
973         /* handle different types */
974         switch(but->type) {
975                 case BUT:
976                         ui_apply_but_BUT(C, but, data);
977                         break;
978                 case TEX:
979                 case SEARCH_MENU:
980                         ui_apply_but_TEX(C, but, data);
981                         break;
982                 case TOGBUT: 
983                 case TOG: 
984                 case TOGR: 
985                 case ICONTOG:
986                 case ICONTOGN:
987                 case TOGN:
988                 case BUT_TOGDUAL:
989                 case OPTION:
990                 case OPTIONN:
991                         ui_apply_but_TOG(C, but, data);
992                         break;
993                 case ROW:
994                 case LISTROW:
995                         ui_apply_but_ROW(C, block, but, data);
996                         break;
997                 case SCROLL:
998                 case NUM:
999                 case NUMABS:
1000                 case SLI:
1001                 case NUMSLI:
1002                         ui_apply_but_NUM(C, but, data);
1003                         break;
1004                 case HSVSLI:
1005                         break;
1006                 case TOG3:      
1007                         ui_apply_but_TOG3(C, but, data);
1008                         break;
1009                 case MENU:
1010                 case ICONROW:
1011                 case ICONTEXTROW:
1012                 case BLOCK:
1013                 case PULLDOWN:
1014                 case COL:
1015                         ui_apply_but_BLOCK(C, but, data);
1016                         break;
1017                 case BUTM:
1018                         ui_apply_but_BUTM(C, but, data);
1019                         break;
1020                 case BUT_NORMAL:
1021                 case HSVCUBE:
1022                 case HSVCIRCLE:
1023                         ui_apply_but_VEC(C, but, data);
1024                         break;
1025                 case BUT_COLORBAND:
1026                         ui_apply_but_COLORBAND(C, but, data);
1027                         break;
1028                 case BUT_CURVE:
1029                         ui_apply_but_CURVE(C, but, data);
1030                         break;
1031                 case IDPOIN:
1032                         ui_apply_but_IDPOIN(C, but, data);
1033                         break;
1034 #ifdef WITH_INTERNATIONAL
1035                 case CHARTAB:
1036                         ui_apply_but_CHARTAB(C, but, data);
1037                         break;
1038 #endif
1039                 case KEYEVT:
1040                 case HOTKEYEVT:
1041                         ui_apply_but_BUT(C, but, data);
1042                         break;
1043                 case LINK:
1044                 case INLINK:
1045                         ui_apply_but_LINK(C, but, data);
1046                         break;
1047                 case BUT_IMAGE: 
1048                         ui_apply_but_IMAGE(C, but, data);
1049                         break;
1050                 case HISTOGRAM: 
1051                         ui_apply_but_HISTOGRAM(C, but, data);
1052                         break;
1053                 case WAVEFORM:
1054                         ui_apply_but_WAVEFORM(C, but, data);
1055                         break;
1056                 case TRACKPREVIEW:
1057                         ui_apply_but_TRACKPREVIEW(C, but, data);
1058                         break;
1059                 default:
1060                         break;
1061         }
1062
1063         but->editstr= editstr;
1064         but->editval= editval;
1065         but->editvec= editvec;
1066         but->editcoba= editcoba;
1067         but->editcumap= editcumap;
1068 }
1069
1070 /* ******************* drop event ********************  */
1071
1072 /* only call if event type is EVT_DROP */
1073 static void ui_but_drop(bContext *C, wmEvent *event, uiBut *but, uiHandleButtonData *data)
1074 {
1075         wmDrag *wmd;
1076         ListBase *drags= event->customdata; /* drop event type has listbase customdata by default */
1077         
1078         for(wmd= drags->first; wmd; wmd= wmd->next) {
1079                 if(wmd->type==WM_DRAG_ID) {
1080                         /* align these types with UI_but_active_drop_name */
1081                         if(ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1082                                 ID *id= (ID *)wmd->poin;
1083                                 
1084                                 if(but->poin==NULL && but->rnapoin.data==NULL) {}
1085                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1086                                 BLI_strncpy(data->str, id->name+2, data->maxlen);
1087                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1088                         }
1089                 }
1090         }
1091         
1092 }
1093
1094 /* ******************* copy and paste ********************  */
1095
1096 /* c = copy, v = paste */
1097 static void ui_but_copy_paste(bContext *C, uiBut *but, uiHandleButtonData *data, char mode)
1098 {
1099         static ColorBand but_copypaste_coba = {0};
1100         char buf[UI_MAX_DRAW_STR+1]= {0};
1101         double val;
1102         
1103         if(mode=='v' && but->lock)
1104                 return;
1105
1106         if(mode=='v') {
1107                 /* extract first line from clipboard in case of multi-line copies */
1108                 char *p, *pbuf= WM_clipboard_text_get(0);
1109                 p= pbuf;
1110                 if(p) {
1111                         int i = 0;
1112                         while (*p && *p!='\r' && *p!='\n' && i<UI_MAX_DRAW_STR) {
1113                                 buf[i++]=*p;
1114                                 p++;
1115                         }
1116                         buf[i]= 0;
1117                         MEM_freeN(pbuf);
1118                 }
1119         }
1120         
1121         /* numeric value */
1122         if ELEM4(but->type, NUM, NUMABS, NUMSLI, HSVSLI) {
1123                 
1124                 if(but->poin==NULL && but->rnapoin.data==NULL);
1125                 else if(mode=='c') {
1126                         if(ui_is_but_float(but))
1127                                 BLI_snprintf(buf, sizeof(buf), "%f", ui_get_but_val(but));
1128                         else
1129                                 BLI_snprintf(buf, sizeof(buf), "%d", (int)ui_get_but_val(but));
1130
1131                         WM_clipboard_text_set(buf, 0);
1132                 }
1133                 else {
1134                         if (sscanf(buf, " %lf ", &val) == 1) {
1135                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1136                                 data->value= val;
1137                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1138                         }
1139                 }
1140         }
1141
1142         /* RGB triple */
1143         else if(but->type==COL) {
1144                 float rgb[3];
1145                 
1146                 if(but->poin==NULL && but->rnapoin.data==NULL);
1147                 else if(mode=='c') {
1148
1149                         ui_get_but_vectorf(but, rgb);
1150                         BLI_snprintf(buf, sizeof(buf), "[%f, %f, %f]", rgb[0], rgb[1], rgb[2]);
1151                         WM_clipboard_text_set(buf, 0);
1152                         
1153                 }
1154                 else {
1155                         if (sscanf(buf, "[%f, %f, %f]", &rgb[0], &rgb[1], &rgb[2]) == 3) {
1156                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1157                                 ui_set_but_vectorf(but, rgb);
1158                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1159                         }
1160                 }
1161         }
1162
1163         /* text/string and ID data */
1164         else if(ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1165                 uiHandleButtonData *active_data= but->active;
1166
1167                 if(but->poin==NULL && but->rnapoin.data==NULL);
1168                 else if(mode=='c') {
1169                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1170                         BLI_strncpy(buf, active_data->str, UI_MAX_DRAW_STR);
1171                         WM_clipboard_text_set(active_data->str, 0);
1172                         active_data->cancel= 1;
1173                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1174                 }
1175                 else {
1176                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1177                         BLI_strncpy(active_data->str, buf, active_data->maxlen);
1178                         if(but->type == SEARCH_MENU) {
1179                                 /* else uiSearchboxData.active member is not updated [#26856] */
1180                                 ui_searchbox_update(C, data->searchbox, but, 1);
1181                         }
1182                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1183                 }
1184         }
1185         /* colorband (not supported by system clipboard) */
1186         else if(but->type==BUT_COLORBAND) {
1187                 if(mode=='c') {
1188                         if(but->poin==NULL)
1189                                 return;
1190
1191                         memcpy(&but_copypaste_coba, but->poin, sizeof(ColorBand));
1192                 }
1193                 else {
1194                         if(but_copypaste_coba.tot==0)
1195                                 return;
1196
1197                         if(!but->poin)
1198                                 but->poin= MEM_callocN(sizeof(ColorBand), "colorband");
1199
1200                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1201                         memcpy(data->coba, &but_copypaste_coba, sizeof(ColorBand) );
1202                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1203                 }
1204         }
1205         /* operator button (any type) */
1206         else if (but->optype) {
1207                 if(mode=='c') {
1208                         PointerRNA *opptr;
1209                         char *str;
1210                         opptr= uiButGetOperatorPtrRNA(but); /* allocated when needed, the button owns it */
1211
1212                         str= WM_operator_pystring(C, but->optype, opptr, 0);
1213
1214                         WM_clipboard_text_set(str, 0);
1215
1216                         MEM_freeN(str);
1217                 }
1218         }
1219 }
1220
1221 /* ************* in-button text selection/editing ************* */
1222
1223 /* return 1 if char ch is special character, otherwise return 0 */
1224 static short test_special_char(char ch)
1225 {
1226         switch(ch) {
1227                 case '\\':
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 '\"': // " - an extra closing one for Aligorith's text editor
1250                 case '<':
1251                 case '>':
1252                 case ',':
1253                 case '.':
1254                 case '?':
1255                 case '_':
1256                 case '-':
1257                 case ' ':
1258                         return 1;
1259                         break;
1260                 default:
1261                         break;
1262         }
1263         return 0;
1264 }
1265
1266 static int ui_textedit_delete_selection(uiBut *but, uiHandleButtonData *data)
1267 {
1268         char *str= data->str;
1269         int len= strlen(str);
1270         int change= 0;
1271         if(but->selsta != but->selend && len) {
1272                 memmove( str+but->selsta, str+but->selend, (len - but->selend) + 1 );
1273                 change= 1;
1274         }
1275         
1276         but->pos = but->selend = but->selsta;
1277         return change;
1278 }
1279
1280 /* note, but->block->aspect is used here, when drawing button style is getting scaled too */
1281 static void ui_textedit_set_cursor_pos(uiBut *but, uiHandleButtonData *data, short x)
1282 {
1283         uiStyle *style= UI_GetStyle();  // XXX pass on as arg
1284         uiFontStyle *fstyle = &style->widget;
1285         int startx= but->x1;
1286         char *origstr;
1287
1288         uiStyleFontSet(fstyle);
1289
1290         if (fstyle->kerning==1) /* for BLF_width */
1291                 BLF_enable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
1292         
1293         origstr= MEM_callocN(sizeof(char)*data->maxlen, "ui_textedit origstr");
1294         
1295         BLI_strncpy(origstr, but->drawstr, data->maxlen);
1296         
1297         /* XXX solve generic */
1298         if(but->type==NUM || but->type==NUMSLI)
1299                 startx += (int)(0.5f*(but->y2 - but->y1));
1300         else if(ELEM(but->type, TEX, SEARCH_MENU)) {
1301                 startx += 5;
1302                 if (but->flag & UI_HAS_ICON)
1303                         startx += UI_DPI_ICON_SIZE;
1304         }
1305         
1306         /* mouse dragged outside the widget to the left */
1307         if (x < startx && but->ofs > 0) {       
1308                 int i= but->ofs;
1309
1310                 origstr[but->ofs] = 0;
1311                 
1312                 while (i > 0) {
1313                         i--;
1314                         if (BLF_width(fstyle->uifont_id, origstr+i) > (startx - x)*0.25f) break;        // 0.25 == scale factor for less sensitivity
1315                 }
1316                 but->ofs = i;
1317                 but->pos = but->ofs;
1318         }
1319         /* mouse inside the widget */
1320         else if (x >= startx) {
1321                 const float aspect_sqrt= sqrtf(but->block->aspect);
1322                 
1323                 but->pos= strlen(origstr)-but->ofs;
1324                 
1325                 /* XXX does not take zoom level into account */
1326                 while (startx + aspect_sqrt * BLF_width(fstyle->uifont_id, origstr+but->ofs) > x) {
1327                         if (but->pos <= 0) break;
1328                         but->pos--;
1329                         origstr[but->pos+but->ofs] = 0;
1330                 }               
1331                 but->pos += but->ofs;
1332                 if(but->pos<0) but->pos= 0;
1333         }
1334         
1335         if (fstyle->kerning == 1)
1336                 BLF_disable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
1337         
1338         MEM_freeN(origstr);
1339 }
1340
1341 static void ui_textedit_set_cursor_select(uiBut *but, uiHandleButtonData *data, short x)
1342 {
1343         if (x > data->selstartx) data->selextend = EXTEND_RIGHT;
1344         else if (x < data->selstartx) data->selextend = EXTEND_LEFT;
1345
1346         ui_textedit_set_cursor_pos(but, data, x);
1347                                                 
1348         if (data->selextend == EXTEND_RIGHT) but->selend = but->pos;
1349         if (data->selextend == EXTEND_LEFT) but->selsta = but->pos;
1350
1351         ui_check_but(but);
1352 }
1353
1354 static int ui_textedit_type_ascii(uiBut *but, uiHandleButtonData *data, char ascii)
1355 {
1356         char *str;
1357         int len, x, changed= 0;
1358
1359         str= data->str;
1360         len= strlen(str);
1361
1362         if(len-(but->selend - but->selsta)+1 <= data->maxlen) {
1363                 /* type over the current selection */
1364                 if ((but->selend - but->selsta) > 0)
1365                         changed= ui_textedit_delete_selection(but, data);
1366
1367                 len= strlen(str);
1368                 if(len+1 < data->maxlen) {
1369                         for(x= data->maxlen; x>but->pos; x--)
1370                                 str[x]= str[x-1];
1371                         str[but->pos]= ascii;
1372                         str[len+1]= '\0';
1373
1374                         but->pos++; 
1375                         changed= 1;
1376                 }
1377         }
1378
1379         return changed;
1380 }
1381
1382 static void ui_textedit_move(uiBut *but, uiHandleButtonData *data, int direction, int select, int jump, int jump_all)
1383 {
1384         const char *str= data->str;
1385         const int len= strlen(str);
1386         const int pos_prev= but->pos;
1387         const int has_sel= (but->selend - but->selsta) > 0;
1388
1389         /* special case, quit selection and set cursor */
1390         if (has_sel && !select) {
1391                 if (jump_all) {
1392                         but->selsta = but->selend= but->pos = direction ? len : 0;
1393                 }
1394                 else {
1395                         if (direction) {
1396                                 but->selsta = but->pos = but->selend;
1397                         }
1398                         else {
1399                                 but->pos = but->selend = but->selsta;
1400                         }
1401                 }
1402                 data->selextend = 0;
1403         }
1404         else {
1405                 if(direction) { /* right*/
1406                         if(jump) {
1407                                 /* jump between special characters (/,\,_,-, etc.),
1408                                  * look at function test_special_char() for complete
1409                                  * list of special character, ctr -> */
1410                                 while(but->pos < len) {
1411                                         but->pos++;
1412                                         if(!jump_all && test_special_char(str[but->pos])) break;
1413                                 }
1414                         }
1415                         else {
1416                                 but->pos++;
1417                                 if(but->pos > len) but->pos= len;
1418                         }
1419                 }
1420                 else { /* left */
1421                         if(jump) {
1422
1423                                 /* left only: compensate for index/change in direction */
1424                                 if(but->pos > 0) {
1425                                         but->pos--;
1426                                 }
1427
1428                                 /* jump between special characters (/,\,_,-, etc.),
1429                                  * look at function test_special_char() for complete
1430                                  * list of special character, ctr -> */
1431                                 while(but->pos > 0){
1432                                         but->pos--;
1433                                         if(!jump_all && test_special_char(str[but->pos])) break;
1434                                 }
1435
1436                                 /* left only: compensate for index/change in direction */
1437                                 if((but->pos != 0) && ABS(pos_prev - but->pos) > 1) {
1438                                         but->pos++;
1439                                 }
1440
1441                         }
1442                         else {
1443                                 if(but->pos>0) but->pos--;
1444                         }
1445                 }
1446
1447
1448                 if(select) {
1449                         /* existing selection */
1450                         if (has_sel) {
1451
1452                                 if(data->selextend == 0) {
1453                                         data->selextend= EXTEND_RIGHT;
1454                                 }
1455
1456                                 if (direction) {
1457                                         if (data->selextend == EXTEND_RIGHT) {
1458                                                 but->selend= but->pos;
1459                                         }
1460                                         else {
1461                                                 but->selsta= but->pos;
1462                                         }
1463                                 }
1464                                 else {
1465                                         if (data->selextend == EXTEND_LEFT) {
1466                                                 but->selsta= but->pos;
1467                                         }
1468                                         else {
1469                                                 but->selend= but->pos;
1470                                         }
1471                                 }
1472
1473                                 if (but->selend < but->selsta) {
1474                                         SWAP(short, but->selsta, but->selend);
1475                                         data->selextend= (data->selextend == EXTEND_RIGHT) ? EXTEND_LEFT : EXTEND_RIGHT;
1476                                 }
1477
1478                         } /* new selection */
1479                         else {
1480                                 if (direction) {
1481                                         data->selextend= EXTEND_RIGHT;
1482                                         but->selend= but->pos;
1483                                         but->selsta= pos_prev;
1484                                 }
1485                                 else {
1486                                         data->selextend= EXTEND_LEFT;
1487                                         but->selend= pos_prev;
1488                                         but->selsta= but->pos;
1489                                 }
1490                         }
1491                 }
1492         }
1493 }
1494
1495 static int ui_textedit_delete(uiBut *but, uiHandleButtonData *data, int direction, int all)
1496 {
1497         char *str;
1498         int len, x, changed= 0;
1499
1500         str= data->str;
1501         len= strlen(str);
1502
1503         if(all) {
1504                 if(len) changed=1;
1505                 str[0]= 0;
1506                 but->pos= 0;
1507         }
1508         else if(direction) { /* delete */
1509                 if ((but->selend - but->selsta) > 0) {
1510                         changed= ui_textedit_delete_selection(but, data);
1511                 }
1512                 else if(but->pos>=0 && but->pos<len) {
1513                         for(x=but->pos; x<len; x++)
1514                                 str[x]= str[x+1];
1515                         str[len-1]='\0';
1516                         changed= 1;
1517                 }
1518         }
1519         else { /* backspace */
1520                 if(len!=0) {
1521                         if ((but->selend - but->selsta) > 0) {
1522                                 changed= ui_textedit_delete_selection(but, data);
1523                         }
1524                         else if(but->pos>0) {
1525                                 for(x=but->pos; x<len; x++)
1526                                         str[x-1]= str[x];
1527                                 str[len-1]='\0';
1528
1529                                 but->pos--;
1530                                 changed= 1;
1531                         }
1532                 } 
1533         }
1534
1535         return changed;
1536 }
1537
1538 static int ui_textedit_autocomplete(bContext *C, uiBut *but, uiHandleButtonData *data)
1539 {
1540         char *str;
1541         int changed= 1;
1542
1543         str= data->str;
1544
1545         if(data->searchbox)
1546                 ui_searchbox_autocomplete(C, data->searchbox, but, data->str);
1547         else
1548                 but->autocomplete_func(C, str, but->autofunc_arg);
1549
1550         but->pos= strlen(str);
1551         but->selsta= but->selend= but->pos;
1552
1553         return changed;
1554 }
1555
1556 static int ui_textedit_copypaste(uiBut *but, uiHandleButtonData *data, int paste, int copy, int cut)
1557 {
1558         char buf[UI_MAX_DRAW_STR]={0};
1559         char *str, *p, *pbuf;
1560         int len, x, i, changed= 0;
1561
1562         str= data->str;
1563         len= strlen(str);
1564         
1565         /* paste */
1566         if (paste) {
1567                 /* extract the first line from the clipboard */
1568                 p = pbuf= WM_clipboard_text_get(0);
1569
1570                 if(p && p[0]) {
1571                         unsigned int y;
1572                         i= 0;
1573                         while (*p && *p!='\r' && *p!='\n' && i<UI_MAX_DRAW_STR-1) {
1574                                 buf[i++]=*p;
1575                                 p++;
1576                         }
1577                         buf[i]= 0;
1578
1579                         /* paste over the current selection */
1580                         if ((but->selend - but->selsta) > 0) {
1581                                 ui_textedit_delete_selection(but, data);
1582                                 len= strlen(str);
1583                         }
1584                         
1585                         for (y=0; y<strlen(buf); y++)
1586                         {
1587                                 /* add contents of buffer */
1588                                 if(len+1 < data->maxlen) {
1589                                         for(x= data->maxlen; x>but->pos; x--)
1590                                                 str[x]= str[x-1];
1591                                         str[but->pos]= buf[y];
1592                                         but->pos++; 
1593                                         len++;
1594                                         str[len]= '\0';
1595                                 }
1596                         }
1597
1598                         changed= 1;
1599                 }
1600
1601                 if(pbuf)
1602                         MEM_freeN(pbuf);
1603         }
1604         /* cut & copy */
1605         else if (copy || cut) {
1606                 /* copy the contents to the copypaste buffer */
1607                 for(x= but->selsta; x <= but->selend; x++) {
1608                         if (x==but->selend)
1609                                 buf[x] = '\0';
1610                         else
1611                                 buf[(x - but->selsta)] = str[x];
1612                 }
1613
1614                 WM_clipboard_text_set(buf, 0);
1615                 
1616                 /* for cut only, delete the selection afterwards */
1617                 if(cut)
1618                         if((but->selend - but->selsta) > 0)
1619                                 changed= ui_textedit_delete_selection(but, data);
1620         } 
1621
1622         return changed;
1623 }
1624
1625 static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
1626 {
1627         if(data->str) {
1628                 MEM_freeN(data->str);
1629                 data->str= NULL;
1630         }
1631
1632         /* retrieve string */
1633         data->maxlen= ui_get_but_string_max_length(but);
1634         data->str= MEM_callocN(sizeof(char)*data->maxlen + 1, "textedit str");
1635         ui_get_but_string(but, data->str, data->maxlen);
1636
1637         if(ELEM3(but->type, NUM, NUMABS, NUMSLI)) {
1638                 /* XXX: we dont have utf editing yet so for numbers its best to strip out utf chars 
1639                  * this is so the deg' synbol isnt included in number editing fields: bug 22274 */
1640                 int i;
1641                 for(i=0; data->str[i]; i++) {
1642                         if(!isascii(data->str[i])) {
1643                                 /* no stripping actually: just convert to alt name */
1644                                 ui_convert_to_unit_alt_name(but, data->str, data->maxlen);
1645                                 break;
1646                         }
1647                 }
1648         }
1649         
1650         
1651         data->origstr= BLI_strdup(data->str);
1652         data->selextend= 0;
1653         data->selstartx= 0;
1654
1655         /* set cursor pos to the end of the text */
1656         but->editstr= data->str;
1657         but->pos= strlen(data->str);
1658         but->selsta= 0;
1659         but->selend= strlen(data->str);
1660
1661         /* optional searchbox */
1662         if(but->type==SEARCH_MENU) {
1663                 data->searchbox= ui_searchbox_create(C, data->region, but);
1664                 ui_searchbox_update(C, data->searchbox, but, 1); /* 1= reset */
1665         }
1666         
1667         ui_check_but(but);
1668         
1669         WM_cursor_modal(CTX_wm_window(C), BC_TEXTEDITCURSOR);
1670 }
1671
1672 static void ui_textedit_end(bContext *C, uiBut *but, uiHandleButtonData *data)
1673 {
1674         if(but) {
1675                 if(ui_is_utf8_but(but)) {
1676                         int strip= BLI_utf8_invalid_strip(but->editstr, strlen(but->editstr));
1677                         /* not a file?, strip non utf-8 chars */
1678                         if(strip) {
1679                                 /* wont happen often so isnt that annoying to keep it here for a while */
1680                                 printf("%s: invalid utf8 - stripped chars %d\n", __func__, strip);
1681                         }
1682                 }
1683                 
1684                 if(data->searchbox) {
1685                         if(data->cancel==0)
1686                                 ui_searchbox_apply(but, data->searchbox);
1687
1688                         ui_searchbox_free(C, data->searchbox);
1689                         data->searchbox= NULL;
1690                 }
1691                 
1692                 but->editstr= NULL;
1693                 but->pos= -1;
1694         }
1695         
1696         WM_cursor_restore(CTX_wm_window(C));
1697 }
1698
1699 static void ui_textedit_next_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1700 {
1701         uiBut *but;
1702
1703         /* label and roundbox can overlap real buttons (backdrops...) */
1704         if(ELEM4(actbut->type, LABEL, SEPR, ROUNDBOX, LISTBOX))
1705                 return;
1706
1707         for(but= actbut->next; but; but= but->next) {
1708                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1709                         if(!(but->flag & UI_BUT_DISABLED)) {
1710                                 data->postbut= but;
1711                                 data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1712                                 return;
1713                         }
1714                 }
1715         }
1716         for(but= block->buttons.first; but!=actbut; but= but->next) {
1717                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1718                         if(!(but->flag & UI_BUT_DISABLED)) {
1719                                 data->postbut= but;
1720                                 data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1721                                 return;
1722                         }
1723                 }
1724         }
1725 }
1726
1727 static void ui_textedit_prev_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1728 {
1729         uiBut *but;
1730
1731         /* label and roundbox can overlap real buttons (backdrops...) */
1732         if(ELEM4(actbut->type, LABEL, SEPR, ROUNDBOX, LISTBOX))
1733                 return;
1734
1735         for(but= actbut->prev; but; but= but->prev) {
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         for(but= block->buttons.last; but!=actbut; but= but->prev) {
1745                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1746                         if(!(but->flag & UI_BUT_DISABLED)) {
1747                                 data->postbut= but;
1748                                 data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1749                                 return;
1750                         }
1751                 }
1752         }
1753 }
1754
1755
1756 static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1757 {
1758         int mx, my, changed= 0, inbox=0, update= 0, retval= WM_UI_HANDLER_CONTINUE;
1759
1760         switch(event->type) {
1761                 case WHEELUPMOUSE:
1762                 case WHEELDOWNMOUSE:
1763                 case MOUSEMOVE:
1764                         if(data->searchbox)
1765                                 ui_searchbox_event(C, data->searchbox, but, event);
1766                         
1767                         break;
1768                 case RIGHTMOUSE:
1769                 case ESCKEY:
1770                         data->cancel= 1;
1771                         data->escapecancel= 1;
1772                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1773                         retval= WM_UI_HANDLER_BREAK;
1774                         break;
1775                 case LEFTMOUSE: {
1776                         
1777                         /* exit on LMB only on RELEASE for searchbox, to mimic other popups, and allow multiple menu levels */
1778                         if(data->searchbox)
1779                                 inbox= ui_searchbox_inside(data->searchbox, event->x, event->y);
1780
1781                         if(event->val==KM_PRESS) {
1782                                 mx= event->x;
1783                                 my= event->y;
1784                                 ui_window_to_block(data->region, block, &mx, &my);
1785
1786                                 if (ui_but_contains_pt(but, mx, my)) {
1787                                         ui_textedit_set_cursor_pos(but, data, mx);
1788                                         but->selsta = but->selend = but->pos;
1789                                         data->selstartx= mx;
1790
1791                                         button_activate_state(C, but, BUTTON_STATE_TEXT_SELECTING);
1792                                         retval= WM_UI_HANDLER_BREAK;
1793                                 }
1794                                 else if(inbox==0) {
1795                                         /* if searchbox, click outside will cancel */
1796                                         if(data->searchbox)
1797                                                 data->cancel= data->escapecancel= 1;
1798                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1799                                         retval= WM_UI_HANDLER_BREAK;
1800                                 }
1801                         }
1802                         else if(inbox) {
1803                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1804                                 retval= WM_UI_HANDLER_BREAK;
1805                         }
1806                         break;
1807                 }
1808         }
1809
1810         if(event->val==KM_PRESS) {
1811                 switch (event->type) {
1812                         case VKEY:
1813                         case XKEY:
1814                         case CKEY:
1815                                 if(event->ctrl || event->oskey) {
1816                                         if(event->type == VKEY)
1817                                                 changed= ui_textedit_copypaste(but, data, 1, 0, 0);
1818                                         else if(event->type == CKEY)
1819                                                 changed= ui_textedit_copypaste(but, data, 0, 1, 0);
1820                                         else if(event->type == XKEY)
1821                                                 changed= ui_textedit_copypaste(but, data, 0, 0, 1);
1822
1823                                         retval= WM_UI_HANDLER_BREAK;
1824                                 }
1825                                 break;
1826                         case RIGHTARROWKEY:
1827                                 ui_textedit_move(but, data, 1, event->shift, event->ctrl, FALSE);
1828                                 retval= WM_UI_HANDLER_BREAK;
1829                                 break;
1830                         case LEFTARROWKEY:
1831                                 ui_textedit_move(but, data, 0, event->shift, event->ctrl, FALSE);
1832                                 retval= WM_UI_HANDLER_BREAK;
1833                                 break;
1834                         case DOWNARROWKEY:
1835                                 if(data->searchbox) {
1836                                         ui_searchbox_event(C, data->searchbox, but, event);
1837                                         break;
1838                                 }
1839                                 /* pass on purposedly */
1840                         case ENDKEY:
1841                                 ui_textedit_move(but, data, 1, event->shift, TRUE, TRUE);
1842                                 retval= WM_UI_HANDLER_BREAK;
1843                                 break;
1844                         case UPARROWKEY:
1845                                 if(data->searchbox) {
1846                                         ui_searchbox_event(C, data->searchbox, but, event);
1847                                         break;
1848                                 }
1849                                 /* pass on purposedly */
1850                         case HOMEKEY:
1851                                 ui_textedit_move(but, data, 0, event->shift, TRUE, TRUE);
1852                                 retval= WM_UI_HANDLER_BREAK;
1853                                 break;
1854                         case PADENTER:
1855                         case RETKEY:
1856                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1857                                 retval= WM_UI_HANDLER_BREAK;
1858                                 break;
1859                         case DELKEY:
1860                                 changed= ui_textedit_delete(but, data, 1, 0);
1861                                 retval= WM_UI_HANDLER_BREAK;
1862                                 break;
1863
1864                         case BACKSPACEKEY:
1865                                 changed= ui_textedit_delete(but, data, 0, event->shift);
1866                                 retval= WM_UI_HANDLER_BREAK;
1867                                 break;
1868                                 
1869                         case TABKEY:
1870                                 /* there is a key conflict here, we can't tab with autocomplete */
1871                                 if(but->autocomplete_func || data->searchbox) {
1872                                         changed= ui_textedit_autocomplete(C, but, data);
1873                                         update= 1; /* do live update for tab key */
1874                                 }
1875                                 /* the hotkey here is not well defined, was G.qual so we check all */
1876                                 else if(event->shift || event->ctrl || event->alt || event->oskey) {
1877                                         ui_textedit_prev_but(block, but, data);
1878                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1879                                 }
1880                                 else {
1881                                         ui_textedit_next_but(block, but, data);
1882                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1883                                 }
1884                                 retval= WM_UI_HANDLER_BREAK;
1885                                 break;
1886                 }
1887
1888                 if(event->ascii && (retval == WM_UI_HANDLER_CONTINUE)) {
1889                         char ascii = event->ascii;
1890
1891                         /* exception that's useful for number buttons, some keyboard
1892                            numpads have a comma instead of a period */
1893                         if(ELEM3(but->type, NUM, NUMABS, NUMSLI))
1894                                 if(event->type == PADPERIOD && ascii == ',')
1895                                         ascii = '.';
1896
1897                         changed= ui_textedit_type_ascii(but, data, ascii);
1898                         retval= WM_UI_HANDLER_BREAK;
1899                         
1900                 }
1901                 /* textbutton with magnifier icon: do live update for search button */
1902                 if(but->icon==ICON_VIEWZOOM)
1903                         update= 1;
1904         }
1905
1906         if(changed) {
1907                 /* only update when typing for TAB key */
1908                 if(update && data->interactive) ui_apply_button(C, block, but, data, 1);
1909                 else ui_check_but(but);
1910                 but->changed= TRUE;
1911                 
1912                 if(data->searchbox)
1913                         ui_searchbox_update(C, data->searchbox, but, 1); /* 1 = reset */
1914         }
1915
1916         if(changed || (retval == WM_UI_HANDLER_BREAK))
1917                 ED_region_tag_redraw(data->region);
1918 }
1919
1920 static void ui_do_but_textedit_select(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1921 {
1922         int mx, my, retval= WM_UI_HANDLER_CONTINUE;
1923
1924         switch(event->type) {
1925                 case MOUSEMOVE: {
1926                         mx= event->x;
1927                         my= event->y;
1928                         ui_window_to_block(data->region, block, &mx, &my);
1929
1930                         ui_textedit_set_cursor_select(but, data, mx);
1931                         retval= WM_UI_HANDLER_BREAK;
1932                         break;
1933                 }
1934                 case LEFTMOUSE:
1935                         if(event->val == KM_RELEASE)
1936                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1937                         retval= WM_UI_HANDLER_BREAK;
1938                         break;
1939         }
1940
1941         if(retval == WM_UI_HANDLER_BREAK) {
1942                 ui_check_but(but);
1943                 ED_region_tag_redraw(data->region);
1944         }
1945 }
1946
1947 /* ************* number editing for various types ************* */
1948
1949 static void ui_numedit_begin(uiBut *but, uiHandleButtonData *data)
1950 {
1951         float softrange, softmin, softmax;
1952
1953         if(but->type == BUT_CURVE) {
1954                 but->editcumap= (CurveMapping*)but->poin;
1955         }
1956         else if(but->type == BUT_COLORBAND) {
1957                 data->coba= (ColorBand*)but->poin;
1958                 but->editcoba= data->coba;
1959         }
1960         else if(ELEM3(but->type, BUT_NORMAL, HSVCUBE, HSVCIRCLE)) {
1961                 ui_get_but_vectorf(but, data->origvec);
1962                 VECCOPY(data->vec, data->origvec);
1963                 but->editvec= data->vec;
1964         }
1965         else {
1966                 data->startvalue= ui_get_but_val(but);
1967                 data->origvalue= data->startvalue;
1968                 data->value= data->origvalue;
1969                 but->editval= &data->value;
1970
1971                 softmin= but->softmin;
1972                 softmax= but->softmax;
1973                 softrange= softmax - softmin;
1974
1975                 data->dragfstart= (softrange == 0.0f)? 0.0f: ((float)data->value - softmin)/softrange;
1976                 data->dragf= data->dragfstart;
1977         }
1978
1979         data->dragchange= 0;
1980         data->draglock= 1;
1981 }
1982
1983 static void ui_numedit_end(uiBut *but, uiHandleButtonData *data)
1984 {
1985         but->editval= NULL;
1986         but->editvec= NULL;
1987         but->editcoba= NULL;
1988         but->editcumap= NULL;
1989
1990         data->dragstartx= 0;
1991         data->draglastx= 0;
1992         data->dragchange= 0;
1993         data->dragcbd= NULL;
1994         data->dragsel= 0;
1995 }
1996
1997 static void ui_numedit_apply(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data)
1998 {
1999         if(data->interactive) ui_apply_button(C, block, but, data, 1);
2000         else ui_check_but(but);
2001
2002         ED_region_tag_redraw(data->region);
2003 }
2004
2005 /* ****************** menu opening for various types **************** */
2006
2007 static void ui_blockopen_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
2008 {
2009         uiBlockCreateFunc func= NULL;
2010         uiBlockHandleCreateFunc handlefunc= NULL;
2011         uiMenuCreateFunc menufunc= NULL;
2012         char *menustr= NULL;
2013         void *arg= NULL;
2014
2015         switch(but->type) {
2016                 case BLOCK:
2017                 case PULLDOWN:
2018                         if(but->menu_create_func) {
2019                                 menufunc= but->menu_create_func;
2020                                 arg= but->poin;
2021                         }
2022                         else {
2023                                 func= but->block_create_func;
2024                                 arg= but->poin?but->poin:but->func_argN;
2025                         }
2026                         break;
2027                 case MENU:
2028                         if(but->menu_create_func) {
2029                                 menufunc= but->menu_create_func;
2030                                 arg= but->poin;
2031                         }
2032                         else {
2033                                 data->origvalue= ui_get_but_val(but);
2034                                 data->value= data->origvalue;
2035                                 but->editval= &data->value;
2036
2037                                 menustr= but->str;
2038                         }
2039                         break;
2040                 case ICONROW:
2041                         menufunc= ui_block_func_ICONROW;
2042                         arg= but;
2043                         break;
2044                 case ICONTEXTROW:
2045                         menufunc= ui_block_func_ICONTEXTROW;
2046                         arg= but;
2047                         break;
2048                 case COL:
2049                         ui_get_but_vectorf(but, data->origvec);
2050                         VECCOPY(data->vec, data->origvec);
2051                         but->editvec= data->vec;
2052
2053                         handlefunc= ui_block_func_COL;
2054                         arg= but;
2055                         break;
2056         }
2057
2058         if(func || handlefunc) {
2059                 data->menu= ui_popup_block_create(C, data->region, but, func, handlefunc, arg);
2060                 if(but->block->handle)
2061                         data->menu->popup= but->block->handle->popup;
2062         }
2063         else if(menufunc || menustr) {
2064                 data->menu= ui_popup_menu_create(C, data->region, but, menufunc, arg, menustr);
2065                 if(but->block->handle)
2066                         data->menu->popup= but->block->handle->popup;
2067         }
2068
2069         /* this makes adjacent blocks auto open from now on */
2070         //if(but->block->auto_open==0) but->block->auto_open= 1;
2071 }
2072
2073 static void ui_blockopen_end(bContext *C, uiBut *but, uiHandleButtonData *data)
2074 {
2075         if(but) {
2076                 but->editval= NULL;
2077                 but->editvec= NULL;
2078
2079                 but->block->auto_open_last= PIL_check_seconds_timer();
2080         }
2081
2082         if(data->menu) {
2083                 ui_popup_block_free(C, data->menu);
2084                 data->menu= NULL;
2085         }
2086 }
2087
2088 /* ***************** events for different button types *************** */
2089
2090 static int ui_do_but_BUT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2091 {
2092         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2093                 if(event->type == LEFTMOUSE && event->val==KM_PRESS) {
2094                         button_activate_state(C, but, BUTTON_STATE_WAIT_RELEASE);
2095                         return WM_UI_HANDLER_BREAK;
2096                 }
2097                 else if(event->type == LEFTMOUSE && but->block->handle) {
2098                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2099                         return WM_UI_HANDLER_BREAK;
2100                 }
2101                 else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS) {
2102                         button_activate_state(C, but, BUTTON_STATE_WAIT_FLASH);
2103                         return WM_UI_HANDLER_BREAK;
2104                 }
2105         }
2106         else if(data->state == BUTTON_STATE_WAIT_RELEASE) {
2107                 if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2108                         if(!(but->flag & UI_SELECT))
2109                                 data->cancel= 1;
2110                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2111                         return WM_UI_HANDLER_BREAK;
2112                 }
2113         }
2114
2115         return WM_UI_HANDLER_CONTINUE;
2116 }
2117
2118 static int ui_do_but_HOTKEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2119 {
2120         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2121                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2122                         but->drawstr[0]= 0;
2123                         but->modifier_key= 0;
2124                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
2125                         return WM_UI_HANDLER_BREAK;
2126                 }
2127         }
2128         else if(data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2129                 
2130                 if(event->type == MOUSEMOVE)
2131                         return WM_UI_HANDLER_CONTINUE;
2132                 
2133                 if(event->type == LEFTMOUSE && event->val==KM_PRESS) {
2134                         /* only cancel if click outside the button */
2135                         if(ui_mouse_inside_button(but->active->region, but, event->x, event->y) == 0) {
2136                                 /* data->cancel doesnt work, this button opens immediate */
2137                                 if(but->flag & UI_BUT_IMMEDIATE)
2138                                         ui_set_but_val(but, 0);
2139                                 else
2140                                         data->cancel= 1;
2141                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2142                                 return WM_UI_HANDLER_BREAK;
2143                         }
2144                 }
2145                 
2146                 /* always set */
2147                 but->modifier_key = 0;
2148                 if(event->shift)
2149                         but->modifier_key |= KM_SHIFT;
2150                 if(event->alt)
2151                         but->modifier_key |= KM_ALT;
2152                 if(event->ctrl)
2153                         but->modifier_key |= KM_CTRL;
2154                 if(event->oskey)
2155                         but->modifier_key |= KM_OSKEY;
2156                 
2157                 ui_check_but(but);
2158                 ED_region_tag_redraw(data->region);
2159                         
2160                 if(event->val==KM_PRESS) {
2161                         if(ISHOTKEY(event->type)) { 
2162                                 
2163                                 if(WM_key_event_string(event->type)[0])
2164                                         ui_set_but_val(but, event->type);
2165                                 else
2166                                         data->cancel= 1;
2167                                 
2168                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2169                                 return WM_UI_HANDLER_BREAK;
2170                         }
2171                         else if(event->type == ESCKEY) {
2172                                 data->cancel= 1;
2173                                 data->escapecancel= 1;
2174                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2175                         }
2176                         
2177                 }
2178         }
2179         
2180         return WM_UI_HANDLER_CONTINUE;
2181 }
2182
2183 static int ui_do_but_KEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2184 {
2185         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2186                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2187                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
2188                         return WM_UI_HANDLER_BREAK;
2189                 }
2190         }
2191         else if(data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2192                 if(event->type == MOUSEMOVE)
2193                         return WM_UI_HANDLER_CONTINUE;
2194
2195                 if(event->val==KM_PRESS) {
2196                         if(WM_key_event_string(event->type)[0])
2197                                 ui_set_but_val(but, event->type);
2198                         else
2199                                 data->cancel= 1;
2200
2201                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2202                 }
2203         }
2204
2205         return WM_UI_HANDLER_CONTINUE;
2206 }
2207
2208 static int ui_do_but_TEX(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2209 {
2210         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2211                 if(ELEM(event->type, LEFTMOUSE, EVT_BUT_OPEN) && event->val==KM_PRESS) {
2212                         if(but->dt == UI_EMBOSSN && !event->ctrl);
2213                         else {
2214                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2215                                 return WM_UI_HANDLER_BREAK;
2216                         }
2217                 }
2218         }
2219         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2220                 ui_do_but_textedit(C, block, but, data, event);
2221                 return WM_UI_HANDLER_BREAK;
2222         }
2223         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2224                 ui_do_but_textedit_select(C, block, but, data, event);
2225                 return WM_UI_HANDLER_BREAK;
2226         }
2227
2228         return WM_UI_HANDLER_CONTINUE;
2229 }
2230
2231 static int ui_do_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2232 {
2233         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2234                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2235                         data->togdual= event->ctrl;
2236                         data->togonly= !event->shift;
2237                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2238                         return WM_UI_HANDLER_BREAK;
2239                 }
2240         }
2241         return WM_UI_HANDLER_CONTINUE;
2242 }
2243
2244 static int ui_do_but_EXIT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2245 {
2246         
2247         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2248
2249                 /* first handle click on icondrag type button */
2250                 if(event->type==LEFTMOUSE && but->dragpoin) {
2251                         if(ui_but_mouse_inside_icon(but, data->region, event)) {
2252                                 
2253                                 /* tell the button to wait and keep checking further events to
2254                                  * see if it should start dragging */
2255                                 button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
2256                                 data->dragstartx= event->x;
2257                                 data->dragstarty= event->y;
2258                                 return WM_UI_HANDLER_CONTINUE;
2259                         }
2260                 }
2261                 
2262                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2263                         int ret = WM_UI_HANDLER_BREAK;
2264                         /* XXX (a bit ugly) Special case handling for filebrowser drag button */
2265                         if(but->dragpoin && but->imb && ui_but_mouse_inside_icon(but, data->region, event)) {
2266                                 ret = WM_UI_HANDLER_CONTINUE;
2267                         }
2268                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2269                         return ret;
2270                 }
2271         }
2272         else if(data->state == BUTTON_STATE_WAIT_DRAG) {
2273                 
2274                 /* this function also ends state */
2275                 if(ui_but_start_drag(C, but, data, event)) {
2276                         return WM_UI_HANDLER_BREAK;
2277                 }
2278                 
2279                 /* If the mouse has been pressed and released, getting to 
2280                  * this point without triggering a drag, then clear the 
2281                  * drag state for this button and continue to pass on the event */
2282                 if(event->type==LEFTMOUSE && event->val==KM_RELEASE) {
2283                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2284                         return WM_UI_HANDLER_CONTINUE;
2285                 }
2286                 
2287                 /* while waiting for a drag to be triggered, always block 
2288                  * other events from getting handled */
2289                 return WM_UI_HANDLER_BREAK;
2290         }
2291         
2292         return WM_UI_HANDLER_CONTINUE;
2293 }
2294
2295 /* var names match ui_numedit_but_NUM */
2296 static float ui_numedit_apply_snapf(uiBut *but, float tempf, float softmin, float softmax, float softrange, int snap)
2297 {
2298         if(tempf==softmin || tempf==softmax || snap==0) {
2299                 /* pass */
2300         }
2301         else {
2302                 float fac= 1.0f;
2303                 
2304                 if(ui_is_but_unit(but)) {
2305                         UnitSettings *unit= but->block->unit;
2306                         int unit_type= uiButGetUnitType(but)>>16;
2307
2308                         if(bUnit_IsValid(unit->system, unit_type)) {
2309                                 fac= (float)bUnit_BaseScalar(unit->system, unit_type);
2310                                 if(ELEM3(unit_type, B_UNIT_LENGTH, B_UNIT_AREA, B_UNIT_VOLUME)) {
2311                                         fac /= unit->scale_length;
2312                                 }
2313                         }
2314                 }
2315
2316                 if(fac != 1.0f) {
2317                         /* snap in unit-space */
2318                         tempf /= fac;
2319                         /* softmin /= fac; */ /* UNUSED */
2320                         /* softmax /= fac; */ /* UNUSED */
2321                         softrange /= fac;
2322                 }
2323
2324                 if(snap==1) {
2325                         if(softrange < 2.10f) tempf= 0.1f*floorf(10.0f*tempf);
2326                         else if(softrange < 21.0f) tempf= floorf(tempf);
2327                         else tempf= 10.0f*floorf(tempf/10.0f);
2328                 }
2329                 else if(snap==2) {
2330                         if(softrange < 2.10f) tempf= 0.01f*floorf(100.0f*tempf);
2331                         else if(softrange < 21.0f) tempf= 0.1f*floorf(10.0f*tempf);
2332                         else tempf= floor(tempf);
2333                 }
2334                 
2335                 if(fac != 1.0f)
2336                         tempf *= fac;
2337         }
2338
2339         return tempf;
2340 }
2341
2342 static float ui_numedit_apply_snap(int temp, float softmin, float softmax, int snap)
2343 {
2344         if(temp==softmin || temp==softmax)
2345                 return temp;
2346
2347         switch(snap) {
2348         case 0:
2349                 break;
2350         case 1:
2351                 temp= 10*(temp/10);
2352                 break;
2353         case 2:
2354                 temp= 100*(temp/100);
2355                 break;
2356         }
2357
2358         return temp;
2359 }
2360
2361 static int ui_numedit_but_NUM(uiBut *but, uiHandleButtonData *data, float fac, int snap, int mx)
2362 {
2363         float deler, tempf, softmin, softmax, softrange;
2364         int lvalue, temp, changed= 0;
2365         
2366         if(mx == data->draglastx)
2367                 return changed;
2368         
2369         /* drag-lock - prevent unwanted scroll adjustments */
2370         /* change value (now 3) to adjust threshold in pixels */
2371         if(data->draglock) {
2372                 if(abs(mx-data->dragstartx) <= 3)
2373                         return changed;
2374
2375                 data->draglock= 0;
2376                 data->dragstartx= mx;  /* ignore mouse movement within drag-lock */
2377         }
2378
2379         softmin= but->softmin;
2380         softmax= but->softmax;
2381         softrange= softmax - softmin;
2382
2383         if(ui_is_a_warp_but(but)) {
2384                 /* Mouse location isn't screen clamped to the screen so use a linear mapping
2385                  * 2px == 1-int, or 1px == 1-ClickStep */
2386                 if(ui_is_but_float(but)) {
2387                         fac *= 0.01f*but->a1;
2388                         tempf = (float)data->startvalue + ((float)(mx - data->dragstartx) * fac);
2389                         tempf= ui_numedit_apply_snapf(but, tempf, softmin, softmax, softrange, snap);
2390
2391 #if 1           /* fake moving the click start, nicer for dragging back after passing the limit */
2392                         if(tempf < softmin) {
2393                                 data->dragstartx -= (softmin-tempf) / fac;
2394                                 tempf= softmin;
2395                         } else if (tempf > softmax) {
2396                                 data->dragstartx += (tempf-softmax) / fac;
2397                                 tempf= softmax;
2398                         }
2399 #else
2400                         CLAMP(tempf, softmin, softmax);
2401 #endif
2402
2403                         if(tempf != (float)data->value) {
2404                                 data->dragchange= 1;
2405                                 data->value= tempf;
2406                                 changed= 1;
2407                         }
2408                 }
2409                 else {
2410                         if(softrange > 256)             fac= 1.0;               /* 1px == 1 */
2411                         else if(softrange > 32) fac= 1.0/2.0;   /* 2px == 1 */
2412                         else                                    fac= 1.0/16.0;  /* 16px == 1? */
2413
2414                         temp= data->startvalue + (((double)mx - data->dragstartx) * (double)fac);
2415                         temp= ui_numedit_apply_snap(temp, softmin, softmax, snap);
2416
2417 #if 1           /* fake moving the click start, nicer for dragging back after passing the limit */
2418                         if(temp < softmin) {
2419                                 data->dragstartx -= (softmin-temp) / fac;
2420                                 temp= softmin;
2421                         } else if (temp > softmax) {
2422                                 data->dragstartx += (temp-softmax) / fac;
2423                                 temp= softmax;
2424                         }
2425 #else
2426                         CLAMP(temp, softmin, softmax);
2427 #endif
2428
2429                         if(temp != data->value) {
2430                                 data->dragchange= 1;
2431                                 data->value= temp;
2432                                 changed= 1;
2433                         }
2434                 }
2435
2436                 data->draglastx= mx;
2437         }
2438         else {
2439                 /* Use a non-linear mapping of the mouse drag especially for large floats (normal behavior) */
2440                 deler= 500;
2441                 if(!ui_is_but_float(but)) {
2442                         /* prevent large ranges from getting too out of control */
2443                         if (softrange > 600) deler = powf(softrange, 0.75);
2444                         
2445                         if (softrange < 100) deler= 200.0;
2446                         if (softrange < 25) deler= 50.0;
2447                 }
2448                 deler /= fac;
2449
2450                 if(softrange > 11) {
2451                         /* non linear change in mouse input- good for high precicsion */
2452                         data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabsf(data->dragstartx-mx)*0.002f);
2453                 } else if (softrange > 129) { /* only scale large int buttons */
2454                         /* non linear change in mouse input- good for high precicsionm ints need less fine tuning */
2455                         data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabsf(data->dragstartx-mx)*0.004f);
2456                 } else {
2457                         /*no scaling */
2458                         data->dragf+= ((float)(mx-data->draglastx))/deler ;
2459                 }
2460         
2461                 CLAMP(data->dragf, 0.0f, 1.0f);
2462                 data->draglastx= mx;
2463                 tempf= (softmin + data->dragf*softrange);
2464
2465
2466                 if(!ui_is_but_float(but)) {
2467                         temp= floorf(tempf + 0.5f);
2468
2469                         temp= ui_numedit_apply_snap(temp, softmin, softmax, snap);
2470
2471                         CLAMP(temp, softmin, softmax);
2472                         lvalue= (int)data->value;
2473                         
2474                         if(temp != lvalue) {
2475                                 data->dragchange= 1;
2476                                 data->value= (double)temp;
2477                                 changed= 1;
2478                         }
2479                 }
2480                 else {
2481                         temp= 0;
2482                         tempf= ui_numedit_apply_snapf(but, tempf, softmin, softmax, softrange, snap);
2483
2484                         CLAMP(tempf, softmin, softmax);
2485
2486                         if(tempf != (float)data->value) {
2487                                 data->dragchange= 1;
2488                                 data->value= tempf;
2489                                 changed= 1;
2490                         }
2491                 }
2492         }
2493
2494
2495         return changed;
2496 }
2497
2498 static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2499 {
2500         int mx, my;     /* mouse location scaled to fit the UI */
2501         int screen_mx, screen_my; /* mouse location kept at screen pixel coords */
2502         int click= 0;
2503         int retval= WM_UI_HANDLER_CONTINUE;
2504         
2505         mx= screen_mx= event->x;
2506         my= screen_my= event->y;
2507
2508         ui_window_to_block(data->region, block, &mx, &my);
2509
2510         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2511                 /* XXX hardcoded keymap check.... */
2512                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2513                         mx= but->x1;
2514                         click= 1;
2515                 }
2516                 else if(event->type == WHEELUPMOUSE && event->alt) {
2517                         mx= but->x2;
2518                         click= 1;
2519                 }
2520                 else if(event->val==KM_PRESS) {
2521                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2522                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2523                                 retval= WM_UI_HANDLER_BREAK;
2524                         }
2525                         else if(event->type == LEFTMOUSE) {
2526                                 data->dragstartx= data->draglastx= ui_is_a_warp_but(but) ? screen_mx:mx;
2527                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2528                                 retval= WM_UI_HANDLER_BREAK;
2529                         }
2530                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2531                                 click= 1;
2532                         else if (event->type == MINUSKEY && event->val==KM_PRESS) {
2533                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2534                                 data->value = -data->value;
2535                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2536                                 retval= WM_UI_HANDLER_BREAK;
2537                         }
2538                 }
2539                 
2540         }
2541         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2542                 if(event->type == ESCKEY) {
2543                         data->cancel= 1;
2544                         data->escapecancel= 1;
2545                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2546                 }
2547                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2548                         if(data->dragchange)
2549                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2550                         else
2551                                 click= 1;
2552                 }
2553                 else if(event->type == MOUSEMOVE) {
2554                         float fac;
2555                         int snap;
2556
2557                         fac= 1.0f;
2558                         if(event->shift) fac /= 10.0f;
2559                         if(event->alt) fac /= 20.0f;
2560                         
2561                         snap= (event->ctrl)? (event->shift)? 2: 1: 0;
2562
2563                         if(ui_numedit_but_NUM(but, data, fac, snap, (ui_is_a_warp_but(but) ? screen_mx:mx)))
2564                                 ui_numedit_apply(C, block, but, data);
2565                 }
2566                 retval= WM_UI_HANDLER_BREAK;
2567         }
2568         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2569                 ui_do_but_textedit(C, block, but, data, event);
2570                 retval= WM_UI_HANDLER_BREAK;
2571         }
2572         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2573                 ui_do_but_textedit_select(C, block, but, data, event);
2574                 retval= WM_UI_HANDLER_BREAK;
2575         }
2576         
2577         if(click) {
2578                 /* we can click on the side arrows to increment/decrement,
2579                  * or click inside to edit the value directly */
2580                 float tempf, softmin, softmax;
2581                 int temp;
2582
2583                 softmin= but->softmin;
2584                 softmax= but->softmax;
2585
2586                 if(!ui_is_but_float(but)) {
2587                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2588                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2589
2590                                 temp= (int)data->value - 1;
2591                                 if(temp>=softmin && temp<=softmax)
2592                                         data->value= (double)temp;
2593                                 else
2594                                         data->cancel= 1;
2595
2596                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2597                         }
2598                         else if(mx > (but->x1 + (2*(but->x2 - but->x1)/3) + 3)) {
2599                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2600
2601                                 temp= (int)data->value + 1;
2602                                 if(temp>=softmin && temp<=softmax)
2603                                         data->value= (double)temp;
2604                                 else
2605                                         data->cancel= 1;
2606
2607                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2608                         }
2609                         else
2610                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2611                 }
2612                 else {
2613                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2614                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2615
2616                                 tempf= (float)data->value - 0.01f * but->a1;
2617                                 if (tempf < softmin) tempf = softmin;
2618                                 data->value= tempf;
2619
2620                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2621                         }
2622                         else if(mx > but->x1 + (2*((but->x2 - but->x1)/3) + 3)) {
2623                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2624
2625                                 tempf= (float)data->value + 0.01f * but->a1;
2626                                 if (tempf > softmax) tempf = softmax;
2627                                 data->value= tempf;
2628
2629                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2630                         }
2631                         else
2632                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2633                 }
2634
2635                 retval= WM_UI_HANDLER_BREAK;
2636         }
2637         
2638         return retval;
2639 }
2640
2641 static int ui_numedit_but_SLI(uiBut *but, uiHandleButtonData *data, int shift, int ctrl, int mx)
2642 {
2643         float deler, f, tempf, softmin, softmax, softrange;
2644         int temp, lvalue, changed= 0;
2645
2646         softmin= but->softmin;
2647         softmax= but->softmax;
2648         softrange= softmax - softmin;
2649
2650         if(but->type==NUMSLI) deler= ((but->x2-but->x1) - 5.0f*but->aspect);
2651         else if(but->type==HSVSLI) deler= ((but->x2-but->x1)/2.0f - 5.0f*but->aspect);
2652         else if(but->type==SCROLL) {
2653                 int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2654                 float size= (horizontal)? (but->x2-but->x1): -(but->y2-but->y1);
2655                 deler= size*(but->softmax - but->softmin)/(but->softmax - but->softmin + but->a1);
2656         }
2657         else deler= (but->x2-but->x1- 5.0f*but->aspect);
2658
2659         f= (float)(mx-data->dragstartx)/deler + data->dragfstart;
2660         
2661         if(shift)
2662                 f= (f-data->dragfstart)/10.0f + data->dragfstart;
2663
2664         CLAMP(f, 0.0f, 1.0f);
2665         tempf= softmin + f*softrange;
2666         temp= floorf(tempf+0.5f);
2667
2668         if(ctrl) {
2669                 if(tempf==softmin || tempf==softmax);
2670                 else if(ui_is_but_float(but)) {
2671
2672                         if(shift) {
2673                                 if(tempf==softmin || tempf==softmax);
2674                                 else if(softmax-softmin < 2.10f) tempf= 0.01f * floorf(100.0f*tempf);
2675                                 else if(softmax-softmin < 21.0f) tempf= 0.1f * floorf(10.0f*tempf);
2676                                 else tempf= floorf(tempf);
2677                         }
2678                         else {
2679                                 if(softmax-softmin < 2.10f) tempf= 0.1f * floorf(10.0f*tempf);
2680                                 else if(softmax-softmin < 21.0f) tempf= floorf(tempf);
2681                                 else tempf= 10.0f*floorf(tempf/10.0f);
2682                         }
2683                 }
2684                 else {
2685                         temp= 10*(temp/10);
2686                         tempf= temp;
2687                 }
2688         }
2689
2690         if(!ui_is_but_float(but)) {
2691                 lvalue= floor(data->value+0.5);
2692
2693                 CLAMP(temp, softmin, softmax);
2694
2695                 if(temp != lvalue) {
2696                         data->value= temp;
2697                         data->dragchange= 1;
2698                         changed= 1;
2699                 }
2700         }
2701         else {
2702                 CLAMP(tempf, softmin, softmax);
2703
2704                 if(tempf != (float)data->value) {
2705                         data->value= tempf;
2706                         data->dragchange= 1;
2707                         changed= 1;
2708                 }
2709         }
2710
2711         return changed;
2712 }
2713
2714 static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2715 {
2716         int mx, my, click= 0;
2717         int retval= WM_UI_HANDLER_CONTINUE;
2718
2719         mx= event->x;
2720         my= event->y;
2721         ui_window_to_block(data->region, block, &mx, &my);
2722
2723         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2724                 /* XXX hardcoded keymap check.... */
2725                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2726                         mx= but->x1;
2727                         click= 2;
2728                 }
2729                 else if(event->type == WHEELUPMOUSE && event->alt) {
2730                         mx= but->x2;
2731                         click= 2;
2732                 }
2733                 else if(event->val==KM_PRESS) {
2734                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2735                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2736                                 retval= WM_UI_HANDLER_BREAK;
2737                         }
2738                         /* alt-click on sides to get "arrows" like in NUM buttons, and match wheel usage above */
2739                         else if(event->type == LEFTMOUSE && event->alt) {
2740                                 int halfpos = (but->x1 + but->x2) / 2;
2741                                 click = 2;
2742                                 if (mx < halfpos)
2743                                         mx = but->x1;
2744                                 else
2745                                         mx = but->x2;
2746                         }
2747                         else if(event->type == LEFTMOUSE) {
2748                                 data->dragstartx= mx;
2749                                 data->draglastx= mx;
2750                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2751                                 retval= WM_UI_HANDLER_BREAK;
2752                         }
2753                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2754                                 click= 1;
2755                         else if (event->type == MINUSKEY && event->val==KM_PRESS) {
2756                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2757                                 data->value = -data->value;
2758                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2759                                 retval= WM_UI_HANDLER_BREAK;
2760                         }
2761                 }
2762         }
2763         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2764                 if(event->type == ESCKEY) {
2765                         data->cancel= 1;
2766                         data->escapecancel= 1;
2767                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2768                 }
2769                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2770                         if(data->dragchange)
2771                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2772                         else
2773                                 click= 1;
2774                 }
2775                 else if(event->type == MOUSEMOVE) {
2776                         if(ui_numedit_but_SLI(but, data, event->shift, event->ctrl, mx))
2777                                 ui_numedit_apply(C, block, but, data);
2778                 }
2779                 retval= WM_UI_HANDLER_BREAK;
2780         }
2781         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2782                 ui_do_but_textedit(C, block, but, data, event);
2783                 retval= WM_UI_HANDLER_BREAK;
2784         }
2785         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2786                 ui_do_but_textedit_select(C, block, but, data, event);
2787                 retval= WM_UI_HANDLER_BREAK;
2788         }
2789
2790         if(click) {
2791                 if (click==2) {
2792                         /* nudge slider to the left or right */
2793                         float f, tempf, softmin, softmax, softrange;
2794                         int temp;
2795                         
2796                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2797                         
2798                         softmin= but->softmin;
2799                         softmax= but->softmax;
2800                         softrange= softmax - softmin;
2801                         
2802                         tempf= data->value;
2803                         temp= (int)data->value;
2804
2805 #if 0
2806                         if(but->type==SLI) {
2807                                 f= (float)(mx-but->x1)/(but->x2-but->x1); /* same as below */
2808                         }
2809                         else
2810 #endif
2811                         {
2812                                 f= (float)(mx- but->x1)/(but->x2-but->x1);
2813                         }
2814                         
2815                         f= softmin + f*softrange;
2816                         
2817                         if(!ui_is_but_float(but)) {
2818                                 if(f<temp) temp--;
2819                                 else temp++;
2820                                 
2821                                 if(temp>=softmin && temp<=softmax)
2822                                         data->value= temp;
2823                                 else
2824                                         data->cancel= 1;
2825                         } 
2826                         else {
2827                                 if(f<tempf) tempf -= 0.01f;
2828                                 else tempf += 0.01f;
2829                                 
2830                                 if(tempf>=softmin && tempf<=softmax)
2831                                         data->value= tempf;
2832                                 else
2833                                         data->cancel= 1;
2834                         }
2835                         
2836                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2837                         retval= WM_UI_HANDLER_BREAK;
2838                 }
2839                 else {
2840                         /* edit the value directly */
2841                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2842                         retval= WM_UI_HANDLER_BREAK;
2843                 }
2844         }
2845         
2846         return retval;
2847 }
2848
2849 static int ui_do_but_SCROLL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2850 {
2851         int mx, my /*, click= 0 */;
2852         int retval= WM_UI_HANDLER_CONTINUE;
2853         int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2854         
2855         mx= event->x;
2856         my= event->y;
2857         ui_window_to_block(data->region, block, &mx, &my);
2858
2859         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2860                 if(event->val==KM_PRESS) {
2861                         if(event->type == LEFTMOUSE) {
2862                                 if(horizontal) {
2863                                         data->dragstartx= mx;
2864                                         data->draglastx= mx;
2865                                 }
2866                                 else {
2867                                         data->dragstartx= my;
2868                                         data->draglastx= my;
2869                                 }
2870                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2871                                 retval= WM_UI_HANDLER_BREAK;
2872                         }
2873                         /* UNUSED - otherwise code is ok, add back if needed */
2874                         /* else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2875                                 click= 1;
2876                         */
2877                 }
2878         }
2879         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2880                 if(event->type == ESCKEY) {
2881                         data->cancel= 1;
2882                         data->escapecancel= 1;
2883                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2884                 }
2885                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2886                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2887                 }
2888                 else if(event->type == MOUSEMOVE) {
2889                         if(ui_numedit_but_SLI(but, data, 0, 0, (horizontal)? mx: my))
2890                                 ui_numedit_apply(C, block, but, data);
2891                 }
2892
2893                 retval= WM_UI_HANDLER_BREAK;
2894         }
2895         
2896         return retval;
2897 }
2898
2899
2900 static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2901 {
2902         
2903         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2904                 
2905                 /* first handle click on icondrag type button */
2906                 if(event->type==LEFTMOUSE && but->dragpoin && event->val==KM_PRESS) {
2907                         if(ui_but_mouse_inside_icon(but, data->region, event)) {
2908                                 button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
2909                                 data->dragstartx= event->x;
2910                                 data->dragstarty= event->y;
2911                                 return WM_UI_HANDLER_BREAK;
2912                         }
2913                 }
2914                 
2915                 /* regular open menu */
2916                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2917                         button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
2918                         return WM_UI_HANDLER_BREAK;
2919                 }
2920                 else if(ELEM3(but->type, MENU, ICONROW, ICONTEXTROW)) {
2921                         
2922                         if(event->type == WHEELDOWNMOUSE && event->alt) {
2923                                 data->value= ui_step_name_menu(but, -1);
2924                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2925                                 ui_apply_button(C, but->block, but, data, 1);
2926                                 return WM_UI_HANDLER_BREAK;
2927                         }
2928                         else if(event->type == WHEELUPMOUSE && event->alt) {
2929                                 data->value= ui_step_name_menu(but, 1);
2930                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2931                                 ui_apply_button(C, but->block, but, data, 1);
2932                                 return WM_UI_HANDLER_BREAK;
2933                         }
2934                 }
2935                 else if(but->type==COL) {
2936                         if( ELEM(event->type, WHEELDOWNMOUSE, WHEELUPMOUSE) && event->alt) {
2937                                 float *hsv= ui_block_hsv_get(but->block);
2938                                 float col[3];
2939                                 
2940                                 ui_get_but_vectorf(but, col);
2941                                 rgb_to_hsv_compat(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
2942
2943                                 if(event->type==WHEELDOWNMOUSE)
2944                                         hsv[2]= CLAMPIS(hsv[2]-0.05f, 0.0f, 1.0f);
2945                                 else
2946                                         hsv[2]= CLAMPIS(hsv[2]+0.05f, 0.0f, 1.0f);
2947                                 
2948                                 hsv_to_rgb(hsv[0], hsv[1], hsv[2], data->vec, data->vec+1, data->vec+2);
2949                                 ui_set_but_vectorf(but, data->vec);
2950                                 
2951                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2952                                 ui_apply_button(C, but->block, but, data, 1);
2953                                 return WM_UI_HANDLER_BREAK;
2954                         }
2955                 }
2956         }
2957         else if(data->state == BUTTON_STATE_WAIT_DRAG) {
2958                 
2959                 /* this function also ends state */
2960                 if(ui_but_start_drag(C, but, data, event)) {
2961                         return WM_UI_HANDLER_BREAK;
2962                 }
2963                 
2964                 /* outside icon quit, not needed if drag activated */
2965                 if(0==ui_but_mouse_inside_icon(but, data->region, event)) {
2966                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2967                         data->cancel= 1;
2968                         return WM_UI_HANDLER_BREAK;
2969                 }
2970                 
2971                 if(event->type==LEFTMOUSE && event->val==KM_RELEASE) {
2972                         button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);