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