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