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