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