merge with trunk at r27259 and commit of a patch by anthony jones to fix msvc (though...
[blender-staging.git] / source / blender / editors / interface / interface_handlers.c
1 /**
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  * 
21  * Contributor(s): Blender Foundation
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 #include <float.h>
27 #include <math.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "DNA_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_CLICK;
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                         if(softrange > 256)             fac= 1.0;               /* 1px == 1 */
2194                         else if(softrange > 32) fac= 1.0/2.0;   /* 2px == 1 */
2195                         else                                    fac= 1.0/16.0;  /* 16px == 1? */
2196
2197                         temp= data->startvalue + ((mx - data->dragstartx) * fac);
2198                         temp= ui_numedit_apply_snap(temp, softmin, softmax, snap);
2199
2200 #if 1           /* fake moving the click start, nicer for dragging back after passing the limit */
2201                         if(temp < softmin) {
2202                                 data->dragstartx -= (softmin-temp) / fac;
2203                                 temp= softmin;
2204                         } else if (temp > softmax) {
2205                                 data->dragstartx += (temp-softmax) / fac;
2206                                 temp= softmax;
2207                         }
2208 #else
2209                         CLAMP(temp, softmin, softmax);
2210 #endif
2211
2212                         if(temp != data->value) {
2213                                 data->dragchange= 1;
2214                                 data->value= temp;
2215                                 changed= 1;
2216                         }
2217                 }
2218
2219                 data->draglastx= mx;
2220         }
2221         else {
2222                 /* Use a non-linear mapping of the mouse drag especially for large floats (normal behavior) */
2223                 deler= 500;
2224                 if(!ui_is_but_float(but)) {
2225                         if((softrange)<100) deler= 200.0;
2226                         if((softrange)<25) deler= 50.0;
2227                 }
2228                 deler /= fac;
2229
2230                 if(ui_is_but_float(but) && softrange > 11) {
2231                         /* non linear change in mouse input- good for high precicsion */
2232                         data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.002);
2233                 } else if (!ui_is_but_float(but) && softrange > 129) { /* only scale large int buttons */
2234                         /* non linear change in mouse input- good for high precicsionm ints need less fine tuning */
2235                         data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.004);
2236                 } else {
2237                         /*no scaling */
2238                         data->dragf+= ((float)(mx-data->draglastx))/deler ;
2239                 }
2240         
2241                 if(data->dragf>1.0) data->dragf= 1.0;
2242                 if(data->dragf<0.0) data->dragf= 0.0;
2243                 data->draglastx= mx;
2244                 tempf= (softmin + data->dragf*softrange);
2245
2246
2247                 if(!ui_is_but_float(but)) {
2248                         temp= floor(tempf+.5);
2249
2250                         temp= ui_numedit_apply_snap(temp, softmin, softmax, snap);
2251
2252                         CLAMP(temp, softmin, softmax);
2253                         lvalue= (int)data->value;
2254
2255                         if(temp != lvalue) {
2256                                 data->dragchange= 1;
2257                                 data->value= (double)temp;
2258                                 changed= 1;
2259                         }
2260                 }
2261                 else {
2262                         temp= 0;
2263                         tempf= ui_numedit_apply_snapf(tempf, softmin, softmax, softrange, snap);
2264
2265                         CLAMP(tempf, softmin, softmax);
2266
2267                         if(tempf != data->value) {
2268                                 data->dragchange= 1;
2269                                 data->value= tempf;
2270                                 changed= 1;
2271                         }
2272                 }
2273         }
2274
2275
2276         return changed;
2277 }
2278
2279 static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2280 {
2281         int mx, my;     /* mouse location scaled to fit the UI */
2282         int screen_mx, screen_my; /* mouse location kept at screen pixel coords */
2283         int click= 0;
2284         int retval= WM_UI_HANDLER_CONTINUE;
2285         
2286         mx= screen_mx= event->x;
2287         my= screen_my= event->y;
2288
2289         ui_window_to_block(data->region, block, &mx, &my);
2290
2291         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2292                 /* XXX hardcoded keymap check.... */
2293                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2294                         mx= but->x1;
2295                         click= 1;
2296                 }
2297                 else if(event->type == WHEELUPMOUSE && event->alt) {
2298                         mx= but->x2;
2299                         click= 1;
2300                 }
2301                 else if(event->val==KM_PRESS) {
2302                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2303                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2304                                 retval= WM_UI_HANDLER_BREAK;
2305                         }
2306                         else if(event->type == LEFTMOUSE) {
2307                                 data->dragstartx= data->draglastx= ui_is_a_warp_but(but) ? screen_mx:mx;
2308                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2309                                 retval= WM_UI_HANDLER_BREAK;
2310                         }
2311                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2312                                 click= 1;
2313                         else if (event->type == MINUSKEY && event->val==KM_PRESS) {
2314                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2315                                 data->value = -data->value;
2316                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2317                                 retval= WM_UI_HANDLER_BREAK;
2318                         }
2319                 }
2320                 
2321         }
2322         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2323                 if(event->type == ESCKEY) {
2324                         data->cancel= 1;
2325                         data->escapecancel= 1;
2326                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2327                 }
2328                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2329                         if(data->dragchange)
2330                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2331                         else
2332                                 click= 1;
2333                 }
2334                 else if(event->type == MOUSEMOVE) {
2335                         float fac;
2336                         int snap;
2337
2338                         fac= 1.0f;
2339                         if(event->shift) fac /= 10.0f;
2340                         if(event->alt) fac /= 20.0f;
2341                         
2342                         snap= (event->ctrl)? (event->shift)? 2: 1: 0;
2343
2344                         if(ui_numedit_but_NUM(but, data, fac, snap, (ui_is_a_warp_but(but) ? screen_mx:mx)))
2345                                 ui_numedit_apply(C, block, but, data);
2346                 }
2347                 retval= WM_UI_HANDLER_BREAK;
2348         }
2349         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2350                 ui_do_but_textedit(C, block, but, data, event);
2351                 retval= WM_UI_HANDLER_BREAK;
2352         }
2353         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2354                 ui_do_but_textedit_select(C, block, but, data, event);
2355                 retval= WM_UI_HANDLER_BREAK;
2356         }
2357         
2358         if(click) {
2359                 /* we can click on the side arrows to increment/decrement,
2360                  * or click inside to edit the value directly */
2361                 float tempf, softmin, softmax;
2362                 int temp;
2363
2364                 softmin= but->softmin;
2365                 softmax= but->softmax;
2366
2367                 if(!ui_is_but_float(but)) {
2368                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2369                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2370
2371                                 temp= (int)data->value - 1;
2372                                 if(temp>=softmin && temp<=softmax)
2373                                         data->value= (double)temp;
2374                                 else
2375                                         data->cancel= 1;
2376
2377                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2378                         }
2379                         else if(mx > (but->x1 + (2*(but->x2 - but->x1)/3) + 3)) {
2380                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2381
2382                                 temp= (int)data->value + 1;
2383                                 if(temp>=softmin && temp<=softmax)
2384                                         data->value= (double)temp;
2385                                 else
2386                                         data->cancel= 1;
2387
2388                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2389                         }
2390                         else
2391                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2392                 }
2393                 else {
2394                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2395                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2396
2397                                 tempf= data->value - 0.01*but->a1;
2398                                 if (tempf < softmin) tempf = softmin;
2399                                 data->value= tempf;
2400
2401                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2402                         }
2403                         else if(mx > but->x1 + (2*((but->x2 - but->x1)/3) + 3)) {
2404                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2405
2406                                 tempf= data->value + 0.01*but->a1;
2407                                 if (tempf > softmax) tempf = softmax;
2408                                 data->value= tempf;
2409
2410                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2411                         }
2412                         else
2413                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2414                 }
2415
2416                 retval= WM_UI_HANDLER_BREAK;
2417         }
2418         
2419         return retval;
2420 }
2421
2422 static int ui_numedit_but_SLI(uiBut *but, uiHandleButtonData *data, int shift, int ctrl, int mx)
2423 {
2424         float deler, f, tempf, softmin, softmax, softrange;
2425         int temp, lvalue, changed= 0;
2426
2427         softmin= but->softmin;
2428         softmax= but->softmax;
2429         softrange= softmax - softmin;
2430
2431         if(but->type==NUMSLI) deler= ((but->x2-but->x1) - 5.0*but->aspect);
2432         else if(but->type==HSVSLI) deler= ((but->x2-but->x1)/2 - 5.0*but->aspect);
2433         else if(but->type==SCROLL) {
2434                 int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2435                 float size= (horizontal)? (but->x2-but->x1): -(but->y2-but->y1);
2436                 deler= size*(but->softmax - but->softmin)/(but->softmax - but->softmin + but->a1);
2437         }
2438         else deler= (but->x2-but->x1- 5.0*but->aspect);
2439
2440         f= (float)(mx-data->dragstartx)/deler + data->dragfstart;
2441         
2442         if(shift)
2443                 f= (f-data->dragfstart)/10.0 + data->dragfstart;
2444
2445         CLAMP(f, 0.0, 1.0);
2446         tempf= softmin + f*softrange;
2447         temp= floor(tempf+.5);
2448
2449         if(ctrl) {
2450                 if(tempf==softmin || tempf==softmax);
2451                 else if(ui_is_but_float(but)) {
2452
2453                         if(shift) {
2454                                 if(tempf==softmin || tempf==softmax);
2455                                 else if(softmax-softmin < 2.10) tempf= 0.01*floor(100.0*tempf);
2456                                 else if(softmax-softmin < 21.0) tempf= 0.1*floor(10.0*tempf);
2457                                 else tempf= floor(tempf);
2458                         }
2459                         else {
2460                                 if(softmax-softmin < 2.10) tempf= 0.1*floor(10*tempf);
2461                                 else if(softmax-softmin < 21.0) tempf= floor(tempf);
2462                                 else tempf= 10.0*floor(tempf/10.0);
2463                         }
2464                 }
2465                 else {
2466                         temp= 10*(temp/10);
2467                         tempf= temp;
2468                 }
2469         }
2470
2471         if(!ui_is_but_float(but)) {
2472                 lvalue= floor(data->value+0.5);
2473
2474                 CLAMP(temp, softmin, softmax);
2475
2476                 if(temp != lvalue) {
2477                         data->value= temp;
2478                         data->dragchange= 1;
2479                         changed= 1;
2480                 }
2481         }
2482         else {
2483                 CLAMP(tempf, softmin, softmax);
2484
2485                 if(tempf != data->value) {
2486                         data->value= tempf;
2487                         data->dragchange= 1;
2488                         changed= 1;
2489                 }
2490         }
2491
2492         return changed;
2493 }
2494
2495 static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2496 {
2497         int mx, my, click= 0;
2498         int retval= WM_UI_HANDLER_CONTINUE;
2499
2500         mx= event->x;
2501         my= event->y;
2502         ui_window_to_block(data->region, block, &mx, &my);
2503
2504         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2505                 /* XXX hardcoded keymap check.... */
2506                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2507                         mx= but->x1;
2508                         click= 2;
2509                 }
2510                 else if(event->type == WHEELUPMOUSE && event->alt) {
2511                         mx= but->x2;
2512                         click= 2;
2513                 }
2514                 else if(event->val==KM_PRESS) {
2515                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2516                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2517                                 retval= WM_UI_HANDLER_BREAK;
2518                         }
2519                         /* alt-click on sides to get "arrows" like in NUM buttons, and match wheel usage above */
2520                         else if(event->type == LEFTMOUSE && event->alt) {
2521                                 int halfpos = (but->x1 + but->x2) / 2;
2522                                 click = 2;
2523                                 if (mx < halfpos)
2524                                         mx = but->x1;
2525                                 else
2526                                         mx = but->x2;
2527                         }
2528                         else if(event->type == LEFTMOUSE) {
2529                                 data->dragstartx= mx;
2530                                 data->draglastx= mx;
2531                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2532                                 retval= WM_UI_HANDLER_BREAK;
2533                         }
2534                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2535                                 click= 1;
2536                         else if (event->type == MINUSKEY && event->val==KM_PRESS) {
2537                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2538                                 data->value = -data->value;
2539                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2540                                 retval= WM_UI_HANDLER_BREAK;
2541                         }
2542                 }
2543         }
2544         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2545                 if(event->type == ESCKEY) {
2546                         data->cancel= 1;
2547                         data->escapecancel= 1;
2548                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2549                 }
2550                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2551                         if(data->dragchange)
2552                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2553                         else
2554                                 click= 1;
2555                 }
2556                 else if(event->type == MOUSEMOVE) {
2557                         if(ui_numedit_but_SLI(but, data, event->shift, event->ctrl, mx))
2558                                 ui_numedit_apply(C, block, but, data);
2559                 }
2560                 retval= WM_UI_HANDLER_BREAK;
2561         }
2562         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2563                 ui_do_but_textedit(C, block, but, data, event);
2564                 retval= WM_UI_HANDLER_BREAK;
2565         }
2566         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2567                 ui_do_but_textedit_select(C, block, but, data, event);
2568                 retval= WM_UI_HANDLER_BREAK;
2569         }
2570
2571         if(click) {
2572                 if (click==2) {
2573                         /* nudge slider to the left or right */
2574                         float f, tempf, softmin, softmax, softrange;
2575                         int temp;
2576                         
2577                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2578                         
2579                         softmin= but->softmin;
2580                         softmax= but->softmax;
2581                         softrange= softmax - softmin;
2582                         
2583                         tempf= data->value;
2584                         temp= (int)data->value;
2585                         
2586                         /* XXX useles "if", same result for f, uh??? */
2587                         if(but->type==SLI) f= (float)(mx-but->x1)/(but->x2-but->x1);
2588                         else f= (float)(mx- but->x1)/(but->x2-but->x1);
2589                         
2590                         f= softmin + f*softrange;
2591                         
2592                         if(!ui_is_but_float(but)) {
2593                                 if(f<temp) temp--;
2594                                 else temp++;
2595                                 
2596                                 if(temp>=softmin && temp<=softmax)
2597                                         data->value= temp;
2598                                 else
2599                                         data->cancel= 1;
2600                         } 
2601                         else {
2602                                 if(f<tempf) tempf-=.01;
2603                                 else tempf+=.01;
2604                                 
2605                                 if(tempf>=softmin && tempf<=softmax)
2606                                         data->value= tempf;
2607                                 else
2608                                         data->cancel= 1;
2609                         }
2610                         
2611                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2612                         retval= WM_UI_HANDLER_BREAK;
2613                 }
2614                 else {
2615                         /* edit the value directly */
2616                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2617                         retval= WM_UI_HANDLER_BREAK;
2618                 }
2619         }
2620         
2621         return retval;
2622 }
2623
2624 static int ui_do_but_SCROLL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2625 {
2626         int mx, my, click= 0;
2627         int retval= WM_UI_HANDLER_CONTINUE;
2628         int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2629         
2630         mx= event->x;
2631         my= event->y;
2632         ui_window_to_block(data->region, block, &mx, &my);
2633
2634         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2635                 if(event->val==KM_PRESS) {
2636                         if(event->type == LEFTMOUSE) {
2637                                 if(horizontal) {
2638                                         data->dragstartx= mx;
2639                                         data->draglastx= mx;
2640                                 }
2641                                 else {
2642                                         data->dragstartx= my;
2643                                         data->draglastx= my;
2644                                 }
2645                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2646                                 retval= WM_UI_HANDLER_BREAK;
2647                         }
2648                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2649                                 click= 1;
2650                 }
2651         }
2652         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2653                 if(event->type == ESCKEY) {
2654                         data->cancel= 1;
2655                         data->escapecancel= 1;
2656                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2657                 }
2658                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2659                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2660                 }
2661                 else if(event->type == MOUSEMOVE) {
2662                         if(ui_numedit_but_SLI(but, data, 0, 0, (horizontal)? mx: my))
2663                                 ui_numedit_apply(C, block, but, data);
2664                 }
2665
2666                 retval= WM_UI_HANDLER_BREAK;
2667         }
2668         
2669         return retval;
2670 }
2671
2672
2673 static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2674 {
2675         
2676         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2677                 
2678                 /* first handle click on icondrag type button */
2679                 if(event->type==LEFTMOUSE && but->dragpoin && event->val==KM_PRESS) {
2680                         if(ui_but_mouse_inside_icon(but, data->region, event)) {
2681                                 button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
2682                                 data->dragstartx= event->x;
2683                                 data->dragstarty= event->y;
2684                                 return WM_UI_HANDLER_BREAK;
2685                         }
2686                 }
2687                 
2688                 /* regular open menu */
2689                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2690                         button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
2691                         return WM_UI_HANDLER_BREAK;
2692                 }
2693                 else if(ELEM3(but->type, MENU, ICONROW, ICONTEXTROW)) {
2694                         
2695                         if(event->type == WHEELDOWNMOUSE && event->alt) {
2696                                 data->value= ui_step_name_menu(but, -1);
2697                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2698                                 ui_apply_button(C, but->block, but, data, 1);
2699                                 return WM_UI_HANDLER_BREAK;
2700                         }
2701                         else if(event->type == WHEELUPMOUSE && event->alt) {
2702                                 data->value= ui_step_name_menu(but, 1);
2703                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2704                                 ui_apply_button(C, but->block, but, data, 1);
2705                                 return WM_UI_HANDLER_BREAK;
2706                         }
2707                 }
2708                 else if(but->type==COL) {
2709                         if( ELEM(event->type, WHEELDOWNMOUSE, WHEELUPMOUSE) && event->alt) {
2710                                 float col[3];
2711                                 
2712                                 ui_get_but_vectorf(but, col);
2713                                 rgb_to_hsv(col[0], col[1], col[2], but->hsv, but->hsv+1, but->hsv+2);
2714
2715                                 if(event->type==WHEELDOWNMOUSE)
2716                                         but->hsv[2]= CLAMPIS(but->hsv[2]-0.05f, 0.0f, 1.0f);
2717                                 else
2718                                         but->hsv[2]= CLAMPIS(but->hsv[2]+0.05f, 0.0f, 1.0f);
2719                                 
2720                                 hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], data->vec, data->vec+1, data->vec+2);
2721                                 ui_set_but_vectorf(but, data->vec);
2722                                 
2723                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2724                                 ui_apply_button(C, but->block, but, data, 1);
2725                                 return WM_UI_HANDLER_BREAK;
2726                         }
2727                 }
2728         }
2729         else if(data->state == BUTTON_STATE_WAIT_DRAG) {
2730                 
2731                 /* this function also ends state */
2732                 if(ui_but_start_drag(C, but, data, event)) {
2733                         return WM_UI_HANDLER_BREAK;
2734                 }
2735                 
2736                 /* outside icon quit, not needed if drag activated */
2737                 if(0==ui_but_mouse_inside_icon(but, data->region, event)) {
2738                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2739                         data->cancel= 1;
2740                         return WM_UI_HANDLER_BREAK;
2741                 }
2742                 
2743                 if(event->type==LEFTMOUSE && event->val==KM_RELEASE) {
2744                         button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
2745                         return WM_UI_HANDLER_BREAK;
2746                 }
2747
2748         }
2749
2750         return WM_UI_HANDLER_CONTINUE;
2751 }
2752
2753 static int ui_numedit_but_NORMAL(uiBut *but, uiHandleButtonData *data, int mx, int my)
2754 {
2755         float dx, dy, rad, radsq, mrad, *fp;
2756         int mdx, mdy, changed= 1;
2757         
2758         /* button is presumed square */
2759         /* if mouse moves outside of sphere, it does negative normal */
2760
2761         fp= data->origvec;
2762         rad= (but->x2 - but->x1);
2763         radsq= rad*rad;
2764         
2765         if(fp[2]>0.0f) {
2766                 mdx= (rad*fp[0]);
2767                 mdy= (rad*fp[1]);
2768         }
2769         else if(fp[2]> -1.0f) {
2770                 mrad= rad/sqrt(fp[0]*fp[0] + fp[1]*fp[1]);
2771                 
2772                 mdx= 2.0f*mrad*fp[0] - (rad*fp[0]);
2773                 mdy= 2.0f*mrad*fp[1] - (rad*fp[1]);
2774         }
2775         else mdx= mdy= 0;
2776         
2777         dx= (float)(mx+mdx-data->dragstartx);
2778         dy= (float)(my+mdy-data->dragstarty);
2779
2780         fp= data->vec;
2781         mrad= dx*dx+dy*dy;
2782         if(mrad < radsq) {      /* inner circle */
2783                 fp[0]= dx;
2784                 fp[1]= dy;
2785                 fp[2]= sqrt( radsq-dx*dx-dy*dy );
2786         }
2787         else {  /* outer circle */
2788                 
2789                 mrad= rad/sqrt(mrad);   // veclen
2790                 
2791                 dx*= (2.0f*mrad - 1.0f);
2792                 dy*= (2.0f*mrad - 1.0f);
2793                 
2794                 mrad= dx*dx+dy*dy;
2795                 if(mrad < radsq) {
2796                         fp[0]= dx;
2797                         fp[1]= dy;
2798                         fp[2]= -sqrt( radsq-dx*dx-dy*dy );
2799                 }
2800         }
2801         normalize_v3(fp);
2802
2803         data->draglastx= mx;
2804         data->draglasty= my;
2805
2806         return changed;
2807 }
2808
2809 static int ui_do_but_NORMAL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2810 {
2811         int mx, my;
2812
2813         mx= event->x;
2814         my= event->y;
2815         ui_window_to_block(data->region, block, &mx, &my);
2816
2817         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2818                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2819                         data->dragstartx= mx;
2820                         data->dragstarty= my;
2821                         data->draglastx= mx;
2822                         data->draglasty= my;
2823                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2824
2825                         /* also do drag the first time */
2826                         if(ui_numedit_but_NORMAL(but, data, mx, my))
2827                                 ui_numedit_apply(C, block, but, data);
2828                         
2829                         return WM_UI_HANDLER_BREAK;
2830                 }
2831         }
2832         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2833                 if(event->type == MOUSEMOVE) {
2834                         if(mx!=data->draglastx || my!=data->draglasty) {
2835                                 if(ui_numedit_but_NORMAL(but, data, mx, my))
2836                                         ui_numedit_apply(C, block, but, data);
2837                         }
2838                 }
2839                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2840                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2841
2842                 return WM_UI_HANDLER_BREAK;
2843         }
2844         
2845         return WM_UI_HANDLER_CONTINUE;
2846 }
2847
2848 static int ui_numedit_but_HSVCUBE(uiBut *but, uiHandleButtonData *data, int mx, int my)
2849 {
2850         float rgb[3], hsv[3];
2851         float x, y;
2852         int changed= 1;
2853         int color_profile = but->block->color_profile;
2854         
2855         if (but->rnaprop) {
2856                 if (RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA)
2857                         color_profile = BLI_PR_NONE;
2858         }
2859         
2860         ui_get_but_vectorf(but, rgb);
2861         rgb_to_hsv(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
2862                 
2863         /* relative position within box */
2864         x= ((float)mx-but->x1)/(but->x2-but->x1);
2865         y= ((float)my-but->y1)/(but->y2-but->y1);
2866         CLAMP(x, 0.0, 1.0);
2867         CLAMP(y, 0.0, 1.0);
2868         
2869         if(but->a1==0) {
2870                 hsv[2]= x; 
2871                 hsv[1]= y; 
2872         }
2873         else if(but->a1==1) {
2874                 hsv[0]= x;                              
2875                 hsv[2]= y;                              
2876         }
2877         else if(but->a1==2) {
2878                 hsv[0]= x; 
2879                 hsv[1]= y; 
2880         }
2881         else if(but->a1==3) {
2882                 hsv[0]= x; 
2883         }
2884         else if(but->a1==4) {
2885                 hsv[1]= x; 
2886         }
2887         else if(but->a1==5) {
2888                 hsv[2]= x; 
2889         }
2890         else if (but->a1==9){
2891                 float range;
2892                 
2893                 /* vertical 'value' strip */
2894                 hsv[2]= y; 
2895                 
2896                 /* exception only for value strip - use the range set in but->min/max */
2897                 range = but->softmax - but->softmin;
2898                 hsv[2] = y*range + but->softmin;
2899                 
2900                 if (color_profile)
2901                         hsv[2] = srgb_to_linearrgb(hsv[2]);
2902                 
2903         }
2904
2905         hsv_to_rgb(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2);
2906         ui_set_but_vectorf(but, rgb);
2907
2908         data->draglastx= mx;
2909         data->draglasty= my;
2910
2911         return changed;
2912 }
2913
2914 static int ui_do_but_HSVCUBE(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2915 {
2916         int mx, my;
2917
2918         mx= event->x;
2919         my= event->y;
2920         ui_window_to_block(data->region, block, &mx, &my);