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