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