5049fc0b130eb4568a76de511c8f3c889ba4e46b
[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
1227         if(data->searchbox)
1228                 ui_searchbox_autocomplete(C, data->searchbox, but, data->str);
1229         else
1230                 but->autocomplete_func(C, str, but->autofunc_arg);
1231
1232         but->pos= strlen(str);
1233         but->selsta= but->selend= but->pos;
1234
1235         return changed;
1236 }
1237
1238 static int ui_textedit_copypaste(uiBut *but, uiHandleButtonData *data, int paste, int copy, int cut)
1239 {
1240         char buf[UI_MAX_DRAW_STR]={0};
1241         char *str, *p, *pbuf;
1242         int len, x, y, i, changed= 0;
1243
1244         str= data->str;
1245         len= strlen(str);
1246         
1247         /* paste */
1248         if (paste) {
1249                 /* extract the first line from the clipboard */
1250                 p = pbuf= WM_clipboard_text_get(0);
1251
1252                 if(p && p[0]) {
1253                         i= 0;
1254                         while (*p && *p!='\r' && *p!='\n' && i<UI_MAX_DRAW_STR-1) {
1255                                 buf[i++]=*p;
1256                                 p++;
1257                         }
1258                         buf[i]= 0;
1259
1260                         /* paste over the current selection */
1261                         if ((but->selend - but->selsta) > 0)
1262                                 ui_textedit_delete_selection(but, data);
1263                         
1264                         for (y=0; y<strlen(buf); y++)
1265                         {
1266                                 /* add contents of buffer */
1267                                 if(len < data->maxlen) {
1268                                         for(x= data->maxlen; x>but->pos; x--)
1269                                                 str[x]= str[x-1];
1270                                         str[but->pos]= buf[y];
1271                                         but->pos++; 
1272                                         len++;
1273                                         str[len]= '\0';
1274                                 }
1275                         }
1276
1277                         changed= 1;
1278                 }
1279
1280                 if(pbuf)
1281                         MEM_freeN(pbuf);
1282         }
1283         /* cut & copy */
1284         else if (copy || cut) {
1285                 /* copy the contents to the copypaste buffer */
1286                 for(x= but->selsta; x <= but->selend; x++) {
1287                         if (x==but->selend)
1288                                 buf[x] = '\0';
1289                         else
1290                                 buf[(x - but->selsta)] = str[x];
1291                 }
1292
1293                 WM_clipboard_text_set(buf, 0);
1294                 
1295                 /* for cut only, delete the selection afterwards */
1296                 if(cut)
1297                         if((but->selend - but->selsta) > 0)
1298                                 changed= ui_textedit_delete_selection(but, data);
1299         } 
1300
1301         return changed;
1302 }
1303
1304 static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
1305 {
1306         if(data->str) {
1307                 MEM_freeN(data->str);
1308                 data->str= NULL;
1309         }
1310
1311         /* retrieve string */
1312         data->maxlen= ui_get_but_string_max_length(but);
1313         data->str= MEM_callocN(sizeof(char)*(data->maxlen+1), "textedit str");
1314         ui_get_but_string(but, data->str, data->maxlen+1);
1315
1316         data->origstr= BLI_strdup(data->str);
1317         data->selextend= 0;
1318         data->selstartx= 0;
1319
1320         /* set cursor pos to the end of the text */
1321         but->editstr= data->str;
1322         but->pos= strlen(data->str);
1323         but->selsta= 0;
1324         but->selend= strlen(but->drawstr) - strlen(but->str);
1325
1326         /* optional searchbox */
1327         if(but->type==SEARCH_MENU) {
1328                 data->searchbox= ui_searchbox_create(C, data->region, but);
1329                 ui_searchbox_update(C, data->searchbox, but, 1); /* 1= reset */
1330         }
1331         
1332         ui_check_but(but);
1333 }
1334
1335 static void ui_textedit_end(bContext *C, uiBut *but, uiHandleButtonData *data)
1336 {
1337         if(but) {
1338                 if(data->searchbox) {
1339                         if(data->cancel==0)
1340                                 ui_searchbox_apply(but, data->searchbox);
1341
1342                         ui_searchbox_free(C, data->searchbox);
1343                         data->searchbox= NULL;
1344                 }
1345                 
1346                 but->editstr= NULL;
1347                 but->pos= -1;
1348         }
1349 }
1350
1351 static void ui_textedit_next_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1352 {
1353         uiBut *but;
1354
1355         /* label and roundbox can overlap real buttons (backdrops...) */
1356         if(actbut->type==LABEL && actbut->type==ROUNDBOX)
1357                 return;
1358
1359         for(but= actbut->next; but; but= but->next) {
1360                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1361                         data->postbut= but;
1362                         data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1363                         return;
1364                 }
1365         }
1366         for(but= block->buttons.first; but!=actbut; but= but->next) {
1367                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1368                         data->postbut= but;
1369                         data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1370                         return;
1371                 }
1372         }
1373 }
1374
1375 static void ui_textedit_prev_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1376 {
1377         uiBut *but;
1378
1379         /* label and roundbox can overlap real buttons (backdrops...) */
1380         if(actbut->type==LABEL && actbut->type==ROUNDBOX)
1381                 return;
1382
1383         for(but= actbut->prev; but; but= but->prev) {
1384                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1385                         data->postbut= but;
1386                         data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1387                         return;
1388                 }
1389         }
1390         for(but= block->buttons.last; but!=actbut; but= but->prev) {
1391                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1392                         data->postbut= but;
1393                         data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1394                         return;
1395                 }
1396         }
1397 }
1398
1399
1400 static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1401 {
1402         int mx, my, changed= 0, inbox=0, retval= WM_UI_HANDLER_CONTINUE;
1403
1404         switch(event->type) {
1405                 case WHEELUPMOUSE:
1406                 case WHEELDOWNMOUSE:
1407                 case MOUSEMOVE:
1408                         if(data->searchbox)
1409                                 ui_searchbox_event(C, data->searchbox, but, event);
1410                         
1411                         break;
1412                 case RIGHTMOUSE:
1413                 case ESCKEY:
1414                         data->cancel= 1;
1415                         data->escapecancel= 1;
1416                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1417                         retval= WM_UI_HANDLER_BREAK;
1418                         break;
1419                 case LEFTMOUSE: {
1420                         
1421                         /* exit on LMB only on RELEASE for searchbox, to mimic other popups, and allow multiple menu levels */
1422                         if(data->searchbox)
1423                                 inbox= ui_searchbox_inside(data->searchbox, event->x, event->y);
1424
1425                         if(event->val==KM_PRESS) {
1426                                 mx= event->x;
1427                                 my= event->y;
1428                                 ui_window_to_block(data->region, block, &mx, &my);
1429
1430                                 if ((but->y1 <= my) && (my <= but->y2) && (but->x1 <= mx) && (mx <= but->x2)) {
1431                                         ui_textedit_set_cursor_pos(but, data, mx);
1432                                         but->selsta = but->selend = but->pos;
1433                                         data->selstartx= mx;
1434
1435                                         button_activate_state(C, but, BUTTON_STATE_TEXT_SELECTING);
1436                                         retval= WM_UI_HANDLER_BREAK;
1437                                 }
1438                                 else if(inbox==0) {
1439                                         /* if searchbox, click outside will cancel */
1440                                         if(data->searchbox)
1441                                                 data->cancel= data->escapecancel= 1;
1442                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1443                                         retval= WM_UI_HANDLER_BREAK;
1444                                 }
1445                         }
1446                         else if(inbox) {
1447                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1448                                 retval= WM_UI_HANDLER_BREAK;
1449                         }
1450                         break;
1451                 }
1452         }
1453
1454         if(event->val==KM_PRESS) {
1455                 switch (event->type) {
1456                         case VKEY:
1457                         case XKEY:
1458                         case CKEY:
1459                                 if(event->ctrl || event->oskey) {
1460                                         if(event->type == VKEY)
1461                                                 changed= ui_textedit_copypaste(but, data, 1, 0, 0);
1462                                         else if(event->type == CKEY)
1463                                                 changed= ui_textedit_copypaste(but, data, 0, 1, 0);
1464                                         else if(event->type == XKEY)
1465                                                 changed= ui_textedit_copypaste(but, data, 0, 0, 1);
1466
1467                                         retval= WM_UI_HANDLER_BREAK;
1468                                 }
1469                                 break;
1470                         case RIGHTARROWKEY:
1471                                 ui_textedit_move(but, data, 1, event->shift, event->ctrl);
1472                                 retval= WM_UI_HANDLER_BREAK;
1473                                 break;
1474                         case LEFTARROWKEY:
1475                                 ui_textedit_move(but, data, 0, event->shift, event->ctrl);
1476                                 retval= WM_UI_HANDLER_BREAK;
1477                                 break;
1478                         case DOWNARROWKEY:
1479                                 if(data->searchbox) {
1480                                         ui_searchbox_event(C, data->searchbox, but, event);
1481                                         break;
1482                                 }
1483                                 /* pass on purposedly */
1484                         case ENDKEY:
1485                                 ui_textedit_move_end(but, data, 1, event->shift);
1486                                 retval= WM_UI_HANDLER_BREAK;
1487                                 break;
1488                         case UPARROWKEY:
1489                                 if(data->searchbox) {
1490                                         ui_searchbox_event(C, data->searchbox, but, event);
1491                                         break;
1492                                 }
1493                                 /* pass on purposedly */
1494                         case HOMEKEY:
1495                                 ui_textedit_move_end(but, data, 0, event->shift);
1496                                 retval= WM_UI_HANDLER_BREAK;
1497                                 break;
1498                         case PADENTER:
1499                         case RETKEY:
1500                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1501                                 retval= WM_UI_HANDLER_BREAK;
1502                                 break;
1503                         case DELKEY:
1504                                 changed= ui_textedit_delete(but, data, 1, 0);
1505                                 retval= WM_UI_HANDLER_BREAK;
1506                                 break;
1507
1508                         case BACKSPACEKEY:
1509                                 changed= ui_textedit_delete(but, data, 0, event->shift);
1510                                 retval= WM_UI_HANDLER_BREAK;
1511                                 break;
1512                                 
1513                         case TABKEY:
1514                                 /* there is a key conflict here, we can't tab with autocomplete */
1515                                 if(but->autocomplete_func || data->searchbox) {
1516                                         changed= ui_textedit_autocomplete(C, but, data);
1517                                         retval= WM_UI_HANDLER_BREAK;
1518                                 }
1519                                 /* the hotkey here is not well defined, was G.qual so we check all */
1520                                 else if(event->shift || event->ctrl || event->alt || event->oskey) {
1521                                         ui_textedit_prev_but(block, but, data);
1522                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1523                                 }
1524                                 else {
1525                                         ui_textedit_next_but(block, but, data);
1526                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1527                                 }
1528                                 retval= WM_UI_HANDLER_BREAK;
1529                                 break;
1530                 }
1531
1532                 if(event->ascii && (retval == WM_UI_HANDLER_CONTINUE)) {
1533                         changed= ui_textedit_type_ascii(but, data, event->ascii);
1534                         retval= WM_UI_HANDLER_BREAK;
1535                 }
1536         }
1537
1538         if(changed) {
1539                 if(data->interactive) ui_apply_button(C, block, but, data, 1);
1540                 else ui_check_but(but);
1541                 
1542                 if(data->searchbox)
1543                         ui_searchbox_update(C, data->searchbox, but, 1); /* 1 = reset */
1544         }
1545
1546         if(changed || (retval == WM_UI_HANDLER_BREAK))
1547                 ED_region_tag_redraw(data->region);
1548 }
1549
1550 static void ui_do_but_textedit_select(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1551 {
1552         int mx, my, retval= WM_UI_HANDLER_CONTINUE;
1553
1554         switch(event->type) {
1555                 case MOUSEMOVE: {
1556                         mx= event->x;
1557                         my= event->y;
1558                         ui_window_to_block(data->region, block, &mx, &my);
1559
1560                         ui_textedit_set_cursor_select(but, data, mx);
1561                         retval= WM_UI_HANDLER_BREAK;
1562                         break;
1563                 }
1564                 case LEFTMOUSE:
1565                         if(event->val == 0)
1566                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1567                         retval= WM_UI_HANDLER_BREAK;
1568                         break;
1569         }
1570
1571         if(retval == WM_UI_HANDLER_BREAK) {
1572                 ui_check_but(but);
1573                 ED_region_tag_redraw(data->region);
1574         }
1575 }
1576
1577 /* ************* number editing for various types ************* */
1578
1579 static void ui_numedit_begin(uiBut *but, uiHandleButtonData *data)
1580 {
1581         float softrange, softmin, softmax;
1582
1583         if(but->type == BUT_CURVE) {
1584                 data->cumap= (CurveMapping*)but->poin;
1585                 but->editcumap= data->coba;
1586         }
1587         else if(but->type == BUT_COLORBAND) {
1588                 data->coba= (ColorBand*)but->poin;
1589                 but->editcoba= data->coba;
1590         }
1591         else if(ELEM3(but->type, BUT_NORMAL, HSVCUBE, HSVCIRCLE)) {
1592                 ui_get_but_vectorf(but, data->origvec);
1593                 VECCOPY(data->vec, data->origvec);
1594                 but->editvec= data->vec;
1595         }
1596         else {
1597                 data->startvalue= ui_get_but_val(but);
1598                 data->origvalue= data->startvalue;
1599                 data->value= data->origvalue;
1600                 but->editval= &data->value;
1601
1602                 softmin= but->softmin;
1603                 softmax= but->softmax;
1604                 softrange= softmax - softmin;
1605
1606                 data->dragfstart= (softrange == 0.0)? 0.0: (data->value - softmin)/softrange;
1607                 data->dragf= data->dragfstart;
1608         }
1609
1610         data->dragchange= 0;
1611         data->draglock= 1;
1612 }
1613
1614 static void ui_numedit_end(uiBut *but, uiHandleButtonData *data)
1615 {
1616         but->editval= NULL;
1617         but->editvec= NULL;
1618         but->editcoba= NULL;
1619         but->editcumap= NULL;
1620
1621         data->dragstartx= 0;
1622         data->draglastx= 0;
1623         data->dragchange= 0;
1624         data->dragcbd= NULL;
1625         data->dragsel= 0;
1626 }
1627
1628 static void ui_numedit_apply(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data)
1629 {
1630         if(data->interactive) ui_apply_button(C, block, but, data, 1);
1631         else ui_check_but(but);
1632
1633         ED_region_tag_redraw(data->region);
1634 }
1635
1636 /* ****************** menu opening for various types **************** */
1637
1638 static void ui_blockopen_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
1639 {
1640         uiBlockCreateFunc func= NULL;
1641         uiBlockHandleCreateFunc handlefunc= NULL;
1642         uiMenuCreateFunc menufunc= NULL;
1643         void *arg= NULL;
1644
1645         switch(but->type) {
1646                 case BLOCK:
1647                 case PULLDOWN:
1648                         if(but->menu_create_func) {
1649                                 menufunc= but->menu_create_func;
1650                                 arg= but->poin;
1651                         }
1652                         else {
1653                                 func= but->block_create_func;
1654                                 arg= but->poin?but->poin:but->func_argN;
1655                         }
1656                         break;
1657                 case MENU:
1658                         if(but->menu_create_func) {
1659                                 menufunc= but->menu_create_func;
1660                                 arg= but->poin;
1661                         }
1662                         else {
1663                                 data->origvalue= ui_get_but_val(but);
1664                                 data->value= data->origvalue;
1665                                 but->editval= &data->value;
1666
1667                                 handlefunc= ui_block_func_MENU;
1668                                 arg= but;
1669                         }
1670                         break;
1671                 case ICONROW:
1672                         handlefunc= ui_block_func_ICONROW;
1673                         arg= but;
1674                         break;
1675                 case ICONTEXTROW:
1676                         handlefunc= ui_block_func_ICONTEXTROW;
1677                         arg= but;
1678                         break;
1679                 case COL:
1680                         ui_get_but_vectorf(but, data->origvec);
1681                         VECCOPY(data->vec, data->origvec);
1682                         but->editvec= data->vec;
1683
1684                         handlefunc= ui_block_func_COL;
1685                         arg= but;
1686                         break;
1687         }
1688
1689         if(func || handlefunc) {
1690                 data->menu= ui_popup_block_create(C, data->region, but, func, handlefunc, arg);
1691                 if(but->block->handle)
1692                         data->menu->popup= but->block->handle->popup;
1693         }
1694         else if(menufunc) {
1695                 data->menu= ui_popup_menu_create(C, data->region, but, menufunc, arg);
1696                 if(but->block->handle)
1697                         data->menu->popup= but->block->handle->popup;
1698         }
1699
1700         /* this makes adjacent blocks auto open from now on */
1701         //if(but->block->auto_open==0) but->block->auto_open= 1;
1702 }
1703
1704 static void ui_blockopen_end(bContext *C, uiBut *but, uiHandleButtonData *data)
1705 {
1706         if(but) {
1707                 but->editval= NULL;
1708                 but->editvec= NULL;
1709
1710                 but->block->auto_open_last= PIL_check_seconds_timer();
1711         }
1712
1713         if(data->menu) {
1714                 ui_popup_block_free(C, data->menu);
1715                 data->menu= NULL;
1716         }
1717 }
1718
1719 /* ***************** events for different button types *************** */
1720
1721 static int ui_do_but_BUT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1722 {
1723         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1724                 if(event->type == LEFTMOUSE && event->val==KM_PRESS) {
1725                         button_activate_state(C, but, BUTTON_STATE_WAIT_RELEASE);
1726                         return WM_UI_HANDLER_BREAK;
1727                 }
1728                 else if(event->type == LEFTMOUSE && but->block->handle) {
1729                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1730                         return WM_UI_HANDLER_BREAK;
1731                 }
1732                 else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS) {
1733                         button_activate_state(C, but, BUTTON_STATE_WAIT_FLASH);
1734                         return WM_UI_HANDLER_BREAK;
1735                 }
1736         }
1737         else if(data->state == BUTTON_STATE_WAIT_RELEASE) {
1738                 if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
1739                         if(!(but->flag & UI_SELECT))
1740                                 data->cancel= 1;
1741                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1742                         return WM_UI_HANDLER_BREAK;
1743                 }
1744         }
1745
1746         return WM_UI_HANDLER_CONTINUE;
1747 }
1748
1749 static int ui_do_but_KEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1750 {
1751         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1752                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
1753                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
1754                         return WM_UI_HANDLER_BREAK;
1755                 }
1756         }
1757         else if(data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
1758                 if(event->type == MOUSEMOVE)
1759                         return WM_UI_HANDLER_CONTINUE;
1760
1761                 if(event->val==KM_PRESS) {
1762                         if(WM_key_event_string(event->type)[0])
1763                                 ui_set_but_val(but, event->type);
1764                         else
1765                                 data->cancel= 1;
1766
1767                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1768                 }
1769         }
1770
1771         return WM_UI_HANDLER_CONTINUE;
1772 }
1773
1774 static int ui_do_but_TEX(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1775 {
1776         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1777                 if(ELEM4(event->type, LEFTMOUSE, PADENTER, RETKEY, EVT_BUT_OPEN) && event->val==KM_PRESS) {
1778                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1779                         return WM_UI_HANDLER_BREAK;
1780                 }
1781         }
1782         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
1783                 ui_do_but_textedit(C, block, but, data, event);
1784                 return WM_UI_HANDLER_BREAK;
1785         }
1786         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
1787                 ui_do_but_textedit_select(C, block, but, data, event);
1788                 return WM_UI_HANDLER_BREAK;
1789         }
1790
1791         return WM_UI_HANDLER_CONTINUE;
1792 }
1793
1794 static int ui_do_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1795 {
1796         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1797                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
1798                         data->togdual= event->ctrl;
1799                         data->togonly= !event->shift;
1800                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1801                         return WM_UI_HANDLER_BREAK;
1802                 }
1803         }
1804         return WM_UI_HANDLER_CONTINUE;
1805 }
1806
1807 static int ui_do_but_EXIT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1808 {
1809         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1810                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
1811                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1812                         return WM_UI_HANDLER_BREAK;
1813                 }
1814         }
1815
1816         return WM_UI_HANDLER_CONTINUE;
1817 }
1818
1819 static int ui_numedit_but_NUM(uiBut *but, uiHandleButtonData *data, float fac, int snap, int mx)
1820 {
1821         float deler, tempf, softmin, softmax, softrange;
1822         int lvalue, temp, changed= 0;
1823         
1824         if(mx == data->draglastx)
1825                 return changed;
1826         
1827         /* drag-lock - prevent unwanted scroll adjustments */
1828         /* change value (now 3) to adjust threshold in pixels */
1829         if(data->draglock) {
1830                 if(abs(mx-data->dragstartx) <= 3)
1831                         return changed;
1832
1833                 data->draglock= 0;
1834                 data->dragstartx= mx;  /* ignore mouse movement within drag-lock */
1835         }
1836
1837         softmin= but->softmin;
1838         softmax= but->softmax;
1839         softrange= softmax - softmin;
1840
1841         deler= 500;
1842         if(!ui_is_but_float(but)) {
1843                 if((softrange)<100) deler= 200.0;
1844                 if((softrange)<25) deler= 50.0;
1845         }
1846         deler /= fac;
1847
1848         if(ui_is_but_float(but) && softrange > 11) {
1849                 /* non linear change in mouse input- good for high precicsion */
1850                 data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.002);
1851         } else if (!ui_is_but_float(but) && softrange > 129) { /* only scale large int buttons */
1852                 /* non linear change in mouse input- good for high precicsionm ints need less fine tuning */
1853                 data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.004);
1854         } else {
1855                 /*no scaling */
1856                 data->dragf+= ((float)(mx-data->draglastx))/deler ;
1857         }
1858
1859         if(data->dragf>1.0) data->dragf= 1.0;
1860         if(data->dragf<0.0) data->dragf= 0.0;
1861         data->draglastx= mx;
1862         tempf= (softmin + data->dragf*softrange);
1863         
1864         if(!ui_is_but_float(but)) {
1865                 temp= floor(tempf+.5);
1866                 
1867                 if(tempf==softmin || tempf==softmax);
1868                 else if(snap) {
1869                         if(snap == 2) temp= 100*(temp/100);
1870                         else temp= 10*(temp/10);
1871                 }
1872
1873                 CLAMP(temp, softmin, softmax);
1874                 lvalue= (int)data->value;
1875                         
1876                 if(temp != lvalue) {
1877                         data->dragchange= 1;
1878                         data->value= (double)temp;
1879                         changed= 1;
1880                 }
1881         }
1882         else {
1883                 temp= 0;
1884
1885                 if(snap) {
1886                         if(snap == 2) {
1887                                 if(tempf==softmin || tempf==softmax);
1888                                 else if(softrange < 2.10) tempf= 0.01*floor(100.0*tempf);
1889                                 else if(softrange < 21.0) tempf= 0.1*floor(10.0*tempf);
1890                                 else tempf= floor(tempf);
1891                         }
1892                         else {
1893                                 if(tempf==softmin || tempf==softmax);
1894                                 else if(softrange < 2.10) tempf= 0.1*floor(10*tempf);
1895                                 else if(softrange < 21.0) tempf= floor(tempf);
1896                                 else tempf= 10.0*floor(tempf/10.0);
1897                         }
1898                 }
1899
1900                 CLAMP(tempf, softmin, softmax);
1901
1902                 if(tempf != data->value) {
1903                         data->dragchange= 1;
1904                         data->value= tempf;
1905                         changed= 1;
1906                 }
1907         }
1908
1909         return changed;
1910 }
1911
1912 static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1913 {
1914         int mx, my, click= 0;
1915         int retval= WM_UI_HANDLER_CONTINUE;
1916         
1917         mx= event->x;
1918         my= event->y;
1919         ui_window_to_block(data->region, block, &mx, &my);
1920
1921         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1922                 /* XXX hardcoded keymap check.... */
1923                 if(event->type == WHEELDOWNMOUSE && event->alt) {
1924                         mx= but->x1;
1925                         click= 1;
1926                 }
1927                 else if(event->type == WHEELUPMOUSE && event->alt) {
1928                         mx= but->x2;
1929                         click= 1;
1930                 }
1931                 else if(event->val==KM_PRESS) {
1932                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
1933                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1934                                 retval= WM_UI_HANDLER_BREAK;
1935                         }
1936                         else if(event->type == LEFTMOUSE) {
1937                                 data->dragstartx= mx;
1938                                 data->draglastx= mx;
1939                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1940                                 retval= WM_UI_HANDLER_BREAK;
1941                         }
1942                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
1943                                 click= 1;
1944                 }
1945                 
1946         }
1947         else if(data->state == BUTTON_STATE_NUM_EDITING) {
1948                 if(event->type == ESCKEY) {
1949                         data->cancel= 1;
1950                         data->escapecancel= 1;
1951                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1952                 }
1953                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
1954                         if(data->dragchange)
1955                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1956                         else
1957                                 click= 1;
1958                 }
1959                 else if(event->type == MOUSEMOVE) {
1960                         float fac;
1961                         int snap;
1962
1963                         fac= 1.0f;
1964                         if(event->shift) fac /= 10.0f;
1965                         if(event->alt) fac /= 20.0f;
1966
1967                         if(event->custom == EVT_DATA_TABLET) {
1968                                 wmTabletData *wmtab= event->customdata;
1969
1970                                 /* de-sensitise based on tablet pressure */
1971                                 if (ELEM(wmtab->Active, DEV_STYLUS, DEV_ERASER))
1972                                         fac *= wmtab->Pressure;
1973                         }
1974                         
1975                         snap= (event->ctrl)? (event->shift)? 2: 1: 0;
1976
1977                         if(ui_numedit_but_NUM(but, data, fac, snap, mx))
1978                                 ui_numedit_apply(C, block, but, data);
1979                 }
1980                 retval= WM_UI_HANDLER_BREAK;
1981         }
1982         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
1983                 ui_do_but_textedit(C, block, but, data, event);
1984                 retval= WM_UI_HANDLER_BREAK;
1985         }
1986         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
1987                 ui_do_but_textedit_select(C, block, but, data, event);
1988                 retval= WM_UI_HANDLER_BREAK;
1989         }
1990         
1991         if(click) {
1992                 /* we can click on the side arrows to increment/decrement,
1993                  * or click inside to edit the value directly */
1994                 float tempf, softmin, softmax;
1995                 int temp;
1996
1997                 softmin= but->softmin;
1998                 softmax= but->softmax;
1999
2000                 if(!ui_is_but_float(but)) {
2001                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2002                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2003
2004                                 temp= (int)data->value - 1;
2005                                 if(temp>=softmin && temp<=softmax)
2006                                         data->value= (double)temp;
2007                                 else
2008                                         data->cancel= 1;
2009
2010                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2011                         }
2012                         else if(mx > (but->x1 + (2*(but->x2 - but->x1)/3) + 3)) {
2013                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2014
2015                                 temp= (int)data->value + 1;
2016                                 if(temp>=softmin && temp<=softmax)
2017                                         data->value= (double)temp;
2018                                 else
2019                                         data->cancel= 1;
2020
2021                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2022                         }
2023                         else
2024                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2025                 }
2026                 else {
2027                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2028                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2029
2030                                 tempf= data->value - 0.01*but->a1;
2031                                 if (tempf < softmin) tempf = softmin;
2032                                 data->value= tempf;
2033
2034                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2035                         }
2036                         else if(mx > but->x1 + (2*((but->x2 - but->x1)/3) + 3)) {
2037                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2038
2039                                 tempf= data->value + 0.01*but->a1;
2040                                 if (tempf > softmax) tempf = softmax;
2041                                 data->value= tempf;
2042
2043                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2044                         }
2045                         else
2046                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2047                 }
2048
2049                 retval= WM_UI_HANDLER_BREAK;
2050         }
2051         
2052         return retval;
2053 }
2054
2055 static int ui_numedit_but_SLI(uiBut *but, uiHandleButtonData *data, int shift, int ctrl, int mx)
2056 {
2057         float deler, f, tempf, softmin, softmax, softrange;
2058         int temp, lvalue, changed= 0;
2059
2060         softmin= but->softmin;
2061         softmax= but->softmax;
2062         softrange= softmax - softmin;
2063
2064         if(but->type==NUMSLI) deler= ((but->x2-but->x1) - 5.0*but->aspect);
2065         else if(but->type==HSVSLI) deler= ((but->x2-but->x1)/2 - 5.0*but->aspect);
2066         else if(but->type==SCROLL) {
2067                 int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2068                 float size= (horizontal)? (but->x2-but->x1): -(but->y2-but->y1);
2069                 deler= size*(but->softmax - but->softmin)/(but->softmax - but->softmin + but->a1);
2070         }
2071         else deler= (but->x2-but->x1- 5.0*but->aspect);
2072
2073         f= (float)(mx-data->dragstartx)/deler + data->dragfstart;
2074         
2075         if(shift)
2076                 f= (f-data->dragfstart)/10.0 + data->dragfstart;
2077
2078         CLAMP(f, 0.0, 1.0);
2079         tempf= softmin + f*softrange;
2080         temp= floor(tempf+.5);
2081
2082         if(ctrl) {
2083                 if(tempf==softmin || tempf==softmax);
2084                 else if(ui_is_but_float(but)) {
2085
2086                         if(shift) {
2087                                 if(tempf==softmin || tempf==softmax);
2088                                 else if(softmax-softmin < 2.10) tempf= 0.01*floor(100.0*tempf);
2089                                 else if(softmax-softmin < 21.0) tempf= 0.1*floor(10.0*tempf);
2090                                 else tempf= floor(tempf);
2091                         }
2092                         else {
2093                                 if(softmax-softmin < 2.10) tempf= 0.1*floor(10*tempf);
2094                                 else if(softmax-softmin < 21.0) tempf= floor(tempf);
2095                                 else tempf= 10.0*floor(tempf/10.0);
2096                         }
2097                 }
2098                 else {
2099                         temp= 10*(temp/10);
2100                         tempf= temp;
2101                 }
2102         }
2103
2104         if(!ui_is_but_float(but)) {
2105                 lvalue= floor(data->value+0.5);
2106
2107                 CLAMP(temp, softmin, softmax);
2108
2109                 if(temp != lvalue) {
2110                         data->value= temp;
2111                         data->dragchange= 1;
2112                         changed= 1;
2113                 }
2114         }
2115         else {
2116                 CLAMP(tempf, softmin, softmax);
2117
2118                 if(tempf != data->value) {
2119                         data->value= tempf;
2120                         data->dragchange= 1;
2121                         changed= 1;
2122                 }
2123         }
2124
2125         return changed;
2126 }
2127
2128 static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2129 {
2130         int mx, my, click= 0;
2131         int retval= WM_UI_HANDLER_CONTINUE;
2132
2133         mx= event->x;
2134         my= event->y;
2135         ui_window_to_block(data->region, block, &mx, &my);
2136
2137         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2138                 /* XXX hardcoded keymap check.... */
2139                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2140                         mx= but->x1;
2141                         click= 2;
2142                 }
2143                 else if(event->type == WHEELUPMOUSE && event->alt) {
2144                         mx= but->x2;
2145                         click= 2;
2146                 }
2147                 else if(event->val==KM_PRESS) {
2148                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2149                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2150                                 retval= WM_UI_HANDLER_BREAK;
2151                         }
2152                         else if(event->type == LEFTMOUSE) {
2153                                 data->dragstartx= mx;
2154                                 data->draglastx= mx;
2155                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2156                                 retval= WM_UI_HANDLER_BREAK;
2157                         }
2158                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2159                                 click= 1;
2160                 }
2161         }
2162         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2163                 if(event->type == ESCKEY) {
2164                         data->cancel= 1;
2165                         data->escapecancel= 1;
2166                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2167                 }
2168                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2169                         if(data->dragchange)
2170                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2171                         else
2172                                 click= 1;
2173                 }
2174                 else if(event->type == MOUSEMOVE) {
2175                         if(ui_numedit_but_SLI(but, data, event->shift, event->ctrl, mx))
2176                                 ui_numedit_apply(C, block, but, data);
2177                 }
2178                 retval= WM_UI_HANDLER_BREAK;
2179         }
2180         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2181                 ui_do_but_textedit(C, block, but, data, event);
2182                 retval= WM_UI_HANDLER_BREAK;
2183         }
2184         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2185                 ui_do_but_textedit_select(C, block, but, data, event);
2186                 retval= WM_UI_HANDLER_BREAK;
2187         }
2188
2189         if(click) {
2190                 if (click==2) {
2191                         /* nudge slider to the left or right */
2192                         float f, tempf, softmin, softmax, softrange;
2193                         int temp;
2194                         
2195                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2196                         
2197                         softmin= but->softmin;
2198                         softmax= but->softmax;
2199                         softrange= softmax - softmin;
2200                         
2201                         tempf= data->value;
2202                         temp= (int)data->value;
2203                         
2204                         if(but->type==SLI) f= (float)(mx-but->x1)/(but->x2-but->x1);
2205                         else f= (float)(mx- but->x1)/(but->x2-but->x1);
2206                         
2207                         f= softmin + f*softrange;
2208                         
2209                         if(!ui_is_but_float(but)) {
2210                                 if(f<temp) temp--;
2211                                 else temp++;
2212                                 
2213                                 if(temp>=softmin && temp<=softmax)
2214                                         data->value= temp;
2215                                 else
2216                                         data->cancel= 1;
2217                         } 
2218                         else {
2219                                 if(f<tempf) tempf-=.01;
2220                                 else tempf+=.01;
2221                                 
2222                                 if(tempf>=softmin && tempf<=softmax)
2223                                         data->value= tempf;
2224                                 else
2225                                         data->cancel= 1;
2226                         }
2227                         
2228                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2229                         retval= WM_UI_HANDLER_BREAK;
2230                 }
2231                 else {
2232                         /* edit the value directly */
2233                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2234                         retval= WM_UI_HANDLER_BREAK;
2235                 }
2236         }
2237         
2238         return retval;
2239 }
2240
2241 static int ui_do_but_SCROLL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2242 {
2243         int mx, my, click= 0;
2244         int retval= WM_UI_HANDLER_CONTINUE;
2245         int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2246         
2247         mx= event->x;
2248         my= event->y;
2249         ui_window_to_block(data->region, block, &mx, &my);
2250
2251         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2252                 if(event->val==KM_PRESS) {
2253                         if(event->type == LEFTMOUSE) {
2254                                 if(horizontal) {
2255                                         data->dragstartx= mx;
2256                                         data->draglastx= mx;
2257                                 }
2258                                 else {
2259                                         data->dragstartx= my;
2260                                         data->draglastx= my;
2261                                 }
2262                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2263                                 retval= WM_UI_HANDLER_BREAK;
2264                         }
2265                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2266                                 click= 1;
2267                 }
2268         }
2269         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2270                 if(event->type == ESCKEY) {
2271                         data->cancel= 1;
2272                         data->escapecancel= 1;
2273                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2274                 }
2275                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2276                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2277                 }
2278                 else if(event->type == MOUSEMOVE) {
2279                         if(ui_numedit_but_SLI(but, data, 0, 0, (horizontal)? mx: my))
2280                                 ui_numedit_apply(C, block, but, data);
2281                 }
2282
2283                 retval= WM_UI_HANDLER_BREAK;
2284         }
2285         
2286         return retval;
2287 }
2288
2289 static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2290 {
2291         
2292         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2293                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2294                         button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
2295                         return WM_UI_HANDLER_BREAK;
2296                 }
2297                 else if(ELEM3(but->type, MENU, ICONROW, ICONTEXTROW)) {
2298                         
2299                         if(event->type == WHEELDOWNMOUSE && 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                         else if(event->type == WHEELUPMOUSE && event->alt) {
2306                                 data->value= ui_step_name_menu(but, 1);
2307                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2308                                 ui_apply_button(C, but->block, but, data, 1);
2309                                 return WM_UI_HANDLER_BREAK;
2310                         }
2311                 }
2312                 else if(but->type==COL) {
2313                         if( ELEM(event->type, WHEELDOWNMOUSE, WHEELUPMOUSE) && event->alt) {
2314                                 float col[3];
2315                                 
2316                                 ui_get_but_vectorf(but, col);
2317                                 rgb_to_hsv(col[0], col[1], col[2], but->hsv, but->hsv+1, but->hsv+2);
2318
2319                                 if(event->type==WHEELDOWNMOUSE)
2320                                         but->hsv[2]= CLAMPIS(but->hsv[2]-0.05f, 0.0f, 1.0f);
2321                                 else
2322                                         but->hsv[2]= CLAMPIS(but->hsv[2]+0.05f, 0.0f, 1.0f);
2323                                 
2324                                 hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], data->vec, data->vec+1, data->vec+2);
2325                                 
2326                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2327                                 ui_apply_button(C, but->block, but, data, 1);
2328                                 return WM_UI_HANDLER_BREAK;
2329                         }
2330                 }
2331         }
2332
2333         return WM_UI_HANDLER_CONTINUE;
2334 }
2335
2336 static int ui_numedit_but_NORMAL(uiBut *but, uiHandleButtonData *data, int mx, int my)
2337 {
2338         float dx, dy, rad, radsq, mrad, *fp;
2339         int mdx, mdy, changed= 1;
2340         
2341         /* button is presumed square */
2342         /* if mouse moves outside of sphere, it does negative normal */
2343
2344         fp= data->origvec;
2345         rad= (but->x2 - but->x1);
2346         radsq= rad*rad;
2347         
2348         if(fp[2]>0.0f) {
2349                 mdx= (rad*fp[0]);
2350                 mdy= (rad*fp[1]);
2351         }
2352         else if(fp[2]> -1.0f) {
2353                 mrad= rad/sqrt(fp[0]*fp[0] + fp[1]*fp[1]);
2354                 
2355                 mdx= 2.0f*mrad*fp[0] - (rad*fp[0]);
2356                 mdy= 2.0f*mrad*fp[1] - (rad*fp[1]);
2357         }
2358         else mdx= mdy= 0;
2359         
2360         dx= (float)(mx+mdx-data->dragstartx);
2361         dy= (float)(my+mdy-data->dragstarty);
2362
2363         fp= data->vec;
2364         mrad= dx*dx+dy*dy;
2365         if(mrad < radsq) {      /* inner circle */
2366                 fp[0]= dx;
2367                 fp[1]= dy;
2368                 fp[2]= sqrt( radsq-dx*dx-dy*dy );
2369         }
2370         else {  /* outer circle */
2371                 
2372                 mrad= rad/sqrt(mrad);   // veclen
2373                 
2374                 dx*= (2.0f*mrad - 1.0f);
2375                 dy*= (2.0f*mrad - 1.0f);
2376                 
2377                 mrad= dx*dx+dy*dy;
2378                 if(mrad < radsq) {
2379                         fp[0]= dx;
2380                         fp[1]= dy;
2381                         fp[2]= -sqrt( radsq-dx*dx-dy*dy );
2382                 }
2383         }
2384         Normalize(fp);
2385
2386         data->draglastx= mx;
2387         data->draglasty= my;
2388
2389         return changed;
2390 }
2391
2392 static int ui_do_but_NORMAL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2393 {
2394         int mx, my;
2395
2396         mx= event->x;
2397         my= event->y;
2398         ui_window_to_block(data->region, block, &mx, &my);
2399
2400         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2401                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2402                         data->dragstartx= mx;
2403                         data->dragstarty= my;
2404                         data->draglastx= mx;
2405                         data->draglasty= my;
2406                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2407
2408                         /* also do drag the first time */
2409                         if(ui_numedit_but_NORMAL(but, data, mx, my))
2410                                 ui_numedit_apply(C, block, but, data);
2411                         
2412                         return WM_UI_HANDLER_BREAK;
2413                 }
2414         }
2415         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2416                 if(event->type == MOUSEMOVE) {
2417                         if(mx!=data->draglastx || my!=data->draglasty) {
2418                                 if(ui_numedit_but_NORMAL(but, data, mx, my))
2419                                         ui_numedit_apply(C, block, but, data);
2420                         }
2421                 }
2422                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2423                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2424
2425                 return WM_UI_HANDLER_BREAK;
2426         }
2427         
2428         return WM_UI_HANDLER_CONTINUE;
2429 }
2430
2431 static int ui_numedit_but_HSVCUBE(uiBut *but, uiHandleButtonData *data, int mx, int my)
2432 {
2433         float x, y;
2434         int changed= 1;
2435
2436         /* relative position within box */
2437         x= ((float)mx-but->x1)/(but->x2-but->x1);
2438         y= ((float)my-but->y1)/(but->y2-but->y1);
2439         CLAMP(x, 0.0, 1.0);
2440         CLAMP(y, 0.0, 1.0);
2441         
2442         if(but->a1==0) {
2443                 but->hsv[0]= x; 
2444                 but->hsv[2]= y; 
2445         }
2446         else if(but->a1==1) {
2447                 but->hsv[0]= x;                                 
2448                 but->hsv[1]= y;                                 
2449         }
2450         else if(but->a1==2) {
2451                 but->hsv[2]= x; 
2452                 but->hsv[1]= y; 
2453         }
2454         else if(but->a1==3) {
2455                 but->hsv[0]= x; 
2456         }
2457         else
2458                 but->hsv[2]= y; 
2459
2460         ui_set_but_hsv(but);    // converts to rgb
2461         
2462         // update button values and strings
2463         ui_update_block_buts_hsv(but->block, but->hsv);
2464
2465         data->draglastx= mx;
2466         data->draglasty= my;
2467
2468         return changed;
2469 }
2470
2471 static int ui_do_but_HSVCUBE(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2472 {
2473         int mx, my;
2474
2475         mx= event->x;
2476         my= event->y;
2477         ui_window_to_block(data->region, block, &mx, &my);
2478
2479         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2480                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2481                         data->dragstartx= mx;
2482                         data->dragstarty= my;
2483                         data->draglastx= mx;
2484                         data->draglasty= my;
2485                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2486
2487                         /* also do drag the first time */
2488                         if(ui_numedit_but_HSVCUBE(but, data, mx, my))
2489                                 ui_numedit_apply(C, block, but, data);
2490                         
2491                         return WM_UI_HANDLER_BREAK;
2492                 }
2493         }
2494         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2495                 if(event->type == MOUSEMOVE) {
2496                         if(mx!=data->draglastx || my!=data->draglasty) {
2497                                 if(ui_numedit_but_HSVCUBE(but, data, mx, my))
2498                                         ui_numedit_apply(C, block, but, data);
2499                         }
2500                 }
2501                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2502                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2503                 
2504                 return WM_UI_HANDLER_BREAK;
2505         }
2506
2507         return WM_UI_HANDLER_CONTINUE;
2508 }
2509
2510 static int ui_numedit_but_HSVCIRCLE(uiBut *but, uiHandleButtonData *data, int mx, int my)
2511 {
2512         rcti rect;
2513         int changed= 1;
2514
2515         rect.xmin= but->x1; rect.xmax= but->x2;
2516         rect.ymin= but->y1; rect.ymax= but->y2;
2517         
2518         ui_hsvcircle_vals_from_pos(but->hsv, but->hsv+1, &rect, (float)mx, (float)my);
2519         
2520         ui_set_but_hsv(but);    // converts to rgb
2521         
2522         // update button values and strings
2523         // XXX ui_update_block_buts_hsv(but->block, but->hsv);
2524         
2525         data->draglastx= mx;
2526         data->draglasty= my;
2527         
2528         return changed;
2529 }
2530
2531
2532 static int ui_do_but_HSVCIRCLE(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2533 {
2534         int mx, my;
2535         
2536         mx= event->x;
2537         my= event->y;
2538         ui_window_to_block(data->region, block, &mx, &my);
2539         
2540         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2541                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2542                         data->dragstartx= mx;
2543                         data->dragstarty= my;
2544                         data->draglastx= mx;
2545                         data->draglasty= my;
2546                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2547                         
2548                         /* also do drag the first time */
2549                         if(ui_numedit_but_HSVCIRCLE(but, data, mx, my))
2550                                 ui_numedit_apply(C, block, but, data);
2551                         
2552                         return WM_UI_HANDLER_BREAK;
2553                 }
2554         }
2555         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2556                 /* XXX hardcoded keymap check.... */
2557                 if(event->type == WHEELDOWNMOUSE) {
2558                         but->hsv[2]= CLAMPIS(but->hsv[2]-0.05f, 0.0f, 1.0f);
2559                         ui_set_but_hsv(but);    // converts to rgb
2560                         ui_numedit_apply(C, block, but, data);
2561                 }
2562                 else if(event->type == WHEELUPMOUSE) {
2563                         but->hsv[2]= CLAMPIS(but->hsv[2]+0.05f, 0.0f, 1.0f);
2564                         ui_set_but_hsv(but);    // converts to rgb
2565                         ui_numedit_apply(C, block, but, data);
2566                 }
2567                 else if(event->type == MOUSEMOVE) {
2568                         if(mx!=data->draglastx || my!=data->draglasty) {
2569                                 if(ui_numedit_but_HSVCIRCLE(but, data, mx, my))
2570                                         ui_numedit_apply(C, block, but, data);
2571                         }
2572                 }
2573                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2574                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2575                 
2576                 return WM_UI_HANDLER_BREAK;
2577         }
2578         
2579         return WM_UI_HANDLER_CONTINUE;
2580 }
2581
2582
2583 static int verg_colorband(const void *a1, const void *a2)
2584 {
2585         const CBData *x1=a1, *x2=a2;
2586         
2587         if( x1->pos > x2->pos ) return 1;
2588         else if( x1->pos < x2->pos) return -1;
2589         return WM_UI_HANDLER_CONTINUE;
2590 }
2591
2592 static void ui_colorband_update(ColorBand *coba)
2593 {
2594         int a;
2595         
2596         if(coba->tot<2) return;
2597         
2598         for(a=0; a<coba->tot; a++) coba->data[a].cur= a;
2599                 qsort(coba->data, coba->tot, sizeof(CBData), verg_colorband);
2600         for(a=0; a<coba->tot; a++) {
2601                 if(coba->data[a].cur==coba->cur) {
2602                         coba->cur= a;
2603                         break;
2604                 }
2605         }
2606 }
2607
2608 static int ui_numedit_but_COLORBAND(uiBut *but, uiHandleButtonData *data, int mx)
2609 {
2610         float dx;
2611         int changed= 0;
2612
2613         if(data->draglastx == mx)
2614                 return changed;
2615
2616         dx= ((float)(mx - data->draglastx))/(but->x2-but->x1);
2617         data->dragcbd->pos += dx;
2618         CLAMP(data->dragcbd->pos, 0.0, 1.0);
2619         
2620         ui_colorband_update(data->coba);
2621         data->dragcbd= data->coba->data + data->coba->cur;      /* because qsort */
2622         
2623         data->draglastx= mx;
2624         changed= 1;
2625
2626         return changed;
2627 }
2628
2629 static int ui_do_but_COLORBAND(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2630 {
2631         ColorBand *coba;
2632         CBData *cbd;
2633         int mx, my, a, xco, mindist= 12;
2634
2635         mx= event->x;
2636         my= event->y;
2637         ui_window_to_block(data->region, block, &mx, &my);
2638
2639         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2640                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2641                         coba= (ColorBand*)but->poin;
2642
2643                         if(event->ctrl) {
2644                                 /* insert new key on mouse location */
2645                                 if(coba->tot < MAXCOLORBAND-1) {
2646                                         float pos= ((float)(mx - but->x1))/(but->x2-but->x1);
2647                                         float col[4];
2648                                         
2649                                         do_colorband(coba, pos, col);   /* executes it */
2650                                         
2651                                         coba->tot++;
2652                                         coba->cur= coba->tot-1;
2653                                         
2654                                         coba->data[coba->cur].r= col[0];
2655                                         coba->data[coba->cur].g= col[1];
2656                                         coba->data[coba->cur].b= col[2];
2657                                         coba->data[coba->cur].a= col[3];
2658                                         coba->data[coba->cur].pos= pos;
2659
2660                                         ui_colorband_update(coba);
2661                                 }
2662
2663                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2664                         }
2665                         else {
2666                                 data->dragstartx= mx;
2667                                 data->dragstarty= my;
2668                                 data->draglastx= mx;
2669                                 data->draglasty= my;
2670
2671                                 /* activate new key when mouse is close */
2672                                 for(a=0, cbd= coba->data; a<coba->tot; a++, cbd++) {
2673                                         xco= but->x1 + (cbd->pos*(but->x2-but->x1));
2674                                         xco= ABS(xco-mx);
2675                                         if(a==coba->cur) xco+= 5; // selected one disadvantage
2676                                         if(xco<mindist) {
2677                                                 coba->cur= a;
2678                                                 mindist= xco;
2679                                         }
2680                                 }
2681                 
2682                                 data->dragcbd= coba->data + coba->cur;
2683                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2684                         }
2685
2686                         return WM_UI_HANDLER_BREAK;
2687                 }
2688         }
2689         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2690                 if(event->type == MOUSEMOVE) {
2691                         if(mx!=data->draglastx || my!=data->draglasty) {
2692                                 if(ui_numedit_but_COLORBAND(but, data, mx))
2693                                         ui_numedit_apply(C, block, but, data);
2694                         }
2695                 }
2696                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2697                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2698                 
2699                 return WM_UI_HANDLER_BREAK;
2700         }
2701
2702         return WM_UI_HANDLER_CONTINUE;
2703 }
2704
2705 static int ui_numedit_but_CURVE(uiBut *but, uiHandleButtonData *data, int snap, int mx, int my)
2706 {
2707         CurveMapping *cumap= data->cumap;
2708         CurveMap *cuma= cumap->cm+cumap->cur;
2709         CurveMapPoint *cmp= cuma->curve;
2710         float fx, fy, zoomx, zoomy, offsx, offsy;
2711         int a, changed= 0;
2712
2713         zoomx= (but->x2-but->x1)/(cumap->curr.xmax-cumap->curr.xmin);
2714         zoomy= (but->y2-but->y1)/(cumap->curr.ymax-cumap->curr.ymin);
2715         offsx= cumap->curr.xmin;
2716         offsy= cumap->curr.ymin;
2717
2718         if(data->dragsel != -1) {
2719                 int moved_point= 0;             /* for ctrl grid, can't use orig coords because of sorting */
2720                 
2721                 fx= (mx-data->draglastx)/zoomx;
2722                 fy= (my-data->draglasty)/zoomy;
2723                 for(a=0; a<cuma->totpoint; a++) {
2724                         if(cmp[a].flag & SELECT) {
2725                                 float origx= cmp[a].x, origy= cmp[a].y;
2726                                 cmp[a].x+= fx;
2727                                 cmp[a].y+= fy;
2728                                 if(snap) {
2729                                         cmp[a].x= 0.125f*floor(0.5f + 8.0f*cmp[a].x);
2730                                         cmp[a].y= 0.125f*floor(0.5f + 8.0f*cmp[a].y);
2731                                 }
2732                                 if(cmp[a].x!=origx || cmp[a].y!=origy)
2733                                         moved_point= 1;
2734                         }
2735                 }
2736
2737                 curvemapping_changed(cumap, 0); /* no remove doubles */
2738                 
2739                 if(moved_point) {
2740                         data->draglastx= mx;
2741                         data->draglasty= my;
2742                         changed= 1;
2743                 }
2744
2745                 data->dragchange= 1; /* mark for selection */
2746         }
2747         else {
2748                 fx= (mx-data->draglastx)/zoomx;
2749                 fy= (my-data->draglasty)/zoomy;
2750                 
2751                 /* clamp for clip */
2752                 if(cumap->flag & CUMA_DO_CLIP) {
2753                         if(cumap->curr.xmin-fx < cumap->clipr.xmin)
2754                                 fx= cumap->curr.xmin - cumap->clipr.xmin;
2755                         else if(cumap->curr.xmax-fx > cumap->clipr.xmax)
2756                                 fx= cumap->curr.xmax - cumap->clipr.xmax;
2757                         if(cumap->curr.ymin-fy < cumap->clipr.ymin)
2758                                 fy= cumap->curr.ymin - cumap->clipr.ymin;
2759                         else if(cumap->curr.ymax-fy > cumap->clipr.ymax)
2760                                 fy= cumap->curr.ymax - cumap->clipr.ymax;
2761                 }                               
2762
2763                 cumap->curr.xmin-=fx;
2764                 cumap->curr.ymin-=fy;
2765                 cumap->curr.xmax-=fx;
2766                 cumap->curr.ymax-=fy;
2767                 
2768                 data->draglastx= mx;
2769                 data->draglasty= my;
2770
2771                 changed= 1;
2772         }
2773
2774         return changed;
2775 }
2776
2777 static int ui_do_but_CURVE(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2778 {
2779         int mx, my, a, changed= 0;
2780
2781         mx= event->x;
2782         my= event->y;
2783         ui_window_to_block(data->region, block, &mx, &my);
2784
2785         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2786                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2787                         CurveMapping *cumap= (CurveMapping*)but->poin;
2788                         CurveMap *cuma= cumap->cm+cumap->cur;
2789                         CurveMapPoint *cmp= cuma->curve;
2790                         float fx, fy, zoomx, zoomy, offsx, offsy;
2791                         float dist, mindist= 200.0f; // 14 pixels radius
2792                         int sel= -1;
2793
2794                         zoomx= (but->x2-but->x1)/(cumap->curr.xmax-cumap->curr.xmin);
2795                         zoomy= (but->y2-but->y1)/(cumap->curr.ymax-cumap->curr.ymin);
2796                         offsx= cumap->curr.xmin;
2797                         offsy= cumap->curr.ymin;
2798
2799                         if(event->ctrl) {
2800                                 fx= ((float)my - but->x1)/zoomx + offsx;
2801                                 fy= ((float)my - but->y1)/zoomy + offsy;
2802                                 
2803                                 curvemap_insert(cuma, fx, fy);
2804                                 curvemapping_changed(cumap, 0);
2805                                 changed= 1;
2806                         }
2807
2808                         /* check for selecting of a point */
2809                         cmp= cuma->curve;       /* ctrl adds point, new malloc */
2810                         for(a=0; a<cuma->totpoint; a++) {
2811                                 fx= but->x1 + zoomx*(cmp[a].x-offsx);
2812                                 fy= but->y1 + zoomy*(cmp[a].y-offsy);
2813                                 dist= (fx-mx)*(fx-mx) + (fy-my)*(fy-my);
2814                                 if(dist < mindist) {
2815                                         sel= a;
2816                                         mindist= dist;
2817                                 }
2818                         }
2819
2820                         if (sel == -1) {
2821                                 /* if the click didn't select anything, check if it's clicked on the 
2822                                  * curve itself, and if so, add a point */
2823                                 fx= ((float)mx - but->x1)/zoomx + offsx;
2824                                 fy= ((float)my - but->y1)/zoomy + offsy;
2825                                 
2826                                 cmp= cuma->table;
2827
2828                                 /* loop through the curve segment table and find what's near the mouse.
2829                                  * 0.05 is kinda arbitrary, but seems to be what works nicely. */
2830                                 for(a=0; a<=CM_TABLE; a++) {                    
2831                                         if ( ( fabs(fx - cmp[a].x) < (0.05) ) && ( fabs(fy - cmp[a].y) < (0.05) ) ) {
2832                                         
2833                                                 curvemap_insert(cuma, fx, fy);
2834                                                 curvemapping_changed(cumap, 0);
2835
2836                                                 changed= 1;
2837                                                 
2838                                                 /* reset cmp back to the curve points again, rather than drawing segments */            
2839                                                 cmp= cuma->curve;
2840                                                 
2841                                                 /* find newly added point and make it 'sel' */
2842                                                 for(a=0; a<cuma->totpoint; a++)
2843                                                         if(cmp[a].x == fx)
2844                                                                 sel = a;
2845                                                         
2846                                                 break;
2847                                         }
2848                                 }
2849                         }
2850
2851                         if(sel!= -1) {
2852                                 /* ok, we move a point */
2853                                 /* deselect all if this one is deselect. except if we hold shift */
2854                                 if(event->shift==0 && (cmp[sel].flag & SELECT)==0)
2855                                         for(a=0; a<cuma->totpoint; a++)
2856                                                 cmp[a].flag &= ~SELECT;
2857                                 cmp[sel].flag |= SELECT;
2858                         }
2859                         else {
2860                                 /* move the view */
2861                                 data->cancel= 1;
2862                         }
2863
2864                         data->dragsel= sel;
2865
2866                         data->dragstartx= mx;
2867                         data->dragstarty= my;
2868                         data->draglastx= mx;
2869                         data->draglasty= my;
2870
2871                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2872                         return WM_UI_HANDLER_BREAK;
2873                 }
2874         }
2875         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2876                 if(event->type == MOUSEMOVE) {
2877                         if(mx!=data->draglastx || my!=data->draglasty) {
2878                                 if(ui_numedit_but_CURVE(but, data, event->shift, mx, my))
2879                                         ui_numedit_apply(C, block, but, data);
2880                         }
2881                 }
2882                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS) {
2883                         if(data->dragsel != -1) {
2884                                 CurveMapping *cumap= data->cumap;
2885                                 CurveMap *cuma= cumap->cm+cumap->cur;
2886                                 CurveMapPoint *cmp= cuma->curve;
2887
2888                                 if(!data->dragchange) {
2889                                         /* deselect all, select one */
2890                                         if(event->shift==0) {
2891                                                 for(a=0; a<cuma->totpoint; a++)
2892                                                         cmp[a].flag &= ~SELECT;
2893                                                 cmp[data->dragsel].flag |= SELECT;
2894                                         }
2895                                 }
2896                                 else
2897                                         curvemapping_changed(cumap, 1); /* remove doubles */
2898                         }
2899
2900                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2901                 }
2902
2903                 return WM_UI_HANDLER_BREAK;
2904         }
2905
2906         return WM_UI_HANDLER_CONTINUE;
2907 }
2908
2909 #ifdef INTERNATIONAL
2910 static int ui_do_but_CHARTAB(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2911 {
2912         /* XXX 2.50 bad global and state access */
2913 #if 0
2914         float sx, sy, ex, ey;
2915         float width, height;
2916         float butw, buth;
2917         int mx, my, x, y, cs, che;
2918
2919         mx= event->x;
2920         my= event->y;
2921         ui_window_to_block(data->region, block, &mx, &my);
2922
2923         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2924                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2925                         /* Calculate the size of the button */
2926                         width = abs(but->x2 - but->x1);
2927                         height = abs(but->y2 - but->y1);
2928
2929                         butw = floor(width / 12);
2930                         buth = floor(height / 6);
2931
2932                         /* Initialize variables */
2933                         sx = but->x1;
2934                         ex = but->x1 + butw;
2935                         sy = but->y1 + height - buth;
2936                         ey = but->y1 + height;
2937
2938                         cs = G.charstart;
2939
2940                         /* And the character is */
2941                         x = (int) ((mx / butw) - 0.5);
2942                         y = (int) (6 - ((my / buth) - 0.5));
2943
2944                         che = cs + (y*12) + x;
2945
2946                         if(che > G.charmax)
2947                                 che = 0;
2948
2949                         if(G.obedit)
2950                         {
2951                                 do_textedit(0,0,che);
2952                         }
2953
2954                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2955                         return WM_UI_HANDLER_BREAK;
2956                 }
2957                 else if(ELEM(event->type, WHEELUPMOUSE, PAGEUPKEY)) {
2958                         for(but= block->buttons.first; but; but= but->next) {
2959