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