4e3ffad48aec26b77790c6421baf6e5f9c7553e5
[blender-staging.git] / source / blender / editors / interface / interface_handlers.c
1 /**
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  * 
21  * Contributor(s): Blender Foundation
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 #include <float.h>
27 #include <math.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "DNA_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 (ui_mouse_inside_button(data->region, but, mx, my)) {
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 == LEFTMOUSE && event->val==KM_PRESS) {
2003                         /* only cancel if click outside the button */
2004                         if(ui_mouse_inside_button(but->active->region, but, event->x, event->y) == 0) {
2005                                 /* data->cancel doesnt work, this button opens immediate */
2006                                 if(but->flag & UI_BUT_IMMEDIATE)
2007                                         ui_set_but_val(but, 0);
2008                                 else
2009                                         data->cancel= 1;
2010                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2011                                 return WM_UI_HANDLER_BREAK;
2012                         }
2013                 }
2014                 
2015                 /* always set */
2016                 but->modifier_key = 0;
2017                 if(event->shift)
2018                         but->modifier_key |= KM_SHIFT;
2019                 if(event->alt)
2020                         but->modifier_key |= KM_ALT;
2021                 if(event->ctrl)
2022                         but->modifier_key |= KM_CTRL;
2023                 if(event->oskey)
2024                         but->modifier_key |= KM_OSKEY;
2025                 
2026                 ui_check_but(but);
2027                 ED_region_tag_redraw(data->region);
2028                         
2029                 if(event->val==KM_PRESS) {
2030                         if(ISHOTKEY(event->type)) { 
2031                                 
2032                                 if(WM_key_event_string(event->type)[0])
2033                                         ui_set_but_val(but, event->type);
2034                                 else
2035                                         data->cancel= 1;
2036                                 
2037                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2038                                 return WM_UI_HANDLER_BREAK;
2039                         }
2040                 }
2041         }
2042         
2043         return WM_UI_HANDLER_CONTINUE;
2044 }
2045
2046 static int ui_do_but_KEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2047 {
2048         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2049                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2050                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
2051                         return WM_UI_HANDLER_BREAK;
2052                 }
2053         }
2054         else if(data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2055                 if(event->type == MOUSEMOVE)
2056                         return WM_UI_HANDLER_CONTINUE;
2057
2058                 if(event->val==KM_PRESS) {
2059                         if(WM_key_event_string(event->type)[0])
2060                                 ui_set_but_val(but, event->type);
2061                         else
2062                                 data->cancel= 1;
2063
2064                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2065                 }
2066         }
2067
2068         return WM_UI_HANDLER_CONTINUE;
2069 }
2070
2071 static int ui_do_but_TEX(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2072 {
2073         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2074                 if(ELEM(event->type, LEFTMOUSE, EVT_BUT_OPEN) && event->val==KM_PRESS) {
2075                         if(but->dt == UI_EMBOSSN && !event->ctrl);
2076                         else {
2077                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2078                                 return WM_UI_HANDLER_BREAK;
2079                         }
2080                 }
2081         }
2082         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2083                 ui_do_but_textedit(C, block, but, data, event);
2084                 return WM_UI_HANDLER_BREAK;
2085         }
2086         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2087                 ui_do_but_textedit_select(C, block, but, data, event);
2088                 return WM_UI_HANDLER_BREAK;
2089         }
2090
2091         return WM_UI_HANDLER_CONTINUE;
2092 }
2093
2094 static int ui_do_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2095 {
2096         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2097                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2098                         data->togdual= event->ctrl;
2099                         data->togonly= !event->shift;
2100                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2101                         return WM_UI_HANDLER_BREAK;
2102                 }
2103         }
2104         return WM_UI_HANDLER_CONTINUE;
2105 }
2106
2107 static int ui_do_but_EXIT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2108 {
2109         
2110         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2111
2112                 /* first handle click on icondrag type button */
2113                 if(event->type==LEFTMOUSE && but->dragpoin) {
2114                         if(ui_but_mouse_inside_icon(but, data->region, event)) {
2115                                 
2116                                 /* tell the button to wait and keep checking further events to
2117                                  * see if it should start dragging */
2118                                 button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
2119                                 data->dragstartx= event->x;
2120                                 data->dragstarty= event->y;
2121                                 return WM_UI_HANDLER_CONTINUE;
2122                         }
2123                 }
2124                 
2125                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2126                         int ret = WM_UI_HANDLER_BREAK;
2127                         /* XXX (a bit ugly) Special case handling for filebrowser drag button */
2128                         if(but->dragpoin && but->imb && ui_but_mouse_inside_icon(but, data->region, event)) {
2129                                 ret = WM_UI_HANDLER_CONTINUE;
2130                         }
2131                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2132                         return ret;
2133                 }
2134         }
2135         else if(data->state == BUTTON_STATE_WAIT_DRAG) {
2136                 
2137                 /* this function also ends state */
2138                 if(ui_but_start_drag(C, but, data, event)) {
2139                         return WM_UI_HANDLER_BREAK;
2140                 }
2141                 
2142                 /* If the mouse has been pressed and released, getting to 
2143                  * this point without triggering a drag, then clear the 
2144                  * drag state for this button and continue to pass on the event */
2145                 if(event->type==LEFTMOUSE && event->val==KM_RELEASE) {
2146                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2147                         return WM_UI_HANDLER_CONTINUE;
2148                 }
2149                 
2150                 /* while waiting for a drag to be triggered, always block 
2151                  * other events from getting handled */
2152                 return WM_UI_HANDLER_BREAK;
2153         }
2154         
2155         return WM_UI_HANDLER_CONTINUE;
2156 }
2157
2158 /* var names match ui_numedit_but_NUM */
2159 static float ui_numedit_apply_snapf(float tempf, float softmin, float softmax, float softrange, int snap)
2160 {
2161         if(tempf==softmin || tempf==softmax)
2162                 return tempf;
2163
2164         switch(snap) {
2165         case 0:
2166                 break;
2167         case 1:
2168                 if(tempf==softmin || tempf==softmax) { }
2169                 else if(softrange < 2.10) tempf= 0.1*floor(10*tempf);
2170                 else if(softrange < 21.0) tempf= floor(tempf);
2171                 else tempf= 10.0*floor(tempf/10.0);
2172                 break;
2173         case 2:
2174                 if(tempf==softmin || tempf==softmax) { }
2175                 else if(softrange < 2.10) tempf= 0.01*floor(100.0*tempf);
2176                 else if(softrange < 21.0) tempf= 0.1*floor(10.0*tempf);
2177                 else tempf= floor(tempf);
2178                 break;
2179         }
2180
2181         return tempf;
2182 }
2183
2184 static float ui_numedit_apply_snap(int temp, float softmin, float softmax, int snap)
2185 {
2186         if(temp==softmin || temp==softmax)
2187                 return temp;
2188
2189         switch(snap) {
2190         case 0:
2191                 break;
2192         case 1:
2193                 temp= 10*(temp/10);
2194                 break;
2195         case 2:
2196                 temp= 100*(temp/100);
2197                 break;
2198         }
2199
2200         return temp;
2201 }
2202
2203 static int ui_numedit_but_NUM(uiBut *but, uiHandleButtonData *data, float fac, int snap, int mx)
2204 {
2205         float deler, tempf, softmin, softmax, softrange;
2206         int lvalue, temp, changed= 0;
2207         
2208         if(mx == data->draglastx)
2209                 return changed;
2210         
2211         /* drag-lock - prevent unwanted scroll adjustments */
2212         /* change value (now 3) to adjust threshold in pixels */
2213         if(data->draglock) {
2214                 if(abs(mx-data->dragstartx) <= 3)
2215                         return changed;
2216
2217                 data->draglock= 0;
2218                 data->dragstartx= mx;  /* ignore mouse movement within drag-lock */
2219         }
2220
2221         softmin= but->softmin;
2222         softmax= but->softmax;
2223         softrange= softmax - softmin;
2224
2225
2226         if(ui_is_a_warp_but(but)) {
2227                 /* Mouse location isn't screen clamped to the screen so use a linear mapping
2228                  * 2px == 1-int, or 1px == 1-ClickStep */
2229                 if(ui_is_but_float(but)) {
2230                         fac *= 0.01*but->a1;
2231                         tempf = data->startvalue + ((mx - data->dragstartx) * fac);
2232                         tempf= ui_numedit_apply_snapf(tempf, softmin, softmax, softrange, snap);
2233
2234 #if 1           /* fake moving the click start, nicer for dragging back after passing the limit */
2235                         if(tempf < softmin) {
2236                                 data->dragstartx -= (softmin-tempf) / fac;
2237                                 tempf= softmin;
2238                         } else if (tempf > softmax) {
2239                                 data->dragstartx += (tempf-softmax) / fac;
2240                                 tempf= softmax;
2241                         }
2242 #else
2243                         CLAMP(tempf, softmin, softmax);
2244 #endif
2245
2246                         if(tempf != data->value) {
2247                                 data->dragchange= 1;
2248                                 data->value= tempf;
2249                                 changed= 1;
2250                         }
2251                 }
2252                 else {
2253                         if(softrange > 256)             fac= 1.0;               /* 1px == 1 */
2254                         else if(softrange > 32) fac= 1.0/2.0;   /* 2px == 1 */
2255                         else                                    fac= 1.0/16.0;  /* 16px == 1? */
2256
2257                         temp= data->startvalue + ((mx - data->dragstartx) * fac);
2258                         temp= ui_numedit_apply_snap(temp, softmin, softmax, snap);
2259
2260 #if 1           /* fake moving the click start, nicer for dragging back after passing the limit */
2261                         if(temp < softmin) {
2262                                 data->dragstartx -= (softmin-temp) / fac;
2263                                 temp= softmin;
2264                         } else if (temp > softmax) {
2265                                 data->dragstartx += (temp-softmax) / fac;
2266                                 temp= softmax;
2267                         }
2268 #else
2269                         CLAMP(temp, softmin, softmax);
2270 #endif
2271
2272                         if(temp != data->value) {
2273                                 data->dragchange= 1;
2274                                 data->value= temp;
2275                                 changed= 1;
2276                         }
2277                 }
2278
2279                 data->draglastx= mx;
2280         }
2281         else {
2282                 /* Use a non-linear mapping of the mouse drag especially for large floats (normal behavior) */
2283                 deler= 500;
2284                 if(!ui_is_but_float(but)) {
2285                         if((softrange)<100) deler= 200.0;
2286                         if((softrange)<25) deler= 50.0;
2287                 }
2288                 deler /= fac;
2289
2290                 if(ui_is_but_float(but) && softrange > 11) {
2291                         /* non linear change in mouse input- good for high precicsion */
2292                         data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.002);
2293                 } else if (!ui_is_but_float(but) && softrange > 129) { /* only scale large int buttons */
2294                         /* non linear change in mouse input- good for high precicsionm ints need less fine tuning */
2295                         data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.004);
2296                 } else {
2297                         /*no scaling */
2298                         data->dragf+= ((float)(mx-data->draglastx))/deler ;
2299                 }
2300         
2301                 if(data->dragf>1.0) data->dragf= 1.0;
2302                 if(data->dragf<0.0) data->dragf= 0.0;
2303                 data->draglastx= mx;
2304                 tempf= (softmin + data->dragf*softrange);
2305
2306
2307                 if(!ui_is_but_float(but)) {
2308                         temp= floor(tempf+.5);
2309
2310                         temp= ui_numedit_apply_snap(temp, softmin, softmax, snap);
2311
2312                         CLAMP(temp, softmin, softmax);
2313                         lvalue= (int)data->value;
2314
2315                         if(temp != lvalue) {
2316                                 data->dragchange= 1;
2317                                 data->value= (double)temp;
2318                                 changed= 1;
2319                         }
2320                 }
2321                 else {
2322                         temp= 0;
2323                         tempf= ui_numedit_apply_snapf(tempf, softmin, softmax, softrange, snap);
2324
2325                         CLAMP(tempf, softmin, softmax);
2326
2327                         if(tempf != data->value) {
2328                                 data->dragchange= 1;
2329                                 data->value= tempf;
2330                                 changed= 1;
2331                         }
2332                 }
2333         }
2334
2335
2336         return changed;
2337 }
2338
2339 static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2340 {
2341         int mx, my;     /* mouse location scaled to fit the UI */
2342         int screen_mx, screen_my; /* mouse location kept at screen pixel coords */
2343         int click= 0;
2344         int retval= WM_UI_HANDLER_CONTINUE;
2345         
2346         mx= screen_mx= event->x;
2347         my= screen_my= event->y;
2348
2349         ui_window_to_block(data->region, block, &mx, &my);
2350
2351         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2352                 /* XXX hardcoded keymap check.... */
2353                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2354                         mx= but->x1;
2355                         click= 1;
2356                 }
2357                 else if(event->type == WHEELUPMOUSE && event->alt) {
2358                         mx= but->x2;
2359                         click= 1;
2360                 }
2361                 else if(event->val==KM_PRESS) {
2362                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2363                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2364                                 retval= WM_UI_HANDLER_BREAK;
2365                         }
2366                         else if(event->type == LEFTMOUSE) {
2367                                 data->dragstartx= data->draglastx= ui_is_a_warp_but(but) ? screen_mx:mx;
2368                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2369                                 retval= WM_UI_HANDLER_BREAK;
2370                         }
2371                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2372                                 click= 1;
2373                         else if (event->type == MINUSKEY && event->val==KM_PRESS) {
2374                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2375                                 data->value = -data->value;
2376                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2377                                 retval= WM_UI_HANDLER_BREAK;
2378                         }
2379                 }
2380                 
2381         }
2382         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2383                 if(event->type == ESCKEY) {
2384                         data->cancel= 1;
2385                         data->escapecancel= 1;
2386                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2387                 }
2388                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2389                         if(data->dragchange)
2390                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2391                         else
2392                                 click= 1;
2393                 }
2394                 else if(event->type == MOUSEMOVE) {
2395                         float fac;
2396                         int snap;
2397
2398                         fac= 1.0f;
2399                         if(event->shift) fac /= 10.0f;
2400                         if(event->alt) fac /= 20.0f;
2401                         
2402                         snap= (event->ctrl)? (event->shift)? 2: 1: 0;
2403
2404                         if(ui_numedit_but_NUM(but, data, fac, snap, (ui_is_a_warp_but(but) ? screen_mx:mx)))
2405                                 ui_numedit_apply(C, block, but, data);
2406                 }
2407                 retval= WM_UI_HANDLER_BREAK;
2408         }
2409         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2410                 ui_do_but_textedit(C, block, but, data, event);
2411                 retval= WM_UI_HANDLER_BREAK;
2412         }
2413         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2414                 ui_do_but_textedit_select(C, block, but, data, event);
2415                 retval= WM_UI_HANDLER_BREAK;
2416         }
2417         
2418         if(click) {
2419                 /* we can click on the side arrows to increment/decrement,
2420                  * or click inside to edit the value directly */
2421                 float tempf, softmin, softmax;
2422                 int temp;
2423
2424                 softmin= but->softmin;
2425                 softmax= but->softmax;
2426
2427                 if(!ui_is_but_float(but)) {
2428                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2429                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2430
2431                                 temp= (int)data->value - 1;
2432                                 if(temp>=softmin && temp<=softmax)
2433                                         data->value= (double)temp;
2434                                 else
2435                                         data->cancel= 1;
2436
2437                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2438                         }
2439                         else if(mx > (but->x1 + (2*(but->x2 - but->x1)/3) + 3)) {
2440                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2441
2442                                 temp= (int)data->value + 1;
2443                                 if(temp>=softmin && temp<=softmax)
2444                                         data->value= (double)temp;
2445                                 else
2446                                         data->cancel= 1;
2447
2448                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2449                         }
2450                         else
2451                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2452                 }
2453                 else {
2454                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2455                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2456
2457                                 tempf= data->value - 0.01*but->a1;
2458                                 if (tempf < softmin) tempf = softmin;
2459                                 data->value= tempf;
2460
2461                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2462                         }
2463                         else if(mx > but->x1 + (2*((but->x2 - but->x1)/3) + 3)) {
2464                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2465
2466                                 tempf= data->value + 0.01*but->a1;
2467                                 if (tempf > softmax) tempf = softmax;
2468                                 data->value= tempf;
2469
2470                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2471                         }
2472                         else
2473                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2474                 }
2475
2476                 retval= WM_UI_HANDLER_BREAK;
2477         }
2478         
2479         return retval;
2480 }
2481
2482 static int ui_numedit_but_SLI(uiBut *but, uiHandleButtonData *data, int shift, int ctrl, int mx)
2483 {
2484         float deler, f, tempf, softmin, softmax, softrange;
2485         int temp, lvalue, changed= 0;
2486
2487         softmin= but->softmin;
2488         softmax= but->softmax;
2489         softrange= softmax - softmin;
2490
2491         if(but->type==NUMSLI) deler= ((but->x2-but->x1) - 5.0*but->aspect);
2492         else if(but->type==HSVSLI) deler= ((but->x2-but->x1)/2 - 5.0*but->aspect);
2493         else if(but->type==SCROLL) {
2494                 int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2495                 float size= (horizontal)? (but->x2-but->x1): -(but->y2-but->y1);
2496                 deler= size*(but->softmax - but->softmin)/(but->softmax - but->softmin + but->a1);
2497         }
2498         else deler= (but->x2-but->x1- 5.0*but->aspect);
2499
2500         f= (float)(mx-data->dragstartx)/deler + data->dragfstart;
2501         
2502         if(shift)
2503                 f= (f-data->dragfstart)/10.0 + data->dragfstart;
2504
2505         CLAMP(f, 0.0, 1.0);
2506         tempf= softmin + f*softrange;
2507         temp= floor(tempf+.5);
2508
2509         if(ctrl) {
2510                 if(tempf==softmin || tempf==softmax);
2511                 else if(ui_is_but_float(but)) {
2512
2513                         if(shift) {
2514                                 if(tempf==softmin || tempf==softmax);
2515                                 else if(softmax-softmin < 2.10) tempf= 0.01*floor(100.0*tempf);
2516                                 else if(softmax-softmin < 21.0) tempf= 0.1*floor(10.0*tempf);
2517                                 else tempf= floor(tempf);
2518                         }
2519                         else {
2520                                 if(softmax-softmin < 2.10) tempf= 0.1*floor(10*tempf);
2521                                 else if(softmax-softmin < 21.0) tempf= floor(tempf);
2522                                 else tempf= 10.0*floor(tempf/10.0);
2523                         }
2524                 }
2525                 else {
2526                         temp= 10*(temp/10);
2527                         tempf= temp;
2528                 }
2529         }
2530
2531         if(!ui_is_but_float(but)) {
2532                 lvalue= floor(data->value+0.5);
2533
2534                 CLAMP(temp, softmin, softmax);
2535
2536                 if(temp != lvalue) {
2537                         data->value= temp;
2538                         data->dragchange= 1;
2539                         changed= 1;
2540                 }
2541         }
2542         else {
2543                 CLAMP(tempf, softmin, softmax);
2544
2545                 if(tempf != data->value) {
2546                         data->value= tempf;
2547                         data->dragchange= 1;
2548                         changed= 1;
2549                 }
2550         }
2551
2552         return changed;
2553 }
2554
2555 static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2556 {
2557         int mx, my, click= 0;
2558         int retval= WM_UI_HANDLER_CONTINUE;
2559
2560         mx= event->x;
2561         my= event->y;
2562         ui_window_to_block(data->region, block, &mx, &my);
2563
2564         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2565                 /* XXX hardcoded keymap check.... */
2566                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2567                         mx= but->x1;
2568                         click= 2;
2569                 }
2570                 else if(event->type == WHEELUPMOUSE && event->alt) {
2571                         mx= but->x2;
2572                         click= 2;
2573                 }
2574                 else if(event->val==KM_PRESS) {
2575                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2576                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2577                                 retval= WM_UI_HANDLER_BREAK;
2578                         }
2579                         /* alt-click on sides to get "arrows" like in NUM buttons, and match wheel usage above */
2580                         else if(event->type == LEFTMOUSE && event->alt) {
2581                                 int halfpos = (but->x1 + but->x2) / 2;
2582                                 click = 2;
2583                                 if (mx < halfpos)
2584                                         mx = but->x1;
2585                                 else
2586                                         mx = but->x2;
2587                         }
2588                         else if(event->type == LEFTMOUSE) {
2589                                 data->dragstartx= mx;
2590                                 data->draglastx= mx;
2591                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2592                                 retval= WM_UI_HANDLER_BREAK;
2593                         }
2594                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2595                                 click= 1;
2596                         else if (event->type == MINUSKEY && event->val==KM_PRESS) {
2597                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2598                                 data->value = -data->value;
2599                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2600                                 retval= WM_UI_HANDLER_BREAK;
2601                         }
2602                 }
2603         }
2604         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2605                 if(event->type == ESCKEY) {
2606                         data->cancel= 1;
2607                         data->escapecancel= 1;
2608                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2609                 }
2610                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2611                         if(data->dragchange)
2612                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2613                         else
2614                                 click= 1;
2615                 }
2616                 else if(event->type == MOUSEMOVE) {
2617                         if(ui_numedit_but_SLI(but, data, event->shift, event->ctrl, mx))
2618                                 ui_numedit_apply(C, block, but, data);
2619                 }
2620                 retval= WM_UI_HANDLER_BREAK;
2621         }
2622         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2623                 ui_do_but_textedit(C, block, but, data, event);
2624                 retval= WM_UI_HANDLER_BREAK;
2625         }
2626         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2627                 ui_do_but_textedit_select(C, block, but, data, event);
2628                 retval= WM_UI_HANDLER_BREAK;
2629         }
2630
2631         if(click) {
2632                 if (click==2) {
2633                         /* nudge slider to the left or right */
2634                         float f, tempf, softmin, softmax, softrange;
2635                         int temp;
2636                         
2637                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2638                         
2639                         softmin= but->softmin;
2640                         softmax= but->softmax;
2641                         softrange= softmax - softmin;
2642                         
2643                         tempf= data->value;
2644                         temp= (int)data->value;
2645                         
2646                         /* XXX useles "if", same result for f, uh??? */
2647                         if(but->type==SLI) f= (float)(mx-but->x1)/(but->x2-but->x1);
2648                         else f= (float)(mx- but->x1)/(but->x2-but->x1);
2649                         
2650                         f= softmin + f*softrange;
2651                         
2652                         if(!ui_is_but_float(but)) {
2653                                 if(f<temp) temp--;
2654                                 else temp++;
2655                                 
2656                                 if(temp>=softmin && temp<=softmax)
2657                                         data->value= temp;
2658                                 else
2659                                         data->cancel= 1;
2660                         } 
2661                         else {
2662                                 if(f<tempf) tempf-=.01;
2663                                 else tempf+=.01;
2664                                 
2665                                 if(tempf>=softmin && tempf<=softmax)
2666                                         data->value= tempf;
2667                                 else
2668                                         data->cancel= 1;
2669                         }
2670                         
2671                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2672                         retval= WM_UI_HANDLER_BREAK;
2673                 }
2674                 else {
2675                         /* edit the value directly */
2676                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2677                         retval= WM_UI_HANDLER_BREAK;
2678                 }
2679         }
2680         
2681         return retval;
2682 }
2683
2684 static int ui_do_but_SCROLL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2685 {
2686         int mx, my, click= 0;
2687         int retval= WM_UI_HANDLER_CONTINUE;
2688         int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2689         
2690         mx= event->x;
2691         my= event->y;
2692         ui_window_to_block(data->region, block, &mx, &my);
2693
2694         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2695                 if(event->val==KM_PRESS) {
2696                         if(event->type == LEFTMOUSE) {
2697                                 if(horizontal) {
2698                                         data->dragstartx= mx;
2699                                         data->draglastx= mx;
2700                                 }
2701                                 else {
2702                                         data->dragstartx= my;
2703                                         data->draglastx= my;
2704                                 }
2705                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2706                                 retval= WM_UI_HANDLER_BREAK;
2707                         }
2708                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2709                                 click= 1;
2710                 }
2711         }
2712         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2713                 if(event->type == ESCKEY) {
2714                         data->cancel= 1;
2715                         data->escapecancel= 1;
2716                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2717                 }
2718                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2719                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2720                 }
2721                 else if(event->type == MOUSEMOVE) {
2722                         if(ui_numedit_but_SLI(but, data, 0, 0, (horizontal)? mx: my))
2723                                 ui_numedit_apply(C, block, but, data);
2724                 }
2725
2726                 retval= WM_UI_HANDLER_BREAK;
2727         }
2728         
2729         return retval;
2730 }
2731
2732
2733 static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2734 {
2735         
2736         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2737                 
2738                 /* first handle click on icondrag type button */
2739                 if(event->type==LEFTMOUSE && but->dragpoin && event->val==KM_PRESS) {
2740                         if(ui_but_mouse_inside_icon(but, data->region, event)) {
2741                                 button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
2742                                 data->dragstartx= event->x;
2743                                 data->dragstarty= event->y;
2744                                 return WM_UI_HANDLER_BREAK;
2745                         }
2746                 }
2747                 
2748                 /* regular open menu */
2749                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2750                         button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
2751                         return WM_UI_HANDLER_BREAK;
2752                 }
2753                 else if(ELEM3(but->type, MENU, ICONROW, ICONTEXTROW)) {
2754                         
2755                         if(event->type == WHEELDOWNMOUSE && event->alt) {
2756                                 data->value= ui_step_name_menu(but, -1);
2757                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2758                                 ui_apply_button(C, but->block, but, data, 1);
2759                                 return WM_UI_HANDLER_BREAK;
2760                         }
2761                         else if(event->type == WHEELUPMOUSE && event->alt) {
2762                                 data->value= ui_step_name_menu(but, 1);
2763                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2764                                 ui_apply_button(C, but->block, but, data, 1);
2765                                 return WM_UI_HANDLER_BREAK;
2766                         }
2767                 }
2768                 else if(but->type==COL) {
2769                         if( ELEM(event->type, WHEELDOWNMOUSE, WHEELUPMOUSE) && event->alt) {
2770                                 float col[3];
2771                                 
2772                                 ui_get_but_vectorf(but, col);
2773                                 rgb_to_hsv(col[0], col[1], col[2], but->hsv, but->hsv+1, but->hsv+2);
2774
2775                                 if(event->type==WHEELDOWNMOUSE)
2776                                         but->hsv[2]= CLAMPIS(but->hsv[2]-0.05f, 0.0f, 1.0f);
2777                                 else
2778                                         but->hsv[2]= CLAMPIS(but->hsv[2]+0.05f, 0.0f, 1.0f);
2779                                 
2780                                 hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], data->vec, data->vec+1, data->vec+2);
2781                                 ui_set_but_vectorf(but, data->vec);
2782                                 
2783                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2784                                 ui_apply_button(C, but->block, but, data, 1);
2785                                 return WM_UI_HANDLER_BREAK;
2786                         }
2787                 }
2788         }
2789         else if(data->state == BUTTON_STATE_WAIT_DRAG) {
2790                 
2791                 /* this function also ends state */
2792                 if(ui_but_start_drag(C, but, data, event)) {
2793                         return WM_UI_HANDLER_BREAK;
2794                 }
2795                 
2796                 /* outside icon quit, not needed if drag activated */
2797                 if(0==ui_but_mouse_inside_icon(but, data->region, event)) {
2798                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2799                         data->cancel= 1;
2800                         return WM_UI_HANDLER_BREAK;
2801                 }
2802                 
2803                 if(event->type==LEFTMOUSE && event->val==KM_RELEASE) {
2804                         button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
2805                         return WM_UI_HANDLER_BREAK;
2806                 }
2807
2808         }
2809
2810         return WM_UI_HANDLER_CONTINUE;
2811 }
2812
2813 static int ui_numedit_but_NORMAL(uiBut *but, uiHandleButtonData *data, int mx, int my)
2814 {
2815         float dx, dy, rad, radsq, mrad, *fp;
2816         int mdx, mdy, changed= 1;
2817         
2818         /* button is presumed square */
2819         /* if mouse moves outside of sphere, it does negative normal */
2820
2821         fp= data->origvec;
2822         rad= (but->x2 - but->x1);
2823         radsq= rad*rad;
2824         
2825         if(fp[2]>0.0f) {
2826                 mdx= (rad*fp[0]);
2827                 mdy= (rad*fp[1]);
2828         }
2829         else if(fp[2]> -1.0f) {
2830                 mrad= rad/sqrt(fp[0]*fp[0] + fp[1]*fp[1]);
2831                 
2832                 mdx= 2.0f*mrad*fp[0] - (rad*fp[0]);
2833                 mdy= 2.0f*mrad*fp[1] - (rad*fp[1]);
2834         }
2835         else mdx= mdy= 0;
2836         
2837         dx= (float)(mx+mdx-data->dragstartx);
2838         dy= (float)(my+mdy-data->dragstarty);
2839
2840         fp= data->vec;
2841         mrad= dx*dx+dy*dy;
2842         if(mrad < radsq) {      /* inner circle */
2843                 fp[0]= dx;
2844                 fp[1]= dy;
2845                 fp[2]= sqrt( radsq-dx*dx-dy*dy );
2846         }
2847         else {  /* outer circle */
2848                 
2849                 mrad= rad/sqrt(mrad);   // veclen
2850                 
2851                 dx*= (2.0f*mrad - 1.0f);
2852                 dy*= (2.0f*mrad - 1.0f);
2853                 
2854                 mrad= dx*dx+dy*dy;
2855                 if(mrad < radsq) {
2856                         fp[0]= dx;
2857                         fp[1]= dy;
2858                         fp[2]= -sqrt( radsq-dx*dx-dy*dy );
2859                 }
2860         }
2861         normalize_v3(fp);
2862
2863         data->draglastx= mx;
2864         data->draglasty= my;
2865
2866         return changed;
2867 }
2868
2869 static int ui_do_but_NORMAL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2870 {
2871         int mx, my;
2872
2873         mx= event->x;
2874         my= event->y;
2875         ui_window_to_block(data->region, block, &mx, &my);
2876
2877         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2878                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2879                         data->dragstartx= mx;
2880                         data->dragstarty= my;
2881                         data->draglastx= mx;
2882                         data->draglasty= my;
2883                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2884
2885                         /* also do drag the first time */
2886                         if(ui_numedit_but_NORMAL(but, data, mx, my))
2887                                 ui_numedit_apply(C, block, but, data);
2888                         
2889                         return WM_UI_HANDLER_BREAK;
2890                 }
2891         }
2892         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2893                 if(event->type == MOUSEMOVE) {
2894                         if(mx!=data->draglastx || my!=data->draglasty) {
2895                                 if(ui_numedit_but_NORMAL(but, data, mx, my))
2896                                         ui_numedit_apply(C, block, but, data);
2897                         }
2898                 }
2899                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2900                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2901
2902                 return WM_UI_HANDLER_BREAK;
2903         }
2904         
2905         return WM_UI_HANDLER_CONTINUE;
2906 }