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