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