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