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